From 7a85d779dddccdea3557695743fe758eac39c0b4 Mon Sep 17 00:00:00 2001 From: Park-Min-Jun Date: Mon, 28 Oct 2024 23:07:55 +0900 Subject: [PATCH 01/10] =?UTF-8?q?feat=20:=20=EC=9E=90=EB=8F=99=EC=B0=A8=20?= =?UTF-8?q?=EC=9D=B4=EB=A6=84=20=EC=9E=85=EB=A0=A5=EB=B0=9B=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 ++ src/main/java/racingcar/Application.java | 12 ++++++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index d0286c859f..84daefdd32 100644 --- a/README.md +++ b/README.md @@ -1 +1,3 @@ # java-racingcar-precourse +## 기능 구현 +- [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..95985d9d6f 100644 --- a/src/main/java/racingcar/Application.java +++ b/src/main/java/racingcar/Application.java @@ -1,7 +1,15 @@ package racingcar; +import camp.nextstep.edu.missionutils.Console; + public class Application { + public static void main(String[] args) { - // TODO: 프로그램 구현 + String[] carNames = getCarNames(); + } + + private static String[] getCarNames() { + System.out.println("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)"); + return Console.readLine().split(","); } -} +} \ No newline at end of file From bcbb9fc512cbec9cf871869bbb418f431273c298 Mon Sep 17 00:00:00 2001 From: Park-Min-Jun Date: Mon, 28 Oct 2024 23:09:39 +0900 Subject: [PATCH 02/10] =?UTF-8?q?feat=20:=20=EC=9E=90=EB=8F=99=EC=B0=A8=20?= =?UTF-8?q?=EC=9D=B4=EB=A6=84=EC=9D=B4=205=EA=B8=80=EC=9E=90=20=EC=9D=B4?= =?UTF-8?q?=ED=95=98=EC=97=AC=EC=95=BC=ED=95=A8.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 5 ++++- src/main/java/racingcar/Application.java | 19 +++++++++++++++++-- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 84daefdd32..806f0e0fdc 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,6 @@ # java-racingcar-precourse ## 기능 구현 -- [x] 경주에 참여할 자동차 이름 입력받기 \ No newline at end of file +- [x] 경주에 참여할 자동차 이름 입력받기 + +## 예외 처리 +- [x] 자동차 이름이 5글자 이하 \ No newline at end of file diff --git a/src/main/java/racingcar/Application.java b/src/main/java/racingcar/Application.java index 95985d9d6f..e4a37b68dc 100644 --- a/src/main/java/racingcar/Application.java +++ b/src/main/java/racingcar/Application.java @@ -10,6 +10,21 @@ public static void main(String[] args) { private static String[] getCarNames() { System.out.println("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)"); - return Console.readLine().split(","); + String[] carNames = Console.readLine().split(","); + validateCarNames(carNames); + return carNames; } -} \ No newline at end of file + + // 자동차 이름 유효성 검사 + private static void validateCarNames(String[] carNames) { + for (String name : carNames) { + if (isInvalidCarName(name)) { + throw new IllegalArgumentException("자동차 이름은 1자 이상 5자 이하로 설정해야 합니다."); + } + } + } + + private static boolean isInvalidCarName(String name) { + return name.trim().length() > 5 || name.trim().isEmpty(); + } +} From e249f799ba0d2856b6771e630e27931623ca78c0 Mon Sep 17 00:00:00 2001 From: Park-Min-Jun Date: Mon, 28 Oct 2024 23:12:41 +0900 Subject: [PATCH 03/10] =?UTF-8?q?feat=20:=20=EC=8B=9C=EB=8F=84=20=ED=9A=9F?= =?UTF-8?q?=EC=88=98=EB=A5=BC=20=EC=9E=85=EB=A0=A5=EB=B0=9B=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + src/main/java/racingcar/Application.java | 11 ++++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 806f0e0fdc..7029a9fc20 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,7 @@ # java-racingcar-precourse ## 기능 구현 - [x] 경주에 참여할 자동차 이름 입력받기 +- [x] 시도할 횟수 입력받기 ## 예외 처리 - [x] 자동차 이름이 5글자 이하 \ No newline at end of file diff --git a/src/main/java/racingcar/Application.java b/src/main/java/racingcar/Application.java index e4a37b68dc..fa94312da0 100644 --- a/src/main/java/racingcar/Application.java +++ b/src/main/java/racingcar/Application.java @@ -6,12 +6,14 @@ public class Application { public static void main(String[] args) { String[] carNames = getCarNames(); + int numberOfAttempts = getNumberOfAttempts(); // 시도 횟수 입력받기 } + // 자동차 이름 입력받기 private static String[] getCarNames() { System.out.println("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)"); String[] carNames = Console.readLine().split(","); - validateCarNames(carNames); + validateCarNames(carNames); // 이름 유효성 검사 호출 return carNames; } @@ -24,7 +26,14 @@ private static void validateCarNames(String[] carNames) { } } + // 이름이 유효하지 않은지 확인하는 메소드 private static boolean isInvalidCarName(String name) { return name.trim().length() > 5 || name.trim().isEmpty(); } + + // 시도 횟수 입력받기 + private static int getNumberOfAttempts() { + System.out.println("시도할 횟수는 몇 회인가요?"); + return Integer.parseInt(Console.readLine()); + } } From cd2d9ce2a632c7a4fe807452e6223c85593b6ed0 Mon Sep 17 00:00:00 2001 From: Park-Min-Jun Date: Mon, 28 Oct 2024 23:14:01 +0900 Subject: [PATCH 04/10] =?UTF-8?q?feat=20:=20=EC=8B=9C=EB=8F=84=ED=95=A0=20?= =?UTF-8?q?=ED=9A=9F=EC=88=98=EB=8A=94=201=EC=9D=B4=EC=83=81=EC=9E=84.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 3 ++- src/main/java/racingcar/Application.java | 11 ++++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 7029a9fc20..f5f4f609fd 100644 --- a/README.md +++ b/README.md @@ -4,4 +4,5 @@ - [x] 시도할 횟수 입력받기 ## 예외 처리 -- [x] 자동차 이름이 5글자 이하 \ No newline at end of file +- [x] 자동차 이름이 5글자 이하 +- [x] 시도 횟수는 1 이상 \ No newline at end of file diff --git a/src/main/java/racingcar/Application.java b/src/main/java/racingcar/Application.java index fa94312da0..bb8cd761fe 100644 --- a/src/main/java/racingcar/Application.java +++ b/src/main/java/racingcar/Application.java @@ -34,6 +34,15 @@ private static boolean isInvalidCarName(String name) { // 시도 횟수 입력받기 private static int getNumberOfAttempts() { System.out.println("시도할 횟수는 몇 회인가요?"); - return Integer.parseInt(Console.readLine()); + int attempts = Integer.parseInt(Console.readLine()); + validateAttempts(attempts); // 시도 횟수 유효성 검사 + return attempts; + } + + // 시도 횟수 유효성 검사 + private static void validateAttempts(int attempts) { + if (attempts < 1) { + throw new IllegalArgumentException("이동 횟수는 1 이상이어야 합니다."); + } } } From 50fc5363b01223e907ab25f5673d1e21951b8335 Mon Sep 17 00:00:00 2001 From: Park-Min-Jun Date: Mon, 28 Oct 2024 23:22:14 +0900 Subject: [PATCH 05/10] =?UTF-8?q?feat=20:=200~9=20=EC=82=AC=EC=9D=B4?= =?UTF-8?q?=EC=9D=98=20=EB=AC=B4=EC=9E=91=EC=9C=84=20=EA=B0=92=EC=9D=84=20?= =?UTF-8?q?=EC=83=9D=EC=84=B1=ED=95=98=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + src/main/java/racingcar/Application.java | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/README.md b/README.md index f5f4f609fd..ba403d8aed 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,7 @@ ## 기능 구현 - [x] 경주에 참여할 자동차 이름 입력받기 - [x] 시도할 횟수 입력받기 +- [x] 0~9 사이의 무작위 값 생성하기 ## 예외 처리 - [x] 자동차 이름이 5글자 이하 diff --git a/src/main/java/racingcar/Application.java b/src/main/java/racingcar/Application.java index bb8cd761fe..f31d6f44db 100644 --- a/src/main/java/racingcar/Application.java +++ b/src/main/java/racingcar/Application.java @@ -1,6 +1,7 @@ package racingcar; import camp.nextstep.edu.missionutils.Console; +import camp.nextstep.edu.missionutils.Randoms; public class Application { @@ -45,4 +46,9 @@ private static void validateAttempts(int attempts) { throw new IllegalArgumentException("이동 횟수는 1 이상이어야 합니다."); } } + + // 0에서 9 사이의 무작위 값 생성 + private static int generateRandomValue() { + return Randoms.pickNumberInRange(0, 9); + } } From 23e206ea4edbe28c3dded42c85d8a620603c4a68 Mon Sep 17 00:00:00 2001 From: Park-Min-Jun Date: Mon, 28 Oct 2024 23:33:49 +0900 Subject: [PATCH 06/10] =?UTF-8?q?feat=20:=20=EB=AC=B4=EC=9E=91=EC=9C=84=20?= =?UTF-8?q?=EA=B0=92=EC=9D=B4=204=20=EC=9D=B4=EC=83=81=EC=9D=BC=20?= =?UTF-8?q?=EA=B2=BD=EC=9A=B0=20=EC=A0=84=EC=A7=84=ED=95=98=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + src/main/java/racingcar/Application.java | 39 +++++++++++++++++++----- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index ba403d8aed..ce4de63fbe 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,7 @@ - [x] 경주에 참여할 자동차 이름 입력받기 - [x] 시도할 횟수 입력받기 - [x] 0~9 사이의 무작위 값 생성하기 +- [x] 무작위 값이 4 이상일 경우 전진하기 ## 예외 처리 - [x] 자동차 이름이 5글자 이하 diff --git a/src/main/java/racingcar/Application.java b/src/main/java/racingcar/Application.java index f31d6f44db..0fc6639f1b 100644 --- a/src/main/java/racingcar/Application.java +++ b/src/main/java/racingcar/Application.java @@ -6,15 +6,20 @@ public class Application { public static void main(String[] args) { - String[] carNames = getCarNames(); - int numberOfAttempts = getNumberOfAttempts(); // 시도 횟수 입력받기 + try { + String[] carNames = getCarNames(); + int numberOfAttempts = getNumberOfAttempts(); + int[] carDistances = startRace(carNames, numberOfAttempts); + } catch (IllegalArgumentException e) { + System.out.println(e.getMessage()); + } } // 자동차 이름 입력받기 private static String[] getCarNames() { System.out.println("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)"); String[] carNames = Console.readLine().split(","); - validateCarNames(carNames); // 이름 유효성 검사 호출 + validateCarNames(carNames); return carNames; } @@ -36,19 +41,37 @@ private static boolean isInvalidCarName(String name) { private static int getNumberOfAttempts() { System.out.println("시도할 횟수는 몇 회인가요?"); int attempts = Integer.parseInt(Console.readLine()); - validateAttempts(attempts); // 시도 횟수 유효성 검사 + validateAttempts(attempts); return attempts; } - // 시도 횟수 유효성 검사 private static void validateAttempts(int attempts) { if (attempts < 1) { throw new IllegalArgumentException("이동 횟수는 1 이상이어야 합니다."); } } - // 0에서 9 사이의 무작위 값 생성 - private static int generateRandomValue() { - return Randoms.pickNumberInRange(0, 9); + // 경주 시작 로직 + private static int[] startRace(String[] carNames, int attempts) { + int[] distances = new int[carNames.length]; + System.out.println("실행 결과"); + for (int i = 0; i < attempts; i++) { + runSingleRace(carNames, distances); + } + return distances; + } + + // 한 번의 경주 시도 + private static void runSingleRace(String[] carNames, int[] distances) { + for (int j = 0; j < carNames.length; j++) { + if (canAdvance()) { + distances[j]++; + } + } + } + + // 전진 조건 체크 + private static boolean canAdvance() { + return Randoms.pickNumberInRange(0, 9) >= 4; } } From f1e86c6db66b9888cc447195f3c2d2e81a8ba832 Mon Sep 17 00:00:00 2001 From: Park-Min-Jun Date: Mon, 28 Oct 2024 23:36:23 +0900 Subject: [PATCH 07/10] =?UTF-8?q?feat=20:=20=EC=8B=9C=EB=8F=84=ED=95=A0=20?= =?UTF-8?q?=EB=95=8C=EB=A7=88=EB=8B=A4=20=EC=A0=84=EC=A7=84=ED=95=9C=20?= =?UTF-8?q?=EC=83=81=ED=99=A9=20'-'=EB=A1=9C=20=EC=B6=9C=EB=A0=A5=ED=95=98?= =?UTF-8?q?=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + src/main/java/racingcar/Application.java | 17 +++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/README.md b/README.md index ce4de63fbe..2076d16889 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,7 @@ - [x] 시도할 횟수 입력받기 - [x] 0~9 사이의 무작위 값 생성하기 - [x] 무작위 값이 4 이상일 경우 전진하기 +- [x] 시도할 때마다 전진한 상황 출력하기 ## 예외 처리 - [x] 자동차 이름이 5글자 이하 diff --git a/src/main/java/racingcar/Application.java b/src/main/java/racingcar/Application.java index 0fc6639f1b..51d8f07bb3 100644 --- a/src/main/java/racingcar/Application.java +++ b/src/main/java/racingcar/Application.java @@ -68,10 +68,27 @@ private static void runSingleRace(String[] carNames, int[] distances) { distances[j]++; } } + displayRaceStatus(carNames, distances); } // 전진 조건 체크 private static boolean canAdvance() { return Randoms.pickNumberInRange(0, 9) >= 4; } + + // 현재 경주 상태 출력 + private static void displayRaceStatus(String[] carNames, int[] distances) { + for (int j = 0; j < carNames.length; j++) { + System.out.print(carNames[j].trim() + " : "); + displayCarProgress(distances[j]); + } + System.out.println(); + } + + private static void displayCarProgress(int distance) { + for (int k = 0; k < distance; k++) { + System.out.print("-"); + } + System.out.println(); + } } From b0e2883dff56346d6c06797b7d712c0ee4201159 Mon Sep 17 00:00:00 2001 From: Park-Min-Jun Date: Mon, 28 Oct 2024 23:41:04 +0900 Subject: [PATCH 08/10] =?UTF-8?q?feat=20:=20=EC=A0=84=EC=A7=84=ED=95=9C=20?= =?UTF-8?q?=EC=B5=9C=EB=8C=93=EA=B0=92=EC=9D=84=20=EA=B5=AC=ED=95=B4?= =?UTF-8?q?=EC=84=9C=20=EC=B5=9C=EC=A2=85=20=EC=9A=B0=EC=8A=B9=EC=9E=90?= =?UTF-8?q?=EB=A5=BC=20=EC=84=A0=EC=A0=95=ED=95=98=EA=B3=A0=20=EC=B6=9C?= =?UTF-8?q?=EB=A0=A5=ED=95=98=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + src/main/java/racingcar/Application.java | 32 ++++++++++++++++++------ 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 2076d16889..25297e9759 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,7 @@ - [x] 0~9 사이의 무작위 값 생성하기 - [x] 무작위 값이 4 이상일 경우 전진하기 - [x] 시도할 때마다 전진한 상황 출력하기 +- [x] 경주가 끝날 경우 최종 우승자 선정하고 출력하기 ## 예외 처리 - [x] 자동차 이름이 5글자 이하 diff --git a/src/main/java/racingcar/Application.java b/src/main/java/racingcar/Application.java index 51d8f07bb3..1391a1942a 100644 --- a/src/main/java/racingcar/Application.java +++ b/src/main/java/racingcar/Application.java @@ -3,16 +3,17 @@ import camp.nextstep.edu.missionutils.Console; import camp.nextstep.edu.missionutils.Randoms; +import java.util.ArrayList; +import java.util.List; +import java.util.Arrays; + public class Application { public static void main(String[] args) { - try { - String[] carNames = getCarNames(); - int numberOfAttempts = getNumberOfAttempts(); - int[] carDistances = startRace(carNames, numberOfAttempts); - } catch (IllegalArgumentException e) { - System.out.println(e.getMessage()); - } + String[] carNames = getCarNames(); + int numberOfAttempts = getNumberOfAttempts(); + int[] carDistances = startRace(carNames, numberOfAttempts); + displayWinners(carNames, carDistances); } // 자동차 이름 입력받기 @@ -91,4 +92,21 @@ private static void displayCarProgress(int distance) { } System.out.println(); } + + // 우승자 계산 및 출력 + private static void displayWinners(String[] carNames, int[] distances) { + int maxDistance = Arrays.stream(distances).max().orElse(0); + List winners = findWinners(carNames, distances, maxDistance); + System.out.println("최종 우승자 : " + String.join(", ", winners)); + } + + private static List findWinners(String[] carNames, int[] distances, int maxDistance) { + List winners = new ArrayList<>(); + for (int i = 0; i < carNames.length; i++) { + if (distances[i] == maxDistance) { + winners.add(carNames[i].trim()); + } + } + return winners; + } } From af16a7143a4ce72c29e376b5b44b6632e89d2b14 Mon Sep 17 00:00:00 2001 From: Park-Min-Jun Date: Mon, 28 Oct 2024 23:42:32 +0900 Subject: [PATCH 09/10] =?UTF-8?q?feat=20:=20=EC=98=88=EC=99=B8=20=EC=B2=98?= =?UTF-8?q?=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 3 ++- src/main/java/racingcar/Application.java | 12 ++++++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 25297e9759..1baf6facd8 100644 --- a/README.md +++ b/README.md @@ -9,4 +9,5 @@ ## 예외 처리 - [x] 자동차 이름이 5글자 이하 -- [x] 시도 횟수는 1 이상 \ No newline at end of file +- [x] 시도 횟수는 1 이상 +- [x] 예외 처리 \ No newline at end of file diff --git a/src/main/java/racingcar/Application.java b/src/main/java/racingcar/Application.java index 1391a1942a..f31c072022 100644 --- a/src/main/java/racingcar/Application.java +++ b/src/main/java/racingcar/Application.java @@ -10,10 +10,14 @@ public class Application { public static void main(String[] args) { - String[] carNames = getCarNames(); - int numberOfAttempts = getNumberOfAttempts(); - int[] carDistances = startRace(carNames, numberOfAttempts); - displayWinners(carNames, carDistances); + try { + String[] carNames = getCarNames(); + int numberOfAttempts = getNumberOfAttempts(); + int[] carDistances = startRace(carNames, numberOfAttempts); + displayWinners(carNames, carDistances); + } catch (IllegalArgumentException e) { + System.out.println(e.getMessage()); + } } // 자동차 이름 입력받기 From d3f3ba360bb19c9d91ecd7195e2e64d9814eb82a Mon Sep 17 00:00:00 2001 From: Park-Min-Jun Date: Mon, 28 Oct 2024 23:59:02 +0900 Subject: [PATCH 10/10] =?UTF-8?q?fix:=20try=20catch=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/racingcar/Application.java | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/main/java/racingcar/Application.java b/src/main/java/racingcar/Application.java index f31c072022..3a7240236b 100644 --- a/src/main/java/racingcar/Application.java +++ b/src/main/java/racingcar/Application.java @@ -10,14 +10,12 @@ public class Application { public static void main(String[] args) { - try { - String[] carNames = getCarNames(); - int numberOfAttempts = getNumberOfAttempts(); - int[] carDistances = startRace(carNames, numberOfAttempts); - displayWinners(carNames, carDistances); - } catch (IllegalArgumentException e) { - System.out.println(e.getMessage()); - } + + String[] carNames = getCarNames(); + int numberOfAttempts = getNumberOfAttempts(); + int[] carDistances = startRace(carNames, numberOfAttempts); + displayWinners(carNames, carDistances); + } // 자동차 이름 입력받기