diff --git a/README.md b/README.md index 41054889277..ec9ffecb723 100644 --- a/README.md +++ b/README.md @@ -18,4 +18,12 @@ - [x] 자동차 전진하거나 멈추는 메서드 구현 - [x] 전진 조건 : 0에서 9 사이에서 random 값을 구한 후 random 값이 4이상일 경우 - [x] 입력받은 이동 수만큼 경기 진행하는 메서드 구현 -- [x] 경기 진행 수와 자동차 상태 표시 +- [x] 경기 진행 수와 자동차 상태 표시 +- [x] 자동차에 name 받기 +- [x] 자동차에 name 변수 추가 +- [x] 자동차 name 쉼표(,)를 기준으로 구분 +- [x] 자동차 객체에 name 포함한 생성자 만들기 +- [x] 전진하는 자동차를 출력할 때 자동차 이름을 같이 출력 +- [x] 자동차에 name 받을 때 5자를 초과시 예외 발생 +- [x] 우승자 표시 추가 + diff --git a/src/main/java/racing/RacingCarApplication.java b/src/main/java/racing/RacingCarApplication.java index 89c0a0e88f7..f96786f6345 100644 --- a/src/main/java/racing/RacingCarApplication.java +++ b/src/main/java/racing/RacingCarApplication.java @@ -1,5 +1,6 @@ package racing; +import java.util.List; import racing.domain.Racing; import racing.view.InputView; import racing.view.ResultView; @@ -7,10 +8,10 @@ public class RacingCarApplication { public static void main(String[] args) { - int carCount = InputView.getNumberOfCars(); + String carNamesRaw = InputView.getNumberOfCars(); int roundCount = InputView.getNumberOfRounds(); - Racing racing = new Racing(carCount); + Racing racing = new Racing(StringToArray(carNamesRaw)); System.out.println("실행 결과"); @@ -18,7 +19,12 @@ public static void main(String[] args) { racing.simulateRace(); ResultView.printRaceResult(racing.getCars()); } - } + List maxPosition = racing.findWinners(); + System.out.println(String.join(", ", maxPosition) + "가 최종 우승했습니다."); + } + private static String[] StringToArray(String carNamesRaw) { + return carNamesRaw.split(","); + } } diff --git a/src/main/java/racing/domain/Car.java b/src/main/java/racing/domain/Car.java index 7073d30234e..0c8341ec9c1 100644 --- a/src/main/java/racing/domain/Car.java +++ b/src/main/java/racing/domain/Car.java @@ -3,18 +3,45 @@ public class Car { private static final int MOVE_THRESHOLD = 4; + private static final int MAX_NAME_LENGTH = 5; + private static final int MIN_RANDOM_VALUE = 0; + private static final int MAX_RANDOM_VALUE = 9; private int position; + private String name; + + public Car(String name) { + this(name, 0); + } + + public Car(String name, int position) { + validateName(name); + this.name = name; + this.position = position; + } + + private void validateName(String name) { + if (name.length() > MAX_NAME_LENGTH) { + throw new IllegalArgumentException("자동차 이름은 " + MAX_NAME_LENGTH + "자를 초과할 수 없습니다."); + } + } + public int getPosition() { return position; } + public String getName() { + return name; + } + public void driveOrStop(int randomNumber) { + if (randomNumber < MIN_RANDOM_VALUE || randomNumber > MAX_RANDOM_VALUE) { + throw new IllegalArgumentException("랜덤 값은 0에서 9 사이여야 합니다."); + } + if (randomNumber >= MOVE_THRESHOLD) { position++; } } - - } diff --git a/src/main/java/racing/domain/Racing.java b/src/main/java/racing/domain/Racing.java index e3171e35e6d..b0f252e954d 100644 --- a/src/main/java/racing/domain/Racing.java +++ b/src/main/java/racing/domain/Racing.java @@ -2,21 +2,25 @@ import java.util.ArrayList; import java.util.List; +import java.util.stream.Collectors; import racing.RandomGenerator; public class Racing { - List cars = new ArrayList<>(); + private final List cars = new ArrayList<>(); - public Racing(int carCount) { - generateCars(carCount); + public Racing(String[] carNames) { + generateCars(carNames); } - private List generateCars(int carCount) { - for (int i = 0; i < carCount; i++) { - cars.add(new Car()); - } + public Racing(List cars) { + this.cars.addAll(cars); + } + private List generateCars(String[] carNames) { + for (String carName : carNames) { + cars.add(new Car(carName)); + } return cars; } @@ -30,5 +34,24 @@ public void simulateRace() { } } + public List findWinners() { + int maxPosition = calculateMaxPosition(); + + return findMaxPosition(maxPosition); + } + + private int calculateMaxPosition() { + return cars.stream() + .mapToInt(Car::getPosition) + .max() + .getAsInt(); + } + + private List findMaxPosition(int maxPosition) { + return cars.stream() + .filter(car -> car.getPosition() == maxPosition) + .map(Car::getName) + .collect(Collectors.toList()); + } } diff --git a/src/main/java/racing/view/InputView.java b/src/main/java/racing/view/InputView.java index 1c07bcbf872..a3519e7ceea 100644 --- a/src/main/java/racing/view/InputView.java +++ b/src/main/java/racing/view/InputView.java @@ -6,9 +6,9 @@ public class InputView { private static final Scanner scanner = new Scanner(System.in); - public static int getNumberOfCars() { - System.out.print("자동차 대수는 몇 대 인가요?"); - return scanner.nextInt(); + public static String getNumberOfCars() { + System.out.print("경주할 자동차 이름을 입력하세요(이름은 쉼표(,)를 기준으로 구분)."); + return scanner.nextLine(); } public static int getNumberOfRounds() { diff --git a/src/main/java/racing/view/ResultView.java b/src/main/java/racing/view/ResultView.java index e985d1cf7e9..8fe51842dda 100644 --- a/src/main/java/racing/view/ResultView.java +++ b/src/main/java/racing/view/ResultView.java @@ -7,7 +7,7 @@ public class ResultView { public static void printRaceResult(List cars) { for (Car car : cars) { - System.out.println("-".repeat(car.getPosition())); + System.out.println(car.getName() + ":" + "-".repeat(car.getPosition())); } System.out.println(); } diff --git a/src/test/java/racing/CarTest.java b/src/test/java/racing/CarTest.java deleted file mode 100644 index 6fcad22fa2d..00000000000 --- a/src/test/java/racing/CarTest.java +++ /dev/null @@ -1,24 +0,0 @@ -package racing; - -import static org.assertj.core.api.Assertions.assertThat; - -import org.junit.jupiter.api.Test; -import racing.domain.Car; - -public class CarTest { - - Car car = new Car(); - - @Test - void 자동차_멈춤() { - car.driveOrStop(3); - assertThat(car.getPosition()).isEqualTo(0); - } - - @Test - void 자동차_전진() { - car.driveOrStop(4); - assertThat(car.getPosition()).isEqualTo(1); - } -} - diff --git a/src/test/java/racing/RacingTest.java b/src/test/java/racing/RacingTest.java deleted file mode 100644 index d7a1fff9000..00000000000 --- a/src/test/java/racing/RacingTest.java +++ /dev/null @@ -1,16 +0,0 @@ -package racing; - -import static org.assertj.core.api.Assertions.assertThat; - -import org.junit.jupiter.api.Test; -import racing.domain.Racing; - -class RacingTest { - - @Test - void 자동차_객체_생성() { - assertThat(new Racing(3).getCars().size()).isEqualTo(3); - - } - -} \ No newline at end of file diff --git a/src/test/java/racing/domain/CarTest.java b/src/test/java/racing/domain/CarTest.java new file mode 100644 index 00000000000..494745727a1 --- /dev/null +++ b/src/test/java/racing/domain/CarTest.java @@ -0,0 +1,46 @@ +package racing.domain; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import org.junit.jupiter.api.Test; +import racing.domain.Car; + +public class CarTest { + + Car car = new Car("pobi"); + + @Test + void 자동차_멈춤() { + car.driveOrStop(3); + assertThat(car.getPosition()).isEqualTo(0); + } + + @Test + void 자동차_전진() { + car.driveOrStop(4); + assertThat(car.getPosition()).isEqualTo(1); + } + + @Test + void 랜덤값_0미만_예외_발생() { + assertThatThrownBy(() -> car.driveOrStop(-1)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("랜덤 값은 0에서 9 사이여야 합니다."); + } + + @Test + void 랜덤값_최대초과_예외_발생() { + assertThatThrownBy(() -> car.driveOrStop(10)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("랜덤 값은 0에서 9 사이여야 합니다."); + } + + @Test + void 자동차_이름_5글자_초과() { + assertThatThrownBy(() -> new Car("longname")) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("자동차 이름은 5자를 초과할 수 없습니다."); + } +} + diff --git a/src/test/java/racing/domain/RacingTest.java b/src/test/java/racing/domain/RacingTest.java new file mode 100644 index 00000000000..8d4fdd368f0 --- /dev/null +++ b/src/test/java/racing/domain/RacingTest.java @@ -0,0 +1,35 @@ +package racing.domain; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.ArrayList; +import java.util.List; +import org.junit.jupiter.api.Test; +import racing.domain.Car; +import racing.domain.Racing; + +class RacingTest { + + @Test + void 자동차_객체_생성() { + String[] carNames = {"pobi", "crong", "honux"}; + assertThat(new Racing(carNames).getCars().size()).isEqualTo(3); + + } + + @Test + void 제일_높은_position_가진_자동차_구하기() { + Car pobi = new Car("pobi", 3); + Car jason = new Car("jason", 1); + Car brown = new Car("brown", 2); + + List cars = List.of(pobi, jason, brown); + + Racing racing = new Racing(cars); + + List winners = racing.findWinners(); + + assertThat(winners).containsExactlyInAnyOrder("pobi"); + } + +} \ No newline at end of file