diff --git a/README.md b/README.md index d0286c859f..d660e2b0ab 100644 --- a/README.md +++ b/README.md @@ -1 +1,7 @@ # java-racingcar-precourse + +- [x] 입력 검증기능 +- [x] 레이스 턴기능 +- [x] 레이스 실행&출력기능 +- [x] 선두주자 index 찾는기능 +- [x] 선두주자 문자열로 표현해 가져오는기능 \ No newline at end of file diff --git a/src/main/java/racingcar/Application.java b/src/main/java/racingcar/Application.java index a17a52e724..b1d2a89be9 100644 --- a/src/main/java/racingcar/Application.java +++ b/src/main/java/racingcar/Application.java @@ -1,7 +1,101 @@ package racingcar; +import java.util.ArrayList; + public class Application { public static void main(String[] args) { // TODO: 프로그램 구현 + + // 입력받기 + System.out.println("경주할 자동차 이름을 입력하세요.(이름은 쉽표(,) 기준으로 구분)"); + String[] cars = camp.nextstep.edu.missionutils.Console.readLine().split(","); + + System.out.println("시도할 횟수는 몇 회인가요?"); + int maxTryCnt = Integer.parseInt(camp.nextstep.edu.missionutils.Console.readLine()); + int currentTryCnt = 0; + int[] currentRecord = new int[]{0, 0, 0}; + + System.out.println(); + System.out.println("실행결과"); + + testValidInput(cars); + + while (currentTryCnt < maxTryCnt) { + raceOneTurn(cars, currentRecord); + currentTryCnt++; + System.out.println(); + } + + int[] maxIndices = findMaxIndices(currentRecord); + + ArrayList winners = findWinners(cars, currentRecord); + + System.out.println("최종 우승자 : " + String.join(", ", winners)); + } + + public static void testValidInput(String[] cars) { + + for (int i = 0; i < cars.length; i++) { + if (cars[i].length() > 5) { + throw new IllegalArgumentException("입력이올바르지 않습니다"); + } + } + } + + static void raceOneTurn(String[] cars, int[] currentRecord) { + for (int i = 0; i < cars.length; i++) { + moveAndPrintCar(cars[i], currentRecord, i); + } } -} + + static void moveAndPrintCar(String carName, int[] currentRecord, int index) { + int randNum = camp.nextstep.edu.missionutils.Randoms.pickNumberInRange(0, 9); + if (randNum >= 4) { + currentRecord[index]++; + } + System.out.println(carName + " : " + "-".repeat(currentRecord[index])); + } + + public static int[] findMaxIndices(int[] arr) { + if (arr == null || arr.length == 0) { + return new int[0]; + } + + // 1. 최댓값 찾기 + int maxValue = arr[0]; + for (int num : arr) { + maxValue = Math.max(maxValue, num); + } + + // 2. 최댓값의 개수 세기 + int count = 0; + for (int num : arr) { + if (num == maxValue) { + count++; + } + } + + // 3. 결과 배열 생성 및 최댓값의 인덱스 저장 + int[] maxIndices = new int[count]; + int index = 0; + for (int i = 0; i < arr.length; i++) { + if (arr[i] == maxValue) { + maxIndices[index++] = i; + } + } + + return maxIndices; + } + + private static ArrayList findWinners(String[] cars, int[] currentRecord) { + int[] maxIndices = findMaxIndices(currentRecord); + ArrayList winners = new ArrayList<>(); + + for (int i = 0; i < maxIndices.length; i++) { + winners.add(cars[maxIndices[i]]); + } + + return winners; + } + +} \ No newline at end of file diff --git a/src/test/java/racingcar/ApplicationTest.java b/src/test/java/racingcar/ApplicationTest.java index 1d35fc33fe..1cb50005c1 100644 --- a/src/test/java/racingcar/ApplicationTest.java +++ b/src/test/java/racingcar/ApplicationTest.java @@ -1,12 +1,14 @@ package racingcar; import camp.nextstep.edu.missionutils.test.NsTest; +import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import static camp.nextstep.edu.missionutils.test.Assertions.assertRandomNumberInRangeTest; import static camp.nextstep.edu.missionutils.test.Assertions.assertSimpleTest; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; class ApplicationTest extends NsTest { private static final int MOVING_FORWARD = 4; @@ -31,6 +33,33 @@ class ApplicationTest extends NsTest { ); } + @Test + @DisplayName("findMaxIndices 테스트 - 최댓값이 하나인 경우") + void testFindMaxIndicesSingleMax() { + int[] arr = {1, 3, 2}; + int[] result = Application.findMaxIndices(arr); + + assertArrayEquals(new int[]{1}, result); + } + + @Test + @DisplayName("findMaxIndices 테스트 - 최댓값이 여러 개인 경우") + void testFindMaxIndicesMultipleMax() { + int[] arr = {3, 3, 2, 3}; + int[] result = Application.findMaxIndices(arr); + + assertArrayEquals(new int[]{0, 1, 3}, result); + } + + @Test + @DisplayName("findMaxIndices 테스트 - 빈 배열인 경우") + void testFindMaxIndicesEmptyArray() { + int[] arr = {}; + int[] result = Application.findMaxIndices(arr); + + assertArrayEquals(new int[]{}, result); + } + @Override public void runMain() { Application.main(new String[]{});