From 17ce83cc9022359ddce9ebea8972aa0ba135a010 Mon Sep 17 00:00:00 2001 From: JYP Date: Mon, 28 Oct 2024 23:45:03 +0900 Subject: [PATCH 1/5] =?UTF-8?q?feat(racingcar):=20=EC=9E=85=EB=A0=A5=20?= =?UTF-8?q?=EA=B2=80=EC=A6=9D=EA=B8=B0=EB=8A=A5=20=EA=B0=9C=EB=B0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 입력 및 검증기능 개발 --- README.md | 6 +++++ src/main/java/racingcar/Application.java | 29 +++++++++++++++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d0286c859f..5f2e404fe2 100644 --- a/README.md +++ b/README.md @@ -1 +1,7 @@ # java-racingcar-precourse + +- [ ] 입력 검증기능 +- [ ] 레이스 턴기능 +- [ ] 레이스 실행&출력기능 +- [ ] 선두주자 index찾는기능 +- [ ] 선두주자 문자열로 표현해 가져오는기능 \ No newline at end of file diff --git a/src/main/java/racingcar/Application.java b/src/main/java/racingcar/Application.java index a17a52e724..2c67c6c8a7 100644 --- a/src/main/java/racingcar/Application.java +++ b/src/main/java/racingcar/Application.java @@ -1,7 +1,34 @@ 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); + + + } + + public static void testValidInput(String[] cars) { + + for (int i = 0; i < cars.length; i++) { + if (cars[i].length() > 5) { + throw new IllegalArgumentException("입력이올바르지 않습니다"); + } + } } -} +} \ No newline at end of file From eb6b3773b8d1aa84b2b4bc1fe987a2c0d023b62d Mon Sep 17 00:00:00 2001 From: JYP Date: Mon, 28 Oct 2024 23:48:05 +0900 Subject: [PATCH 2/5] =?UTF-8?q?feat(racingcar):=20=EB=A0=88=EC=9D=B4?= =?UTF-8?q?=EC=8B=A4=20=ED=84=B4,=EC=8B=A4=ED=96=89,=EC=B6=9C=EB=A0=A5?= =?UTF-8?q?=EA=B8=B0=EB=8A=A5=EA=B0=9C=EB=B0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 8 ++++---- src/main/java/racingcar/Application.java | 21 ++++++++++++++++++++- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 5f2e404fe2..5201f8487b 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # java-racingcar-precourse -- [ ] 입력 검증기능 -- [ ] 레이스 턴기능 -- [ ] 레이스 실행&출력기능 -- [ ] 선두주자 index찾는기능 +- [x] 입력 검증기능 +- [x] 레이스 턴기능 +- [x] 레이스 실행&출력기능 +- [x] 선두주자 index 찾는기능 - [ ] 선두주자 문자열로 표현해 가져오는기능 \ No newline at end of file diff --git a/src/main/java/racingcar/Application.java b/src/main/java/racingcar/Application.java index 2c67c6c8a7..fb1a4995f5 100644 --- a/src/main/java/racingcar/Application.java +++ b/src/main/java/racingcar/Application.java @@ -20,7 +20,11 @@ public static void main(String[] args) { testValidInput(cars); - + while (currentTryCnt < maxTryCnt) { + raceOneTurn(cars, currentRecord); + currentTryCnt++; + System.out.println(); + } } public static void testValidInput(String[] cars) { @@ -31,4 +35,19 @@ public static void testValidInput(String[] cars) { } } } + + 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])); + } + } \ No newline at end of file From 41ecf98648c6dac2f904b685ed2f516f925909fd Mon Sep 17 00:00:00 2001 From: JYP Date: Mon, 28 Oct 2024 23:49:10 +0900 Subject: [PATCH 3/5] =?UTF-8?q?feat(racingcar):=20=EC=84=A0=EB=91=90?= =?UTF-8?q?=EC=A3=BC=EC=9E=90=20index=20=EC=B0=BE=EB=8A=94=EA=B8=B0?= =?UTF-8?q?=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/racingcar/Application.java | 33 ++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/main/java/racingcar/Application.java b/src/main/java/racingcar/Application.java index fb1a4995f5..ad9f3b6869 100644 --- a/src/main/java/racingcar/Application.java +++ b/src/main/java/racingcar/Application.java @@ -25,6 +25,8 @@ public static void main(String[] args) { currentTryCnt++; System.out.println(); } + + int[] maxIndices = findMaxIndices(currentRecord); } public static void testValidInput(String[] cars) { @@ -50,4 +52,35 @@ static void moveAndPrintCar(String carName, int[] currentRecord, int 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; + } + } \ No newline at end of file From 7dc63c1210a071964f937506222a57d3f1619256 Mon Sep 17 00:00:00 2001 From: JYP Date: Mon, 28 Oct 2024 23:50:07 +0900 Subject: [PATCH 4/5] =?UTF-8?q?feat(racingcar):=20=EC=84=A0=EB=91=90?= =?UTF-8?q?=EC=A3=BC=EC=9E=90=20=EB=AC=B8=EC=9E=90=EC=97=B4=EB=A1=9C=20?= =?UTF-8?q?=ED=91=9C=ED=98=84=ED=95=98=EB=8A=94=20=EA=B8=B0=EB=8A=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- src/main/java/racingcar/Application.java | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 5201f8487b..d660e2b0ab 100644 --- a/README.md +++ b/README.md @@ -4,4 +4,4 @@ - [x] 레이스 턴기능 - [x] 레이스 실행&출력기능 - [x] 선두주자 index 찾는기능 -- [ ] 선두주자 문자열로 표현해 가져오는기능 \ No newline at end of file +- [x] 선두주자 문자열로 표현해 가져오는기능 \ No newline at end of file diff --git a/src/main/java/racingcar/Application.java b/src/main/java/racingcar/Application.java index ad9f3b6869..b1d2a89be9 100644 --- a/src/main/java/racingcar/Application.java +++ b/src/main/java/racingcar/Application.java @@ -27,6 +27,10 @@ public static void main(String[] args) { } int[] maxIndices = findMaxIndices(currentRecord); + + ArrayList winners = findWinners(cars, currentRecord); + + System.out.println("최종 우승자 : " + String.join(", ", winners)); } public static void testValidInput(String[] cars) { @@ -83,4 +87,15 @@ public static int[] findMaxIndices(int[] arr) { 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 From c7c9c5620118aec0f84e87768727dc13f61bb1b4 Mon Sep 17 00:00:00 2001 From: JYP Date: Mon, 28 Oct 2024 23:51:31 +0900 Subject: [PATCH 5/5] =?UTF-8?q?test(racingcar):=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EA=B0=9C=EB=B0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit findMaxIndices 테스트 개발 --- src/test/java/racingcar/ApplicationTest.java | 29 ++++++++++++++++++++ 1 file changed, 29 insertions(+) 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[]{});