diff --git a/src/main/java/Car.java b/src/main/java/Car.java new file mode 100644 index 00000000000..c693abc86b5 --- /dev/null +++ b/src/main/java/Car.java @@ -0,0 +1,30 @@ +public class Car { + private static final int STANDARD_VALUE = 4; + private int position; + + public Car(int position) { + this.position = position; + } + + public Car() { + this(0); + } + + public Car(Car car) { + this.position = car.position; + } + + public void moveBy(int value) { + if (value > STANDARD_VALUE) { + this.position++; + } + } + + public boolean isPosition(int value) { + return this.position == value; + } + + public int getPosition() { + return this.position; + } +} diff --git a/src/main/java/Cars.java b/src/main/java/Cars.java new file mode 100644 index 00000000000..3459fa763a5 --- /dev/null +++ b/src/main/java/Cars.java @@ -0,0 +1,23 @@ +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +public class Cars { + private final List cars; + + public Cars(int carNum) { + this.cars = Stream.generate(Car::new).limit(carNum).collect(Collectors.toList()); + } + + public Cars(Cars cars) { + this.cars = cars.cars.stream().map(Car::new).collect(Collectors.toList()); + } + + public void move() { + this.cars.forEach(car -> car.moveBy(RandomNumberUtil.getRandomNumberFromZeroToNine())); + } + + public List getCars() { + return this.cars; + } +} diff --git a/src/main/java/CarsSnapShots.java b/src/main/java/CarsSnapShots.java new file mode 100644 index 00000000000..700bf348752 --- /dev/null +++ b/src/main/java/CarsSnapShots.java @@ -0,0 +1,18 @@ +import java.util.ArrayList; +import java.util.List; + +public class CarsSnapShots { + private final List carsSnapShots; + + public CarsSnapShots() { + this.carsSnapShots = new ArrayList<>(); + } + + public void add(Cars cars) { + this.carsSnapShots.add(new Cars(cars)); + } + + public List getCars() { + return this.carsSnapShots; + } +} diff --git a/src/main/java/InputView.java b/src/main/java/InputView.java new file mode 100644 index 00000000000..e602f024ee6 --- /dev/null +++ b/src/main/java/InputView.java @@ -0,0 +1,18 @@ +import java.util.Scanner; + +public final class InputView { + + private InputView() {} + + public static int inputCarNum() { + final Scanner scanner = new Scanner(System.in); + System.out.println("자동차 대수는 몇 대 인가요?"); + return scanner.nextInt(); + } + + public static int inputTryNum() { + final Scanner scanner = new Scanner(System.in); + System.out.println("시도할 회수는 몇 회 인가요?"); + return scanner.nextInt(); + } +} diff --git a/src/main/java/Main.java b/src/main/java/Main.java new file mode 100644 index 00000000000..0b2993500a1 --- /dev/null +++ b/src/main/java/Main.java @@ -0,0 +1,13 @@ +public class Main { + public static void main(String[] args) { + final int carNum = InputView.inputCarNum(); + final int tryNum = InputView.inputTryNum(); + + final RacingManager racingManager = new RacingManager(carNum, tryNum); + + racingManager.play(); + + ResultView.printTitle(); + ResultView.printResult(racingManager.getCarsSnapShots()); + } +} diff --git a/src/main/java/RacingManager.java b/src/main/java/RacingManager.java new file mode 100644 index 00000000000..731dd8a0d8a --- /dev/null +++ b/src/main/java/RacingManager.java @@ -0,0 +1,25 @@ +public class RacingManager { + private final Cars cars; + private final CarsSnapShots carsSnapShots; + private final int tryNum; + + public RacingManager(int carNum, int tryNum) { + if (carNum <= 0 || tryNum <= 0) { + throw new IllegalArgumentException("자동차 대수와 시도 횟수는 0보다 커야 합니다."); + } + this.tryNum = tryNum; + this.cars = new Cars(carNum); + carsSnapShots = new CarsSnapShots(); + } + + public void play() { + for (int i = 0; i < tryNum; i++) { + this.cars.move(); + carsSnapShots.add(this.cars); + } + } + + public CarsSnapShots getCarsSnapShots() { + return this.carsSnapShots; + } +} diff --git a/src/main/java/RandomNumberUtil.java b/src/main/java/RandomNumberUtil.java new file mode 100644 index 00000000000..fd302496e98 --- /dev/null +++ b/src/main/java/RandomNumberUtil.java @@ -0,0 +1,7 @@ +public final class RandomNumberUtil { + private RandomNumberUtil() {} + + public static int getRandomNumberFromZeroToNine() { + return (int) (Math.random() * 10); + } +} diff --git a/src/main/java/ResultView.java b/src/main/java/ResultView.java new file mode 100644 index 00000000000..04b15fcd1fe --- /dev/null +++ b/src/main/java/ResultView.java @@ -0,0 +1,22 @@ +public final class ResultView { + + private ResultView() {} + + public static void printResult(final CarsSnapShots carsSnapShots) { + carsSnapShots.getCars().forEach(cars -> { + cars.getCars().forEach(car -> { + printCarPosition(car.getPosition()); + System.out.println(); + }); + System.out.println(); + }); + } + + public static void printTitle() { + System.out.println("실행 결과"); + } + + private static void printCarPosition(int position) { + System.out.print("-".repeat(position)); + } +} diff --git a/src/test/java/CarTest.java b/src/test/java/CarTest.java new file mode 100644 index 00000000000..22362c9e229 --- /dev/null +++ b/src/test/java/CarTest.java @@ -0,0 +1,22 @@ +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +public class CarTest { + @Test + @DisplayName("기준값 보다 크면 1칸 이동한다") + void moveBy_go() { + final Car car = new Car(); + car.moveBy(5); + assertThat(car.isPosition(1)).isTrue(); + } + + @Test + @DisplayName("기준값 보다 작으면 이동하지 않는다") + void moveBy_wait() { + final Car car = new Car(); + car.moveBy(4); + assertThat(car.isPosition(0)).isTrue(); + } +} diff --git a/src/test/java/RacingManagerTest.java b/src/test/java/RacingManagerTest.java new file mode 100644 index 00000000000..951b8e790a5 --- /dev/null +++ b/src/test/java/RacingManagerTest.java @@ -0,0 +1,23 @@ +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +class RacingManagerTest { + + @Test + @DisplayName("자동차 개수와 시도 횟수가 0보다 작을 때 예외를 던진다.") + void car() { + assertThrows(IllegalArgumentException.class, () -> { + new RacingManager(0, 5); + }); + + assertThrows(IllegalArgumentException.class, () -> { + new RacingManager(3, 0); + }); + + assertThrows(IllegalArgumentException.class, () -> { + new RacingManager(0, 0); + }); + } +} diff --git a/src/test/java/RandomNumberUtilTest.java b/src/test/java/RandomNumberUtilTest.java new file mode 100644 index 00000000000..d05965d1644 --- /dev/null +++ b/src/test/java/RandomNumberUtilTest.java @@ -0,0 +1,13 @@ +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +class RandomNumberUtilTest { + + @Test + @DisplayName("0부터 9까지의 랜덤 숫자를 반환한다.") + void getRandomNumberFromZeroToNine() { + assertThat(RandomNumberUtil.getRandomNumberFromZeroToNine()).isBetween(0, 9); + } +}