From 67acfb92a1a6cea2c6513f48260d5e4aaf2080ee Mon Sep 17 00:00:00 2001 From: SeoJiWon Date: Sun, 25 Jun 2023 16:32:45 +0900 Subject: [PATCH 01/26] =?UTF-8?q?docs:=20=EA=B8=B0=EB=B3=B8=20=EA=B8=B0?= =?UTF-8?q?=EB=8A=A5=20=EB=B0=8F=20=EB=AA=A8=EB=8D=B8=EB=A7=81=EC=97=90=20?= =?UTF-8?q?=EB=8C=80=ED=95=9C=20README.md=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 | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) diff --git a/docs/README.md b/docs/README.md index e69de29..98b4429 100644 --- a/docs/README.md +++ b/docs/README.md @@ -0,0 +1,89 @@ +## Feature +- [ ] 컴퓨터는 1..9 범위에서 서로 다른 임의의 수 3개 선택 +- [ ] 유저는 1..9 범위에서 서로 다른 숫자 3개를 입력한다 + - [ ] 숫자가 아닌 값을 입력한 경우 `IllegalArgumentException` 발생 +- [ ] 심판은 `컴퓨터 <-> 유저의 3가지 숫자`를 토대로 숫자 야구 게임 결과를 도출한다 + - 비교 후 `strikeCount & ballCount` 정보를 `Result`로 Wrapping한 후 결과를 발송한다 +- [ ] 컴퓨터가 선택한 모든 숫자를 맞히면 게임은 클리어된다 + - [ ] 사용자는 재시작[1], 종료[2] 중 하나를 입력한다 + - [ ] 숫자가 아닌 값을 입력한 경우 `IllegalArgumentException` 발생 + +
+
+ +## Model +### `Baseballs` +- 입력한 3개의 숫자들을 추상화시킨 `Baseballs` + - Baseballs에 속한 `List`은 + - [ ] 각 원소가 `1..9` 범위 사이여야 한다 + - [ ] 원소의 크기가 3이여야 한다 + - [ ] 중복된 원소가 없어야 한다 + +
+ +### `Computer: 컴퓨터` +- 1..9 범위에서 서로 다른 임의의 수 3개 선택 + - Computer는 중복 숫자를 선택하지 않도록 구현 +- User 선택을 통한 결과 산출의 기준 + +
+ +### `User: 사용자` +- 숫자 3개 입력 + - 숫자를 입력하지 않을 경우 (IllegalArgumentException) +- 게임 재시작/종료와 관련된 커맨드 입력 + - 숫자를 입력하지 않을 경우 (IllegalArgumentException) + +
+ +### `Referee: 심판` +- 플레이어 점수 채점 + - 숫자 겹침 + 동일 위치 = 스트라이크 + - 숫자 겹침 + 다른 위치 = 볼 + - 숫자 안겹침 = 낫싱 + +
+ +### `Result: 채점 결과` +- 스트라이크 & 볼에 대한 count field 보유 + +
+ +### `GameStatus: 게임 상태` +- 진행중인 게임에 대한 상태 + - `GAME_RUN` -> 게임 실행 + - `GAME_TERMINATE` -> 게임 종료 + +
+ +### `GameProcessDecider: 재시작 or 종료 결정자` +- 게임 클리어 후 종료 or 재시작에 대한 커맨더 + +
+
+ +## Utils +### `ExceptionConstants` +- 전역 예외 메시지 통합 컴포넌트 + +### `BaseballConstants` +- Baseball에 대한 상수 전용 컴포넌트 + - 숫자 범위 + - Baseball 개수 + +
+
+ +## View +### `InputView` +- 사용자 Input을 받기 위한 컴포넌트 + +### `OutputView` +- 게임 진행과 관련된 출력 컴포넌트 + +
+
+ +## Controller +### `GameController` +- 게임 진행과 관련된 컨트롤러 From d1eef0e34a319d5c237258b5fd08da5aa44a5be0 Mon Sep 17 00:00:00 2001 From: SeoJiWon Date: Sun, 25 Jun 2023 16:39:23 +0900 Subject: [PATCH 02/26] =?UTF-8?q?feat:=20Baseballs=20=EA=B4=80=EB=A0=A8=20?= =?UTF-8?q?=EC=83=81=EC=88=98=20=EC=9D=B8=ED=84=B0=ED=8E=98=EC=9D=B4?= =?UTF-8?q?=EC=8A=A4=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/baseball/utils/BaseballConstants.java | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 src/main/java/baseball/utils/BaseballConstants.java diff --git a/src/main/java/baseball/utils/BaseballConstants.java b/src/main/java/baseball/utils/BaseballConstants.java new file mode 100644 index 0000000..be546a8 --- /dev/null +++ b/src/main/java/baseball/utils/BaseballConstants.java @@ -0,0 +1,7 @@ +package baseball.utils; + +public interface BaseballConstants { + int MIN_BASEBALL = 1; + int MAX_BASEBALL = 9; + int BASEBALL_SIZE = 3; +} From 7077cceda77c44a0fb9db16816bdff3409822afb Mon Sep 17 00:00:00 2001 From: SeoJiWon Date: Sun, 25 Jun 2023 16:39:51 +0900 Subject: [PATCH 03/26] =?UTF-8?q?feat:=20Baseballs=20=EB=AA=A8=EB=8D=B8=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84=20=EB=B0=8F=20=EC=98=88=EC=99=B8=20=EC=BC=80?= =?UTF-8?q?=EC=9D=B4=EC=8A=A4=20=EA=B2=80=EC=A6=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 6 +-- src/main/java/baseball/model/Baseballs.java | 51 +++++++++++++++++++ .../baseball/utils/ExceptionConstants.java | 16 ++++++ 3 files changed, 70 insertions(+), 3 deletions(-) create mode 100644 src/main/java/baseball/model/Baseballs.java create mode 100644 src/main/java/baseball/utils/ExceptionConstants.java diff --git a/docs/README.md b/docs/README.md index 98b4429..0d3b7b7 100644 --- a/docs/README.md +++ b/docs/README.md @@ -15,9 +15,9 @@ ### `Baseballs` - 입력한 3개의 숫자들을 추상화시킨 `Baseballs` - Baseballs에 속한 `List`은 - - [ ] 각 원소가 `1..9` 범위 사이여야 한다 - - [ ] 원소의 크기가 3이여야 한다 - - [ ] 중복된 원소가 없어야 한다 + - [X] 각 원소가 `1..9` 범위 사이여야 한다 + - [X] 원소의 크기가 3이여야 한다 + - [X] 중복된 원소가 없어야 한다
diff --git a/src/main/java/baseball/model/Baseballs.java b/src/main/java/baseball/model/Baseballs.java new file mode 100644 index 0000000..c0df58e --- /dev/null +++ b/src/main/java/baseball/model/Baseballs.java @@ -0,0 +1,51 @@ +package baseball.model; + +import java.util.Collections; +import java.util.List; + +import static baseball.utils.BaseballConstants.*; +import static baseball.utils.ExceptionConstants.BaseballException.*; + +public class Baseballs { + private final List baseballs; + + public Baseballs(final List baseballs) { + validateEachBaseballElementIsInRange(baseballs); + validateTotalBaseballSize(baseballs); + validateBaseballHasDuplicateNumber(baseballs); + this.baseballs = baseballs; + } + + private void validateEachBaseballElementIsInRange(final List baseballs) { + if (hasOutOfRange(baseballs)) { + throw new IllegalArgumentException(BASEBALL_IS_NOT_IN_RANGE.message); + } + } + + private boolean hasOutOfRange(final List baseballs) { + return baseballs.stream() + .anyMatch(baseball -> baseball < MIN_BASEBALL || baseball > MAX_BASEBALL); + } + + private void validateTotalBaseballSize(final List baseballs) { + if (baseballs.size() != BASEBALL_SIZE) { + throw new IllegalArgumentException(BASEBALL_SIZE_IS_NOT_FULFILL.message); + } + } + + private void validateBaseballHasDuplicateNumber(final List baseballs) { + if (hasDuplicateNumber(baseballs)) { + throw new IllegalArgumentException(BASEBALL_MUST_BE_UNIQUE.message); + } + } + + private boolean hasDuplicateNumber(final List baseballs) { + return baseballs.stream() + .distinct() + .count() != BASEBALL_SIZE; + } + + public List getBaseballs() { + return Collections.unmodifiableList(baseballs); + } +} diff --git a/src/main/java/baseball/utils/ExceptionConstants.java b/src/main/java/baseball/utils/ExceptionConstants.java new file mode 100644 index 0000000..d656df2 --- /dev/null +++ b/src/main/java/baseball/utils/ExceptionConstants.java @@ -0,0 +1,16 @@ +package baseball.utils; + +public class ExceptionConstants { + public enum BaseballException { + BASEBALL_IS_NOT_IN_RANGE("숫자는 1..9 범위만 허용합니다."), + BASEBALL_SIZE_IS_NOT_FULFILL("숫자 3개를 입력해주세요."), + BASEBALL_MUST_BE_UNIQUE("중복된 숫자는 허용하지 않습니다."), + ; + + public final String message; + + BaseballException(final String message) { + this.message = message; + } + } +} From bd0fecc39f5532f1caaf79651adb1fc3f6df9c9b Mon Sep 17 00:00:00 2001 From: SeoJiWon Date: Sun, 25 Jun 2023 16:40:06 +0900 Subject: [PATCH 04/26] =?UTF-8?q?test:=20Baseballs=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EC=BC=80=EC=9D=B4=EC=8A=A4=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/baseball/model/BaseballsTest.java | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 src/test/java/baseball/model/BaseballsTest.java diff --git a/src/test/java/baseball/model/BaseballsTest.java b/src/test/java/baseball/model/BaseballsTest.java new file mode 100644 index 0000000..8282ed1 --- /dev/null +++ b/src/test/java/baseball/model/BaseballsTest.java @@ -0,0 +1,71 @@ +package baseball.model; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +import java.util.List; +import java.util.stream.Stream; + +import static baseball.utils.ExceptionConstants.BaseballException.*; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; + +class BaseballsTest { + @ParameterizedTest + @MethodSource("invalidRange") + @DisplayName("1..9 범위가 아닌 숫자가 존재하면 Baseballs를 생성할 수 없다") + void throwExceptionByBaseballIsNotInRange(List baseballs) { + assertThatThrownBy(() -> new Baseballs(baseballs)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage(BASEBALL_IS_NOT_IN_RANGE.message); + } + + private static Stream invalidRange() { + return Stream.of( + Arguments.of(List.of(0, 1, 9)), + Arguments.of(List.of(1, 9, 10)) + ); + } + + @ParameterizedTest + @MethodSource("invalidSize") + @DisplayName("숫자가 3개 미만이면 Baseballs를 생성할 수 없다") + void throwExceptionByBaseballSizeNotFulfill(List baseballs) { + assertThatThrownBy(() -> new Baseballs(baseballs)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage(BASEBALL_SIZE_IS_NOT_FULFILL.message); + } + + private static Stream invalidSize() { + return Stream.of( + Arguments.of(List.of()), + Arguments.of(List.of(1)), + Arguments.of(List.of(1, 2)) + ); + } + + @ParameterizedTest + @MethodSource("duplicateNumber") + @DisplayName("중복된 숫자가 존재하면 Baseballs를 생성할 수 없다") + void throwExceptionByBaseballIsNotUnique(List baseballs) { + assertThatThrownBy(() -> new Baseballs(baseballs)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage(BASEBALL_MUST_BE_UNIQUE.message); + } + + private static Stream duplicateNumber() { + return Stream.of( + Arguments.of(List.of(1, 2, 2)), + Arguments.of(List.of(3, 3, 3)) + ); + } + + @Test + @DisplayName("Baseballs를 생성한다") + void construct() { + assertDoesNotThrow(() -> new Baseballs(List.of(1, 2, 3))); + } +} From 2da9bf0fcb271e1a19750403e29e15c4fb232876 Mon Sep 17 00:00:00 2001 From: SeoJiWon Date: Sun, 25 Jun 2023 16:41:48 +0900 Subject: [PATCH 05/26] =?UTF-8?q?feat:=20Computer=20=EB=AA=A8=EB=8D=B8=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84=20=EB=B0=8F=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20?= =?UTF-8?q?=EC=BC=80=EC=9D=B4=EC=8A=A4=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 | 27 ++++++++++++--- src/main/java/baseball/model/Computer.java | 33 +++++++++++++++++++ .../java/baseball/model/ComputerTest.java | 18 ++++++++++ 3 files changed, 73 insertions(+), 5 deletions(-) create mode 100644 src/main/java/baseball/model/Computer.java create mode 100644 src/test/java/baseball/model/ComputerTest.java diff --git a/docs/README.md b/docs/README.md index 0d3b7b7..c6ec172 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,18 +1,21 @@ ## Feature -- [ ] 컴퓨터는 1..9 범위에서 서로 다른 임의의 수 3개 선택 + +- [X] 컴퓨터는 1..9 범위에서 서로 다른 임의의 수 3개 선택 - [ ] 유저는 1..9 범위에서 서로 다른 숫자 3개를 입력한다 - - [ ] 숫자가 아닌 값을 입력한 경우 `IllegalArgumentException` 발생 + - [ ] 숫자가 아닌 값을 입력한 경우 `IllegalArgumentException` 발생 - [ ] 심판은 `컴퓨터 <-> 유저의 3가지 숫자`를 토대로 숫자 야구 게임 결과를 도출한다 - - 비교 후 `strikeCount & ballCount` 정보를 `Result`로 Wrapping한 후 결과를 발송한다 + - 비교 후 `strikeCount & ballCount` 정보를 `Result`로 Wrapping한 후 결과를 발송한다 - [ ] 컴퓨터가 선택한 모든 숫자를 맞히면 게임은 클리어된다 - - [ ] 사용자는 재시작[1], 종료[2] 중 하나를 입력한다 - - [ ] 숫자가 아닌 값을 입력한 경우 `IllegalArgumentException` 발생 + - [ ] 사용자는 재시작[1], 종료[2] 중 하나를 입력한다 + - [ ] 숫자가 아닌 값을 입력한 경우 `IllegalArgumentException` 발생

## Model + ### `Baseballs` + - 입력한 3개의 숫자들을 추상화시킨 `Baseballs` - Baseballs에 속한 `List`은 - [X] 각 원소가 `1..9` 범위 사이여야 한다 @@ -22,6 +25,7 @@
### `Computer: 컴퓨터` + - 1..9 범위에서 서로 다른 임의의 수 3개 선택 - Computer는 중복 숫자를 선택하지 않도록 구현 - User 선택을 통한 결과 산출의 기준 @@ -29,6 +33,7 @@
### `User: 사용자` + - 숫자 3개 입력 - 숫자를 입력하지 않을 경우 (IllegalArgumentException) - 게임 재시작/종료와 관련된 커맨드 입력 @@ -37,6 +42,7 @@
### `Referee: 심판` + - 플레이어 점수 채점 - 숫자 겹침 + 동일 위치 = 스트라이크 - 숫자 겹침 + 다른 위치 = 볼 @@ -45,11 +51,13 @@
### `Result: 채점 결과` + - 스트라이크 & 볼에 대한 count field 보유
### `GameStatus: 게임 상태` + - 진행중인 게임에 대한 상태 - `GAME_RUN` -> 게임 실행 - `GAME_TERMINATE` -> 게임 종료 @@ -57,16 +65,20 @@
### `GameProcessDecider: 재시작 or 종료 결정자` + - 게임 클리어 후 종료 or 재시작에 대한 커맨더

## Utils + ### `ExceptionConstants` + - 전역 예외 메시지 통합 컴포넌트 ### `BaseballConstants` + - Baseball에 대한 상수 전용 컴포넌트 - 숫자 범위 - Baseball 개수 @@ -75,15 +87,20 @@
## View + ### `InputView` + - 사용자 Input을 받기 위한 컴포넌트 ### `OutputView` + - 게임 진행과 관련된 출력 컴포넌트

## Controller + ### `GameController` + - 게임 진행과 관련된 컨트롤러 diff --git a/src/main/java/baseball/model/Computer.java b/src/main/java/baseball/model/Computer.java new file mode 100644 index 0000000..65841c0 --- /dev/null +++ b/src/main/java/baseball/model/Computer.java @@ -0,0 +1,33 @@ +package baseball.model; + +import camp.nextstep.edu.missionutils.Randoms; + +import java.util.ArrayList; +import java.util.List; + +import static baseball.utils.BaseballConstants.*; + +public class Computer { + private final Baseballs baseballs; + + public Computer() { + this.baseballs = generateRandomBaseballs(); + } + + private Baseballs generateRandomBaseballs() { + List baseballs = new ArrayList<>(); + + while (baseballs.size() != BASEBALL_SIZE) { + int randomNumber = Randoms.pickNumberInRange(MIN_BASEBALL, MAX_BASEBALL); + if (!baseballs.contains(randomNumber)) { + baseballs.add(randomNumber); + } + } + + return new Baseballs(baseballs); + } + + public List getBaseballs() { + return baseballs.getBaseballs(); + } +} diff --git a/src/test/java/baseball/model/ComputerTest.java b/src/test/java/baseball/model/ComputerTest.java new file mode 100644 index 0000000..44aef57 --- /dev/null +++ b/src/test/java/baseball/model/ComputerTest.java @@ -0,0 +1,18 @@ +package baseball.model; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +class ComputerTest { + @Test + @DisplayName("Baseballs 랜덤 제공을 통해서 Computer를 생성한다") + void construct() { + // when + final Computer computer = new Computer(); + + // then + assertThat(computer.getBaseballs()).hasSize(3); + } +} From 312f766638549499925812de816de7a8bf1d1365 Mon Sep 17 00:00:00 2001 From: SeoJiWon Date: Sun, 25 Jun 2023 16:46:12 +0900 Subject: [PATCH 06/26] =?UTF-8?q?feat:=20User=20=EB=AA=A8=EB=8D=B8=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84=20=EB=B0=8F=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20?= =?UTF-8?q?=EC=BC=80=EC=9D=B4=EC=8A=A4=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/baseball/model/User.java | 15 ++++++ src/test/java/baseball/model/UserTest.java | 61 ++++++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 src/main/java/baseball/model/User.java create mode 100644 src/test/java/baseball/model/UserTest.java diff --git a/src/main/java/baseball/model/User.java b/src/main/java/baseball/model/User.java new file mode 100644 index 0000000..b6d8379 --- /dev/null +++ b/src/main/java/baseball/model/User.java @@ -0,0 +1,15 @@ +package baseball.model; + +import java.util.List; + +public class User { + private final Baseballs baseballs; + + public User(final List baseballs) { + this.baseballs = new Baseballs(baseballs); + } + + public List getBaseballs() { + return baseballs.getBaseballs(); + } +} diff --git a/src/test/java/baseball/model/UserTest.java b/src/test/java/baseball/model/UserTest.java new file mode 100644 index 0000000..7559bc2 --- /dev/null +++ b/src/test/java/baseball/model/UserTest.java @@ -0,0 +1,61 @@ +package baseball.model; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static baseball.utils.ExceptionConstants.BaseballException.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +class UserTest { + @Test + @DisplayName("입력한 원소중에 1..9 범위가 아닌 원소가 있으면 User는 Baseballs를 가질 수 없다") + void throwExceptionByBaseballIsNotInRange() { + // given + final List baseballs = List.of(1, 9, 10); + + // when - then + assertThatThrownBy(() -> new Baseballs(baseballs)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage(BASEBALL_IS_NOT_IN_RANGE.message); + } + + @Test + @DisplayName("입력한 원소의 크기가 3이 아니면 User는 Baseballs를 가질 수 없다") + void throwExceptionByBaseballSizeNotFulfill() { + // given + final List baseballs = List.of(1, 2); + + // when - then + assertThatThrownBy(() -> new Baseballs(baseballs)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage(BASEBALL_SIZE_IS_NOT_FULFILL.message); + } + + @Test + @DisplayName("입력한 원소중에 중복된 원소가 존재한다면 User는 Baseballs를 가질 수 없다") + void throwExceptionByBaseballIsNotUnique() { + // given + final List baseballs = List.of(1, 2, 2); + + // when - then + assertThatThrownBy(() -> new Baseballs(baseballs)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage(BASEBALL_MUST_BE_UNIQUE.message); + } + + @Test + @DisplayName("User를 생성한다") + void construct() { + // given + final List baseballs = List.of(1, 2, 3); + + // when + final User user = new User(baseballs); + + // then + assertThat(user.getBaseballs()).containsExactlyElementsOf(baseballs); + } +} From 55bee8ecae90f6fa72c9baa34aff00095dfba43e Mon Sep 17 00:00:00 2001 From: SeoJiWon Date: Sun, 25 Jun 2023 16:47:11 +0900 Subject: [PATCH 07/26] =?UTF-8?q?feat:=20=EA=B2=8C=EC=9E=84=20=EA=B2=B0?= =?UTF-8?q?=EA=B3=BC=20=EB=AA=85=EC=84=B8=EB=A5=BC=20=EC=9C=84=ED=95=9C=20?= =?UTF-8?q?Result=20=EB=AA=A8=EB=8D=B8=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/baseball/model/Result.java | 25 ++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 src/main/java/baseball/model/Result.java diff --git a/src/main/java/baseball/model/Result.java b/src/main/java/baseball/model/Result.java new file mode 100644 index 0000000..f0ba3ff --- /dev/null +++ b/src/main/java/baseball/model/Result.java @@ -0,0 +1,25 @@ +package baseball.model; + +import static baseball.utils.BaseballConstants.BASEBALL_SIZE; + +public class Result { + private final int strikeCount; + private final int ballCount; + + public Result(final int strikeCount, final int ballCount) { + this.strikeCount = strikeCount; + this.ballCount = ballCount; + } + + public int getStrikeCount() { + return strikeCount; + } + + public int getBallCount() { + return ballCount; + } + + public boolean isGameClear() { + return strikeCount == BASEBALL_SIZE; + } +} From 6ffb501549b3de9bdea9d8eb50213896c298cbfb Mon Sep 17 00:00:00 2001 From: SeoJiWon Date: Sun, 25 Jun 2023 16:50:46 +0900 Subject: [PATCH 08/26] =?UTF-8?q?feat:=20=EA=B2=8C=EC=9E=84=20=EA=B2=B0?= =?UTF-8?q?=EA=B3=BC=20=EB=8F=84=EC=B6=9C=EC=9D=84=20=EC=9C=84=ED=95=9C=20?= =?UTF-8?q?Referee=20=EB=AA=A8=EB=8D=B8=20=EA=B5=AC=ED=98=84=20=EB=B0=8F?= =?UTF-8?q?=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BC=80=EC=9D=B4=EC=8A=A4=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 --- docs/README.md | 2 +- src/main/java/baseball/model/Referee.java | 47 ++++++++ src/test/java/baseball/model/RefereeTest.java | 112 ++++++++++++++++++ 3 files changed, 160 insertions(+), 1 deletion(-) create mode 100644 src/main/java/baseball/model/Referee.java create mode 100644 src/test/java/baseball/model/RefereeTest.java diff --git a/docs/README.md b/docs/README.md index c6ec172..4c91bcc 100644 --- a/docs/README.md +++ b/docs/README.md @@ -3,7 +3,7 @@ - [X] 컴퓨터는 1..9 범위에서 서로 다른 임의의 수 3개 선택 - [ ] 유저는 1..9 범위에서 서로 다른 숫자 3개를 입력한다 - [ ] 숫자가 아닌 값을 입력한 경우 `IllegalArgumentException` 발생 -- [ ] 심판은 `컴퓨터 <-> 유저의 3가지 숫자`를 토대로 숫자 야구 게임 결과를 도출한다 +- [X] 심판은 `컴퓨터 <-> 유저의 3가지 숫자`를 토대로 숫자 야구 게임 결과를 도출한다 - 비교 후 `strikeCount & ballCount` 정보를 `Result`로 Wrapping한 후 결과를 발송한다 - [ ] 컴퓨터가 선택한 모든 숫자를 맞히면 게임은 클리어된다 - [ ] 사용자는 재시작[1], 종료[2] 중 하나를 입력한다 diff --git a/src/main/java/baseball/model/Referee.java b/src/main/java/baseball/model/Referee.java new file mode 100644 index 0000000..f20ff45 --- /dev/null +++ b/src/main/java/baseball/model/Referee.java @@ -0,0 +1,47 @@ +package baseball.model; + +import java.util.List; +import java.util.stream.IntStream; + +import static baseball.utils.BaseballConstants.BASEBALL_SIZE; + +public class Referee { + private Referee() { + } + + public static Result judge( + final List computerBaseballs, + final List userBaseballs + ) { + final int strikeCount = calculateStrikeCount(computerBaseballs, userBaseballs); + final int ballCount = calculateBallCount(computerBaseballs, userBaseballs); + + return new Result(strikeCount, ballCount); + } + + private static int calculateStrikeCount( + final List computerBaseballs, + final List userBaseballs + ) { + return (int) IntStream.range(0, BASEBALL_SIZE) + .filter(index -> computerBaseballs.get(index).equals(userBaseballs.get(index))) + .count(); + } + + private static int calculateBallCount( + final List computerBaseballs, + final List userBaseballs + ) { + return getContainedElementsCount(computerBaseballs, userBaseballs) + - calculateStrikeCount(computerBaseballs, userBaseballs); + } + + private static int getContainedElementsCount( + final List computerBaseballs, + final List userBaseballs + ) { + return (int) userBaseballs.stream() + .filter(computerBaseballs::contains) + .count(); + } +} diff --git a/src/test/java/baseball/model/RefereeTest.java b/src/test/java/baseball/model/RefereeTest.java new file mode 100644 index 0000000..49ac51c --- /dev/null +++ b/src/test/java/baseball/model/RefereeTest.java @@ -0,0 +1,112 @@ +package baseball.model; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +import java.util.List; +import java.util.stream.Stream; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertAll; + +class RefereeTest { + @ParameterizedTest + @MethodSource("dummyBaseballs") + @DisplayName("Referee가 Computer & User 간 숫자 야구 게임 결과(Result)를 도출한다") + void judge( + List computerBaseballs, + List userBaseballs, + int strikeCount, + int ballCount + ) { + // when + Result result = Referee.judge(computerBaseballs, userBaseballs); + + // then + assertAll( + () -> assertThat(result.getStrikeCount()).isEqualTo(strikeCount), + () -> assertThat(result.getBallCount()).isEqualTo(ballCount) + ); + } + + private static Stream dummyBaseballs() { + return Stream.of( + Arguments.of( + List.of(1, 2, 3), + List.of(4, 5, 6), + 0, + 0 + ), + Arguments.of( + List.of(1, 2, 3), + List.of(4, 5, 1), + 0, + 1 + ), + Arguments.of( + List.of(1, 2, 3), + List.of(2, 1, 5), + 0, + 2 + ), + Arguments.of( + List.of(1, 2, 3), + List.of(3, 1, 2), + 0, + 3 + ), + Arguments.of( + List.of(1, 2, 3), + List.of(1, 4, 5), + 1, + 0 + ), + Arguments.of( + List.of(1, 2, 3), + List.of(1, 2, 5), + 2, + 0 + ), + Arguments.of( + List.of(1, 2, 3), + List.of(1, 2, 3), + 3, + 0 + ), + Arguments.of( + List.of(1, 2, 3), + List.of(1, 3, 5), + 1, + 1 + ), + Arguments.of( + List.of(1, 2, 3), + List.of(1, 3, 2), + 1, + 2 + ) + ); + } + + @Test + @DisplayName("Game Clear(All Strike) 여부를 확인한다") + void isGameClear() { + // given + final List computerBaseballs = List.of(1, 2, 3); + final List userBaseballs1 = List.of(1, 2, 3); + final List userBaseballs2 = List.of(1, 2, 4); + + // when + Result result1 = Referee.judge(computerBaseballs, userBaseballs1); + Result result2 = Referee.judge(computerBaseballs, userBaseballs2); + + // then + assertAll( + () -> assertThat(result1.isGameClear()).isTrue(), + () -> assertThat(result2.isGameClear()).isFalse() + ); + } +} From 1dde87d8ee880ccabfc82f3d3261af0ac43f6508 Mon Sep 17 00:00:00 2001 From: SeoJiWon Date: Sun, 25 Jun 2023 16:54:22 +0900 Subject: [PATCH 09/26] =?UTF-8?q?feat:=20=EA=B2=8C=EC=9E=84=20=EC=A7=84?= =?UTF-8?q?=ED=96=89=20=EC=83=81=ED=83=9C=20Tracking=EC=9D=84=20=EC=9C=84?= =?UTF-8?q?=ED=95=9C=20GameStatus=20=EA=B5=AC=ED=98=84=20=EB=B0=8F=20?= =?UTF-8?q?=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BC=80=EC=9D=B4=EC=8A=A4=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 --- src/main/java/baseball/model/GameStatus.java | 11 ++++++++++ .../java/baseball/model/GameStatusTest.java | 20 +++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 src/main/java/baseball/model/GameStatus.java create mode 100644 src/test/java/baseball/model/GameStatusTest.java diff --git a/src/main/java/baseball/model/GameStatus.java b/src/main/java/baseball/model/GameStatus.java new file mode 100644 index 0000000..056b21d --- /dev/null +++ b/src/main/java/baseball/model/GameStatus.java @@ -0,0 +1,11 @@ +package baseball.model; + +public enum GameStatus { + GAME_RUNNING, + GAME_TERMINATE, + ; + + public boolean isGameNotTerminated() { + return this != GAME_TERMINATE; + } +} diff --git a/src/test/java/baseball/model/GameStatusTest.java b/src/test/java/baseball/model/GameStatusTest.java new file mode 100644 index 0000000..7bc593e --- /dev/null +++ b/src/test/java/baseball/model/GameStatusTest.java @@ -0,0 +1,20 @@ +package baseball.model; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import static baseball.model.GameStatus.GAME_RUNNING; +import static baseball.model.GameStatus.GAME_TERMINATE; +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertAll; + +class GameStatusTest { + @Test + @DisplayName("GameStatus가 TERMINATED가 아닌지 확인한다") + void isGameNotTerminated() { + assertAll( + () -> assertThat(GAME_RUNNING.isGameNotTerminated()).isTrue(), + () -> assertThat(GAME_TERMINATE.isGameNotTerminated()).isFalse() + ); + } +} From 7a3481e1503fcba8bdafd51fade86f447d71f26d Mon Sep 17 00:00:00 2001 From: SeoJiWon Date: Sun, 25 Jun 2023 17:08:33 +0900 Subject: [PATCH 10/26] =?UTF-8?q?feat:=20=EA=B2=8C=EC=9E=84=20=EC=9E=AC?= =?UTF-8?q?=EC=8B=9C=EC=9E=91=20=EA=B4=80=EB=A0=A8=20GameProcessDecider=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84=20=EB=B0=8F=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20?= =?UTF-8?q?=EC=BC=80=EC=9D=B4=EC=8A=A4=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../baseball/model/GameProcessDecider.java | 24 +++++++++++ .../baseball/utils/ExceptionConstants.java | 10 +++++ .../model/GameProcessDeciderTest.java | 40 +++++++++++++++++++ 3 files changed, 74 insertions(+) create mode 100644 src/main/java/baseball/model/GameProcessDecider.java create mode 100644 src/test/java/baseball/model/GameProcessDeciderTest.java diff --git a/src/main/java/baseball/model/GameProcessDecider.java b/src/main/java/baseball/model/GameProcessDecider.java new file mode 100644 index 0000000..fba54b4 --- /dev/null +++ b/src/main/java/baseball/model/GameProcessDecider.java @@ -0,0 +1,24 @@ +package baseball.model; + +import java.util.Arrays; + +import static baseball.utils.ExceptionConstants.GameProcessCommandException.INVALID_COMMAND; + +public enum GameProcessDecider { + GAME_RESTART(1), + GAME_END(2), + ; + + private final int command; + + GameProcessDecider(final int command) { + this.command = command; + } + + public static GameProcessDecider getDecider(final int userCommand) { + return Arrays.stream(values()) + .filter(restartDecider -> restartDecider.command == userCommand) + .findFirst() + .orElseThrow(() -> new IllegalArgumentException(INVALID_COMMAND.message)); + } +} diff --git a/src/main/java/baseball/utils/ExceptionConstants.java b/src/main/java/baseball/utils/ExceptionConstants.java index d656df2..35d2cb3 100644 --- a/src/main/java/baseball/utils/ExceptionConstants.java +++ b/src/main/java/baseball/utils/ExceptionConstants.java @@ -13,4 +13,14 @@ public enum BaseballException { this.message = message; } } + + public enum GameProcessCommandException { + INVALID_COMMAND("재시작[1] / 종료[2] 중 하나를 입력해주세요."); + + public final String message; + + GameProcessCommandException(final String message) { + this.message = message; + } + } } diff --git a/src/test/java/baseball/model/GameProcessDeciderTest.java b/src/test/java/baseball/model/GameProcessDeciderTest.java new file mode 100644 index 0000000..1335be9 --- /dev/null +++ b/src/test/java/baseball/model/GameProcessDeciderTest.java @@ -0,0 +1,40 @@ +package baseball.model; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import static baseball.model.GameProcessDecider.GAME_END; +import static baseball.model.GameProcessDecider.GAME_RESTART; +import static baseball.utils.ExceptionConstants.GameProcessCommandException.INVALID_COMMAND; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +class GameProcessDeciderTest { + @Test + @DisplayName("재시작[1], 종료[2] 이외의 Command를 입력하면 예외가 발생한다") + void throwExceptionByInvalidCommand() { + assertThatThrownBy(() -> GameProcessDecider.getDecider(3)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage(INVALID_COMMAND.message); + } + + @Test + @DisplayName("게임 재시작[1] Decider를 선택한다") + void selectGameRestart() { + // when + GameProcessDecider decider = GameProcessDecider.getDecider(1); + + // then + assertThat(decider).isEqualTo(GAME_RESTART); + } + + @Test + @DisplayName("게임 종료[2] Decider를 선택한다") + void selectGameEnd() { + // when + GameProcessDecider decider = GameProcessDecider.getDecider(2); + + // then + assertThat(decider).isEqualTo(GAME_END); + } +} From ed74d6ef620d431f32d6abe5f03bbe003dc2e2a6 Mon Sep 17 00:00:00 2001 From: SeoJiWon Date: Sun, 25 Jun 2023 17:14:39 +0900 Subject: [PATCH 11/26] =?UTF-8?q?feat:=20=EC=82=AC=EC=9A=A9=EC=9E=90=20Inp?= =?UTF-8?q?ut=EC=9D=84=20=EC=9C=84=ED=95=9C=20InputView=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 --- docs/README.md | 10 ++--- .../baseball/utils/ExceptionConstants.java | 10 +++++ src/main/java/baseball/view/InputView.java | 41 +++++++++++++++++++ 3 files changed, 56 insertions(+), 5 deletions(-) create mode 100644 src/main/java/baseball/view/InputView.java diff --git a/docs/README.md b/docs/README.md index 4c91bcc..9fe98b4 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,13 +1,13 @@ ## Feature - [X] 컴퓨터는 1..9 범위에서 서로 다른 임의의 수 3개 선택 -- [ ] 유저는 1..9 범위에서 서로 다른 숫자 3개를 입력한다 - - [ ] 숫자가 아닌 값을 입력한 경우 `IllegalArgumentException` 발생 +- [X] 유저는 1..9 범위에서 서로 다른 숫자 3개를 입력한다 + - [X] 숫자가 아닌 값을 입력한 경우 `IllegalArgumentException` 발생 - [X] 심판은 `컴퓨터 <-> 유저의 3가지 숫자`를 토대로 숫자 야구 게임 결과를 도출한다 - 비교 후 `strikeCount & ballCount` 정보를 `Result`로 Wrapping한 후 결과를 발송한다 -- [ ] 컴퓨터가 선택한 모든 숫자를 맞히면 게임은 클리어된다 - - [ ] 사용자는 재시작[1], 종료[2] 중 하나를 입력한다 - - [ ] 숫자가 아닌 값을 입력한 경우 `IllegalArgumentException` 발생 +- [X] 컴퓨터가 선택한 모든 숫자를 맞히면 게임은 클리어된다 + - [X] 사용자는 재시작[1], 종료[2] 중 하나를 입력한다 + - [X] 숫자가 아닌 값을 입력한 경우 `IllegalArgumentException` 발생

diff --git a/src/main/java/baseball/utils/ExceptionConstants.java b/src/main/java/baseball/utils/ExceptionConstants.java index 35d2cb3..eca370c 100644 --- a/src/main/java/baseball/utils/ExceptionConstants.java +++ b/src/main/java/baseball/utils/ExceptionConstants.java @@ -23,4 +23,14 @@ public enum GameProcessCommandException { this.message = message; } } + + public enum InputException { + INPUT_MUST_BE_NUMERIC("숫자를 입력해주세요."); + + public final String message; + + InputException(final String message) { + this.message = message; + } + } } diff --git a/src/main/java/baseball/view/InputView.java b/src/main/java/baseball/view/InputView.java new file mode 100644 index 0000000..26729b9 --- /dev/null +++ b/src/main/java/baseball/view/InputView.java @@ -0,0 +1,41 @@ +package baseball.view; + +import camp.nextstep.edu.missionutils.Console; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +import static baseball.utils.ExceptionConstants.InputException.INPUT_MUST_BE_NUMERIC; + +public class InputView { + public static List readUserBaseballInput() { + System.out.print("숫자를 입력해주세요 : "); + + String userInput = Console.readLine(); + validateInputIsNumeric(userInput); + + return convertUserInputToIntegerList(userInput); + } + + private static List convertUserInputToIntegerList(final String userInput) { + return Arrays.stream(userInput.split("")) + .map(Integer::parseInt) + .collect(Collectors.toList()); + } + + public static int readUserRestartCommandInput() { + String userInput = Console.readLine(); + validateInputIsNumeric(userInput); + + return Integer.parseInt(userInput); + } + + private static void validateInputIsNumeric(final String userInput) { + try { + Integer.parseInt(userInput); + } catch (NumberFormatException exception) { + throw new IllegalArgumentException(INPUT_MUST_BE_NUMERIC.message); + } + } +} From cb910f182a832c6448c1a0f7ed1662eff72a5a5d Mon Sep 17 00:00:00 2001 From: SeoJiWon Date: Sun, 25 Jun 2023 17:18:12 +0900 Subject: [PATCH 12/26] =?UTF-8?q?feat:=20=EC=BD=98=EC=86=94=20=EC=B6=9C?= =?UTF-8?q?=EB=A0=A5=EC=9D=84=20=EC=9C=84=ED=95=9C=20OutputView=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 --- src/main/java/baseball/view/OutputView.java | 59 +++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 src/main/java/baseball/view/OutputView.java diff --git a/src/main/java/baseball/view/OutputView.java b/src/main/java/baseball/view/OutputView.java new file mode 100644 index 0000000..01301eb --- /dev/null +++ b/src/main/java/baseball/view/OutputView.java @@ -0,0 +1,59 @@ +package baseball.view; + +import baseball.model.Result; + +import static baseball.utils.BaseballConstants.BASEBALL_SIZE; + +public class OutputView { + private static final String ENTER = "\n"; + private static final String SPACE = " "; + + private static final String NOTHING = "낫싱"; + private static final String STRIKE = "스트라이크"; + private static final String BALL = "볼"; + + public static void printGameStart() { + System.out.println("숫자 야구 게임을 시작합니다."); + } + + public static void printGameResult(final Result result) { + System.out.println(refineGameResult(result)); + } + + private static String refineGameResult(final Result result) { + if (!hasStrikeCount(result) && !hasBallCount(result)) { + return NOTHING; + } + + StringBuilder resultFormat = new StringBuilder(); + + if (hasBallCount(result)) { + resultFormat.append(result.getBallCount()) + .append(BALL) + .append(SPACE); + } + + if (hasStrikeCount(result)) { + resultFormat.append(result.getStrikeCount()) + .append(STRIKE); + } + + return resultFormat.toString(); + } + + private static boolean hasBallCount(final Result result) { + return result.getBallCount() > 0; + } + + private static boolean hasStrikeCount(final Result result) { + return result.getStrikeCount() > 0; + } + + public static void printGameClear() { + System.out.printf( + "%d개의 숫자를 모두 맞히셨습니다! 게임 종료" + ENTER + + "게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요." + ENTER, + BASEBALL_SIZE + ); + } +} From 715085117d1deec37a7431b4dc4ebf503feca901 Mon Sep 17 00:00:00 2001 From: SeoJiWon Date: Sun, 25 Jun 2023 18:31:21 +0900 Subject: [PATCH 13/26] =?UTF-8?q?feat:=20=EA=B2=8C=EC=9E=84=20=ED=94=84?= =?UTF-8?q?=EB=A1=9C=EC=84=B8=EC=8B=B1=EC=9D=84=20=EC=9C=84=ED=95=9C=20Gam?= =?UTF-8?q?eController=20=EB=B0=8F=20Application=20=EB=8F=99=EC=9E=91=20?= =?UTF-8?q?=EC=B2=98=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 2 + src/main/java/baseball/Application.java | 4 +- .../baseball/controller/GameController.java | 67 +++++++++++++++++++ 3 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 src/main/java/baseball/controller/GameController.java diff --git a/docs/README.md b/docs/README.md index 9fe98b4..ddf62b4 100644 --- a/docs/README.md +++ b/docs/README.md @@ -104,3 +104,5 @@ ### `GameController` - 게임 진행과 관련된 컨트롤러 + +
diff --git a/src/main/java/baseball/Application.java b/src/main/java/baseball/Application.java index dd95a34..119c1e3 100644 --- a/src/main/java/baseball/Application.java +++ b/src/main/java/baseball/Application.java @@ -1,7 +1,9 @@ package baseball; +import baseball.controller.GameController; + public class Application { public static void main(String[] args) { - // TODO: 프로그램 구현 + new GameController().run(); } } diff --git a/src/main/java/baseball/controller/GameController.java b/src/main/java/baseball/controller/GameController.java new file mode 100644 index 0000000..5317d84 --- /dev/null +++ b/src/main/java/baseball/controller/GameController.java @@ -0,0 +1,67 @@ +package baseball.controller; + +import baseball.model.*; +import baseball.view.InputView; +import baseball.view.OutputView; + +import java.util.List; + +import static baseball.model.GameProcessDecider.GAME_RESTART; +import static baseball.model.GameStatus.GAME_RUNNING; +import static baseball.model.GameStatus.GAME_TERMINATE; + +public class GameController { + private static GameStatus gameStatus; + private Computer computer; + private User user; + + public GameController() { + computer = new Computer(); + gameStatus = GAME_RUNNING; + } + + public void run() { + // 게임 시작 + OutputView.printGameStart(); + + while (gameStatus.isGameNotTerminated()) { + // User - Baseball 입력 + readUserBaseballInput(); + + // 게임 결과 확인 + Result result = judgeGameByReferee(); + OutputView.printGameResult(result); + + // 게임 클리어 확인 + checkGameClear(result); + } + } + + public void readUserBaseballInput() { + List userBaseballs = InputView.readUserBaseballInput(); + user = new User(userBaseballs); + } + + private Result judgeGameByReferee() { + return Referee.judge(computer.getBaseballs(), user.getBaseballs()); + } + + private void checkGameClear(final Result result) { + if (result.isGameClear()) { + OutputView.printGameClear(); + determineGameRestartOrEnd(); + } + } + + private void determineGameRestartOrEnd() { + int userCommand = InputView.readUserRestartCommandInput(); + GameProcessDecider decider = GameProcessDecider.getDecider(userCommand); + + if (decider == GAME_RESTART) { + computer = new Computer(); + gameStatus = GAME_RUNNING; + } else { + gameStatus = GAME_TERMINATE; + } + } +} From f28211a81c49c9bb25fe68e4ffb53e2425bed735 Mon Sep 17 00:00:00 2001 From: SeoJiWon Date: Sun, 25 Jun 2023 18:40:12 +0900 Subject: [PATCH 14/26] =?UTF-8?q?test:=20Controller=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EC=BC=80=EC=9D=B4=EC=8A=A4=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/baseball/ApplicationTest.java | 79 ++++++++++++++++++++- 1 file changed, 77 insertions(+), 2 deletions(-) diff --git a/src/test/java/baseball/ApplicationTest.java b/src/test/java/baseball/ApplicationTest.java index 3fa29fa..dd218f6 100644 --- a/src/test/java/baseball/ApplicationTest.java +++ b/src/test/java/baseball/ApplicationTest.java @@ -13,13 +13,88 @@ class ApplicationTest extends NsTest { void 게임종료_후_재시작() { assertRandomNumberInRangeTest( () -> { - run("246", "135", "1", "597", "589", "2"); - assertThat(output()).contains("낫싱", "3스트라이크", "1볼 1스트라이크", "3스트라이크", "게임 종료"); + run( + "246", + "135", + "1", + "597", + "589", + "2" + ); + assertThat(output()) + .contains( + "낫싱", + "3스트라이크", + "1볼 1스트라이크", + "3스트라이크", + "게임 종료" + ); }, 1, 3, 5, 5, 8, 9 ); } + @Test + void 게임종료_후_재시작2() { + assertRandomNumberInRangeTest( + () -> { + run( + "124", + "154", + "127", + "123", + "2" + ); + assertThat(output()) + .contains( + "2스트라이크", + "1스트라이크", + "2스트라이크", + "3스트라이크", + "게임 종료" + ); + }, + 1, 2, 3 + ); + } + + @Test + void 게임종료_후_재시작3() { + assertRandomNumberInRangeTest( + () -> { + run( + "123", + "128", + "813", + "782", + "287", + "987", + "1", + "316", + "312", + "213", + "123", + "2" + ); + assertThat(output()) + .contains( + "낫싱", + "1볼", + "1볼", + "1볼 1스트라이크", + "2스트라이크", + "3스트라이크", + "2볼", + "3볼", + "2볼 1스트라이크", + "3스트라이크", + "게임 종료" + ); + }, + 9, 8, 7, 1, 2, 3 + ); + } + @Test void 예외_테스트() { assertSimpleTest(() -> From 7f0707bbc5cf3e31c7a9d076f8ff727e1e1784e8 Mon Sep 17 00:00:00 2001 From: SeoJiWon Date: Sun, 25 Jun 2023 19:02:05 +0900 Subject: [PATCH 15/26] =?UTF-8?q?refactor:=20=EC=82=AC=EC=9A=A9=EC=9E=90?= =?UTF-8?q?=20=EC=88=AB=EC=9E=90=20Input=EC=8B=9C=20Baseballs=20=EC=88=98?= =?UTF-8?q?=EB=A7=8C=ED=81=BC=20=EC=9E=85=EB=A0=A5=ED=96=88=EB=8A=94?= =?UTF-8?q?=EC=A7=80=20=EA=B2=80=EC=A6=9D=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/baseball/utils/ExceptionConstants.java | 3 ++- src/main/java/baseball/view/InputView.java | 9 +++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/main/java/baseball/utils/ExceptionConstants.java b/src/main/java/baseball/utils/ExceptionConstants.java index eca370c..2dc808c 100644 --- a/src/main/java/baseball/utils/ExceptionConstants.java +++ b/src/main/java/baseball/utils/ExceptionConstants.java @@ -3,7 +3,7 @@ public class ExceptionConstants { public enum BaseballException { BASEBALL_IS_NOT_IN_RANGE("숫자는 1..9 범위만 허용합니다."), - BASEBALL_SIZE_IS_NOT_FULFILL("숫자 3개를 입력해주세요."), + BASEBALL_SIZE_IS_NOT_FULFILL("숫자 야구 게임을 위해서 숫자 3개가 필요합니다."), BASEBALL_MUST_BE_UNIQUE("중복된 숫자는 허용하지 않습니다."), ; @@ -25,6 +25,7 @@ public enum GameProcessCommandException { } public enum InputException { + INPUT_LENGTH_MUST_BE_ACCEPTABLE_FOR_BASEBALLS("띄어쓰기 없이 숫자 3개를 입력해주세요."), INPUT_MUST_BE_NUMERIC("숫자를 입력해주세요."); public final String message; diff --git a/src/main/java/baseball/view/InputView.java b/src/main/java/baseball/view/InputView.java index 26729b9..44840fe 100644 --- a/src/main/java/baseball/view/InputView.java +++ b/src/main/java/baseball/view/InputView.java @@ -6,6 +6,8 @@ import java.util.List; import java.util.stream.Collectors; +import static baseball.utils.BaseballConstants.BASEBALL_SIZE; +import static baseball.utils.ExceptionConstants.InputException.INPUT_LENGTH_MUST_BE_ACCEPTABLE_FOR_BASEBALLS; import static baseball.utils.ExceptionConstants.InputException.INPUT_MUST_BE_NUMERIC; public class InputView { @@ -14,6 +16,7 @@ public static List readUserBaseballInput() { String userInput = Console.readLine(); validateInputIsNumeric(userInput); + validateInputLengthIsAcceptableForBaseballs(userInput); return convertUserInputToIntegerList(userInput); } @@ -31,6 +34,12 @@ public static int readUserRestartCommandInput() { return Integer.parseInt(userInput); } + private static void validateInputLengthIsAcceptableForBaseballs(final String userInput) { + if (userInput.length() != BASEBALL_SIZE) { + throw new IllegalArgumentException(INPUT_LENGTH_MUST_BE_ACCEPTABLE_FOR_BASEBALLS.message); + } + } + private static void validateInputIsNumeric(final String userInput) { try { Integer.parseInt(userInput); From b0dabead03c89e89cd8e57e19ec34fa26302b0f1 Mon Sep 17 00:00:00 2001 From: SeoJiWon Date: Sun, 25 Jun 2023 22:29:50 +0900 Subject: [PATCH 16/26] =?UTF-8?q?refactor:=20=EC=82=AC=EC=9A=A9=EC=9E=90?= =?UTF-8?q?=20Input=EC=97=90=20=EB=8C=80=ED=95=9C=20Length=20=EA=B2=80?= =?UTF-8?q?=EC=A6=9D=20->=20=EA=B3=B5=EB=B0=B1=20=EC=A1=B4=EC=9E=AC=20?= =?UTF-8?q?=EA=B2=80=EC=A6=9D=20=EC=BC=80=EC=9D=B4=EC=8A=A4=EB=A1=9C=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../baseball/utils/ExceptionConstants.java | 8 ++-- src/main/java/baseball/view/InputView.java | 38 ++++++++++--------- 2 files changed, 26 insertions(+), 20 deletions(-) diff --git a/src/main/java/baseball/utils/ExceptionConstants.java b/src/main/java/baseball/utils/ExceptionConstants.java index 2dc808c..b33d5af 100644 --- a/src/main/java/baseball/utils/ExceptionConstants.java +++ b/src/main/java/baseball/utils/ExceptionConstants.java @@ -15,7 +15,8 @@ public enum BaseballException { } public enum GameProcessCommandException { - INVALID_COMMAND("재시작[1] / 종료[2] 중 하나를 입력해주세요."); + INVALID_COMMAND("재시작[1] / 종료[2] 중 하나를 입력해주세요."), + ; public final String message; @@ -25,8 +26,9 @@ public enum GameProcessCommandException { } public enum InputException { - INPUT_LENGTH_MUST_BE_ACCEPTABLE_FOR_BASEBALLS("띄어쓰기 없이 숫자 3개를 입력해주세요."), - INPUT_MUST_BE_NUMERIC("숫자를 입력해주세요."); + INPUT_MUST_BE_NUMERIC("숫자를 입력해주세요."), + INPUT_MUST_NOT_CONTAINS_SPACE("공백없이 숫자를 입력해주세요."), + ; public final String message; diff --git a/src/main/java/baseball/view/InputView.java b/src/main/java/baseball/view/InputView.java index 44840fe..a2dda42 100644 --- a/src/main/java/baseball/view/InputView.java +++ b/src/main/java/baseball/view/InputView.java @@ -6,9 +6,8 @@ import java.util.List; import java.util.stream.Collectors; -import static baseball.utils.BaseballConstants.BASEBALL_SIZE; -import static baseball.utils.ExceptionConstants.InputException.INPUT_LENGTH_MUST_BE_ACCEPTABLE_FOR_BASEBALLS; import static baseball.utils.ExceptionConstants.InputException.INPUT_MUST_BE_NUMERIC; +import static baseball.utils.ExceptionConstants.InputException.INPUT_MUST_NOT_CONTAINS_SPACE; public class InputView { public static List readUserBaseballInput() { @@ -16,11 +15,30 @@ public static List readUserBaseballInput() { String userInput = Console.readLine(); validateInputIsNumeric(userInput); - validateInputLengthIsAcceptableForBaseballs(userInput); + validateInputHasSpace(userInput); return convertUserInputToIntegerList(userInput); } + private static void validateInputIsNumeric(final String userInput) { + try { + Integer.parseInt(userInput); + } catch (NumberFormatException exception) { + throw new IllegalArgumentException(INPUT_MUST_BE_NUMERIC.message); + } + } + + private static void validateInputHasSpace(final String userInput) { + if (hasSpace(userInput)) { + throw new IllegalArgumentException(INPUT_MUST_NOT_CONTAINS_SPACE.message); + } + } + + private static boolean hasSpace(String userInput) { + return userInput.chars() + .anyMatch(Character::isWhitespace); + } + private static List convertUserInputToIntegerList(final String userInput) { return Arrays.stream(userInput.split("")) .map(Integer::parseInt) @@ -33,18 +51,4 @@ public static int readUserRestartCommandInput() { return Integer.parseInt(userInput); } - - private static void validateInputLengthIsAcceptableForBaseballs(final String userInput) { - if (userInput.length() != BASEBALL_SIZE) { - throw new IllegalArgumentException(INPUT_LENGTH_MUST_BE_ACCEPTABLE_FOR_BASEBALLS.message); - } - } - - private static void validateInputIsNumeric(final String userInput) { - try { - Integer.parseInt(userInput); - } catch (NumberFormatException exception) { - throw new IllegalArgumentException(INPUT_MUST_BE_NUMERIC.message); - } - } } From ba99c7e7ad9b5457bf2937dd3b69bfc7066dbdb7 Mon Sep 17 00:00:00 2001 From: SeoJiWon Date: Fri, 30 Jun 2023 00:29:58 +0900 Subject: [PATCH 17/26] =?UTF-8?q?refactor:=20SPACE=20->=20SEPARATOR=20?= =?UTF-8?q?=EB=84=A4=EC=9D=B4=EB=B0=8D=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/baseball/view/OutputView.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/baseball/view/OutputView.java b/src/main/java/baseball/view/OutputView.java index 01301eb..22721d3 100644 --- a/src/main/java/baseball/view/OutputView.java +++ b/src/main/java/baseball/view/OutputView.java @@ -6,7 +6,7 @@ public class OutputView { private static final String ENTER = "\n"; - private static final String SPACE = " "; + private static final String SEPARATOR = " "; private static final String NOTHING = "낫싱"; private static final String STRIKE = "스트라이크"; @@ -30,7 +30,7 @@ private static String refineGameResult(final Result result) { if (hasBallCount(result)) { resultFormat.append(result.getBallCount()) .append(BALL) - .append(SPACE); + .append(SEPARATOR); } if (hasStrikeCount(result)) { From 36107ff667b5e23c6bc9c4568f53b0d88ec7684f Mon Sep 17 00:00:00 2001 From: SeoJiWon Date: Sun, 2 Jul 2023 10:47:29 +0900 Subject: [PATCH 18/26] =?UTF-8?q?fix:=20=EC=BB=B4=ED=93=A8=ED=84=B0=20Base?= =?UTF-8?q?balls=20=EB=9E=9C=EB=8D=A4=20=EC=83=9D=EC=84=B1=20=EA=B4=80?= =?UTF-8?q?=EB=A0=A8=20not=20equal=20->=20less=20=EC=A1=B0=EA=B1=B4?= =?UTF-8?q?=EC=9C=BC=EB=A1=9C=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/baseball/model/Computer.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/baseball/model/Computer.java b/src/main/java/baseball/model/Computer.java index 65841c0..9328a59 100644 --- a/src/main/java/baseball/model/Computer.java +++ b/src/main/java/baseball/model/Computer.java @@ -17,7 +17,7 @@ public Computer() { private Baseballs generateRandomBaseballs() { List baseballs = new ArrayList<>(); - while (baseballs.size() != BASEBALL_SIZE) { + while (baseballs.size() < BASEBALL_SIZE) { int randomNumber = Randoms.pickNumberInRange(MIN_BASEBALL, MAX_BASEBALL); if (!baseballs.contains(randomNumber)) { baseballs.add(randomNumber); From 10961ce4590ff4cf45154e5c57b084ca5d8db3f7 Mon Sep 17 00:00:00 2001 From: SeoJiWon Date: Sun, 2 Jul 2023 10:55:12 +0900 Subject: [PATCH 19/26] =?UTF-8?q?test:=20Computer=20Baseballs=20=EB=9E=9C?= =?UTF-8?q?=EB=8D=A4=20=EC=83=9D=EC=84=B1=20=EA=B4=80=EB=A0=A8=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8=20=EC=BC=80=EC=9D=B4=EC=8A=A4=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 --- src/test/java/baseball/model/ComputerTest.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/test/java/baseball/model/ComputerTest.java b/src/test/java/baseball/model/ComputerTest.java index 44aef57..ea771bb 100644 --- a/src/test/java/baseball/model/ComputerTest.java +++ b/src/test/java/baseball/model/ComputerTest.java @@ -4,6 +4,7 @@ import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertAll; class ComputerTest { @Test @@ -13,6 +14,14 @@ void construct() { final Computer computer = new Computer(); // then - assertThat(computer.getBaseballs()).hasSize(3); + assertAll( + () -> assertThat(computer.getBaseballs()).hasSize(3), // size + () -> assertThat( + computer.getBaseballs() + .stream() + .distinct() + .count() + ).isEqualTo(3) // has duplicate + ); } } From e619cf89bf553415dbd14ff441447da4a8aa4262 Mon Sep 17 00:00:00 2001 From: SeoJiWon Date: Mon, 3 Jul 2023 14:36:56 +0900 Subject: [PATCH 20/26] =?UTF-8?q?refactor:=20readUserBaseballInput=20?= =?UTF-8?q?=EC=A0=91=EA=B7=BC=EC=A0=9C=EC=96=B4=EC=9E=90=20public=20->=20p?= =?UTF-8?q?rivate?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/baseball/controller/GameController.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/baseball/controller/GameController.java b/src/main/java/baseball/controller/GameController.java index 5317d84..bcb0d22 100644 --- a/src/main/java/baseball/controller/GameController.java +++ b/src/main/java/baseball/controller/GameController.java @@ -37,7 +37,7 @@ public void run() { } } - public void readUserBaseballInput() { + private void readUserBaseballInput() { List userBaseballs = InputView.readUserBaseballInput(); user = new User(userBaseballs); } From 95f69e008f53125b030a97fadb363e2b1ba90b9d Mon Sep 17 00:00:00 2001 From: SeoJiWon Date: Mon, 3 Jul 2023 19:26:03 +0900 Subject: [PATCH 21/26] =?UTF-8?q?refactor:=20Computer=20baseballs=20?= =?UTF-8?q?=EC=83=9D=EC=84=B1=20=EA=B0=84=20=EB=82=B4=EB=B6=80=20=EB=A1=9C?= =?UTF-8?q?=EC=A7=81=20=EB=A9=94=EC=86=8C=EB=93=9C=ED=99=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/baseball/model/Computer.java | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/main/java/baseball/model/Computer.java b/src/main/java/baseball/model/Computer.java index 9328a59..e0eaa74 100644 --- a/src/main/java/baseball/model/Computer.java +++ b/src/main/java/baseball/model/Computer.java @@ -17,9 +17,9 @@ public Computer() { private Baseballs generateRandomBaseballs() { List baseballs = new ArrayList<>(); - while (baseballs.size() < BASEBALL_SIZE) { - int randomNumber = Randoms.pickNumberInRange(MIN_BASEBALL, MAX_BASEBALL); - if (!baseballs.contains(randomNumber)) { + while (isNotReachedLimitSize(baseballs)) { + int randomNumber = getRandomNumberInRange(); + if (isRandomNumberAbsentInBaseballs(baseballs, randomNumber)) { baseballs.add(randomNumber); } } @@ -27,6 +27,21 @@ private Baseballs generateRandomBaseballs() { return new Baseballs(baseballs); } + private boolean isNotReachedLimitSize(final List baseballs) { + return baseballs.size() < BASEBALL_SIZE; + } + + private int getRandomNumberInRange() { + return Randoms.pickNumberInRange(MIN_BASEBALL, MAX_BASEBALL); + } + + private boolean isRandomNumberAbsentInBaseballs( + final List baseballs, + final int randomNumber + ) { + return !baseballs.contains(randomNumber); + } + public List getBaseballs() { return baseballs.getBaseballs(); } From 0e47d8981186518e87f56925da9ef6c36118c67c Mon Sep 17 00:00:00 2001 From: SeoJiWon Date: Mon, 3 Jul 2023 19:31:24 +0900 Subject: [PATCH 22/26] =?UTF-8?q?refactor:=20Referee=20strikeCount=20?= =?UTF-8?q?=EA=B3=84=EC=82=B0=20=EB=A1=9C=EC=A7=81=20=EA=B0=84=20filter=20?= =?UTF-8?q?comparing=20method=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/baseball/model/Referee.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/main/java/baseball/model/Referee.java b/src/main/java/baseball/model/Referee.java index f20ff45..318f02b 100644 --- a/src/main/java/baseball/model/Referee.java +++ b/src/main/java/baseball/model/Referee.java @@ -24,10 +24,18 @@ private static int calculateStrikeCount( final List userBaseballs ) { return (int) IntStream.range(0, BASEBALL_SIZE) - .filter(index -> computerBaseballs.get(index).equals(userBaseballs.get(index))) + .filter(index -> isExactlyMatchAtIndex(computerBaseballs, userBaseballs, index)) .count(); } + private static boolean isExactlyMatchAtIndex( + final List computerBaseballs, + final List userBaseballs, + final int index + ) { + return computerBaseballs.get(index).equals(userBaseballs.get(index)); + } + private static int calculateBallCount( final List computerBaseballs, final List userBaseballs From d71a42944cb1215252cb685df260b6ec343c4047 Mon Sep 17 00:00:00 2001 From: SeoJiWon Date: Mon, 3 Jul 2023 19:34:03 +0900 Subject: [PATCH 23/26] refactor: ExceptionConstants class -> interface --- src/main/java/baseball/utils/ExceptionConstants.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/baseball/utils/ExceptionConstants.java b/src/main/java/baseball/utils/ExceptionConstants.java index b33d5af..cc4985d 100644 --- a/src/main/java/baseball/utils/ExceptionConstants.java +++ b/src/main/java/baseball/utils/ExceptionConstants.java @@ -1,7 +1,7 @@ package baseball.utils; -public class ExceptionConstants { - public enum BaseballException { +public interface ExceptionConstants { + enum BaseballException { BASEBALL_IS_NOT_IN_RANGE("숫자는 1..9 범위만 허용합니다."), BASEBALL_SIZE_IS_NOT_FULFILL("숫자 야구 게임을 위해서 숫자 3개가 필요합니다."), BASEBALL_MUST_BE_UNIQUE("중복된 숫자는 허용하지 않습니다."), @@ -14,7 +14,7 @@ public enum BaseballException { } } - public enum GameProcessCommandException { + enum GameProcessCommandException { INVALID_COMMAND("재시작[1] / 종료[2] 중 하나를 입력해주세요."), ; @@ -25,7 +25,7 @@ public enum GameProcessCommandException { } } - public enum InputException { + enum InputException { INPUT_MUST_BE_NUMERIC("숫자를 입력해주세요."), INPUT_MUST_NOT_CONTAINS_SPACE("공백없이 숫자를 입력해주세요."), ; From 56cf0e5355128f7a25fdca643ccd33d19bf26aa4 Mon Sep 17 00:00:00 2001 From: SeoJiWon Date: Mon, 3 Jul 2023 19:39:03 +0900 Subject: [PATCH 24/26] =?UTF-8?q?refactor:=20=EC=82=AC=EC=9A=A9=EC=9E=90?= =?UTF-8?q?=20Input=20I/O=EA=B0=84=20=EA=B2=80=EC=A6=9D=20=EC=88=9C?= =?UTF-8?q?=EC=84=9C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - baseball 입력 -> space 확인 -> 정수 확인 - restart command -> space 확인 -> 정수 확인 --- .../baseball/controller/GameController.java | 4 ++-- src/main/java/baseball/view/InputView.java | 19 ++++++++++--------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/main/java/baseball/controller/GameController.java b/src/main/java/baseball/controller/GameController.java index bcb0d22..b3f1db8 100644 --- a/src/main/java/baseball/controller/GameController.java +++ b/src/main/java/baseball/controller/GameController.java @@ -38,7 +38,7 @@ public void run() { } private void readUserBaseballInput() { - List userBaseballs = InputView.readUserBaseballInput(); + List userBaseballs = InputView.readUserBaseballs(); user = new User(userBaseballs); } @@ -54,7 +54,7 @@ private void checkGameClear(final Result result) { } private void determineGameRestartOrEnd() { - int userCommand = InputView.readUserRestartCommandInput(); + int userCommand = InputView.readUserRestartCommand(); GameProcessDecider decider = GameProcessDecider.getDecider(userCommand); if (decider == GAME_RESTART) { diff --git a/src/main/java/baseball/view/InputView.java b/src/main/java/baseball/view/InputView.java index a2dda42..9ab4afd 100644 --- a/src/main/java/baseball/view/InputView.java +++ b/src/main/java/baseball/view/InputView.java @@ -10,16 +10,22 @@ import static baseball.utils.ExceptionConstants.InputException.INPUT_MUST_NOT_CONTAINS_SPACE; public class InputView { - public static List readUserBaseballInput() { + public static List readUserBaseballs() { System.out.print("숫자를 입력해주세요 : "); String userInput = Console.readLine(); - validateInputIsNumeric(userInput); validateInputHasSpace(userInput); + validateInputIsNumeric(userInput); return convertUserInputToIntegerList(userInput); } + private static void validateInputHasSpace(final String userInput) { + if (hasSpace(userInput)) { + throw new IllegalArgumentException(INPUT_MUST_NOT_CONTAINS_SPACE.message); + } + } + private static void validateInputIsNumeric(final String userInput) { try { Integer.parseInt(userInput); @@ -28,12 +34,6 @@ private static void validateInputIsNumeric(final String userInput) { } } - private static void validateInputHasSpace(final String userInput) { - if (hasSpace(userInput)) { - throw new IllegalArgumentException(INPUT_MUST_NOT_CONTAINS_SPACE.message); - } - } - private static boolean hasSpace(String userInput) { return userInput.chars() .anyMatch(Character::isWhitespace); @@ -45,8 +45,9 @@ private static List convertUserInputToIntegerList(final String userInpu .collect(Collectors.toList()); } - public static int readUserRestartCommandInput() { + public static int readUserRestartCommand() { String userInput = Console.readLine(); + validateInputHasSpace(userInput); validateInputIsNumeric(userInput); return Integer.parseInt(userInput); From 991a5d918566cfd8977869415ed17bfbc0c63d96 Mon Sep 17 00:00:00 2001 From: SeoJiWon Date: Mon, 3 Jul 2023 23:16:54 +0900 Subject: [PATCH 25/26] =?UTF-8?q?refactor:=20hasSpace=20=EB=A9=94=EC=86=8C?= =?UTF-8?q?=EB=93=9C=20=EC=9C=84=EC=B9=98=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/baseball/view/InputView.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/baseball/view/InputView.java b/src/main/java/baseball/view/InputView.java index 9ab4afd..388b084 100644 --- a/src/main/java/baseball/view/InputView.java +++ b/src/main/java/baseball/view/InputView.java @@ -26,6 +26,11 @@ private static void validateInputHasSpace(final String userInput) { } } + private static boolean hasSpace(String userInput) { + return userInput.chars() + .anyMatch(Character::isWhitespace); + } + private static void validateInputIsNumeric(final String userInput) { try { Integer.parseInt(userInput); @@ -34,11 +39,6 @@ private static void validateInputIsNumeric(final String userInput) { } } - private static boolean hasSpace(String userInput) { - return userInput.chars() - .anyMatch(Character::isWhitespace); - } - private static List convertUserInputToIntegerList(final String userInput) { return Arrays.stream(userInput.split("")) .map(Integer::parseInt) From bdb23468786ede598d88a22b1fc18c35f608de6c Mon Sep 17 00:00:00 2001 From: SeoJiWon Date: Sat, 15 Jul 2023 15:16:31 +0900 Subject: [PATCH 26/26] =?UTF-8?q?fix:=20UserTest=EA=B0=84=20new=20Baseball?= =?UTF-8?q?=20->=20new=20User=EB=A1=9C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/baseball/model/UserTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/test/java/baseball/model/UserTest.java b/src/test/java/baseball/model/UserTest.java index 7559bc2..6e85b41 100644 --- a/src/test/java/baseball/model/UserTest.java +++ b/src/test/java/baseball/model/UserTest.java @@ -17,7 +17,7 @@ void throwExceptionByBaseballIsNotInRange() { final List baseballs = List.of(1, 9, 10); // when - then - assertThatThrownBy(() -> new Baseballs(baseballs)) + assertThatThrownBy(() -> new User(baseballs)) .isInstanceOf(IllegalArgumentException.class) .hasMessage(BASEBALL_IS_NOT_IN_RANGE.message); } @@ -29,7 +29,7 @@ void throwExceptionByBaseballSizeNotFulfill() { final List baseballs = List.of(1, 2); // when - then - assertThatThrownBy(() -> new Baseballs(baseballs)) + assertThatThrownBy(() -> new User(baseballs)) .isInstanceOf(IllegalArgumentException.class) .hasMessage(BASEBALL_SIZE_IS_NOT_FULFILL.message); } @@ -41,7 +41,7 @@ void throwExceptionByBaseballIsNotUnique() { final List baseballs = List.of(1, 2, 2); // when - then - assertThatThrownBy(() -> new Baseballs(baseballs)) + assertThatThrownBy(() -> new User(baseballs)) .isInstanceOf(IllegalArgumentException.class) .hasMessage(BASEBALL_MUST_BE_UNIQUE.message); }