From 5854263f8a51e3e2bf0ca1cedfbb2808db5ef8ff Mon Sep 17 00:00:00 2001 From: doyooon Date: Mon, 3 Jun 2024 15:36:21 +0900 Subject: [PATCH 01/14] =?UTF-8?q?docs=20README.md:=20=EA=B8=B0=EB=8A=A5=20?= =?UTF-8?q?=EC=9A=94=EA=B5=AC=EC=82=AC=ED=95=AD=20=EB=AA=A9=EB=A1=9D=20?= =?UTF-8?q?=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 491aece1..2885af32 100644 --- a/README.md +++ b/README.md @@ -1 +1,14 @@ -# java-racingcar-precourse \ No newline at end of file +# java-racingcar-precourse + +### 기능 요구사항 목록 +- [ ] 자동차 이름 입력받기 + - [ ] 쉼표(,)로 입력 받은 이름 구분하기 + - [ ] 각 이름 5자 이하의 이름인지 검사하기 +- [ ] 이동 횟수 입력 받기 +- [ ] 자동차 전진하기 + - [ ] 입력 횟수 만큼 자동차 전진하기 + - [ ] 0 ~ 9 사이의 무작위 값 구하기 + - [ ] 무작위 값이 4 이상 인 경우 전진하기 +- [ ] 각 횟수별로 전진 한 결과 출력하기 +- [ ] 우승자 출력하기 + - [ ] 우승자가 여러명일 경우 쉼표(,)로 구분해서 출력하기 \ No newline at end of file From c0dfc51534cc9a46cae17b38630e0dd6870203f8 Mon Sep 17 00:00:00 2001 From: doyooon Date: Mon, 10 Jun 2024 04:58:41 +0900 Subject: [PATCH 02/14] =?UTF-8?q?docs=20README.md:=20=EA=B8=B0=EB=8A=A5=20?= =?UTF-8?q?=EC=9A=94=EA=B5=AC=EC=82=AC=ED=95=AD=20=EB=AA=A9=EB=A1=9D=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80(=EC=9D=B4=EB=8F=99=20=ED=9A=9F=EC=88=98=20?= =?UTF-8?q?=EC=9E=85=EB=A0=A5=EA=B0=92=20=EA=B2=80=EC=82=AC=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 | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 2885af32..131cbdca 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,8 @@ - [ ] 쉼표(,)로 입력 받은 이름 구분하기 - [ ] 각 이름 5자 이하의 이름인지 검사하기 - [ ] 이동 횟수 입력 받기 + - [ ] 입력 값이 숫자인지 검사하기 + - [ ] 입력 값이 자연수 인지 검사하기 - [ ] 자동차 전진하기 - [ ] 입력 횟수 만큼 자동차 전진하기 - [ ] 0 ~ 9 사이의 무작위 값 구하기 From 32c3ac5638b0762bfa8c14d5d64ed8de20bb7311 Mon Sep 17 00:00:00 2001 From: doyooon Date: Mon, 10 Jun 2024 05:03:40 +0900 Subject: [PATCH 03/14] =?UTF-8?q?feat=20InputView=20:=20=EC=82=AC=EC=9A=A9?= =?UTF-8?q?=EC=9E=90=EC=97=90=EA=B2=8C=20=EC=9D=B4=EB=A6=84=20=EC=9E=85?= =?UTF-8?q?=EB=A0=A5=20=EB=B0=9B=EA=B3=A0,=20=EC=89=BC=ED=91=9C=EB=A1=9C?= =?UTF-8?q?=20=EA=B5=AC=EB=B6=84=ED=95=98=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/view/InputView.java | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 src/main/java/view/InputView.java diff --git a/src/main/java/view/InputView.java b/src/main/java/view/InputView.java new file mode 100644 index 00000000..5a97c949 --- /dev/null +++ b/src/main/java/view/InputView.java @@ -0,0 +1,25 @@ +package view; + +import java.util.InputMismatchException; +import java.util.Scanner; + +public class InputView { + + private String[] carNames; + private int iteration; + + public void inputName() { + Scanner scanner = new Scanner(System.in); + System.out.println("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)"); + this.carNames = scanner.nextLine().split("\\s*,\\s*"); + } + + public String[] getCarNames() { + return this.carNames; + } + + public int getIteration() { + return this.iteration; + } + +} From af63ec2f901bda0879805c5770794c2cbe6746b5 Mon Sep 17 00:00:00 2001 From: doyooon Date: Mon, 10 Jun 2024 05:05:09 +0900 Subject: [PATCH 04/14] =?UTF-8?q?feat=20InputView=20:=20=EC=9D=B4=EB=8F=99?= =?UTF-8?q?=20=ED=9A=9F=EC=88=98=20=EC=9E=85=EB=A0=A5=20=EB=B0=9B=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 8 ++++---- src/main/java/view/InputView.java | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 131cbdca..ea4b77f9 100644 --- a/README.md +++ b/README.md @@ -2,11 +2,11 @@ ### 기능 요구사항 목록 - [ ] 자동차 이름 입력받기 - - [ ] 쉼표(,)로 입력 받은 이름 구분하기 + - [x] 쉼표(,)로 입력 받은 이름 구분하기 - [ ] 각 이름 5자 이하의 이름인지 검사하기 -- [ ] 이동 횟수 입력 받기 - - [ ] 입력 값이 숫자인지 검사하기 - - [ ] 입력 값이 자연수 인지 검사하기 +- [x] 이동 횟수 입력 받기 + - [x] 입력 값이 숫자인지 검사하기 + - [x] 입력 값이 자연수 인지 검사하기 - [ ] 자동차 전진하기 - [ ] 입력 횟수 만큼 자동차 전진하기 - [ ] 0 ~ 9 사이의 무작위 값 구하기 diff --git a/src/main/java/view/InputView.java b/src/main/java/view/InputView.java index 5a97c949..de7c4576 100644 --- a/src/main/java/view/InputView.java +++ b/src/main/java/view/InputView.java @@ -14,6 +14,21 @@ public void inputName() { this.carNames = scanner.nextLine().split("\\s*,\\s*"); } + public void inputIteration() { + Scanner scanner = new Scanner(System.in); + System.out.println("시도할 회수는 몇회인가요?"); + try { // 횟수 입력 검사 + this.iteration = scanner.nextInt(); + if (this.iteration < 1) { + throw new IllegalArgumentException(); + } + } catch (InputMismatchException e) { // 숫자가 아닌 값을 입력 받은 경우 + System.out.println("[ERROR] : 시도할 회수는 자연수만 입력 가능합니다. 다시 입력 해주세요."); + } catch (IllegalArgumentException e) { // 0 또는 음의 정수를 입력 받은 경우 + System.out.println("[ERROR] : 시도할 회수는 자연수만 입력 가능합니다. 다시 입력 해주세요."); + } + } + public String[] getCarNames() { return this.carNames; } From 7b13aaac08d5dfc2c8d24cc5822de2afe051c749 Mon Sep 17 00:00:00 2001 From: doyooon Date: Mon, 10 Jun 2024 05:11:38 +0900 Subject: [PATCH 05/14] =?UTF-8?q?feat=20Car=20:=20Car=20=EA=B0=9D=EC=B2=B4?= =?UTF-8?q?=20=EA=B5=AC=ED=98=84,=200~9=20=EC=82=AC=EC=9D=B4=20=EB=AC=B4?= =?UTF-8?q?=EC=9E=91=EC=9C=84=20=EA=B0=92=EC=9D=84=20=EA=B5=AC=ED=95=98?= =?UTF-8?q?=EA=B3=A0=204=20=EC=9D=B4=EC=83=81=20=EC=9D=B8=20=EA=B2=BD?= =?UTF-8?q?=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 | 4 +-- src/main/java/model/Car.java | 49 ++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 src/main/java/model/Car.java diff --git a/README.md b/README.md index ea4b77f9..3d017448 100644 --- a/README.md +++ b/README.md @@ -9,8 +9,8 @@ - [x] 입력 값이 자연수 인지 검사하기 - [ ] 자동차 전진하기 - [ ] 입력 횟수 만큼 자동차 전진하기 - - [ ] 0 ~ 9 사이의 무작위 값 구하기 - - [ ] 무작위 값이 4 이상 인 경우 전진하기 + - [x] 0 ~ 9 사이의 무작위 값 구하기 + - [x] 무작위 값이 4 이상 인 경우 전진하기 - [ ] 각 횟수별로 전진 한 결과 출력하기 - [ ] 우승자 출력하기 - [ ] 우승자가 여러명일 경우 쉼표(,)로 구분해서 출력하기 \ No newline at end of file diff --git a/src/main/java/model/Car.java b/src/main/java/model/Car.java new file mode 100644 index 00000000..397fda6d --- /dev/null +++ b/src/main/java/model/Car.java @@ -0,0 +1,49 @@ +package model; + +import java.util.Random; + +public class Car { + + private String name; + private int distance = 0; + private boolean winner = false; + + public Car(String name) { + setName(name); + } + + private void setName(String name) { + this.name = name; + } + + private void setDistance(int distance) { + this.distance = distance; + } + + public void setWinner(boolean isWin) { + this.winner = isWin; + } + + public String getName() { + return this.name; + } + + public int getDistance() { + return this.distance; + } + + public boolean isWinner() { + return this.winner; + } + + public void moveForward() { + Random random = new Random(); + + int distance = random.nextInt(0, 10); + + if (distance > 3) { + setDistance(this.distance + 1); + } + } + +} From a09256634d979410077ee46b8a181684b805fff8 Mon Sep 17 00:00:00 2001 From: doyooon Date: Mon, 10 Jun 2024 05:30:00 +0900 Subject: [PATCH 06/14] =?UTF-8?q?feat=20RacingCarGame=20:=20=EC=9E=90?= =?UTF-8?q?=EB=8F=99=EC=B0=A8=20=EC=9D=B4=EB=A6=84=20=EC=9E=85=EB=A0=A5?= =?UTF-8?q?=EB=B0=9B=EA=B3=A0,=20=EA=B0=81=20=EC=9D=B4=EB=A6=84=EC=9D=B4?= =?UTF-8?q?=205=EC=9E=90=20=EC=9D=B4=ED=95=98=EC=9D=98=20=EC=9D=B4?= =?UTF-8?q?=EB=A6=84=EC=9D=B8=EC=A7=80=20=EA=B2=80=EC=82=AC=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 | 4 +- src/main/java/controller/RacingCarGame.java | 45 +++++++++++++++++++++ 2 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 src/main/java/controller/RacingCarGame.java diff --git a/README.md b/README.md index 3d017448..9cf45042 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,9 @@ # java-racingcar-precourse ### 기능 요구사항 목록 -- [ ] 자동차 이름 입력받기 +- [x] 자동차 이름 입력받기 - [x] 쉼표(,)로 입력 받은 이름 구분하기 - - [ ] 각 이름 5자 이하의 이름인지 검사하기 + - [x] 각 이름 5자 이하의 이름인지 검사하기 - [x] 이동 횟수 입력 받기 - [x] 입력 값이 숫자인지 검사하기 - [x] 입력 값이 자연수 인지 검사하기 diff --git a/src/main/java/controller/RacingCarGame.java b/src/main/java/controller/RacingCarGame.java new file mode 100644 index 00000000..c0e73521 --- /dev/null +++ b/src/main/java/controller/RacingCarGame.java @@ -0,0 +1,45 @@ +package controller; + +import java.util.ArrayList; +import model.Car; +import view.InputView; + +public class RacingCarGame { + + private final InputView inputView = new InputView(); + private final int MAX_NAME_LENGTH = 5; + + public void racingGame() { + ArrayList cars = new ArrayList<>(); + input(cars); + } + + private ArrayList input(ArrayList cars) throws IllegalArgumentException { + inputView.inputName(); + + for (String name : inputView.getCarNames()) { + if (!checkNameLength(name)) { + return input(cars); + } + cars.add(new Car(name)); + } + + inputView.inputIteration(); + + return cars; + } + + private boolean checkNameLength(String name) { + try { + if (name.length() > MAX_NAME_LENGTH + 1) { + throw new IllegalArgumentException(); + } + } catch (IllegalArgumentException e) { + System.out.println("[ERROR] : 이름은 " + MAX_NAME_LENGTH + "자 이하만 가능합니다." + name + " 부분부터 다시 입력해 주세요."); + return false; + } + + return true; + } + +} From 81f683745c5e2d4b0d3f52aea4844f546632477d Mon Sep 17 00:00:00 2001 From: doyooon Date: Mon, 10 Jun 2024 05:34:28 +0900 Subject: [PATCH 07/14] =?UTF-8?q?feat=20OutputView=20:=20=EC=9E=90?= =?UTF-8?q?=EB=8F=99=EC=B0=A8=20=EC=A0=84=EC=A7=84=20=ED=95=9C=20=EA=B2=B0?= =?UTF-8?q?=EA=B3=BC,=20=EC=9A=B0=EC=8A=B9=EC=9E=90=20=EC=B6=9C=EB=A0=A5?= =?UTF-8?q?=ED=95=98=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 6 +++--- src/main/java/view/OutputView.java | 32 ++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 src/main/java/view/OutputView.java diff --git a/README.md b/README.md index 9cf45042..620d2efd 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,6 @@ - [ ] 입력 횟수 만큼 자동차 전진하기 - [x] 0 ~ 9 사이의 무작위 값 구하기 - [x] 무작위 값이 4 이상 인 경우 전진하기 -- [ ] 각 횟수별로 전진 한 결과 출력하기 -- [ ] 우승자 출력하기 - - [ ] 우승자가 여러명일 경우 쉼표(,)로 구분해서 출력하기 \ No newline at end of file +- [x] 각 횟수별로 전진 한 결과 출력하기 +- [x] 우승자 출력하기 + - [x] 우승자가 여러명일 경우 쉼표(,)로 구분해서 출력하기 \ No newline at end of file diff --git a/src/main/java/view/OutputView.java b/src/main/java/view/OutputView.java new file mode 100644 index 00000000..0126b70b --- /dev/null +++ b/src/main/java/view/OutputView.java @@ -0,0 +1,32 @@ +package view; + +import java.util.ArrayList; +import model.Car; + +public class OutputView { + + public void showExecutionResult() { + System.out.println("실행 결과"); + } + + public void showCarMove(Car car) { + int distance = car.getDistance(); + String name = car.getName(); + + System.out.println(name + " : " + ("-".repeat(distance))); + } + + public void showWinner(ArrayList cars) { + ArrayList winners = new ArrayList<>(); + + for (Car car : cars) { + if (car.isWinner()) { + winners.add(car.getName()); + } + } + + String winnersName = String.join(",", winners); + + System.out.println("최종 우승자 : " + winnersName); + } +} From 0f2477952004a899218c1043eb081d829addcc6c Mon Sep 17 00:00:00 2001 From: doyooon Date: Mon, 10 Jun 2024 05:36:56 +0900 Subject: [PATCH 08/14] =?UTF-8?q?feat=20RacingCarGame=20:=20=EC=9E=85?= =?UTF-8?q?=EB=A0=A5=20=ED=9A=9F=EC=88=98=20=EB=A7=8C=ED=81=BC=20=EC=9E=90?= =?UTF-8?q?=EB=8F=99=EC=B0=A8=20=EC=A0=84=EC=A7=84=ED=95=98=EA=B8=B0,=20?= =?UTF-8?q?=EC=A0=84=EC=A7=84=ED=95=9C=20=EA=B2=B0=EA=B3=BC=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 | 4 ++-- src/main/java/controller/RacingCarGame.java | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 620d2efd..6e658193 100644 --- a/README.md +++ b/README.md @@ -7,8 +7,8 @@ - [x] 이동 횟수 입력 받기 - [x] 입력 값이 숫자인지 검사하기 - [x] 입력 값이 자연수 인지 검사하기 -- [ ] 자동차 전진하기 - - [ ] 입력 횟수 만큼 자동차 전진하기 +- [x] 자동차 전진하기 + - [x] 입력 횟수 만큼 자동차 전진하기 - [x] 0 ~ 9 사이의 무작위 값 구하기 - [x] 무작위 값이 4 이상 인 경우 전진하기 - [x] 각 횟수별로 전진 한 결과 출력하기 diff --git a/src/main/java/controller/RacingCarGame.java b/src/main/java/controller/RacingCarGame.java index c0e73521..2efb816e 100644 --- a/src/main/java/controller/RacingCarGame.java +++ b/src/main/java/controller/RacingCarGame.java @@ -3,15 +3,19 @@ import java.util.ArrayList; import model.Car; import view.InputView; +import view.OutputView; public class RacingCarGame { private final InputView inputView = new InputView(); + private final OutputView outputView = new OutputView(); private final int MAX_NAME_LENGTH = 5; public void racingGame() { ArrayList cars = new ArrayList<>(); input(cars); + System.out.println(); + output(cars); } private ArrayList input(ArrayList cars) throws IllegalArgumentException { @@ -42,4 +46,18 @@ private boolean checkNameLength(String name) { return true; } + private void output(ArrayList cars){ + outputView.showExecutionResult(); + + for (int i = 0; i < inputView.getIteration(); i++) { + for (Car car : cars) { + car.moveForward(); + outputView.showCarMove(car); + } + System.out.println(); + } + + outputView.showWinner(cars); + } + } From b344e894ad3484dbc05df081bc16062a15a60a75 Mon Sep 17 00:00:00 2001 From: doyooon Date: Mon, 10 Jun 2024 05:38:25 +0900 Subject: [PATCH 09/14] =?UTF-8?q?docs=20README.md=20:=20=EC=9A=B0=EC=8A=B9?= =?UTF-8?q?=EC=9E=90=20=EC=B0=BE=EA=B8=B0=20=EA=B8=B0=EB=8A=A5=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 6e658193..1ff20bb5 100644 --- a/README.md +++ b/README.md @@ -13,4 +13,5 @@ - [x] 무작위 값이 4 이상 인 경우 전진하기 - [x] 각 횟수별로 전진 한 결과 출력하기 - [x] 우승자 출력하기 - - [x] 우승자가 여러명일 경우 쉼표(,)로 구분해서 출력하기 \ No newline at end of file + - [x] 우승자가 여러명일 경우 쉼표(,)로 구분해서 출력하기 + - [ ] 우승자 찾기 \ No newline at end of file From 05a387a5e7ff904d12b2e7753e17fe0eb79aaa74 Mon Sep 17 00:00:00 2001 From: doyooon Date: Mon, 10 Jun 2024 05:41:55 +0900 Subject: [PATCH 10/14] =?UTF-8?q?feat=20RacingCarGame=20:=20=EC=9A=B0?= =?UTF-8?q?=EC=8A=B9=EC=9E=90=20=EC=B0=BE=EA=B8=B0,=20=EC=9A=B0=EC=8A=B9?= =?UTF-8?q?=EC=9E=90=20=EC=B6=9C=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 | 2 +- src/main/java/Application.java | 11 +++++++++++ src/main/java/controller/RacingCarGame.java | 22 +++++++++++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 src/main/java/Application.java diff --git a/README.md b/README.md index 1ff20bb5..941aa82c 100644 --- a/README.md +++ b/README.md @@ -14,4 +14,4 @@ - [x] 각 횟수별로 전진 한 결과 출력하기 - [x] 우승자 출력하기 - [x] 우승자가 여러명일 경우 쉼표(,)로 구분해서 출력하기 - - [ ] 우승자 찾기 \ No newline at end of file + - [x] 우승자 찾기 \ No newline at end of file diff --git a/src/main/java/Application.java b/src/main/java/Application.java new file mode 100644 index 00000000..c829182b --- /dev/null +++ b/src/main/java/Application.java @@ -0,0 +1,11 @@ +import controller.RacingCarGame; +import java.io.IOException; + +public class Application { + + private static final RacingCarGame racingCarGame = new RacingCarGame(); + + public static void main(String[] args) throws IOException { + racingCarGame.racingGame(); + } +} \ No newline at end of file diff --git a/src/main/java/controller/RacingCarGame.java b/src/main/java/controller/RacingCarGame.java index 2efb816e..a06c21cd 100644 --- a/src/main/java/controller/RacingCarGame.java +++ b/src/main/java/controller/RacingCarGame.java @@ -57,7 +57,29 @@ private void output(ArrayList cars){ System.out.println(); } + findWinner(cars); outputView.showWinner(cars); } + private void findWinner(ArrayList cars) { + int furthestDistance = findFurthestDistance(cars); + + for (Car car : cars) { + if (car.getDistance() == furthestDistance) { + car.setWinner(true); + } + } + } + + private int findFurthestDistance(ArrayList cars){ + int furthestDistance = 0; + for (Car car : cars) { + if (car.getDistance() > furthestDistance) { + furthestDistance = car.getDistance(); + } + } + + return furthestDistance; + } + } From 1264df61ce526480c367b6f1643cdc8a0e305fa0 Mon Sep 17 00:00:00 2001 From: doyooon Date: Mon, 10 Jun 2024 07:37:23 +0900 Subject: [PATCH 11/14] =?UTF-8?q?refactor=20InputView=20:=20inputIteration?= =?UTF-8?q?=EC=97=90=EC=84=9C=20=EC=9E=85=EB=A0=A5=EA=B0=92=20=EA=B2=80?= =?UTF-8?q?=EC=82=AC=ED=95=98=EA=B8=B0=20=EC=A0=84=EC=97=90=20=EC=9E=85?= =?UTF-8?q?=EB=A0=A5=EA=B0=92=EC=9D=84=20=EB=A9=A4=EB=B2=84=EB=B3=80?= =?UTF-8?q?=EC=88=98=EC=97=90=20=EC=A0=80=EC=9E=A5=ED=95=98=EB=8A=94=20?= =?UTF-8?q?=EB=A1=9C=EC=A7=81=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/view/InputView.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/view/InputView.java b/src/main/java/view/InputView.java index de7c4576..6deb2461 100644 --- a/src/main/java/view/InputView.java +++ b/src/main/java/view/InputView.java @@ -18,10 +18,11 @@ public void inputIteration() { Scanner scanner = new Scanner(System.in); System.out.println("시도할 회수는 몇회인가요?"); try { // 횟수 입력 검사 - this.iteration = scanner.nextInt(); - if (this.iteration < 1) { + int iteration = scanner.nextInt(); + if (iteration < 1) { throw new IllegalArgumentException(); } + this.iteration = iteration; } catch (InputMismatchException e) { // 숫자가 아닌 값을 입력 받은 경우 System.out.println("[ERROR] : 시도할 회수는 자연수만 입력 가능합니다. 다시 입력 해주세요."); } catch (IllegalArgumentException e) { // 0 또는 음의 정수를 입력 받은 경우 From 9a5fb045f05bb9a5708ac49b6480828cf9c31872 Mon Sep 17 00:00:00 2001 From: doyooon Date: Mon, 10 Jun 2024 10:31:50 +0900 Subject: [PATCH 12/14] =?UTF-8?q?refactor=20InputView=20:=20=EC=98=AC?= =?UTF-8?q?=EB=B0=94=EB=A5=B8=20=EC=9E=85=EB=A0=A5=EC=9D=84=20=EB=B0=9B?= =?UTF-8?q?=EC=9D=84=20=EB=95=8C=EA=B9=8C=EC=A7=80=20inputIteration?= =?UTF-8?q?=EC=9D=84=20=EB=B0=98=EB=B3=B5=20=EC=88=98=ED=96=89=ED=95=98?= =?UTF-8?q?=EB=8F=84=EB=A1=9D=20=EB=A1=9C=EC=A7=81=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/controller/RacingCarGame.java | 18 ++++++++++-------- src/main/java/view/InputView.java | 14 +++++++------- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/src/main/java/controller/RacingCarGame.java b/src/main/java/controller/RacingCarGame.java index a06c21cd..68395a26 100644 --- a/src/main/java/controller/RacingCarGame.java +++ b/src/main/java/controller/RacingCarGame.java @@ -28,25 +28,27 @@ private ArrayList input(ArrayList cars) throws IllegalArgumentExceptio cars.add(new Car(name)); } - inputView.inputIteration(); - + System.out.println("시도할 회수는 몇회인가요?"); + while (!inputView.inputIteration()) { + } return cars; } - private boolean checkNameLength(String name) { + public boolean checkNameLength(String name) { try { - if (name.length() > MAX_NAME_LENGTH + 1) { + if (name.length() > MAX_NAME_LENGTH) { throw new IllegalArgumentException(); } } catch (IllegalArgumentException e) { - System.out.println("[ERROR] : 이름은 " + MAX_NAME_LENGTH + "자 이하만 가능합니다." + name + " 부분부터 다시 입력해 주세요."); + System.out.println( + "[ERROR] : 이름은 " + MAX_NAME_LENGTH + "자 이하만 가능합니다." + name + " 부분부터 다시 입력해 주세요."); return false; } return true; } - private void output(ArrayList cars){ + public void output(ArrayList cars) { outputView.showExecutionResult(); for (int i = 0; i < inputView.getIteration(); i++) { @@ -61,7 +63,7 @@ private void output(ArrayList cars){ outputView.showWinner(cars); } - private void findWinner(ArrayList cars) { + public void findWinner(ArrayList cars) { int furthestDistance = findFurthestDistance(cars); for (Car car : cars) { @@ -71,7 +73,7 @@ private void findWinner(ArrayList cars) { } } - private int findFurthestDistance(ArrayList cars){ + public int findFurthestDistance(ArrayList cars) { int furthestDistance = 0; for (Car car : cars) { if (car.getDistance() > furthestDistance) { diff --git a/src/main/java/view/InputView.java b/src/main/java/view/InputView.java index 6deb2461..048a33c3 100644 --- a/src/main/java/view/InputView.java +++ b/src/main/java/view/InputView.java @@ -14,22 +14,22 @@ public void inputName() { this.carNames = scanner.nextLine().split("\\s*,\\s*"); } - public void inputIteration() { + public boolean inputIteration() { Scanner scanner = new Scanner(System.in); - System.out.println("시도할 회수는 몇회인가요?"); - try { // 횟수 입력 검사 + try { int iteration = scanner.nextInt(); if (iteration < 1) { - throw new IllegalArgumentException(); + throw new IllegalArgumentException("[ERROR] : 시도할 회수는 자연수만 입력 가능합니다. 다시 입력 해주세요."); } this.iteration = iteration; - } catch (InputMismatchException e) { // 숫자가 아닌 값을 입력 받은 경우 - System.out.println("[ERROR] : 시도할 회수는 자연수만 입력 가능합니다. 다시 입력 해주세요."); - } catch (IllegalArgumentException e) { // 0 또는 음의 정수를 입력 받은 경우 + return true; + } catch (InputMismatchException | IllegalArgumentException e) { // 올바르지 않은 값을 입력 받은 경우 System.out.println("[ERROR] : 시도할 회수는 자연수만 입력 가능합니다. 다시 입력 해주세요."); + return false; } } + public String[] getCarNames() { return this.carNames; } From a65ff359dc693d288b384f6c0c509c7c1e959cb2 Mon Sep 17 00:00:00 2001 From: doyooon Date: Mon, 10 Jun 2024 11:16:39 +0900 Subject: [PATCH 13/14] =?UTF-8?q?feat=20CarTest=20:=20Car=20domain=20logic?= =?UTF-8?q?=20test=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/model/Car.java | 4 +-- src/test/java/model/CarTest.java | 62 ++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 src/test/java/model/CarTest.java diff --git a/src/main/java/model/Car.java b/src/main/java/model/Car.java index 397fda6d..3b223d3e 100644 --- a/src/main/java/model/Car.java +++ b/src/main/java/model/Car.java @@ -12,11 +12,11 @@ public Car(String name) { setName(name); } - private void setName(String name) { + public void setName(String name) { this.name = name; } - private void setDistance(int distance) { + public void setDistance(int distance) { this.distance = distance; } diff --git a/src/test/java/model/CarTest.java b/src/test/java/model/CarTest.java new file mode 100644 index 00000000..6e523b5d --- /dev/null +++ b/src/test/java/model/CarTest.java @@ -0,0 +1,62 @@ +package model; + +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.RepeatedTest; +import org.junit.jupiter.api.Test; + +class CarTest { + + private Car car; + + @BeforeEach + void setCar(){ + car = new Car("test"); + } + + @Test + @DisplayName("이름이 제대로 입력 되는지 테스트") + void getName(){ + assertEquals("test", car.getName()); + } + + @RepeatedTest(100) + @DisplayName("자동차가 한칸씩 전진하는지 테스트") + void moveForward_한칸_전진() { + // given + int beforeDistance = car.getDistance(); + // when + car.moveForward(); + // then + assertTrue(car.getDistance() - beforeDistance == 1 || car.getDistance() == beforeDistance); + } + + @Test + @DisplayName("자동차가 한번에 두칸 이상 전진 테스트") + void moveForward_한번에_두칸_이상_전진(){ + // given + int beforeDistance = car.getDistance(); + // when + car.moveForward(); + // then + assertFalse(car.getDistance() - beforeDistance > 1); + + } + + @Test + @DisplayName("자동차 여러번 전진 테스트") + void moveForward_여러번_전진() { + // given + int beforeDistance = car.getDistance(); + int repeat = 100; + // when + for (int i = 0; i < repeat; i++) { + car.moveForward(); + } + // then + assertTrue(car.getDistance() - beforeDistance >= 0 && car.getDistance() - beforeDistance <= repeat); + } + +} \ No newline at end of file From 92aca1a3cc1cd147525225d58b54e8c89f81c013 Mon Sep 17 00:00:00 2001 From: doyooon Date: Mon, 10 Jun 2024 11:17:17 +0900 Subject: [PATCH 14/14] =?UTF-8?q?feat=20RacingCarGameTest=20:=20RacingCarG?= =?UTF-8?q?ame=20domain=20logic=20test=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/controller/RacingCarGameTest.java | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 src/test/java/controller/RacingCarGameTest.java diff --git a/src/test/java/controller/RacingCarGameTest.java b/src/test/java/controller/RacingCarGameTest.java new file mode 100644 index 00000000..536fa476 --- /dev/null +++ b/src/test/java/controller/RacingCarGameTest.java @@ -0,0 +1,55 @@ +package controller; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.jupiter.api.Assertions.*; + +import java.util.ArrayList; +import model.Car; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +class RacingCarGameTest { + + private RacingCarGame racingCarGame = new RacingCarGame(); + + @Test + @DisplayName("이름이 5자 넘는 경우 테스트") + void checkNameLength_이름이_5자_초과() { + // given + String name = "abcdef"; + // when + boolean actual = racingCarGame.checkNameLength(name); + // then + assertEquals(false, actual); + } + + @Test + @DisplayName("이름이 5자 이하로 알맞게 들어온 경우 테스트") + void checkNameLength_이름이_5자_이하() { + // given + String name = "abcd"; + // when + boolean actual = racingCarGame.checkNameLength(name); + // then + assertEquals(true, actual); + } + + @Test + @DisplayName("우승자 찾기 테스트") + void findWinner() { + // given + ArrayList cars = new ArrayList<>(); + Car winner = new Car("winner"); + winner.setDistance(2); + cars.add(winner); + Car loser = new Car("loser"); + loser.setDistance(1); + cars.add(loser); + //when + racingCarGame.findWinner(cars); + //then + assertEquals(true, winner.isWinner()); + assertEquals(false, loser.isWinner()); + } +} \ No newline at end of file