From b37e0505885caae40a28551b354c31ac20e5a5dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=97=B0=EC=A7=84?= Date: Fri, 8 Sep 2023 15:09:51 +0900 Subject: [PATCH 01/34] =?UTF-8?q?docs:=20=EA=B8=B0=EB=8A=A5=20=EB=AA=A9?= =?UTF-8?q?=EB=A1=9D=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 docs/README.md diff --git a/docs/README.md b/docs/README.md new file mode 100644 index 0000000000..c17d5ae9bf --- /dev/null +++ b/docs/README.md @@ -0,0 +1,17 @@ +## 기능 목록 + +- [ ] 이름 입력 안내 출력 +- [ ] 이름 입력 + - [ ] `,`로 이름을 나누어 입력하지 않은 경우 예외 발생 + - [ ] 이름이 5글자가 넘을 경우 예외 발생 +- [ ] 횟수 입력 안내 출력 +- [ ] 횟수 입력 + - [ ] 숫자로 입력하지 않은 경우 예외 발생 +- [ ] 입력받은 횟수만큼 자동차 이동 + - [ ] 랜덤으로 숫자를 뽑는다. + - [ ] 뽑은 숫자가 4이상일 경우 이동한다. +- [ ] 최종 결과 + - [ ] 가장 멀리 나간 자동차가 무엇인지 구한다. +- [ ] 최종 우승자 출력 + - [ ] 우승자가 한 명일 경우 그냥 출력 + - [ ] 우승자가 여러명일 경우 `,`로 나누어 출력 \ No newline at end of file From 809116fb1373bd3fb7d1f8cf47ea1ba8ffb302e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=97=B0=EC=A7=84?= Date: Fri, 8 Sep 2023 15:11:41 +0900 Subject: [PATCH 02/34] =?UTF-8?q?refactor(Car):=20=ED=8C=A8=ED=82=A4?= =?UTF-8?q?=EC=A7=80=20=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/{ => domain}/Car.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename src/main/java/racingcar/{ => domain}/Car.java (87%) diff --git a/src/main/java/racingcar/Car.java b/src/main/java/racingcar/domain/Car.java similarity index 87% rename from src/main/java/racingcar/Car.java rename to src/main/java/racingcar/domain/Car.java index ab3df94921..7ab69bf089 100644 --- a/src/main/java/racingcar/Car.java +++ b/src/main/java/racingcar/domain/Car.java @@ -1,4 +1,4 @@ -package racingcar; +package racingcar.domain; public class Car { private final String name; From 88a3fded6ee7d6e5f8d80aa46c0edd07547fc998 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=97=B0=EC=A7=84?= Date: Fri, 8 Sep 2023 15:31:39 +0900 Subject: [PATCH 03/34] =?UTF-8?q?feat(OutputView):=20=EC=9D=B4=EB=A6=84=20?= =?UTF-8?q?=EC=9E=85=EB=A0=A5=20=EC=95=88=EB=82=B4=20=EC=B6=9C=EB=A0=A5=20?= =?UTF-8?q?=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 2 +- .../java/racingcar/constant/OutputMessage.java | 16 ++++++++++++++++ src/main/java/racingcar/view/OutputView.java | 10 ++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 src/main/java/racingcar/constant/OutputMessage.java create mode 100644 src/main/java/racingcar/view/OutputView.java diff --git a/docs/README.md b/docs/README.md index c17d5ae9bf..8616f829f8 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,6 +1,6 @@ ## 기능 목록 -- [ ] 이름 입력 안내 출력 +- [x] 이름 입력 안내 출력 - [ ] 이름 입력 - [ ] `,`로 이름을 나누어 입력하지 않은 경우 예외 발생 - [ ] 이름이 5글자가 넘을 경우 예외 발생 diff --git a/src/main/java/racingcar/constant/OutputMessage.java b/src/main/java/racingcar/constant/OutputMessage.java new file mode 100644 index 0000000000..947c17f7de --- /dev/null +++ b/src/main/java/racingcar/constant/OutputMessage.java @@ -0,0 +1,16 @@ +package racingcar.constant; + +public enum OutputMessage { + READ_NAME("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)"); + + private final String message; + + OutputMessage(String message) { + this.message = message; + } + + @Override + public String toString() { + return message; + } +} diff --git a/src/main/java/racingcar/view/OutputView.java b/src/main/java/racingcar/view/OutputView.java new file mode 100644 index 0000000000..fab49a6c46 --- /dev/null +++ b/src/main/java/racingcar/view/OutputView.java @@ -0,0 +1,10 @@ +package racingcar.view; + +import racingcar.constant.OutputMessage; + +public class OutputView { + + public void printCarNames() { + System.out.println(OutputMessage.READ_NAME); + } +} From 0820e66bc8e5f7f19d5839158e854dcab2ef2808 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=97=B0=EC=A7=84?= Date: Fri, 8 Sep 2023 15:32:04 +0900 Subject: [PATCH 04/34] =?UTF-8?q?feat(RacingCarController):=20=EA=B2=8C?= =?UTF-8?q?=EC=9E=84=20=EC=8B=9C=EC=9E=91=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 --- src/main/java/controller/RacingCarController.java | 12 ++++++++++++ src/main/java/racingcar/Application.java | 5 ++++- 2 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 src/main/java/controller/RacingCarController.java diff --git a/src/main/java/controller/RacingCarController.java b/src/main/java/controller/RacingCarController.java new file mode 100644 index 0000000000..a0e4e788c9 --- /dev/null +++ b/src/main/java/controller/RacingCarController.java @@ -0,0 +1,12 @@ +package controller; + +import racingcar.view.OutputView; + +public class RacingCarController { + + OutputView outputView = new OutputView(); + + public void start() { + outputView.printCarNames(); + } +} diff --git a/src/main/java/racingcar/Application.java b/src/main/java/racingcar/Application.java index b9ed0456a3..989a56c7c4 100644 --- a/src/main/java/racingcar/Application.java +++ b/src/main/java/racingcar/Application.java @@ -1,7 +1,10 @@ package racingcar; +import controller.RacingCarController; + public class Application { public static void main(String[] args) { - // TODO 구현 진행 + RacingCarController racingCarController = new RacingCarController(); + racingCarController.start(); } } From 4c258e7bfceb0da485204d66d7f32996943f9129 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=97=B0=EC=A7=84?= Date: Fri, 8 Sep 2023 15:37:50 +0900 Subject: [PATCH 05/34] =?UTF-8?q?feat(InputView):=20=EC=9D=B4=EB=A6=84=20?= =?UTF-8?q?=EC=9E=85=EB=A0=A5=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 2 +- .../java/controller/RacingCarController.java | 5 +++++ src/main/java/racingcar/view/InputView.java | 18 ++++++++++++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 src/main/java/racingcar/view/InputView.java diff --git a/docs/README.md b/docs/README.md index 8616f829f8..ed47e3248f 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,7 +1,7 @@ ## 기능 목록 - [x] 이름 입력 안내 출력 -- [ ] 이름 입력 +- [x] 이름 입력 - [ ] `,`로 이름을 나누어 입력하지 않은 경우 예외 발생 - [ ] 이름이 5글자가 넘을 경우 예외 발생 - [ ] 횟수 입력 안내 출력 diff --git a/src/main/java/controller/RacingCarController.java b/src/main/java/controller/RacingCarController.java index a0e4e788c9..0efcd0a9af 100644 --- a/src/main/java/controller/RacingCarController.java +++ b/src/main/java/controller/RacingCarController.java @@ -1,12 +1,17 @@ package controller; +import racingcar.view.InputView; import racingcar.view.OutputView; +import java.util.List; + public class RacingCarController { OutputView outputView = new OutputView(); + InputView interView = new InputView(); public void start() { outputView.printCarNames(); + List names = interView.readNames(); } } diff --git a/src/main/java/racingcar/view/InputView.java b/src/main/java/racingcar/view/InputView.java new file mode 100644 index 0000000000..a272d27a78 --- /dev/null +++ b/src/main/java/racingcar/view/InputView.java @@ -0,0 +1,18 @@ +package racingcar.view; + +import camp.nextstep.edu.missionutils.Console; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +public class InputView { + + private static final String NAMES_REGEX = ","; + + public List readNames() { + String names = Console.readLine(); + return Arrays.stream(names.split(NAMES_REGEX)) + .collect(Collectors.toList()); + } +} From 23ecd46cdc68b33fb6ba9f3c94d98849c63b1737 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=97=B0=EC=A7=84?= Date: Fri, 8 Sep 2023 15:44:03 +0900 Subject: [PATCH 06/34] =?UTF-8?q?feat(Name):=20=EC=9D=B4=EB=A6=84=EC=9D=B4?= =?UTF-8?q?=205=EA=B8=80=EC=9E=90=EA=B0=80=20=EB=84=98=EC=9D=84=20?= =?UTF-8?q?=EA=B2=BD=EC=9A=B0=20=EC=98=88=EC=99=B8=20=EB=B0=9C=EC=83=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 2 +- .../java/controller/RacingCarController.java | 3 ++- .../racingcar/constant/ExceptionMessage.java | 18 +++++++++++++ src/main/java/racingcar/domain/Name.java | 25 +++++++++++++++++++ src/main/java/racingcar/view/InputView.java | 4 ++- 5 files changed, 49 insertions(+), 3 deletions(-) create mode 100644 src/main/java/racingcar/constant/ExceptionMessage.java create mode 100644 src/main/java/racingcar/domain/Name.java diff --git a/docs/README.md b/docs/README.md index ed47e3248f..2f534651e2 100644 --- a/docs/README.md +++ b/docs/README.md @@ -3,7 +3,7 @@ - [x] 이름 입력 안내 출력 - [x] 이름 입력 - [ ] `,`로 이름을 나누어 입력하지 않은 경우 예외 발생 - - [ ] 이름이 5글자가 넘을 경우 예외 발생 + - [x] 이름이 5글자가 넘을 경우 예외 발생 - [ ] 횟수 입력 안내 출력 - [ ] 횟수 입력 - [ ] 숫자로 입력하지 않은 경우 예외 발생 diff --git a/src/main/java/controller/RacingCarController.java b/src/main/java/controller/RacingCarController.java index 0efcd0a9af..f332e5c45c 100644 --- a/src/main/java/controller/RacingCarController.java +++ b/src/main/java/controller/RacingCarController.java @@ -1,5 +1,6 @@ package controller; +import racingcar.domain.Name; import racingcar.view.InputView; import racingcar.view.OutputView; @@ -12,6 +13,6 @@ public class RacingCarController { public void start() { outputView.printCarNames(); - List names = interView.readNames(); + List names = interView.readNames(); } } diff --git a/src/main/java/racingcar/constant/ExceptionMessage.java b/src/main/java/racingcar/constant/ExceptionMessage.java new file mode 100644 index 0000000000..4a97be1d96 --- /dev/null +++ b/src/main/java/racingcar/constant/ExceptionMessage.java @@ -0,0 +1,18 @@ +package racingcar.constant; + +public enum ExceptionMessage { + + INVALID_LENGTH("이름은 5글자 이하로 입력해야합니다."); + + private static final String PREFIX = "[ERROR] "; + private final String message; + + ExceptionMessage(String message) { + this.message = message; + } + + @Override + public String toString() { + return PREFIX + message; + } +} diff --git a/src/main/java/racingcar/domain/Name.java b/src/main/java/racingcar/domain/Name.java new file mode 100644 index 0000000000..5744be0178 --- /dev/null +++ b/src/main/java/racingcar/domain/Name.java @@ -0,0 +1,25 @@ +package racingcar.domain; + +import racingcar.constant.ExceptionMessage; + +public class Name { + + private static final int MAX_LENGTH = 5; + private final String value; + + public Name(String value) { + validateName(value); + this.value = value; + } + + private void validateName(String value) { + if (value.length() > MAX_LENGTH) { + throw new IllegalArgumentException(ExceptionMessage.INVALID_LENGTH.toString()); + } + } + + @Override + public String toString() { + return value; + } +} diff --git a/src/main/java/racingcar/view/InputView.java b/src/main/java/racingcar/view/InputView.java index a272d27a78..6c78affed5 100644 --- a/src/main/java/racingcar/view/InputView.java +++ b/src/main/java/racingcar/view/InputView.java @@ -1,6 +1,7 @@ package racingcar.view; import camp.nextstep.edu.missionutils.Console; +import racingcar.domain.Name; import java.util.Arrays; import java.util.List; @@ -10,9 +11,10 @@ public class InputView { private static final String NAMES_REGEX = ","; - public List readNames() { + public List readNames() { String names = Console.readLine(); return Arrays.stream(names.split(NAMES_REGEX)) + .map(Name::new) .collect(Collectors.toList()); } } From 3398f73812082e584f2d472df2c4f03d5b6f6700 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=97=B0=EC=A7=84?= Date: Fri, 8 Sep 2023 15:46:20 +0900 Subject: [PATCH 07/34] =?UTF-8?q?feat(OutputView):=20=ED=9A=9F=EC=88=98=20?= =?UTF-8?q?=EC=9E=85=EB=A0=A5=20=EC=95=88=EB=82=B4=20=EC=B6=9C=EB=A0=A5=20?= =?UTF-8?q?=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 2 +- src/main/java/racingcar/constant/OutputMessage.java | 3 ++- src/main/java/racingcar/view/OutputView.java | 4 ++++ 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/docs/README.md b/docs/README.md index 2f534651e2..a55c8e1f6a 100644 --- a/docs/README.md +++ b/docs/README.md @@ -4,7 +4,7 @@ - [x] 이름 입력 - [ ] `,`로 이름을 나누어 입력하지 않은 경우 예외 발생 - [x] 이름이 5글자가 넘을 경우 예외 발생 -- [ ] 횟수 입력 안내 출력 +- [x] 횟수 입력 안내 출력 - [ ] 횟수 입력 - [ ] 숫자로 입력하지 않은 경우 예외 발생 - [ ] 입력받은 횟수만큼 자동차 이동 diff --git a/src/main/java/racingcar/constant/OutputMessage.java b/src/main/java/racingcar/constant/OutputMessage.java index 947c17f7de..a7cfffd02a 100644 --- a/src/main/java/racingcar/constant/OutputMessage.java +++ b/src/main/java/racingcar/constant/OutputMessage.java @@ -1,7 +1,8 @@ package racingcar.constant; public enum OutputMessage { - READ_NAME("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)"); + READ_NAME("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)"), + READ_TRY_COUNT("시도할 회수는 몇회인가요?"); private final String message; diff --git a/src/main/java/racingcar/view/OutputView.java b/src/main/java/racingcar/view/OutputView.java index fab49a6c46..892e479635 100644 --- a/src/main/java/racingcar/view/OutputView.java +++ b/src/main/java/racingcar/view/OutputView.java @@ -7,4 +7,8 @@ public class OutputView { public void printCarNames() { System.out.println(OutputMessage.READ_NAME); } + + public void printTryCount() { + System.out.println(OutputMessage.READ_TRY_COUNT); + } } From 94c4d6ef48ac2687a77f89e8fdedb835d21fd782 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=97=B0=EC=A7=84?= Date: Fri, 8 Sep 2023 15:47:52 +0900 Subject: [PATCH 08/34] =?UTF-8?q?feat(InputView):=20=ED=9A=9F=EC=88=98=20?= =?UTF-8?q?=EC=9E=85=EB=A0=A5=20=EA=B8=B0=EB=8A=A5=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/racingcar/view/InputView.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/java/racingcar/view/InputView.java b/src/main/java/racingcar/view/InputView.java index 6c78affed5..4607ea1aa3 100644 --- a/src/main/java/racingcar/view/InputView.java +++ b/src/main/java/racingcar/view/InputView.java @@ -17,4 +17,8 @@ public List readNames() { .map(Name::new) .collect(Collectors.toList()); } + + public int readTryCount() { + return Integer.parseInt(Console.readLine()); + } } From b680d76f29ca6fee172542097cce35cfd0490b75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=97=B0=EC=A7=84?= Date: Fri, 8 Sep 2023 15:49:58 +0900 Subject: [PATCH 09/34] =?UTF-8?q?feat(RacingCarController):=20=ED=9A=9F?= =?UTF-8?q?=EC=88=98=20=EC=9E=85=EC=B6=9C=EB=A0=A5=20=EA=B8=B0=EB=8A=A5=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 2 +- src/main/java/controller/RacingCarController.java | 14 ++++++++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/docs/README.md b/docs/README.md index a55c8e1f6a..7431f70ee5 100644 --- a/docs/README.md +++ b/docs/README.md @@ -5,7 +5,7 @@ - [ ] `,`로 이름을 나누어 입력하지 않은 경우 예외 발생 - [x] 이름이 5글자가 넘을 경우 예외 발생 - [x] 횟수 입력 안내 출력 -- [ ] 횟수 입력 +- [x] 횟수 입력 - [ ] 숫자로 입력하지 않은 경우 예외 발생 - [ ] 입력받은 횟수만큼 자동차 이동 - [ ] 랜덤으로 숫자를 뽑는다. diff --git a/src/main/java/controller/RacingCarController.java b/src/main/java/controller/RacingCarController.java index f332e5c45c..7bc8a8eda3 100644 --- a/src/main/java/controller/RacingCarController.java +++ b/src/main/java/controller/RacingCarController.java @@ -9,10 +9,20 @@ public class RacingCarController { OutputView outputView = new OutputView(); - InputView interView = new InputView(); + InputView inputView = new InputView(); public void start() { + List names = readNames(); + int tryCount = readTryCount(); + } + + private List readNames() { outputView.printCarNames(); - List names = interView.readNames(); + return inputView.readNames(); + } + + private int readTryCount() { + outputView.printTryCount(); + return inputView.readTryCount(); } } From caa95e8ba530d0830e357bc005ab4a1c0defb693 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=97=B0=EC=A7=84?= Date: Fri, 8 Sep 2023 15:59:16 +0900 Subject: [PATCH 10/34] =?UTF-8?q?feat(Cars):=20=EC=9E=85=EB=A0=A5=EB=B0=9B?= =?UTF-8?q?=EC=9D=80=20=EC=9D=B4=EB=A6=84=EC=9D=98=20=EB=A0=88=EC=9D=B4?= =?UTF-8?q?=EC=8B=B1=EC=B9=B4=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/controller/RacingCarController.java | 8 +++++--- src/main/java/racingcar/domain/Car.java | 4 ++-- src/main/java/racingcar/domain/Cars.java | 14 ++++++++++++++ src/main/java/racingcar/service/CarService.java | 15 +++++++++++++++ 4 files changed, 36 insertions(+), 5 deletions(-) create mode 100644 src/main/java/racingcar/domain/Cars.java create mode 100644 src/main/java/racingcar/service/CarService.java diff --git a/src/main/java/controller/RacingCarController.java b/src/main/java/controller/RacingCarController.java index 7bc8a8eda3..d6e5792da9 100644 --- a/src/main/java/controller/RacingCarController.java +++ b/src/main/java/controller/RacingCarController.java @@ -1,6 +1,7 @@ package controller; import racingcar.domain.Name; +import racingcar.service.CarService; import racingcar.view.InputView; import racingcar.view.OutputView; @@ -8,11 +9,12 @@ public class RacingCarController { - OutputView outputView = new OutputView(); - InputView inputView = new InputView(); + private final OutputView outputView = new OutputView(); + private final InputView inputView = new InputView(); + private final CarService carService = new CarService(); public void start() { - List names = readNames(); + carService.addCars(readNames()); int tryCount = readTryCount(); } diff --git a/src/main/java/racingcar/domain/Car.java b/src/main/java/racingcar/domain/Car.java index 7ab69bf089..3ca186c396 100644 --- a/src/main/java/racingcar/domain/Car.java +++ b/src/main/java/racingcar/domain/Car.java @@ -1,10 +1,10 @@ package racingcar.domain; public class Car { - private final String name; + private final Name name; private int position = 0; - public Car(String name) { + public Car(Name name) { this.name = name; } diff --git a/src/main/java/racingcar/domain/Cars.java b/src/main/java/racingcar/domain/Cars.java new file mode 100644 index 0000000000..af8e30bb47 --- /dev/null +++ b/src/main/java/racingcar/domain/Cars.java @@ -0,0 +1,14 @@ +package racingcar.domain; + +import java.util.ArrayList; +import java.util.List; + +public class Cars { + + private final List cars = new ArrayList<>(); + + public Cars(List names) { + names.forEach(name -> cars.add(new Car(name))); + System.out.println(cars); + } +} diff --git a/src/main/java/racingcar/service/CarService.java b/src/main/java/racingcar/service/CarService.java new file mode 100644 index 0000000000..ac0fae875e --- /dev/null +++ b/src/main/java/racingcar/service/CarService.java @@ -0,0 +1,15 @@ +package racingcar.service; + +import racingcar.domain.Cars; +import racingcar.domain.Name; + +import java.util.List; + +public class CarService { + + private Cars cars; + + public void addCars(List names) { + cars = new Cars(names); + } +} From ebc8aaf37a34d1064cbc96eee45654c79b76e256 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=97=B0=EC=A7=84?= Date: Fri, 8 Sep 2023 16:09:00 +0900 Subject: [PATCH 11/34] =?UTF-8?q?feat(Position):=20=EB=9E=9C=EB=8D=A4?= =?UTF-8?q?=EC=9C=BC=EB=A1=9C=20=EC=88=AB=EC=9E=90=20=EB=BD=91=EA=B8=B0=20?= =?UTF-8?q?=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 2 +- .../java/racingcar/constant/RandomNumber.java | 18 ++++++++++++++++++ src/main/java/racingcar/domain/Position.java | 19 +++++++++++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 src/main/java/racingcar/constant/RandomNumber.java create mode 100644 src/main/java/racingcar/domain/Position.java diff --git a/docs/README.md b/docs/README.md index 7431f70ee5..7515c56b91 100644 --- a/docs/README.md +++ b/docs/README.md @@ -8,7 +8,7 @@ - [x] 횟수 입력 - [ ] 숫자로 입력하지 않은 경우 예외 발생 - [ ] 입력받은 횟수만큼 자동차 이동 - - [ ] 랜덤으로 숫자를 뽑는다. + - [x] 랜덤으로 숫자를 뽑는다. - [ ] 뽑은 숫자가 4이상일 경우 이동한다. - [ ] 최종 결과 - [ ] 가장 멀리 나간 자동차가 무엇인지 구한다. diff --git a/src/main/java/racingcar/constant/RandomNumber.java b/src/main/java/racingcar/constant/RandomNumber.java new file mode 100644 index 0000000000..a417e7cd5e --- /dev/null +++ b/src/main/java/racingcar/constant/RandomNumber.java @@ -0,0 +1,18 @@ +package racingcar.constant; + +public enum RandomNumber { + + START_RANGE(0), + END_RANGE(9), + CAN_MOVE_NUMBER(4); + + private final int value; + + RandomNumber(int value) { + this.value = value; + } + + public int getValue() { + return value; + } +} diff --git a/src/main/java/racingcar/domain/Position.java b/src/main/java/racingcar/domain/Position.java new file mode 100644 index 0000000000..696525f559 --- /dev/null +++ b/src/main/java/racingcar/domain/Position.java @@ -0,0 +1,19 @@ +package racingcar.domain; + +import camp.nextstep.edu.missionutils.Randoms; +import racingcar.constant.RandomNumber; + +public class Position { + + private final int value = 0; + + public void move() { + + } + + private int pickRandomNumber() { + int start = RandomNumber.START_RANGE.getValue(); + int end = RandomNumber.END_RANGE.getValue(); + return Randoms.pickNumberInRange(start, end); + } +} From 68917bc44134e37568432e7dfe3dfc033c79fd21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=97=B0=EC=A7=84?= Date: Fri, 8 Sep 2023 16:20:15 +0900 Subject: [PATCH 12/34] =?UTF-8?q?feat(Position):=20=EB=BD=91=EC=9D=80=20?= =?UTF-8?q?=EC=88=AB=EC=9E=90=EA=B0=80=204=EC=9D=B4=EC=83=81=EC=9D=BC=20?= =?UTF-8?q?=EA=B2=BD=EC=9A=B0=20=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/domain/Car.java | 7 +++++-- src/main/java/racingcar/domain/Cars.java | 4 ++++ src/main/java/racingcar/domain/Position.java | 8 +++++++- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/main/java/racingcar/domain/Car.java b/src/main/java/racingcar/domain/Car.java index 3ca186c396..dc373421b8 100644 --- a/src/main/java/racingcar/domain/Car.java +++ b/src/main/java/racingcar/domain/Car.java @@ -2,11 +2,14 @@ public class Car { private final Name name; - private int position = 0; + private final Position position; public Car(Name name) { this.name = name; + this.position = new Position(); } - // 추가 기능 구현 + public void race() { + position.move(); + } } diff --git a/src/main/java/racingcar/domain/Cars.java b/src/main/java/racingcar/domain/Cars.java index af8e30bb47..0b6f611532 100644 --- a/src/main/java/racingcar/domain/Cars.java +++ b/src/main/java/racingcar/domain/Cars.java @@ -11,4 +11,8 @@ public Cars(List names) { names.forEach(name -> cars.add(new Car(name))); System.out.println(cars); } + + public void race() { + cars.forEach(Car::race); + } } diff --git a/src/main/java/racingcar/domain/Position.java b/src/main/java/racingcar/domain/Position.java index 696525f559..f413a5f5dd 100644 --- a/src/main/java/racingcar/domain/Position.java +++ b/src/main/java/racingcar/domain/Position.java @@ -5,10 +5,16 @@ public class Position { - private final int value = 0; + private int value = 0; public void move() { + if (canMove()) { + value += 1; + } + } + private boolean canMove() { + return pickRandomNumber() > RandomNumber.CAN_MOVE_NUMBER.getValue(); } private int pickRandomNumber() { From be6ce0eadcfcba4b238abbd5b9678a47b1f0ad23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=97=B0=EC=A7=84?= Date: Fri, 8 Sep 2023 16:20:49 +0900 Subject: [PATCH 13/34] =?UTF-8?q?feat(CarService):=20=EC=9E=85=EB=A0=A5?= =?UTF-8?q?=EB=B0=9B=EC=9D=80=20=EC=8B=9C=EB=8F=84=20=ED=9A=9F=EC=88=98?= =?UTF-8?q?=EB=A7=8C=ED=81=BC=20=EC=B0=A8=20=EC=9D=B4=EB=8F=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/controller/RacingCarController.java | 2 +- src/main/java/racingcar/service/CarService.java | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/main/java/controller/RacingCarController.java b/src/main/java/controller/RacingCarController.java index d6e5792da9..1812fad5f8 100644 --- a/src/main/java/controller/RacingCarController.java +++ b/src/main/java/controller/RacingCarController.java @@ -15,7 +15,7 @@ public class RacingCarController { public void start() { carService.addCars(readNames()); - int tryCount = readTryCount(); + carService.race(readTryCount()); } private List readNames() { diff --git a/src/main/java/racingcar/service/CarService.java b/src/main/java/racingcar/service/CarService.java index ac0fae875e..e1a67e0de3 100644 --- a/src/main/java/racingcar/service/CarService.java +++ b/src/main/java/racingcar/service/CarService.java @@ -12,4 +12,10 @@ public class CarService { public void addCars(List names) { cars = new Cars(names); } + + public void race(int tryCount) { + for (int i = 0; i < tryCount; i++) { + cars.race(); + } + } } From 7bbee81351f1085a528c7a30afbe720ae59a7ba4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=97=B0=EC=A7=84?= Date: Fri, 8 Sep 2023 16:21:15 +0900 Subject: [PATCH 14/34] =?UTF-8?q?docs:=20=EA=B5=AC=ED=98=84=20=EC=99=84?= =?UTF-8?q?=EB=A3=8C=20=EA=B8=B0=EB=8A=A5=20=EC=B2=B4=ED=81=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/README.md b/docs/README.md index 7515c56b91..0870a1bb55 100644 --- a/docs/README.md +++ b/docs/README.md @@ -7,9 +7,9 @@ - [x] 횟수 입력 안내 출력 - [x] 횟수 입력 - [ ] 숫자로 입력하지 않은 경우 예외 발생 -- [ ] 입력받은 횟수만큼 자동차 이동 +- [x] 입력받은 횟수만큼 자동차 이동 - [x] 랜덤으로 숫자를 뽑는다. - - [ ] 뽑은 숫자가 4이상일 경우 이동한다. + - [x] 뽑은 숫자가 4이상일 경우 이동한다. - [ ] 최종 결과 - [ ] 가장 멀리 나간 자동차가 무엇인지 구한다. - [ ] 최종 우승자 출력 From bededc2948450c1a30c9fc24b3e99ba256f8ee84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=97=B0=EC=A7=84?= Date: Fri, 8 Sep 2023 16:47:48 +0900 Subject: [PATCH 15/34] =?UTF-8?q?feat(Position):=20=EB=8B=AC=EB=A6=B0=20?= =?UTF-8?q?=EB=A7=8C=ED=81=BC=20`-`=EB=A5=BC=20=EC=B6=9C=EB=A0=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/racingcar/domain/Position.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/main/java/racingcar/domain/Position.java b/src/main/java/racingcar/domain/Position.java index f413a5f5dd..64020f7c83 100644 --- a/src/main/java/racingcar/domain/Position.java +++ b/src/main/java/racingcar/domain/Position.java @@ -5,6 +5,7 @@ public class Position { + private static final String RACING_LINE = "-"; private int value = 0; public void move() { @@ -22,4 +23,12 @@ private int pickRandomNumber() { int end = RandomNumber.END_RANGE.getValue(); return Randoms.pickNumberInRange(start, end); } + + public String getValue() { + StringBuilder stringBuilder = new StringBuilder(); + for (int i = 0; i < value; i++) { + stringBuilder.append(RACING_LINE); + } + return stringBuilder.toString(); + } } From 1bdf9c3d62da86a5b996ce1d4884a28d77582974 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=97=B0=EC=A7=84?= Date: Fri, 8 Sep 2023 16:48:54 +0900 Subject: [PATCH 16/34] =?UTF-8?q?feat(Car):=20`=EC=9D=B4=EB=A6=84:=20--`?= =?UTF-8?q?=20=ED=98=95=EC=8B=9D=EC=9C=BC=EB=A1=9C=20=EC=B6=9C=EB=A0=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 3 +++ src/main/java/racingcar/constant/OutputMessage.java | 3 ++- src/main/java/racingcar/domain/Car.java | 6 ++++++ src/main/java/racingcar/domain/Cars.java | 10 +++++++++- src/main/java/racingcar/domain/Name.java | 4 ++++ 5 files changed, 24 insertions(+), 2 deletions(-) diff --git a/docs/README.md b/docs/README.md index 0870a1bb55..6655f67122 100644 --- a/docs/README.md +++ b/docs/README.md @@ -10,6 +10,9 @@ - [x] 입력받은 횟수만큼 자동차 이동 - [x] 랜덤으로 숫자를 뽑는다. - [x] 뽑은 숫자가 4이상일 경우 이동한다. +- [x] 진행 내용 출력 + - [x] 달린 만큼 `-`를 출력한다 + - [x] `이름: --` 형식으로 출력한다. - [ ] 최종 결과 - [ ] 가장 멀리 나간 자동차가 무엇인지 구한다. - [ ] 최종 우승자 출력 diff --git a/src/main/java/racingcar/constant/OutputMessage.java b/src/main/java/racingcar/constant/OutputMessage.java index a7cfffd02a..6686a2ae0f 100644 --- a/src/main/java/racingcar/constant/OutputMessage.java +++ b/src/main/java/racingcar/constant/OutputMessage.java @@ -2,7 +2,8 @@ public enum OutputMessage { READ_NAME("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)"), - READ_TRY_COUNT("시도할 회수는 몇회인가요?"); + READ_TRY_COUNT("시도할 회수는 몇회인가요?"), + RACING_RESULT("%s : %s"); private final String message; diff --git a/src/main/java/racingcar/domain/Car.java b/src/main/java/racingcar/domain/Car.java index dc373421b8..4f21067305 100644 --- a/src/main/java/racingcar/domain/Car.java +++ b/src/main/java/racingcar/domain/Car.java @@ -1,5 +1,7 @@ package racingcar.domain; +import racingcar.constant.OutputMessage; + public class Car { private final Name name; private final Position position; @@ -12,4 +14,8 @@ public Car(Name name) { public void race() { position.move(); } + + public String getRacingResult() { + return String.format(OutputMessage.RACING_RESULT.toString(), name.getValue(), position.getValue()); + } } diff --git a/src/main/java/racingcar/domain/Cars.java b/src/main/java/racingcar/domain/Cars.java index 0b6f611532..d79d3f9bd4 100644 --- a/src/main/java/racingcar/domain/Cars.java +++ b/src/main/java/racingcar/domain/Cars.java @@ -9,10 +9,18 @@ public class Cars { public Cars(List names) { names.forEach(name -> cars.add(new Car(name))); - System.out.println(cars); } public void race() { cars.forEach(Car::race); } + + public String getRacingResult() { + StringBuilder stringBuilder = new StringBuilder(); + cars.forEach(car -> + stringBuilder.append(car.getRacingResult()) + .append("\n") + ); + return stringBuilder.toString(); + } } diff --git a/src/main/java/racingcar/domain/Name.java b/src/main/java/racingcar/domain/Name.java index 5744be0178..0b1f42b9e9 100644 --- a/src/main/java/racingcar/domain/Name.java +++ b/src/main/java/racingcar/domain/Name.java @@ -18,6 +18,10 @@ private void validateName(String value) { } } + public String getValue() { + return value; + } + @Override public String toString() { return value; From ab75b3ffe9365808d39b60ace2ead3b953f0eb2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=97=B0=EC=A7=84?= Date: Fri, 8 Sep 2023 17:00:05 +0900 Subject: [PATCH 17/34] =?UTF-8?q?feat(OutputView):=20=EC=B0=A8=EC=88=98?= =?UTF-8?q?=EB=B3=84=20=EC=8B=A4=ED=96=89=20=EA=B2=B0=EA=B3=BC=20=EC=B6=9C?= =?UTF-8?q?=EB=A0=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/controller/RacingCarController.java | 6 +++++- src/main/java/racingcar/constant/OutputMessage.java | 3 ++- src/main/java/racingcar/service/CarService.java | 7 +++---- src/main/java/racingcar/view/OutputView.java | 8 ++++++++ 4 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/main/java/controller/RacingCarController.java b/src/main/java/controller/RacingCarController.java index 1812fad5f8..f870ac7ad2 100644 --- a/src/main/java/controller/RacingCarController.java +++ b/src/main/java/controller/RacingCarController.java @@ -15,7 +15,11 @@ public class RacingCarController { public void start() { carService.addCars(readNames()); - carService.race(readTryCount()); + int tryCount = readTryCount(); + outputView.printResultMessage(); + for (int i = 0; i < tryCount; i++) { + outputView.printRacingResult(carService.race()); + } } private List readNames() { diff --git a/src/main/java/racingcar/constant/OutputMessage.java b/src/main/java/racingcar/constant/OutputMessage.java index 6686a2ae0f..610c64b15b 100644 --- a/src/main/java/racingcar/constant/OutputMessage.java +++ b/src/main/java/racingcar/constant/OutputMessage.java @@ -3,7 +3,8 @@ public enum OutputMessage { READ_NAME("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)"), READ_TRY_COUNT("시도할 회수는 몇회인가요?"), - RACING_RESULT("%s : %s"); + RACING_RESULT("%s : %s"), + RESULT_MESSAGE("실행 결과"); private final String message; diff --git a/src/main/java/racingcar/service/CarService.java b/src/main/java/racingcar/service/CarService.java index e1a67e0de3..1bddeb8ebe 100644 --- a/src/main/java/racingcar/service/CarService.java +++ b/src/main/java/racingcar/service/CarService.java @@ -13,9 +13,8 @@ public void addCars(List names) { cars = new Cars(names); } - public void race(int tryCount) { - for (int i = 0; i < tryCount; i++) { - cars.race(); - } + public String race() { + cars.race(); + return cars.getRacingResult(); } } diff --git a/src/main/java/racingcar/view/OutputView.java b/src/main/java/racingcar/view/OutputView.java index 892e479635..1915f7b642 100644 --- a/src/main/java/racingcar/view/OutputView.java +++ b/src/main/java/racingcar/view/OutputView.java @@ -11,4 +11,12 @@ public void printCarNames() { public void printTryCount() { System.out.println(OutputMessage.READ_TRY_COUNT); } + + public void printResultMessage() { + System.out.println(OutputMessage.RESULT_MESSAGE); + } + + public void printRacingResult(String result) { + System.out.println(result); + } } From afc00cf7fa51b48585905ab4b5b2ca43550e09ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=97=B0=EC=A7=84?= Date: Fri, 8 Sep 2023 17:36:41 +0900 Subject: [PATCH 18/34] =?UTF-8?q?feat(Cars):=20=EC=9A=B0=EC=8A=B9=EC=9E=90?= =?UTF-8?q?=20=EA=B3=84=EC=82=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/racingcar/domain/Car.java | 12 ++++++++++++ src/main/java/racingcar/domain/Cars.java | 19 +++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/src/main/java/racingcar/domain/Car.java b/src/main/java/racingcar/domain/Car.java index 4f21067305..0b3cf1799a 100644 --- a/src/main/java/racingcar/domain/Car.java +++ b/src/main/java/racingcar/domain/Car.java @@ -15,7 +15,19 @@ public void race() { position.move(); } + public boolean isWinner(int maxPosition) { + return position.isSame(maxPosition); + } + public String getRacingResult() { return String.format(OutputMessage.RACING_RESULT.toString(), name.getValue(), position.getValue()); } + + public Position getPosition() { + return position; + } + + public Name getName() { + return name; + } } diff --git a/src/main/java/racingcar/domain/Cars.java b/src/main/java/racingcar/domain/Cars.java index d79d3f9bd4..2975100ed7 100644 --- a/src/main/java/racingcar/domain/Cars.java +++ b/src/main/java/racingcar/domain/Cars.java @@ -1,7 +1,9 @@ package racingcar.domain; import java.util.ArrayList; +import java.util.Collections; import java.util.List; +import java.util.stream.Collectors; public class Cars { @@ -23,4 +25,21 @@ public String getRacingResult() { ); return stringBuilder.toString(); } + + public List getWinner() { + int maxPosition = calculateMaxPosition(); + return cars.stream() + .filter(car -> car.isWinner(maxPosition)) + .map(Car::getName) + .collect(Collectors.toList()); + } + + public int calculateMaxPosition() { + List positions = cars.stream() + .map(Car::getPosition) + .mapToInt(Position::getPosition) + .boxed() + .collect(Collectors.toList()); + return Collections.max(positions); + } } From 5722ce83507d0793b9c9d5cfa0134eb065647ec0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=97=B0=EC=A7=84?= Date: Fri, 8 Sep 2023 17:37:51 +0900 Subject: [PATCH 19/34] =?UTF-8?q?feat:=20=EC=9A=B0=EC=8A=B9=EC=9E=90=20?= =?UTF-8?q?=EC=B6=9C=EB=A0=A5=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 10 +++++----- src/main/java/racingcar/Application.java | 2 +- src/main/java/racingcar/constant/OutputMessage.java | 3 ++- .../controller/RacingCarController.java | 3 ++- src/main/java/racingcar/domain/Position.java | 8 ++++++++ src/main/java/racingcar/service/CarService.java | 4 ++++ src/main/java/racingcar/view/OutputView.java | 10 ++++++++++ 7 files changed, 32 insertions(+), 8 deletions(-) rename src/main/java/{ => racingcar}/controller/RacingCarController.java (91%) diff --git a/docs/README.md b/docs/README.md index 6655f67122..d52508e2a2 100644 --- a/docs/README.md +++ b/docs/README.md @@ -13,8 +13,8 @@ - [x] 진행 내용 출력 - [x] 달린 만큼 `-`를 출력한다 - [x] `이름: --` 형식으로 출력한다. -- [ ] 최종 결과 - - [ ] 가장 멀리 나간 자동차가 무엇인지 구한다. -- [ ] 최종 우승자 출력 - - [ ] 우승자가 한 명일 경우 그냥 출력 - - [ ] 우승자가 여러명일 경우 `,`로 나누어 출력 \ No newline at end of file +- [x] 최종 결과 + - [x] 가장 멀리 나간 자동차가 무엇인지 구한다. +- [x] 최종 우승자 출력 + - [x] 우승자가 한 명일 경우 그냥 출력 + - [x] 우승자가 여러명일 경우 `,`로 나누어 출력 \ No newline at end of file diff --git a/src/main/java/racingcar/Application.java b/src/main/java/racingcar/Application.java index 989a56c7c4..93aabb6b57 100644 --- a/src/main/java/racingcar/Application.java +++ b/src/main/java/racingcar/Application.java @@ -1,6 +1,6 @@ package racingcar; -import controller.RacingCarController; +import racingcar.controller.RacingCarController; public class Application { public static void main(String[] args) { diff --git a/src/main/java/racingcar/constant/OutputMessage.java b/src/main/java/racingcar/constant/OutputMessage.java index 610c64b15b..72bc42ed35 100644 --- a/src/main/java/racingcar/constant/OutputMessage.java +++ b/src/main/java/racingcar/constant/OutputMessage.java @@ -4,7 +4,8 @@ public enum OutputMessage { READ_NAME("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)"), READ_TRY_COUNT("시도할 회수는 몇회인가요?"), RACING_RESULT("%s : %s"), - RESULT_MESSAGE("실행 결과"); + RESULT_MESSAGE("실행 결과"), + WINNER("최종 우승자 : "); private final String message; diff --git a/src/main/java/controller/RacingCarController.java b/src/main/java/racingcar/controller/RacingCarController.java similarity index 91% rename from src/main/java/controller/RacingCarController.java rename to src/main/java/racingcar/controller/RacingCarController.java index f870ac7ad2..b1744fd860 100644 --- a/src/main/java/controller/RacingCarController.java +++ b/src/main/java/racingcar/controller/RacingCarController.java @@ -1,4 +1,4 @@ -package controller; +package racingcar.controller; import racingcar.domain.Name; import racingcar.service.CarService; @@ -20,6 +20,7 @@ public void start() { for (int i = 0; i < tryCount; i++) { outputView.printRacingResult(carService.race()); } + outputView.printWinner(carService.getWinner()); } private List readNames() { diff --git a/src/main/java/racingcar/domain/Position.java b/src/main/java/racingcar/domain/Position.java index 64020f7c83..7d2fa14f8b 100644 --- a/src/main/java/racingcar/domain/Position.java +++ b/src/main/java/racingcar/domain/Position.java @@ -24,6 +24,14 @@ private int pickRandomNumber() { return Randoms.pickNumberInRange(start, end); } + public boolean isSame(int maxPosition) { + return value == maxPosition; + } + + public int getPosition() { + return value; + } + public String getValue() { StringBuilder stringBuilder = new StringBuilder(); for (int i = 0; i < value; i++) { diff --git a/src/main/java/racingcar/service/CarService.java b/src/main/java/racingcar/service/CarService.java index 1bddeb8ebe..2a393c0b19 100644 --- a/src/main/java/racingcar/service/CarService.java +++ b/src/main/java/racingcar/service/CarService.java @@ -17,4 +17,8 @@ public String race() { cars.race(); return cars.getRacingResult(); } + + public List getWinner() { + return cars.getWinner(); + } } diff --git a/src/main/java/racingcar/view/OutputView.java b/src/main/java/racingcar/view/OutputView.java index 1915f7b642..c9db8de7bf 100644 --- a/src/main/java/racingcar/view/OutputView.java +++ b/src/main/java/racingcar/view/OutputView.java @@ -1,6 +1,10 @@ package racingcar.view; import racingcar.constant.OutputMessage; +import racingcar.domain.Name; + +import java.util.List; +import java.util.StringJoiner; public class OutputView { @@ -19,4 +23,10 @@ public void printResultMessage() { public void printRacingResult(String result) { System.out.println(result); } + + public void printWinner(List names) { + StringJoiner stringJoiner = new StringJoiner(", "); + names.forEach(name -> stringJoiner.add(name.getValue())); + System.out.println(OutputMessage.WINNER + stringJoiner.toString()); + } } From 61a169a454696e245a7100182ec06f0369ab8ebc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=97=B0=EC=A7=84?= Date: Fri, 8 Sep 2023 17:43:32 +0900 Subject: [PATCH 20/34] =?UTF-8?q?fix(Position):=20=EC=9D=B4=EB=8F=99=20?= =?UTF-8?q?=EA=B0=80=EB=8A=A5=20=ED=99=95=EC=9D=B8=20=EC=8B=9D=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/racingcar/domain/Position.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/racingcar/domain/Position.java b/src/main/java/racingcar/domain/Position.java index 7d2fa14f8b..57af03ee82 100644 --- a/src/main/java/racingcar/domain/Position.java +++ b/src/main/java/racingcar/domain/Position.java @@ -15,7 +15,7 @@ public void move() { } private boolean canMove() { - return pickRandomNumber() > RandomNumber.CAN_MOVE_NUMBER.getValue(); + return pickRandomNumber() >= RandomNumber.CAN_MOVE_NUMBER.getValue(); } private int pickRandomNumber() { From 068ebe5a2b6b2940cc6814323536187084cb70f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=97=B0=EC=A7=84?= Date: Fri, 8 Sep 2023 17:55:09 +0900 Subject: [PATCH 21/34] =?UTF-8?q?feat(InputValidator):=20`,`=EB=A1=9C=20?= =?UTF-8?q?=EC=9D=B4=EB=A6=84=EC=9D=84=20=EB=82=98=EB=88=84=EC=96=B4=20?= =?UTF-8?q?=EC=9E=85=EB=A0=A5=ED=95=98=EC=A7=80=20=EC=95=8A=EC=9D=80=20?= =?UTF-8?q?=EA=B2=BD=EC=9A=B0=20=EC=98=88=EC=99=B8=20=EB=B0=9C=EC=83=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 2 +- .../racingcar/constant/ExceptionMessage.java | 3 ++- .../racingcar/validator/InputValidator.java | 17 +++++++++++++++++ src/main/java/racingcar/view/InputView.java | 4 ++++ 4 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 src/main/java/racingcar/validator/InputValidator.java diff --git a/docs/README.md b/docs/README.md index d52508e2a2..30c5d3a1f3 100644 --- a/docs/README.md +++ b/docs/README.md @@ -2,7 +2,7 @@ - [x] 이름 입력 안내 출력 - [x] 이름 입력 - - [ ] `,`로 이름을 나누어 입력하지 않은 경우 예외 발생 + - [x] `,`로 이름을 나누어 입력하지 않은 경우 예외 발생 - [x] 이름이 5글자가 넘을 경우 예외 발생 - [x] 횟수 입력 안내 출력 - [x] 횟수 입력 diff --git a/src/main/java/racingcar/constant/ExceptionMessage.java b/src/main/java/racingcar/constant/ExceptionMessage.java index 4a97be1d96..f5fa85fd59 100644 --- a/src/main/java/racingcar/constant/ExceptionMessage.java +++ b/src/main/java/racingcar/constant/ExceptionMessage.java @@ -2,7 +2,8 @@ public enum ExceptionMessage { - INVALID_LENGTH("이름은 5글자 이하로 입력해야합니다."); + INVALID_LENGTH("이름은 5글자 이하로 입력해야합니다."), + INCORRECT_DELIMITER("','로 구분하여 입력해야합니다."); private static final String PREFIX = "[ERROR] "; private final String message; diff --git a/src/main/java/racingcar/validator/InputValidator.java b/src/main/java/racingcar/validator/InputValidator.java new file mode 100644 index 0000000000..b8f7fe57d2 --- /dev/null +++ b/src/main/java/racingcar/validator/InputValidator.java @@ -0,0 +1,17 @@ +package racingcar.validator; + +import racingcar.constant.ExceptionMessage; + +import java.util.regex.Pattern; + +public class InputValidator { + + private static final String DELIMITER_REGEXP = "^[a-zA-Zㄱ-힣0-9,]*$"; + + public void validateDelimiter(String input) { + if (!Pattern.matches(DELIMITER_REGEXP, input)) { + ExceptionMessage exceptionMessage = ExceptionMessage.INCORRECT_DELIMITER; + throw new IllegalArgumentException(exceptionMessage.toString()); + } + } +} diff --git a/src/main/java/racingcar/view/InputView.java b/src/main/java/racingcar/view/InputView.java index 4607ea1aa3..e6a19df3b5 100644 --- a/src/main/java/racingcar/view/InputView.java +++ b/src/main/java/racingcar/view/InputView.java @@ -2,6 +2,7 @@ import camp.nextstep.edu.missionutils.Console; import racingcar.domain.Name; +import racingcar.validator.InputValidator; import java.util.Arrays; import java.util.List; @@ -11,8 +12,11 @@ public class InputView { private static final String NAMES_REGEX = ","; + private final InputValidator inputValidator = new InputValidator(); + public List readNames() { String names = Console.readLine(); + inputValidator.validateDelimiter(names); return Arrays.stream(names.split(NAMES_REGEX)) .map(Name::new) .collect(Collectors.toList()); From c9a0cd1b72d7f55e536739f9182e82d936908200 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=97=B0=EC=A7=84?= Date: Fri, 8 Sep 2023 17:58:03 +0900 Subject: [PATCH 22/34] =?UTF-8?q?feat(InputValidator):=20=EC=88=AB?= =?UTF-8?q?=EC=9E=90=EB=A1=9C=20=EC=9E=85=EB=A0=A5=ED=95=98=EC=A7=80=20?= =?UTF-8?q?=EC=95=8A=EC=9D=80=20=EA=B2=BD=EC=9A=B0=20=EC=98=88=EC=99=B8=20?= =?UTF-8?q?=EB=B0=9C=EC=83=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 2 +- src/main/java/racingcar/constant/ExceptionMessage.java | 3 ++- src/main/java/racingcar/validator/InputValidator.java | 8 ++++++++ src/main/java/racingcar/view/InputView.java | 4 +++- 4 files changed, 14 insertions(+), 3 deletions(-) diff --git a/docs/README.md b/docs/README.md index 30c5d3a1f3..fc90f9c5f8 100644 --- a/docs/README.md +++ b/docs/README.md @@ -6,7 +6,7 @@ - [x] 이름이 5글자가 넘을 경우 예외 발생 - [x] 횟수 입력 안내 출력 - [x] 횟수 입력 - - [ ] 숫자로 입력하지 않은 경우 예외 발생 + - [x] 숫자로 입력하지 않은 경우 예외 발생 - [x] 입력받은 횟수만큼 자동차 이동 - [x] 랜덤으로 숫자를 뽑는다. - [x] 뽑은 숫자가 4이상일 경우 이동한다. diff --git a/src/main/java/racingcar/constant/ExceptionMessage.java b/src/main/java/racingcar/constant/ExceptionMessage.java index f5fa85fd59..cc0ab3cbe6 100644 --- a/src/main/java/racingcar/constant/ExceptionMessage.java +++ b/src/main/java/racingcar/constant/ExceptionMessage.java @@ -3,7 +3,8 @@ public enum ExceptionMessage { INVALID_LENGTH("이름은 5글자 이하로 입력해야합니다."), - INCORRECT_DELIMITER("','로 구분하여 입력해야합니다."); + INCORRECT_DELIMITER("','로 구분하여 입력해야합니다."), + NOT_INTEGER("숫자로 입력해야 합니다."); private static final String PREFIX = "[ERROR] "; private final String message; diff --git a/src/main/java/racingcar/validator/InputValidator.java b/src/main/java/racingcar/validator/InputValidator.java index b8f7fe57d2..e820573088 100644 --- a/src/main/java/racingcar/validator/InputValidator.java +++ b/src/main/java/racingcar/validator/InputValidator.java @@ -7,6 +7,7 @@ public class InputValidator { private static final String DELIMITER_REGEXP = "^[a-zA-Zㄱ-힣0-9,]*$"; + private static final String NUMBER_REGEXP = "^\\d*$"; public void validateDelimiter(String input) { if (!Pattern.matches(DELIMITER_REGEXP, input)) { @@ -14,4 +15,11 @@ public void validateDelimiter(String input) { throw new IllegalArgumentException(exceptionMessage.toString()); } } + + public void validateIsNumber(String input) { + if (!Pattern.matches(NUMBER_REGEXP, input)) { + ExceptionMessage exceptionMessage = ExceptionMessage.NOT_INTEGER; + throw new IllegalArgumentException(exceptionMessage.toString()); + } + } } diff --git a/src/main/java/racingcar/view/InputView.java b/src/main/java/racingcar/view/InputView.java index e6a19df3b5..661e56c5a9 100644 --- a/src/main/java/racingcar/view/InputView.java +++ b/src/main/java/racingcar/view/InputView.java @@ -23,6 +23,8 @@ public List readNames() { } public int readTryCount() { - return Integer.parseInt(Console.readLine()); + String input = Console.readLine(); + inputValidator.validateIsNumber(input); + return Integer.parseInt(input); } } From 762d67143dc82424e56df950c305491c7a26a2e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=97=B0=EC=A7=84?= Date: Fri, 8 Sep 2023 18:02:14 +0900 Subject: [PATCH 23/34] =?UTF-8?q?feat(InputView):=20=EC=98=A4=EB=A5=98=20?= =?UTF-8?q?=EB=B0=9C=EC=83=9D=20=EC=A7=80=EC=A0=90=EB=B6=80=ED=84=B0=20?= =?UTF-8?q?=EB=8B=A4=EC=8B=9C=20=EC=9E=85=EB=A0=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/racingcar/view/InputView.java | 30 +++++++++++++++------ 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/src/main/java/racingcar/view/InputView.java b/src/main/java/racingcar/view/InputView.java index 661e56c5a9..a882c3afa5 100644 --- a/src/main/java/racingcar/view/InputView.java +++ b/src/main/java/racingcar/view/InputView.java @@ -6,6 +6,7 @@ import java.util.Arrays; import java.util.List; +import java.util.function.Supplier; import java.util.stream.Collectors; public class InputView { @@ -15,16 +16,29 @@ public class InputView { private final InputValidator inputValidator = new InputValidator(); public List readNames() { - String names = Console.readLine(); - inputValidator.validateDelimiter(names); - return Arrays.stream(names.split(NAMES_REGEX)) - .map(Name::new) - .collect(Collectors.toList()); + return attemptedInput(() -> { + String names = Console.readLine(); + inputValidator.validateDelimiter(names); + return Arrays.stream(names.split(NAMES_REGEX)) + .map(Name::new) + .collect(Collectors.toList()); + }); } public int readTryCount() { - String input = Console.readLine(); - inputValidator.validateIsNumber(input); - return Integer.parseInt(input); + return attemptedInput(() -> { + String input = Console.readLine(); + inputValidator.validateIsNumber(input); + return Integer.parseInt(input); + }); + } + + private T attemptedInput(Supplier supplier) { + try { + return supplier.get(); + } catch (IllegalArgumentException exception) { + System.out.println(exception.getMessage()); + return supplier.get(); + } } } From d9c272411df8590667906b8b6e8db0b3fd611b84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=97=B0=EC=A7=84?= Date: Fri, 8 Sep 2023 18:14:36 +0900 Subject: [PATCH 24/34] =?UTF-8?q?feat:=20Names=20=EC=9D=BC=EA=B8=89?= =?UTF-8?q?=EC=BB=AC=EB=A0=89=EC=85=98=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/RacingCarController.java | 6 ++--- src/main/java/racingcar/domain/Cars.java | 10 +++++--- src/main/java/racingcar/domain/Names.java | 25 +++++++++++++++++++ .../java/racingcar/service/CarService.java | 8 +++--- src/main/java/racingcar/view/InputView.java | 10 +++++--- src/main/java/racingcar/view/OutputView.java | 11 +++----- 6 files changed, 45 insertions(+), 25 deletions(-) create mode 100644 src/main/java/racingcar/domain/Names.java diff --git a/src/main/java/racingcar/controller/RacingCarController.java b/src/main/java/racingcar/controller/RacingCarController.java index b1744fd860..9a5882763e 100644 --- a/src/main/java/racingcar/controller/RacingCarController.java +++ b/src/main/java/racingcar/controller/RacingCarController.java @@ -1,12 +1,10 @@ package racingcar.controller; -import racingcar.domain.Name; +import racingcar.domain.Names; import racingcar.service.CarService; import racingcar.view.InputView; import racingcar.view.OutputView; -import java.util.List; - public class RacingCarController { private final OutputView outputView = new OutputView(); @@ -23,7 +21,7 @@ public void start() { outputView.printWinner(carService.getWinner()); } - private List readNames() { + private Names readNames() { outputView.printCarNames(); return inputView.readNames(); } diff --git a/src/main/java/racingcar/domain/Cars.java b/src/main/java/racingcar/domain/Cars.java index 2975100ed7..76b7932fc2 100644 --- a/src/main/java/racingcar/domain/Cars.java +++ b/src/main/java/racingcar/domain/Cars.java @@ -9,8 +9,9 @@ public class Cars { private final List cars = new ArrayList<>(); - public Cars(List names) { - names.forEach(name -> cars.add(new Car(name))); + public Cars(Names names) { + names.getNames() + .forEach(name -> cars.add(new Car(name))); } public void race() { @@ -26,12 +27,13 @@ public String getRacingResult() { return stringBuilder.toString(); } - public List getWinner() { + public Names getWinner() { int maxPosition = calculateMaxPosition(); - return cars.stream() + List winner = cars.stream() .filter(car -> car.isWinner(maxPosition)) .map(Car::getName) .collect(Collectors.toList()); + return new Names(winner); } public int calculateMaxPosition() { diff --git a/src/main/java/racingcar/domain/Names.java b/src/main/java/racingcar/domain/Names.java new file mode 100644 index 0000000000..5b012ec749 --- /dev/null +++ b/src/main/java/racingcar/domain/Names.java @@ -0,0 +1,25 @@ +package racingcar.domain; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.StringJoiner; + +public class Names { + + private final List element; + + public Names(List names) { + this.element = new ArrayList<>(names); + } + + public String getElements() { + StringJoiner stringJoiner = new StringJoiner(", "); + element.forEach(name -> stringJoiner.add(name.getValue())); + return stringJoiner.toString(); + } + + public List getNames() { + return Collections.unmodifiableList(element); + } +} diff --git a/src/main/java/racingcar/service/CarService.java b/src/main/java/racingcar/service/CarService.java index 2a393c0b19..54a83ce079 100644 --- a/src/main/java/racingcar/service/CarService.java +++ b/src/main/java/racingcar/service/CarService.java @@ -1,15 +1,13 @@ package racingcar.service; import racingcar.domain.Cars; -import racingcar.domain.Name; - -import java.util.List; +import racingcar.domain.Names; public class CarService { private Cars cars; - public void addCars(List names) { + public void addCars(Names names) { cars = new Cars(names); } @@ -18,7 +16,7 @@ public String race() { return cars.getRacingResult(); } - public List getWinner() { + public Names getWinner() { return cars.getWinner(); } } diff --git a/src/main/java/racingcar/view/InputView.java b/src/main/java/racingcar/view/InputView.java index a882c3afa5..8d55aa98e6 100644 --- a/src/main/java/racingcar/view/InputView.java +++ b/src/main/java/racingcar/view/InputView.java @@ -2,6 +2,7 @@ import camp.nextstep.edu.missionutils.Console; import racingcar.domain.Name; +import racingcar.domain.Names; import racingcar.validator.InputValidator; import java.util.Arrays; @@ -15,13 +16,14 @@ public class InputView { private final InputValidator inputValidator = new InputValidator(); - public List readNames() { + public Names readNames() { return attemptedInput(() -> { - String names = Console.readLine(); - inputValidator.validateDelimiter(names); - return Arrays.stream(names.split(NAMES_REGEX)) + String input = Console.readLine(); + inputValidator.validateDelimiter(input); + List names = Arrays.stream(input.split(NAMES_REGEX)) .map(Name::new) .collect(Collectors.toList()); + return new Names(names); }); } diff --git a/src/main/java/racingcar/view/OutputView.java b/src/main/java/racingcar/view/OutputView.java index c9db8de7bf..ee307a4183 100644 --- a/src/main/java/racingcar/view/OutputView.java +++ b/src/main/java/racingcar/view/OutputView.java @@ -1,10 +1,7 @@ package racingcar.view; import racingcar.constant.OutputMessage; -import racingcar.domain.Name; - -import java.util.List; -import java.util.StringJoiner; +import racingcar.domain.Names; public class OutputView { @@ -24,9 +21,7 @@ public void printRacingResult(String result) { System.out.println(result); } - public void printWinner(List names) { - StringJoiner stringJoiner = new StringJoiner(", "); - names.forEach(name -> stringJoiner.add(name.getValue())); - System.out.println(OutputMessage.WINNER + stringJoiner.toString()); + public void printWinner(Names names) { + System.out.println(OutputMessage.WINNER + names.getElements()); } } From 6993dfb4f6651e66922bc5abdbb8b914d08a6e64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=97=B0=EC=A7=84?= Date: Fri, 8 Sep 2023 18:26:18 +0900 Subject: [PATCH 25/34] =?UTF-8?q?test(Name):=20=EC=9D=B4=EB=A6=84=20?= =?UTF-8?q?=EC=83=9D=EC=84=B1=20=ED=85=8C=EC=8A=A4=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/racingcar/domain/NameTest.java | 21 ++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 src/test/java/racingcar/domain/NameTest.java diff --git a/src/test/java/racingcar/domain/NameTest.java b/src/test/java/racingcar/domain/NameTest.java new file mode 100644 index 0000000000..f81b6c66cd --- /dev/null +++ b/src/test/java/racingcar/domain/NameTest.java @@ -0,0 +1,21 @@ +package racingcar.domain; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +public class NameTest { + + private static final String ERROR_MESSAGE = "[ERROR]"; + + @DisplayName("이름의 길이가 5가 넘어갈 경우 예외가 발생한다.") + @Test + void nameLengthException() { + String name = "김연진김연진"; + + assertThatThrownBy(() -> new Name(name)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining(ERROR_MESSAGE); + } +} From 8e2f5ac51c2aea8608f30ec6c3979ab88d8b262c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=97=B0=EC=A7=84?= Date: Fri, 8 Sep 2023 18:31:28 +0900 Subject: [PATCH 26/34] =?UTF-8?q?test(InputValidator):=20=EC=9E=85?= =?UTF-8?q?=EB=A0=A5=EA=B0=92=20=EA=B2=80=EC=A6=9D=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../racingcar/domain/InputValidatorTest.java | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 src/test/java/racingcar/domain/InputValidatorTest.java diff --git a/src/test/java/racingcar/domain/InputValidatorTest.java b/src/test/java/racingcar/domain/InputValidatorTest.java new file mode 100644 index 0000000000..6512ba2257 --- /dev/null +++ b/src/test/java/racingcar/domain/InputValidatorTest.java @@ -0,0 +1,44 @@ +package racingcar.domain; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import racingcar.validator.InputValidator; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +public class InputValidatorTest { + + private static final String ERROR_MESSAGE = "[ERROR]"; + + private final InputValidator inputValidator = new InputValidator(); + + @DisplayName("콤마이외의 특수문자가 있을 경우 예외 발생") + @Test + void specialCharactersException() { + String input = "po|pi"; + + assertThatThrownBy(() -> inputValidator.validateDelimiter(input)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining(ERROR_MESSAGE); + } + + @DisplayName("공백이 존재할 경우 예외 발생") + @Test + void spaceException() { + String input = "po, pi"; + + assertThatThrownBy(() -> inputValidator.validateDelimiter(input)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining(ERROR_MESSAGE); + } + + @DisplayName("숫자가 아닐 경우 예외 발생") + @Test + void nameLengthException() { + String input = "o"; + + assertThatThrownBy(() -> inputValidator.validateIsNumber(input)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining(ERROR_MESSAGE); + } +} From 2800d0ce5cb82df150b9a8b51259538ab0e848c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=97=B0=EC=A7=84?= Date: Fri, 8 Sep 2023 18:32:37 +0900 Subject: [PATCH 27/34] =?UTF-8?q?refactor(Name):=20=EB=AF=B8=EC=82=AC?= =?UTF-8?q?=EC=9A=A9=20=EB=A9=94=EC=84=9C=EB=93=9C=20=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/racingcar/domain/Name.java | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/main/java/racingcar/domain/Name.java b/src/main/java/racingcar/domain/Name.java index 0b1f42b9e9..31d1fa4966 100644 --- a/src/main/java/racingcar/domain/Name.java +++ b/src/main/java/racingcar/domain/Name.java @@ -21,9 +21,4 @@ private void validateName(String value) { public String getValue() { return value; } - - @Override - public String toString() { - return value; - } } From 0c8df1dc83146a5cb4a057714198f3623c23d922 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=97=B0=EC=A7=84?= Date: Fri, 8 Sep 2023 18:40:28 +0900 Subject: [PATCH 28/34] =?UTF-8?q?refactor:=20=ED=95=98=EB=93=9C=EC=BD=94?= =?UTF-8?q?=EB=94=A9=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/racingcar/constant/PrintElement.java | 17 +++++++++++++++++ src/main/java/racingcar/domain/Cars.java | 4 +++- src/main/java/racingcar/domain/Names.java | 4 +++- 3 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 src/main/java/racingcar/constant/PrintElement.java diff --git a/src/main/java/racingcar/constant/PrintElement.java b/src/main/java/racingcar/constant/PrintElement.java new file mode 100644 index 0000000000..ded851ee24 --- /dev/null +++ b/src/main/java/racingcar/constant/PrintElement.java @@ -0,0 +1,17 @@ +package racingcar.constant; + +public enum PrintElement { + + NEW_LINE("\n"), + SEPERATOR(", "); + + private final String element; + + PrintElement(String element) { + this.element = element; + } + + public String getElement() { + return element; + } +} diff --git a/src/main/java/racingcar/domain/Cars.java b/src/main/java/racingcar/domain/Cars.java index 76b7932fc2..ca88117ff9 100644 --- a/src/main/java/racingcar/domain/Cars.java +++ b/src/main/java/racingcar/domain/Cars.java @@ -1,5 +1,7 @@ package racingcar.domain; +import racingcar.constant.PrintElement; + import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -22,7 +24,7 @@ public String getRacingResult() { StringBuilder stringBuilder = new StringBuilder(); cars.forEach(car -> stringBuilder.append(car.getRacingResult()) - .append("\n") + .append(PrintElement.NEW_LINE) ); return stringBuilder.toString(); } diff --git a/src/main/java/racingcar/domain/Names.java b/src/main/java/racingcar/domain/Names.java index 5b012ec749..339cd3d023 100644 --- a/src/main/java/racingcar/domain/Names.java +++ b/src/main/java/racingcar/domain/Names.java @@ -1,5 +1,7 @@ package racingcar.domain; +import racingcar.constant.PrintElement; + import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -14,7 +16,7 @@ public Names(List names) { } public String getElements() { - StringJoiner stringJoiner = new StringJoiner(", "); + StringJoiner stringJoiner = new StringJoiner(PrintElement.SEPERATOR.getElement()); element.forEach(name -> stringJoiner.add(name.getValue())); return stringJoiner.toString(); } From ca594296bfb56f689abc00d1b01774b48c1bfa9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=97=B0=EC=A7=84?= Date: Fri, 8 Sep 2023 18:45:15 +0900 Subject: [PATCH 29/34] =?UTF-8?q?refactor:=20=EC=A4=84=20=EB=B0=94?= =?UTF-8?q?=EA=BF=88=20=EC=B6=9C=EB=A0=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/racingcar/constant/PrintElement.java | 2 +- .../java/racingcar/controller/RacingCarController.java | 8 ++++++-- src/main/java/racingcar/domain/Cars.java | 2 +- src/main/java/racingcar/domain/Names.java | 2 +- src/main/java/racingcar/view/OutputView.java | 4 ++++ 5 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/main/java/racingcar/constant/PrintElement.java b/src/main/java/racingcar/constant/PrintElement.java index ded851ee24..6e584e7758 100644 --- a/src/main/java/racingcar/constant/PrintElement.java +++ b/src/main/java/racingcar/constant/PrintElement.java @@ -3,7 +3,7 @@ public enum PrintElement { NEW_LINE("\n"), - SEPERATOR(", "); + SEPARATOR(", "); private final String element; diff --git a/src/main/java/racingcar/controller/RacingCarController.java b/src/main/java/racingcar/controller/RacingCarController.java index 9a5882763e..bee5bb0ade 100644 --- a/src/main/java/racingcar/controller/RacingCarController.java +++ b/src/main/java/racingcar/controller/RacingCarController.java @@ -23,11 +23,15 @@ public void start() { private Names readNames() { outputView.printCarNames(); - return inputView.readNames(); + Names names = inputView.readNames(); + outputView.printNewLine(); + return names; } private int readTryCount() { outputView.printTryCount(); - return inputView.readTryCount(); + int tryCount = inputView.readTryCount(); + outputView.printNewLine(); + return tryCount; } } diff --git a/src/main/java/racingcar/domain/Cars.java b/src/main/java/racingcar/domain/Cars.java index ca88117ff9..7cd409d432 100644 --- a/src/main/java/racingcar/domain/Cars.java +++ b/src/main/java/racingcar/domain/Cars.java @@ -24,7 +24,7 @@ public String getRacingResult() { StringBuilder stringBuilder = new StringBuilder(); cars.forEach(car -> stringBuilder.append(car.getRacingResult()) - .append(PrintElement.NEW_LINE) + .append(PrintElement.NEW_LINE.getElement()) ); return stringBuilder.toString(); } diff --git a/src/main/java/racingcar/domain/Names.java b/src/main/java/racingcar/domain/Names.java index 339cd3d023..7d909601e5 100644 --- a/src/main/java/racingcar/domain/Names.java +++ b/src/main/java/racingcar/domain/Names.java @@ -16,7 +16,7 @@ public Names(List names) { } public String getElements() { - StringJoiner stringJoiner = new StringJoiner(PrintElement.SEPERATOR.getElement()); + StringJoiner stringJoiner = new StringJoiner(PrintElement.SEPARATOR.getElement()); element.forEach(name -> stringJoiner.add(name.getValue())); return stringJoiner.toString(); } diff --git a/src/main/java/racingcar/view/OutputView.java b/src/main/java/racingcar/view/OutputView.java index ee307a4183..a202e7f01c 100644 --- a/src/main/java/racingcar/view/OutputView.java +++ b/src/main/java/racingcar/view/OutputView.java @@ -24,4 +24,8 @@ public void printRacingResult(String result) { public void printWinner(Names names) { System.out.println(OutputMessage.WINNER + names.getElements()); } + + public void printNewLine() { + System.out.println(); + } } From 80eef93bea62b03e57d17f53b2d553d840c28722 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=97=B0=EC=A7=84?= Date: Fri, 8 Sep 2023 18:50:28 +0900 Subject: [PATCH 30/34] =?UTF-8?q?refactor(RacingCarController):=20?= =?UTF-8?q?=EB=A9=94=EC=84=9C=EB=93=9C=20=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/racingcar/Application.java | 2 +- .../controller/RacingCarController.java | 16 ++++++++++------ 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/main/java/racingcar/Application.java b/src/main/java/racingcar/Application.java index 93aabb6b57..667ba1f98d 100644 --- a/src/main/java/racingcar/Application.java +++ b/src/main/java/racingcar/Application.java @@ -5,6 +5,6 @@ public class Application { public static void main(String[] args) { RacingCarController racingCarController = new RacingCarController(); - racingCarController.start(); + racingCarController.run(); } } diff --git a/src/main/java/racingcar/controller/RacingCarController.java b/src/main/java/racingcar/controller/RacingCarController.java index bee5bb0ade..96c88cd698 100644 --- a/src/main/java/racingcar/controller/RacingCarController.java +++ b/src/main/java/racingcar/controller/RacingCarController.java @@ -11,14 +11,10 @@ public class RacingCarController { private final InputView inputView = new InputView(); private final CarService carService = new CarService(); - public void start() { + public void run() { carService.addCars(readNames()); int tryCount = readTryCount(); - outputView.printResultMessage(); - for (int i = 0; i < tryCount; i++) { - outputView.printRacingResult(carService.race()); - } - outputView.printWinner(carService.getWinner()); + startRacing(tryCount); } private Names readNames() { @@ -34,4 +30,12 @@ private int readTryCount() { outputView.printNewLine(); return tryCount; } + + private void startRacing(int tryCount) { + outputView.printResultMessage(); + for (int i = 0; i < tryCount; i++) { + outputView.printRacingResult(carService.race()); + } + outputView.printWinner(carService.getWinner()); + } } From dc9ee2e1bf5fd3b0eab1fe9833e7b18f8a3d0472 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=97=B0=EC=A7=84?= Date: Fri, 8 Sep 2023 19:02:55 +0900 Subject: [PATCH 31/34] =?UTF-8?q?feat(TryCount):=200=EC=9D=B4=ED=95=98?= =?UTF-8?q?=EC=9D=98=20=EC=88=AB=EC=9E=90=EB=A5=BC=20=EC=9E=85=EB=A0=A5?= =?UTF-8?q?=ED=95=A0=20=EA=B2=BD=EC=9A=B0=20=EC=98=88=EC=99=B8=20=EB=B0=9C?= =?UTF-8?q?=EC=83=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 1 + .../racingcar/constant/ExceptionMessage.java | 3 ++- .../controller/RacingCarController.java | 11 ++++---- src/main/java/racingcar/domain/TryCount.java | 25 +++++++++++++++++++ src/main/java/racingcar/view/InputView.java | 5 ++-- 5 files changed, 37 insertions(+), 8 deletions(-) create mode 100644 src/main/java/racingcar/domain/TryCount.java diff --git a/docs/README.md b/docs/README.md index fc90f9c5f8..6cf87569bc 100644 --- a/docs/README.md +++ b/docs/README.md @@ -7,6 +7,7 @@ - [x] 횟수 입력 안내 출력 - [x] 횟수 입력 - [x] 숫자로 입력하지 않은 경우 예외 발생 + - [x] 0이하의 숫자를 입력할 경우 예외 발생 - [x] 입력받은 횟수만큼 자동차 이동 - [x] 랜덤으로 숫자를 뽑는다. - [x] 뽑은 숫자가 4이상일 경우 이동한다. diff --git a/src/main/java/racingcar/constant/ExceptionMessage.java b/src/main/java/racingcar/constant/ExceptionMessage.java index cc0ab3cbe6..261ad70f0e 100644 --- a/src/main/java/racingcar/constant/ExceptionMessage.java +++ b/src/main/java/racingcar/constant/ExceptionMessage.java @@ -4,7 +4,8 @@ public enum ExceptionMessage { INVALID_LENGTH("이름은 5글자 이하로 입력해야합니다."), INCORRECT_DELIMITER("','로 구분하여 입력해야합니다."), - NOT_INTEGER("숫자로 입력해야 합니다."); + NOT_INTEGER("숫자로 입력해야 합니다."), + INVALID_TRY_COUNT_RANGE("0이상의 숫자를 입력해야 합니다."); private static final String PREFIX = "[ERROR] "; private final String message; diff --git a/src/main/java/racingcar/controller/RacingCarController.java b/src/main/java/racingcar/controller/RacingCarController.java index 96c88cd698..fc569a99a0 100644 --- a/src/main/java/racingcar/controller/RacingCarController.java +++ b/src/main/java/racingcar/controller/RacingCarController.java @@ -1,6 +1,7 @@ package racingcar.controller; import racingcar.domain.Names; +import racingcar.domain.TryCount; import racingcar.service.CarService; import racingcar.view.InputView; import racingcar.view.OutputView; @@ -13,7 +14,7 @@ public class RacingCarController { public void run() { carService.addCars(readNames()); - int tryCount = readTryCount(); + TryCount tryCount = readTryCount(); startRacing(tryCount); } @@ -24,16 +25,16 @@ private Names readNames() { return names; } - private int readTryCount() { + private TryCount readTryCount() { outputView.printTryCount(); - int tryCount = inputView.readTryCount(); + TryCount tryCount = inputView.readTryCount(); outputView.printNewLine(); return tryCount; } - private void startRacing(int tryCount) { + private void startRacing(TryCount tryCount) { outputView.printResultMessage(); - for (int i = 0; i < tryCount; i++) { + for (int i = 0; i < tryCount.getValue(); i++) { outputView.printRacingResult(carService.race()); } outputView.printWinner(carService.getWinner()); diff --git a/src/main/java/racingcar/domain/TryCount.java b/src/main/java/racingcar/domain/TryCount.java new file mode 100644 index 0000000000..1be714fecf --- /dev/null +++ b/src/main/java/racingcar/domain/TryCount.java @@ -0,0 +1,25 @@ +package racingcar.domain; + +import racingcar.constant.ExceptionMessage; + +public class TryCount { + + private static final int MIN_VALUE= 1; + + private final int value; + + public TryCount(int value) { + validateRange(value); + this.value = value; + } + + public void validateRange(int value) { + if (value < MIN_VALUE) { + throw new IllegalArgumentException(ExceptionMessage.INVALID_TRY_COUNT_RANGE.toString()); + } + } + + public int getValue() { + return value; + } +} diff --git a/src/main/java/racingcar/view/InputView.java b/src/main/java/racingcar/view/InputView.java index 8d55aa98e6..30f376a662 100644 --- a/src/main/java/racingcar/view/InputView.java +++ b/src/main/java/racingcar/view/InputView.java @@ -3,6 +3,7 @@ import camp.nextstep.edu.missionutils.Console; import racingcar.domain.Name; import racingcar.domain.Names; +import racingcar.domain.TryCount; import racingcar.validator.InputValidator; import java.util.Arrays; @@ -27,11 +28,11 @@ public Names readNames() { }); } - public int readTryCount() { + public TryCount readTryCount() { return attemptedInput(() -> { String input = Console.readLine(); inputValidator.validateIsNumber(input); - return Integer.parseInt(input); + return new TryCount(Integer.parseInt(input)); }); } From 0de42f15ce7b7bcf1e7d1fea4bee4f3e355d7541 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=97=B0=EC=A7=84?= Date: Fri, 8 Sep 2023 19:06:14 +0900 Subject: [PATCH 32/34] =?UTF-8?q?test(TryCount):=20=EC=8B=9C=EB=8F=84=20?= =?UTF-8?q?=ED=9A=9F=EC=88=98=20=EC=9E=85=EB=A0=A5=20=EC=8B=9C=20=EC=98=88?= =?UTF-8?q?=EC=99=B8=20=EB=B0=9C=EC=83=9D=20=ED=85=8C=EC=8A=A4=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/racingcar/domain/TryCountTest.java | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 src/test/java/racingcar/domain/TryCountTest.java diff --git a/src/test/java/racingcar/domain/TryCountTest.java b/src/test/java/racingcar/domain/TryCountTest.java new file mode 100644 index 0000000000..49ffeb0859 --- /dev/null +++ b/src/test/java/racingcar/domain/TryCountTest.java @@ -0,0 +1,21 @@ +package racingcar.domain; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +public class TryCountTest { + + private static final String ERROR_MESSAGE = "[ERROR]"; + + @DisplayName("시도 횟수에 0이하의 숫자를 입력할 경우 예외 발생") + @Test + void tryCountException() { + int count = 0; + + assertThatThrownBy(() -> new TryCount(count)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining(ERROR_MESSAGE); + } +} From 1197ac8bcca494c305d0402a90a11ea5a7c8210f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=97=B0=EC=A7=84?= Date: Fri, 8 Sep 2023 21:30:09 +0900 Subject: [PATCH 33/34] =?UTF-8?q?refactor(Cars):=20=EC=A0=91=EA=B7=BC=20?= =?UTF-8?q?=EC=A0=9C=EC=96=B4=EC=9E=90=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/domain/Cars.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/racingcar/domain/Cars.java b/src/main/java/racingcar/domain/Cars.java index 7cd409d432..7e8ecf91ee 100644 --- a/src/main/java/racingcar/domain/Cars.java +++ b/src/main/java/racingcar/domain/Cars.java @@ -38,7 +38,7 @@ public Names getWinner() { return new Names(winner); } - public int calculateMaxPosition() { + private int calculateMaxPosition() { List positions = cars.stream() .map(Car::getPosition) .mapToInt(Position::getPosition) From 1f070deb7aac87c2562ef43a208b014439c6896c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=97=B0=EC=A7=84?= Date: Tue, 19 Sep 2023 15:02:10 +0900 Subject: [PATCH 34/34] =?UTF-8?q?refactor(CarService):=20=EC=84=9C?= =?UTF-8?q?=EB=B9=84=EC=8A=A4=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/racingcar/controller/RacingCarController.java | 4 ++-- src/main/java/racingcar/service/CarService.java | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/racingcar/controller/RacingCarController.java b/src/main/java/racingcar/controller/RacingCarController.java index fc569a99a0..8abe87797c 100644 --- a/src/main/java/racingcar/controller/RacingCarController.java +++ b/src/main/java/racingcar/controller/RacingCarController.java @@ -10,10 +10,10 @@ public class RacingCarController { private final OutputView outputView = new OutputView(); private final InputView inputView = new InputView(); - private final CarService carService = new CarService(); + private CarService carService; public void run() { - carService.addCars(readNames()); + carService = new CarService(readNames()); TryCount tryCount = readTryCount(); startRacing(tryCount); } diff --git a/src/main/java/racingcar/service/CarService.java b/src/main/java/racingcar/service/CarService.java index 54a83ce079..aea164b3ed 100644 --- a/src/main/java/racingcar/service/CarService.java +++ b/src/main/java/racingcar/service/CarService.java @@ -5,10 +5,10 @@ public class CarService { - private Cars cars; + private final Cars cars; - public void addCars(Names names) { - cars = new Cars(names); + public CarService(Names names) { + this.cars = new Cars(names); } public String race() {