Skip to content

Commit 3f46c26

Browse files
committed
리팩토링3
- Car, InputService 예외 검증 책임 분리 - getWinners 메서드 stream 사용 - Cars 오타 수정 - InputService 사용하지 않는 코드 삭제 - 변경사항에 맞게 테스트 코드 수정 및 추가
1 parent c945a0b commit 3f46c26

6 files changed

Lines changed: 38 additions & 66 deletions

File tree

src/main/java/carrace/domain/car/Car.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ public Car(String carName) {
1414
}
1515

1616
private void validateName(String carName) {
17-
if (carName == null || carName.length() > MAX_NAME_LENGTH) {
17+
if (carName == null || carName.isBlank()) {
18+
throw new IllegalArgumentException("자동 이름은 비어있을 수 없습니다.");
19+
}
20+
if (carName.length() > MAX_NAME_LENGTH) {
1821
throw new IllegalArgumentException("자동차 이름은 5글자를 초과할 수 없습니다.");
1922
}
2023
}

src/main/java/carrace/domain/car/Cars.java

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.util.List;
99
import java.util.Set;
1010

11+
1112
public class Cars {
1213

1314
private final List<Car> cars;
@@ -29,7 +30,7 @@ public static Cars from(String input) {
2930

3031
private void validate(List<Car> cars) {
3132
if (cars.isEmpty()) {
32-
throw new IllegalArgumentException("자동차는 최소 1대 이상이어여 합니다.");
33+
throw new IllegalArgumentException("자동차는 최소 1대 이상이어야 합니다.");
3334
}
3435
validateDuplicateNames(cars);
3536
}
@@ -48,20 +49,18 @@ public void moveAll(MoveCondition condition) {
4849
}
4950

5051
public List<Car> getWinners() {
51-
int max = 0;
52-
for (Car car : cars) {
53-
max = Math.max(max, car.getPosition());
54-
}
52+
int maxPosition = cars.stream()
53+
.mapToInt(Car::getPosition)
54+
.max()
55+
.orElse(0);
5556

56-
List<Car> winners = new ArrayList<>();
57-
for (Car car : cars) {
58-
if (car.getPosition() == max) {
59-
winners.add(car);
60-
}
61-
}
62-
return winners;
57+
return cars.stream()
58+
.filter(car -> car.getPosition() == maxPosition)
59+
.toList();
6360
}
6461

62+
63+
6564
public List<Car> asList() {
6665
return List.copyOf(cars);
6766
}

src/main/java/carrace/service/InputService.java

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,44 +16,28 @@ public String readCarNames() {
1616
return input;
1717
}
1818

19-
public int readCarCount() {
20-
int value = parseInt(
21-
inputView.readCarCount(),
22-
"자동차 대수는 숫자여야 합니다."
23-
);
24-
25-
if (value <= 0) {
26-
throw new IllegalArgumentException("자동차는 1대 이상이어야 합니다.");
27-
}
28-
return value;
29-
}
30-
3119
public int readRounds() {
32-
int value = parseInt(
33-
inputView.readMoveCount(),
34-
"시도 횟수는 숫자여야 합니다."
35-
);
20+
int value = parseInt(inputView.readMoveCount());
3621

3722
if (value <= 0) {
3823
throw new IllegalArgumentException("시도 횟수는 1 이상이어야 합니다.");
3924
}
4025
return value;
4126
}
4227

43-
private int parseInt(String input, String errorMessage) {
44-
try {
45-
return Integer.parseInt(input);
46-
} catch (NumberFormatException e) {
47-
throw new IllegalArgumentException(errorMessage);
48-
}
28+
private int parseInt(String input) {
29+
return Integer.parseInt(input);
4930
}
5031

5132
private void validateCarNames(String input) {
33+
if (input == null || input.isBlank()) {
34+
throw new IllegalArgumentException("자동차 이름을 입력하세요");
35+
}
36+
5237
String[] names = input.split(",");
5338
for (String name : names) {
54-
String trimmed = name.trim();
55-
if (trimmed.isEmpty() || trimmed.length() > 5) {
56-
throw new IllegalArgumentException("자동차 이름은 5자를 초과할 수 없습니다.");
39+
if (name.trim().isBlank()) {
40+
throw new IllegalArgumentException("자동차 이름은 공백일 수 없습니다.");
5741
}
5842
}
5943
}

src/main/java/carrace/view/CarRaceInputView.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@ public String readCarNames() {
1111
System.out.println("경주할 자동차 이름을 입력하세요(이름은 쉼표(,)를 기준으로 구분).");
1212
return scanner.nextLine();
1313
}
14-
public String readCarCount() {
15-
System.out.println("자동차 대수는 몇 대인가요?");
16-
return scanner.nextLine();
17-
}
1814

1915
public String readMoveCount() {
2016
System.out.println("시도할 횟수는 몇 회인가요?");

src/test/java/carrace/domain/car/CarTest.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import carrace.fixture.MoveConditions;
44
import org.junit.jupiter.api.DisplayName;
55
import org.junit.jupiter.api.Test;
6-
76
import static org.assertj.core.api.Assertions.assertThat;
87
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;
98

@@ -55,4 +54,18 @@ void name_too_long_throws() {
5554
.isInstanceOf(IllegalArgumentException.class);
5655
}
5756

57+
@Test
58+
@DisplayName("이름이 null 이면 예외")
59+
void name_null_throws() {
60+
assertThatThrownBy(() -> new Car(null))
61+
.isInstanceOf(IllegalArgumentException.class);
62+
}
63+
64+
@Test
65+
@DisplayName("이름이 공백이면 예외")
66+
void name_blank_throws() {
67+
assertThatThrownBy(() -> new Car(" "))
68+
.isInstanceOf(IllegalArgumentException.class);
69+
}
70+
5871
}

src/test/java/carrace/service/InputServiceTest.java

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,36 +8,13 @@
88

99
class InputServiceTest {
1010

11-
@DisplayName("자동차 수가 1이면 정상 입력 (경계값)")
12-
@Test
13-
void carCountOneIsValid() {
14-
InputService service = new InputService(new StubInputView("1", "5"));
15-
assertEquals(1, service.readCarCount());
16-
}
17-
18-
@DisplayName("자동차 수가 0이면 예외 발생 (경계값)")
19-
@Test
20-
void carCountZeroThrows() {
21-
InputService service = new InputService(new StubInputView("0", "5"));
22-
assertThrows(IllegalArgumentException.class, service::readCarCount);
23-
}
24-
2511
@DisplayName("시도 횟수가 음수면 예외 발생 (경계값)")
2612
@Test
2713
void roundsNegativeThrows() {
2814
InputService service = new InputService(new StubInputView("3", "-1"));
2915
assertThrows(IllegalArgumentException.class, service::readRounds);
3016
}
3117

32-
@Test
33-
@DisplayName("자동차 이름이 5글자 초과이면 예외 발생")
34-
void carNameTooLongThrows() {
35-
StubInputView inputView = new StubInputView("TOOLONG","3");
36-
InputService service = new InputService(inputView);
37-
38-
assertThrows(IllegalArgumentException.class, service::readCarNames);
39-
}
40-
4118
@Test
4219
@DisplayName("자동차 이름 공백시 예외 발생")
4320
void carNameBlankThrows() {

0 commit comments

Comments
 (0)