From cebfefc9875c946ed62b53bd540600cade3e149e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=84=B8=EC=A7=84?= <67494004+kim-se-jin@users.noreply.github.com> Date: Sun, 9 Jun 2024 20:53:55 +0900 Subject: [PATCH 1/3] =?UTF-8?q?ReadMe=EC=9D=98=20=E3=84=B1=EB=B2=88=20?= =?UTF-8?q?=EA=B8=B0=EB=8A=A5=EA=B9=8C=EC=A7=80=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Main.java | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ README.md | 9 ++++++++- 2 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 Main.java diff --git a/Main.java b/Main.java new file mode 100644 index 00000000..142a7bc6 --- /dev/null +++ b/Main.java @@ -0,0 +1,53 @@ +import java.util.ArrayList; +import java.util.List; +import java.util.Random; +import java.util.Scanner; + +public class Main { + + static List carList; + static int raceNum; + static Random random; + + public static void main(String[] args) { + System.out.println("경주할 자동차 이름을 입력하세요. (이름은 쉼표(,) 기준으로 구분"); + carList = checkCarName(getUserInput(0)); + + System.out.println("시도할 회수는 몇회인가요?"); + raceNum = Integer.parseInt(getUserInput(1)); + + System.out.println("실행 결과"); + random = new Random(); + for(int i = 0; i < raceNum; i++){ + raceStart(i); + } + + } + + public static String getUserInput(int inputOrder){ // 사용자 입력 받기 + Scanner scanner = new Scanner(System.in); + String input = scanner.nextLine(); + if (input == null || input.trim().isEmpty()) { + throw new IllegalArgumentException("Input must be a non-empty string."); + } + return input ; + } + + public static List checkCarName(String input){ // 입력받은 차 이름 반환 + List checkedNames = new ArrayList<>(); + String[] names = input.split(","); + + for (String name : names) { + if (name.length() <= 5) { + checkedNames.add(name); + } + } + return checkedNames; + } + + public static void raceStart(int i){ + int randomNumber = random.nextInt(10); + + + } +} diff --git a/README.md b/README.md index 491aece1..7ea7bd5e 100644 --- a/README.md +++ b/README.md @@ -1 +1,8 @@ -# java-racingcar-precourse \ No newline at end of file +# java-racingcar-precourse + +1. 안내문구 프린트하기 +2. 사용자 문자 입력받기 +3. 사용자 입력 쉼표로 분리하기 +4. 사용자 숫자 입력받기 +5. 입력된 숫자만큼 무작위로 자동차 주행하기 + 1. else, switch/case 사용 불가 \ No newline at end of file From 11d490e9888d8ee43043c5b1ce932c21c70c2ad1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=84=B8=EC=A7=84?= <67494004+kim-se-jin@users.noreply.github.com> Date: Mon, 10 Jun 2024 01:25:51 +0900 Subject: [PATCH 2/3] =?UTF-8?q?5=EB=B2=88=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Main.java | 27 +++++++++++++++++++++++---- README.md | 3 ++- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/Main.java b/Main.java index 142a7bc6..304b8189 100644 --- a/Main.java +++ b/Main.java @@ -6,6 +6,7 @@ public class Main { static List carList; + static List carRaceInfo; static int raceNum; static Random random; @@ -18,10 +19,13 @@ public static void main(String[] args) { System.out.println("실행 결과"); random = new Random(); - for(int i = 0; i < raceNum; i++){ - raceStart(i); + makeCarRaceInfoList(); + for(int nowRace = 0; nowRace < raceNum; nowRace++){ + for (int i = 0; i < carList.size(); i++) { + raceStart(i); + } + printRaceInfo(); } - } public static String getUserInput(int inputOrder){ // 사용자 입력 받기 @@ -45,9 +49,24 @@ public static List checkCarName(String input){ // 입력받은 차 이 return checkedNames; } + public static void makeCarRaceInfoList(){ + carRaceInfo = new ArrayList<>(carList.size()); + for (int i = 0; i < carList.size(); i++) { + carRaceInfo.add(""); + } + } + public static void raceStart(int i){ int randomNumber = random.nextInt(10); - + if (randomNumber <= 4){ + carRaceInfo.set(i, carRaceInfo.get(i)+"-"); + } + } + public static void printRaceInfo(){ + for(int i = 0; i Date: Mon, 10 Jun 2024 02:20:51 +0900 Subject: [PATCH 3/3] =?UTF-8?q?6=EB=B2=88=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84/MVC=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CarRaceController.java | 19 +++++++++ CarRaceModel.java | 45 +++++++++++++++++++++ CarRaceView.java | 26 +++++++++++++ Main.java | 88 ++++++++++++++++++++++++------------------ 4 files changed, 141 insertions(+), 37 deletions(-) create mode 100644 CarRaceController.java create mode 100644 CarRaceModel.java create mode 100644 CarRaceView.java diff --git a/CarRaceController.java b/CarRaceController.java new file mode 100644 index 00000000..f1b1af1d --- /dev/null +++ b/CarRaceController.java @@ -0,0 +1,19 @@ +public class CarRaceController { + private CarRaceModel model; + private CarRaceView view; + private int raceNum; // 사용자 입력값 / 레이스 횟수 + + public CarRaceController(CarRaceModel model, CarRaceView view) { + this.model = model; + this.view = view; + } + + public void startRace(int raceNum) { + this.raceNum = raceNum; + for (int nowRace = 0; nowRace < raceNum; nowRace++) { + model.race(); + view.printRaceInfo(model.getCarList(), model.getCarRaceInfo()); + } + view.printResult(model.getCarList(), model.getCarRaceInfo(), model.getWinnerLength()); + } +} diff --git a/CarRaceModel.java b/CarRaceModel.java new file mode 100644 index 00000000..f8c7ba96 --- /dev/null +++ b/CarRaceModel.java @@ -0,0 +1,45 @@ +import java.util.ArrayList; +import java.util.List; +import java.util.Random; + +public class CarRaceModel { + private List carList; // 사용자 입력값 / 자동차 이름 + private List carRaceInfo; // 자동차마다의 레이스 상황값 + private Random random; + private int winnerLen = 0; // 레이스에서의 최대 길이 + + public CarRaceModel(List carList) { + this.carList = carList; + this.carRaceInfo = new ArrayList<>(carList.size()); + for (int i = 0; i < carList.size(); i++) { + this.carRaceInfo.add(""); + } + this.random = new Random(); + } + + public void race() { + for (int i = 0; i < carList.size(); i++) { + raceStart(i); + } + } + + private void raceStart(int i) { + int randomNumber = random.nextInt(10); + if (randomNumber <= 4) { + carRaceInfo.set(i, carRaceInfo.get(i) + "-"); + winnerLen = Math.max(winnerLen, carRaceInfo.get(i).length()); + } + } + + public List getCarList() { + return carList; + } + + public List getCarRaceInfo() { + return carRaceInfo; + } + + public int getWinnerLength() { + return winnerLen; + } +} diff --git a/CarRaceView.java b/CarRaceView.java new file mode 100644 index 00000000..9b604d46 --- /dev/null +++ b/CarRaceView.java @@ -0,0 +1,26 @@ +import java.util.List; + +public class CarRaceView { + public void printRaceInfo(List carList, List carRaceInfo) { + for (int i = 0; i < carList.size(); i++) { + System.out.println(carList.get(i) + " : " + carRaceInfo.get(i)); + } + System.out.println(); + } + + public void printResult(List carList, List carRaceInfo, int winnerLen) { + boolean firstWinner = true; + System.out.print("최종 우승자 : "); + + for (int i = 0; i < carList.size(); i++) { + if (carRaceInfo.get(i).length() == winnerLen) { + if (firstWinner) { + firstWinner = false; + System.out.print(carList.get(i)); + continue; + } + System.out.print(", " + carList.get(i)); + } + } + } +} diff --git a/Main.java b/Main.java index 304b8189..6b92dc62 100644 --- a/Main.java +++ b/Main.java @@ -1,43 +1,47 @@ -import java.util.ArrayList; import java.util.List; -import java.util.Random; import java.util.Scanner; +import java.util.ArrayList; public class Main { - static List carList; - static List carRaceInfo; - static int raceNum; - static Random random; + static Scanner scanner ; + static List carList ; public static void main(String[] args) { + scanner = new Scanner(System.in); + System.out.println("경주할 자동차 이름을 입력하세요. (이름은 쉼표(,) 기준으로 구분"); - carList = checkCarName(getUserInput(0)); + getCarList(); System.out.println("시도할 회수는 몇회인가요?"); - raceNum = Integer.parseInt(getUserInput(1)); + int raceNum = getRaceNum(); - System.out.println("실행 결과"); - random = new Random(); - makeCarRaceInfoList(); - for(int nowRace = 0; nowRace < raceNum; nowRace++){ - for (int i = 0; i < carList.size(); i++) { - raceStart(i); - } - printRaceInfo(); - } + // int raceNum = Integer.parseInt(scanner.nextLine()); + System.out.println(); + + CarRaceModel model = new CarRaceModel(carList); + CarRaceView view = new CarRaceView(); + CarRaceController controller = new CarRaceController(model, view); + controller.startRace(raceNum); } - public static String getUserInput(int inputOrder){ // 사용자 입력 받기 - Scanner scanner = new Scanner(System.in); - String input = scanner.nextLine(); - if (input == null || input.trim().isEmpty()) { - throw new IllegalArgumentException("Input must be a non-empty string."); + public static List getCarList(){ + carList = new ArrayList<>(); + + while (true) { + String input = scanner.nextLine().trim(); + + try { + validateInput(input); + break; + } catch (IllegalArgumentException e) { + System.out.println("[ERROR] " + e.getMessage()); + } } - return input ; + return carList ; } - public static List checkCarName(String input){ // 입력받은 차 이름 반환 + public static List checkCarName(String input) { // 입력받은 차 이름 반환 List checkedNames = new ArrayList<>(); String[] names = input.split(","); @@ -49,24 +53,34 @@ public static List checkCarName(String input){ // 입력받은 차 이 return checkedNames; } - public static void makeCarRaceInfoList(){ - carRaceInfo = new ArrayList<>(carList.size()); - for (int i = 0; i < carList.size(); i++) { - carRaceInfo.add(""); + private static void validateInput(String input) throws IllegalArgumentException { + if (input.isEmpty()) { + throw new IllegalArgumentException("입력이 비어있습니다."); } - } - public static void raceStart(int i){ - int randomNumber = random.nextInt(10); - if (randomNumber <= 4){ - carRaceInfo.set(i, carRaceInfo.get(i)+"-"); + String[] names = input.split(","); + for (String name : names) { + if (name.trim().isEmpty()) { + throw new IllegalArgumentException("빈 자동차 이름이 있습니다."); + } + if (name.trim().length() > 5) { + throw new IllegalArgumentException("자동차 이름은 5자 이하여야 합니다."); + } + carList.add(name); } } - public static void printRaceInfo(){ - for(int i = 0; i