From 4c9b5bd090d3768fc5591b09db862650bb1a40ce Mon Sep 17 00:00:00 2001 From: junseoplee Date: Thu, 28 Dec 2023 19:26:38 +0900 Subject: [PATCH 01/14] commit --- src/main/java/racingcar/Application.java | 75 ++++++++++++++++++++++++ src/main/java/racingcar/Car.java | 17 +++++- 2 files changed, 91 insertions(+), 1 deletion(-) diff --git a/src/main/java/racingcar/Application.java b/src/main/java/racingcar/Application.java index b9ed0456a3..34ea5cb8e8 100644 --- a/src/main/java/racingcar/Application.java +++ b/src/main/java/racingcar/Application.java @@ -1,7 +1,82 @@ package racingcar; +import camp.nextstep.edu.missionutils.Console; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + public class Application { public static void main(String[] args) { // TODO 구현 진행 + // 자동차 이름 입력 -> 유효성 검사 + List carNames = getCarNames(); + // 자동차 목록 생성 + List cars = createCars(carNames); + // 시도 횟수 입력 + int tryCount = getTryCount(); + // 레이싱 게임 실행 -> 한 라운드 결과 출력 + runRacingGame(cars, tryCount); + // 우승자 목록 찾기 -> 우승자 목록 출력 + printWinners(findWinners(cars)); + } + + // 자동차 이름 입력 + private static List getCarNames() { +// Scanner scanner = new Scanner(System.in); + List carNames; + while (true) { + System.out.println("경주할 자동차 이름을 입력하세요. (이름은 쉼표(,) 기준으로 구분)"); + String input = Console.readLine(); + carNames = Arrays.stream(input.split("," , -1)); + try { // 예외가 발생한다면? -> catch + validateCarNames(carNames); // 유효성 검사 + break; + } + catch (IllegalArgumentException e) { + System.out.println("올바른 형식의 자동차 이름을 입력하세요."); + } + } + return carNames; + } + + // 자동차 이름 유효성 검사 + private static void validateCarNames(List carNames) { + for (String str : carNames) { + if (str.isEmpty() || str.length() > 5) { + throw new IllegalArgumentException(); + } + } + Set uniqueNames = new HashSet<>(carNames); // 중복을 허용하지 않는 자료구조 + if (uniqueNames.size() < carNames.size()) { // 작다면 중복된 이름이 있다는 것을 의미함 + throw new IllegalArgumentException(); + } } + + // 시도 횟수 입력 + private static int getTryCount() { + int tryCount; + while (true) { + System.out.println("시도할 횟수는 몇 회인가요?"); + String input = Console.readLine(); + try { + tryCount = Integer.parseInt(input); // 문자열을 정수로 변환하는 문법 + if (tryCount <= 0) { + throw new IllegalArgumentException(); + } + break; + } + catch (IllegalArgumentException e) { + System.out.println("올바른 형식의 숫자를 입력하세요."); + } + } + return tryCount; + } + + // 자동차 목록 생성. + private static List createCars(List carNames) { + + } + } diff --git a/src/main/java/racingcar/Car.java b/src/main/java/racingcar/Car.java index ab3df94921..2ed5550b08 100644 --- a/src/main/java/racingcar/Car.java +++ b/src/main/java/racingcar/Car.java @@ -1,5 +1,7 @@ package racingcar; +import camp.nextstep.edu.missionutils.Randoms; + public class Car { private final String name; private int position = 0; @@ -8,5 +10,18 @@ public Car(String name) { this.name = name; } - // 추가 기능 구현 + public void moveForward() { + int randomValue = Randoms.pickNumberInRange(0,9); + if (randomValue >= 4) { + position++; + } + } + + public String getName() { + return name; + } + + public int getPosition() { + return position; + } } From 1dd0320f0d2d701ef52823583edaa24e66469a2c Mon Sep 17 00:00:00 2001 From: junseoplee Date: Fri, 29 Dec 2023 16:59:43 +0900 Subject: [PATCH 02/14] commit --- src/main/java/racingcar/Application.java | 29 ++++++++++++++++++++---- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/src/main/java/racingcar/Application.java b/src/main/java/racingcar/Application.java index 34ea5cb8e8..9beb9dd806 100644 --- a/src/main/java/racingcar/Application.java +++ b/src/main/java/racingcar/Application.java @@ -2,10 +2,7 @@ import camp.nextstep.edu.missionutils.Console; -import java.util.Arrays; -import java.util.HashSet; -import java.util.List; -import java.util.Set; +import java.util.*; public class Application { public static void main(String[] args) { @@ -29,7 +26,7 @@ private static List getCarNames() { while (true) { System.out.println("경주할 자동차 이름을 입력하세요. (이름은 쉼표(,) 기준으로 구분)"); String input = Console.readLine(); - carNames = Arrays.stream(input.split("," , -1)); + carNames = Arrays.asList(input.split(",", -1)); try { // 예외가 발생한다면? -> catch validateCarNames(carNames); // 유효성 검사 break; @@ -76,7 +73,29 @@ private static int getTryCount() { // 자동차 목록 생성. private static List createCars(List carNames) { + List cars = new ArrayList<>(); // 리스트 생성 후 리스트의 원소가 Car 크랫스의 인스턴스가 되도록 + for (String name : carNames) { + cars.add(new Car(name)); + } + return cars; + } + // 레이싱 게임 실행 + private static void runRacingGame(List cars, int tryCount) { + for (int = 0; i < tryCount; i++) { + for (Car car : cars) { + car.moveForward(); + } + printRoundResult(cars); + } + } + + // 한 라운드 결과 출력 + private static void printRoundResult(List cars) { + for (Car car : cars) { +// System.out.println(car.getName() + " : " + "-".repeat(car.getPosition())); + System.out.println(car.getName() + " : " + new String(new char[car.getPosition()]).replace('\0', '-')); + } } } From 778bd4232e22515fe87fbbccc82370091f67cfb9 Mon Sep 17 00:00:00 2001 From: junseoplee Date: Fri, 29 Dec 2023 19:31:01 +0900 Subject: [PATCH 03/14] commit --- src/main/java/racingcar/Application.java | 28 +++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/src/main/java/racingcar/Application.java b/src/main/java/racingcar/Application.java index 9beb9dd806..edc99caa32 100644 --- a/src/main/java/racingcar/Application.java +++ b/src/main/java/racingcar/Application.java @@ -82,7 +82,7 @@ private static List createCars(List carNames) { // 레이싱 게임 실행 private static void runRacingGame(List cars, int tryCount) { - for (int = 0; i < tryCount; i++) { + for (int i = 0; i < tryCount; i++) { for (Car car : cars) { car.moveForward(); } @@ -96,6 +96,32 @@ private static void printRoundResult(List cars) { // System.out.println(car.getName() + " : " + "-".repeat(car.getPosition())); System.out.println(car.getName() + " : " + new String(new char[car.getPosition()]).replace('\0', '-')); } + System.out.println(); } + // 우승자 목록 찾기 + private static List findWinners(List cars) { + int maxPosition = getMaxPosition(cars); + List winners = new ArrayList<>(); + for (Car car : cars) { + if (car.getPosition() == maxPosition) { + winners.add(car.getName()); + } + } + return winners; + } + + // 최대 위치 찾기 + private static int getMaxPosition(List cars) { + int maxPosition = 0; + for (Car car : cars) { + maxPosition = Math.max(maxPosition, car.getPosition()); + } + return maxPosition; + } + + // 우승자 목록 출력 + private static void printWinners(List winners) { + System.out.println("최종 우승자는 " + String.join(", ", winners) + " 입니다!"); + } } From 7e2027e9f98ae8328acd833be94cc181fb9938f1 Mon Sep 17 00:00:00 2001 From: junseoplee Date: Fri, 29 Dec 2023 19:55:45 +0900 Subject: [PATCH 04/14] commit --- src/main/java/racingcar/Application.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/main/java/racingcar/Application.java b/src/main/java/racingcar/Application.java index edc99caa32..134e5bd488 100644 --- a/src/main/java/racingcar/Application.java +++ b/src/main/java/racingcar/Application.java @@ -32,7 +32,7 @@ private static List getCarNames() { break; } catch (IllegalArgumentException e) { - System.out.println("올바른 형식의 자동차 이름을 입력하세요."); + System.out.println("[ERROR] 올바른 형식의 자동차 이름을 입력하세요."); } } return carNames; @@ -65,7 +65,7 @@ private static int getTryCount() { break; } catch (IllegalArgumentException e) { - System.out.println("올바른 형식의 숫자를 입력하세요."); + System.out.println("[ERROR] 올바른 형식의 숫자를 입력하세요."); } } return tryCount; @@ -82,6 +82,7 @@ private static List createCars(List carNames) { // 레이싱 게임 실행 private static void runRacingGame(List cars, int tryCount) { + System.out.println("\n실행 결과"); for (int i = 0; i < tryCount; i++) { for (Car car : cars) { car.moveForward(); @@ -122,6 +123,6 @@ private static int getMaxPosition(List cars) { // 우승자 목록 출력 private static void printWinners(List winners) { - System.out.println("최종 우승자는 " + String.join(", ", winners) + " 입니다!"); + System.out.println("최종 우승자 : " + String.join(", ", winners)); } } From fc327317b649225c4e0bb6971a87e1204c498772 Mon Sep 17 00:00:00 2001 From: junseoplee Date: Mon, 1 Jan 2024 22:05:55 +0900 Subject: [PATCH 05/14] commit --- src/main/java/racingcar/Application.java | 34 +++++++++--------------- 1 file changed, 13 insertions(+), 21 deletions(-) diff --git a/src/main/java/racingcar/Application.java b/src/main/java/racingcar/Application.java index 134e5bd488..e14a5fb3d1 100644 --- a/src/main/java/racingcar/Application.java +++ b/src/main/java/racingcar/Application.java @@ -6,22 +6,19 @@ public class Application { public static void main(String[] args) { - // TODO 구현 진행 - // 자동차 이름 입력 -> 유효성 검사 List carNames = getCarNames(); - // 자동차 목록 생성 + List cars = createCars(carNames); - // 시도 횟수 입력 + int tryCount = getTryCount(); - // 레이싱 게임 실행 -> 한 라운드 결과 출력 + runRacingGame(cars, tryCount); - // 우승자 목록 찾기 -> 우승자 목록 출력 + printWinners(findWinners(cars)); } - // 자동차 이름 입력 + private static List getCarNames() { -// Scanner scanner = new Scanner(System.in); List carNames; while (true) { System.out.println("경주할 자동차 이름을 입력하세요. (이름은 쉼표(,) 기준으로 구분)"); @@ -38,7 +35,6 @@ private static List getCarNames() { return carNames; } - // 자동차 이름 유효성 검사 private static void validateCarNames(List carNames) { for (String str : carNames) { if (str.isEmpty() || str.length() > 5) { @@ -51,7 +47,6 @@ private static void validateCarNames(List carNames) { } } - // 시도 횟수 입력 private static int getTryCount() { int tryCount; while (true) { @@ -59,9 +54,7 @@ private static int getTryCount() { String input = Console.readLine(); try { tryCount = Integer.parseInt(input); // 문자열을 정수로 변환하는 문법 - if (tryCount <= 0) { - throw new IllegalArgumentException(); - } + validateTryCount(tryCount); break; } catch (IllegalArgumentException e) { @@ -71,16 +64,20 @@ private static int getTryCount() { return tryCount; } - // 자동차 목록 생성. + private static void validateTryCount(int tryCount) { + if (tryCount <= 0) { + throw new IllegalArgumentException(); + } + } + private static List createCars(List carNames) { - List cars = new ArrayList<>(); // 리스트 생성 후 리스트의 원소가 Car 크랫스의 인스턴스가 되도록 + List cars = new ArrayList<>(); // 리스트 생성 후 리스트의 원소가 Car 클래스의 인스턴스가 되도록 for (String name : carNames) { cars.add(new Car(name)); } return cars; } - // 레이싱 게임 실행 private static void runRacingGame(List cars, int tryCount) { System.out.println("\n실행 결과"); for (int i = 0; i < tryCount; i++) { @@ -91,16 +88,13 @@ private static void runRacingGame(List cars, int tryCount) { } } - // 한 라운드 결과 출력 private static void printRoundResult(List cars) { for (Car car : cars) { -// System.out.println(car.getName() + " : " + "-".repeat(car.getPosition())); System.out.println(car.getName() + " : " + new String(new char[car.getPosition()]).replace('\0', '-')); } System.out.println(); } - // 우승자 목록 찾기 private static List findWinners(List cars) { int maxPosition = getMaxPosition(cars); List winners = new ArrayList<>(); @@ -112,7 +106,6 @@ private static List findWinners(List cars) { return winners; } - // 최대 위치 찾기 private static int getMaxPosition(List cars) { int maxPosition = 0; for (Car car : cars) { @@ -121,7 +114,6 @@ private static int getMaxPosition(List cars) { return maxPosition; } - // 우승자 목록 출력 private static void printWinners(List winners) { System.out.println("최종 우승자 : " + String.join(", ", winners)); } From 8d549090e01f343399a75b24dce74c7d9681b926 Mon Sep 17 00:00:00 2001 From: junseoplee Date: Tue, 2 Jan 2024 01:16:01 +0900 Subject: [PATCH 06/14] =?UTF-8?q?TryCount=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84=20=EB=8B=A8=EC=9C=84=20=EA=B0=84=EC=86=8C=ED=99=94,?= =?UTF-8?q?=20getter=EC=99=80=20=ED=98=BC=EB=8F=99=EB=90=98=EB=8A=94=20?= =?UTF-8?q?=EB=A9=94=EC=84=9C=EB=93=9C=EB=AA=85=20receive=EB=A1=9C=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/racingcar/Application.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/racingcar/Application.java b/src/main/java/racingcar/Application.java index e14a5fb3d1..eca44a0194 100644 --- a/src/main/java/racingcar/Application.java +++ b/src/main/java/racingcar/Application.java @@ -6,11 +6,11 @@ public class Application { public static void main(String[] args) { - List carNames = getCarNames(); + List carNames = receiveCarNames(); List cars = createCars(carNames); - int tryCount = getTryCount(); + int tryCount = receiveTryCount(); runRacingGame(cars, tryCount); @@ -18,7 +18,7 @@ public static void main(String[] args) { } - private static List getCarNames() { + private static List receiveCarNames() { List carNames; while (true) { System.out.println("경주할 자동차 이름을 입력하세요. (이름은 쉼표(,) 기준으로 구분)"); @@ -47,7 +47,7 @@ private static void validateCarNames(List carNames) { } } - private static int getTryCount() { + private static int receiveTryCount() { int tryCount; while (true) { System.out.println("시도할 횟수는 몇 회인가요?"); From 3d5d1ff34f063cfbc918ee354d8fdf0248af64a3 Mon Sep 17 00:00:00 2001 From: junseoplee Date: Tue, 2 Jan 2024 01:48:32 +0900 Subject: [PATCH 07/14] =?UTF-8?q?=EC=9E=84=EC=8B=9C=20=EB=B3=80=EC=88=98?= =?UTF-8?q?=EB=AA=85=20str=EC=9D=84=20inputName=EC=9C=BC=EB=A1=9C=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/racingcar/Application.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/racingcar/Application.java b/src/main/java/racingcar/Application.java index eca44a0194..0ec0176e09 100644 --- a/src/main/java/racingcar/Application.java +++ b/src/main/java/racingcar/Application.java @@ -36,8 +36,8 @@ private static List receiveCarNames() { } private static void validateCarNames(List carNames) { - for (String str : carNames) { - if (str.isEmpty() || str.length() > 5) { + for (String inputName : carNames) { + if (inputName.isEmpty() || inputName.length() > 5) { throw new IllegalArgumentException(); } } From 06548c066d2c18ab00f793f2a997bccb8dc97b5a Mon Sep 17 00:00:00 2001 From: junseoplee Date: Tue, 2 Jan 2024 17:01:21 +0900 Subject: [PATCH 08/14] =?UTF-8?q?=EB=A9=A4=EB=B2=84=20=EB=B3=80=EC=88=98?= =?UTF-8?q?=EB=8A=94=20=EB=90=98=EB=8F=84=EB=A1=9D=20=EC=83=9D=EC=84=B1?= =?UTF-8?q?=EC=9E=90=EC=97=90=EC=84=9C=20=EC=B4=88=EA=B8=B0=ED=99=94=20?= =?UTF-8?q?=ED=95=98=EB=8F=84=EB=A1=9D=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/racingcar/Car.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/racingcar/Car.java b/src/main/java/racingcar/Car.java index 2ed5550b08..ba21564ecd 100644 --- a/src/main/java/racingcar/Car.java +++ b/src/main/java/racingcar/Car.java @@ -4,10 +4,11 @@ public class Car { private final String name; - private int position = 0; + private int position; public Car(String name) { this.name = name; + this.position = 0; } public void moveForward() { From 537662013bf3aead0e61f21b4c43192b87866379 Mon Sep 17 00:00:00 2001 From: junseoplee Date: Tue, 2 Jan 2024 17:28:37 +0900 Subject: [PATCH 09/14] =?UTF-8?q?MAX=5FNAME=5FLENGTH=20=EC=83=81=EC=88=98?= =?UTF-8?q?=20=EC=84=A0=EC=96=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/racingcar/Application.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/racingcar/Application.java b/src/main/java/racingcar/Application.java index 0ec0176e09..5d49f78de1 100644 --- a/src/main/java/racingcar/Application.java +++ b/src/main/java/racingcar/Application.java @@ -37,7 +37,8 @@ private static List receiveCarNames() { private static void validateCarNames(List carNames) { for (String inputName : carNames) { - if (inputName.isEmpty() || inputName.length() > 5) { + int MAX_NAME_LENGTH = 5; + if (inputName.isEmpty() || inputName.length() > MAX_NAME_LENGTH) { throw new IllegalArgumentException(); } } From cfa16f25bda14804f0f75cd94f86da1ff3bc9479 Mon Sep 17 00:00:00 2001 From: junseoplee Date: Tue, 2 Jan 2024 18:45:33 +0900 Subject: [PATCH 10/14] =?UTF-8?q?=EC=9C=A0=ED=9A=A8=EC=84=B1=20=EA=B2=80?= =?UTF-8?q?=EC=82=AC=20=EB=A9=94=EC=84=9C=EB=93=9C=EB=A5=BC=20Car=20?= =?UTF-8?q?=ED=81=B4=EB=9E=98=EC=8A=A4=20=EC=95=88=EC=9C=BC=EB=A1=9C=20?= =?UTF-8?q?=EC=9D=B4=EB=8F=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/racingcar/Application.java | 15 +-------------- src/main/java/racingcar/Car.java | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/src/main/java/racingcar/Application.java b/src/main/java/racingcar/Application.java index 5d49f78de1..c39739c238 100644 --- a/src/main/java/racingcar/Application.java +++ b/src/main/java/racingcar/Application.java @@ -25,7 +25,7 @@ private static List receiveCarNames() { String input = Console.readLine(); carNames = Arrays.asList(input.split(",", -1)); try { // 예외가 발생한다면? -> catch - validateCarNames(carNames); // 유효성 검사 + Car.validateCarNames(carNames); // 유효성 검사 break; } catch (IllegalArgumentException e) { @@ -35,19 +35,6 @@ private static List receiveCarNames() { return carNames; } - private static void validateCarNames(List carNames) { - for (String inputName : carNames) { - int MAX_NAME_LENGTH = 5; - if (inputName.isEmpty() || inputName.length() > MAX_NAME_LENGTH) { - throw new IllegalArgumentException(); - } - } - Set uniqueNames = new HashSet<>(carNames); // 중복을 허용하지 않는 자료구조 - if (uniqueNames.size() < carNames.size()) { // 작다면 중복된 이름이 있다는 것을 의미함 - throw new IllegalArgumentException(); - } - } - private static int receiveTryCount() { int tryCount; while (true) { diff --git a/src/main/java/racingcar/Car.java b/src/main/java/racingcar/Car.java index ba21564ecd..e5c5335255 100644 --- a/src/main/java/racingcar/Car.java +++ b/src/main/java/racingcar/Car.java @@ -2,6 +2,10 @@ import camp.nextstep.edu.missionutils.Randoms; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + public class Car { private final String name; private int position; @@ -25,4 +29,17 @@ public String getName() { public int getPosition() { return position; } + + public static void validateCarNames(List carNames) { + for (String inputName : carNames) { + int MAX_NAME_LENGTH = 5; + if (inputName.isEmpty() || inputName.length() > MAX_NAME_LENGTH) { + throw new IllegalArgumentException(); + } + } + Set uniqueNames = new HashSet<>(carNames); // 중복을 허용하지 않는 자료구조 + if (uniqueNames.size() < carNames.size()) { // 작다면 중복된 이름이 있다는 것을 의미함 + throw new IllegalArgumentException(); + } + } } From 4a3b5bc63860ff8c5316c3c9723780770506b825 Mon Sep 17 00:00:00 2001 From: junseoplee Date: Wed, 3 Jan 2024 17:44:37 +0900 Subject: [PATCH 11/14] =?UTF-8?q?refactor:=20=EC=9C=A0=ED=9A=A8=EC=84=B1?= =?UTF-8?q?=20=EA=B2=80=EC=82=AC=EB=A5=BC=20Car=20=ED=81=B4=EB=9E=98?= =?UTF-8?q?=EC=8A=A4=EC=9D=98=20=EC=B1=85=EC=9E=84=EC=9C=BC=EB=A1=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - validateCarNames 생성자에서 실행 - createCars, receiveNames 통합 --- src/main/java/racingcar/Application.java | 32 ++++++++---------------- src/main/java/racingcar/Car.java | 21 +++++----------- 2 files changed, 17 insertions(+), 36 deletions(-) diff --git a/src/main/java/racingcar/Application.java b/src/main/java/racingcar/Application.java index c39739c238..5736715fde 100644 --- a/src/main/java/racingcar/Application.java +++ b/src/main/java/racingcar/Application.java @@ -6,9 +6,7 @@ public class Application { public static void main(String[] args) { - List carNames = receiveCarNames(); - - List cars = createCars(carNames); + List cars = createCars(); int tryCount = receiveTryCount(); @@ -17,27 +15,27 @@ public static void main(String[] args) { printWinners(findWinners(cars)); } - - private static List receiveCarNames() { - List carNames; - while (true) { + private static List createCars() { + List carNames = null; + List cars = new ArrayList<>(); + while (carNames == null) { System.out.println("경주할 자동차 이름을 입력하세요. (이름은 쉼표(,) 기준으로 구분)"); String input = Console.readLine(); - carNames = Arrays.asList(input.split(",", -1)); - try { // 예외가 발생한다면? -> catch - Car.validateCarNames(carNames); // 유효성 검사 + try { + carNames = Arrays.asList(input.split(",", -1)); + carNames.forEach(inputName -> cars.add(new Car(inputName))); // carNames 리스트의 각 요소를 inputName으로 받아서 객체를 생성하고 cars 리스트에 추가함. break; } catch (IllegalArgumentException e) { System.out.println("[ERROR] 올바른 형식의 자동차 이름을 입력하세요."); } } - return carNames; + return cars; } private static int receiveTryCount() { - int tryCount; - while (true) { + int tryCount = 0; + while (tryCount == 0) { System.out.println("시도할 횟수는 몇 회인가요?"); String input = Console.readLine(); try { @@ -58,14 +56,6 @@ private static void validateTryCount(int tryCount) { } } - private static List createCars(List carNames) { - List cars = new ArrayList<>(); // 리스트 생성 후 리스트의 원소가 Car 클래스의 인스턴스가 되도록 - for (String name : carNames) { - cars.add(new Car(name)); - } - return cars; - } - private static void runRacingGame(List cars, int tryCount) { System.out.println("\n실행 결과"); for (int i = 0; i < tryCount; i++) { diff --git a/src/main/java/racingcar/Car.java b/src/main/java/racingcar/Car.java index e5c5335255..581ca92907 100644 --- a/src/main/java/racingcar/Car.java +++ b/src/main/java/racingcar/Car.java @@ -2,16 +2,13 @@ import camp.nextstep.edu.missionutils.Randoms; -import java.util.HashSet; -import java.util.List; -import java.util.Set; - public class Car { private final String name; private int position; - public Car(String name) { - this.name = name; + public Car(String inputName) { + validateCarNames(inputName); + this.name = inputName; this.position = 0; } @@ -30,15 +27,9 @@ public int getPosition() { return position; } - public static void validateCarNames(List carNames) { - for (String inputName : carNames) { - int MAX_NAME_LENGTH = 5; - if (inputName.isEmpty() || inputName.length() > MAX_NAME_LENGTH) { - throw new IllegalArgumentException(); - } - } - Set uniqueNames = new HashSet<>(carNames); // 중복을 허용하지 않는 자료구조 - if (uniqueNames.size() < carNames.size()) { // 작다면 중복된 이름이 있다는 것을 의미함 + private void validateCarNames(String inputName) { + int MAX_NAME_LENGTH = 5; + if (inputName.isEmpty() || inputName.length() > MAX_NAME_LENGTH) { throw new IllegalArgumentException(); } } From b696c3d8dc735c9037839a3c075264a90c21e306 Mon Sep 17 00:00:00 2001 From: junseoplee Date: Wed, 3 Jan 2024 18:14:24 +0900 Subject: [PATCH 12/14] =?UTF-8?q?fix:=20=EC=99=80=EC=9D=BC=EB=93=9C=20?= =?UTF-8?q?=EC=B9=B4=EB=93=9C=20=EC=82=AC=EC=9A=A9=ED=95=98=EC=A7=80=20?= =?UTF-8?q?=EC=95=8A=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/racingcar/Application.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/racingcar/Application.java b/src/main/java/racingcar/Application.java index 5736715fde..7224f6a616 100644 --- a/src/main/java/racingcar/Application.java +++ b/src/main/java/racingcar/Application.java @@ -2,7 +2,9 @@ import camp.nextstep.edu.missionutils.Console; -import java.util.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; public class Application { public static void main(String[] args) { From 76d0c3925b32ca32f14f4992d34329bf5597a903 Mon Sep 17 00:00:00 2001 From: junseoplee Date: Wed, 3 Jan 2024 18:19:29 +0900 Subject: [PATCH 13/14] =?UTF-8?q?fix:=20=EC=BB=A8=EB=B2=A4=EC=85=98=20?= =?UTF-8?q?=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 | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/racingcar/Application.java b/src/main/java/racingcar/Application.java index 7224f6a616..113dde4328 100644 --- a/src/main/java/racingcar/Application.java +++ b/src/main/java/racingcar/Application.java @@ -27,8 +27,7 @@ private static List createCars() { carNames = Arrays.asList(input.split(",", -1)); carNames.forEach(inputName -> cars.add(new Car(inputName))); // carNames 리스트의 각 요소를 inputName으로 받아서 객체를 생성하고 cars 리스트에 추가함. break; - } - catch (IllegalArgumentException e) { + } catch (IllegalArgumentException e) { System.out.println("[ERROR] 올바른 형식의 자동차 이름을 입력하세요."); } } From 5839d3144140e393a8e4f805942c4e122a23e3cf Mon Sep 17 00:00:00 2001 From: junseoplee Date: Fri, 5 Jan 2024 01:49:02 +0900 Subject: [PATCH 14/14] =?UTF-8?q?fix:=20=EC=83=81=EC=88=98=EB=8A=94=20stat?= =?UTF-8?q?ic=20final=EB=A1=9C=20=ED=81=B4=EB=9E=98=EC=8A=A4=20=EB=A7=A8?= =?UTF-8?q?=20=EC=9C=84=EC=97=90=20=EC=84=A0=EC=96=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/racingcar/Application.java | 2 +- src/main/java/racingcar/Car.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/racingcar/Application.java b/src/main/java/racingcar/Application.java index 113dde4328..b08f5bccaa 100644 --- a/src/main/java/racingcar/Application.java +++ b/src/main/java/racingcar/Application.java @@ -24,7 +24,7 @@ private static List createCars() { System.out.println("경주할 자동차 이름을 입력하세요. (이름은 쉼표(,) 기준으로 구분)"); String input = Console.readLine(); try { - carNames = Arrays.asList(input.split(",", -1)); + carNames = Arrays.asList(input.split(",")); carNames.forEach(inputName -> cars.add(new Car(inputName))); // carNames 리스트의 각 요소를 inputName으로 받아서 객체를 생성하고 cars 리스트에 추가함. break; } catch (IllegalArgumentException e) { diff --git a/src/main/java/racingcar/Car.java b/src/main/java/racingcar/Car.java index 581ca92907..65d59a59b8 100644 --- a/src/main/java/racingcar/Car.java +++ b/src/main/java/racingcar/Car.java @@ -3,6 +3,7 @@ import camp.nextstep.edu.missionutils.Randoms; public class Car { + static final int MAX_NAME_LENGTH = 5; private final String name; private int position; @@ -28,7 +29,6 @@ public int getPosition() { } private void validateCarNames(String inputName) { - int MAX_NAME_LENGTH = 5; if (inputName.isEmpty() || inputName.length() > MAX_NAME_LENGTH) { throw new IllegalArgumentException(); }