File tree 11 files changed +214
-0
lines changed
11 files changed +214
-0
lines changed Original file line number Diff line number Diff line change
1
+ public class Car {
2
+ private static final int STANDARD_VALUE = 4 ;
3
+ private int position ;
4
+
5
+ public Car (int position ) {
6
+ this .position = position ;
7
+ }
8
+
9
+ public Car () {
10
+ this (0 );
11
+ }
12
+
13
+ public Car (Car car ) {
14
+ this .position = car .position ;
15
+ }
16
+
17
+ public void moveBy (int value ) {
18
+ if (value > STANDARD_VALUE ) {
19
+ this .position ++;
20
+ }
21
+ }
22
+
23
+ public boolean isPosition (int value ) {
24
+ return this .position == value ;
25
+ }
26
+
27
+ public int getPosition () {
28
+ return this .position ;
29
+ }
30
+ }
Original file line number Diff line number Diff line change
1
+ import java .util .List ;
2
+ import java .util .stream .Collectors ;
3
+ import java .util .stream .Stream ;
4
+
5
+ public class Cars {
6
+ private final List <Car > cars ;
7
+
8
+ public Cars (int carNum ) {
9
+ this .cars = Stream .generate (Car ::new ).limit (carNum ).collect (Collectors .toList ());
10
+ }
11
+
12
+ public Cars (Cars cars ) {
13
+ this .cars = cars .cars .stream ().map (Car ::new ).collect (Collectors .toList ());
14
+ }
15
+
16
+ public void move () {
17
+ this .cars .forEach (car -> car .moveBy (RandomNumberUtil .getRandomNumberFromZeroToNine ()));
18
+ }
19
+
20
+ public List <Car > getCars () {
21
+ return this .cars ;
22
+ }
23
+ }
Original file line number Diff line number Diff line change
1
+ import java .util .ArrayList ;
2
+ import java .util .List ;
3
+
4
+ public class CarsSnapShots {
5
+ private final List <Cars > carsSnapShots ;
6
+
7
+ public CarsSnapShots () {
8
+ this .carsSnapShots = new ArrayList <>();
9
+ }
10
+
11
+ public void add (Cars cars ) {
12
+ this .carsSnapShots .add (new Cars (cars ));
13
+ }
14
+
15
+ public List <Cars > getCars () {
16
+ return this .carsSnapShots ;
17
+ }
18
+ }
Original file line number Diff line number Diff line change
1
+ import java .util .Scanner ;
2
+
3
+ public final class InputView {
4
+
5
+ private InputView () {}
6
+
7
+ public static int inputCarNum () {
8
+ final Scanner scanner = new Scanner (System .in );
9
+ System .out .println ("자동차 대수는 몇 대 인가요?" );
10
+ return scanner .nextInt ();
11
+ }
12
+
13
+ public static int inputTryNum () {
14
+ final Scanner scanner = new Scanner (System .in );
15
+ System .out .println ("시도할 회수는 몇 회 인가요?" );
16
+ return scanner .nextInt ();
17
+ }
18
+ }
Original file line number Diff line number Diff line change
1
+ public class Main {
2
+ public static void main (String [] args ) {
3
+ final int carNum = InputView .inputCarNum ();
4
+ final int tryNum = InputView .inputTryNum ();
5
+
6
+ final RacingManager racingManager = new RacingManager (carNum , tryNum );
7
+
8
+ racingManager .play ();
9
+
10
+ ResultView .printTitle ();
11
+ ResultView .printResult (racingManager .getCarsSnapShots ());
12
+ }
13
+ }
Original file line number Diff line number Diff line change
1
+ public class RacingManager {
2
+ private final Cars cars ;
3
+ private final CarsSnapShots carsSnapShots ;
4
+ private final int tryNum ;
5
+
6
+ public RacingManager (int carNum , int tryNum ) {
7
+ if (carNum <= 0 || tryNum <= 0 ) {
8
+ throw new IllegalArgumentException ("자동차 대수와 시도 횟수는 0보다 커야 합니다." );
9
+ }
10
+ this .tryNum = tryNum ;
11
+ this .cars = new Cars (carNum );
12
+ carsSnapShots = new CarsSnapShots ();
13
+ }
14
+
15
+ public void play () {
16
+ for (int i = 0 ; i < tryNum ; i ++) {
17
+ this .cars .move ();
18
+ carsSnapShots .add (this .cars );
19
+ }
20
+ }
21
+
22
+ public CarsSnapShots getCarsSnapShots () {
23
+ return this .carsSnapShots ;
24
+ }
25
+ }
Original file line number Diff line number Diff line change
1
+ public final class RandomNumberUtil {
2
+ private RandomNumberUtil () {}
3
+
4
+ public static int getRandomNumberFromZeroToNine () {
5
+ return (int ) (Math .random () * 10 );
6
+ }
7
+ }
Original file line number Diff line number Diff line change
1
+ public final class ResultView {
2
+
3
+ private ResultView () {}
4
+
5
+ public static void printResult (final CarsSnapShots carsSnapShots ) {
6
+ carsSnapShots .getCars ().forEach (cars -> {
7
+ cars .getCars ().forEach (car -> {
8
+ printCarPosition (car .getPosition ());
9
+ System .out .println ();
10
+ });
11
+ System .out .println ();
12
+ });
13
+ }
14
+
15
+ public static void printTitle () {
16
+ System .out .println ("실행 결과" );
17
+ }
18
+
19
+ private static void printCarPosition (int position ) {
20
+ System .out .print ("-" .repeat (position ));
21
+ }
22
+ }
Original file line number Diff line number Diff line change
1
+ import static org .assertj .core .api .Assertions .assertThat ;
2
+
3
+ import org .junit .jupiter .api .DisplayName ;
4
+ import org .junit .jupiter .api .Test ;
5
+
6
+ public class CarTest {
7
+ @ Test
8
+ @ DisplayName ("기준값 보다 크면 1칸 이동한다" )
9
+ void moveBy_go () {
10
+ final Car car = new Car ();
11
+ car .moveBy (5 );
12
+ assertThat (car .isPosition (1 )).isTrue ();
13
+ }
14
+
15
+ @ Test
16
+ @ DisplayName ("기준값 보다 작으면 이동하지 않는다" )
17
+ void moveBy_wait () {
18
+ final Car car = new Car ();
19
+ car .moveBy (4 );
20
+ assertThat (car .isPosition (0 )).isTrue ();
21
+ }
22
+ }
Original file line number Diff line number Diff line change
1
+ import static org .junit .jupiter .api .Assertions .*;
2
+
3
+ import org .junit .jupiter .api .DisplayName ;
4
+ import org .junit .jupiter .api .Test ;
5
+
6
+ class RacingManagerTest {
7
+
8
+ @ Test
9
+ @ DisplayName ("자동차 개수와 시도 횟수가 0보다 작을 때 예외를 던진다." )
10
+ void car () {
11
+ assertThrows (IllegalArgumentException .class , () -> {
12
+ new RacingManager (0 , 5 );
13
+ });
14
+
15
+ assertThrows (IllegalArgumentException .class , () -> {
16
+ new RacingManager (3 , 0 );
17
+ });
18
+
19
+ assertThrows (IllegalArgumentException .class , () -> {
20
+ new RacingManager (0 , 0 );
21
+ });
22
+ }
23
+ }
Original file line number Diff line number Diff line change
1
+ import static org .assertj .core .api .AssertionsForClassTypes .assertThat ;
2
+
3
+ import org .junit .jupiter .api .DisplayName ;
4
+ import org .junit .jupiter .api .Test ;
5
+
6
+ class RandomNumberUtilTest {
7
+
8
+ @ Test
9
+ @ DisplayName ("0부터 9까지의 랜덤 숫자를 반환한다." )
10
+ void getRandomNumberFromZeroToNine () {
11
+ assertThat (RandomNumberUtil .getRandomNumberFromZeroToNine ()).isBetween (0 , 9 );
12
+ }
13
+ }
You can’t perform that action at this time.
0 commit comments