From 5209627fc0ed2cb7f1f56dfc8b24c7d97eaad847 Mon Sep 17 00:00:00 2001 From: WoonKyung Kim Date: Sat, 18 Mar 2023 20:51:27 +0900 Subject: [PATCH 1/4] =?UTF-8?q?feat:=20Car=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/Car.java | 41 ++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/src/main/java/racingcar/Car.java b/src/main/java/racingcar/Car.java index ab3df94..339f64d 100644 --- a/src/main/java/racingcar/Car.java +++ b/src/main/java/racingcar/Car.java @@ -1,12 +1,49 @@ package racingcar; +import camp.nextstep.edu.missionutils.Randoms; public class Car { private final String name; private int position = 0; - + private int distance = 0; public Car(String name) { this.name = name; } - // 추가 기능 구현 + public int GenerateRandomNumber() { + int randomNumber = Randoms.pickNumberInRange(0, 9); + return randomNumber; + } + + public int getDistance() { + return distance; + } + + public String getName() { + return this.name; + } + + public boolean CheckAdvance(int num) { + if(num>=4) { + return true; } + return false; + } + + public void Advance() { + this.distance++; + } + + public void show_result() { + System.out.print(this.name + " : "); + for(int i=0; i Date: Sat, 18 Mar 2023 20:51:45 +0900 Subject: [PATCH 2/4] =?UTF-8?q?feat:=20Main=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 | 84 +++++++++++++++++++++++- 1 file changed, 83 insertions(+), 1 deletion(-) diff --git a/src/main/java/racingcar/Application.java b/src/main/java/racingcar/Application.java index b9ed045..895af4d 100644 --- a/src/main/java/racingcar/Application.java +++ b/src/main/java/racingcar/Application.java @@ -1,7 +1,89 @@ package racingcar; +import java.util.InputMismatchException; +import java.util.Scanner; +import camp.nextstep.edu.missionutils.Console; + public class Application { public static void main(String[] args) { - // TODO 구현 진행 + //getInput(); + System.out.println("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)"); + Scanner scanner = new Scanner(System.in); + + String input = Console.readLine(); + + String[] car_name = input.split(","); + Car[] cars = new Car[car_name.length]; + + + + int attempt =0; + boolean validInput = false; + + // + while(!validInput) { + try { + System.out.println("시도할 회수는 몇회인가요?"); + attempt = scanner.nextInt(); + validInput = true; + } catch (InputMismatchException e) { + System.out.println("[ERROR] 시도 횟수는 숫자여야 한다."); + Console.readLine(); + } + } + + System.out.println("\n실행 결과"); + + //makeCarInstance + for(int i=0; i< car_name.length; i++) { + String carName = car_name[i]; + cars[i] = new Car(carName); + } + + int[] distance_arr = new int[car_name.length]; + + for(int j=0; j max) { + max = distance_arr[i]; + } + } + int cnt = 0; + for(int i=0; i< car_name.length; i++) { + if(distance_arr[i] == max) { + cnt++; + } + } + + //printWinner carArray + System.out.print("최초 우승자 : "); + for(int i=0; i< car_name.length; i++) { + if (distance_arr[i] == max) { + if (cnt > 1) { + System.out.print(cars[i].getName() + ", "); + cnt--; + } + else { + System.out.println(cars[i].getName()); + } + } + } } + } From c37f2f8d41cf21682140337f6dd9036860c2e6a7 Mon Sep 17 00:00:00 2001 From: WoonKyung Kim Date: Sat, 25 Mar 2023 10:53:42 +0900 Subject: [PATCH 3/4] =?UTF-8?q?feat:=20=EA=B5=AC=EC=A1=B0=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD=20=EB=B0=8F=20=EB=A9=94=EC=86=8C=EB=93=9C=ED=99=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/racingcar/Application.java | 119 ++++++++++------------- src/main/java/racingcar/Car.java | 32 ++---- 2 files changed, 61 insertions(+), 90 deletions(-) diff --git a/src/main/java/racingcar/Application.java b/src/main/java/racingcar/Application.java index 895af4d..f8da98d 100644 --- a/src/main/java/racingcar/Application.java +++ b/src/main/java/racingcar/Application.java @@ -1,89 +1,76 @@ package racingcar; -import java.util.InputMismatchException; -import java.util.Scanner; +import java.util.ArrayList; +import java.util.List; import camp.nextstep.edu.missionutils.Console; + public class Application { - public static void main(String[] args) { - //getInput(); - System.out.println("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)"); - Scanner scanner = new Scanner(System.in); - String input = Console.readLine(); + private List cars; + private InputManager inputManager; + private OutputManger outputManger; + private int totalAttemptNum; - String[] car_name = input.split(","); - Car[] cars = new Car[car_name.length]; + public static void main(String[] args) { + Application app = new Application(); + app.inputManager = new InputManager(); + app.outputManger = new OutputManger(); + app.run(); + } + public void run() { + getInfo(); + simulation(); + finishGame(); + } - int attempt =0; - boolean validInput = false; + private void getInfo() { + cars = new ArrayList<>(); - // - while(!validInput) { - try { - System.out.println("시도할 회수는 몇회인가요?"); - attempt = scanner.nextInt(); - validInput = true; - } catch (InputMismatchException e) { - System.out.println("[ERROR] 시도 횟수는 숫자여야 한다."); - Console.readLine(); - } + outputManger.printGetCarName(); + String[] carName = inputManager.getCar(); + + for(String name : carName) { + cars.add(new Car(name)); } - System.out.println("\n실행 결과"); + outputManger.printGetAttemptNum(); + this.totalAttemptNum = inputManager.getAttemptNum(); - //makeCarInstance - for(int i=0; i< car_name.length; i++) { - String carName = car_name[i]; - cars[i] = new Car(carName); - } + outputManger.printRunResultMessage(); + } - int[] distance_arr = new int[car_name.length]; - - for(int j=0; j max) { - max = distance_arr[i]; - } + private List calWinner() { + int maxPosition = 0; + List winners = new ArrayList(); + + for (Car car : cars) { + maxPosition = Math.max(maxPosition, car.getPosition()); } - int cnt = 0; - for(int i=0; i< car_name.length; i++) { - if(distance_arr[i] == max) { - cnt++; + + for (Car car : cars) { + if (car.getPosition() == maxPosition) { + winners.add(car); } } - //printWinner carArray - System.out.print("최초 우승자 : "); - for(int i=0; i< car_name.length; i++) { - if (distance_arr[i] == max) { - if (cnt > 1) { - System.out.print(cars[i].getName() + ", "); - cnt--; - } - else { - System.out.println(cars[i].getName()); - } - } - } + return winners; + } + + private void finishGame() { + outputManger.printWinner(calWinner()); } -} +} \ No newline at end of file diff --git a/src/main/java/racingcar/Car.java b/src/main/java/racingcar/Car.java index 339f64d..c45cc76 100644 --- a/src/main/java/racingcar/Car.java +++ b/src/main/java/racingcar/Car.java @@ -4,46 +4,30 @@ public class Car { private final String name; private int position = 0; - private int distance = 0; public Car(String name) { this.name = name; } - public int GenerateRandomNumber() { - int randomNumber = Randoms.pickNumberInRange(0, 9); - return randomNumber; - } - - public int getDistance() { - return distance; + public int getPosition() { + return position; } public String getName() { return this.name; } - public boolean CheckAdvance(int num) { - if(num>=4) { - return true; } - return false; - } - - public void Advance() { - this.distance++; + public int GenerateRandomNumber() { + int randomNumber = Randoms.pickNumberInRange(0, 9); + return randomNumber; } - public void show_result() { - System.out.print(this.name + " : "); - for(int i=0; i=4) { + this.position++; } - System.out.println(); } - public void show_winner() { - System.out.print("최종 우승자 : " + this.name); - } } From 80ce8bded2931769f782d1d7b416a5a829802388 Mon Sep 17 00:00:00 2001 From: WoonKyung Kim Date: Sat, 25 Mar 2023 10:55:00 +0900 Subject: [PATCH 4/4] =?UTF-8?q?feat:=20=EC=9E=85=EB=A0=A5,=20=EC=B6=9C?= =?UTF-8?q?=EB=A0=A5=EC=9A=A9=20=ED=81=B4=EB=9E=98=EC=8A=A4=20=EC=83=9D?= =?UTF-8?q?=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/racingcar/InputManager.java | 15 +++++++++ src/main/java/racingcar/OutputManger.java | 41 +++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 src/main/java/racingcar/InputManager.java create mode 100644 src/main/java/racingcar/OutputManger.java diff --git a/src/main/java/racingcar/InputManager.java b/src/main/java/racingcar/InputManager.java new file mode 100644 index 0000000..1c93905 --- /dev/null +++ b/src/main/java/racingcar/InputManager.java @@ -0,0 +1,15 @@ +package racingcar; + +import camp.nextstep.edu.missionutils.Console; + +public class InputManager { + public String[] getCar() { + String input = Console.readLine(); + return input.split(","); + } + public int getAttemptNum() { + String attemptNum = Console.readLine(); + return Integer.parseInt(attemptNum); + } + +} diff --git a/src/main/java/racingcar/OutputManger.java b/src/main/java/racingcar/OutputManger.java new file mode 100644 index 0000000..b571e7f --- /dev/null +++ b/src/main/java/racingcar/OutputManger.java @@ -0,0 +1,41 @@ +package racingcar; + +import java.util.List; +import camp.nextstep.edu.missionutils.Console; +public class OutputManger { + public void printGetCarName() { + System.out.println("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)"); + } + + public void printGetAttemptNum() { + System.out.println("시도할 회수는 몇회인가요?"); + } + + public void printRunResultMessage() { + System.out.println("실행 결과\n"); + } + + public void printSimulationResult(List cars) { + for (Car car : cars) { + printCarInfo(car); + } + System.out.println(); + } + + public void printCarInfo(Car car) { + System.out.print(car.getName() + " : "); + for(int i=0; i winners) { + System.out.print("최종 우승자 : "); + for(int i=0; i