Skip to content

Commit 0a0dc82

Browse files
committed
refactor: Car 객체 테스트 가능하도록 수정
1 parent 5f3ffd5 commit 0a0dc82

File tree

6 files changed

+54
-32
lines changed

6 files changed

+54
-32
lines changed

README.md

+15-1
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,18 @@
4646
--
4747
-
4848
--
49-
```
49+
```
50+
51+
---
52+
## 1차 코멘트
53+
- [x] View가 상태를 가지고 있는게 맞을까요?
54+
- [ ] Scanner에 static을 붙여 메모리를 아껴보면 어떨까요 ㅎㅎ
55+
- [x] 객체지향 설계: Car라는 도메인이 있고 그 Car가 Position을 가지고 있고, move하는 행위도 스스로 하는거 아닐까요?
56+
- [x] 상수를 통해 해당 코드의 의미를 나타내보시면 어떨까요?
57+
58+
## 2차 코멘트
59+
- [x] String.repeat() 사용
60+
- [ ] [일급컬렉션 적용](https://jojoldu.tistory.com/412) `private final List<Car> cars = new ArrayList<>();`
61+
- [ ] [테스트 하기 좋은 코드로 인터페이스를 통해 전략패턴](https://tecoble.techcourse.co.kr/post/2020-05-17-appropriate_method_for_test_by_interface/)
62+
- canMove에서 랜덤값을 처리하기 떄문에 실제 테스트를 하기가 어려운 문제가 있습니다. 이유는 position이 1이 될지, 0이 될지 알수가 없기 떄문이죠.
63+
- [ ] position의 유효성은 따로 없을까요? 예를들어 0 이상의 값이여야만 한다. 같은 거요!

src/main/java/Car.java

+18-6
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,28 @@
22

33
public class Car {
44
private static final Random random = new Random();
5-
private static final int moveThreshold = 4;
5+
private static final int MOVE_THRESHOLD = 4;
66
private int position;
7-
7+
8+
9+
// public int move() {
10+
// if (canMove()) position++;
11+
// return position;
12+
// }
13+
14+
// private boolean canMove() {
15+
// return random.nextInt(10) >= MOVE_THRESHOLD;
16+
// }
17+
818
public int move() {
9-
if (canMove()) position++;
10-
return position;
19+
int randomValue = random.nextInt(10);
20+
return move(randomValue);
1121
}
1222

13-
private boolean canMove() {
14-
return random.nextInt(10) >= moveThreshold;
23+
public int move(int random) {
24+
if(random >= MOVE_THRESHOLD) position++;
25+
return position;
1526
}
1627

28+
1729
}

src/main/java/CarRace.java

+12-15
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,28 @@
1-
import java.util.ArrayList;
21
import java.util.List;
2+
import java.util.stream.Collectors;
3+
import java.util.stream.IntStream;
34

45
public class CarRace {
5-
private final List<Car> cars = new ArrayList<>();
66
private final int runCount;
7+
private final List<Car> cars;
78

89
public CarRace(int carCount, int runCount) {
9-
for (int i = 0; i < carCount; i++) {
10-
cars.add(new Car());
11-
}
1210
this.runCount = runCount;
11+
this.cars = IntStream.range(0, carCount)
12+
.mapToObj(i -> new Car())
13+
.collect(Collectors.toList());
1314
}
1415

1516
public List<List<Integer>> run() {
16-
List<List<Integer>> result = new ArrayList<>(runCount);
17-
for (int i = 0; i < runCount; i++) {
18-
result.add(runOnce());
19-
}
20-
return result;
17+
return IntStream.range(0, runCount)
18+
.mapToObj(i -> runOnce())
19+
.collect(Collectors.toList());
2120
}
2221

2322
private List<Integer> runOnce() {
24-
List<Integer> result = new ArrayList<>(cars.size());
25-
for (Car car : cars) {
26-
result.add(car.move());
27-
}
28-
return result;
23+
return cars.stream()
24+
.map(Car::move)
25+
.collect(Collectors.toList());
2926
}
3027

3128
}

src/main/java/InputView.java

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ public class InputView {
66
public int getCarCount() {
77
return validatePositiveInt(scanInt("자동차 대수는 몇 대 인가요?"));
88
}
9+
910
public int getRunCount() {
1011
return validatePositiveInt(scanInt("시도할 회수는 몇 회 인가요?"));
1112
}

src/main/java/ResultView.java

+6-8
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
11
import java.util.List;
22

33
public class ResultView {
4+
private static final String PATTERN = "-";
45

56
public void print(List<List<Integer>> result) {
67
System.out.println();
78
System.out.println("실행 결과");
89

9-
for (List<Integer> cars : result) {
10-
for(int car : cars){
11-
printDash(car);
10+
for (List<Integer> runResult : result) {
11+
for (int carPosition : runResult) {
12+
printPattern(carPosition);
1213
}
1314
System.out.println();
1415
}
1516
System.out.println();
1617
}
1718

18-
private void printDash(int count) {
19-
while(count-->0){
20-
System.out.print("-");
21-
}
22-
System.out.println();
19+
private void printPattern(int count) {
20+
System.out.println(PATTERN.repeat(count));
2321
}
2422
}

src/test/java/CarRaceTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ void printGuideMessage(int carNumber, int tryCount) {
3333
}
3434

3535
@ParameterizedTest
36-
@ValueSource(strings = {"-3\n","0\n"})
36+
@ValueSource(strings = {"-3\n", "0\n"})
3737
@DisplayName("자동차 대수는 양수여야한다.")
3838
void throwIfCarCountPositive(String carCount) {
3939
System.setIn(new ByteArrayInputStream(carCount.getBytes()));
@@ -45,7 +45,7 @@ void throwIfCarCountPositive(String carCount) {
4545
}
4646

4747
@ParameterizedTest
48-
@ValueSource(strings = {"0\n","-1\n"})
48+
@ValueSource(strings = {"0\n", "-1\n"})
4949
@DisplayName("시도할 회수는 양수여야한다.")
5050
void throwIfRunCountPositive(String runCount) {
5151
System.setIn(new ByteArrayInputStream(runCount.getBytes()));

0 commit comments

Comments
 (0)