File tree 6 files changed +54
-32
lines changed
6 files changed +54
-32
lines changed Original file line number Diff line number Diff line change 46
46
--
47
47
-
48
48
--
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 이상의 값이여야만 한다. 같은 거요!
Original file line number Diff line number Diff line change 2
2
3
3
public class Car {
4
4
private static final Random random = new Random ();
5
- private static final int moveThreshold = 4 ;
5
+ private static final int MOVE_THRESHOLD = 4 ;
6
6
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
+
8
18
public int move () {
9
- if ( canMove ()) position ++ ;
10
- return position ;
19
+ int randomValue = random . nextInt ( 10 ) ;
20
+ return move ( randomValue ) ;
11
21
}
12
22
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 ;
15
26
}
16
27
28
+
17
29
}
Original file line number Diff line number Diff line change 1
- import java .util .ArrayList ;
2
1
import java .util .List ;
2
+ import java .util .stream .Collectors ;
3
+ import java .util .stream .IntStream ;
3
4
4
5
public class CarRace {
5
- private final List <Car > cars = new ArrayList <>();
6
6
private final int runCount ;
7
+ private final List <Car > cars ;
7
8
8
9
public CarRace (int carCount , int runCount ) {
9
- for (int i = 0 ; i < carCount ; i ++) {
10
- cars .add (new Car ());
11
- }
12
10
this .runCount = runCount ;
11
+ this .cars = IntStream .range (0 , carCount )
12
+ .mapToObj (i -> new Car ())
13
+ .collect (Collectors .toList ());
13
14
}
14
15
15
16
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 ());
21
20
}
22
21
23
22
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 ());
29
26
}
30
27
31
28
}
Original file line number Diff line number Diff line change @@ -6,6 +6,7 @@ public class InputView {
6
6
public int getCarCount () {
7
7
return validatePositiveInt (scanInt ("자동차 대수는 몇 대 인가요?" ));
8
8
}
9
+
9
10
public int getRunCount () {
10
11
return validatePositiveInt (scanInt ("시도할 회수는 몇 회 인가요?" ));
11
12
}
Original file line number Diff line number Diff line change 1
1
import java .util .List ;
2
2
3
3
public class ResultView {
4
+ private static final String PATTERN = "-" ;
4
5
5
6
public void print (List <List <Integer >> result ) {
6
7
System .out .println ();
7
8
System .out .println ("실행 결과" );
8
9
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 );
12
13
}
13
14
System .out .println ();
14
15
}
15
16
System .out .println ();
16
17
}
17
18
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 ));
23
21
}
24
22
}
Original file line number Diff line number Diff line change @@ -33,7 +33,7 @@ void printGuideMessage(int carNumber, int tryCount) {
33
33
}
34
34
35
35
@ ParameterizedTest
36
- @ ValueSource (strings = {"-3\n " ,"0\n " })
36
+ @ ValueSource (strings = {"-3\n " , "0\n " })
37
37
@ DisplayName ("자동차 대수는 양수여야한다." )
38
38
void throwIfCarCountPositive (String carCount ) {
39
39
System .setIn (new ByteArrayInputStream (carCount .getBytes ()));
@@ -45,7 +45,7 @@ void throwIfCarCountPositive(String carCount) {
45
45
}
46
46
47
47
@ ParameterizedTest
48
- @ ValueSource (strings = {"0\n " ,"-1\n " })
48
+ @ ValueSource (strings = {"0\n " , "-1\n " })
49
49
@ DisplayName ("시도할 회수는 양수여야한다." )
50
50
void throwIfRunCountPositive (String runCount ) {
51
51
System .setIn (new ByteArrayInputStream (runCount .getBytes ()));
You can’t perform that action at this time.
0 commit comments