diff --git a/build.gradle b/build.gradle index 6282254c4ce..972bad86a1e 100644 --- a/build.gradle +++ b/build.gradle @@ -1,4 +1,6 @@ plugins { + id 'org.springframework.boot' version '2.5.4' + id 'io.spring.dependency-management' version '1.0.11.RELEASE' id 'java' } diff --git a/src/main/java/racing/controller/CarRacingController.java b/src/main/java/racing/controller/CarRacingController.java new file mode 100644 index 00000000000..31d74df7cf0 --- /dev/null +++ b/src/main/java/racing/controller/CarRacingController.java @@ -0,0 +1,28 @@ +package racing.controller; + +import racing.entity.Car; +import racing.service.Cars; +import racing.view.InputView; +import racing.view.ResultView; + +import java.util.List; + +public class CarRacingController { + + public static void main(String[] args) { + InputView inputView = new InputView(); + List cars = inputView.inputCar(); + int rounds = inputView.inputNumber(); + + Cars racingService = new Cars(cars); + ResultView resultView = new ResultView(); + + for (int i = 0; i < rounds; i++) { + racingService.moveCars(); + resultView.printResult(racingService.getCars()); + } + resultView.findWinners(racingService.findWinners()); + } +} + + diff --git a/src/main/java/racing/entity/Car.java b/src/main/java/racing/entity/Car.java new file mode 100644 index 00000000000..df9a924217c --- /dev/null +++ b/src/main/java/racing/entity/Car.java @@ -0,0 +1,40 @@ +package racing.entity; + +import java.util.Random; + +public class Car { + + private final String name; + protected int position; + private final Random random = new Random(); + + private static final int MOVE_THRESHOLD = 4; + private static final int RANDOM_RANGE = 10; + + public Car(String name) { + this.name = name; + this.position = 0; + } + + public String getName() { + return name; + } + + public int getPosition() { + return position; + } + + public void move() { + if (canMove()) { + position += 1; + } + } + + protected boolean canMove() { + int randomValue = random.nextInt(RANDOM_RANGE); + return randomValue >= MOVE_THRESHOLD; + } +} + + + diff --git a/src/main/java/racing/service/Cars.java b/src/main/java/racing/service/Cars.java new file mode 100644 index 00000000000..6756a3aaf3c --- /dev/null +++ b/src/main/java/racing/service/Cars.java @@ -0,0 +1,36 @@ +package racing.service; + +import racing.entity.Car; +import java.util.List; +import java.util.stream.Collectors; + +public class Cars { + + private final List cars; + + public Cars(List cars) { + this.cars = cars; + } + + public void moveCars() { + for (Car car : cars) { + car.move(); + } + } + + public List getCars() { + return cars; + } + + public List findWinners() { + int maxPosition = cars.stream() + .mapToInt(Car::getPosition) + .max() + .orElse(0); + + return cars.stream() + .filter(car -> car.getPosition() == maxPosition) + .collect(Collectors.toList()); + } +} + diff --git a/src/main/java/racing/view/InputView.java b/src/main/java/racing/view/InputView.java new file mode 100644 index 00000000000..3ed3cad3e92 --- /dev/null +++ b/src/main/java/racing/view/InputView.java @@ -0,0 +1,27 @@ +package racing.view; + +import racing.entity.Car; +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; + +public class InputView { + + private final Scanner scanner = new Scanner(System.in); + + public List inputCar() { + System.out.println("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)"); + String[] names = scanner.nextLine().split(","); + List cars = new ArrayList<>(); + for (String name : names) { + cars.add(new Car(name.trim())); + } + return cars; + } + + public int inputNumber() { + System.out.println("시도할 횟수를 입력하세요 : "); + return scanner.nextInt(); + } +} + diff --git a/src/main/java/racing/view/ResultView.java b/src/main/java/racing/view/ResultView.java new file mode 100644 index 00000000000..7dfd1ee3746 --- /dev/null +++ b/src/main/java/racing/view/ResultView.java @@ -0,0 +1,34 @@ +package racing.view; + +import racing.entity.Car; + +import java.util.List; +import java.util.stream.Collectors; + +public class ResultView { + public void printResult(List cars) { + StringBuilder result = new StringBuilder(); + cars.forEach(car -> result.append(buildCarResult(car)).append("\n")); + System.out.println(result.toString()); + } + + private String buildCarResult(Car car) { + return car.getName() + ": " + carPosition(car.getPosition()); + } + + private String carPosition(int position) { + StringBuilder stringBuilder = new StringBuilder(); + for (int i = 0; i < position; i++) { + stringBuilder.append("-"); + } + return stringBuilder.toString(); + } + + public void findWinners(List winners) { + String winnerNames = winners.stream() + .map(Car::getName) + .collect(Collectors.joining(", ")); + System.out.println("우승자는 " + winnerNames + " 입니다."); + } +} + diff --git a/src/test/java/racing/CarTest.java b/src/test/java/racing/CarTest.java new file mode 100644 index 00000000000..35d20eb540e --- /dev/null +++ b/src/test/java/racing/CarTest.java @@ -0,0 +1,41 @@ +package racing; + +import org.junit.jupiter.api.Test; +import racing.entity.Car; +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class CarTest { + + @Test + void carMoves() { + Car car = new Car("Car"); + car.move(); + assertEquals(1, car.getPosition()); + } + + @Test + void carDoesNotMove() { + Car car = new Car("CarzCar") { + @Override + public void move() { + if (!canMove()) { + position = 0; + } + } + }; + car.move(); + assertEquals(0, car.getPosition()); + } + + @Test + void carNameIsSetCorrectly() { + Car car = new Car("Car"); + assertEquals("Car", car.getName()); + } + + @Test + void carInitialPositionIsZero() { + Car car = new Car("Car"); + assertEquals(0, car.getPosition()); + } +} diff --git a/src/test/java/racing/CarsTest.java b/src/test/java/racing/CarsTest.java new file mode 100644 index 00000000000..b0cd5ed232a --- /dev/null +++ b/src/test/java/racing/CarsTest.java @@ -0,0 +1,35 @@ +package racing; + +import org.junit.jupiter.api.Test; +import racing.entity.Car; +import racing.service.Cars; +import java.util.Arrays; +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class CarsTest { + + @Test + void testMoveCars() { + Car car1 = new Car("Car1"); + Car car2 = new Car("Car2"); + Cars racingService = new Cars(Arrays.asList(car1, car2)); + + int initialPositionCar1 = car1.getPosition(); + int initialPositionCar2 = car2.getPosition(); + + racingService.moveCars(); + + assertEquals(calculateExpectedPosition(initialPositionCar1, car1.getPosition()), car1.getPosition()); + assertEquals(calculateExpectedPosition(initialPositionCar2, car2.getPosition()), car2.getPosition()); + } + + + private int calculateExpectedPosition(int initialPosition, int currentPosition) { + if (currentPosition > initialPosition) { + return initialPosition + 1; + } + return initialPosition; + } + +} +