diff --git a/src/main/java/Car.java b/src/main/java/Car.java new file mode 100644 index 00000000..32576165 --- /dev/null +++ b/src/main/java/Car.java @@ -0,0 +1,45 @@ +import java.util.*; + +public class Car { + private String name; + private int location; + private static final Random rand = new Random(); + + public Car(String name) { + this.name = name; + this.location = 0; + validateName(name); + } + + private void validateName(String name) { + if (name.length() > 5) { + throw new IllegalArgumentException("자동차 이름은 5자 이하여야 함"); + } + } + + + public int move() { + int randomValue = rand.nextInt(10); + + if (randomValue >= 4) { + return location++; + } + return location; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public int getLocation() { + return location; + } + + public void setLocation(int location) { + this.location = location; + } +} diff --git a/src/main/java/CarRacingGame.java b/src/main/java/CarRacingGame.java new file mode 100644 index 00000000..3f985294 --- /dev/null +++ b/src/main/java/CarRacingGame.java @@ -0,0 +1,60 @@ +import java.util.List; +import java.util.ArrayList; +import java.util.stream.Collectors; + +public class CarRacingGame { + private final List cars; + private final int rounds; + + public CarRacingGame(List carNames, int rounds) { + this.cars = createCars(carNames); + this.rounds = rounds; + } + + private List createCars(List carNames) { + // 자동차 이름으로 car 객채 생성 + return carNames.stream() + .map(Car::new) + .collect(Collectors.toList()); // car 객체들을 리스트로 변환 + } + + public void race() { + for (int i = 0; i < rounds; i++) { + cars.forEach(Car::move); + printRace(); + } + } + +// 라운드가 진행될 때마다 자동차들의 위치 출력 + private void printRace() { + for (Car car : cars) { + System.out.println(car.getName() + " : " + "-".repeat(car.getLocation())); + } + System.out.println(); + } + +// 이동 거리가 가장 긴 자동차 리스트를 반환 + public List getWinners() { + int maxLocation = findMaxLocation(); + return findWinners(maxLocation); + } + +// 그 최대 이동 거리를 구하는 메소드 + private int findMaxLocation() { + return cars.stream() + .mapToInt(Car::getLocation) + .max() + .orElse(0); + } + +// 앞에서 구한 최대 이동 거리를 통해 우승 자동차 리스트를 뽑는 메소드 + private List findWinners(int maxLocation) { + return cars.stream() + .filter(car -> car.getLocation() == maxLocation) + .collect(Collectors.toList()); + } + + public List getCars() { + return cars; + } +} diff --git a/src/main/java/CarRacingGameApp.java b/src/main/java/CarRacingGameApp.java new file mode 100644 index 00000000..5f96b5e5 --- /dev/null +++ b/src/main/java/CarRacingGameApp.java @@ -0,0 +1,43 @@ +import java.util.Scanner; +import java.util.List; +import java.util.Arrays; +import java.util.stream.Collectors; + +public class CarRacingGameApp { + public static void main(String[] args) { + List carNames = getCarNames(); + int rounds = getRounds(); + + CarRacingGame game = new CarRacingGame(carNames, rounds); + game.race(); + + printWinners(game.getWinners()); + } + +// 자동차 이름 추출 + private static List getCarNames() { + System.out.println("경주할 자동차 이름을 입력하세요(이름은 쉼표(,)를 기준으로 구분)."); + Scanner scanner = new Scanner(System.in); + String input = scanner.nextLine(); + + // 쉼표를 기준으로 이름 추출 + return Arrays.stream(input.split(",")) + .map(String::trim) + .collect(Collectors.toList()); + } + +// 라운드 횟수 추출 + private static int getRounds() { + System.out.println("시도할 횟수는 몇 회인가요?"); + Scanner scanner = new Scanner(System.in); + return scanner.nextInt(); + } + +// CarRacingGame 클래스로부터 받아온 우승자 명단을 출력 + private static void printWinners(List winnerList) { + String winnerNames = winnerList.stream() + .map(Car::getName) + .collect(Collectors.joining(", ")); + System.out.println(winnerNames + "가 최종 우승했습니다."); + } +} diff --git a/src/test/java/CarRacingGameTest.java b/src/test/java/CarRacingGameTest.java new file mode 100644 index 00000000..d13d87d3 --- /dev/null +++ b/src/test/java/CarRacingGameTest.java @@ -0,0 +1,35 @@ +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; +import java.util.List; +import java.util.Arrays; + +public class CarRacingGameTest { + + @Test + void 자동차_경주_이동_테스트() { + Car car = new Car("포니"); + int initialLocation = car.getLocation(); + + for (int i = 0; i < 100; i++) { + car.move(); + } + +// 현재 위치값이 초기값 이상이면 테스트 통과 + assertTrue(car.getLocation() >= initialLocation); + } + + @Test + void 우승자_테스트() { + List carNames = Arrays.asList("쿠페", "다시아", "버기", "피코버스", "오토바이"); + CarRacingGame game = new CarRacingGame(carNames, 5); + game.race(); + + List winners = game.getWinners(); + int maxLocation = winners.get(0).getLocation(); // 우승자의 위치 저장 + +// 우승자들의 위치가 같은 지 확인 + for (Car winner : winners) { + assertEquals(maxLocation, winner.getLocation()); + } + } +} diff --git a/src/test/java/CarTest.java b/src/test/java/CarTest.java new file mode 100644 index 00000000..4d5f985f --- /dev/null +++ b/src/test/java/CarTest.java @@ -0,0 +1,37 @@ +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +public class CarTest { + + @Test + @DisplayName("자동차 움직임 기능 테스트") + public void moveTest() { + Car car = new Car("부릉부릉"); + int location = car.getLocation(); + + // 초기 위치 테스트 + Assertions.assertThat(location).isEqualTo(0); + + // 10번 랜덤 돌리기 + for (int i = 0; i < 10; i++) { + car.move(); + } + + // 자동차가 멈췄다면 위치가 변하지 않을 수 있음을 확인 + Assertions.assertThat(car.getLocation()).isBetween(location, location + 10); + } + + @Test + @DisplayName("작명 기능 테스트") + public void nameTest() { + Car car = new Car("꼬마자동차"); + + // 기본 이름 테스트 + Assertions.assertThat(car.getName()).isEqualTo("꼬마자동차"); + + // 이름 변경 테스트 + car.setName("UAZ"); + Assertions.assertThat(car.getName()).isEqualTo("UAZ"); + } +}