From 33a7d0d73c3b1f24aa7f3bd44d96914e0cf1250a Mon Sep 17 00:00:00 2001 From: Jiwoo Kim Date: Sat, 3 Jul 2021 18:07:19 +0900 Subject: [PATCH 01/55] docs: initial commit --- src/main/java/empty.txt | 0 src/test/java/empty.txt | 0 2 files changed, 0 insertions(+), 0 deletions(-) delete mode 100644 src/main/java/empty.txt delete mode 100644 src/test/java/empty.txt diff --git a/src/main/java/empty.txt b/src/main/java/empty.txt deleted file mode 100644 index e69de29b..00000000 diff --git a/src/test/java/empty.txt b/src/test/java/empty.txt deleted file mode 100644 index e69de29b..00000000 From e9d200100c1f1df9b3c4ee69520dc66029cd6e7d Mon Sep 17 00:00:00 2001 From: Jiwoo Kim Date: Sat, 3 Jul 2021 18:14:39 +0900 Subject: [PATCH 02/55] =?UTF-8?q?docs:=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84=20=EB=AA=A9=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 --- README.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/README.md b/README.md index fc275557..03b57942 100644 --- a/README.md +++ b/README.md @@ -1 +1,27 @@ # java-lotto 게임 + +# 구현 기능 목록 + +- [ ] 구입금액 입력받기 + - [ ] 1000원 미만 입력 시 처리 + - [ ] 1000의 배수 아닌 값 입력 시 처리 + - [ ] 숫자가 아닌 값 (문자열) + - [ ] 정상값 처리 + - [ ] 공백 처리 +- [ ] 구매 개수 구하기 +- [ ] 랜덤 로또번호 구매 개수만큼 만들기 +- [ ] 지난 주 당첨 번호 입력받기 + - [ ] 숫자 개수 6개 확인 + - [ ] 숫자가 아닌 값 포함 + - [ ] 범위 (1~45) 확인 + - [ ] 공백 처리 +- [ ] 보너스 볼 입력받기 + - [ ] 숫자가 아닌 값 + - [ ] 범위 (1~45) 확인 + - [ ] 공백 처리 +- [ ] 당첨 통계 + - [ ] 당첨 조건을 enum 처리 + - [ ] 일치 개수 찾기 + - [ ] 5개 일치 시 보너스 볼 일치 여부 확인 + - [ ] 수익률 구하기 (당첨값의 합 / 구입금액) + - [ ] 결과 출력 \ No newline at end of file From 05fbeba382ca1623a92d7a66aac4a6361dd45710 Mon Sep 17 00:00:00 2001 From: Jiwoo Kim Date: Sat, 3 Jul 2021 18:33:20 +0900 Subject: [PATCH 03/55] =?UTF-8?q?feat:1000=EC=9B=90=20=EB=AF=B8=EB=A7=8C?= =?UTF-8?q?=20=EC=9E=85=EB=A0=A5=20=EC=8B=9C=20=ED=85=8C=EC=8A=A4=ED=8A=B8?= =?UTF-8?q?=EB=A9=94=EC=84=9C=EB=93=9C=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 ++-- src/main/java/LottoService.java | 8 ++++++++ src/test/java/LottoServiceTest.java | 20 ++++++++++++++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 src/main/java/LottoService.java create mode 100644 src/test/java/LottoServiceTest.java diff --git a/README.md b/README.md index 03b57942..c56f43da 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,9 @@ -# java-lotto 게임 +# java-Lotto 게임 # 구현 기능 목록 - [ ] 구입금액 입력받기 - - [ ] 1000원 미만 입력 시 처리 + - [x] 1000원 미만 입력 시 처리 - [ ] 1000의 배수 아닌 값 입력 시 처리 - [ ] 숫자가 아닌 값 (문자열) - [ ] 정상값 처리 diff --git a/src/main/java/LottoService.java b/src/main/java/LottoService.java new file mode 100644 index 00000000..55f3c6d7 --- /dev/null +++ b/src/main/java/LottoService.java @@ -0,0 +1,8 @@ +public class LottoService { + + public void validate(int input) { + if(input < 1000) { + throw new RuntimeException("입력값이 잘못되었습니다"); + } + } +} diff --git a/src/test/java/LottoServiceTest.java b/src/test/java/LottoServiceTest.java new file mode 100644 index 00000000..30af7ef1 --- /dev/null +++ b/src/test/java/LottoServiceTest.java @@ -0,0 +1,20 @@ +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertThrows; + +public class LottoServiceTest { + LottoService lottoservice; + + @Test + public void testInputValudUnder1000() throws Exception { + // given + int input = 800; + + // when + + // then + assertThrows(RuntimeException.class, () -> { + lottoservice.validate(input); + }); + } +} From 538e6b8f60279212ce1206e398528ce9bc8f3688 Mon Sep 17 00:00:00 2001 From: Jiwoo Kim Date: Sat, 3 Jul 2021 18:49:32 +0900 Subject: [PATCH 04/55] =?UTF-8?q?feat:=201000=EC=9D=98=20=EB=B0=B0?= =?UTF-8?q?=EC=88=98=20=EC=95=84=EB=8B=8C=20=EA=B0=92=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=EB=A9=94=EC=84=9C=EB=93=9C=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- src/main/java/LottoService.java | 8 ++++++-- src/test/java/LottoServiceTest.java | 22 ++++++++++++++++------ 3 files changed, 23 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index c56f43da..3049f188 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ - [ ] 구입금액 입력받기 - [x] 1000원 미만 입력 시 처리 - - [ ] 1000의 배수 아닌 값 입력 시 처리 + - [x] 1000의 배수 아닌 값 입력 시 처리 - [ ] 숫자가 아닌 값 (문자열) - [ ] 정상값 처리 - [ ] 공백 처리 diff --git a/src/main/java/LottoService.java b/src/main/java/LottoService.java index 55f3c6d7..894071ad 100644 --- a/src/main/java/LottoService.java +++ b/src/main/java/LottoService.java @@ -1,8 +1,12 @@ public class LottoService { public void validate(int input) { - if(input < 1000) { - throw new RuntimeException("입력값이 잘못되었습니다"); + if (notMatchesCondition(input)) { + throw new IllegalArgumentException("입력값이 잘못되었습니다"); } } + + private boolean notMatchesCondition(int input) { + return input < 1000 || input % 1000 != 0; + } } diff --git a/src/test/java/LottoServiceTest.java b/src/test/java/LottoServiceTest.java index 30af7ef1..613ff6d3 100644 --- a/src/test/java/LottoServiceTest.java +++ b/src/test/java/LottoServiceTest.java @@ -1,20 +1,30 @@ import org.junit.jupiter.api.Test; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.jupiter.api.Assertions.assertThrows; public class LottoServiceTest { - LottoService lottoservice; + + LottoService lottoService = new LottoService(); @Test - public void testInputValudUnder1000() throws Exception { + public void testInputValueUnder1000() throws Exception { // given int input = 800; - // when - - // then + // when, then assertThrows(RuntimeException.class, () -> { - lottoservice.validate(input); + lottoService.validate(input); }); } + + @Test + public void testInputValueNotMultiple1000() throws Exception { + // given + int input = 1234; + + // when, then + assertThatThrownBy(() -> lottoService.validate(input)) + .isInstanceOf(IllegalArgumentException.class); + } } From 96e6d6332bdb361bc2c27924b6164fb55654121b Mon Sep 17 00:00:00 2001 From: Jiwoo Kim Date: Sat, 3 Jul 2021 19:13:10 +0900 Subject: [PATCH 05/55] =?UTF-8?q?feat:=20=EA=B8=88=EC=95=A1=20=EC=9E=85?= =?UTF-8?q?=EB=A0=A5=EC=97=90=20=EB=AC=B8=EC=9E=90=EC=97=B4=20=EC=9E=85?= =?UTF-8?q?=EB=A0=A5=EC=8B=9C=20=EC=98=88=EC=99=B8=20=EC=B2=98=EB=A6=AC=20?= =?UTF-8?q?=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EB=A9=94=EC=84=9C=EB=93=9C=20?= =?UTF-8?q?=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- src/main/java/LottoService.java | 10 ++++++++-- src/test/java/LottoServiceTest.java | 27 ++++++++++++++++++++++++--- 3 files changed, 33 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 3049f188..cfe3d8b8 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ - [ ] 구입금액 입력받기 - [x] 1000원 미만 입력 시 처리 - [x] 1000의 배수 아닌 값 입력 시 처리 - - [ ] 숫자가 아닌 값 (문자열) + - [x] 숫자가 아닌 값 (문자열) - [ ] 정상값 처리 - [ ] 공백 처리 - [ ] 구매 개수 구하기 diff --git a/src/main/java/LottoService.java b/src/main/java/LottoService.java index 894071ad..9e5fe23a 100644 --- a/src/main/java/LottoService.java +++ b/src/main/java/LottoService.java @@ -1,7 +1,13 @@ +import java.util.regex.Pattern; + public class LottoService { - public void validate(int input) { - if (notMatchesCondition(input)) { + public void validate(String input) { + if (!Pattern.matches("^[0-9]*$", input)) { + throw new RuntimeException("잘못된 입력입니다"); + } + int inputConversion = Integer.parseInt(input); + if (notMatchesCondition(inputConversion)) { throw new IllegalArgumentException("입력값이 잘못되었습니다"); } } diff --git a/src/test/java/LottoServiceTest.java b/src/test/java/LottoServiceTest.java index 613ff6d3..4cffa6df 100644 --- a/src/test/java/LottoServiceTest.java +++ b/src/test/java/LottoServiceTest.java @@ -1,16 +1,16 @@ import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertThrows; public class LottoServiceTest { - LottoService lottoService = new LottoService(); @Test public void testInputValueUnder1000() throws Exception { // given - int input = 800; + String input = "800"; // when, then assertThrows(RuntimeException.class, () -> { @@ -18,13 +18,34 @@ public void testInputValueUnder1000() throws Exception { }); } + @Test + public void testInputValueNotUnder1000() throws Exception { + // given + String input = "2000"; + + // when, // then + assertDoesNotThrow(() -> lottoService.validate(input)); + } + @Test public void testInputValueNotMultiple1000() throws Exception { // given - int input = 1234; + String input = "1234"; // when, then assertThatThrownBy(() -> lottoService.validate(input)) .isInstanceOf(IllegalArgumentException.class); } + + @Test + public void testInputNotNumber() throws Exception { + // given + String input = "error"; + + // when + + // then + assertThatThrownBy(() -> lottoService.validate(input)) + .isInstanceOf(RuntimeException.class); + } } From fd24a8ddcc7102be3e1476435acb1ce12dd7a521 Mon Sep 17 00:00:00 2001 From: Jiwoo Kim Date: Sat, 3 Jul 2021 19:34:56 +0900 Subject: [PATCH 06/55] =?UTF-8?q?refactor:=20perchasePrice,=20LottoControl?= =?UTF-8?q?ler=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 7 ++++ src/main/java/LottoService.java | 18 ---------- src/main/java/lotto/LottoController.java | 8 +++++ src/main/java/lotto/LottoService.java | 11 ++++++ src/main/java/lotto/PurchasePrice.java | 35 +++++++++++++++++++ ...viceTest.java => LottoControllerTest.java} | 16 +++++---- 6 files changed, 70 insertions(+), 25 deletions(-) delete mode 100644 src/main/java/LottoService.java create mode 100644 src/main/java/lotto/LottoController.java create mode 100644 src/main/java/lotto/LottoService.java create mode 100644 src/main/java/lotto/PurchasePrice.java rename src/test/java/{LottoServiceTest.java => LottoControllerTest.java} (68%) diff --git a/build.gradle b/build.gradle index 092a1c08..538e48d2 100644 --- a/build.gradle +++ b/build.gradle @@ -10,6 +10,13 @@ repositories { dependencies { testImplementation('org.junit.jupiter:junit-jupiter:5.6.0') testImplementation('org.assertj:assertj-core:3.15.0') + + compileOnly 'org.projectlombok:lombok:1.18.20' + annotationProcessor 'org.projectlombok:lombok:1.18.20' + + testCompileOnly 'org.projectlombok:lombok:1.18.20' + testAnnotationProcessor 'org.projectlombok:lombok:1.18.20' + } test { diff --git a/src/main/java/LottoService.java b/src/main/java/LottoService.java deleted file mode 100644 index 9e5fe23a..00000000 --- a/src/main/java/LottoService.java +++ /dev/null @@ -1,18 +0,0 @@ -import java.util.regex.Pattern; - -public class LottoService { - - public void validate(String input) { - if (!Pattern.matches("^[0-9]*$", input)) { - throw new RuntimeException("잘못된 입력입니다"); - } - int inputConversion = Integer.parseInt(input); - if (notMatchesCondition(inputConversion)) { - throw new IllegalArgumentException("입력값이 잘못되었습니다"); - } - } - - private boolean notMatchesCondition(int input) { - return input < 1000 || input % 1000 != 0; - } -} diff --git a/src/main/java/lotto/LottoController.java b/src/main/java/lotto/LottoController.java new file mode 100644 index 00000000..9ae55b4a --- /dev/null +++ b/src/main/java/lotto/LottoController.java @@ -0,0 +1,8 @@ +package lotto; + +public class LottoController { + public void validate(String input) { + PurchasePrice purchasePrice = new PurchasePrice(input); + + } +} diff --git a/src/main/java/lotto/LottoService.java b/src/main/java/lotto/LottoService.java new file mode 100644 index 00000000..3ec87674 --- /dev/null +++ b/src/main/java/lotto/LottoService.java @@ -0,0 +1,11 @@ +package lotto; + +import lotto.PurchasePrice; + +import java.util.regex.Pattern; + + +public class LottoService { + + +} diff --git a/src/main/java/lotto/PurchasePrice.java b/src/main/java/lotto/PurchasePrice.java new file mode 100644 index 00000000..5d69fc54 --- /dev/null +++ b/src/main/java/lotto/PurchasePrice.java @@ -0,0 +1,35 @@ +package lotto; + +import java.util.regex.Pattern; + +public class PurchasePrice { + + private static final String NUMBER_REGEX = "^[0-9]*$"; + private static final int MINIMUM_INPUT = 1000; + private static final int STANDARD = 1000; + + private final int purchasePrice; + + public PurchasePrice(String input) { + validate(input); + this.purchasePrice = Integer.parseInt(input); + } + + private void validate(String input) { + if (!Pattern.matches(NUMBER_REGEX, input)) { + throw new IllegalArgumentException("잘못된 입력입니다"); + } + int inputConversion = Integer.parseInt(input); + if (notMatchesCondition(inputConversion)) { + throw new IllegalArgumentException("입력값이 잘못되었습니다"); + } + } + + private boolean notMatchesCondition(int input) { + return input < MINIMUM_INPUT || notMultipleOfStandard(input); + } + + private boolean notMultipleOfStandard(int input) { + return input % STANDARD != 0; + } +} diff --git a/src/test/java/LottoServiceTest.java b/src/test/java/LottoControllerTest.java similarity index 68% rename from src/test/java/LottoServiceTest.java rename to src/test/java/LottoControllerTest.java index 4cffa6df..47614577 100644 --- a/src/test/java/LottoServiceTest.java +++ b/src/test/java/LottoControllerTest.java @@ -1,11 +1,13 @@ +import lotto.LottoController; +import lotto.LottoService; import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertThrows; -public class LottoServiceTest { - LottoService lottoService = new LottoService(); +public class LottoControllerTest { + LottoController lottoController = new LottoController(); @Test public void testInputValueUnder1000() throws Exception { @@ -13,8 +15,8 @@ public void testInputValueUnder1000() throws Exception { String input = "800"; // when, then - assertThrows(RuntimeException.class, () -> { - lottoService.validate(input); + assertThrows(IllegalArgumentException.class, () -> { + lottoController.validate(input); }); } @@ -24,7 +26,7 @@ public void testInputValueNotUnder1000() throws Exception { String input = "2000"; // when, // then - assertDoesNotThrow(() -> lottoService.validate(input)); + assertDoesNotThrow(() -> lottoController.validate(input)); } @Test @@ -33,7 +35,7 @@ public void testInputValueNotMultiple1000() throws Exception { String input = "1234"; // when, then - assertThatThrownBy(() -> lottoService.validate(input)) + assertThatThrownBy(() -> lottoController.validate(input)) .isInstanceOf(IllegalArgumentException.class); } @@ -45,7 +47,7 @@ public void testInputNotNumber() throws Exception { // when // then - assertThatThrownBy(() -> lottoService.validate(input)) + assertThatThrownBy(() -> lottoController.validate(input)) .isInstanceOf(RuntimeException.class); } } From 862c24312cea2377bddf455f233f2ad8e96d17e4 Mon Sep 17 00:00:00 2001 From: Jiwoo Kim Date: Sat, 3 Jul 2021 19:59:07 +0900 Subject: [PATCH 07/55] =?UTF-8?q?refactor:=20=ED=85=8C=EC=8A=A4=ED=8A=B8?= =?UTF-8?q?=20=EB=A9=94=EC=84=9C=EB=93=9C=20=EB=A6=AC=ED=8C=A9=ED=86=A0?= =?UTF-8?q?=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/lotto/PurchasePrice.java | 1 + src/test/java/LottoControllerTest.java | 39 ---------------- src/test/java/PurchasePriceTest.java | 64 ++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 39 deletions(-) create mode 100644 src/test/java/PurchasePriceTest.java diff --git a/src/main/java/lotto/PurchasePrice.java b/src/main/java/lotto/PurchasePrice.java index 5d69fc54..6288b40b 100644 --- a/src/main/java/lotto/PurchasePrice.java +++ b/src/main/java/lotto/PurchasePrice.java @@ -11,6 +11,7 @@ public class PurchasePrice { private final int purchasePrice; public PurchasePrice(String input) { + input = input.trim(); validate(input); this.purchasePrice = Integer.parseInt(input); } diff --git a/src/test/java/LottoControllerTest.java b/src/test/java/LottoControllerTest.java index 47614577..972dc549 100644 --- a/src/test/java/LottoControllerTest.java +++ b/src/test/java/LottoControllerTest.java @@ -9,45 +9,6 @@ public class LottoControllerTest { LottoController lottoController = new LottoController(); - @Test - public void testInputValueUnder1000() throws Exception { - // given - String input = "800"; - // when, then - assertThrows(IllegalArgumentException.class, () -> { - lottoController.validate(input); - }); - } - @Test - public void testInputValueNotUnder1000() throws Exception { - // given - String input = "2000"; - - // when, // then - assertDoesNotThrow(() -> lottoController.validate(input)); - } - - @Test - public void testInputValueNotMultiple1000() throws Exception { - // given - String input = "1234"; - - // when, then - assertThatThrownBy(() -> lottoController.validate(input)) - .isInstanceOf(IllegalArgumentException.class); - } - - @Test - public void testInputNotNumber() throws Exception { - // given - String input = "error"; - - // when - - // then - assertThatThrownBy(() -> lottoController.validate(input)) - .isInstanceOf(RuntimeException.class); - } } diff --git a/src/test/java/PurchasePriceTest.java b/src/test/java/PurchasePriceTest.java new file mode 100644 index 00000000..93f6182f --- /dev/null +++ b/src/test/java/PurchasePriceTest.java @@ -0,0 +1,64 @@ +import lotto.PurchasePrice; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertThrows; + +public class PurchasePriceTest { + @Test + public void testInputValueUnder1000() throws Exception { + // given + String input = "800"; + + + // when, then + assertThrows(IllegalArgumentException.class, () -> { + PurchasePrice purchasePrice = new PurchasePrice(input);; + }); + } + + @Test + public void testInputValueNotUnder1000() throws Exception { + // given + String input = "2000"; + + // when, // then + assertDoesNotThrow(() -> new PurchasePrice(input)); + } + + @Test + public void testInputValueNotMultiple1000() throws Exception { + // given + String input = "1234"; + + // when, then + assertThatThrownBy(() -> new PurchasePrice(input)) + .isInstanceOf(IllegalArgumentException.class); + } + + @Test + public void testInputNotNumber() throws Exception { + // given + String input = "error"; + + // when + + // then + assertThatThrownBy(() -> new PurchasePrice(input)) + .isInstanceOf(RuntimeException.class); + } + + // 왼쪽공백, 오른쪽공백, 양옆공백 + + @Test + public void testPriceInputWithLeftBlank() throws Exception { + // given + String input = " 1000"; + + // when + + // then + assertDoesNotThrow(() -> new PurchasePrice(input)); + } +} From 27424d27714db8af5f5d8641b98ee22b05b24fdd Mon Sep 17 00:00:00 2001 From: Jiwoo Kim Date: Sat, 3 Jul 2021 20:22:50 +0900 Subject: [PATCH 08/55] =?UTF-8?q?test:=20=EA=B3=B5=EB=B0=B1=20=EC=9E=85?= =?UTF-8?q?=EB=A0=A5=20=EC=B2=98=EB=A6=AC=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20?= =?UTF-8?q?=EB=A9=94=EC=84=9C=EB=93=9C=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- src/main/java/lotto/PurchasePrice.java | 3 ++ src/test/java/PurchasePriceTest.java | 41 ++++++++++++++++++++++++-- 3 files changed, 42 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index cfe3d8b8..97a08cb2 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ - [x] 1000의 배수 아닌 값 입력 시 처리 - [x] 숫자가 아닌 값 (문자열) - [ ] 정상값 처리 - - [ ] 공백 처리 + - [x] 공백 처리 - [ ] 구매 개수 구하기 - [ ] 랜덤 로또번호 구매 개수만큼 만들기 - [ ] 지난 주 당첨 번호 입력받기 diff --git a/src/main/java/lotto/PurchasePrice.java b/src/main/java/lotto/PurchasePrice.java index 6288b40b..fe1a765c 100644 --- a/src/main/java/lotto/PurchasePrice.java +++ b/src/main/java/lotto/PurchasePrice.java @@ -1,5 +1,7 @@ package lotto; +import lombok.Getter; + import java.util.regex.Pattern; public class PurchasePrice { @@ -8,6 +10,7 @@ public class PurchasePrice { private static final int MINIMUM_INPUT = 1000; private static final int STANDARD = 1000; + @Getter private final int purchasePrice; public PurchasePrice(String input) { diff --git a/src/test/java/PurchasePriceTest.java b/src/test/java/PurchasePriceTest.java index 93f6182f..237feff7 100644 --- a/src/test/java/PurchasePriceTest.java +++ b/src/test/java/PurchasePriceTest.java @@ -1,12 +1,18 @@ import lotto.PurchasePrice; +import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import java.util.Collections; + +import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertThrows; public class PurchasePriceTest { + @Test + @DisplayName("") public void testInputValueUnder1000() throws Exception { // given String input = "800"; @@ -19,6 +25,7 @@ public void testInputValueUnder1000() throws Exception { } @Test + @DisplayName("") public void testInputValueNotUnder1000() throws Exception { // given String input = "2000"; @@ -28,6 +35,7 @@ public void testInputValueNotUnder1000() throws Exception { } @Test + @DisplayName("") public void testInputValueNotMultiple1000() throws Exception { // given String input = "1234"; @@ -38,6 +46,7 @@ public void testInputValueNotMultiple1000() throws Exception { } @Test + @DisplayName("") public void testInputNotNumber() throws Exception { // given String input = "error"; @@ -49,16 +58,42 @@ public void testInputNotNumber() throws Exception { .isInstanceOf(RuntimeException.class); } - // 왼쪽공백, 오른쪽공백, 양옆공백 - @Test + @DisplayName("공백이 오른쪽에 있을 때 정상 처리된다") public void testPriceInputWithLeftBlank() throws Exception { // given String input = " 1000"; // when + PurchasePrice purchasePrice = new PurchasePrice(input); // then - assertDoesNotThrow(() -> new PurchasePrice(input)); + assertThat(purchasePrice.getPurchasePrice()).isEqualTo(1000); + } + + @Test + @DisplayName("공백이 오른쪽에 있을 때 정상 처리된다") + public void testPriceInputWithRightBlank() throws Exception { + // given + String input = "1000 "; + + // when + PurchasePrice purchasePrice = new PurchasePrice(input); + + // then + assertThat(purchasePrice.getPurchasePrice()).isEqualTo(1000); + } + + @Test + @DisplayName("공백이 양쪽에 있을 때 정상 처리된다") + public void testPriceInputWithLeftAndRightBlank() throws Exception { + // given + String input = " 1000 "; + + // when + PurchasePrice purchasePrice = new PurchasePrice(input); + + // then + assertThat(purchasePrice.getPurchasePrice()).isEqualTo(1000); } } From 39be046e0edb90c7cdfcfd2211e5dd1787a045f0 Mon Sep 17 00:00:00 2001 From: Jiwoo Kim Date: Sat, 3 Jul 2021 20:34:59 +0900 Subject: [PATCH 09/55] =?UTF-8?q?refactor:=20PurchasePrice=20->=20Purchase?= =?UTF-8?q?Count=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- src/main/java/lotto/LottoController.java | 2 +- src/main/java/lotto/LottoService.java | 6 ----- ...{PurchasePrice.java => PurchaseCount.java} | 8 +++--- ...ePriceTest.java => PurchaseCountTest.java} | 27 +++++++++---------- 5 files changed, 19 insertions(+), 26 deletions(-) rename src/main/java/lotto/{PurchasePrice.java => PurchaseCount.java} (84%) rename src/test/java/{PurchasePriceTest.java => PurchaseCountTest.java} (72%) diff --git a/README.md b/README.md index 97a08cb2..2b33f0ed 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ - [x] 숫자가 아닌 값 (문자열) - [ ] 정상값 처리 - [x] 공백 처리 -- [ ] 구매 개수 구하기 +- [x] 구매 개수 구하기 - [ ] 랜덤 로또번호 구매 개수만큼 만들기 - [ ] 지난 주 당첨 번호 입력받기 - [ ] 숫자 개수 6개 확인 diff --git a/src/main/java/lotto/LottoController.java b/src/main/java/lotto/LottoController.java index 9ae55b4a..cd9d54a2 100644 --- a/src/main/java/lotto/LottoController.java +++ b/src/main/java/lotto/LottoController.java @@ -2,7 +2,7 @@ public class LottoController { public void validate(String input) { - PurchasePrice purchasePrice = new PurchasePrice(input); + PurchaseCount purchaseCount = new PurchaseCount(input); } } diff --git a/src/main/java/lotto/LottoService.java b/src/main/java/lotto/LottoService.java index 3ec87674..9e7d1d7a 100644 --- a/src/main/java/lotto/LottoService.java +++ b/src/main/java/lotto/LottoService.java @@ -1,11 +1,5 @@ package lotto; -import lotto.PurchasePrice; - -import java.util.regex.Pattern; - - public class LottoService { - } diff --git a/src/main/java/lotto/PurchasePrice.java b/src/main/java/lotto/PurchaseCount.java similarity index 84% rename from src/main/java/lotto/PurchasePrice.java rename to src/main/java/lotto/PurchaseCount.java index fe1a765c..c0f66406 100644 --- a/src/main/java/lotto/PurchasePrice.java +++ b/src/main/java/lotto/PurchaseCount.java @@ -4,19 +4,19 @@ import java.util.regex.Pattern; -public class PurchasePrice { +public class PurchaseCount { private static final String NUMBER_REGEX = "^[0-9]*$"; private static final int MINIMUM_INPUT = 1000; private static final int STANDARD = 1000; @Getter - private final int purchasePrice; + private final int purchaseCount; - public PurchasePrice(String input) { + public PurchaseCount(String input) { input = input.trim(); validate(input); - this.purchasePrice = Integer.parseInt(input); + this.purchaseCount = Integer.parseInt(input)/STANDARD; } private void validate(String input) { diff --git a/src/test/java/PurchasePriceTest.java b/src/test/java/PurchaseCountTest.java similarity index 72% rename from src/test/java/PurchasePriceTest.java rename to src/test/java/PurchaseCountTest.java index 237feff7..9eb7db6e 100644 --- a/src/test/java/PurchasePriceTest.java +++ b/src/test/java/PurchaseCountTest.java @@ -1,15 +1,13 @@ -import lotto.PurchasePrice; +import lotto.PurchaseCount; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; -import java.util.Collections; - import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertThrows; -public class PurchasePriceTest { +public class PurchaseCountTest { @Test @DisplayName("") @@ -20,7 +18,7 @@ public void testInputValueUnder1000() throws Exception { // when, then assertThrows(IllegalArgumentException.class, () -> { - PurchasePrice purchasePrice = new PurchasePrice(input);; + PurchaseCount purchaseCount = new PurchaseCount(input);; }); } @@ -31,7 +29,7 @@ public void testInputValueNotUnder1000() throws Exception { String input = "2000"; // when, // then - assertDoesNotThrow(() -> new PurchasePrice(input)); + assertDoesNotThrow(() -> new PurchaseCount(input)); } @Test @@ -41,7 +39,7 @@ public void testInputValueNotMultiple1000() throws Exception { String input = "1234"; // when, then - assertThatThrownBy(() -> new PurchasePrice(input)) + assertThatThrownBy(() -> new PurchaseCount(input)) .isInstanceOf(IllegalArgumentException.class); } @@ -54,7 +52,7 @@ public void testInputNotNumber() throws Exception { // when // then - assertThatThrownBy(() -> new PurchasePrice(input)) + assertThatThrownBy(() -> new PurchaseCount(input)) .isInstanceOf(RuntimeException.class); } @@ -65,10 +63,10 @@ public void testPriceInputWithLeftBlank() throws Exception { String input = " 1000"; // when - PurchasePrice purchasePrice = new PurchasePrice(input); + PurchaseCount purchaseCount = new PurchaseCount(input); // then - assertThat(purchasePrice.getPurchasePrice()).isEqualTo(1000); + assertThat(purchaseCount.getPurchaseCount()).isEqualTo(1); } @Test @@ -78,10 +76,10 @@ public void testPriceInputWithRightBlank() throws Exception { String input = "1000 "; // when - PurchasePrice purchasePrice = new PurchasePrice(input); + PurchaseCount purchaseCount = new PurchaseCount(input); // then - assertThat(purchasePrice.getPurchasePrice()).isEqualTo(1000); + assertThat(purchaseCount.getPurchaseCount()).isEqualTo(1); } @Test @@ -91,9 +89,10 @@ public void testPriceInputWithLeftAndRightBlank() throws Exception { String input = " 1000 "; // when - PurchasePrice purchasePrice = new PurchasePrice(input); + PurchaseCount purchaseCount = new PurchaseCount(input); // then - assertThat(purchasePrice.getPurchasePrice()).isEqualTo(1000); + assertThat(purchaseCount.getPurchaseCount()).isEqualTo(1); } + } From c98315643742e3582f621a6cc461c044bda62572 Mon Sep 17 00:00:00 2001 From: Jiwoo Kim Date: Sat, 3 Jul 2021 21:03:00 +0900 Subject: [PATCH 10/55] =?UTF-8?q?feat:=20Lotto=20=ED=81=B4=EB=9E=98?= =?UTF-8?q?=EC=8A=A4,=20=ED=85=8C=EC=8A=A4=ED=8A=B8=ED=81=B4=EB=9E=98?= =?UTF-8?q?=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 --- README.md | 4 +++- src/main/java/lotto/Lotto.java | 24 ++++++++++++++++++++++++ src/test/java/LottoTest.java | 20 ++++++++++++++++++++ 3 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 src/main/java/lotto/Lotto.java create mode 100644 src/test/java/LottoTest.java diff --git a/README.md b/README.md index 2b33f0ed..41883973 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,9 @@ - [ ] 정상값 처리 - [x] 공백 처리 - [x] 구매 개수 구하기 -- [ ] 랜덤 로또번호 구매 개수만큼 만들기 +- [x] 랜덤 로또번호 구매 개수만큼 만들기 + - [ ] defaultNumberSet 1번만 생성되도록 변경 + - [ ] LottoTest 상수 리팩토링 - [ ] 지난 주 당첨 번호 입력받기 - [ ] 숫자 개수 6개 확인 - [ ] 숫자가 아닌 값 포함 diff --git a/src/main/java/lotto/Lotto.java b/src/main/java/lotto/Lotto.java new file mode 100644 index 00000000..28a3903c --- /dev/null +++ b/src/main/java/lotto/Lotto.java @@ -0,0 +1,24 @@ +package lotto; + +import lombok.Getter; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +@Getter +public class Lotto { + private static final int INDEX_LOWER_BOUND = 0; + private static final int INDEX_UPPER_BOUND = 6; + private final List lotto; + + public Lotto() { + ArrayList defaultNumberSet = new ArrayList<>(); + for (int i = 0; i < 44; i++) { + defaultNumberSet.add(i+1); + } + + Collections.shuffle(defaultNumberSet); + this.lotto = defaultNumberSet.subList(INDEX_LOWER_BOUND, INDEX_UPPER_BOUND); + } +} diff --git a/src/test/java/LottoTest.java b/src/test/java/LottoTest.java new file mode 100644 index 00000000..7b1c9c1c --- /dev/null +++ b/src/test/java/LottoTest.java @@ -0,0 +1,20 @@ +import lotto.Lotto; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class LottoTest { + Lotto lotto = new Lotto(); + + @Test + @DisplayName("로또 1개 생성") + public void testGenerateLotto() throws Exception { + // given + + // when + + // then + assertThat(lotto.getLotto().size()).isEqualTo(6); + } +} From f89c53e59e0e5ceb05b686c39279d269e3177e75 Mon Sep 17 00:00:00 2001 From: Jiwoo Kim Date: Sat, 3 Jul 2021 21:18:26 +0900 Subject: [PATCH 11/55] =?UTF-8?q?feat:=20=EB=A1=9C=EB=98=90=20=EA=B5=AC?= =?UTF-8?q?=EB=A7=A4=20=EA=B0=9C=EC=88=98=EB=A7=8C=ED=81=BC=20=EC=83=9D?= =?UTF-8?q?=EC=84=B1=ED=95=B4=EC=84=9C=20LottoSet=EC=97=90=20=EC=A0=80?= =?UTF-8?q?=EC=9E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + src/main/java/lotto/Lotto.java | 1 + src/main/java/lotto/LottoSet.java | 22 ++++++++++++++++++++++ src/test/java/LottoSetTest.java | 23 +++++++++++++++++++++++ 4 files changed, 47 insertions(+) create mode 100644 src/main/java/lotto/LottoSet.java create mode 100644 src/test/java/LottoSetTest.java diff --git a/README.md b/README.md index 41883973..795615e2 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,7 @@ - [x] 랜덤 로또번호 구매 개수만큼 만들기 - [ ] defaultNumberSet 1번만 생성되도록 변경 - [ ] LottoTest 상수 리팩토링 + - [ ] PurchaseCount의 1000 접근 - [ ] 지난 주 당첨 번호 입력받기 - [ ] 숫자 개수 6개 확인 - [ ] 숫자가 아닌 값 포함 diff --git a/src/main/java/lotto/Lotto.java b/src/main/java/lotto/Lotto.java index 28a3903c..c7d13281 100644 --- a/src/main/java/lotto/Lotto.java +++ b/src/main/java/lotto/Lotto.java @@ -10,6 +10,7 @@ public class Lotto { private static final int INDEX_LOWER_BOUND = 0; private static final int INDEX_UPPER_BOUND = 6; + private final List lotto; public Lotto() { diff --git a/src/main/java/lotto/LottoSet.java b/src/main/java/lotto/LottoSet.java new file mode 100644 index 00000000..318dba4a --- /dev/null +++ b/src/main/java/lotto/LottoSet.java @@ -0,0 +1,22 @@ +package lotto; + +import lombok.Getter; + +import java.util.HashSet; +import java.util.Set; + +public class LottoSet { + + @Getter + private final Set lottoSet = new HashSet<>(); + + public LottoSet(PurchaseCount purchaseCount) { + generateRandomLottoSetWithSize(purchaseCount.getPurchaseCount()); + } + + private void generateRandomLottoSetWithSize(int size) { + for (int i = 0; i < size; i++) { + lottoSet.add(new Lotto()); + } + } +} diff --git a/src/test/java/LottoSetTest.java b/src/test/java/LottoSetTest.java new file mode 100644 index 00000000..4c9f0ded --- /dev/null +++ b/src/test/java/LottoSetTest.java @@ -0,0 +1,23 @@ +import lotto.LottoSet; +import lotto.PurchaseCount; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class LottoSetTest { + + private static final int PURCHASE_COUNT_STANDARD = 1000; + + @Test + public void testGenerateLottoSet() throws Exception { + // given + int targetSize = 10; + PurchaseCount purchaseCount = new PurchaseCount(String.valueOf(targetSize * PURCHASE_COUNT_STANDARD)); + + // when + LottoSet lottoSet = new LottoSet(purchaseCount); + + // then + assertThat(lottoSet.getLottoSet().size()).isEqualTo(targetSize); + } +} From 27e69acef96afdfc7e9cd0fff1c5ca0aa5c92ca2 Mon Sep 17 00:00:00 2001 From: Jiwoo Kim Date: Sat, 3 Jul 2021 21:46:26 +0900 Subject: [PATCH 12/55] =?UTF-8?q?feat:=20=EB=8B=B9=EC=B2=A8=20=EB=B2=88?= =?UTF-8?q?=ED=98=B8=20=ED=81=B4=EB=9E=98=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 --- README.md | 1 + src/main/java/lotto/WinningNumbers.java | 22 ++++++++++++++++ src/test/java/WinningNumbersTest.java | 35 +++++++++++++++++++++++++ 3 files changed, 58 insertions(+) create mode 100644 src/main/java/lotto/WinningNumbers.java create mode 100644 src/test/java/WinningNumbersTest.java diff --git a/README.md b/README.md index 795615e2..a1fd64ed 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,7 @@ - [ ] LottoTest 상수 리팩토링 - [ ] PurchaseCount의 1000 접근 - [ ] 지난 주 당첨 번호 입력받기 + - [x] WinningNumbers 멤버변수 ArrayList 클래스 확인 - [ ] 숫자 개수 6개 확인 - [ ] 숫자가 아닌 값 포함 - [ ] 범위 (1~45) 확인 diff --git a/src/main/java/lotto/WinningNumbers.java b/src/main/java/lotto/WinningNumbers.java new file mode 100644 index 00000000..3722de0d --- /dev/null +++ b/src/main/java/lotto/WinningNumbers.java @@ -0,0 +1,22 @@ +package lotto; + +import lombok.Getter; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +@Getter +public class WinningNumbers { + private List winningNumbers; + + public WinningNumbers(String input) { + String[] chunks = input.split(","); + winningNumbers = Arrays.stream(chunks) + .map(String::trim) + .mapToInt(Integer::valueOf) + .boxed() + .collect(Collectors.toList()); + } +} diff --git a/src/test/java/WinningNumbersTest.java b/src/test/java/WinningNumbersTest.java new file mode 100644 index 00000000..dbbddb40 --- /dev/null +++ b/src/test/java/WinningNumbersTest.java @@ -0,0 +1,35 @@ +import lotto.WinningNumbers; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; + +import static org.assertj.core.api.Assertions.assertThat; + +public class WinningNumbersTest { + + @Test + @DisplayName("WinningNumbers 타입 확인") + public void test() throws Exception { + // given + String input = "1, 2, 3, 4, 5, 6"; + WinningNumbers winningNumbers = new WinningNumbers(input); + + // when + + // then + assertThat(winningNumbers.getWinningNumbers()).isExactlyInstanceOf(ArrayList.class); + } + + @Test + @DisplayName("숫자가 6개인지 확인") + public void testWinningNumberSize() throws Exception { + // given + String input = "1, 2, 3, 4, 5, 6"; + + // when + + // then +// WinningNumbers(input). + } +} From 9680ee3c3f503f545b9f8f11c44612ca4d996d50 Mon Sep 17 00:00:00 2001 From: chunghyeon-kim Date: Mon, 5 Jul 2021 19:17:08 +0900 Subject: [PATCH 13/55] =?UTF-8?q?test:=20=EC=88=AB=EC=9E=90=20=EA=B0=9C?= =?UTF-8?q?=EC=88=98=206=EA=B0=9C=20=ED=99=95=EC=9D=B8=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8=20=EB=A9=94=EC=84=9C=EB=93=9C=20=EC=9E=91?= =?UTF-8?q?=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- src/test/java/WinningNumbersTest.java | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index a1fd64ed..e17a1e43 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ - [ ] PurchaseCount의 1000 접근 - [ ] 지난 주 당첨 번호 입력받기 - [x] WinningNumbers 멤버변수 ArrayList 클래스 확인 - - [ ] 숫자 개수 6개 확인 + - [x] 숫자 개수 6개 확인 - [ ] 숫자가 아닌 값 포함 - [ ] 범위 (1~45) 확인 - [ ] 공백 처리 diff --git a/src/test/java/WinningNumbersTest.java b/src/test/java/WinningNumbersTest.java index dbbddb40..57258632 100644 --- a/src/test/java/WinningNumbersTest.java +++ b/src/test/java/WinningNumbersTest.java @@ -28,8 +28,10 @@ public void testWinningNumberSize() throws Exception { String input = "1, 2, 3, 4, 5, 6"; // when + WinningNumbers winningNumbers = new WinningNumbers(input); // then -// WinningNumbers(input). + assertThat(winningNumbers.getWinningNumbers().size()).isEqualTo(6); + } } From 8bd4e7febaa225f11efdf780ba2a1493ee9fee94 Mon Sep 17 00:00:00 2001 From: chunghyeon-kim Date: Mon, 5 Jul 2021 19:32:06 +0900 Subject: [PATCH 14/55] =?UTF-8?q?test:=20=EC=88=AB=EC=9E=90=EA=B0=80=20?= =?UTF-8?q?=EC=95=84=EB=8B=8C=20=EA=B0=92=20=EC=9E=85=EB=A0=A5=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8=20=EB=A9=94=EC=84=9C=EB=93=9C=20=EC=9E=91?= =?UTF-8?q?=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- src/test/java/WinningNumbersTest.java | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index e17a1e43..9e6e58e3 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ - [ ] 지난 주 당첨 번호 입력받기 - [x] WinningNumbers 멤버변수 ArrayList 클래스 확인 - [x] 숫자 개수 6개 확인 - - [ ] 숫자가 아닌 값 포함 + - [x] 숫자가 아닌 값 포함 - [ ] 범위 (1~45) 확인 - [ ] 공백 처리 - [ ] 보너스 볼 입력받기 diff --git a/src/test/java/WinningNumbersTest.java b/src/test/java/WinningNumbersTest.java index 57258632..a348406c 100644 --- a/src/test/java/WinningNumbersTest.java +++ b/src/test/java/WinningNumbersTest.java @@ -5,6 +5,9 @@ import java.util.ArrayList; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.assertj.core.api.InstanceOfAssertFactories.PATH; +import static org.junit.jupiter.api.Assertions.assertThrows; public class WinningNumbersTest { @@ -34,4 +37,17 @@ public void testWinningNumberSize() throws Exception { assertThat(winningNumbers.getWinningNumbers().size()).isEqualTo(6); } + + + @Test + @DisplayName("숫자가 아닌 값 포함") + public void testInputWithNonInteger() throws Exception { + // given + String input = "1, a, 3, 4, 5, 6"; + + // when, then + assertThrows(NumberFormatException.class, () -> { + new WinningNumbers(input); + }); + } } From 3ee2d118614f7d26715f6556d0e984ca7f40c77e Mon Sep 17 00:00:00 2001 From: chunghyeon-kim Date: Mon, 5 Jul 2021 20:45:31 +0900 Subject: [PATCH 15/55] =?UTF-8?q?test:=20=EB=A1=9C=EB=98=90=20=EB=B2=88?= =?UTF-8?q?=ED=98=B8=20=EC=9E=85=EB=A0=A5=EB=B2=94=EC=9C=84=201~45=20?= =?UTF-8?q?=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EB=A9=94=EC=84=9C=EB=93=9C=20?= =?UTF-8?q?=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- src/main/java/lotto/IntegerValidateUtils.java | 12 ++++ src/main/java/lotto/LottoNumber.java | 36 ++++++++++++ src/main/java/lotto/PurchaseCount.java | 6 +- src/main/java/lotto/WinningNumbers.java | 6 +- src/test/java/LottoNumberTest.java | 58 +++++++++++++++++++ src/test/java/WinningNumbersTest.java | 27 ++++++++- 7 files changed, 140 insertions(+), 7 deletions(-) create mode 100644 src/main/java/lotto/IntegerValidateUtils.java create mode 100644 src/main/java/lotto/LottoNumber.java create mode 100644 src/test/java/LottoNumberTest.java diff --git a/README.md b/README.md index 9e6e58e3..1be05497 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ - [x] WinningNumbers 멤버변수 ArrayList 클래스 확인 - [x] 숫자 개수 6개 확인 - [x] 숫자가 아닌 값 포함 - - [ ] 범위 (1~45) 확인 + - [x] 범위 (1~45) 확인 - [ ] 공백 처리 - [ ] 보너스 볼 입력받기 - [ ] 숫자가 아닌 값 diff --git a/src/main/java/lotto/IntegerValidateUtils.java b/src/main/java/lotto/IntegerValidateUtils.java new file mode 100644 index 00000000..121479a1 --- /dev/null +++ b/src/main/java/lotto/IntegerValidateUtils.java @@ -0,0 +1,12 @@ +package lotto; + +import java.util.regex.Pattern; + +public class IntegerValidateUtils { + private static final String NUMBER_REGEX = "^[0-9]*$"; + + public static boolean isNumber(String input) { + return Pattern.matches(NUMBER_REGEX, input); + } + +} diff --git a/src/main/java/lotto/LottoNumber.java b/src/main/java/lotto/LottoNumber.java new file mode 100644 index 00000000..5b49b7fc --- /dev/null +++ b/src/main/java/lotto/LottoNumber.java @@ -0,0 +1,36 @@ +package lotto; + +import lombok.Getter; + +import static lotto.IntegerValidateUtils.isNumber; + +public class LottoNumber { + + private static final int LOWER_BOUND = 1; + private static final int UPPER_BOUND = 45; + + @Getter + private final int lottoNumber; + + public LottoNumber(String lottoNumber) { + validate(lottoNumber); + + this.lottoNumber = Integer.parseInt(lottoNumber); + } + + private void validate(String input) { + if(!isNumber(input)) { + throw new IllegalArgumentException("숫자를 입력해주세요."); + } + + int lottoNumber = Integer.parseInt(input); + + if (isOutOfBound(lottoNumber)) { + throw new IllegalArgumentException("1~45 값을 입력해주세요"); + } + } + + private boolean isOutOfBound(int lottoNumber) { + return lottoNumber < LOWER_BOUND || lottoNumber > UPPER_BOUND; + } +} diff --git a/src/main/java/lotto/PurchaseCount.java b/src/main/java/lotto/PurchaseCount.java index c0f66406..903a309c 100644 --- a/src/main/java/lotto/PurchaseCount.java +++ b/src/main/java/lotto/PurchaseCount.java @@ -4,9 +4,11 @@ import java.util.regex.Pattern; +import static lotto.IntegerValidateUtils.isNumber; + public class PurchaseCount { - private static final String NUMBER_REGEX = "^[0-9]*$"; + private static final int MINIMUM_INPUT = 1000; private static final int STANDARD = 1000; @@ -20,7 +22,7 @@ public PurchaseCount(String input) { } private void validate(String input) { - if (!Pattern.matches(NUMBER_REGEX, input)) { + if (!isNumber(input)) { throw new IllegalArgumentException("잘못된 입력입니다"); } int inputConversion = Integer.parseInt(input); diff --git a/src/main/java/lotto/WinningNumbers.java b/src/main/java/lotto/WinningNumbers.java index 3722de0d..2817c42b 100644 --- a/src/main/java/lotto/WinningNumbers.java +++ b/src/main/java/lotto/WinningNumbers.java @@ -9,14 +9,14 @@ @Getter public class WinningNumbers { - private List winningNumbers; + + private final List winningNumbers; public WinningNumbers(String input) { String[] chunks = input.split(","); winningNumbers = Arrays.stream(chunks) .map(String::trim) - .mapToInt(Integer::valueOf) - .boxed() + .map(LottoNumber::new) .collect(Collectors.toList()); } } diff --git a/src/test/java/LottoNumberTest.java b/src/test/java/LottoNumberTest.java new file mode 100644 index 00000000..8088e2f5 --- /dev/null +++ b/src/test/java/LottoNumberTest.java @@ -0,0 +1,58 @@ +import lotto.LottoNumber; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; + +public class LottoNumberTest { + + @Test + @DisplayName("주어진 1~45 숫자로 LottoNumber 객체 생성") + void testGenerateLottoNumberWithInteger() { + // given + List inputNumbers = new ArrayList<>(); + inputNumbers.add("1"); + inputNumbers.add("2"); + inputNumbers.add("44"); + inputNumbers.add("45"); + List lottoNumbers = new ArrayList<>(); + + // when + for (String inputNumber : inputNumbers) { + lottoNumbers.add(new LottoNumber(inputNumber)); + } + + // then + for (int i = 0; i { + new LottoNumber(inputNumber); + }); + } + + @Test + @DisplayName("45보다 큰 숫자로 LottoNumber 생성 시 예외") + void testGenerateLottoNumberWithIntegerGreaterThanUpperBound() { + //given + String inputNumber = "46"; + + //when, then + assertThrows(IllegalArgumentException.class, ()-> { + new LottoNumber(inputNumber); + }); + } +} diff --git a/src/test/java/WinningNumbersTest.java b/src/test/java/WinningNumbersTest.java index a348406c..018048c5 100644 --- a/src/test/java/WinningNumbersTest.java +++ b/src/test/java/WinningNumbersTest.java @@ -46,8 +46,33 @@ public void testInputWithNonInteger() throws Exception { String input = "1, a, 3, 4, 5, 6"; // when, then - assertThrows(NumberFormatException.class, () -> { + assertThrows(IllegalArgumentException.class, () -> { new WinningNumbers(input); }); } + + @Test + @DisplayName("로또 번호 입력값이 45 초과면 예외 발생") + public void testInputGreaterThanUpperBoundary() throws Exception { + // given + String input = "1, 2, 3, 4, 5, 46"; + + // when, then + assertThrows(IllegalArgumentException.class, () -> { + new WinningNumbers(input); + }); + } + + @Test + @DisplayName("로또 번호 입력값이 1 미만이면 예외 발생") + public void testInputSmallerThanLowerBoundary() throws Exception { + // given + String input = "0, 2, 3, 4, 5, 6"; + + // when, then + assertThrows(IllegalArgumentException.class, () -> { + new WinningNumbers(input); + }); + } + } From 0f0a9aa75e930c27ac4769ea340ef5c2542a3a1c Mon Sep 17 00:00:00 2001 From: chunghyeon-kim Date: Mon, 5 Jul 2021 21:45:37 +0900 Subject: [PATCH 16/55] =?UTF-8?q?feat:=20=EB=8B=B9=EC=B2=A8=20=EC=A1=B0?= =?UTF-8?q?=EA=B1=B4=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EB=A9=94=EC=84=9C?= =?UTF-8?q?=EB=93=9C=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 15 ++--- src/main/java/lotto/PrizeCondition.java | 39 +++++++++++++ src/main/java/lotto/PrizeMoney.java | 16 ++++++ src/main/java/lotto/PurchaseCount.java | 3 +- src/test/java/PrizeConditionTest.java | 75 +++++++++++++++++++++++++ 5 files changed, 137 insertions(+), 11 deletions(-) create mode 100644 src/main/java/lotto/PrizeCondition.java create mode 100644 src/main/java/lotto/PrizeMoney.java create mode 100644 src/test/java/PrizeConditionTest.java diff --git a/README.md b/README.md index 1be05497..ce7d1bb8 100644 --- a/README.md +++ b/README.md @@ -13,19 +13,16 @@ - [ ] defaultNumberSet 1번만 생성되도록 변경 - [ ] LottoTest 상수 리팩토링 - [ ] PurchaseCount의 1000 접근 -- [ ] 지난 주 당첨 번호 입력받기 +- [x] 지난 주 당첨 번호 입력받기 - [x] WinningNumbers 멤버변수 ArrayList 클래스 확인 - [x] 숫자 개수 6개 확인 - [x] 숫자가 아닌 값 포함 - [x] 범위 (1~45) 확인 - - [ ] 공백 처리 -- [ ] 보너스 볼 입력받기 - - [ ] 숫자가 아닌 값 - - [ ] 범위 (1~45) 확인 - - [ ] 공백 처리 + - [x] 공백 처리 +- [x] 보너스 볼 입력받기 - [ ] 당첨 통계 - - [ ] 당첨 조건을 enum 처리 - - [ ] 일치 개수 찾기 - - [ ] 5개 일치 시 보너스 볼 일치 여부 확인 + - [x] 당첨 조건을 enum 처리 + - [x] 일치 개수 찾기 + - [x] 5개 일치 시 보너스 볼 일치 여부 확인 - [ ] 수익률 구하기 (당첨값의 합 / 구입금액) - [ ] 결과 출력 \ No newline at end of file diff --git a/src/main/java/lotto/PrizeCondition.java b/src/main/java/lotto/PrizeCondition.java new file mode 100644 index 00000000..048bd216 --- /dev/null +++ b/src/main/java/lotto/PrizeCondition.java @@ -0,0 +1,39 @@ +package lotto; + +public enum PrizeCondition { + + FIRST(6, false), + SECOND(6, true), + THIRD(5, false), + FOURTH(4, false), + FIFTH(3, false); + + private final int matchNumbersCount; + private final boolean isBonus; + + PrizeCondition(int matchNumbersCount, boolean isBonus) { + this.matchNumbersCount = matchNumbersCount; + this.isBonus = isBonus; + } + + + public static PrizeCondition findPrize(int matchNumbersCount, boolean isBonus) { + if (matchNumbersCount == FIFTH.matchNumbersCount) { + return FIFTH; + } + if (matchNumbersCount == FOURTH.matchNumbersCount) { + return FOURTH; + } + if (matchNumbersCount == THIRD.matchNumbersCount) { + return THIRD; + } + return dissolveFirstOrSecond(isBonus); + } + + private static PrizeCondition dissolveFirstOrSecond(boolean isBonus) { + if (isBonus) { + return PrizeCondition.SECOND; + } + return PrizeCondition.FIRST; + } +} diff --git a/src/main/java/lotto/PrizeMoney.java b/src/main/java/lotto/PrizeMoney.java new file mode 100644 index 00000000..c6582762 --- /dev/null +++ b/src/main/java/lotto/PrizeMoney.java @@ -0,0 +1,16 @@ +package lotto; + +public enum PrizeMoney { + + FIRST(2000000000), + SECOND(30000000), + THIRD(1500000), + FOURTH(50000), + FIFTH(5000); + + private final int value; + + PrizeMoney(int value) { + this.value = value; + } +} diff --git a/src/main/java/lotto/PurchaseCount.java b/src/main/java/lotto/PurchaseCount.java index 903a309c..ea421f25 100644 --- a/src/main/java/lotto/PurchaseCount.java +++ b/src/main/java/lotto/PurchaseCount.java @@ -7,8 +7,7 @@ import static lotto.IntegerValidateUtils.isNumber; public class PurchaseCount { - - + private static final int MINIMUM_INPUT = 1000; private static final int STANDARD = 1000; diff --git a/src/test/java/PrizeConditionTest.java b/src/test/java/PrizeConditionTest.java new file mode 100644 index 00000000..864c3c8a --- /dev/null +++ b/src/test/java/PrizeConditionTest.java @@ -0,0 +1,75 @@ +import lotto.PrizeCondition; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class PrizeConditionTest { + + @Test + @DisplayName("숫자가 6개 일치하고 보너스번호는 일치하지 않을 때 1등") + void firstPrize() { + //given + int matchNumbersCount = 6; + boolean isBonus = false; + + //when + PrizeCondition prizeCondition = PrizeCondition.findPrize(matchNumbersCount, isBonus); + + //then + assertThat(prizeCondition).isEqualTo(PrizeCondition.FIRST); + } + + @Test + @DisplayName("숫자가 6개 일치하고 보너스번호도 일치할 때 2등") + void secondPrize() { + //given + int matchNumbersCount = 6; + boolean isBonus = true; + + //when + PrizeCondition prizeCondition = PrizeCondition.findPrize(matchNumbersCount, isBonus); + + //then + assertThat(prizeCondition).isEqualTo(PrizeCondition.SECOND); + } + + @Test + @DisplayName("숫자가 5개 일치할 때 3등") + void thirdPrize() { + //given + int matchNumbersCount = 5; + + //when, then + assertThat(PrizeCondition.findPrize(matchNumbersCount, false)) + .isEqualTo(PrizeCondition.THIRD); + assertThat(PrizeCondition.findPrize(matchNumbersCount, true)) + .isEqualTo(PrizeCondition.THIRD); + } + + @Test + @DisplayName("숫자가 4개 일치할 때 4등") + void fourthPrize() { + //given + int matchNumbersCount = 4; + + //when, then + assertThat(PrizeCondition.findPrize(matchNumbersCount, false)) + .isEqualTo(PrizeCondition.FOURTH); + assertThat(PrizeCondition.findPrize(matchNumbersCount, true)) + .isEqualTo(PrizeCondition.FOURTH); + } + + @Test + @DisplayName("숫자가 3개 일치할 때 5등") + void fifthPrize() { + //given + int matchNumbersCount = 3; + + //when, then + assertThat(PrizeCondition.findPrize(matchNumbersCount, false)) + .isEqualTo(PrizeCondition.FIFTH); + assertThat(PrizeCondition.findPrize(matchNumbersCount, true)) + .isEqualTo(PrizeCondition.FIFTH); + } +} From 6cb70d421e1ebc884cf7c73964b4c2373efc9deb Mon Sep 17 00:00:00 2001 From: chunghyeon-kim Date: Mon, 5 Jul 2021 21:52:31 +0900 Subject: [PATCH 17/55] =?UTF-8?q?feat:=20=ED=86=B5=EA=B3=84=20=ED=81=B4?= =?UTF-8?q?=EB=9E=98=EC=8A=A4,=20=ED=86=B5=EA=B3=84=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=ED=81=B4=EB=9E=98=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/main/java/lotto/LottoStatistics.java | 5 +++++ src/main/java/lotto/PurchaseCount.java | 4 +--- src/test/java/LottoStatisticsTest.java | 3 +++ 3 files changed, 9 insertions(+), 3 deletions(-) create mode 100644 src/main/java/lotto/LottoStatistics.java create mode 100644 src/test/java/LottoStatisticsTest.java diff --git a/src/main/java/lotto/LottoStatistics.java b/src/main/java/lotto/LottoStatistics.java new file mode 100644 index 00000000..85092127 --- /dev/null +++ b/src/main/java/lotto/LottoStatistics.java @@ -0,0 +1,5 @@ +package lotto; + +public class LottoStatistics { + +} diff --git a/src/main/java/lotto/PurchaseCount.java b/src/main/java/lotto/PurchaseCount.java index ea421f25..821caaaa 100644 --- a/src/main/java/lotto/PurchaseCount.java +++ b/src/main/java/lotto/PurchaseCount.java @@ -2,12 +2,10 @@ import lombok.Getter; -import java.util.regex.Pattern; - import static lotto.IntegerValidateUtils.isNumber; public class PurchaseCount { - + private static final int MINIMUM_INPUT = 1000; private static final int STANDARD = 1000; diff --git a/src/test/java/LottoStatisticsTest.java b/src/test/java/LottoStatisticsTest.java new file mode 100644 index 00000000..f954c0fb --- /dev/null +++ b/src/test/java/LottoStatisticsTest.java @@ -0,0 +1,3 @@ +public class LottoStatisticsTest { + +} From dcf4842d43f933759cc5525f4c8c26db5035ae11 Mon Sep 17 00:00:00 2001 From: chunghyeon-kim Date: Tue, 6 Jul 2021 19:45:36 +0900 Subject: [PATCH 18/55] =?UTF-8?q?refactor:=20=ED=8C=A8=ED=82=A4=EC=A7=80?= =?UTF-8?q?=20=EA=B5=AC=EC=A1=B0=20=EB=B0=8F=20=ED=81=B4=EB=9E=98=EC=8A=A4?= =?UTF-8?q?=20=EC=9D=B4=EB=A6=84=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 5 +- src/main/java/lotto/Lotto.java | 25 --------- src/main/java/lotto/LottoController.java | 2 + src/main/java/lotto/LottoService.java | 5 -- src/main/java/lotto/LottoStatistics.java | 5 -- src/main/java/lotto/WinningNumbers.java | 22 -------- .../lotto/{ => constant}/PrizeCondition.java | 2 +- .../java/lotto/{ => constant}/PrizeMoney.java | 2 +- src/main/java/lotto/domain/Lotto.java | 15 +++++ .../java/lotto/{ => domain}/LottoNumber.java | 6 +- .../java/lotto/{ => domain}/LottoSet.java | 6 +- .../lotto/{ => domain}/PurchaseCount.java | 6 +- src/main/java/lotto/domain/RandomLotto.java | 34 +++++++++++ src/main/java/lotto/domain/WinningLotto.java | 17 ++++++ .../NumberValidateUtils.java} | 6 +- src/test/java/LottoControllerTest.java | 14 ----- src/test/java/LottoStatisticsTest.java | 3 - src/test/java/RandomLottoCountTest.java | 56 +++++++++++++++++++ .../constant}/PrizeConditionTest.java | 4 +- .../constant}/PurchaseCountTest.java | 4 +- .../{ => lotto/domain}/LottoNumberTest.java | 4 +- .../java/{ => lotto/domain}/LottoSetTest.java | 6 +- .../domain/RandomLottoTest.java} | 11 ++-- .../domain/WinningLottoTest.java} | 21 +++---- 24 files changed, 172 insertions(+), 109 deletions(-) delete mode 100644 src/main/java/lotto/Lotto.java delete mode 100644 src/main/java/lotto/LottoService.java delete mode 100644 src/main/java/lotto/LottoStatistics.java delete mode 100644 src/main/java/lotto/WinningNumbers.java rename src/main/java/lotto/{ => constant}/PrizeCondition.java (97%) rename src/main/java/lotto/{ => constant}/PrizeMoney.java (90%) create mode 100644 src/main/java/lotto/domain/Lotto.java rename src/main/java/lotto/{ => domain}/LottoNumber.java (87%) rename src/main/java/lotto/{ => domain}/LottoSet.java (73%) rename src/main/java/lotto/{ => domain}/PurchaseCount.java (89%) create mode 100644 src/main/java/lotto/domain/RandomLotto.java create mode 100644 src/main/java/lotto/domain/WinningLotto.java rename src/main/java/lotto/{IntegerValidateUtils.java => util/NumberValidateUtils.java} (59%) delete mode 100644 src/test/java/LottoControllerTest.java delete mode 100644 src/test/java/LottoStatisticsTest.java create mode 100644 src/test/java/RandomLottoCountTest.java rename src/test/java/{ => lotto/constant}/PrizeConditionTest.java (97%) rename src/test/java/{ => lotto/constant}/PurchaseCountTest.java (97%) rename src/test/java/{ => lotto/domain}/LottoNumberTest.java (96%) rename src/test/java/{ => lotto/domain}/LottoSetTest.java (86%) rename src/test/java/{LottoTest.java => lotto/domain/RandomLottoTest.java} (56%) rename src/test/java/{WinningNumbersTest.java => lotto/domain/WinningLottoTest.java} (74%) diff --git a/README.md b/README.md index ce7d1bb8..c4fe2bc4 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ - [x] 구매 개수 구하기 - [x] 랜덤 로또번호 구매 개수만큼 만들기 - [ ] defaultNumberSet 1번만 생성되도록 변경 - - [ ] LottoTest 상수 리팩토링 + - [ ] RandomLottoTest 상수 리팩토링 - [ ] PurchaseCount의 1000 접근 - [x] 지난 주 당첨 번호 입력받기 - [x] WinningNumbers 멤버변수 ArrayList 클래스 확인 @@ -24,5 +24,6 @@ - [x] 당첨 조건을 enum 처리 - [x] 일치 개수 찾기 - [x] 5개 일치 시 보너스 볼 일치 여부 확인 - - [ ] 수익률 구하기 (당첨값의 합 / 구입금액) + - [ ] 당첨값의 합 구하기 + - [ ] 수익률 구하기 - [ ] 결과 출력 \ No newline at end of file diff --git a/src/main/java/lotto/Lotto.java b/src/main/java/lotto/Lotto.java deleted file mode 100644 index c7d13281..00000000 --- a/src/main/java/lotto/Lotto.java +++ /dev/null @@ -1,25 +0,0 @@ -package lotto; - -import lombok.Getter; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -@Getter -public class Lotto { - private static final int INDEX_LOWER_BOUND = 0; - private static final int INDEX_UPPER_BOUND = 6; - - private final List lotto; - - public Lotto() { - ArrayList defaultNumberSet = new ArrayList<>(); - for (int i = 0; i < 44; i++) { - defaultNumberSet.add(i+1); - } - - Collections.shuffle(defaultNumberSet); - this.lotto = defaultNumberSet.subList(INDEX_LOWER_BOUND, INDEX_UPPER_BOUND); - } -} diff --git a/src/main/java/lotto/LottoController.java b/src/main/java/lotto/LottoController.java index cd9d54a2..e2a0bee6 100644 --- a/src/main/java/lotto/LottoController.java +++ b/src/main/java/lotto/LottoController.java @@ -1,5 +1,7 @@ package lotto; +import lotto.domain.PurchaseCount; + public class LottoController { public void validate(String input) { PurchaseCount purchaseCount = new PurchaseCount(input); diff --git a/src/main/java/lotto/LottoService.java b/src/main/java/lotto/LottoService.java deleted file mode 100644 index 9e7d1d7a..00000000 --- a/src/main/java/lotto/LottoService.java +++ /dev/null @@ -1,5 +0,0 @@ -package lotto; - -public class LottoService { - -} diff --git a/src/main/java/lotto/LottoStatistics.java b/src/main/java/lotto/LottoStatistics.java deleted file mode 100644 index 85092127..00000000 --- a/src/main/java/lotto/LottoStatistics.java +++ /dev/null @@ -1,5 +0,0 @@ -package lotto; - -public class LottoStatistics { - -} diff --git a/src/main/java/lotto/WinningNumbers.java b/src/main/java/lotto/WinningNumbers.java deleted file mode 100644 index 2817c42b..00000000 --- a/src/main/java/lotto/WinningNumbers.java +++ /dev/null @@ -1,22 +0,0 @@ -package lotto; - -import lombok.Getter; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import java.util.stream.Collectors; - -@Getter -public class WinningNumbers { - - private final List winningNumbers; - - public WinningNumbers(String input) { - String[] chunks = input.split(","); - winningNumbers = Arrays.stream(chunks) - .map(String::trim) - .map(LottoNumber::new) - .collect(Collectors.toList()); - } -} diff --git a/src/main/java/lotto/PrizeCondition.java b/src/main/java/lotto/constant/PrizeCondition.java similarity index 97% rename from src/main/java/lotto/PrizeCondition.java rename to src/main/java/lotto/constant/PrizeCondition.java index 048bd216..2c5a0041 100644 --- a/src/main/java/lotto/PrizeCondition.java +++ b/src/main/java/lotto/constant/PrizeCondition.java @@ -1,4 +1,4 @@ -package lotto; +package lotto.constant; public enum PrizeCondition { diff --git a/src/main/java/lotto/PrizeMoney.java b/src/main/java/lotto/constant/PrizeMoney.java similarity index 90% rename from src/main/java/lotto/PrizeMoney.java rename to src/main/java/lotto/constant/PrizeMoney.java index c6582762..01f34840 100644 --- a/src/main/java/lotto/PrizeMoney.java +++ b/src/main/java/lotto/constant/PrizeMoney.java @@ -1,4 +1,4 @@ -package lotto; +package lotto.constant; public enum PrizeMoney { diff --git a/src/main/java/lotto/domain/Lotto.java b/src/main/java/lotto/domain/Lotto.java new file mode 100644 index 00000000..9350cb09 --- /dev/null +++ b/src/main/java/lotto/domain/Lotto.java @@ -0,0 +1,15 @@ +package lotto.domain; + +import lombok.Getter; + +import java.util.List; + +public class Lotto { + + @Getter + protected final List lottoNumbers; + + public Lotto(List lottoNumbers) { + this.lottoNumbers = lottoNumbers; + } +} diff --git a/src/main/java/lotto/LottoNumber.java b/src/main/java/lotto/domain/LottoNumber.java similarity index 87% rename from src/main/java/lotto/LottoNumber.java rename to src/main/java/lotto/domain/LottoNumber.java index 5b49b7fc..7799c59a 100644 --- a/src/main/java/lotto/LottoNumber.java +++ b/src/main/java/lotto/domain/LottoNumber.java @@ -1,8 +1,8 @@ -package lotto; +package lotto.domain; import lombok.Getter; -import static lotto.IntegerValidateUtils.isNumber; +import static lotto.util.NumberValidateUtils.isInteger; public class LottoNumber { @@ -19,7 +19,7 @@ public LottoNumber(String lottoNumber) { } private void validate(String input) { - if(!isNumber(input)) { + if(!isInteger(input)) { throw new IllegalArgumentException("숫자를 입력해주세요."); } diff --git a/src/main/java/lotto/LottoSet.java b/src/main/java/lotto/domain/LottoSet.java similarity index 73% rename from src/main/java/lotto/LottoSet.java rename to src/main/java/lotto/domain/LottoSet.java index 318dba4a..03005cef 100644 --- a/src/main/java/lotto/LottoSet.java +++ b/src/main/java/lotto/domain/LottoSet.java @@ -1,4 +1,4 @@ -package lotto; +package lotto.domain; import lombok.Getter; @@ -8,7 +8,7 @@ public class LottoSet { @Getter - private final Set lottoSet = new HashSet<>(); + private final Set lottoSet = new HashSet<>(); public LottoSet(PurchaseCount purchaseCount) { generateRandomLottoSetWithSize(purchaseCount.getPurchaseCount()); @@ -16,7 +16,7 @@ public LottoSet(PurchaseCount purchaseCount) { private void generateRandomLottoSetWithSize(int size) { for (int i = 0; i < size; i++) { - lottoSet.add(new Lotto()); + lottoSet.add(new RandomLotto()); } } } diff --git a/src/main/java/lotto/PurchaseCount.java b/src/main/java/lotto/domain/PurchaseCount.java similarity index 89% rename from src/main/java/lotto/PurchaseCount.java rename to src/main/java/lotto/domain/PurchaseCount.java index 821caaaa..c90e8de3 100644 --- a/src/main/java/lotto/PurchaseCount.java +++ b/src/main/java/lotto/domain/PurchaseCount.java @@ -1,8 +1,8 @@ -package lotto; +package lotto.domain; import lombok.Getter; -import static lotto.IntegerValidateUtils.isNumber; +import static lotto.util.NumberValidateUtils.isInteger; public class PurchaseCount { @@ -19,7 +19,7 @@ public PurchaseCount(String input) { } private void validate(String input) { - if (!isNumber(input)) { + if (!isInteger(input)) { throw new IllegalArgumentException("잘못된 입력입니다"); } int inputConversion = Integer.parseInt(input); diff --git a/src/main/java/lotto/domain/RandomLotto.java b/src/main/java/lotto/domain/RandomLotto.java new file mode 100644 index 00000000..6af69984 --- /dev/null +++ b/src/main/java/lotto/domain/RandomLotto.java @@ -0,0 +1,34 @@ +package lotto.domain; + +import lombok.Getter; +import lotto.domain.Lotto; +import lotto.domain.LottoNumber; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; + +@Getter +public class RandomLotto extends Lotto { + private static final int INDEX_LOWER_BOUND = 0; + private static final int INDEX_UPPER_BOUND = 6; + + public RandomLotto() { + super(generateRandomLottoNumbers()); + } + + private static List generateRandomLottoNumbers() { + ArrayList defaultNumberSet = new ArrayList<>(); + for (int i = 0; i < 44; i++) { + defaultNumberSet.add(i+1); + } + + Collections.shuffle(defaultNumberSet); + return defaultNumberSet.subList(INDEX_LOWER_BOUND, INDEX_UPPER_BOUND) + .stream() + .map(String::valueOf) + .map(LottoNumber::new) + .collect(Collectors.toList()); + } +} diff --git a/src/main/java/lotto/domain/WinningLotto.java b/src/main/java/lotto/domain/WinningLotto.java new file mode 100644 index 00000000..3a0c8931 --- /dev/null +++ b/src/main/java/lotto/domain/WinningLotto.java @@ -0,0 +1,17 @@ +package lotto.domain; + +import lotto.domain.Lotto; +import lotto.domain.LottoNumber; + +import java.util.Arrays; +import java.util.stream.Collectors; + +public class WinningLotto extends Lotto { + + public WinningLotto(String input) { + super(Arrays.stream(input.split(",")) + .map(String::trim) + .map(LottoNumber::new) + .collect(Collectors.toList())); + } +} diff --git a/src/main/java/lotto/IntegerValidateUtils.java b/src/main/java/lotto/util/NumberValidateUtils.java similarity index 59% rename from src/main/java/lotto/IntegerValidateUtils.java rename to src/main/java/lotto/util/NumberValidateUtils.java index 121479a1..08e68532 100644 --- a/src/main/java/lotto/IntegerValidateUtils.java +++ b/src/main/java/lotto/util/NumberValidateUtils.java @@ -1,11 +1,11 @@ -package lotto; +package lotto.util; import java.util.regex.Pattern; -public class IntegerValidateUtils { +public class NumberValidateUtils { private static final String NUMBER_REGEX = "^[0-9]*$"; - public static boolean isNumber(String input) { + public static boolean isInteger(String input) { return Pattern.matches(NUMBER_REGEX, input); } diff --git a/src/test/java/LottoControllerTest.java b/src/test/java/LottoControllerTest.java deleted file mode 100644 index 972dc549..00000000 --- a/src/test/java/LottoControllerTest.java +++ /dev/null @@ -1,14 +0,0 @@ -import lotto.LottoController; -import lotto.LottoService; -import org.junit.jupiter.api.Test; - -import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; -import static org.junit.jupiter.api.Assertions.assertThrows; - -public class LottoControllerTest { - LottoController lottoController = new LottoController(); - - - -} diff --git a/src/test/java/LottoStatisticsTest.java b/src/test/java/LottoStatisticsTest.java deleted file mode 100644 index f954c0fb..00000000 --- a/src/test/java/LottoStatisticsTest.java +++ /dev/null @@ -1,3 +0,0 @@ -public class LottoStatisticsTest { - -} diff --git a/src/test/java/RandomLottoCountTest.java b/src/test/java/RandomLottoCountTest.java new file mode 100644 index 00000000..30977333 --- /dev/null +++ b/src/test/java/RandomLottoCountTest.java @@ -0,0 +1,56 @@ +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +public class RandomLottoCountTest { + + @Test + @DisplayName("1등 당첨 개수를 집계한다") + void countFirstPrize() { + //given + + //when + + //then + } + + @Test + @DisplayName("2등 당첨 개수를 집계한다") + void countSecondPrize() { + //given + + //when + + //then + } + + @Test + @DisplayName("3등 당첨 개수를 집계한다") + void countThirdPrize() { + //given + + //when + + //then + } + + @Test + @DisplayName("4등 당첨 개수를 집계한다") + void countFourthPrize() { + //given + + //when + + //then + } + + @Test + @DisplayName("5등 당첨 개수를 집계한다") + void countFifthPrize() { + //given + + //when + + //then + } + +} diff --git a/src/test/java/PrizeConditionTest.java b/src/test/java/lotto/constant/PrizeConditionTest.java similarity index 97% rename from src/test/java/PrizeConditionTest.java rename to src/test/java/lotto/constant/PrizeConditionTest.java index 864c3c8a..cdf6e017 100644 --- a/src/test/java/PrizeConditionTest.java +++ b/src/test/java/lotto/constant/PrizeConditionTest.java @@ -1,4 +1,6 @@ -import lotto.PrizeCondition; +package lotto.constant; + +import lotto.constant.PrizeCondition; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; diff --git a/src/test/java/PurchaseCountTest.java b/src/test/java/lotto/constant/PurchaseCountTest.java similarity index 97% rename from src/test/java/PurchaseCountTest.java rename to src/test/java/lotto/constant/PurchaseCountTest.java index 9eb7db6e..3fa290d0 100644 --- a/src/test/java/PurchaseCountTest.java +++ b/src/test/java/lotto/constant/PurchaseCountTest.java @@ -1,4 +1,6 @@ -import lotto.PurchaseCount; +package lotto.constant; + +import lotto.domain.PurchaseCount; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; diff --git a/src/test/java/LottoNumberTest.java b/src/test/java/lotto/domain/LottoNumberTest.java similarity index 96% rename from src/test/java/LottoNumberTest.java rename to src/test/java/lotto/domain/LottoNumberTest.java index 8088e2f5..5b3e599a 100644 --- a/src/test/java/LottoNumberTest.java +++ b/src/test/java/lotto/domain/LottoNumberTest.java @@ -1,4 +1,6 @@ -import lotto.LottoNumber; +package lotto.domain; + +import lotto.domain.LottoNumber; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; diff --git a/src/test/java/LottoSetTest.java b/src/test/java/lotto/domain/LottoSetTest.java similarity index 86% rename from src/test/java/LottoSetTest.java rename to src/test/java/lotto/domain/LottoSetTest.java index 4c9f0ded..6c9f539a 100644 --- a/src/test/java/LottoSetTest.java +++ b/src/test/java/lotto/domain/LottoSetTest.java @@ -1,5 +1,7 @@ -import lotto.LottoSet; -import lotto.PurchaseCount; +package lotto.domain; + +import lotto.domain.LottoSet; +import lotto.domain.PurchaseCount; import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; diff --git a/src/test/java/LottoTest.java b/src/test/java/lotto/domain/RandomLottoTest.java similarity index 56% rename from src/test/java/LottoTest.java rename to src/test/java/lotto/domain/RandomLottoTest.java index 7b1c9c1c..57eda6ef 100644 --- a/src/test/java/LottoTest.java +++ b/src/test/java/lotto/domain/RandomLottoTest.java @@ -1,20 +1,23 @@ -import lotto.Lotto; +package lotto.domain; + +import lotto.domain.RandomLotto; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; -public class LottoTest { - Lotto lotto = new Lotto(); +public class RandomLottoTest { @Test @DisplayName("로또 1개 생성") public void testGenerateLotto() throws Exception { // given + RandomLotto randomLotto = new RandomLotto(); // when + int size = randomLotto.getLottoNumbers().size(); // then - assertThat(lotto.getLotto().size()).isEqualTo(6); + assertThat(size).isEqualTo(6); } } diff --git a/src/test/java/WinningNumbersTest.java b/src/test/java/lotto/domain/WinningLottoTest.java similarity index 74% rename from src/test/java/WinningNumbersTest.java rename to src/test/java/lotto/domain/WinningLottoTest.java index 018048c5..6b6ed4cd 100644 --- a/src/test/java/WinningNumbersTest.java +++ b/src/test/java/lotto/domain/WinningLottoTest.java @@ -1,4 +1,6 @@ -import lotto.WinningNumbers; +package lotto.domain; + +import lotto.domain.WinningLotto; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -6,22 +8,21 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.assertj.core.api.InstanceOfAssertFactories.PATH; import static org.junit.jupiter.api.Assertions.assertThrows; -public class WinningNumbersTest { +public class WinningLottoTest { @Test @DisplayName("WinningNumbers 타입 확인") public void test() throws Exception { // given String input = "1, 2, 3, 4, 5, 6"; - WinningNumbers winningNumbers = new WinningNumbers(input); + WinningLotto winningLotto = new WinningLotto(input); // when // then - assertThat(winningNumbers.getWinningNumbers()).isExactlyInstanceOf(ArrayList.class); + assertThat(winningLotto.getLottoNumbers()).isExactlyInstanceOf(ArrayList.class); } @Test @@ -31,10 +32,10 @@ public void testWinningNumberSize() throws Exception { String input = "1, 2, 3, 4, 5, 6"; // when - WinningNumbers winningNumbers = new WinningNumbers(input); + WinningLotto winningLotto = new WinningLotto(input); // then - assertThat(winningNumbers.getWinningNumbers().size()).isEqualTo(6); + assertThat(winningLotto.getLottoNumbers().size()).isEqualTo(6); } @@ -47,7 +48,7 @@ public void testInputWithNonInteger() throws Exception { // when, then assertThrows(IllegalArgumentException.class, () -> { - new WinningNumbers(input); + new WinningLotto(input); }); } @@ -59,7 +60,7 @@ public void testInputGreaterThanUpperBoundary() throws Exception { // when, then assertThrows(IllegalArgumentException.class, () -> { - new WinningNumbers(input); + new WinningLotto(input); }); } @@ -71,7 +72,7 @@ public void testInputSmallerThanLowerBoundary() throws Exception { // when, then assertThrows(IllegalArgumentException.class, () -> { - new WinningNumbers(input); + new WinningLotto(input); }); } From c5128693bda6ac34bc6dce4bbc493d9aced9fb67 Mon Sep 17 00:00:00 2001 From: chunghyeon-kim Date: Tue, 6 Jul 2021 20:04:41 +0900 Subject: [PATCH 19/55] =?UTF-8?q?test:=20=EB=B3=B4=EB=84=88=EC=8A=A4=20?= =?UTF-8?q?=EB=B2=88=ED=98=B8=20=ED=99=95=EC=9D=B8=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../{LottoSet.java => RandomLottoSet.java} | 4 +- src/main/java/lotto/domain/WinningLotto.java | 9 ++- src/test/java/RandomLottoCountTest.java | 56 ------------------- ...toSetTest.java => RandomLottoSetTest.java} | 8 +-- .../java/lotto/domain/WinningLottoTest.java | 29 ++++++++-- 5 files changed, 36 insertions(+), 70 deletions(-) rename src/main/java/lotto/domain/{LottoSet.java => RandomLottoSet.java} (82%) delete mode 100644 src/test/java/RandomLottoCountTest.java rename src/test/java/lotto/domain/{LottoSetTest.java => RandomLottoSetTest.java} (66%) diff --git a/src/main/java/lotto/domain/LottoSet.java b/src/main/java/lotto/domain/RandomLottoSet.java similarity index 82% rename from src/main/java/lotto/domain/LottoSet.java rename to src/main/java/lotto/domain/RandomLottoSet.java index 03005cef..7592b345 100644 --- a/src/main/java/lotto/domain/LottoSet.java +++ b/src/main/java/lotto/domain/RandomLottoSet.java @@ -5,12 +5,12 @@ import java.util.HashSet; import java.util.Set; -public class LottoSet { +public class RandomLottoSet { @Getter private final Set lottoSet = new HashSet<>(); - public LottoSet(PurchaseCount purchaseCount) { + public RandomLottoSet(PurchaseCount purchaseCount) { generateRandomLottoSetWithSize(purchaseCount.getPurchaseCount()); } diff --git a/src/main/java/lotto/domain/WinningLotto.java b/src/main/java/lotto/domain/WinningLotto.java index 3a0c8931..ebca5522 100644 --- a/src/main/java/lotto/domain/WinningLotto.java +++ b/src/main/java/lotto/domain/WinningLotto.java @@ -1,5 +1,6 @@ package lotto.domain; +import lombok.Getter; import lotto.domain.Lotto; import lotto.domain.LottoNumber; @@ -8,10 +9,14 @@ public class WinningLotto extends Lotto { - public WinningLotto(String input) { - super(Arrays.stream(input.split(",")) + @Getter + private LottoNumber bonusNumber; + + public WinningLotto(String winningNumberInput, String bonusNumberInput) { + super(Arrays.stream(winningNumberInput.split(",")) .map(String::trim) .map(LottoNumber::new) .collect(Collectors.toList())); + this.bonusNumber = new LottoNumber(bonusNumberInput); } } diff --git a/src/test/java/RandomLottoCountTest.java b/src/test/java/RandomLottoCountTest.java deleted file mode 100644 index 30977333..00000000 --- a/src/test/java/RandomLottoCountTest.java +++ /dev/null @@ -1,56 +0,0 @@ -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; - -public class RandomLottoCountTest { - - @Test - @DisplayName("1등 당첨 개수를 집계한다") - void countFirstPrize() { - //given - - //when - - //then - } - - @Test - @DisplayName("2등 당첨 개수를 집계한다") - void countSecondPrize() { - //given - - //when - - //then - } - - @Test - @DisplayName("3등 당첨 개수를 집계한다") - void countThirdPrize() { - //given - - //when - - //then - } - - @Test - @DisplayName("4등 당첨 개수를 집계한다") - void countFourthPrize() { - //given - - //when - - //then - } - - @Test - @DisplayName("5등 당첨 개수를 집계한다") - void countFifthPrize() { - //given - - //when - - //then - } - -} diff --git a/src/test/java/lotto/domain/LottoSetTest.java b/src/test/java/lotto/domain/RandomLottoSetTest.java similarity index 66% rename from src/test/java/lotto/domain/LottoSetTest.java rename to src/test/java/lotto/domain/RandomLottoSetTest.java index 6c9f539a..5d293f34 100644 --- a/src/test/java/lotto/domain/LottoSetTest.java +++ b/src/test/java/lotto/domain/RandomLottoSetTest.java @@ -1,12 +1,10 @@ package lotto.domain; -import lotto.domain.LottoSet; -import lotto.domain.PurchaseCount; import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; -public class LottoSetTest { +public class RandomLottoSetTest { private static final int PURCHASE_COUNT_STANDARD = 1000; @@ -17,9 +15,9 @@ public void testGenerateLottoSet() throws Exception { PurchaseCount purchaseCount = new PurchaseCount(String.valueOf(targetSize * PURCHASE_COUNT_STANDARD)); // when - LottoSet lottoSet = new LottoSet(purchaseCount); + RandomLottoSet randomLottoSet = new RandomLottoSet(purchaseCount); // then - assertThat(lottoSet.getLottoSet().size()).isEqualTo(targetSize); + assertThat(randomLottoSet.getLottoSet().size()).isEqualTo(targetSize); } } diff --git a/src/test/java/lotto/domain/WinningLottoTest.java b/src/test/java/lotto/domain/WinningLottoTest.java index 6b6ed4cd..4ef1b53a 100644 --- a/src/test/java/lotto/domain/WinningLottoTest.java +++ b/src/test/java/lotto/domain/WinningLottoTest.java @@ -17,7 +17,8 @@ public class WinningLottoTest { public void test() throws Exception { // given String input = "1, 2, 3, 4, 5, 6"; - WinningLotto winningLotto = new WinningLotto(input); + String bonus = "10"; + WinningLotto winningLotto = new WinningLotto(input, bonus); // when @@ -30,9 +31,10 @@ public void test() throws Exception { public void testWinningNumberSize() throws Exception { // given String input = "1, 2, 3, 4, 5, 6"; + String bonus = "10"; // when - WinningLotto winningLotto = new WinningLotto(input); + WinningLotto winningLotto = new WinningLotto(input, bonus); // then assertThat(winningLotto.getLottoNumbers().size()).isEqualTo(6); @@ -45,10 +47,11 @@ public void testWinningNumberSize() throws Exception { public void testInputWithNonInteger() throws Exception { // given String input = "1, a, 3, 4, 5, 6"; + String bonus = "10"; // when, then assertThrows(IllegalArgumentException.class, () -> { - new WinningLotto(input); + new WinningLotto(input, bonus); }); } @@ -57,10 +60,11 @@ public void testInputWithNonInteger() throws Exception { public void testInputGreaterThanUpperBoundary() throws Exception { // given String input = "1, 2, 3, 4, 5, 46"; + String bonus = "10"; // when, then assertThrows(IllegalArgumentException.class, () -> { - new WinningLotto(input); + new WinningLotto(input, bonus); }); } @@ -69,11 +73,26 @@ public void testInputGreaterThanUpperBoundary() throws Exception { public void testInputSmallerThanLowerBoundary() throws Exception { // given String input = "0, 2, 3, 4, 5, 6"; + String bonus = "10"; // when, then assertThrows(IllegalArgumentException.class, () -> { - new WinningLotto(input); + new WinningLotto(input, bonus); }); } + @Test + @DisplayName("보너스 번호 입력값을 확인") + void testBonusInput() { + //given + String input = "1, 2, 3, 4, 5, 6"; + String bonus = "10"; + + // when + WinningLotto winningLotto = new WinningLotto(input, bonus); + + // then + assertThat(winningLotto.getBonusNumber().getLottoNumber()).isEqualTo(10); + } + } From a6ef7deae292874a069db54a457f8f59fb949fea Mon Sep 17 00:00:00 2001 From: chunghyeon-kim Date: Tue, 6 Jul 2021 20:57:23 +0900 Subject: [PATCH 20/55] =?UTF-8?q?feat:=20=EB=A1=9C=EB=98=90=201=20?= =?UTF-8?q?=EA=B2=8C=EC=9E=84=EC=9D=98=20=EB=93=B1=EC=88=98=20=EA=B3=84?= =?UTF-8?q?=EC=82=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/lotto/constant/PrizeCondition.java | 12 +-- src/main/java/lotto/domain/LottoNumber.java | 19 ++++ src/main/java/lotto/domain/WinningLotto.java | 36 +++++++ .../java/lotto/domain/WinningLottoTest.java | 100 +++++++++++++----- 4 files changed, 136 insertions(+), 31 deletions(-) diff --git a/src/main/java/lotto/constant/PrizeCondition.java b/src/main/java/lotto/constant/PrizeCondition.java index 2c5a0041..7148d19d 100644 --- a/src/main/java/lotto/constant/PrizeCondition.java +++ b/src/main/java/lotto/constant/PrizeCondition.java @@ -3,7 +3,7 @@ public enum PrizeCondition { FIRST(6, false), - SECOND(6, true), + SECOND(5, true), THIRD(5, false), FOURTH(4, false), FIFTH(3, false); @@ -24,16 +24,16 @@ public static PrizeCondition findPrize(int matchNumbersCount, boolean isBonus) { if (matchNumbersCount == FOURTH.matchNumbersCount) { return FOURTH; } - if (matchNumbersCount == THIRD.matchNumbersCount) { - return THIRD; + if (matchNumbersCount == FIRST.matchNumbersCount) { + return FIRST; } - return dissolveFirstOrSecond(isBonus); + return dissolveSecondOrThird(isBonus); } - private static PrizeCondition dissolveFirstOrSecond(boolean isBonus) { + private static PrizeCondition dissolveSecondOrThird(boolean isBonus) { if (isBonus) { return PrizeCondition.SECOND; } - return PrizeCondition.FIRST; + return PrizeCondition.THIRD; } } diff --git a/src/main/java/lotto/domain/LottoNumber.java b/src/main/java/lotto/domain/LottoNumber.java index 7799c59a..3f0abc65 100644 --- a/src/main/java/lotto/domain/LottoNumber.java +++ b/src/main/java/lotto/domain/LottoNumber.java @@ -2,6 +2,8 @@ import lombok.Getter; +import java.util.Objects; + import static lotto.util.NumberValidateUtils.isInteger; public class LottoNumber { @@ -33,4 +35,21 @@ private void validate(String input) { private boolean isOutOfBound(int lottoNumber) { return lottoNumber < LOWER_BOUND || lottoNumber > UPPER_BOUND; } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + LottoNumber that = (LottoNumber) o; + return lottoNumber == that.lottoNumber; + } + + @Override + public int hashCode() { + return Objects.hash(lottoNumber); + } } diff --git a/src/main/java/lotto/domain/WinningLotto.java b/src/main/java/lotto/domain/WinningLotto.java index ebca5522..f65ed4f4 100644 --- a/src/main/java/lotto/domain/WinningLotto.java +++ b/src/main/java/lotto/domain/WinningLotto.java @@ -1,10 +1,14 @@ package lotto.domain; import lombok.Getter; +import lotto.constant.PrizeCondition; import lotto.domain.Lotto; import lotto.domain.LottoNumber; import java.util.Arrays; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; import java.util.stream.Collectors; public class WinningLotto extends Lotto { @@ -19,4 +23,36 @@ public WinningLotto(String winningNumberInput, String bonusNumberInput) { .collect(Collectors.toList())); this.bonusNumber = new LottoNumber(bonusNumberInput); } + + public PrizeCondition findPrize(Lotto targetLotto) { + return PrizeCondition.findPrize(getMatchNumbersCount(targetLotto), isBonusMatch(targetLotto)); + } + + private boolean isBonusMatch(Lotto targetLotto) { + return targetLotto.getLottoNumbers().contains(bonusNumber); + } + + private int getMatchNumbersCount(Lotto targetLotto) { + targetLotto.getLottoNumbers().sort(Comparator.comparingInt(LottoNumber::getLottoNumber)); + this.lottoNumbers.sort(Comparator.comparingInt(LottoNumber::getLottoNumber)); + int targetIdx = 0, winningIdx = 0; + int matchNumbersCount = 0; + while (targetIdx < targetLotto.lottoNumbers.size() && winningIdx < this.lottoNumbers.size()) { + int targetNumber = targetLotto.lottoNumbers.get(targetIdx).getLottoNumber(); + int winningNumber = this.lottoNumbers.get(winningIdx).getLottoNumber(); + if (targetNumber == winningNumber) { + matchNumbersCount++; + targetIdx++; + winningIdx++; + } + else if (targetNumber > winningNumber) { + winningIdx++; + } + else { + targetIdx++; + } + } + System.out.println(matchNumbersCount); + return matchNumbersCount; + } } diff --git a/src/test/java/lotto/domain/WinningLottoTest.java b/src/test/java/lotto/domain/WinningLottoTest.java index 4ef1b53a..759ec43f 100644 --- a/src/test/java/lotto/domain/WinningLottoTest.java +++ b/src/test/java/lotto/domain/WinningLottoTest.java @@ -1,10 +1,12 @@ package lotto.domain; -import lotto.domain.WinningLotto; +import lotto.constant.PrizeCondition; +import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import java.util.ArrayList; +import java.util.List; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; @@ -12,33 +14,35 @@ public class WinningLottoTest { + private final String winningNumberInput = "1, 2, 3, 4, 5, 6"; + private final String bonusNumberInput = "10"; + private static Lotto targetLotto; + private final WinningLotto winningLotto = new WinningLotto(winningNumberInput, bonusNumberInput); + + @BeforeAll + private static void generateTargetLotto() { + List lottoNumbers = new ArrayList<>(); + lottoNumbers.add(new LottoNumber("1")); + lottoNumbers.add(new LottoNumber("2")); + lottoNumbers.add(new LottoNumber("3")); + lottoNumbers.add(new LottoNumber("4")); + lottoNumbers.add(new LottoNumber("5")); + lottoNumbers.add(new LottoNumber("6")); + targetLotto = new Lotto(lottoNumbers); + } + @Test @DisplayName("WinningNumbers 타입 확인") public void test() throws Exception { - // given - String input = "1, 2, 3, 4, 5, 6"; - String bonus = "10"; - WinningLotto winningLotto = new WinningLotto(input, bonus); - - // when - - // then + // given, when, then assertThat(winningLotto.getLottoNumbers()).isExactlyInstanceOf(ArrayList.class); } @Test @DisplayName("숫자가 6개인지 확인") public void testWinningNumberSize() throws Exception { - // given - String input = "1, 2, 3, 4, 5, 6"; - String bonus = "10"; - - // when - WinningLotto winningLotto = new WinningLotto(input, bonus); - - // then + // given, when, then assertThat(winningLotto.getLottoNumbers().size()).isEqualTo(6); - } @@ -84,15 +88,61 @@ public void testInputSmallerThanLowerBoundary() throws Exception { @Test @DisplayName("보너스 번호 입력값을 확인") void testBonusInput() { - //given - String input = "1, 2, 3, 4, 5, 6"; - String bonus = "10"; + //given, when, then + assertThat(winningLotto.getBonusNumber().getLottoNumber()).isEqualTo(10); + } - // when - WinningLotto winningLotto = new WinningLotto(input, bonus); + @Test + @DisplayName("보너스 미포함 6개 맞으면 1등") + void testFindPrize_firstWithoutBonus() { + //given, when + PrizeCondition prize = winningLotto.findPrize(targetLotto); - // then - assertThat(winningLotto.getBonusNumber().getLottoNumber()).isEqualTo(10); + //then + assertThat(prize).isEqualTo(PrizeCondition.FIRST); } + @Test + @DisplayName("5개 + 보너스 맞으면 2등") + void testFindPrize_secondWithBonus() { + //given, when + WinningLotto winningLotto = new WinningLotto("1,2,3,4,5,7", "6"); + PrizeCondition prize = winningLotto.findPrize(targetLotto); + + //then + assertThat(prize).isEqualTo(PrizeCondition.SECOND); + } + + @Test + @DisplayName("5개 + 보너스 안 맞으면 3등") + void testFindPrize_third() { + //given, when + WinningLotto winningLotto = new WinningLotto("1,2,3,4,5,7", bonusNumberInput); + PrizeCondition prize = winningLotto.findPrize(targetLotto); + + //then + assertThat(prize).isEqualTo(PrizeCondition.THIRD); + } + + @Test + @DisplayName("보너스 미포함 4개 맞으면 4등") + void testFindPrize_fourth() { + //given, when + WinningLotto winningLotto = new WinningLotto("4,3,2,1,8,7", bonusNumberInput); + PrizeCondition prize = winningLotto.findPrize(targetLotto); + + //then + assertThat(prize).isEqualTo(PrizeCondition.FOURTH); + } + + @Test + @DisplayName("보너스 미포함 3개 맞으면 5등") + void testFindPrize_fifth() { + //given, when + WinningLotto winningLotto = new WinningLotto("4,3,2,9,8,7", bonusNumberInput); + PrizeCondition prize = winningLotto.findPrize(targetLotto); + + //then + assertThat(prize).isEqualTo(PrizeCondition.FIFTH); + } } From 078bf69707743e9d01d7ffd4315a4b70c2b5d4ac Mon Sep 17 00:00:00 2001 From: chunghyeon-kim Date: Wed, 7 Jul 2021 18:50:10 +0900 Subject: [PATCH 21/55] =?UTF-8?q?feat:=20=EB=A1=9C=EB=98=90=20=EB=8B=B9?= =?UTF-8?q?=EC=B2=A8=20=EA=B0=9C=EC=88=98=20=EA=B5=AC=ED=95=98=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + .../java/lotto/constant/PrizeCondition.java | 6 +- src/main/java/lotto/domain/LottoSet.java | 15 +++++ src/main/java/lotto/domain/PrizeCount.java | 41 +++++++++++++ .../java/lotto/domain/RandomLottoSet.java | 12 ++-- src/main/java/lotto/domain/WinningLotto.java | 14 +---- src/main/java/lotto/parser/LottoParser.java | 18 ++++++ .../lotto/constant/PrizeConditionTest.java | 25 ++++---- .../lotto/constant/PurchaseCountTest.java | 15 +++-- .../java/lotto/domain/LottoNumberTest.java | 1 - .../java/lotto/domain/PrizeCountTest.java | 57 +++++++++++++++++++ .../java/lotto/domain/RandomLottoSetTest.java | 3 +- .../java/lotto/domain/RandomLottoTest.java | 3 +- .../java/lotto/domain/WinningLottoTest.java | 1 - 14 files changed, 166 insertions(+), 46 deletions(-) create mode 100644 src/main/java/lotto/domain/LottoSet.java create mode 100644 src/main/java/lotto/domain/PrizeCount.java create mode 100644 src/main/java/lotto/parser/LottoParser.java create mode 100644 src/test/java/lotto/domain/PrizeCountTest.java diff --git a/README.md b/README.md index c4fe2bc4..ddd16434 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,7 @@ - [x] 당첨 조건을 enum 처리 - [x] 일치 개수 찾기 - [x] 5개 일치 시 보너스 볼 일치 여부 확인 + - [x] 로또 당첨 개수 구하기 - [ ] 당첨값의 합 구하기 - [ ] 수익률 구하기 - [ ] 결과 출력 \ No newline at end of file diff --git a/src/main/java/lotto/constant/PrizeCondition.java b/src/main/java/lotto/constant/PrizeCondition.java index 7148d19d..92f3e74c 100644 --- a/src/main/java/lotto/constant/PrizeCondition.java +++ b/src/main/java/lotto/constant/PrizeCondition.java @@ -6,7 +6,8 @@ public enum PrizeCondition { SECOND(5, true), THIRD(5, false), FOURTH(4, false), - FIFTH(3, false); + FIFTH(3, false), + LOSE(2, false); private final int matchNumbersCount; private final boolean isBonus; @@ -18,6 +19,9 @@ public enum PrizeCondition { public static PrizeCondition findPrize(int matchNumbersCount, boolean isBonus) { + if (matchNumbersCount <= LOSE.matchNumbersCount) { + return LOSE; + } if (matchNumbersCount == FIFTH.matchNumbersCount) { return FIFTH; } diff --git a/src/main/java/lotto/domain/LottoSet.java b/src/main/java/lotto/domain/LottoSet.java new file mode 100644 index 00000000..2cd72f31 --- /dev/null +++ b/src/main/java/lotto/domain/LottoSet.java @@ -0,0 +1,15 @@ +package lotto.domain; + +import lombok.Getter; + +import java.util.Set; + +public class LottoSet { + + @Getter + private final Set lottoSet; + + public LottoSet(Set lottoSet) { + this.lottoSet = lottoSet; + } +} diff --git a/src/main/java/lotto/domain/PrizeCount.java b/src/main/java/lotto/domain/PrizeCount.java new file mode 100644 index 00000000..35ab99aa --- /dev/null +++ b/src/main/java/lotto/domain/PrizeCount.java @@ -0,0 +1,41 @@ +package lotto.domain; + +import lombok.Getter; +import lotto.constant.PrizeCondition; + +@Getter +public class PrizeCount { + private int countFirst; + private int countSecond; + private int countThird; + private int countFourth; + private int countFifth; + + public PrizeCount(LottoSet lottoset, WinningLotto winningLotto) { + for (Lotto lotto : lottoset.getLottoSet()) { + updateCounts(winningLotto.findPrize(lotto)); + } + } + + private void updateCounts(PrizeCondition condition) { + if(condition.equals(PrizeCondition.FIRST)) { + countFirst++; + return; + } + if(condition.equals(PrizeCondition.SECOND)) { + countSecond++; + return; + } + if(condition.equals(PrizeCondition.THIRD)) { + countThird++; + return; + } + if(condition.equals(PrizeCondition.FOURTH)) { + countFourth++; + return; + } + if(condition.equals(PrizeCondition.FIFTH)) { + countFifth++; + } + } +} diff --git a/src/main/java/lotto/domain/RandomLottoSet.java b/src/main/java/lotto/domain/RandomLottoSet.java index 7592b345..74786b2d 100644 --- a/src/main/java/lotto/domain/RandomLottoSet.java +++ b/src/main/java/lotto/domain/RandomLottoSet.java @@ -5,18 +5,18 @@ import java.util.HashSet; import java.util.Set; -public class RandomLottoSet { - - @Getter - private final Set lottoSet = new HashSet<>(); +public class RandomLottoSet extends LottoSet { public RandomLottoSet(PurchaseCount purchaseCount) { - generateRandomLottoSetWithSize(purchaseCount.getPurchaseCount()); + super(generateRandomLottoSetWithSize(purchaseCount.getPurchaseCount())); + } - private void generateRandomLottoSetWithSize(int size) { + private static Set generateRandomLottoSetWithSize(int size) { + Set lottoSet = new HashSet<>(); for (int i = 0; i < size; i++) { lottoSet.add(new RandomLotto()); } + return lottoSet; } } diff --git a/src/main/java/lotto/domain/WinningLotto.java b/src/main/java/lotto/domain/WinningLotto.java index f65ed4f4..f28c3848 100644 --- a/src/main/java/lotto/domain/WinningLotto.java +++ b/src/main/java/lotto/domain/WinningLotto.java @@ -2,14 +2,9 @@ import lombok.Getter; import lotto.constant.PrizeCondition; -import lotto.domain.Lotto; -import lotto.domain.LottoNumber; +import lotto.parser.LottoParser; -import java.util.Arrays; -import java.util.Collections; import java.util.Comparator; -import java.util.List; -import java.util.stream.Collectors; public class WinningLotto extends Lotto { @@ -17,10 +12,7 @@ public class WinningLotto extends Lotto { private LottoNumber bonusNumber; public WinningLotto(String winningNumberInput, String bonusNumberInput) { - super(Arrays.stream(winningNumberInput.split(",")) - .map(String::trim) - .map(LottoNumber::new) - .collect(Collectors.toList())); + super(LottoParser.generateLotto(winningNumberInput)); this.bonusNumber = new LottoNumber(bonusNumberInput); } @@ -52,7 +44,7 @@ else if (targetNumber > winningNumber) { targetIdx++; } } - System.out.println(matchNumbersCount); return matchNumbersCount; } + } diff --git a/src/main/java/lotto/parser/LottoParser.java b/src/main/java/lotto/parser/LottoParser.java new file mode 100644 index 00000000..442080f6 --- /dev/null +++ b/src/main/java/lotto/parser/LottoParser.java @@ -0,0 +1,18 @@ +package lotto.parser; + +import lotto.domain.LottoNumber; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +public class LottoParser { + private static final String DELIMITER = ","; + + public static List generateLotto(String numberInput) { + return Arrays.stream(numberInput.split(DELIMITER)) + .map(String::trim) + .map(LottoNumber::new) + .collect(Collectors.toList()); + } +} diff --git a/src/test/java/lotto/constant/PrizeConditionTest.java b/src/test/java/lotto/constant/PrizeConditionTest.java index cdf6e017..e780d970 100644 --- a/src/test/java/lotto/constant/PrizeConditionTest.java +++ b/src/test/java/lotto/constant/PrizeConditionTest.java @@ -1,6 +1,5 @@ package lotto.constant; -import lotto.constant.PrizeCondition; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -9,24 +8,23 @@ public class PrizeConditionTest { @Test - @DisplayName("숫자가 6개 일치하고 보너스번호는 일치하지 않을 때 1등") + @DisplayName("숫자가 6개 일치하 1등") void firstPrize() { //given int matchNumbersCount = 6; - boolean isBonus = false; - - //when - PrizeCondition prizeCondition = PrizeCondition.findPrize(matchNumbersCount, isBonus); - //then - assertThat(prizeCondition).isEqualTo(PrizeCondition.FIRST); + //when, then + assertThat(PrizeCondition.findPrize(matchNumbersCount, true)) + .isEqualTo(PrizeCondition.FIRST); + assertThat(PrizeCondition.findPrize(matchNumbersCount, false)) + .isEqualTo(PrizeCondition.FIRST); } @Test - @DisplayName("숫자가 6개 일치하고 보너스번호도 일치할 때 2등") + @DisplayName("숫자가 5개 일치하고 보너스번호도 일치할 때 2등") void secondPrize() { //given - int matchNumbersCount = 6; + int matchNumbersCount = 5; boolean isBonus = true; //when @@ -37,15 +35,14 @@ void secondPrize() { } @Test - @DisplayName("숫자가 5개 일치할 때 3등") + @DisplayName("숫자가 5개 일치하고 보너스 번호가 틀 때 3등") void thirdPrize() { //given int matchNumbersCount = 5; + boolean isBonus = false; //when, then - assertThat(PrizeCondition.findPrize(matchNumbersCount, false)) - .isEqualTo(PrizeCondition.THIRD); - assertThat(PrizeCondition.findPrize(matchNumbersCount, true)) + assertThat(PrizeCondition.findPrize(matchNumbersCount, isBonus)) .isEqualTo(PrizeCondition.THIRD); } diff --git a/src/test/java/lotto/constant/PurchaseCountTest.java b/src/test/java/lotto/constant/PurchaseCountTest.java index 3fa290d0..2ecd143c 100644 --- a/src/test/java/lotto/constant/PurchaseCountTest.java +++ b/src/test/java/lotto/constant/PurchaseCountTest.java @@ -13,11 +13,10 @@ public class PurchaseCountTest { @Test @DisplayName("") - public void testInputValueUnder1000() throws Exception { + void testInputValueUnder1000() throws Exception { // given String input = "800"; - // when, then assertThrows(IllegalArgumentException.class, () -> { PurchaseCount purchaseCount = new PurchaseCount(input);; @@ -26,7 +25,7 @@ public void testInputValueUnder1000() throws Exception { @Test @DisplayName("") - public void testInputValueNotUnder1000() throws Exception { + void testInputValueNotUnder1000() throws Exception { // given String input = "2000"; @@ -36,7 +35,7 @@ public void testInputValueNotUnder1000() throws Exception { @Test @DisplayName("") - public void testInputValueNotMultiple1000() throws Exception { + void testInputValueNotMultiple1000() throws Exception { // given String input = "1234"; @@ -47,7 +46,7 @@ public void testInputValueNotMultiple1000() throws Exception { @Test @DisplayName("") - public void testInputNotNumber() throws Exception { + void testInputNotNumber() throws Exception { // given String input = "error"; @@ -60,7 +59,7 @@ public void testInputNotNumber() throws Exception { @Test @DisplayName("공백이 오른쪽에 있을 때 정상 처리된다") - public void testPriceInputWithLeftBlank() throws Exception { + void testPriceInputWithLeftBlank() throws Exception { // given String input = " 1000"; @@ -73,7 +72,7 @@ public void testPriceInputWithLeftBlank() throws Exception { @Test @DisplayName("공백이 오른쪽에 있을 때 정상 처리된다") - public void testPriceInputWithRightBlank() throws Exception { + void testPriceInputWithRightBlank() throws Exception { // given String input = "1000 "; @@ -86,7 +85,7 @@ public void testPriceInputWithRightBlank() throws Exception { @Test @DisplayName("공백이 양쪽에 있을 때 정상 처리된다") - public void testPriceInputWithLeftAndRightBlank() throws Exception { + void testPriceInputWithLeftAndRightBlank() throws Exception { // given String input = " 1000 "; diff --git a/src/test/java/lotto/domain/LottoNumberTest.java b/src/test/java/lotto/domain/LottoNumberTest.java index 5b3e599a..111ad8ec 100644 --- a/src/test/java/lotto/domain/LottoNumberTest.java +++ b/src/test/java/lotto/domain/LottoNumberTest.java @@ -1,6 +1,5 @@ package lotto.domain; -import lotto.domain.LottoNumber; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; diff --git a/src/test/java/lotto/domain/PrizeCountTest.java b/src/test/java/lotto/domain/PrizeCountTest.java new file mode 100644 index 00000000..b98a2f93 --- /dev/null +++ b/src/test/java/lotto/domain/PrizeCountTest.java @@ -0,0 +1,57 @@ +package lotto.domain; + +import lotto.parser.LottoParser; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.HashSet; + +public class PrizeCountTest { + + private final String winningNumberInput = "1, 2, 3, 4, 5, 6"; + private final String bonusNumberInput = "7"; + private final WinningLotto winningLotto = new WinningLotto(winningNumberInput, bonusNumberInput); + + @Test + @DisplayName("로또 여러 게임의 당첨 통계를 구한다") + void testCountPrizesOfLottoSet() { + //given + HashSet lottos = new HashSet<>(); + lottos.add(new Lotto(LottoParser.generateLotto("1, 2, 3, 4, 5, 6"))); + + lottos.add(new Lotto(LottoParser.generateLotto("1, 2, 3, 4, 5, 7"))); + lottos.add(new Lotto(LottoParser.generateLotto("1, 2, 3, 4, 5, 7"))); + + lottos.add(new Lotto(LottoParser.generateLotto("1, 2, 3, 4, 5, 10"))); + lottos.add(new Lotto(LottoParser.generateLotto("1, 2, 3, 4, 5, 10"))); + lottos.add(new Lotto(LottoParser.generateLotto("1, 2, 3, 4, 5, 10"))); + + lottos.add(new Lotto(LottoParser.generateLotto("1, 2, 3, 4, 10, 11"))); + lottos.add(new Lotto(LottoParser.generateLotto("1, 2, 3, 4, 10, 11"))); + lottos.add(new Lotto(LottoParser.generateLotto("1, 2, 3, 4, 10, 11"))); + lottos.add(new Lotto(LottoParser.generateLotto("1, 2, 3, 4, 10, 11"))); + + lottos.add(new Lotto(LottoParser.generateLotto("1, 2, 3, 10, 11, 12"))); + lottos.add(new Lotto(LottoParser.generateLotto("1, 2, 3, 10, 11, 12"))); + lottos.add(new Lotto(LottoParser.generateLotto("1, 2, 3, 10, 11, 12"))); + lottos.add(new Lotto(LottoParser.generateLotto("1, 2, 3, 10, 11, 12"))); + lottos.add(new Lotto(LottoParser.generateLotto("1, 2, 3, 10, 11, 12"))); + + lottos.add(new Lotto(LottoParser.generateLotto("1, 2, 10, 11, 12, 13"))); + lottos.add(new Lotto(LottoParser.generateLotto("1, 10, 11, 12, 13, 14"))); + lottos.add(new Lotto(LottoParser.generateLotto("19, 10, 11, 12, 13, 14"))); + LottoSet lottoset = new LottoSet(lottos); + + //when + PrizeCount prizeCount = new PrizeCount(lottoset, winningLotto); + + //then + assertThat(prizeCount.getCountFirst()).isEqualTo(1); + assertThat(prizeCount.getCountSecond()).isEqualTo(2); + assertThat(prizeCount.getCountThird()).isEqualTo(3); + assertThat(prizeCount.getCountFourth()).isEqualTo(4); + assertThat(prizeCount.getCountFifth()).isEqualTo(5); + } +} diff --git a/src/test/java/lotto/domain/RandomLottoSetTest.java b/src/test/java/lotto/domain/RandomLottoSetTest.java index 5d293f34..0ee990be 100644 --- a/src/test/java/lotto/domain/RandomLottoSetTest.java +++ b/src/test/java/lotto/domain/RandomLottoSetTest.java @@ -5,11 +5,10 @@ import static org.assertj.core.api.Assertions.assertThat; public class RandomLottoSetTest { - private static final int PURCHASE_COUNT_STANDARD = 1000; @Test - public void testGenerateLottoSet() throws Exception { + void testGenerateLottoSet() throws Exception { // given int targetSize = 10; PurchaseCount purchaseCount = new PurchaseCount(String.valueOf(targetSize * PURCHASE_COUNT_STANDARD)); diff --git a/src/test/java/lotto/domain/RandomLottoTest.java b/src/test/java/lotto/domain/RandomLottoTest.java index 57eda6ef..8edd042d 100644 --- a/src/test/java/lotto/domain/RandomLottoTest.java +++ b/src/test/java/lotto/domain/RandomLottoTest.java @@ -1,6 +1,5 @@ package lotto.domain; -import lotto.domain.RandomLotto; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -10,7 +9,7 @@ public class RandomLottoTest { @Test @DisplayName("로또 1개 생성") - public void testGenerateLotto() throws Exception { + void testGenerateLotto() throws Exception { // given RandomLotto randomLotto = new RandomLotto(); diff --git a/src/test/java/lotto/domain/WinningLottoTest.java b/src/test/java/lotto/domain/WinningLottoTest.java index 759ec43f..6a03c056 100644 --- a/src/test/java/lotto/domain/WinningLottoTest.java +++ b/src/test/java/lotto/domain/WinningLottoTest.java @@ -9,7 +9,6 @@ import java.util.List; import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.jupiter.api.Assertions.assertThrows; public class WinningLottoTest { From 4a83ad012ca365f412e8c8818289e0de4428e476 Mon Sep 17 00:00:00 2001 From: chunghyeon-kim Date: Wed, 7 Jul 2021 19:09:13 +0900 Subject: [PATCH 22/55] =?UTF-8?q?refactor:=20=ED=85=8C=EC=8A=A4=ED=8A=B8?= =?UTF-8?q?=EC=9A=A9=20=EB=A1=9C=EB=98=90=20=ED=81=B4=EB=9E=98=EC=8A=A4=20?= =?UTF-8?q?=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/lotto/domain/PrizeCountTest.java | 33 ++------------- src/test/java/lotto/domain/TestLottoSet.java | 42 +++++++++++++++++++ .../java/lotto/domain/TestWinningLotto.java | 10 +++++ 3 files changed, 55 insertions(+), 30 deletions(-) create mode 100644 src/test/java/lotto/domain/TestLottoSet.java create mode 100644 src/test/java/lotto/domain/TestWinningLotto.java diff --git a/src/test/java/lotto/domain/PrizeCountTest.java b/src/test/java/lotto/domain/PrizeCountTest.java index b98a2f93..2a3e6b3f 100644 --- a/src/test/java/lotto/domain/PrizeCountTest.java +++ b/src/test/java/lotto/domain/PrizeCountTest.java @@ -10,42 +10,15 @@ public class PrizeCountTest { - private final String winningNumberInput = "1, 2, 3, 4, 5, 6"; - private final String bonusNumberInput = "7"; - private final WinningLotto winningLotto = new WinningLotto(winningNumberInput, bonusNumberInput); - @Test @DisplayName("로또 여러 게임의 당첨 통계를 구한다") void testCountPrizesOfLottoSet() { //given - HashSet lottos = new HashSet<>(); - lottos.add(new Lotto(LottoParser.generateLotto("1, 2, 3, 4, 5, 6"))); - - lottos.add(new Lotto(LottoParser.generateLotto("1, 2, 3, 4, 5, 7"))); - lottos.add(new Lotto(LottoParser.generateLotto("1, 2, 3, 4, 5, 7"))); - - lottos.add(new Lotto(LottoParser.generateLotto("1, 2, 3, 4, 5, 10"))); - lottos.add(new Lotto(LottoParser.generateLotto("1, 2, 3, 4, 5, 10"))); - lottos.add(new Lotto(LottoParser.generateLotto("1, 2, 3, 4, 5, 10"))); - - lottos.add(new Lotto(LottoParser.generateLotto("1, 2, 3, 4, 10, 11"))); - lottos.add(new Lotto(LottoParser.generateLotto("1, 2, 3, 4, 10, 11"))); - lottos.add(new Lotto(LottoParser.generateLotto("1, 2, 3, 4, 10, 11"))); - lottos.add(new Lotto(LottoParser.generateLotto("1, 2, 3, 4, 10, 11"))); - - lottos.add(new Lotto(LottoParser.generateLotto("1, 2, 3, 10, 11, 12"))); - lottos.add(new Lotto(LottoParser.generateLotto("1, 2, 3, 10, 11, 12"))); - lottos.add(new Lotto(LottoParser.generateLotto("1, 2, 3, 10, 11, 12"))); - lottos.add(new Lotto(LottoParser.generateLotto("1, 2, 3, 10, 11, 12"))); - lottos.add(new Lotto(LottoParser.generateLotto("1, 2, 3, 10, 11, 12"))); - - lottos.add(new Lotto(LottoParser.generateLotto("1, 2, 10, 11, 12, 13"))); - lottos.add(new Lotto(LottoParser.generateLotto("1, 10, 11, 12, 13, 14"))); - lottos.add(new Lotto(LottoParser.generateLotto("19, 10, 11, 12, 13, 14"))); - LottoSet lottoset = new LottoSet(lottos); + TestLottoSet lottoSet = new TestLottoSet(); + TestWinningLotto winningLotto = new TestWinningLotto(); //when - PrizeCount prizeCount = new PrizeCount(lottoset, winningLotto); + PrizeCount prizeCount = new PrizeCount(lottoSet, winningLotto); //then assertThat(prizeCount.getCountFirst()).isEqualTo(1); diff --git a/src/test/java/lotto/domain/TestLottoSet.java b/src/test/java/lotto/domain/TestLottoSet.java new file mode 100644 index 00000000..6552af7d --- /dev/null +++ b/src/test/java/lotto/domain/TestLottoSet.java @@ -0,0 +1,42 @@ +package lotto.domain; + +import lotto.parser.LottoParser; + +import java.util.HashSet; + +public class TestLottoSet extends LottoSet{ + + public TestLottoSet() { + super(generateLottoSet()); + } + + public static HashSet generateLottoSet() { + HashSet lottos = new HashSet<>(); + lottos.add(new Lotto(LottoParser.generateLotto("1, 2, 3, 4, 5, 6"))); + + lottos.add(new Lotto(LottoParser.generateLotto("1, 2, 3, 4, 5, 7"))); + lottos.add(new Lotto(LottoParser.generateLotto("1, 2, 3, 4, 5, 7"))); + + lottos.add(new Lotto(LottoParser.generateLotto("1, 2, 3, 4, 5, 10"))); + lottos.add(new Lotto(LottoParser.generateLotto("1, 2, 3, 4, 5, 10"))); + lottos.add(new Lotto(LottoParser.generateLotto("1, 2, 3, 4, 5, 10"))); + + lottos.add(new Lotto(LottoParser.generateLotto("1, 2, 3, 4, 10, 11"))); + lottos.add(new Lotto(LottoParser.generateLotto("1, 2, 3, 4, 10, 11"))); + lottos.add(new Lotto(LottoParser.generateLotto("1, 2, 3, 4, 10, 11"))); + lottos.add(new Lotto(LottoParser.generateLotto("1, 2, 3, 4, 10, 11"))); + + lottos.add(new Lotto(LottoParser.generateLotto("1, 2, 3, 10, 11, 12"))); + lottos.add(new Lotto(LottoParser.generateLotto("1, 2, 3, 10, 11, 12"))); + lottos.add(new Lotto(LottoParser.generateLotto("1, 2, 3, 10, 11, 12"))); + lottos.add(new Lotto(LottoParser.generateLotto("1, 2, 3, 10, 11, 12"))); + lottos.add(new Lotto(LottoParser.generateLotto("1, 2, 3, 10, 11, 12"))); + + lottos.add(new Lotto(LottoParser.generateLotto("1, 2, 10, 11, 12, 13"))); + lottos.add(new Lotto(LottoParser.generateLotto("1, 10, 11, 12, 13, 14"))); + lottos.add(new Lotto(LottoParser.generateLotto("19, 10, 11, 12, 13, 14"))); + + return lottos; + } + +} diff --git a/src/test/java/lotto/domain/TestWinningLotto.java b/src/test/java/lotto/domain/TestWinningLotto.java new file mode 100644 index 00000000..ceb5a74e --- /dev/null +++ b/src/test/java/lotto/domain/TestWinningLotto.java @@ -0,0 +1,10 @@ +package lotto.domain; + +public class TestWinningLotto extends WinningLotto{ + private static final String winningNumberInput = "1, 2, 3, 4, 5, 6"; + private static final String bonusNumberInput = "7"; + + public TestWinningLotto() { + super(winningNumberInput, bonusNumberInput); + } +} From f8da3c55703c85da77366298bfaeb6577e628035 Mon Sep 17 00:00:00 2001 From: chunghyeon-kim Date: Wed, 7 Jul 2021 19:36:02 +0900 Subject: [PATCH 23/55] =?UTF-8?q?feat:=20=EC=B4=9D=20=EC=83=81=EA=B8=88?= =?UTF-8?q?=EC=95=A1=20=EA=B3=84=EC=82=B0=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20?= =?UTF-8?q?=EB=A9=94=EC=84=9C=EB=93=9C=20=EB=B0=8F=20=ED=81=B4=EB=9E=98?= =?UTF-8?q?=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 --- README.md | 8 +++--- src/main/java/lotto/constant/PrizeMoney.java | 7 +++-- src/main/java/lotto/domain/Lotto.java | 2 ++ .../java/lotto/domain/LottoStatistics.java | 18 +++++++++++++ src/main/java/lotto/domain/PurchaseCount.java | 5 ++-- .../lotto/domain/LottoStatisticsTest.java | 27 +++++++++++++++++++ 6 files changed, 58 insertions(+), 9 deletions(-) create mode 100644 src/main/java/lotto/domain/LottoStatistics.java create mode 100644 src/test/java/lotto/domain/LottoStatisticsTest.java diff --git a/README.md b/README.md index ddd16434..6f81340f 100644 --- a/README.md +++ b/README.md @@ -11,8 +11,8 @@ - [x] 구매 개수 구하기 - [x] 랜덤 로또번호 구매 개수만큼 만들기 - [ ] defaultNumberSet 1번만 생성되도록 변경 - - [ ] RandomLottoTest 상수 리팩토링 - - [ ] PurchaseCount의 1000 접근 + - [x] RandomLottoTest 상수 리팩토링 + - [x] PurchaseCount의 1000 접근 - [x] 지난 주 당첨 번호 입력받기 - [x] WinningNumbers 멤버변수 ArrayList 클래스 확인 - [x] 숫자 개수 6개 확인 @@ -25,6 +25,6 @@ - [x] 일치 개수 찾기 - [x] 5개 일치 시 보너스 볼 일치 여부 확인 - [x] 로또 당첨 개수 구하기 - - [ ] 당첨값의 합 구하기 - - [ ] 수익률 구하기 + - [x] 당첨값의 합 구하기 + - [x] 수익률 구하기 - [ ] 결과 출력 \ No newline at end of file diff --git a/src/main/java/lotto/constant/PrizeMoney.java b/src/main/java/lotto/constant/PrizeMoney.java index 01f34840..910cff49 100644 --- a/src/main/java/lotto/constant/PrizeMoney.java +++ b/src/main/java/lotto/constant/PrizeMoney.java @@ -1,5 +1,8 @@ package lotto.constant; +import lombok.Getter; + +@Getter public enum PrizeMoney { FIRST(2000000000), @@ -8,9 +11,9 @@ public enum PrizeMoney { FOURTH(50000), FIFTH(5000); - private final int value; + private final long value; - PrizeMoney(int value) { + PrizeMoney(long value) { this.value = value; } } diff --git a/src/main/java/lotto/domain/Lotto.java b/src/main/java/lotto/domain/Lotto.java index 9350cb09..a79f05ae 100644 --- a/src/main/java/lotto/domain/Lotto.java +++ b/src/main/java/lotto/domain/Lotto.java @@ -6,6 +6,8 @@ public class Lotto { + public static final int PRICE = 1000; + @Getter protected final List lottoNumbers; diff --git a/src/main/java/lotto/domain/LottoStatistics.java b/src/main/java/lotto/domain/LottoStatistics.java new file mode 100644 index 00000000..cb199cec --- /dev/null +++ b/src/main/java/lotto/domain/LottoStatistics.java @@ -0,0 +1,18 @@ +package lotto.domain; + +import lotto.constant.PrizeMoney; + +public class LottoStatistics { + + public static double calcProfitRate(PrizeCount prizeCount, PurchaseCount purchaseCount) { + return (double) calcSum(prizeCount) / (purchaseCount.getPurchaseCount() * Lotto.PRICE); + } + + private static long calcSum(PrizeCount prizeCount) { + return prizeCount.getCountFirst() * PrizeMoney.FIRST.getValue() + + prizeCount.getCountSecond() * PrizeMoney.SECOND.getValue() + + prizeCount.getCountThird() * PrizeMoney.THIRD.getValue() + + prizeCount.getCountFourth() * PrizeMoney.FOURTH.getValue() + + prizeCount.getCountFifth() * PrizeMoney.FIFTH.getValue(); + } +} diff --git a/src/main/java/lotto/domain/PurchaseCount.java b/src/main/java/lotto/domain/PurchaseCount.java index c90e8de3..6d67be07 100644 --- a/src/main/java/lotto/domain/PurchaseCount.java +++ b/src/main/java/lotto/domain/PurchaseCount.java @@ -7,7 +7,6 @@ public class PurchaseCount { private static final int MINIMUM_INPUT = 1000; - private static final int STANDARD = 1000; @Getter private final int purchaseCount; @@ -15,7 +14,7 @@ public class PurchaseCount { public PurchaseCount(String input) { input = input.trim(); validate(input); - this.purchaseCount = Integer.parseInt(input)/STANDARD; + this.purchaseCount = Integer.parseInt(input) / Lotto.PRICE; } private void validate(String input) { @@ -33,6 +32,6 @@ private boolean notMatchesCondition(int input) { } private boolean notMultipleOfStandard(int input) { - return input % STANDARD != 0; + return input % Lotto.PRICE != 0; } } diff --git a/src/test/java/lotto/domain/LottoStatisticsTest.java b/src/test/java/lotto/domain/LottoStatisticsTest.java new file mode 100644 index 00000000..24fdcb69 --- /dev/null +++ b/src/test/java/lotto/domain/LottoStatisticsTest.java @@ -0,0 +1,27 @@ +package lotto.domain; + +import lotto.constant.PrizeMoney; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class LottoStatisticsTest { + + private static final PrizeCount PRIZE_COUNT = new PrizeCount(new TestLottoSet(), new TestWinningLotto()); + private static final long PRIZE_SUM = PrizeMoney.FIRST.getValue() + + 2 * PrizeMoney.SECOND.getValue() + + 3 * PrizeMoney.THIRD.getValue() + + 4 * PrizeMoney.FOURTH.getValue() + + 5 * PrizeMoney.FIFTH.getValue(); + private static final int PURCHASE_MONEY = 18000; + private static final double RESULT = (double) PRIZE_SUM / PURCHASE_MONEY; + + @Test + @DisplayName("당첨 로또 개수를 받으면 총 상금액을 반환한다") + void testLottoStatistics() { + //given, when, then + assertThat(LottoStatistics.calcProfitRate(PRIZE_COUNT, new PurchaseCount(String.valueOf(PURCHASE_MONEY)))) + .isEqualTo(RESULT); + } +} From a84a57e71d672151ce72396cd04947a042d3341a Mon Sep 17 00:00:00 2001 From: chunghyeon-kim Date: Wed, 7 Jul 2021 21:10:09 +0900 Subject: [PATCH 24/55] =?UTF-8?q?feat:=20Controller,=20InputView,=20Output?= =?UTF-8?q?View=201=EC=B0=A8=20=EC=99=84=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 8 +-- .../java/lotto/constant/PrizeMessage.java | 18 +++++++ .../lotto/controller/LottoController.java | 30 ++++++++++++ .../java/lotto/domain/LottoStatistics.java | 16 ++++-- .../java/lotto/domain/RandomLottoSet.java | 3 -- .../domain/dto/PurchasePriceInputDTO.java | 11 +++++ .../domain/dto/WinningLottoInputDTO.java | 12 +++++ src/main/java/lotto/view/InputView.java | 26 ++++++++++ src/main/java/lotto/view/OutputView.java | 49 +++++++++++++++++++ src/main/java/lotto/view/View.java | 32 ++++++++++++ 10 files changed, 195 insertions(+), 10 deletions(-) create mode 100644 src/main/java/lotto/constant/PrizeMessage.java create mode 100644 src/main/java/lotto/controller/LottoController.java create mode 100644 src/main/java/lotto/domain/dto/PurchasePriceInputDTO.java create mode 100644 src/main/java/lotto/domain/dto/WinningLottoInputDTO.java create mode 100644 src/main/java/lotto/view/InputView.java create mode 100644 src/main/java/lotto/view/OutputView.java create mode 100644 src/main/java/lotto/view/View.java diff --git a/README.md b/README.md index 6f81340f..3783cad5 100644 --- a/README.md +++ b/README.md @@ -2,11 +2,11 @@ # 구현 기능 목록 -- [ ] 구입금액 입력받기 +- [x] 구입금액 입력받기 - [x] 1000원 미만 입력 시 처리 - [x] 1000의 배수 아닌 값 입력 시 처리 - [x] 숫자가 아닌 값 (문자열) - - [ ] 정상값 처리 + - [x] 정상값 처리 - [x] 공백 처리 - [x] 구매 개수 구하기 - [x] 랜덤 로또번호 구매 개수만큼 만들기 @@ -20,11 +20,11 @@ - [x] 범위 (1~45) 확인 - [x] 공백 처리 - [x] 보너스 볼 입력받기 -- [ ] 당첨 통계 +- [x] 당첨 통계 - [x] 당첨 조건을 enum 처리 - [x] 일치 개수 찾기 - [x] 5개 일치 시 보너스 볼 일치 여부 확인 - [x] 로또 당첨 개수 구하기 - [x] 당첨값의 합 구하기 - [x] 수익률 구하기 - - [ ] 결과 출력 \ No newline at end of file + - [x] 결과 출력 \ No newline at end of file diff --git a/src/main/java/lotto/constant/PrizeMessage.java b/src/main/java/lotto/constant/PrizeMessage.java new file mode 100644 index 00000000..6ee19c3e --- /dev/null +++ b/src/main/java/lotto/constant/PrizeMessage.java @@ -0,0 +1,18 @@ +package lotto.constant; + +import lombok.Getter; + +public enum PrizeMessage { + FIRST("6개 일치 (2000000000원)- "), + SECOND("5개 일치, 보너스 볼 일치(30000000원) - "), + THIRD("5개 일치 (1500000원)- "), + FOURTH("4개 일치 (50000원)- "), + FIFTH("3개 일치 (5000원)- "); + + @Getter + private final String message; + + PrizeMessage(String message) { + this.message= message; + } +} diff --git a/src/main/java/lotto/controller/LottoController.java b/src/main/java/lotto/controller/LottoController.java new file mode 100644 index 00000000..fb470805 --- /dev/null +++ b/src/main/java/lotto/controller/LottoController.java @@ -0,0 +1,30 @@ +package lotto.controller; + +import lotto.domain.*; +import lotto.domain.dto.PurchasePriceInputDTO; +import lotto.domain.dto.WinningLottoInputDTO; +import lotto.view.View; + +public class LottoController { + public static void main(String[] args) { + start(); + } + + public static void start() { + PurchasePriceInputDTO purchasePriceInputDTO = View.getPurchaseCost(); + + PurchaseCount purchaseCount = new PurchaseCount(purchasePriceInputDTO.getInput()); + RandomLottoSet randomLottoSet = new RandomLottoSet(purchaseCount); + + View.printLottoCount(purchaseCount); + View.printLottoSet(randomLottoSet); + + WinningLottoInputDTO winningLottoInputDTO = View.getWinningLottoAndBonus(); + WinningLotto winningLotto = new WinningLotto( + winningLottoInputDTO.getWinningLottoNumbers(), + winningLottoInputDTO.getWinningLottoBonus()); + + LottoStatistics lottoStatistics = new LottoStatistics(new PrizeCount(randomLottoSet, winningLotto), purchaseCount); + View.printLottoStatistics(lottoStatistics); + } +} diff --git a/src/main/java/lotto/domain/LottoStatistics.java b/src/main/java/lotto/domain/LottoStatistics.java index cb199cec..0c46b9f2 100644 --- a/src/main/java/lotto/domain/LottoStatistics.java +++ b/src/main/java/lotto/domain/LottoStatistics.java @@ -1,14 +1,24 @@ package lotto.domain; +import lombok.Getter; import lotto.constant.PrizeMoney; public class LottoStatistics { - public static double calcProfitRate(PrizeCount prizeCount, PurchaseCount purchaseCount) { - return (double) calcSum(prizeCount) / (purchaseCount.getPurchaseCount() * Lotto.PRICE); + @Getter + PrizeCount prizeCount; + PurchaseCount purchaseCount; + + public LottoStatistics(PrizeCount prizeCount, PurchaseCount purchaseCount) { + this.prizeCount = prizeCount; + this.purchaseCount = purchaseCount; + } + + public double calcProfitRate() { + return (double) calcSum() / (purchaseCount.getPurchaseCount() * Lotto.PRICE); } - private static long calcSum(PrizeCount prizeCount) { + private long calcSum() { return prizeCount.getCountFirst() * PrizeMoney.FIRST.getValue() + prizeCount.getCountSecond() * PrizeMoney.SECOND.getValue() + prizeCount.getCountThird() * PrizeMoney.THIRD.getValue() diff --git a/src/main/java/lotto/domain/RandomLottoSet.java b/src/main/java/lotto/domain/RandomLottoSet.java index 74786b2d..8e8d4114 100644 --- a/src/main/java/lotto/domain/RandomLottoSet.java +++ b/src/main/java/lotto/domain/RandomLottoSet.java @@ -1,7 +1,5 @@ package lotto.domain; -import lombok.Getter; - import java.util.HashSet; import java.util.Set; @@ -9,7 +7,6 @@ public class RandomLottoSet extends LottoSet { public RandomLottoSet(PurchaseCount purchaseCount) { super(generateRandomLottoSetWithSize(purchaseCount.getPurchaseCount())); - } private static Set generateRandomLottoSetWithSize(int size) { diff --git a/src/main/java/lotto/domain/dto/PurchasePriceInputDTO.java b/src/main/java/lotto/domain/dto/PurchasePriceInputDTO.java new file mode 100644 index 00000000..04843171 --- /dev/null +++ b/src/main/java/lotto/domain/dto/PurchasePriceInputDTO.java @@ -0,0 +1,11 @@ +package lotto.domain.dto; + +import lombok.Getter; +import lombok.RequiredArgsConstructor; + +@Getter +@RequiredArgsConstructor +public class PurchasePriceInputDTO { + + private final String input; +} diff --git a/src/main/java/lotto/domain/dto/WinningLottoInputDTO.java b/src/main/java/lotto/domain/dto/WinningLottoInputDTO.java new file mode 100644 index 00000000..7e4d6690 --- /dev/null +++ b/src/main/java/lotto/domain/dto/WinningLottoInputDTO.java @@ -0,0 +1,12 @@ +package lotto.domain.dto; + +import lombok.Getter; +import lombok.RequiredArgsConstructor; + +@Getter +@RequiredArgsConstructor +public class WinningLottoInputDTO { + + private final String winningLottoNumbers; + private final String winningLottoBonus; +} diff --git a/src/main/java/lotto/view/InputView.java b/src/main/java/lotto/view/InputView.java new file mode 100644 index 00000000..f8b40887 --- /dev/null +++ b/src/main/java/lotto/view/InputView.java @@ -0,0 +1,26 @@ +package lotto.view; + +import lotto.domain.dto.PurchasePriceInputDTO; +import lotto.domain.dto.WinningLottoInputDTO; + +import java.util.Scanner; + +public class InputView { + private final Scanner scanner = new Scanner(System.in); + + public PurchasePriceInputDTO getPurchaseCost() { + System.out.println("구입금액을 입력해 주세요."); + String purchaseCountInput = scanner.nextLine(); + + return new PurchasePriceInputDTO(purchaseCountInput); + } + + public WinningLottoInputDTO getWinningLottoAndBonus() { + System.out.println("지난 주 당첨 번호를 입력해 주세요."); + String winningLottoNumbers = scanner.nextLine(); + + System.out.println("보너스 볼을 입력해 주세요."); + String winningLottoBonus = scanner.nextLine(); + return new WinningLottoInputDTO(winningLottoNumbers, winningLottoBonus); + } +} diff --git a/src/main/java/lotto/view/OutputView.java b/src/main/java/lotto/view/OutputView.java new file mode 100644 index 00000000..28c17dfd --- /dev/null +++ b/src/main/java/lotto/view/OutputView.java @@ -0,0 +1,49 @@ +package lotto.view; + +import lotto.constant.PrizeMessage; +import lotto.domain.*; + +public class OutputView { + + private static final String COMMA = ", "; + + public void printLottoCount(PurchaseCount purchaseCount) { + int count = purchaseCount.getPurchaseCount(); + System.out.println(count + "개를 구매했습니다."); + } + + public void printLottoSet(RandomLottoSet lottoSet) { + for (Lotto lotto : lottoSet.getLottoSet()) { + printLotto(lotto); + } + } + + private void printLotto(Lotto lotto) { + StringBuilder sb = new StringBuilder(); + sb.append("["); + for (LottoNumber lottoNumber : lotto.getLottoNumbers()) { + sb.append(lottoNumber.getLottoNumber()).append(COMMA); + } + System.out.println(removeCommaAtTheEnd(sb.toString()) + "]"); + } + + private String removeCommaAtTheEnd(String str) { + return str.substring(0, str.length() - COMMA.length()); + } + + public void printLottoStatistic(LottoStatistics lottoStatistics) { + System.out.println("당첨 통계"); + System.out.println("---------"); + System.out.print(PrizeMessage.FIFTH.getMessage()); + System.out.println(lottoStatistics.getPrizeCount().getCountFifth() + "개"); + System.out.print(PrizeMessage.FOURTH.getMessage()); + System.out.println(lottoStatistics.getPrizeCount().getCountFourth() + "개"); + System.out.print(PrizeMessage.THIRD.getMessage()); + System.out.println(lottoStatistics.getPrizeCount().getCountThird() + "개"); + System.out.print(PrizeMessage.SECOND.getMessage()); + System.out.println(lottoStatistics.getPrizeCount().getCountSecond() + "개"); + System.out.print(PrizeMessage.FIRST.getMessage()); + System.out.println(lottoStatistics.getPrizeCount().getCountFirst() + "개"); + System.out.println("총 수익률은 " + lottoStatistics.calcProfitRate() + "입니다."); + } +} diff --git a/src/main/java/lotto/view/View.java b/src/main/java/lotto/view/View.java new file mode 100644 index 00000000..1e1144bc --- /dev/null +++ b/src/main/java/lotto/view/View.java @@ -0,0 +1,32 @@ +package lotto.view; + +import lotto.domain.LottoStatistics; +import lotto.domain.PurchaseCount; +import lotto.domain.RandomLottoSet; +import lotto.domain.dto.PurchasePriceInputDTO; +import lotto.domain.dto.WinningLottoInputDTO; + +public class View { + private static final InputView inputView = new InputView(); + private static final OutputView outputView = new OutputView(); + + public static PurchasePriceInputDTO getPurchaseCost() { + return inputView.getPurchaseCost(); + } + + public static WinningLottoInputDTO getWinningLottoAndBonus() { + return inputView.getWinningLottoAndBonus(); + } + + public static void printLottoCount(PurchaseCount purchaseCount) { + outputView.printLottoCount(purchaseCount); + } + + public static void printLottoSet(RandomLottoSet lottoSet) { + outputView.printLottoSet(lottoSet); + } + + public static void printLottoStatistics(LottoStatistics lottoStatistics) { + outputView.printLottoStatistic(lottoStatistics); + } +} From 016863a61b752a8f6ea010bcecb7ae7a39e5560a Mon Sep 17 00:00:00 2001 From: chunghyeon-kim Date: Wed, 7 Jul 2021 21:30:52 +0900 Subject: [PATCH 25/55] =?UTF-8?q?refactor:=20DefaultNumbers=20=ED=95=9C=20?= =?UTF-8?q?=EB=B2=88=EB=A7=8C=20=EC=83=9D=EC=84=B1=ED=95=98=EB=8F=84?= =?UTF-8?q?=EB=A1=9D=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- src/main/java/lotto/domain/DefaultNumbers.java | 17 +++++++++++++++++ src/main/java/lotto/domain/LottoNumber.java | 4 ++-- src/main/java/lotto/domain/RandomLotto.java | 10 +++------- src/main/java/lotto/domain/WinningLotto.java | 2 +- .../java/lotto/domain/LottoStatisticsTest.java | 9 ++++++--- src/test/java/lotto/domain/PrizeCountTest.java | 3 --- 7 files changed, 30 insertions(+), 17 deletions(-) create mode 100644 src/main/java/lotto/domain/DefaultNumbers.java diff --git a/README.md b/README.md index 3783cad5..4868d3ff 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ - [x] 공백 처리 - [x] 구매 개수 구하기 - [x] 랜덤 로또번호 구매 개수만큼 만들기 - - [ ] defaultNumberSet 1번만 생성되도록 변경 + - [x] defaultNumberSet 1번만 생성되도록 변경 - [x] RandomLottoTest 상수 리팩토링 - [x] PurchaseCount의 1000 접근 - [x] 지난 주 당첨 번호 입력받기 diff --git a/src/main/java/lotto/domain/DefaultNumbers.java b/src/main/java/lotto/domain/DefaultNumbers.java new file mode 100644 index 00000000..b71d8b16 --- /dev/null +++ b/src/main/java/lotto/domain/DefaultNumbers.java @@ -0,0 +1,17 @@ +package lotto.domain; + +import lombok.Getter; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.stream.IntStream; + +public class DefaultNumbers { + @Getter + private static final List defaultNumbers = Collections.unmodifiableList(new ArrayList() {{ + IntStream + .range(LottoNumber.LOWER_BOUND, LottoNumber.UPPER_BOUND) + .forEach(this::add); + }}); +} diff --git a/src/main/java/lotto/domain/LottoNumber.java b/src/main/java/lotto/domain/LottoNumber.java index 3f0abc65..95b0442d 100644 --- a/src/main/java/lotto/domain/LottoNumber.java +++ b/src/main/java/lotto/domain/LottoNumber.java @@ -8,8 +8,8 @@ public class LottoNumber { - private static final int LOWER_BOUND = 1; - private static final int UPPER_BOUND = 45; + public static final int LOWER_BOUND = 1; + public static final int UPPER_BOUND = 45; @Getter private final int lottoNumber; diff --git a/src/main/java/lotto/domain/RandomLotto.java b/src/main/java/lotto/domain/RandomLotto.java index 6af69984..880d232a 100644 --- a/src/main/java/lotto/domain/RandomLotto.java +++ b/src/main/java/lotto/domain/RandomLotto.java @@ -19,13 +19,9 @@ public RandomLotto() { } private static List generateRandomLottoNumbers() { - ArrayList defaultNumberSet = new ArrayList<>(); - for (int i = 0; i < 44; i++) { - defaultNumberSet.add(i+1); - } - - Collections.shuffle(defaultNumberSet); - return defaultNumberSet.subList(INDEX_LOWER_BOUND, INDEX_UPPER_BOUND) + List defaultNumbers = new ArrayList<>(DefaultNumbers.getDefaultNumbers()); + Collections.shuffle(defaultNumbers); + return defaultNumbers.subList(INDEX_LOWER_BOUND, INDEX_UPPER_BOUND) .stream() .map(String::valueOf) .map(LottoNumber::new) diff --git a/src/main/java/lotto/domain/WinningLotto.java b/src/main/java/lotto/domain/WinningLotto.java index f28c3848..c39d7f69 100644 --- a/src/main/java/lotto/domain/WinningLotto.java +++ b/src/main/java/lotto/domain/WinningLotto.java @@ -14,6 +14,7 @@ public class WinningLotto extends Lotto { public WinningLotto(String winningNumberInput, String bonusNumberInput) { super(LottoParser.generateLotto(winningNumberInput)); this.bonusNumber = new LottoNumber(bonusNumberInput); + this.lottoNumbers.sort(Comparator.comparingInt(LottoNumber::getLottoNumber)); } public PrizeCondition findPrize(Lotto targetLotto) { @@ -26,7 +27,6 @@ private boolean isBonusMatch(Lotto targetLotto) { private int getMatchNumbersCount(Lotto targetLotto) { targetLotto.getLottoNumbers().sort(Comparator.comparingInt(LottoNumber::getLottoNumber)); - this.lottoNumbers.sort(Comparator.comparingInt(LottoNumber::getLottoNumber)); int targetIdx = 0, winningIdx = 0; int matchNumbersCount = 0; while (targetIdx < targetLotto.lottoNumbers.size() && winningIdx < this.lottoNumbers.size()) { diff --git a/src/test/java/lotto/domain/LottoStatisticsTest.java b/src/test/java/lotto/domain/LottoStatisticsTest.java index 24fdcb69..69bcc4b8 100644 --- a/src/test/java/lotto/domain/LottoStatisticsTest.java +++ b/src/test/java/lotto/domain/LottoStatisticsTest.java @@ -20,8 +20,11 @@ public class LottoStatisticsTest { @Test @DisplayName("당첨 로또 개수를 받으면 총 상금액을 반환한다") void testLottoStatistics() { - //given, when, then - assertThat(LottoStatistics.calcProfitRate(PRIZE_COUNT, new PurchaseCount(String.valueOf(PURCHASE_MONEY)))) - .isEqualTo(RESULT); + //given, when + LottoStatistics lottoStatistics = new LottoStatistics(PRIZE_COUNT, + new PurchaseCount(String.valueOf(PURCHASE_MONEY))); + + // then + assertThat(lottoStatistics.calcProfitRate()).isEqualTo(RESULT); } } diff --git a/src/test/java/lotto/domain/PrizeCountTest.java b/src/test/java/lotto/domain/PrizeCountTest.java index 2a3e6b3f..ee616052 100644 --- a/src/test/java/lotto/domain/PrizeCountTest.java +++ b/src/test/java/lotto/domain/PrizeCountTest.java @@ -1,13 +1,10 @@ package lotto.domain; -import lotto.parser.LottoParser; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; -import java.util.HashSet; - public class PrizeCountTest { @Test From 3b933953d88179527e15eea25d8dbb4beff7df6f Mon Sep 17 00:00:00 2001 From: chunghyeon-kim Date: Wed, 7 Jul 2021 21:54:39 +0900 Subject: [PATCH 26/55] =?UTF-8?q?refactor:=20Test=20=EB=A9=94=EC=84=9C?= =?UTF-8?q?=EB=93=9C=20DisplayName=20=EC=A0=95=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/lotto/domain/Lotto.java | 1 + .../lotto/constant/PrizeConditionTest.java | 43 ++++++++++++++- .../lotto/constant/PurchaseCountTest.java | 10 ++-- .../java/lotto/domain/LottoNumberTest.java | 9 ++-- .../lotto/domain/LottoStatisticsTest.java | 4 +- .../java/lotto/domain/RandomLottoSetTest.java | 5 +- .../java/lotto/domain/RandomLottoTest.java | 4 +- .../java/lotto/domain/WinningLottoTest.java | 54 +++++++------------ 8 files changed, 78 insertions(+), 52 deletions(-) diff --git a/src/main/java/lotto/domain/Lotto.java b/src/main/java/lotto/domain/Lotto.java index a79f05ae..eed590c7 100644 --- a/src/main/java/lotto/domain/Lotto.java +++ b/src/main/java/lotto/domain/Lotto.java @@ -7,6 +7,7 @@ public class Lotto { public static final int PRICE = 1000; + public static final int LOTTO_NUMBER_SIZE = 6; @Getter protected final List lottoNumbers; diff --git a/src/test/java/lotto/constant/PrizeConditionTest.java b/src/test/java/lotto/constant/PrizeConditionTest.java index e780d970..41e134f6 100644 --- a/src/test/java/lotto/constant/PrizeConditionTest.java +++ b/src/test/java/lotto/constant/PrizeConditionTest.java @@ -8,7 +8,7 @@ public class PrizeConditionTest { @Test - @DisplayName("숫자가 6개 일치하 1등") + @DisplayName("숫자가 6개 일치할 때 1등") void firstPrize() { //given int matchNumbersCount = 6; @@ -35,7 +35,7 @@ void secondPrize() { } @Test - @DisplayName("숫자가 5개 일치하고 보너스 번호가 틀 때 3등") + @DisplayName("숫자가 5개 일치하고 보너스 번호가 일치하지 않을 때 3등") void thirdPrize() { //given int matchNumbersCount = 5; @@ -71,4 +71,43 @@ void fifthPrize() { assertThat(PrizeCondition.findPrize(matchNumbersCount, true)) .isEqualTo(PrizeCondition.FIFTH); } + + @Test + @DisplayName("숫자가 2개 일치할 때 꽝") + void loseMatchesTwo() { + //given + int matchNumbersCount = 2; + + //when, then + assertThat(PrizeCondition.findPrize(matchNumbersCount, false)) + .isEqualTo(PrizeCondition.LOSE); + assertThat(PrizeCondition.findPrize(matchNumbersCount, true)) + .isEqualTo(PrizeCondition.LOSE); + } + + @Test + @DisplayName("숫자가 1개 일치할 때 꽝") + void loseMatchesOne() { + //given + int matchNumbersCount = 1; + + //when, then + assertThat(PrizeCondition.findPrize(matchNumbersCount, false)) + .isEqualTo(PrizeCondition.LOSE); + assertThat(PrizeCondition.findPrize(matchNumbersCount, true)) + .isEqualTo(PrizeCondition.LOSE); + } + + @Test + @DisplayName("숫자가 0개 일치할 때 꽝") + void lose() { + //given + int matchNumbersCount = 0; + + //when, then + assertThat(PrizeCondition.findPrize(matchNumbersCount, false)) + .isEqualTo(PrizeCondition.LOSE); + assertThat(PrizeCondition.findPrize(matchNumbersCount, true)) + .isEqualTo(PrizeCondition.LOSE); + } } diff --git a/src/test/java/lotto/constant/PurchaseCountTest.java b/src/test/java/lotto/constant/PurchaseCountTest.java index 2ecd143c..f0eea50f 100644 --- a/src/test/java/lotto/constant/PurchaseCountTest.java +++ b/src/test/java/lotto/constant/PurchaseCountTest.java @@ -12,7 +12,7 @@ public class PurchaseCountTest { @Test - @DisplayName("") + @DisplayName("구매금액 입력값이 1000 미만이면 예외를 던진다") void testInputValueUnder1000() throws Exception { // given String input = "800"; @@ -24,7 +24,7 @@ void testInputValueUnder1000() throws Exception { } @Test - @DisplayName("") + @DisplayName("구매금액 입력값이 1000 이상이면 정상처리된다") void testInputValueNotUnder1000() throws Exception { // given String input = "2000"; @@ -34,7 +34,7 @@ void testInputValueNotUnder1000() throws Exception { } @Test - @DisplayName("") + @DisplayName("구매금액 입력값이 1000의 배수가 아니면 예외를 던진다") void testInputValueNotMultiple1000() throws Exception { // given String input = "1234"; @@ -45,7 +45,7 @@ void testInputValueNotMultiple1000() throws Exception { } @Test - @DisplayName("") + @DisplayName("구매금액 입력값이 숫자가 아니면 예외를 던진다") void testInputNotNumber() throws Exception { // given String input = "error"; @@ -58,7 +58,7 @@ void testInputNotNumber() throws Exception { } @Test - @DisplayName("공백이 오른쪽에 있을 때 정상 처리된다") + @DisplayName("공백이 왼쪽에 있을 때 정상 처리된다") void testPriceInputWithLeftBlank() throws Exception { // given String input = " 1000"; diff --git a/src/test/java/lotto/domain/LottoNumberTest.java b/src/test/java/lotto/domain/LottoNumberTest.java index 111ad8ec..2e084c51 100644 --- a/src/test/java/lotto/domain/LottoNumberTest.java +++ b/src/test/java/lotto/domain/LottoNumberTest.java @@ -12,7 +12,7 @@ public class LottoNumberTest { @Test - @DisplayName("주어진 1~45 숫자로 LottoNumber 객체 생성") + @DisplayName("주어진 1~45 숫자로 LottoNumber 객체를 생성한다") void testGenerateLottoNumberWithInteger() { // given List inputNumbers = new ArrayList<>(); @@ -29,12 +29,13 @@ void testGenerateLottoNumberWithInteger() { // then for (int i = 0; i lottoNumbers = new ArrayList<>(); - lottoNumbers.add(new LottoNumber("1")); - lottoNumbers.add(new LottoNumber("2")); - lottoNumbers.add(new LottoNumber("3")); - lottoNumbers.add(new LottoNumber("4")); - lottoNumbers.add(new LottoNumber("5")); - lottoNumbers.add(new LottoNumber("6")); - targetLotto = new Lotto(lottoNumbers); - } - - @Test - @DisplayName("WinningNumbers 타입 확인") - public void test() throws Exception { - // given, when, then - assertThat(winningLotto.getLottoNumbers()).isExactlyInstanceOf(ArrayList.class); + targetLotto = new Lotto(LottoParser.generateLotto("1, 2, 3, 4, 5, 6")); } @Test - @DisplayName("숫자가 6개인지 확인") - public void testWinningNumberSize() throws Exception { + @DisplayName("숫자가 6개인지 확인한다") + void testWinningNumberSize() throws Exception { // given, when, then - assertThat(winningLotto.getLottoNumbers().size()).isEqualTo(6); + assertThat(winningLotto.getLottoNumbers().size()).isEqualTo(Lotto.LOTTO_NUMBER_SIZE); } - @Test - @DisplayName("숫자가 아닌 값 포함") - public void testInputWithNonInteger() throws Exception { + @DisplayName("숫자가 아닌 값 포함할 때 예외를 던진다") + void testInputWithNonInteger() throws Exception { // given String input = "1, a, 3, 4, 5, 6"; String bonus = "10"; @@ -59,8 +42,8 @@ public void testInputWithNonInteger() throws Exception { } @Test - @DisplayName("로또 번호 입력값이 45 초과면 예외 발생") - public void testInputGreaterThanUpperBoundary() throws Exception { + @DisplayName("45 초과인 값 포함할 때 예외를 던진다") + void testInputGreaterThanUpperBoundary() throws Exception { // given String input = "1, 2, 3, 4, 5, 46"; String bonus = "10"; @@ -72,8 +55,8 @@ public void testInputGreaterThanUpperBoundary() throws Exception { } @Test - @DisplayName("로또 번호 입력값이 1 미만이면 예외 발생") - public void testInputSmallerThanLowerBoundary() throws Exception { + @DisplayName("1 미만인 값 포함할 때 예외를 던진다") + void testInputSmallerThanLowerBoundary() throws Exception { // given String input = "0, 2, 3, 4, 5, 6"; String bonus = "10"; @@ -85,14 +68,15 @@ public void testInputSmallerThanLowerBoundary() throws Exception { } @Test - @DisplayName("보너스 번호 입력값을 확인") + @DisplayName("보너스 번호 입력값을 확인한다") void testBonusInput() { //given, when, then - assertThat(winningLotto.getBonusNumber().getLottoNumber()).isEqualTo(10); + assertThat(winningLotto.getBonusNumber().getLottoNumber()) + .isEqualTo(Integer.parseInt(bonusNumberInput)); } @Test - @DisplayName("보너스 미포함 6개 맞으면 1등") + @DisplayName("숫자가 6개 일치할 때 1등") void testFindPrize_firstWithoutBonus() { //given, when PrizeCondition prize = winningLotto.findPrize(targetLotto); @@ -102,7 +86,7 @@ void testFindPrize_firstWithoutBonus() { } @Test - @DisplayName("5개 + 보너스 맞으면 2등") + @DisplayName("숫자가 5개 일치하고 보너스번호도 일치할 때 2등") void testFindPrize_secondWithBonus() { //given, when WinningLotto winningLotto = new WinningLotto("1,2,3,4,5,7", "6"); @@ -113,7 +97,7 @@ void testFindPrize_secondWithBonus() { } @Test - @DisplayName("5개 + 보너스 안 맞으면 3등") + @DisplayName("숫자가 5개 일치하고 보너스 번호가 일치하지 않을 때 3등") void testFindPrize_third() { //given, when WinningLotto winningLotto = new WinningLotto("1,2,3,4,5,7", bonusNumberInput); @@ -124,7 +108,7 @@ void testFindPrize_third() { } @Test - @DisplayName("보너스 미포함 4개 맞으면 4등") + @DisplayName("숫자가 4개 일치할 때 4등") void testFindPrize_fourth() { //given, when WinningLotto winningLotto = new WinningLotto("4,3,2,1,8,7", bonusNumberInput); @@ -135,7 +119,7 @@ void testFindPrize_fourth() { } @Test - @DisplayName("보너스 미포함 3개 맞으면 5등") + @DisplayName("숫자가 3개 일치할 때 5등") void testFindPrize_fifth() { //given, when WinningLotto winningLotto = new WinningLotto("4,3,2,9,8,7", bonusNumberInput); From ddff79ae8c61dde7676559b423d37d29afef6ea4 Mon Sep 17 00:00:00 2001 From: chunghyeon-kim Date: Wed, 7 Jul 2021 23:41:07 +0900 Subject: [PATCH 27/55] =?UTF-8?q?refactor:=20DefaultNumbers=20=EC=A7=80?= =?UTF-8?q?=EB=84=A4=EB=A6=AD=EC=8A=A4=20=EC=88=98=EC=A0=95,=20=ED=95=84?= =?UTF-8?q?=EC=9A=94=EC=97=86=EB=8A=94=20=ED=81=B4=EB=9E=98=EC=8A=A4?= =?UTF-8?q?=ED=8C=8C=EC=9D=BC=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/lotto/LottoController.java | 10 ---------- src/main/java/lotto/domain/DefaultNumbers.java | 2 +- src/main/java/lotto/domain/RandomLotto.java | 2 -- 3 files changed, 1 insertion(+), 13 deletions(-) delete mode 100644 src/main/java/lotto/LottoController.java diff --git a/src/main/java/lotto/LottoController.java b/src/main/java/lotto/LottoController.java deleted file mode 100644 index e2a0bee6..00000000 --- a/src/main/java/lotto/LottoController.java +++ /dev/null @@ -1,10 +0,0 @@ -package lotto; - -import lotto.domain.PurchaseCount; - -public class LottoController { - public void validate(String input) { - PurchaseCount purchaseCount = new PurchaseCount(input); - - } -} diff --git a/src/main/java/lotto/domain/DefaultNumbers.java b/src/main/java/lotto/domain/DefaultNumbers.java index b71d8b16..abab9957 100644 --- a/src/main/java/lotto/domain/DefaultNumbers.java +++ b/src/main/java/lotto/domain/DefaultNumbers.java @@ -9,7 +9,7 @@ public class DefaultNumbers { @Getter - private static final List defaultNumbers = Collections.unmodifiableList(new ArrayList() {{ + private static final List defaultNumbers = Collections.unmodifiableList(new ArrayList() {{ IntStream .range(LottoNumber.LOWER_BOUND, LottoNumber.UPPER_BOUND) .forEach(this::add); diff --git a/src/main/java/lotto/domain/RandomLotto.java b/src/main/java/lotto/domain/RandomLotto.java index 880d232a..a65ea341 100644 --- a/src/main/java/lotto/domain/RandomLotto.java +++ b/src/main/java/lotto/domain/RandomLotto.java @@ -1,8 +1,6 @@ package lotto.domain; import lombok.Getter; -import lotto.domain.Lotto; -import lotto.domain.LottoNumber; import java.util.ArrayList; import java.util.Collections; From e0d1880c1799922cf34e2c70cf8a12609b102ecd Mon Sep 17 00:00:00 2001 From: Jiwoo Kim Date: Thu, 8 Jul 2021 00:47:47 +0900 Subject: [PATCH 28/55] =?UTF-8?q?refactor:=20View=20=EB=A1=9C=EC=A7=81?= =?UTF-8?q?=EA=B3=BC=20=EA=B0=80=EB=8F=85=EC=84=B1=20=EA=B0=9C=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/lotto/view/InputView.java | 24 +++++----- src/main/java/lotto/view/OutputView.java | 60 +++++++++++++++++------- src/main/java/lotto/view/View.java | 14 +++++- 3 files changed, 65 insertions(+), 33 deletions(-) diff --git a/src/main/java/lotto/view/InputView.java b/src/main/java/lotto/view/InputView.java index f8b40887..03d72c3b 100644 --- a/src/main/java/lotto/view/InputView.java +++ b/src/main/java/lotto/view/InputView.java @@ -1,26 +1,24 @@ package lotto.view; -import lotto.domain.dto.PurchasePriceInputDTO; -import lotto.domain.dto.WinningLottoInputDTO; - import java.util.Scanner; public class InputView { + private final Scanner scanner = new Scanner(System.in); - public PurchasePriceInputDTO getPurchaseCost() { - System.out.println("구입금액을 입력해 주세요."); - String purchaseCountInput = scanner.nextLine(); + private String nextLine() { + return scanner.nextLine(); + } - return new PurchasePriceInputDTO(purchaseCountInput); + public String getPurchaseCost() { + return nextLine(); } - public WinningLottoInputDTO getWinningLottoAndBonus() { - System.out.println("지난 주 당첨 번호를 입력해 주세요."); - String winningLottoNumbers = scanner.nextLine(); + public String getWinningLottoNumbers() { + return nextLine(); + } - System.out.println("보너스 볼을 입력해 주세요."); - String winningLottoBonus = scanner.nextLine(); - return new WinningLottoInputDTO(winningLottoNumbers, winningLottoBonus); + public String getWinningLottoBonus() { + return nextLine(); } } diff --git a/src/main/java/lotto/view/OutputView.java b/src/main/java/lotto/view/OutputView.java index 28c17dfd..26f8f4c4 100644 --- a/src/main/java/lotto/view/OutputView.java +++ b/src/main/java/lotto/view/OutputView.java @@ -6,10 +6,18 @@ public class OutputView { private static final String COMMA = ", "; + private static final String PARENTHESIS_LEFT = "["; + private static final String PARENTHESIS_RIGHT = "]"; + private static final String UNIT = "개"; + private static final String NEW_LINE = "\n"; + + private void print(String contents) { + System.out.println(contents); + } public void printLottoCount(PurchaseCount purchaseCount) { int count = purchaseCount.getPurchaseCount(); - System.out.println(count + "개를 구매했습니다."); + print(count + "개를 구매했습니다."); } public void printLottoSet(RandomLottoSet lottoSet) { @@ -19,31 +27,47 @@ public void printLottoSet(RandomLottoSet lottoSet) { } private void printLotto(Lotto lotto) { - StringBuilder sb = new StringBuilder(); - sb.append("["); + String result = PARENTHESIS_LEFT; for (LottoNumber lottoNumber : lotto.getLottoNumbers()) { - sb.append(lottoNumber.getLottoNumber()).append(COMMA); + result += lottoNumber.getLottoNumber() + COMMA; } - System.out.println(removeCommaAtTheEnd(sb.toString()) + "]"); + result = removeCommaAtTheEnd(result) + PARENTHESIS_RIGHT; + + print(result); } private String removeCommaAtTheEnd(String str) { return str.substring(0, str.length() - COMMA.length()); } + public void askPurchaseCost() { + print("구입금액을 입력해 주세요."); + } + + public void askWinningLottoNumbers() { + print("지난 주 당첨 번호를 입력해 주세요."); + } + + public void askWinningLottoBonus() { + print("보너스 볼을 입력해 주세요."); + } + public void printLottoStatistic(LottoStatistics lottoStatistics) { - System.out.println("당첨 통계"); - System.out.println("---------"); - System.out.print(PrizeMessage.FIFTH.getMessage()); - System.out.println(lottoStatistics.getPrizeCount().getCountFifth() + "개"); - System.out.print(PrizeMessage.FOURTH.getMessage()); - System.out.println(lottoStatistics.getPrizeCount().getCountFourth() + "개"); - System.out.print(PrizeMessage.THIRD.getMessage()); - System.out.println(lottoStatistics.getPrizeCount().getCountThird() + "개"); - System.out.print(PrizeMessage.SECOND.getMessage()); - System.out.println(lottoStatistics.getPrizeCount().getCountSecond() + "개"); - System.out.print(PrizeMessage.FIRST.getMessage()); - System.out.println(lottoStatistics.getPrizeCount().getCountFirst() + "개"); - System.out.println("총 수익률은 " + lottoStatistics.calcProfitRate() + "입니다."); + String result = "당첨 통계" + "---------" + NEW_LINE + + generatePrizeMessage(PrizeMessage.FIFTH, lottoStatistics.getPrizeCount().getCountFifth()) + + generatePrizeMessage(PrizeMessage.FOURTH, lottoStatistics.getPrizeCount().getCountFourth()) + + generatePrizeMessage(PrizeMessage.THIRD, lottoStatistics.getPrizeCount().getCountThird()) + + generatePrizeMessage(PrizeMessage.SECOND, lottoStatistics.getPrizeCount().getCountSecond()) + + generatePrizeMessage(PrizeMessage.FIRST, lottoStatistics.getPrizeCount().getCountFirst()) + + generateProfitRateMessage(lottoStatistics.calcProfitRate()); + print(result); + } + + private String generatePrizeMessage(PrizeMessage prizeMessage, int prizeCount) { + return prizeMessage.getMessage() + prizeCount + UNIT + NEW_LINE; + } + + private String generateProfitRateMessage(double profitRate) { + return "총 수익률은 " + profitRate + "입니다."; } } diff --git a/src/main/java/lotto/view/View.java b/src/main/java/lotto/view/View.java index 1e1144bc..e75e124c 100644 --- a/src/main/java/lotto/view/View.java +++ b/src/main/java/lotto/view/View.java @@ -7,15 +7,25 @@ import lotto.domain.dto.WinningLottoInputDTO; public class View { + private static final InputView inputView = new InputView(); private static final OutputView outputView = new OutputView(); public static PurchasePriceInputDTO getPurchaseCost() { - return inputView.getPurchaseCost(); + outputView.askPurchaseCost(); + String purchaseCountInput = inputView.getPurchaseCost(); + + return new PurchasePriceInputDTO(purchaseCountInput); } public static WinningLottoInputDTO getWinningLottoAndBonus() { - return inputView.getWinningLottoAndBonus(); + outputView.askWinningLottoNumbers(); + String winningLottoNumbers = inputView.getWinningLottoNumbers(); + + outputView.askWinningLottoBonus(); + String winningLottoBonus = inputView.getWinningLottoBonus(); + + return new WinningLottoInputDTO(winningLottoNumbers, winningLottoBonus); } public static void printLottoCount(PurchaseCount purchaseCount) { From f4d2c40b6a43ea994d58fcccd82968d7b4954ee1 Mon Sep 17 00:00:00 2001 From: Jiwoo Kim Date: Thu, 8 Jul 2021 01:56:32 +0900 Subject: [PATCH 29/55] =?UTF-8?q?refactor:=20=EB=A6=AC=ED=8C=A9=ED=86=A0?= =?UTF-8?q?=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/lotto/constant/PrizeCondition.java | 1 - .../lotto/controller/LottoController.java | 36 +++++++++-------- .../java/lotto/domain/DefaultNumbers.java | 4 +- src/main/java/lotto/domain/Lotto.java | 2 +- src/main/java/lotto/domain/LottoNumber.java | 17 ++++---- src/main/java/lotto/domain/LottoSet.java | 3 +- .../java/lotto/domain/LottoStatistics.java | 10 +++-- src/main/java/lotto/domain/PrizeCount.java | 17 ++++---- src/main/java/lotto/domain/PurchaseCount.java | 18 ++++++--- src/main/java/lotto/domain/RandomLotto.java | 6 +-- src/main/java/lotto/domain/WinningLotto.java | 24 +++++------ .../domain/dto/PurchasePriceInputDTO.java | 2 + .../lotto/domain/dto/PurchaseResultDTO.java | 16 ++++++++ .../lotto/domain/dto/StatisticsResultDTO.java | 15 +++++++ .../domain/dto/WinningLottoInputDTO.java | 2 + .../lotto/exception/ExceptionMessage.java | 18 +++++++++ src/main/java/lotto/parser/LottoParser.java | 18 ++++++++- src/main/java/lotto/service/LottoService.java | 40 +++++++++++++++++++ .../java/lotto/util/NumberValidateUtils.java | 2 +- src/main/java/lotto/view/OutputView.java | 14 +++++-- src/main/java/lotto/view/View.java | 31 ++++++++------ .../lotto/constant/PurchaseCountTest.java | 2 +- src/test/java/lotto/domain/TestLottoSet.java | 40 +++++++++---------- .../java/lotto/domain/TestWinningLotto.java | 1 + .../java/lotto/domain/WinningLottoTest.java | 25 +++++++++--- 25 files changed, 255 insertions(+), 109 deletions(-) create mode 100644 src/main/java/lotto/domain/dto/PurchaseResultDTO.java create mode 100644 src/main/java/lotto/domain/dto/StatisticsResultDTO.java create mode 100644 src/main/java/lotto/exception/ExceptionMessage.java create mode 100644 src/main/java/lotto/service/LottoService.java diff --git a/src/main/java/lotto/constant/PrizeCondition.java b/src/main/java/lotto/constant/PrizeCondition.java index 92f3e74c..a09f3303 100644 --- a/src/main/java/lotto/constant/PrizeCondition.java +++ b/src/main/java/lotto/constant/PrizeCondition.java @@ -17,7 +17,6 @@ public enum PrizeCondition { this.isBonus = isBonus; } - public static PrizeCondition findPrize(int matchNumbersCount, boolean isBonus) { if (matchNumbersCount <= LOSE.matchNumbersCount) { return LOSE; diff --git a/src/main/java/lotto/controller/LottoController.java b/src/main/java/lotto/controller/LottoController.java index fb470805..1db4d987 100644 --- a/src/main/java/lotto/controller/LottoController.java +++ b/src/main/java/lotto/controller/LottoController.java @@ -1,30 +1,32 @@ package lotto.controller; -import lotto.domain.*; +import lotto.domain.dto.StatisticsResultDTO; import lotto.domain.dto.PurchasePriceInputDTO; +import lotto.domain.dto.PurchaseResultDTO; import lotto.domain.dto.WinningLottoInputDTO; +import lotto.service.LottoService; import lotto.view.View; public class LottoController { - public static void main(String[] args) { - start(); - } - - public static void start() { - PurchasePriceInputDTO purchasePriceInputDTO = View.getPurchaseCost(); - PurchaseCount purchaseCount = new PurchaseCount(purchasePriceInputDTO.getInput()); - RandomLottoSet randomLottoSet = new RandomLottoSet(purchaseCount); + private static final View view = new View(); + private static final LottoService lottoService = new LottoService(); - View.printLottoCount(purchaseCount); - View.printLottoSet(randomLottoSet); + public static void main(String[] args) { + try { + start(); + } catch (IllegalArgumentException e) { + view.printException(e.getMessage()); + } + } - WinningLottoInputDTO winningLottoInputDTO = View.getWinningLottoAndBonus(); - WinningLotto winningLotto = new WinningLotto( - winningLottoInputDTO.getWinningLottoNumbers(), - winningLottoInputDTO.getWinningLottoBonus()); + public static void start() throws IllegalArgumentException { + PurchasePriceInputDTO purchasePriceInputDTO = view.getPurchaseCost(); + PurchaseResultDTO purchaseResultDTO = lottoService.purchase(purchasePriceInputDTO); + view.printLottoPurchaseResult(purchaseResultDTO); - LottoStatistics lottoStatistics = new LottoStatistics(new PrizeCount(randomLottoSet, winningLotto), purchaseCount); - View.printLottoStatistics(lottoStatistics); + WinningLottoInputDTO winningLottoInputDTO = view.getWinningLottoAndBonus(); + StatisticsResultDTO statisticsResultDTO = lottoService.calcResult(purchaseResultDTO, winningLottoInputDTO); + view.printLottoStatistics(statisticsResultDTO); } } diff --git a/src/main/java/lotto/domain/DefaultNumbers.java b/src/main/java/lotto/domain/DefaultNumbers.java index abab9957..7692faea 100644 --- a/src/main/java/lotto/domain/DefaultNumbers.java +++ b/src/main/java/lotto/domain/DefaultNumbers.java @@ -8,10 +8,10 @@ import java.util.stream.IntStream; public class DefaultNumbers { + @Getter private static final List defaultNumbers = Collections.unmodifiableList(new ArrayList() {{ - IntStream - .range(LottoNumber.LOWER_BOUND, LottoNumber.UPPER_BOUND) + IntStream.range(LottoNumber.LOWER_BOUND, LottoNumber.UPPER_BOUND) .forEach(this::add); }}); } diff --git a/src/main/java/lotto/domain/Lotto.java b/src/main/java/lotto/domain/Lotto.java index eed590c7..513132a8 100644 --- a/src/main/java/lotto/domain/Lotto.java +++ b/src/main/java/lotto/domain/Lotto.java @@ -10,7 +10,7 @@ public class Lotto { public static final int LOTTO_NUMBER_SIZE = 6; @Getter - protected final List lottoNumbers; + protected List lottoNumbers; public Lotto(List lottoNumbers) { this.lottoNumbers = lottoNumbers; diff --git a/src/main/java/lotto/domain/LottoNumber.java b/src/main/java/lotto/domain/LottoNumber.java index 95b0442d..38849b72 100644 --- a/src/main/java/lotto/domain/LottoNumber.java +++ b/src/main/java/lotto/domain/LottoNumber.java @@ -4,6 +4,8 @@ import java.util.Objects; +import static lotto.exception.ExceptionMessage.NON_INTEGER_INPUT_FOR_LOTTO_NUMBER; +import static lotto.exception.ExceptionMessage.OUT_OF_BOUND_INPUT_FOR_LOTTO_NUMBER; import static lotto.util.NumberValidateUtils.isInteger; public class LottoNumber { @@ -14,21 +16,20 @@ public class LottoNumber { @Getter private final int lottoNumber; - public LottoNumber(String lottoNumber) { - validate(lottoNumber); + public LottoNumber(String input) throws IllegalArgumentException { + validate(input); - this.lottoNumber = Integer.parseInt(lottoNumber); + this.lottoNumber = Integer.parseInt(input); } - private void validate(String input) { - if(!isInteger(input)) { - throw new IllegalArgumentException("숫자를 입력해주세요."); + private void validate(String input) throws IllegalArgumentException { + if (!isInteger(input)) { + throw new IllegalArgumentException(NON_INTEGER_INPUT_FOR_LOTTO_NUMBER.getMessage()); } int lottoNumber = Integer.parseInt(input); - if (isOutOfBound(lottoNumber)) { - throw new IllegalArgumentException("1~45 값을 입력해주세요"); + throw new IllegalArgumentException(OUT_OF_BOUND_INPUT_FOR_LOTTO_NUMBER.getMessage()); } } diff --git a/src/main/java/lotto/domain/LottoSet.java b/src/main/java/lotto/domain/LottoSet.java index 2cd72f31..18e52ff0 100644 --- a/src/main/java/lotto/domain/LottoSet.java +++ b/src/main/java/lotto/domain/LottoSet.java @@ -2,6 +2,7 @@ import lombok.Getter; +import java.util.Collections; import java.util.Set; public class LottoSet { @@ -10,6 +11,6 @@ public class LottoSet { private final Set lottoSet; public LottoSet(Set lottoSet) { - this.lottoSet = lottoSet; + this.lottoSet = Collections.unmodifiableSet(lottoSet); } } diff --git a/src/main/java/lotto/domain/LottoStatistics.java b/src/main/java/lotto/domain/LottoStatistics.java index 0c46b9f2..523f8d34 100644 --- a/src/main/java/lotto/domain/LottoStatistics.java +++ b/src/main/java/lotto/domain/LottoStatistics.java @@ -1,24 +1,26 @@ package lotto.domain; +import lombok.Builder; import lombok.Getter; import lotto.constant.PrizeMoney; public class LottoStatistics { @Getter - PrizeCount prizeCount; - PurchaseCount purchaseCount; + private final PrizeCount prizeCount; + private final PurchaseCount purchaseCount; + @Builder public LottoStatistics(PrizeCount prizeCount, PurchaseCount purchaseCount) { this.prizeCount = prizeCount; this.purchaseCount = purchaseCount; } public double calcProfitRate() { - return (double) calcSum() / (purchaseCount.getPurchaseCount() * Lotto.PRICE); + return (double) sumOfPrizeMoney() / (purchaseCount.getPurchaseCount() * Lotto.PRICE); } - private long calcSum() { + private long sumOfPrizeMoney() { return prizeCount.getCountFirst() * PrizeMoney.FIRST.getValue() + prizeCount.getCountSecond() * PrizeMoney.SECOND.getValue() + prizeCount.getCountThird() * PrizeMoney.THIRD.getValue() diff --git a/src/main/java/lotto/domain/PrizeCount.java b/src/main/java/lotto/domain/PrizeCount.java index 35ab99aa..76681964 100644 --- a/src/main/java/lotto/domain/PrizeCount.java +++ b/src/main/java/lotto/domain/PrizeCount.java @@ -1,40 +1,43 @@ package lotto.domain; +import lombok.Builder; import lombok.Getter; import lotto.constant.PrizeCondition; @Getter public class PrizeCount { + private int countFirst; private int countSecond; private int countThird; private int countFourth; private int countFifth; + @Builder public PrizeCount(LottoSet lottoset, WinningLotto winningLotto) { for (Lotto lotto : lottoset.getLottoSet()) { - updateCounts(winningLotto.findPrize(lotto)); + updateCounts(winningLotto.findPrizeCondition(lotto)); } } - private void updateCounts(PrizeCondition condition) { - if(condition.equals(PrizeCondition.FIRST)) { + private void updateCounts(PrizeCondition prizeCondition) { + if (prizeCondition.equals(PrizeCondition.FIRST)) { countFirst++; return; } - if(condition.equals(PrizeCondition.SECOND)) { + if (prizeCondition.equals(PrizeCondition.SECOND)) { countSecond++; return; } - if(condition.equals(PrizeCondition.THIRD)) { + if (prizeCondition.equals(PrizeCondition.THIRD)) { countThird++; return; } - if(condition.equals(PrizeCondition.FOURTH)) { + if (prizeCondition.equals(PrizeCondition.FOURTH)) { countFourth++; return; } - if(condition.equals(PrizeCondition.FIFTH)) { + if (prizeCondition.equals(PrizeCondition.FIFTH)) { countFifth++; } } diff --git a/src/main/java/lotto/domain/PurchaseCount.java b/src/main/java/lotto/domain/PurchaseCount.java index 6d67be07..9113f5c8 100644 --- a/src/main/java/lotto/domain/PurchaseCount.java +++ b/src/main/java/lotto/domain/PurchaseCount.java @@ -1,7 +1,10 @@ package lotto.domain; +import lombok.Builder; import lombok.Getter; +import static lotto.exception.ExceptionMessage.NON_INTEGER_INPUT_FOR_PURCHASE_MONEY; +import static lotto.exception.ExceptionMessage.NON_MULTIPLE_OF_LOTTO_PRICE_INPUT_FOR_PURCHASE_MONEY; import static lotto.util.NumberValidateUtils.isInteger; public class PurchaseCount { @@ -11,27 +14,30 @@ public class PurchaseCount { @Getter private final int purchaseCount; + @Builder public PurchaseCount(String input) { input = input.trim(); validate(input); + this.purchaseCount = Integer.parseInt(input) / Lotto.PRICE; } private void validate(String input) { if (!isInteger(input)) { - throw new IllegalArgumentException("잘못된 입력입니다"); + throw new IllegalArgumentException(NON_INTEGER_INPUT_FOR_PURCHASE_MONEY.getMessage()); } - int inputConversion = Integer.parseInt(input); - if (notMatchesCondition(inputConversion)) { - throw new IllegalArgumentException("입력값이 잘못되었습니다"); + + int purchaseCount = Integer.parseInt(input); + if (notMatchesCondition(purchaseCount)) { + throw new IllegalArgumentException(NON_MULTIPLE_OF_LOTTO_PRICE_INPUT_FOR_PURCHASE_MONEY.getMessage()); } } private boolean notMatchesCondition(int input) { - return input < MINIMUM_INPUT || notMultipleOfStandard(input); + return input < MINIMUM_INPUT || notMultipleOfLottoPrice(input); } - private boolean notMultipleOfStandard(int input) { + private boolean notMultipleOfLottoPrice(int input) { return input % Lotto.PRICE != 0; } } diff --git a/src/main/java/lotto/domain/RandomLotto.java b/src/main/java/lotto/domain/RandomLotto.java index a65ea341..e6841404 100644 --- a/src/main/java/lotto/domain/RandomLotto.java +++ b/src/main/java/lotto/domain/RandomLotto.java @@ -1,14 +1,12 @@ package lotto.domain; -import lombok.Getter; - import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.stream.Collectors; -@Getter public class RandomLotto extends Lotto { + private static final int INDEX_LOWER_BOUND = 0; private static final int INDEX_UPPER_BOUND = 6; @@ -16,7 +14,7 @@ public RandomLotto() { super(generateRandomLottoNumbers()); } - private static List generateRandomLottoNumbers() { + private static List generateRandomLottoNumbers() throws IllegalArgumentException { List defaultNumbers = new ArrayList<>(DefaultNumbers.getDefaultNumbers()); Collections.shuffle(defaultNumbers); return defaultNumbers.subList(INDEX_LOWER_BOUND, INDEX_UPPER_BOUND) diff --git a/src/main/java/lotto/domain/WinningLotto.java b/src/main/java/lotto/domain/WinningLotto.java index c39d7f69..d1d648c3 100644 --- a/src/main/java/lotto/domain/WinningLotto.java +++ b/src/main/java/lotto/domain/WinningLotto.java @@ -1,5 +1,6 @@ package lotto.domain; +import lombok.Builder; import lombok.Getter; import lotto.constant.PrizeCondition; import lotto.parser.LottoParser; @@ -9,22 +10,19 @@ public class WinningLotto extends Lotto { @Getter - private LottoNumber bonusNumber; + private final LottoNumber bonusNumber; - public WinningLotto(String winningNumberInput, String bonusNumberInput) { - super(LottoParser.generateLotto(winningNumberInput)); + @Builder + public WinningLotto(String winningNumberInput, String bonusNumberInput) throws IllegalArgumentException { + super(LottoParser.parseInputIntoLottoNumbers(winningNumberInput)); this.bonusNumber = new LottoNumber(bonusNumberInput); this.lottoNumbers.sort(Comparator.comparingInt(LottoNumber::getLottoNumber)); } - public PrizeCondition findPrize(Lotto targetLotto) { + public PrizeCondition findPrizeCondition(Lotto targetLotto) { return PrizeCondition.findPrize(getMatchNumbersCount(targetLotto), isBonusMatch(targetLotto)); } - private boolean isBonusMatch(Lotto targetLotto) { - return targetLotto.getLottoNumbers().contains(bonusNumber); - } - private int getMatchNumbersCount(Lotto targetLotto) { targetLotto.getLottoNumbers().sort(Comparator.comparingInt(LottoNumber::getLottoNumber)); int targetIdx = 0, winningIdx = 0; @@ -36,15 +34,17 @@ private int getMatchNumbersCount(Lotto targetLotto) { matchNumbersCount++; targetIdx++; winningIdx++; - } - else if (targetNumber > winningNumber) { + } else if (targetNumber > winningNumber) { winningIdx++; - } - else { + } else { targetIdx++; } } return matchNumbersCount; } + private boolean isBonusMatch(Lotto targetLotto) { + return targetLotto.getLottoNumbers().contains(bonusNumber); + } + } diff --git a/src/main/java/lotto/domain/dto/PurchasePriceInputDTO.java b/src/main/java/lotto/domain/dto/PurchasePriceInputDTO.java index 04843171..6cdfe562 100644 --- a/src/main/java/lotto/domain/dto/PurchasePriceInputDTO.java +++ b/src/main/java/lotto/domain/dto/PurchasePriceInputDTO.java @@ -1,9 +1,11 @@ package lotto.domain.dto; +import lombok.Builder; import lombok.Getter; import lombok.RequiredArgsConstructor; @Getter +@Builder @RequiredArgsConstructor public class PurchasePriceInputDTO { diff --git a/src/main/java/lotto/domain/dto/PurchaseResultDTO.java b/src/main/java/lotto/domain/dto/PurchaseResultDTO.java new file mode 100644 index 00000000..b7167e1a --- /dev/null +++ b/src/main/java/lotto/domain/dto/PurchaseResultDTO.java @@ -0,0 +1,16 @@ +package lotto.domain.dto; + +import lombok.Builder; +import lombok.Getter; +import lombok.RequiredArgsConstructor; +import lotto.domain.PurchaseCount; +import lotto.domain.RandomLottoSet; + +@Getter +@RequiredArgsConstructor +@Builder +public class PurchaseResultDTO { + + private final PurchaseCount purchaseCount; + private final RandomLottoSet randomLottoSet; +} diff --git a/src/main/java/lotto/domain/dto/StatisticsResultDTO.java b/src/main/java/lotto/domain/dto/StatisticsResultDTO.java new file mode 100644 index 00000000..c25c1e78 --- /dev/null +++ b/src/main/java/lotto/domain/dto/StatisticsResultDTO.java @@ -0,0 +1,15 @@ +package lotto.domain.dto; + +import lombok.Builder; +import lombok.Getter; +import lombok.RequiredArgsConstructor; +import lotto.domain.PrizeCount; + +@Getter +@RequiredArgsConstructor +@Builder +public class StatisticsResultDTO { + + private final PrizeCount prizeCount; + private final double profitRate; +} diff --git a/src/main/java/lotto/domain/dto/WinningLottoInputDTO.java b/src/main/java/lotto/domain/dto/WinningLottoInputDTO.java index 7e4d6690..3b558167 100644 --- a/src/main/java/lotto/domain/dto/WinningLottoInputDTO.java +++ b/src/main/java/lotto/domain/dto/WinningLottoInputDTO.java @@ -1,9 +1,11 @@ package lotto.domain.dto; +import lombok.Builder; import lombok.Getter; import lombok.RequiredArgsConstructor; @Getter +@Builder @RequiredArgsConstructor public class WinningLottoInputDTO { diff --git a/src/main/java/lotto/exception/ExceptionMessage.java b/src/main/java/lotto/exception/ExceptionMessage.java new file mode 100644 index 00000000..9e6c56e9 --- /dev/null +++ b/src/main/java/lotto/exception/ExceptionMessage.java @@ -0,0 +1,18 @@ +package lotto.exception; + +import lombok.Getter; +import lombok.RequiredArgsConstructor; +import lotto.domain.Lotto; + +@RequiredArgsConstructor +public enum ExceptionMessage { + + NON_INTEGER_INPUT_FOR_LOTTO_NUMBER("로또 번호는 숫자를 입력해주세요."), + OUT_OF_BOUND_INPUT_FOR_LOTTO_NUMBER("로또 번호는 1 이상 45 이하의 값을 입력해주세요."), + NON_INTEGER_INPUT_FOR_PURCHASE_MONEY("구입금액은 숫자로 입력해주세요."), + NON_MULTIPLE_OF_LOTTO_PRICE_INPUT_FOR_PURCHASE_MONEY("구입금액은" + Lotto.PRICE + "의 배수로 입력해주세요."), + INVALID_LENGTH_INPUT_FOR_WINNING_LOTTO("로또 번호는 6개를 입력해주세요"); + + @Getter + private final String message; +} diff --git a/src/main/java/lotto/parser/LottoParser.java b/src/main/java/lotto/parser/LottoParser.java index 442080f6..08668628 100644 --- a/src/main/java/lotto/parser/LottoParser.java +++ b/src/main/java/lotto/parser/LottoParser.java @@ -1,18 +1,32 @@ package lotto.parser; +import lotto.domain.Lotto; import lotto.domain.LottoNumber; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; +import static lotto.exception.ExceptionMessage.INVALID_LENGTH_INPUT_FOR_WINNING_LOTTO; + public class LottoParser { + private static final String DELIMITER = ","; - public static List generateLotto(String numberInput) { - return Arrays.stream(numberInput.split(DELIMITER)) + public static List parseInputIntoLottoNumbers(String numberInput) throws IllegalArgumentException { + List lottoNumbers = Arrays.stream(numberInput.split(DELIMITER)) .map(String::trim) .map(LottoNumber::new) .collect(Collectors.toList()); + + validate(lottoNumbers); + + return lottoNumbers; + } + + private static void validate(List lottoNumbers) throws IllegalArgumentException { + if (lottoNumbers.size() != Lotto.LOTTO_NUMBER_SIZE) { + throw new IllegalArgumentException(INVALID_LENGTH_INPUT_FOR_WINNING_LOTTO.getMessage()); + } } } diff --git a/src/main/java/lotto/service/LottoService.java b/src/main/java/lotto/service/LottoService.java new file mode 100644 index 00000000..1cb19139 --- /dev/null +++ b/src/main/java/lotto/service/LottoService.java @@ -0,0 +1,40 @@ +package lotto.service; + +import lotto.domain.*; +import lotto.domain.dto.StatisticsResultDTO; +import lotto.domain.dto.PurchasePriceInputDTO; +import lotto.domain.dto.PurchaseResultDTO; +import lotto.domain.dto.WinningLottoInputDTO; + +public class LottoService { + + public PurchaseResultDTO purchase(PurchasePriceInputDTO purchasePriceInputDTO) { + PurchaseCount purchaseCount = new PurchaseCount(purchasePriceInputDTO.getInput()); + RandomLottoSet randomLottoSet = new RandomLottoSet(purchaseCount); + + return PurchaseResultDTO.builder() + .purchaseCount(purchaseCount) + .randomLottoSet(randomLottoSet) + .build(); + } + + public StatisticsResultDTO calcResult(PurchaseResultDTO purchaseResultDTO, WinningLottoInputDTO winningLottoInputDTO) { + WinningLotto winningLotto = WinningLotto.builder() + .winningNumberInput(winningLottoInputDTO.getWinningLottoNumbers()) + .bonusNumberInput(winningLottoInputDTO.getWinningLottoBonus()) + .build(); + PrizeCount prizeCount = PrizeCount.builder() + .lottoset(purchaseResultDTO.getRandomLottoSet()) + .winningLotto(winningLotto) + .build(); + LottoStatistics lottoStatistics = LottoStatistics.builder() + .prizeCount(prizeCount) + .purchaseCount(purchaseResultDTO.getPurchaseCount()) + .build(); + + return StatisticsResultDTO.builder() + .prizeCount(prizeCount) + .profitRate(lottoStatistics.calcProfitRate()) + .build(); + } +} diff --git a/src/main/java/lotto/util/NumberValidateUtils.java b/src/main/java/lotto/util/NumberValidateUtils.java index 08e68532..56ee0b79 100644 --- a/src/main/java/lotto/util/NumberValidateUtils.java +++ b/src/main/java/lotto/util/NumberValidateUtils.java @@ -3,10 +3,10 @@ import java.util.regex.Pattern; public class NumberValidateUtils { + private static final String NUMBER_REGEX = "^[0-9]*$"; public static boolean isInteger(String input) { return Pattern.matches(NUMBER_REGEX, input); } - } diff --git a/src/main/java/lotto/view/OutputView.java b/src/main/java/lotto/view/OutputView.java index 26f8f4c4..ea62fd05 100644 --- a/src/main/java/lotto/view/OutputView.java +++ b/src/main/java/lotto/view/OutputView.java @@ -1,7 +1,11 @@ package lotto.view; import lotto.constant.PrizeMessage; -import lotto.domain.*; +import lotto.domain.Lotto; +import lotto.domain.LottoNumber; +import lotto.domain.PurchaseCount; +import lotto.domain.RandomLottoSet; +import lotto.domain.dto.StatisticsResultDTO; public class OutputView { @@ -52,14 +56,14 @@ public void askWinningLottoBonus() { print("보너스 볼을 입력해 주세요."); } - public void printLottoStatistic(LottoStatistics lottoStatistics) { + public void printLottoStatistic(StatisticsResultDTO lottoStatistics) { String result = "당첨 통계" + "---------" + NEW_LINE + generatePrizeMessage(PrizeMessage.FIFTH, lottoStatistics.getPrizeCount().getCountFifth()) + generatePrizeMessage(PrizeMessage.FOURTH, lottoStatistics.getPrizeCount().getCountFourth()) + generatePrizeMessage(PrizeMessage.THIRD, lottoStatistics.getPrizeCount().getCountThird()) + generatePrizeMessage(PrizeMessage.SECOND, lottoStatistics.getPrizeCount().getCountSecond()) + generatePrizeMessage(PrizeMessage.FIRST, lottoStatistics.getPrizeCount().getCountFirst()) + - generateProfitRateMessage(lottoStatistics.calcProfitRate()); + generateProfitRateMessage(lottoStatistics.getProfitRate()); print(result); } @@ -70,4 +74,8 @@ private String generatePrizeMessage(PrizeMessage prizeMessage, int prizeCount) { private String generateProfitRateMessage(double profitRate) { return "총 수익률은 " + profitRate + "입니다."; } + + public void printException(String message) { + print(message); + } } diff --git a/src/main/java/lotto/view/View.java b/src/main/java/lotto/view/View.java index e75e124c..7f1ba7ca 100644 --- a/src/main/java/lotto/view/View.java +++ b/src/main/java/lotto/view/View.java @@ -1,9 +1,8 @@ package lotto.view; -import lotto.domain.LottoStatistics; -import lotto.domain.PurchaseCount; -import lotto.domain.RandomLottoSet; +import lotto.domain.dto.StatisticsResultDTO; import lotto.domain.dto.PurchasePriceInputDTO; +import lotto.domain.dto.PurchaseResultDTO; import lotto.domain.dto.WinningLottoInputDTO; public class View { @@ -11,32 +10,38 @@ public class View { private static final InputView inputView = new InputView(); private static final OutputView outputView = new OutputView(); - public static PurchasePriceInputDTO getPurchaseCost() { + public PurchasePriceInputDTO getPurchaseCost() { outputView.askPurchaseCost(); String purchaseCountInput = inputView.getPurchaseCost(); - return new PurchasePriceInputDTO(purchaseCountInput); + return PurchasePriceInputDTO.builder() + .input(purchaseCountInput) + .build(); } - public static WinningLottoInputDTO getWinningLottoAndBonus() { + public WinningLottoInputDTO getWinningLottoAndBonus() { outputView.askWinningLottoNumbers(); String winningLottoNumbers = inputView.getWinningLottoNumbers(); outputView.askWinningLottoBonus(); String winningLottoBonus = inputView.getWinningLottoBonus(); - return new WinningLottoInputDTO(winningLottoNumbers, winningLottoBonus); + return WinningLottoInputDTO.builder() + .winningLottoNumbers(winningLottoNumbers) + .winningLottoBonus(winningLottoBonus) + .build(); } - public static void printLottoCount(PurchaseCount purchaseCount) { - outputView.printLottoCount(purchaseCount); + public void printLottoStatistics(StatisticsResultDTO statisticsResultDTO) { + outputView.printLottoStatistic(statisticsResultDTO); } - public static void printLottoSet(RandomLottoSet lottoSet) { - outputView.printLottoSet(lottoSet); + public void printException(String message) { + outputView.printException(message); } - public static void printLottoStatistics(LottoStatistics lottoStatistics) { - outputView.printLottoStatistic(lottoStatistics); + public void printLottoPurchaseResult(PurchaseResultDTO purchaseResultDTO) { + outputView.printLottoCount(purchaseResultDTO.getPurchaseCount()); + outputView.printLottoSet(purchaseResultDTO.getRandomLottoSet()); } } diff --git a/src/test/java/lotto/constant/PurchaseCountTest.java b/src/test/java/lotto/constant/PurchaseCountTest.java index f0eea50f..8910853c 100644 --- a/src/test/java/lotto/constant/PurchaseCountTest.java +++ b/src/test/java/lotto/constant/PurchaseCountTest.java @@ -19,7 +19,7 @@ void testInputValueUnder1000() throws Exception { // when, then assertThrows(IllegalArgumentException.class, () -> { - PurchaseCount purchaseCount = new PurchaseCount(input);; + new PurchaseCount(input);; }); } diff --git a/src/test/java/lotto/domain/TestLottoSet.java b/src/test/java/lotto/domain/TestLottoSet.java index 6552af7d..02353ae5 100644 --- a/src/test/java/lotto/domain/TestLottoSet.java +++ b/src/test/java/lotto/domain/TestLottoSet.java @@ -4,37 +4,37 @@ import java.util.HashSet; -public class TestLottoSet extends LottoSet{ +public class TestLottoSet extends LottoSet { public TestLottoSet() { super(generateLottoSet()); } - public static HashSet generateLottoSet() { + public static HashSet generateLottoSet() throws IllegalArgumentException { HashSet lottos = new HashSet<>(); - lottos.add(new Lotto(LottoParser.generateLotto("1, 2, 3, 4, 5, 6"))); + lottos.add(new Lotto(LottoParser.parseInputIntoLottoNumbers("1, 2, 3, 4, 5, 6"))); - lottos.add(new Lotto(LottoParser.generateLotto("1, 2, 3, 4, 5, 7"))); - lottos.add(new Lotto(LottoParser.generateLotto("1, 2, 3, 4, 5, 7"))); + lottos.add(new Lotto(LottoParser.parseInputIntoLottoNumbers("1, 2, 3, 4, 5, 7"))); + lottos.add(new Lotto(LottoParser.parseInputIntoLottoNumbers("1, 2, 3, 4, 5, 7"))); - lottos.add(new Lotto(LottoParser.generateLotto("1, 2, 3, 4, 5, 10"))); - lottos.add(new Lotto(LottoParser.generateLotto("1, 2, 3, 4, 5, 10"))); - lottos.add(new Lotto(LottoParser.generateLotto("1, 2, 3, 4, 5, 10"))); + lottos.add(new Lotto(LottoParser.parseInputIntoLottoNumbers("1, 2, 3, 4, 5, 10"))); + lottos.add(new Lotto(LottoParser.parseInputIntoLottoNumbers("1, 2, 3, 4, 5, 10"))); + lottos.add(new Lotto(LottoParser.parseInputIntoLottoNumbers("1, 2, 3, 4, 5, 10"))); - lottos.add(new Lotto(LottoParser.generateLotto("1, 2, 3, 4, 10, 11"))); - lottos.add(new Lotto(LottoParser.generateLotto("1, 2, 3, 4, 10, 11"))); - lottos.add(new Lotto(LottoParser.generateLotto("1, 2, 3, 4, 10, 11"))); - lottos.add(new Lotto(LottoParser.generateLotto("1, 2, 3, 4, 10, 11"))); + lottos.add(new Lotto(LottoParser.parseInputIntoLottoNumbers("1, 2, 3, 4, 10, 11"))); + lottos.add(new Lotto(LottoParser.parseInputIntoLottoNumbers("1, 2, 3, 4, 10, 11"))); + lottos.add(new Lotto(LottoParser.parseInputIntoLottoNumbers("1, 2, 3, 4, 10, 11"))); + lottos.add(new Lotto(LottoParser.parseInputIntoLottoNumbers("1, 2, 3, 4, 10, 11"))); - lottos.add(new Lotto(LottoParser.generateLotto("1, 2, 3, 10, 11, 12"))); - lottos.add(new Lotto(LottoParser.generateLotto("1, 2, 3, 10, 11, 12"))); - lottos.add(new Lotto(LottoParser.generateLotto("1, 2, 3, 10, 11, 12"))); - lottos.add(new Lotto(LottoParser.generateLotto("1, 2, 3, 10, 11, 12"))); - lottos.add(new Lotto(LottoParser.generateLotto("1, 2, 3, 10, 11, 12"))); + lottos.add(new Lotto(LottoParser.parseInputIntoLottoNumbers("1, 2, 3, 10, 11, 12"))); + lottos.add(new Lotto(LottoParser.parseInputIntoLottoNumbers("1, 2, 3, 10, 11, 12"))); + lottos.add(new Lotto(LottoParser.parseInputIntoLottoNumbers("1, 2, 3, 10, 11, 12"))); + lottos.add(new Lotto(LottoParser.parseInputIntoLottoNumbers("1, 2, 3, 10, 11, 12"))); + lottos.add(new Lotto(LottoParser.parseInputIntoLottoNumbers("1, 2, 3, 10, 11, 12"))); - lottos.add(new Lotto(LottoParser.generateLotto("1, 2, 10, 11, 12, 13"))); - lottos.add(new Lotto(LottoParser.generateLotto("1, 10, 11, 12, 13, 14"))); - lottos.add(new Lotto(LottoParser.generateLotto("19, 10, 11, 12, 13, 14"))); + lottos.add(new Lotto(LottoParser.parseInputIntoLottoNumbers("1, 2, 10, 11, 12, 13"))); + lottos.add(new Lotto(LottoParser.parseInputIntoLottoNumbers("1, 10, 11, 12, 13, 14"))); + lottos.add(new Lotto(LottoParser.parseInputIntoLottoNumbers("19, 10, 11, 12, 13, 14"))); return lottos; } diff --git a/src/test/java/lotto/domain/TestWinningLotto.java b/src/test/java/lotto/domain/TestWinningLotto.java index ceb5a74e..78ee9cc0 100644 --- a/src/test/java/lotto/domain/TestWinningLotto.java +++ b/src/test/java/lotto/domain/TestWinningLotto.java @@ -1,6 +1,7 @@ package lotto.domain; public class TestWinningLotto extends WinningLotto{ + private static final String winningNumberInput = "1, 2, 3, 4, 5, 6"; private static final String bonusNumberInput = "7"; diff --git a/src/test/java/lotto/domain/WinningLottoTest.java b/src/test/java/lotto/domain/WinningLottoTest.java index 683095b1..8a6a579a 100644 --- a/src/test/java/lotto/domain/WinningLottoTest.java +++ b/src/test/java/lotto/domain/WinningLottoTest.java @@ -18,7 +18,7 @@ public class WinningLottoTest { @BeforeAll private static void generateTargetLotto() { - targetLotto = new Lotto(LottoParser.generateLotto("1, 2, 3, 4, 5, 6")); + targetLotto = new Lotto(LottoParser.parseInputIntoLottoNumbers("1, 2, 3, 4, 5, 6")); } @Test @@ -67,6 +67,19 @@ void testInputSmallerThanLowerBoundary() throws Exception { }); } + @Test + @DisplayName("6개 이하의 숫자가 입력됐을 때 예외를 던진다") + void testInputLengthLessThanStandard() throws Exception { + // given + String input = "1, 2, 3, 4, 5"; + String bonus = "10"; + + // when, then + assertThrows(IllegalArgumentException.class, () -> { + new WinningLotto(input, bonus); + }); + } + @Test @DisplayName("보너스 번호 입력값을 확인한다") void testBonusInput() { @@ -79,7 +92,7 @@ void testBonusInput() { @DisplayName("숫자가 6개 일치할 때 1등") void testFindPrize_firstWithoutBonus() { //given, when - PrizeCondition prize = winningLotto.findPrize(targetLotto); + PrizeCondition prize = winningLotto.findPrizeCondition(targetLotto); //then assertThat(prize).isEqualTo(PrizeCondition.FIRST); @@ -90,7 +103,7 @@ void testFindPrize_firstWithoutBonus() { void testFindPrize_secondWithBonus() { //given, when WinningLotto winningLotto = new WinningLotto("1,2,3,4,5,7", "6"); - PrizeCondition prize = winningLotto.findPrize(targetLotto); + PrizeCondition prize = winningLotto.findPrizeCondition(targetLotto); //then assertThat(prize).isEqualTo(PrizeCondition.SECOND); @@ -101,7 +114,7 @@ void testFindPrize_secondWithBonus() { void testFindPrize_third() { //given, when WinningLotto winningLotto = new WinningLotto("1,2,3,4,5,7", bonusNumberInput); - PrizeCondition prize = winningLotto.findPrize(targetLotto); + PrizeCondition prize = winningLotto.findPrizeCondition(targetLotto); //then assertThat(prize).isEqualTo(PrizeCondition.THIRD); @@ -112,7 +125,7 @@ void testFindPrize_third() { void testFindPrize_fourth() { //given, when WinningLotto winningLotto = new WinningLotto("4,3,2,1,8,7", bonusNumberInput); - PrizeCondition prize = winningLotto.findPrize(targetLotto); + PrizeCondition prize = winningLotto.findPrizeCondition(targetLotto); //then assertThat(prize).isEqualTo(PrizeCondition.FOURTH); @@ -123,7 +136,7 @@ void testFindPrize_fourth() { void testFindPrize_fifth() { //given, when WinningLotto winningLotto = new WinningLotto("4,3,2,9,8,7", bonusNumberInput); - PrizeCondition prize = winningLotto.findPrize(targetLotto); + PrizeCondition prize = winningLotto.findPrizeCondition(targetLotto); //then assertThat(prize).isEqualTo(PrizeCondition.FIFTH); From eaac13698cbbc72730f1d7863a139107ba33fe05 Mon Sep 17 00:00:00 2001 From: Jiwoo Kim Date: Thu, 8 Jul 2021 02:04:52 +0900 Subject: [PATCH 30/55] =?UTF-8?q?refactor:=20=EC=97=90=EB=9F=AC=20?= =?UTF-8?q?=EB=A9=94=EC=8B=9C=EC=A7=80=20=EC=B6=9C=EB=A0=A5=20=EC=8B=9C=20?= =?UTF-8?q?=ED=97=A4=EB=8D=94=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/lotto/view/OutputView.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/lotto/view/OutputView.java b/src/main/java/lotto/view/OutputView.java index ea62fd05..df5c9e2a 100644 --- a/src/main/java/lotto/view/OutputView.java +++ b/src/main/java/lotto/view/OutputView.java @@ -9,6 +9,7 @@ public class OutputView { + private static final String ERROR_HEADER = "[ERROR] "; private static final String COMMA = ", "; private static final String PARENTHESIS_LEFT = "["; private static final String PARENTHESIS_RIGHT = "]"; @@ -76,6 +77,6 @@ private String generateProfitRateMessage(double profitRate) { } public void printException(String message) { - print(message); + print(ERROR_HEADER + message); } } From 674ae964df443fa321bc390aa68d382cf50c4137 Mon Sep 17 00:00:00 2001 From: Jiwoo Kim Date: Sat, 17 Jul 2021 18:01:17 +0900 Subject: [PATCH 31/55] =?UTF-8?q?refactor:=20Prize=20enum=20=ED=95=98?= =?UTF-8?q?=EB=82=98=EB=A1=9C=20=ED=95=A9=EC=B9=98=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/lotto/constant/Prize.java | 56 +++++++++ .../java/lotto/constant/PrizeCondition.java | 42 ------- .../java/lotto/constant/PrizeMessage.java | 18 --- src/main/java/lotto/constant/PrizeMoney.java | 19 --- .../java/lotto/domain/LottoStatistics.java | 15 +-- src/main/java/lotto/domain/PrizeCount.java | 14 +-- src/main/java/lotto/domain/WinningLotto.java | 6 +- src/main/java/lotto/view/OutputView.java | 29 +++-- .../lotto/constant/PrizeConditionTest.java | 113 ------------------ src/test/java/lotto/constant/PrizeTest.java | 113 ++++++++++++++++++ .../lotto/domain/LottoStatisticsTest.java | 10 +- .../java/lotto/domain/WinningLottoTest.java | 22 ++-- 12 files changed, 218 insertions(+), 239 deletions(-) create mode 100644 src/main/java/lotto/constant/Prize.java delete mode 100644 src/main/java/lotto/constant/PrizeCondition.java delete mode 100644 src/main/java/lotto/constant/PrizeMessage.java delete mode 100644 src/main/java/lotto/constant/PrizeMoney.java delete mode 100644 src/test/java/lotto/constant/PrizeConditionTest.java create mode 100644 src/test/java/lotto/constant/PrizeTest.java diff --git a/src/main/java/lotto/constant/Prize.java b/src/main/java/lotto/constant/Prize.java new file mode 100644 index 00000000..837bd803 --- /dev/null +++ b/src/main/java/lotto/constant/Prize.java @@ -0,0 +1,56 @@ +package lotto.constant; + +import lombok.Getter; +import lotto.domain.PrizeCount; + +@Getter +public enum Prize { + + FIRST(6, false, 2000000000), + SECOND(5, true, 30000000), + THIRD(5, false, 1500000), + FOURTH(4, false, 50000), + FIFTH(3, false, 5000), + LOSE(2, false, 0); + + private final int matchNumbersCount; + private final boolean isBonus; + private final long prizeMoney; + + Prize(int matchNumbersCount, boolean isBonus, long prizeMoney) { + this.matchNumbersCount = matchNumbersCount; + this.isBonus = isBonus; + this.prizeMoney = prizeMoney; + } + + public static Prize findPrize(int matchNumbersCount, boolean isBonus) { + if (matchNumbersCount <= LOSE.matchNumbersCount) { + return LOSE; + } + if (matchNumbersCount == FIFTH.matchNumbersCount) { + return FIFTH; + } + if (matchNumbersCount == FOURTH.matchNumbersCount) { + return FOURTH; + } + if (matchNumbersCount == FIRST.matchNumbersCount) { + return FIRST; + } + return dissolveSecondOrThird(isBonus); + } + + private static Prize dissolveSecondOrThird(boolean isBonus) { + if (isBonus) { + return Prize.SECOND; + } + return Prize.THIRD; + } + + public static long sumOfPrizeMoney(PrizeCount prizeCount) { + return prizeCount.getCountFirst() * FIRST.prizeMoney + + prizeCount.getCountSecond() * SECOND.prizeMoney + + prizeCount.getCountThird() * THIRD.prizeMoney + + prizeCount.getCountFourth() * FOURTH.prizeMoney + + prizeCount.getCountFifth() * FIFTH.prizeMoney; + } +} diff --git a/src/main/java/lotto/constant/PrizeCondition.java b/src/main/java/lotto/constant/PrizeCondition.java deleted file mode 100644 index a09f3303..00000000 --- a/src/main/java/lotto/constant/PrizeCondition.java +++ /dev/null @@ -1,42 +0,0 @@ -package lotto.constant; - -public enum PrizeCondition { - - FIRST(6, false), - SECOND(5, true), - THIRD(5, false), - FOURTH(4, false), - FIFTH(3, false), - LOSE(2, false); - - private final int matchNumbersCount; - private final boolean isBonus; - - PrizeCondition(int matchNumbersCount, boolean isBonus) { - this.matchNumbersCount = matchNumbersCount; - this.isBonus = isBonus; - } - - public static PrizeCondition findPrize(int matchNumbersCount, boolean isBonus) { - if (matchNumbersCount <= LOSE.matchNumbersCount) { - return LOSE; - } - if (matchNumbersCount == FIFTH.matchNumbersCount) { - return FIFTH; - } - if (matchNumbersCount == FOURTH.matchNumbersCount) { - return FOURTH; - } - if (matchNumbersCount == FIRST.matchNumbersCount) { - return FIRST; - } - return dissolveSecondOrThird(isBonus); - } - - private static PrizeCondition dissolveSecondOrThird(boolean isBonus) { - if (isBonus) { - return PrizeCondition.SECOND; - } - return PrizeCondition.THIRD; - } -} diff --git a/src/main/java/lotto/constant/PrizeMessage.java b/src/main/java/lotto/constant/PrizeMessage.java deleted file mode 100644 index 6ee19c3e..00000000 --- a/src/main/java/lotto/constant/PrizeMessage.java +++ /dev/null @@ -1,18 +0,0 @@ -package lotto.constant; - -import lombok.Getter; - -public enum PrizeMessage { - FIRST("6개 일치 (2000000000원)- "), - SECOND("5개 일치, 보너스 볼 일치(30000000원) - "), - THIRD("5개 일치 (1500000원)- "), - FOURTH("4개 일치 (50000원)- "), - FIFTH("3개 일치 (5000원)- "); - - @Getter - private final String message; - - PrizeMessage(String message) { - this.message= message; - } -} diff --git a/src/main/java/lotto/constant/PrizeMoney.java b/src/main/java/lotto/constant/PrizeMoney.java deleted file mode 100644 index 910cff49..00000000 --- a/src/main/java/lotto/constant/PrizeMoney.java +++ /dev/null @@ -1,19 +0,0 @@ -package lotto.constant; - -import lombok.Getter; - -@Getter -public enum PrizeMoney { - - FIRST(2000000000), - SECOND(30000000), - THIRD(1500000), - FOURTH(50000), - FIFTH(5000); - - private final long value; - - PrizeMoney(long value) { - this.value = value; - } -} diff --git a/src/main/java/lotto/domain/LottoStatistics.java b/src/main/java/lotto/domain/LottoStatistics.java index 523f8d34..847d59ed 100644 --- a/src/main/java/lotto/domain/LottoStatistics.java +++ b/src/main/java/lotto/domain/LottoStatistics.java @@ -2,7 +2,7 @@ import lombok.Builder; import lombok.Getter; -import lotto.constant.PrizeMoney; +import lotto.constant.Prize; public class LottoStatistics { @@ -17,14 +17,7 @@ public LottoStatistics(PrizeCount prizeCount, PurchaseCount purchaseCount) { } public double calcProfitRate() { - return (double) sumOfPrizeMoney() / (purchaseCount.getPurchaseCount() * Lotto.PRICE); + return (double) Prize.sumOfPrizeMoney(prizeCount) + / (purchaseCount.getPurchaseCount() * Lotto.PRICE); } - - private long sumOfPrizeMoney() { - return prizeCount.getCountFirst() * PrizeMoney.FIRST.getValue() - + prizeCount.getCountSecond() * PrizeMoney.SECOND.getValue() - + prizeCount.getCountThird() * PrizeMoney.THIRD.getValue() - + prizeCount.getCountFourth() * PrizeMoney.FOURTH.getValue() - + prizeCount.getCountFifth() * PrizeMoney.FIFTH.getValue(); - } -} +} \ No newline at end of file diff --git a/src/main/java/lotto/domain/PrizeCount.java b/src/main/java/lotto/domain/PrizeCount.java index 76681964..5049a8a3 100644 --- a/src/main/java/lotto/domain/PrizeCount.java +++ b/src/main/java/lotto/domain/PrizeCount.java @@ -2,7 +2,7 @@ import lombok.Builder; import lombok.Getter; -import lotto.constant.PrizeCondition; +import lotto.constant.Prize; @Getter public class PrizeCount { @@ -20,24 +20,24 @@ public PrizeCount(LottoSet lottoset, WinningLotto winningLotto) { } } - private void updateCounts(PrizeCondition prizeCondition) { - if (prizeCondition.equals(PrizeCondition.FIRST)) { + private void updateCounts(Prize prize) { + if (prize.equals(Prize.FIRST)) { countFirst++; return; } - if (prizeCondition.equals(PrizeCondition.SECOND)) { + if (prize.equals(Prize.SECOND)) { countSecond++; return; } - if (prizeCondition.equals(PrizeCondition.THIRD)) { + if (prize.equals(Prize.THIRD)) { countThird++; return; } - if (prizeCondition.equals(PrizeCondition.FOURTH)) { + if (prize.equals(Prize.FOURTH)) { countFourth++; return; } - if (prizeCondition.equals(PrizeCondition.FIFTH)) { + if (prize.equals(Prize.FIFTH)) { countFifth++; } } diff --git a/src/main/java/lotto/domain/WinningLotto.java b/src/main/java/lotto/domain/WinningLotto.java index d1d648c3..04573df5 100644 --- a/src/main/java/lotto/domain/WinningLotto.java +++ b/src/main/java/lotto/domain/WinningLotto.java @@ -2,7 +2,7 @@ import lombok.Builder; import lombok.Getter; -import lotto.constant.PrizeCondition; +import lotto.constant.Prize; import lotto.parser.LottoParser; import java.util.Comparator; @@ -19,8 +19,8 @@ public WinningLotto(String winningNumberInput, String bonusNumberInput) throws I this.lottoNumbers.sort(Comparator.comparingInt(LottoNumber::getLottoNumber)); } - public PrizeCondition findPrizeCondition(Lotto targetLotto) { - return PrizeCondition.findPrize(getMatchNumbersCount(targetLotto), isBonusMatch(targetLotto)); + public Prize findPrizeCondition(Lotto targetLotto) { + return Prize.findPrize(getMatchNumbersCount(targetLotto), isBonusMatch(targetLotto)); } private int getMatchNumbersCount(Lotto targetLotto) { diff --git a/src/main/java/lotto/view/OutputView.java b/src/main/java/lotto/view/OutputView.java index df5c9e2a..5f65bfbf 100644 --- a/src/main/java/lotto/view/OutputView.java +++ b/src/main/java/lotto/view/OutputView.java @@ -1,6 +1,6 @@ package lotto.view; -import lotto.constant.PrizeMessage; +import lotto.constant.Prize; import lotto.domain.Lotto; import lotto.domain.LottoNumber; import lotto.domain.PurchaseCount; @@ -59,17 +59,30 @@ public void askWinningLottoBonus() { public void printLottoStatistic(StatisticsResultDTO lottoStatistics) { String result = "당첨 통계" + "---------" + NEW_LINE + - generatePrizeMessage(PrizeMessage.FIFTH, lottoStatistics.getPrizeCount().getCountFifth()) + - generatePrizeMessage(PrizeMessage.FOURTH, lottoStatistics.getPrizeCount().getCountFourth()) + - generatePrizeMessage(PrizeMessage.THIRD, lottoStatistics.getPrizeCount().getCountThird()) + - generatePrizeMessage(PrizeMessage.SECOND, lottoStatistics.getPrizeCount().getCountSecond()) + - generatePrizeMessage(PrizeMessage.FIRST, lottoStatistics.getPrizeCount().getCountFirst()) + + generatePrizeResultMessage(Prize.FIFTH, lottoStatistics.getPrizeCount().getCountFifth()) + + generatePrizeResultMessage(Prize.FOURTH, lottoStatistics.getPrizeCount().getCountFourth()) + + generatePrizeResultMessage(Prize.THIRD, lottoStatistics.getPrizeCount().getCountThird()) + + generatePrizeResultMessage(Prize.SECOND, lottoStatistics.getPrizeCount().getCountSecond()) + + generatePrizeResultMessage(Prize.FIRST, lottoStatistics.getPrizeCount().getCountFirst()) + generateProfitRateMessage(lottoStatistics.getProfitRate()); print(result); } - private String generatePrizeMessage(PrizeMessage prizeMessage, int prizeCount) { - return prizeMessage.getMessage() + prizeCount + UNIT + NEW_LINE; + private String generatePrizeResultMessage(Prize prize, int prizeCount) { + return generatePrizeResultMessage(prize) + prizeCount + UNIT + NEW_LINE; + } + + private String generatePrizeResultMessage(Prize prize) { + StringBuilder sb = new StringBuilder(); + sb.append(prize.getMatchNumbersCount()) + .append("개 일치"); + if (prize.isBonus()) { + sb.append(", 보너스 볼 일치 "); + } + sb.append("(") + .append(prize.getPrizeMoney()) + .append("원)- "); + return sb.toString(); } private String generateProfitRateMessage(double profitRate) { diff --git a/src/test/java/lotto/constant/PrizeConditionTest.java b/src/test/java/lotto/constant/PrizeConditionTest.java deleted file mode 100644 index 41e134f6..00000000 --- a/src/test/java/lotto/constant/PrizeConditionTest.java +++ /dev/null @@ -1,113 +0,0 @@ -package lotto.constant; - -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; - -import static org.assertj.core.api.Assertions.assertThat; - -public class PrizeConditionTest { - - @Test - @DisplayName("숫자가 6개 일치할 때 1등") - void firstPrize() { - //given - int matchNumbersCount = 6; - - //when, then - assertThat(PrizeCondition.findPrize(matchNumbersCount, true)) - .isEqualTo(PrizeCondition.FIRST); - assertThat(PrizeCondition.findPrize(matchNumbersCount, false)) - .isEqualTo(PrizeCondition.FIRST); - } - - @Test - @DisplayName("숫자가 5개 일치하고 보너스번호도 일치할 때 2등") - void secondPrize() { - //given - int matchNumbersCount = 5; - boolean isBonus = true; - - //when - PrizeCondition prizeCondition = PrizeCondition.findPrize(matchNumbersCount, isBonus); - - //then - assertThat(prizeCondition).isEqualTo(PrizeCondition.SECOND); - } - - @Test - @DisplayName("숫자가 5개 일치하고 보너스 번호가 일치하지 않을 때 3등") - void thirdPrize() { - //given - int matchNumbersCount = 5; - boolean isBonus = false; - - //when, then - assertThat(PrizeCondition.findPrize(matchNumbersCount, isBonus)) - .isEqualTo(PrizeCondition.THIRD); - } - - @Test - @DisplayName("숫자가 4개 일치할 때 4등") - void fourthPrize() { - //given - int matchNumbersCount = 4; - - //when, then - assertThat(PrizeCondition.findPrize(matchNumbersCount, false)) - .isEqualTo(PrizeCondition.FOURTH); - assertThat(PrizeCondition.findPrize(matchNumbersCount, true)) - .isEqualTo(PrizeCondition.FOURTH); - } - - @Test - @DisplayName("숫자가 3개 일치할 때 5등") - void fifthPrize() { - //given - int matchNumbersCount = 3; - - //when, then - assertThat(PrizeCondition.findPrize(matchNumbersCount, false)) - .isEqualTo(PrizeCondition.FIFTH); - assertThat(PrizeCondition.findPrize(matchNumbersCount, true)) - .isEqualTo(PrizeCondition.FIFTH); - } - - @Test - @DisplayName("숫자가 2개 일치할 때 꽝") - void loseMatchesTwo() { - //given - int matchNumbersCount = 2; - - //when, then - assertThat(PrizeCondition.findPrize(matchNumbersCount, false)) - .isEqualTo(PrizeCondition.LOSE); - assertThat(PrizeCondition.findPrize(matchNumbersCount, true)) - .isEqualTo(PrizeCondition.LOSE); - } - - @Test - @DisplayName("숫자가 1개 일치할 때 꽝") - void loseMatchesOne() { - //given - int matchNumbersCount = 1; - - //when, then - assertThat(PrizeCondition.findPrize(matchNumbersCount, false)) - .isEqualTo(PrizeCondition.LOSE); - assertThat(PrizeCondition.findPrize(matchNumbersCount, true)) - .isEqualTo(PrizeCondition.LOSE); - } - - @Test - @DisplayName("숫자가 0개 일치할 때 꽝") - void lose() { - //given - int matchNumbersCount = 0; - - //when, then - assertThat(PrizeCondition.findPrize(matchNumbersCount, false)) - .isEqualTo(PrizeCondition.LOSE); - assertThat(PrizeCondition.findPrize(matchNumbersCount, true)) - .isEqualTo(PrizeCondition.LOSE); - } -} diff --git a/src/test/java/lotto/constant/PrizeTest.java b/src/test/java/lotto/constant/PrizeTest.java new file mode 100644 index 00000000..c7389dd9 --- /dev/null +++ b/src/test/java/lotto/constant/PrizeTest.java @@ -0,0 +1,113 @@ +package lotto.constant; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class PrizeTest { + + @Test + @DisplayName("숫자가 6개 일치할 때 1등") + void firstPrize() { + //given + int matchNumbersCount = 6; + + //when, then + assertThat(Prize.findPrize(matchNumbersCount, true)) + .isEqualTo(Prize.FIRST); + assertThat(Prize.findPrize(matchNumbersCount, false)) + .isEqualTo(Prize.FIRST); + } + + @Test + @DisplayName("숫자가 5개 일치하고 보너스번호도 일치할 때 2등") + void secondPrize() { + //given + int matchNumbersCount = 5; + boolean isBonus = true; + + //when + Prize prize = Prize.findPrize(matchNumbersCount, isBonus); + + //then + assertThat(prize).isEqualTo(Prize.SECOND); + } + + @Test + @DisplayName("숫자가 5개 일치하고 보너스 번호가 일치하지 않을 때 3등") + void thirdPrize() { + //given + int matchNumbersCount = 5; + boolean isBonus = false; + + //when, then + assertThat(Prize.findPrize(matchNumbersCount, isBonus)) + .isEqualTo(Prize.THIRD); + } + + @Test + @DisplayName("숫자가 4개 일치할 때 4등") + void fourthPrize() { + //given + int matchNumbersCount = 4; + + //when, then + assertThat(Prize.findPrize(matchNumbersCount, false)) + .isEqualTo(Prize.FOURTH); + assertThat(Prize.findPrize(matchNumbersCount, true)) + .isEqualTo(Prize.FOURTH); + } + + @Test + @DisplayName("숫자가 3개 일치할 때 5등") + void fifthPrize() { + //given + int matchNumbersCount = 3; + + //when, then + assertThat(Prize.findPrize(matchNumbersCount, false)) + .isEqualTo(Prize.FIFTH); + assertThat(Prize.findPrize(matchNumbersCount, true)) + .isEqualTo(Prize.FIFTH); + } + + @Test + @DisplayName("숫자가 2개 일치할 때 꽝") + void loseMatchesTwo() { + //given + int matchNumbersCount = 2; + + //when, then + assertThat(Prize.findPrize(matchNumbersCount, false)) + .isEqualTo(Prize.LOSE); + assertThat(Prize.findPrize(matchNumbersCount, true)) + .isEqualTo(Prize.LOSE); + } + + @Test + @DisplayName("숫자가 1개 일치할 때 꽝") + void loseMatchesOne() { + //given + int matchNumbersCount = 1; + + //when, then + assertThat(Prize.findPrize(matchNumbersCount, false)) + .isEqualTo(Prize.LOSE); + assertThat(Prize.findPrize(matchNumbersCount, true)) + .isEqualTo(Prize.LOSE); + } + + @Test + @DisplayName("숫자가 0개 일치할 때 꽝") + void lose() { + //given + int matchNumbersCount = 0; + + //when, then + assertThat(Prize.findPrize(matchNumbersCount, false)) + .isEqualTo(Prize.LOSE); + assertThat(Prize.findPrize(matchNumbersCount, true)) + .isEqualTo(Prize.LOSE); + } +} diff --git a/src/test/java/lotto/domain/LottoStatisticsTest.java b/src/test/java/lotto/domain/LottoStatisticsTest.java index 779903b5..6804b39d 100644 --- a/src/test/java/lotto/domain/LottoStatisticsTest.java +++ b/src/test/java/lotto/domain/LottoStatisticsTest.java @@ -1,6 +1,6 @@ package lotto.domain; -import lotto.constant.PrizeMoney; +import lotto.constant.Prize; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -8,14 +8,10 @@ public class LottoStatisticsTest { - private static final long PRIZE_SUM = PrizeMoney.FIRST.getValue() - + 2 * PrizeMoney.SECOND.getValue() - + 3 * PrizeMoney.THIRD.getValue() - + 4 * PrizeMoney.FOURTH.getValue() - + 5 * PrizeMoney.FIFTH.getValue(); + private static final PrizeCount PRIZE_COUNT = new PrizeCount(new TestLottoSet(), new TestWinningLotto()); + private static final long PRIZE_SUM = Prize.sumOfPrizeMoney(PRIZE_COUNT); private static final int PURCHASE_MONEY = 18000; private static final double RESULT = (double) PRIZE_SUM / PURCHASE_MONEY; - private static final PrizeCount PRIZE_COUNT = new PrizeCount(new TestLottoSet(), new TestWinningLotto()); @Test @DisplayName("로또 구매 개수와 당첨 로또 개수를 입력받으면 수익률을 반환한다") diff --git a/src/test/java/lotto/domain/WinningLottoTest.java b/src/test/java/lotto/domain/WinningLottoTest.java index 8a6a579a..3e19934f 100644 --- a/src/test/java/lotto/domain/WinningLottoTest.java +++ b/src/test/java/lotto/domain/WinningLottoTest.java @@ -1,6 +1,6 @@ package lotto.domain; -import lotto.constant.PrizeCondition; +import lotto.constant.Prize; import lotto.parser.LottoParser; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.DisplayName; @@ -92,10 +92,10 @@ void testBonusInput() { @DisplayName("숫자가 6개 일치할 때 1등") void testFindPrize_firstWithoutBonus() { //given, when - PrizeCondition prize = winningLotto.findPrizeCondition(targetLotto); + Prize prize = winningLotto.findPrizeCondition(targetLotto); //then - assertThat(prize).isEqualTo(PrizeCondition.FIRST); + assertThat(prize).isEqualTo(Prize.FIRST); } @Test @@ -103,10 +103,10 @@ void testFindPrize_firstWithoutBonus() { void testFindPrize_secondWithBonus() { //given, when WinningLotto winningLotto = new WinningLotto("1,2,3,4,5,7", "6"); - PrizeCondition prize = winningLotto.findPrizeCondition(targetLotto); + Prize prize = winningLotto.findPrizeCondition(targetLotto); //then - assertThat(prize).isEqualTo(PrizeCondition.SECOND); + assertThat(prize).isEqualTo(Prize.SECOND); } @Test @@ -114,10 +114,10 @@ void testFindPrize_secondWithBonus() { void testFindPrize_third() { //given, when WinningLotto winningLotto = new WinningLotto("1,2,3,4,5,7", bonusNumberInput); - PrizeCondition prize = winningLotto.findPrizeCondition(targetLotto); + Prize prize = winningLotto.findPrizeCondition(targetLotto); //then - assertThat(prize).isEqualTo(PrizeCondition.THIRD); + assertThat(prize).isEqualTo(Prize.THIRD); } @Test @@ -125,10 +125,10 @@ void testFindPrize_third() { void testFindPrize_fourth() { //given, when WinningLotto winningLotto = new WinningLotto("4,3,2,1,8,7", bonusNumberInput); - PrizeCondition prize = winningLotto.findPrizeCondition(targetLotto); + Prize prize = winningLotto.findPrizeCondition(targetLotto); //then - assertThat(prize).isEqualTo(PrizeCondition.FOURTH); + assertThat(prize).isEqualTo(Prize.FOURTH); } @Test @@ -136,9 +136,9 @@ void testFindPrize_fourth() { void testFindPrize_fifth() { //given, when WinningLotto winningLotto = new WinningLotto("4,3,2,9,8,7", bonusNumberInput); - PrizeCondition prize = winningLotto.findPrizeCondition(targetLotto); + Prize prize = winningLotto.findPrizeCondition(targetLotto); //then - assertThat(prize).isEqualTo(PrizeCondition.FIFTH); + assertThat(prize).isEqualTo(Prize.FIFTH); } } From a0173eaa43607ba27c207d9a4ca0426a3e7298c1 Mon Sep 17 00:00:00 2001 From: Jiwoo Kim Date: Sat, 17 Jul 2021 18:15:12 +0900 Subject: [PATCH 32/55] refactor: move Prize from constant package into domain package --- src/main/java/lotto/domain/LottoStatistics.java | 1 - src/main/java/lotto/{constant => domain}/Prize.java | 3 +-- src/main/java/lotto/domain/PrizeCount.java | 1 - src/main/java/lotto/domain/WinningLotto.java | 1 - src/main/java/lotto/view/OutputView.java | 2 +- src/test/java/lotto/domain/LottoStatisticsTest.java | 1 - src/test/java/lotto/{constant => domain}/PrizeTest.java | 3 ++- .../java/lotto/{constant => domain}/PurchaseCountTest.java | 2 +- src/test/java/lotto/domain/WinningLottoTest.java | 1 - 9 files changed, 5 insertions(+), 10 deletions(-) rename src/main/java/lotto/{constant => domain}/Prize.java (96%) rename src/test/java/lotto/{constant => domain}/PrizeTest.java (98%) rename src/test/java/lotto/{constant => domain}/PurchaseCountTest.java (99%) diff --git a/src/main/java/lotto/domain/LottoStatistics.java b/src/main/java/lotto/domain/LottoStatistics.java index 847d59ed..e44bfd46 100644 --- a/src/main/java/lotto/domain/LottoStatistics.java +++ b/src/main/java/lotto/domain/LottoStatistics.java @@ -2,7 +2,6 @@ import lombok.Builder; import lombok.Getter; -import lotto.constant.Prize; public class LottoStatistics { diff --git a/src/main/java/lotto/constant/Prize.java b/src/main/java/lotto/domain/Prize.java similarity index 96% rename from src/main/java/lotto/constant/Prize.java rename to src/main/java/lotto/domain/Prize.java index 837bd803..2502b91b 100644 --- a/src/main/java/lotto/constant/Prize.java +++ b/src/main/java/lotto/domain/Prize.java @@ -1,7 +1,6 @@ -package lotto.constant; +package lotto.domain; import lombok.Getter; -import lotto.domain.PrizeCount; @Getter public enum Prize { diff --git a/src/main/java/lotto/domain/PrizeCount.java b/src/main/java/lotto/domain/PrizeCount.java index 5049a8a3..cdd0f544 100644 --- a/src/main/java/lotto/domain/PrizeCount.java +++ b/src/main/java/lotto/domain/PrizeCount.java @@ -2,7 +2,6 @@ import lombok.Builder; import lombok.Getter; -import lotto.constant.Prize; @Getter public class PrizeCount { diff --git a/src/main/java/lotto/domain/WinningLotto.java b/src/main/java/lotto/domain/WinningLotto.java index 04573df5..901ce275 100644 --- a/src/main/java/lotto/domain/WinningLotto.java +++ b/src/main/java/lotto/domain/WinningLotto.java @@ -2,7 +2,6 @@ import lombok.Builder; import lombok.Getter; -import lotto.constant.Prize; import lotto.parser.LottoParser; import java.util.Comparator; diff --git a/src/main/java/lotto/view/OutputView.java b/src/main/java/lotto/view/OutputView.java index 5f65bfbf..3af62a51 100644 --- a/src/main/java/lotto/view/OutputView.java +++ b/src/main/java/lotto/view/OutputView.java @@ -1,6 +1,6 @@ package lotto.view; -import lotto.constant.Prize; +import lotto.domain.Prize; import lotto.domain.Lotto; import lotto.domain.LottoNumber; import lotto.domain.PurchaseCount; diff --git a/src/test/java/lotto/domain/LottoStatisticsTest.java b/src/test/java/lotto/domain/LottoStatisticsTest.java index 6804b39d..effc702a 100644 --- a/src/test/java/lotto/domain/LottoStatisticsTest.java +++ b/src/test/java/lotto/domain/LottoStatisticsTest.java @@ -1,6 +1,5 @@ package lotto.domain; -import lotto.constant.Prize; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; diff --git a/src/test/java/lotto/constant/PrizeTest.java b/src/test/java/lotto/domain/PrizeTest.java similarity index 98% rename from src/test/java/lotto/constant/PrizeTest.java rename to src/test/java/lotto/domain/PrizeTest.java index c7389dd9..777168ce 100644 --- a/src/test/java/lotto/constant/PrizeTest.java +++ b/src/test/java/lotto/domain/PrizeTest.java @@ -1,5 +1,6 @@ -package lotto.constant; +package lotto.domain; +import lotto.domain.Prize; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; diff --git a/src/test/java/lotto/constant/PurchaseCountTest.java b/src/test/java/lotto/domain/PurchaseCountTest.java similarity index 99% rename from src/test/java/lotto/constant/PurchaseCountTest.java rename to src/test/java/lotto/domain/PurchaseCountTest.java index 8910853c..b5a10e92 100644 --- a/src/test/java/lotto/constant/PurchaseCountTest.java +++ b/src/test/java/lotto/domain/PurchaseCountTest.java @@ -1,4 +1,4 @@ -package lotto.constant; +package lotto.domain; import lotto.domain.PurchaseCount; import org.junit.jupiter.api.DisplayName; diff --git a/src/test/java/lotto/domain/WinningLottoTest.java b/src/test/java/lotto/domain/WinningLottoTest.java index 3e19934f..18efe24c 100644 --- a/src/test/java/lotto/domain/WinningLottoTest.java +++ b/src/test/java/lotto/domain/WinningLottoTest.java @@ -1,6 +1,5 @@ package lotto.domain; -import lotto.constant.Prize; import lotto.parser.LottoParser; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.DisplayName; From 1bf532548095765de36a60daf94068ce9ec06a47 Mon Sep 17 00:00:00 2001 From: Jiwoo Kim Date: Sat, 17 Jul 2021 18:19:24 +0900 Subject: [PATCH 33/55] refactor: rename abbreviation calc into calculate --- src/main/java/lotto/controller/LottoController.java | 2 +- src/main/java/lotto/domain/LottoStatistics.java | 2 +- src/main/java/lotto/service/LottoService.java | 4 ++-- src/test/java/lotto/domain/LottoStatisticsTest.java | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/lotto/controller/LottoController.java b/src/main/java/lotto/controller/LottoController.java index 1db4d987..8ced3a57 100644 --- a/src/main/java/lotto/controller/LottoController.java +++ b/src/main/java/lotto/controller/LottoController.java @@ -26,7 +26,7 @@ public static void start() throws IllegalArgumentException { view.printLottoPurchaseResult(purchaseResultDTO); WinningLottoInputDTO winningLottoInputDTO = view.getWinningLottoAndBonus(); - StatisticsResultDTO statisticsResultDTO = lottoService.calcResult(purchaseResultDTO, winningLottoInputDTO); + StatisticsResultDTO statisticsResultDTO = lottoService.calculateResult(purchaseResultDTO, winningLottoInputDTO); view.printLottoStatistics(statisticsResultDTO); } } diff --git a/src/main/java/lotto/domain/LottoStatistics.java b/src/main/java/lotto/domain/LottoStatistics.java index e44bfd46..3ee78ff0 100644 --- a/src/main/java/lotto/domain/LottoStatistics.java +++ b/src/main/java/lotto/domain/LottoStatistics.java @@ -15,7 +15,7 @@ public LottoStatistics(PrizeCount prizeCount, PurchaseCount purchaseCount) { this.purchaseCount = purchaseCount; } - public double calcProfitRate() { + public double calculateProfitRate() { return (double) Prize.sumOfPrizeMoney(prizeCount) / (purchaseCount.getPurchaseCount() * Lotto.PRICE); } diff --git a/src/main/java/lotto/service/LottoService.java b/src/main/java/lotto/service/LottoService.java index 1cb19139..06f5cbde 100644 --- a/src/main/java/lotto/service/LottoService.java +++ b/src/main/java/lotto/service/LottoService.java @@ -18,7 +18,7 @@ public PurchaseResultDTO purchase(PurchasePriceInputDTO purchasePriceInputDTO) { .build(); } - public StatisticsResultDTO calcResult(PurchaseResultDTO purchaseResultDTO, WinningLottoInputDTO winningLottoInputDTO) { + public StatisticsResultDTO calculateResult(PurchaseResultDTO purchaseResultDTO, WinningLottoInputDTO winningLottoInputDTO) { WinningLotto winningLotto = WinningLotto.builder() .winningNumberInput(winningLottoInputDTO.getWinningLottoNumbers()) .bonusNumberInput(winningLottoInputDTO.getWinningLottoBonus()) @@ -34,7 +34,7 @@ public StatisticsResultDTO calcResult(PurchaseResultDTO purchaseResultDTO, Winni return StatisticsResultDTO.builder() .prizeCount(prizeCount) - .profitRate(lottoStatistics.calcProfitRate()) + .profitRate(lottoStatistics.calculateProfitRate()) .build(); } } diff --git a/src/test/java/lotto/domain/LottoStatisticsTest.java b/src/test/java/lotto/domain/LottoStatisticsTest.java index effc702a..58a9e348 100644 --- a/src/test/java/lotto/domain/LottoStatisticsTest.java +++ b/src/test/java/lotto/domain/LottoStatisticsTest.java @@ -20,6 +20,6 @@ void testLottoStatistics() { new PurchaseCount(String.valueOf(PURCHASE_MONEY))); // then - assertThat(lottoStatistics.calcProfitRate()).isEqualTo(RESULT); + assertThat(lottoStatistics.calculateProfitRate()).isEqualTo(RESULT); } } From ffed9078dacf72083cea0f01e007c9575414ae9f Mon Sep 17 00:00:00 2001 From: Jiwoo Kim Date: Sat, 17 Jul 2021 18:30:46 +0900 Subject: [PATCH 34/55] refactor: change parameter of WinningLotto constructor --- src/main/java/lotto/domain/WinningLotto.java | 7 +- src/main/java/lotto/service/LottoService.java | 5 +- .../java/lotto/domain/TestWinningLotto.java | 9 ++- .../java/lotto/domain/WinningLottoTest.java | 69 +++++++++++++++---- 4 files changed, 69 insertions(+), 21 deletions(-) diff --git a/src/main/java/lotto/domain/WinningLotto.java b/src/main/java/lotto/domain/WinningLotto.java index 901ce275..f5a857b1 100644 --- a/src/main/java/lotto/domain/WinningLotto.java +++ b/src/main/java/lotto/domain/WinningLotto.java @@ -2,6 +2,7 @@ import lombok.Builder; import lombok.Getter; +import lotto.domain.dto.WinningLottoInputDTO; import lotto.parser.LottoParser; import java.util.Comparator; @@ -12,9 +13,9 @@ public class WinningLotto extends Lotto { private final LottoNumber bonusNumber; @Builder - public WinningLotto(String winningNumberInput, String bonusNumberInput) throws IllegalArgumentException { - super(LottoParser.parseInputIntoLottoNumbers(winningNumberInput)); - this.bonusNumber = new LottoNumber(bonusNumberInput); + public WinningLotto(WinningLottoInputDTO winningLottoInputDTO) throws IllegalArgumentException { + super(LottoParser.parseInputIntoLottoNumbers(winningLottoInputDTO.getWinningLottoNumbers())); + this.bonusNumber = new LottoNumber(winningLottoInputDTO.getWinningLottoBonus()); this.lottoNumbers.sort(Comparator.comparingInt(LottoNumber::getLottoNumber)); } diff --git a/src/main/java/lotto/service/LottoService.java b/src/main/java/lotto/service/LottoService.java index 06f5cbde..7a337893 100644 --- a/src/main/java/lotto/service/LottoService.java +++ b/src/main/java/lotto/service/LottoService.java @@ -1,9 +1,9 @@ package lotto.service; import lotto.domain.*; -import lotto.domain.dto.StatisticsResultDTO; import lotto.domain.dto.PurchasePriceInputDTO; import lotto.domain.dto.PurchaseResultDTO; +import lotto.domain.dto.StatisticsResultDTO; import lotto.domain.dto.WinningLottoInputDTO; public class LottoService { @@ -20,8 +20,7 @@ public PurchaseResultDTO purchase(PurchasePriceInputDTO purchasePriceInputDTO) { public StatisticsResultDTO calculateResult(PurchaseResultDTO purchaseResultDTO, WinningLottoInputDTO winningLottoInputDTO) { WinningLotto winningLotto = WinningLotto.builder() - .winningNumberInput(winningLottoInputDTO.getWinningLottoNumbers()) - .bonusNumberInput(winningLottoInputDTO.getWinningLottoBonus()) + .winningLottoInputDTO(winningLottoInputDTO) .build(); PrizeCount prizeCount = PrizeCount.builder() .lottoset(purchaseResultDTO.getRandomLottoSet()) diff --git a/src/test/java/lotto/domain/TestWinningLotto.java b/src/test/java/lotto/domain/TestWinningLotto.java index 78ee9cc0..3aaa306b 100644 --- a/src/test/java/lotto/domain/TestWinningLotto.java +++ b/src/test/java/lotto/domain/TestWinningLotto.java @@ -1,11 +1,16 @@ package lotto.domain; -public class TestWinningLotto extends WinningLotto{ +import lotto.domain.dto.WinningLottoInputDTO; + +public class TestWinningLotto extends WinningLotto { private static final String winningNumberInput = "1, 2, 3, 4, 5, 6"; private static final String bonusNumberInput = "7"; public TestWinningLotto() { - super(winningNumberInput, bonusNumberInput); + super(WinningLottoInputDTO.builder() + .winningLottoNumbers(winningNumberInput) + .winningLottoBonus(bonusNumberInput) + .build()); } } diff --git a/src/test/java/lotto/domain/WinningLottoTest.java b/src/test/java/lotto/domain/WinningLottoTest.java index 18efe24c..72c2f6e9 100644 --- a/src/test/java/lotto/domain/WinningLottoTest.java +++ b/src/test/java/lotto/domain/WinningLottoTest.java @@ -1,5 +1,6 @@ package lotto.domain; +import lotto.domain.dto.WinningLottoInputDTO; import lotto.parser.LottoParser; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.DisplayName; @@ -13,7 +14,9 @@ public class WinningLottoTest { private final String winningNumberInput = "1, 2, 3, 4, 5, 6"; private final String bonusNumberInput = "10"; private static Lotto targetLotto; - private final WinningLotto winningLotto = new WinningLotto(winningNumberInput, bonusNumberInput); + private final WinningLotto winningLotto = new WinningLotto( + new WinningLottoInputDTO(winningNumberInput, bonusNumberInput) + ); @BeforeAll private static void generateTargetLotto() { @@ -33,10 +36,14 @@ void testInputWithNonInteger() throws Exception { // given String input = "1, a, 3, 4, 5, 6"; String bonus = "10"; + WinningLottoInputDTO winningLottoInputDTO = WinningLottoInputDTO.builder() + .winningLottoNumbers(input) + .winningLottoBonus(bonus) + .build(); // when, then assertThrows(IllegalArgumentException.class, () -> { - new WinningLotto(input, bonus); + new WinningLotto(winningLottoInputDTO); }); } @@ -46,10 +53,14 @@ void testInputGreaterThanUpperBoundary() throws Exception { // given String input = "1, 2, 3, 4, 5, 46"; String bonus = "10"; + WinningLottoInputDTO winningLottoInputDTO = WinningLottoInputDTO.builder() + .winningLottoNumbers(input) + .winningLottoBonus(bonus) + .build(); // when, then assertThrows(IllegalArgumentException.class, () -> { - new WinningLotto(input, bonus); + new WinningLotto(winningLottoInputDTO); }); } @@ -59,10 +70,14 @@ void testInputSmallerThanLowerBoundary() throws Exception { // given String input = "0, 2, 3, 4, 5, 6"; String bonus = "10"; + WinningLottoInputDTO winningLottoInputDTO = WinningLottoInputDTO.builder() + .winningLottoNumbers(input) + .winningLottoBonus(bonus) + .build(); // when, then assertThrows(IllegalArgumentException.class, () -> { - new WinningLotto(input, bonus); + new WinningLotto(winningLottoInputDTO); }); } @@ -72,10 +87,14 @@ void testInputLengthLessThanStandard() throws Exception { // given String input = "1, 2, 3, 4, 5"; String bonus = "10"; + WinningLottoInputDTO winningLottoInputDTO = WinningLottoInputDTO.builder() + .winningLottoNumbers(input) + .winningLottoBonus(bonus) + .build(); // when, then assertThrows(IllegalArgumentException.class, () -> { - new WinningLotto(input, bonus); + new WinningLotto(winningLottoInputDTO); }); } @@ -100,8 +119,14 @@ void testFindPrize_firstWithoutBonus() { @Test @DisplayName("숫자가 5개 일치하고 보너스번호도 일치할 때 2등") void testFindPrize_secondWithBonus() { - //given, when - WinningLotto winningLotto = new WinningLotto("1,2,3,4,5,7", "6"); + //given + WinningLottoInputDTO winningLottoInputDTO = WinningLottoInputDTO.builder() + .winningLottoNumbers("1,2,3,4,5,7") + .winningLottoBonus("6") + .build(); + + // when + WinningLotto winningLotto = new WinningLotto(winningLottoInputDTO); Prize prize = winningLotto.findPrizeCondition(targetLotto); //then @@ -111,8 +136,14 @@ void testFindPrize_secondWithBonus() { @Test @DisplayName("숫자가 5개 일치하고 보너스 번호가 일치하지 않을 때 3등") void testFindPrize_third() { - //given, when - WinningLotto winningLotto = new WinningLotto("1,2,3,4,5,7", bonusNumberInput); + //given + WinningLottoInputDTO winningLottoInputDTO = WinningLottoInputDTO.builder() + .winningLottoNumbers("1,2,3,4,5,7") + .winningLottoBonus(bonusNumberInput) + .build(); + + // when + WinningLotto winningLotto = new WinningLotto(winningLottoInputDTO); Prize prize = winningLotto.findPrizeCondition(targetLotto); //then @@ -122,8 +153,14 @@ void testFindPrize_third() { @Test @DisplayName("숫자가 4개 일치할 때 4등") void testFindPrize_fourth() { - //given, when - WinningLotto winningLotto = new WinningLotto("4,3,2,1,8,7", bonusNumberInput); + //given + WinningLottoInputDTO winningLottoInputDTO = WinningLottoInputDTO.builder() + .winningLottoNumbers("4,3,2,1,8,7") + .winningLottoBonus(bonusNumberInput) + .build(); + + // when + WinningLotto winningLotto = new WinningLotto(winningLottoInputDTO); Prize prize = winningLotto.findPrizeCondition(targetLotto); //then @@ -133,8 +170,14 @@ void testFindPrize_fourth() { @Test @DisplayName("숫자가 3개 일치할 때 5등") void testFindPrize_fifth() { - //given, when - WinningLotto winningLotto = new WinningLotto("4,3,2,9,8,7", bonusNumberInput); + //given + WinningLottoInputDTO winningLottoInputDTO = WinningLottoInputDTO.builder() + .winningLottoNumbers("4,3,2,9,8,7") + .winningLottoBonus(bonusNumberInput) + .build(); + + // when + WinningLotto winningLotto = new WinningLotto(winningLottoInputDTO); Prize prize = winningLotto.findPrizeCondition(targetLotto); //then From c48c8e1a3af9f28cb8f2622192dc2703fb07b6c0 Mon Sep 17 00:00:00 2001 From: Jiwoo Kim Date: Sat, 17 Jul 2021 18:40:59 +0900 Subject: [PATCH 35/55] refactor: abstract InputView and OutputView --- src/main/java/lotto/LottoApplication.java | 18 ++++ .../lotto/controller/LottoController.java | 31 ++++--- .../java/lotto/view/ConsoleInputView.java | 24 +++++ .../java/lotto/view/ConsoleOutputView.java | 91 +++++++++++++++++++ src/main/java/lotto/view/InputView.java | 23 +---- src/main/java/lotto/view/OutputView.java | 90 ++---------------- src/main/java/lotto/view/View.java | 10 +- 7 files changed, 168 insertions(+), 119 deletions(-) create mode 100644 src/main/java/lotto/LottoApplication.java create mode 100644 src/main/java/lotto/view/ConsoleInputView.java create mode 100644 src/main/java/lotto/view/ConsoleOutputView.java diff --git a/src/main/java/lotto/LottoApplication.java b/src/main/java/lotto/LottoApplication.java new file mode 100644 index 00000000..ef112d31 --- /dev/null +++ b/src/main/java/lotto/LottoApplication.java @@ -0,0 +1,18 @@ +package lotto; + +import lotto.controller.LottoController; +import lotto.view.ConsoleInputView; +import lotto.view.ConsoleOutputView; +import lotto.view.View; + +public class LottoApplication { + + public static void main(String[] args) { + View view = View.builder() + .inputView(new ConsoleInputView()) + .outputView(new ConsoleOutputView()) + .build(); + LottoController lottoController = new LottoController(view); + lottoController.start(); + } +} diff --git a/src/main/java/lotto/controller/LottoController.java b/src/main/java/lotto/controller/LottoController.java index 8ced3a57..942e249a 100644 --- a/src/main/java/lotto/controller/LottoController.java +++ b/src/main/java/lotto/controller/LottoController.java @@ -1,32 +1,33 @@ package lotto.controller; -import lotto.domain.dto.StatisticsResultDTO; import lotto.domain.dto.PurchasePriceInputDTO; import lotto.domain.dto.PurchaseResultDTO; +import lotto.domain.dto.StatisticsResultDTO; import lotto.domain.dto.WinningLottoInputDTO; import lotto.service.LottoService; import lotto.view.View; public class LottoController { - private static final View view = new View(); - private static final LottoService lottoService = new LottoService(); + private final View view; + private final LottoService lottoService; + + public LottoController(View view) { + this.view = view; + this.lottoService = new LottoService(); + } - public static void main(String[] args) { + public void start() throws IllegalArgumentException { try { - start(); + PurchasePriceInputDTO purchasePriceInputDTO = view.getPurchaseCost(); + PurchaseResultDTO purchaseResultDTO = lottoService.purchase(purchasePriceInputDTO); + view.printLottoPurchaseResult(purchaseResultDTO); + + WinningLottoInputDTO winningLottoInputDTO = view.getWinningLottoAndBonus(); + StatisticsResultDTO statisticsResultDTO = lottoService.calculateResult(purchaseResultDTO, winningLottoInputDTO); + view.printLottoStatistics(statisticsResultDTO); } catch (IllegalArgumentException e) { view.printException(e.getMessage()); } } - - public static void start() throws IllegalArgumentException { - PurchasePriceInputDTO purchasePriceInputDTO = view.getPurchaseCost(); - PurchaseResultDTO purchaseResultDTO = lottoService.purchase(purchasePriceInputDTO); - view.printLottoPurchaseResult(purchaseResultDTO); - - WinningLottoInputDTO winningLottoInputDTO = view.getWinningLottoAndBonus(); - StatisticsResultDTO statisticsResultDTO = lottoService.calculateResult(purchaseResultDTO, winningLottoInputDTO); - view.printLottoStatistics(statisticsResultDTO); - } } diff --git a/src/main/java/lotto/view/ConsoleInputView.java b/src/main/java/lotto/view/ConsoleInputView.java new file mode 100644 index 00000000..123d7f6e --- /dev/null +++ b/src/main/java/lotto/view/ConsoleInputView.java @@ -0,0 +1,24 @@ +package lotto.view; + +import java.util.Scanner; + +public class ConsoleInputView implements InputView { + + private final Scanner scanner = new Scanner(System.in); + + private String nextLine() { + return scanner.nextLine(); + } + + public String getPurchaseCost() { + return nextLine(); + } + + public String getWinningLottoNumbers() { + return nextLine(); + } + + public String getWinningLottoBonus() { + return nextLine(); + } +} diff --git a/src/main/java/lotto/view/ConsoleOutputView.java b/src/main/java/lotto/view/ConsoleOutputView.java new file mode 100644 index 00000000..0fec6bd8 --- /dev/null +++ b/src/main/java/lotto/view/ConsoleOutputView.java @@ -0,0 +1,91 @@ +package lotto.view; + +import lotto.domain.*; +import lotto.domain.dto.StatisticsResultDTO; + +public class ConsoleOutputView implements OutputView { + + private static final String ERROR_HEADER = "[ERROR] "; + private static final String COMMA = ", "; + private static final String PARENTHESIS_LEFT = "["; + private static final String PARENTHESIS_RIGHT = "]"; + private static final String UNIT = "개"; + private static final String NEW_LINE = "\n"; + + private void print(String contents) { + System.out.println(contents); + } + + public void printLottoCount(PurchaseCount purchaseCount) { + int count = purchaseCount.getPurchaseCount(); + print(count + "개를 구매했습니다."); + } + + public void printLottoSet(RandomLottoSet lottoSet) { + for (Lotto lotto : lottoSet.getLottoSet()) { + printLotto(lotto); + } + } + + private void printLotto(Lotto lotto) { + String result = PARENTHESIS_LEFT; + for (LottoNumber lottoNumber : lotto.getLottoNumbers()) { + result += lottoNumber.getLottoNumber() + COMMA; + } + result = removeCommaAtTheEnd(result) + PARENTHESIS_RIGHT; + + print(result); + } + + private String removeCommaAtTheEnd(String str) { + return str.substring(0, str.length() - COMMA.length()); + } + + public void askPurchaseCost() { + print("구입금액을 입력해 주세요."); + } + + public void askWinningLottoNumbers() { + print("지난 주 당첨 번호를 입력해 주세요."); + } + + public void askWinningLottoBonus() { + print("보너스 볼을 입력해 주세요."); + } + + public void printLottoStatistic(StatisticsResultDTO lottoStatistics) { + String result = "당첨 통계" + NEW_LINE + "---------" + NEW_LINE + + generatePrizeResultMessage(Prize.FIFTH, lottoStatistics.getPrizeCount().getCountFifth()) + + generatePrizeResultMessage(Prize.FOURTH, lottoStatistics.getPrizeCount().getCountFourth()) + + generatePrizeResultMessage(Prize.THIRD, lottoStatistics.getPrizeCount().getCountThird()) + + generatePrizeResultMessage(Prize.SECOND, lottoStatistics.getPrizeCount().getCountSecond()) + + generatePrizeResultMessage(Prize.FIRST, lottoStatistics.getPrizeCount().getCountFirst()) + + generateProfitRateMessage(lottoStatistics.getProfitRate()); + print(result); + } + + private String generatePrizeResultMessage(Prize prize, int prizeCount) { + return generatePrizeResultMessage(prize) + prizeCount + UNIT + NEW_LINE; + } + + private String generatePrizeResultMessage(Prize prize) { + StringBuilder sb = new StringBuilder(); + sb.append(prize.getMatchNumbersCount()) + .append("개 일치"); + if (prize.isBonus()) { + sb.append(", 보너스 볼 일치 "); + } + sb.append("(") + .append(prize.getPrizeMoney()) + .append("원)- "); + return sb.toString(); + } + + private String generateProfitRateMessage(double profitRate) { + return "총 수익률은 " + profitRate + "입니다."; + } + + public void printException(String message) { + print(ERROR_HEADER + message); + } +} diff --git a/src/main/java/lotto/view/InputView.java b/src/main/java/lotto/view/InputView.java index 03d72c3b..8aed9a18 100644 --- a/src/main/java/lotto/view/InputView.java +++ b/src/main/java/lotto/view/InputView.java @@ -1,24 +1,9 @@ package lotto.view; -import java.util.Scanner; +public interface InputView { + String getPurchaseCost(); -public class InputView { + String getWinningLottoNumbers(); - private final Scanner scanner = new Scanner(System.in); - - private String nextLine() { - return scanner.nextLine(); - } - - public String getPurchaseCost() { - return nextLine(); - } - - public String getWinningLottoNumbers() { - return nextLine(); - } - - public String getWinningLottoBonus() { - return nextLine(); - } + String getWinningLottoBonus(); } diff --git a/src/main/java/lotto/view/OutputView.java b/src/main/java/lotto/view/OutputView.java index 3af62a51..840c2383 100644 --- a/src/main/java/lotto/view/OutputView.java +++ b/src/main/java/lotto/view/OutputView.java @@ -1,95 +1,21 @@ package lotto.view; -import lotto.domain.Prize; -import lotto.domain.Lotto; -import lotto.domain.LottoNumber; import lotto.domain.PurchaseCount; import lotto.domain.RandomLottoSet; import lotto.domain.dto.StatisticsResultDTO; -public class OutputView { +public interface OutputView { + void askPurchaseCost(); - private static final String ERROR_HEADER = "[ERROR] "; - private static final String COMMA = ", "; - private static final String PARENTHESIS_LEFT = "["; - private static final String PARENTHESIS_RIGHT = "]"; - private static final String UNIT = "개"; - private static final String NEW_LINE = "\n"; + void printLottoCount(PurchaseCount purchaseCount); - private void print(String contents) { - System.out.println(contents); - } + void printLottoSet(RandomLottoSet randomLottoSet); - public void printLottoCount(PurchaseCount purchaseCount) { - int count = purchaseCount.getPurchaseCount(); - print(count + "개를 구매했습니다."); - } + void askWinningLottoNumbers(); - public void printLottoSet(RandomLottoSet lottoSet) { - for (Lotto lotto : lottoSet.getLottoSet()) { - printLotto(lotto); - } - } + void askWinningLottoBonus(); - private void printLotto(Lotto lotto) { - String result = PARENTHESIS_LEFT; - for (LottoNumber lottoNumber : lotto.getLottoNumbers()) { - result += lottoNumber.getLottoNumber() + COMMA; - } - result = removeCommaAtTheEnd(result) + PARENTHESIS_RIGHT; + void printLottoStatistic(StatisticsResultDTO statisticsResultDTO); - print(result); - } - - private String removeCommaAtTheEnd(String str) { - return str.substring(0, str.length() - COMMA.length()); - } - - public void askPurchaseCost() { - print("구입금액을 입력해 주세요."); - } - - public void askWinningLottoNumbers() { - print("지난 주 당첨 번호를 입력해 주세요."); - } - - public void askWinningLottoBonus() { - print("보너스 볼을 입력해 주세요."); - } - - public void printLottoStatistic(StatisticsResultDTO lottoStatistics) { - String result = "당첨 통계" + "---------" + NEW_LINE + - generatePrizeResultMessage(Prize.FIFTH, lottoStatistics.getPrizeCount().getCountFifth()) + - generatePrizeResultMessage(Prize.FOURTH, lottoStatistics.getPrizeCount().getCountFourth()) + - generatePrizeResultMessage(Prize.THIRD, lottoStatistics.getPrizeCount().getCountThird()) + - generatePrizeResultMessage(Prize.SECOND, lottoStatistics.getPrizeCount().getCountSecond()) + - generatePrizeResultMessage(Prize.FIRST, lottoStatistics.getPrizeCount().getCountFirst()) + - generateProfitRateMessage(lottoStatistics.getProfitRate()); - print(result); - } - - private String generatePrizeResultMessage(Prize prize, int prizeCount) { - return generatePrizeResultMessage(prize) + prizeCount + UNIT + NEW_LINE; - } - - private String generatePrizeResultMessage(Prize prize) { - StringBuilder sb = new StringBuilder(); - sb.append(prize.getMatchNumbersCount()) - .append("개 일치"); - if (prize.isBonus()) { - sb.append(", 보너스 볼 일치 "); - } - sb.append("(") - .append(prize.getPrizeMoney()) - .append("원)- "); - return sb.toString(); - } - - private String generateProfitRateMessage(double profitRate) { - return "총 수익률은 " + profitRate + "입니다."; - } - - public void printException(String message) { - print(ERROR_HEADER + message); - } + void printException(String message); } diff --git a/src/main/java/lotto/view/View.java b/src/main/java/lotto/view/View.java index 7f1ba7ca..9ca50e1b 100644 --- a/src/main/java/lotto/view/View.java +++ b/src/main/java/lotto/view/View.java @@ -1,14 +1,18 @@ package lotto.view; -import lotto.domain.dto.StatisticsResultDTO; +import lombok.Builder; +import lombok.RequiredArgsConstructor; import lotto.domain.dto.PurchasePriceInputDTO; import lotto.domain.dto.PurchaseResultDTO; +import lotto.domain.dto.StatisticsResultDTO; import lotto.domain.dto.WinningLottoInputDTO; +@RequiredArgsConstructor +@Builder public class View { - private static final InputView inputView = new InputView(); - private static final OutputView outputView = new OutputView(); + private final InputView inputView; + private final OutputView outputView; public PurchasePriceInputDTO getPurchaseCost() { outputView.askPurchaseCost(); From 2c6da43f7e8cf86f1511b597f15638756e229289 Mon Sep 17 00:00:00 2001 From: Jiwoo Kim Date: Mon, 19 Jul 2021 14:23:05 +0900 Subject: [PATCH 36/55] refactor: remove DTO postfix --- .../lotto/controller/LottoController.java | 20 +++++------ src/main/java/lotto/domain/WinningLotto.java | 8 ++--- ...eInputDTO.java => PurchasePriceInput.java} | 2 +- ...haseResultDTO.java => PurchaseResult.java} | 2 +- ...csResultDTO.java => StatisticsResult.java} | 2 +- ...toInputDTO.java => WinningLottoInput.java} | 2 +- src/main/java/lotto/service/LottoService.java | 24 ++++++------- .../java/lotto/view/ConsoleOutputView.java | 4 +-- src/main/java/lotto/view/OutputView.java | 4 +-- src/main/java/lotto/view/View.java | 26 +++++++------- .../java/lotto/domain/TestWinningLotto.java | 4 +-- .../java/lotto/domain/WinningLottoTest.java | 36 +++++++++---------- 12 files changed, 67 insertions(+), 67 deletions(-) rename src/main/java/lotto/domain/dto/{PurchasePriceInputDTO.java => PurchasePriceInput.java} (83%) rename src/main/java/lotto/domain/dto/{PurchaseResultDTO.java => PurchaseResult.java} (90%) rename src/main/java/lotto/domain/dto/{StatisticsResultDTO.java => StatisticsResult.java} (88%) rename src/main/java/lotto/domain/dto/{WinningLottoInputDTO.java => WinningLottoInput.java} (87%) diff --git a/src/main/java/lotto/controller/LottoController.java b/src/main/java/lotto/controller/LottoController.java index 942e249a..706eba55 100644 --- a/src/main/java/lotto/controller/LottoController.java +++ b/src/main/java/lotto/controller/LottoController.java @@ -1,9 +1,9 @@ package lotto.controller; -import lotto.domain.dto.PurchasePriceInputDTO; -import lotto.domain.dto.PurchaseResultDTO; -import lotto.domain.dto.StatisticsResultDTO; -import lotto.domain.dto.WinningLottoInputDTO; +import lotto.domain.dto.PurchasePriceInput; +import lotto.domain.dto.PurchaseResult; +import lotto.domain.dto.StatisticsResult; +import lotto.domain.dto.WinningLottoInput; import lotto.service.LottoService; import lotto.view.View; @@ -19,13 +19,13 @@ public LottoController(View view) { public void start() throws IllegalArgumentException { try { - PurchasePriceInputDTO purchasePriceInputDTO = view.getPurchaseCost(); - PurchaseResultDTO purchaseResultDTO = lottoService.purchase(purchasePriceInputDTO); - view.printLottoPurchaseResult(purchaseResultDTO); + PurchasePriceInput purchasePriceInput = view.getPurchaseCost(); + PurchaseResult purchaseResult = lottoService.purchase(purchasePriceInput); + view.printLottoPurchaseResult(purchaseResult); - WinningLottoInputDTO winningLottoInputDTO = view.getWinningLottoAndBonus(); - StatisticsResultDTO statisticsResultDTO = lottoService.calculateResult(purchaseResultDTO, winningLottoInputDTO); - view.printLottoStatistics(statisticsResultDTO); + WinningLottoInput winningLottoInput = view.getWinningLottoAndBonus(); + StatisticsResult statisticsResult = lottoService.calculateResult(purchaseResult, winningLottoInput); + view.printLottoStatistics(statisticsResult); } catch (IllegalArgumentException e) { view.printException(e.getMessage()); } diff --git a/src/main/java/lotto/domain/WinningLotto.java b/src/main/java/lotto/domain/WinningLotto.java index f5a857b1..63dcdef5 100644 --- a/src/main/java/lotto/domain/WinningLotto.java +++ b/src/main/java/lotto/domain/WinningLotto.java @@ -2,7 +2,7 @@ import lombok.Builder; import lombok.Getter; -import lotto.domain.dto.WinningLottoInputDTO; +import lotto.domain.dto.WinningLottoInput; import lotto.parser.LottoParser; import java.util.Comparator; @@ -13,9 +13,9 @@ public class WinningLotto extends Lotto { private final LottoNumber bonusNumber; @Builder - public WinningLotto(WinningLottoInputDTO winningLottoInputDTO) throws IllegalArgumentException { - super(LottoParser.parseInputIntoLottoNumbers(winningLottoInputDTO.getWinningLottoNumbers())); - this.bonusNumber = new LottoNumber(winningLottoInputDTO.getWinningLottoBonus()); + public WinningLotto(WinningLottoInput winningLottoInput) throws IllegalArgumentException { + super(LottoParser.parseInputIntoLottoNumbers(winningLottoInput.getWinningLottoNumbers())); + this.bonusNumber = new LottoNumber(winningLottoInput.getWinningLottoBonus()); this.lottoNumbers.sort(Comparator.comparingInt(LottoNumber::getLottoNumber)); } diff --git a/src/main/java/lotto/domain/dto/PurchasePriceInputDTO.java b/src/main/java/lotto/domain/dto/PurchasePriceInput.java similarity index 83% rename from src/main/java/lotto/domain/dto/PurchasePriceInputDTO.java rename to src/main/java/lotto/domain/dto/PurchasePriceInput.java index 6cdfe562..9f7052bd 100644 --- a/src/main/java/lotto/domain/dto/PurchasePriceInputDTO.java +++ b/src/main/java/lotto/domain/dto/PurchasePriceInput.java @@ -7,7 +7,7 @@ @Getter @Builder @RequiredArgsConstructor -public class PurchasePriceInputDTO { +public class PurchasePriceInput { private final String input; } diff --git a/src/main/java/lotto/domain/dto/PurchaseResultDTO.java b/src/main/java/lotto/domain/dto/PurchaseResult.java similarity index 90% rename from src/main/java/lotto/domain/dto/PurchaseResultDTO.java rename to src/main/java/lotto/domain/dto/PurchaseResult.java index b7167e1a..4f2912e6 100644 --- a/src/main/java/lotto/domain/dto/PurchaseResultDTO.java +++ b/src/main/java/lotto/domain/dto/PurchaseResult.java @@ -9,7 +9,7 @@ @Getter @RequiredArgsConstructor @Builder -public class PurchaseResultDTO { +public class PurchaseResult { private final PurchaseCount purchaseCount; private final RandomLottoSet randomLottoSet; diff --git a/src/main/java/lotto/domain/dto/StatisticsResultDTO.java b/src/main/java/lotto/domain/dto/StatisticsResult.java similarity index 88% rename from src/main/java/lotto/domain/dto/StatisticsResultDTO.java rename to src/main/java/lotto/domain/dto/StatisticsResult.java index c25c1e78..6c31e973 100644 --- a/src/main/java/lotto/domain/dto/StatisticsResultDTO.java +++ b/src/main/java/lotto/domain/dto/StatisticsResult.java @@ -8,7 +8,7 @@ @Getter @RequiredArgsConstructor @Builder -public class StatisticsResultDTO { +public class StatisticsResult { private final PrizeCount prizeCount; private final double profitRate; diff --git a/src/main/java/lotto/domain/dto/WinningLottoInputDTO.java b/src/main/java/lotto/domain/dto/WinningLottoInput.java similarity index 87% rename from src/main/java/lotto/domain/dto/WinningLottoInputDTO.java rename to src/main/java/lotto/domain/dto/WinningLottoInput.java index 3b558167..0a975eea 100644 --- a/src/main/java/lotto/domain/dto/WinningLottoInputDTO.java +++ b/src/main/java/lotto/domain/dto/WinningLottoInput.java @@ -7,7 +7,7 @@ @Getter @Builder @RequiredArgsConstructor -public class WinningLottoInputDTO { +public class WinningLottoInput { private final String winningLottoNumbers; private final String winningLottoBonus; diff --git a/src/main/java/lotto/service/LottoService.java b/src/main/java/lotto/service/LottoService.java index 7a337893..cd3ba862 100644 --- a/src/main/java/lotto/service/LottoService.java +++ b/src/main/java/lotto/service/LottoService.java @@ -1,37 +1,37 @@ package lotto.service; import lotto.domain.*; -import lotto.domain.dto.PurchasePriceInputDTO; -import lotto.domain.dto.PurchaseResultDTO; -import lotto.domain.dto.StatisticsResultDTO; -import lotto.domain.dto.WinningLottoInputDTO; +import lotto.domain.dto.PurchasePriceInput; +import lotto.domain.dto.PurchaseResult; +import lotto.domain.dto.StatisticsResult; +import lotto.domain.dto.WinningLottoInput; public class LottoService { - public PurchaseResultDTO purchase(PurchasePriceInputDTO purchasePriceInputDTO) { - PurchaseCount purchaseCount = new PurchaseCount(purchasePriceInputDTO.getInput()); + public PurchaseResult purchase(PurchasePriceInput purchasePriceInput) { + PurchaseCount purchaseCount = new PurchaseCount(purchasePriceInput.getInput()); RandomLottoSet randomLottoSet = new RandomLottoSet(purchaseCount); - return PurchaseResultDTO.builder() + return PurchaseResult.builder() .purchaseCount(purchaseCount) .randomLottoSet(randomLottoSet) .build(); } - public StatisticsResultDTO calculateResult(PurchaseResultDTO purchaseResultDTO, WinningLottoInputDTO winningLottoInputDTO) { + public StatisticsResult calculateResult(PurchaseResult purchaseResult, WinningLottoInput winningLottoInput) { WinningLotto winningLotto = WinningLotto.builder() - .winningLottoInputDTO(winningLottoInputDTO) + .winningLottoInputDTO(winningLottoInput) .build(); PrizeCount prizeCount = PrizeCount.builder() - .lottoset(purchaseResultDTO.getRandomLottoSet()) + .lottoset(purchaseResult.getRandomLottoSet()) .winningLotto(winningLotto) .build(); LottoStatistics lottoStatistics = LottoStatistics.builder() .prizeCount(prizeCount) - .purchaseCount(purchaseResultDTO.getPurchaseCount()) + .purchaseCount(purchaseResult.getPurchaseCount()) .build(); - return StatisticsResultDTO.builder() + return StatisticsResult.builder() .prizeCount(prizeCount) .profitRate(lottoStatistics.calculateProfitRate()) .build(); diff --git a/src/main/java/lotto/view/ConsoleOutputView.java b/src/main/java/lotto/view/ConsoleOutputView.java index 0fec6bd8..4e23f28b 100644 --- a/src/main/java/lotto/view/ConsoleOutputView.java +++ b/src/main/java/lotto/view/ConsoleOutputView.java @@ -1,7 +1,7 @@ package lotto.view; import lotto.domain.*; -import lotto.domain.dto.StatisticsResultDTO; +import lotto.domain.dto.StatisticsResult; public class ConsoleOutputView implements OutputView { @@ -53,7 +53,7 @@ public void askWinningLottoBonus() { print("보너스 볼을 입력해 주세요."); } - public void printLottoStatistic(StatisticsResultDTO lottoStatistics) { + public void printLottoStatistic(StatisticsResult lottoStatistics) { String result = "당첨 통계" + NEW_LINE + "---------" + NEW_LINE + generatePrizeResultMessage(Prize.FIFTH, lottoStatistics.getPrizeCount().getCountFifth()) + generatePrizeResultMessage(Prize.FOURTH, lottoStatistics.getPrizeCount().getCountFourth()) + diff --git a/src/main/java/lotto/view/OutputView.java b/src/main/java/lotto/view/OutputView.java index 840c2383..ad773c29 100644 --- a/src/main/java/lotto/view/OutputView.java +++ b/src/main/java/lotto/view/OutputView.java @@ -2,7 +2,7 @@ import lotto.domain.PurchaseCount; import lotto.domain.RandomLottoSet; -import lotto.domain.dto.StatisticsResultDTO; +import lotto.domain.dto.StatisticsResult; public interface OutputView { void askPurchaseCost(); @@ -15,7 +15,7 @@ public interface OutputView { void askWinningLottoBonus(); - void printLottoStatistic(StatisticsResultDTO statisticsResultDTO); + void printLottoStatistic(StatisticsResult statisticsResult); void printException(String message); } diff --git a/src/main/java/lotto/view/View.java b/src/main/java/lotto/view/View.java index 9ca50e1b..19b1d82a 100644 --- a/src/main/java/lotto/view/View.java +++ b/src/main/java/lotto/view/View.java @@ -2,10 +2,10 @@ import lombok.Builder; import lombok.RequiredArgsConstructor; -import lotto.domain.dto.PurchasePriceInputDTO; -import lotto.domain.dto.PurchaseResultDTO; -import lotto.domain.dto.StatisticsResultDTO; -import lotto.domain.dto.WinningLottoInputDTO; +import lotto.domain.dto.PurchasePriceInput; +import lotto.domain.dto.PurchaseResult; +import lotto.domain.dto.StatisticsResult; +import lotto.domain.dto.WinningLottoInput; @RequiredArgsConstructor @Builder @@ -14,38 +14,38 @@ public class View { private final InputView inputView; private final OutputView outputView; - public PurchasePriceInputDTO getPurchaseCost() { + public PurchasePriceInput getPurchaseCost() { outputView.askPurchaseCost(); String purchaseCountInput = inputView.getPurchaseCost(); - return PurchasePriceInputDTO.builder() + return PurchasePriceInput.builder() .input(purchaseCountInput) .build(); } - public WinningLottoInputDTO getWinningLottoAndBonus() { + public WinningLottoInput getWinningLottoAndBonus() { outputView.askWinningLottoNumbers(); String winningLottoNumbers = inputView.getWinningLottoNumbers(); outputView.askWinningLottoBonus(); String winningLottoBonus = inputView.getWinningLottoBonus(); - return WinningLottoInputDTO.builder() + return WinningLottoInput.builder() .winningLottoNumbers(winningLottoNumbers) .winningLottoBonus(winningLottoBonus) .build(); } - public void printLottoStatistics(StatisticsResultDTO statisticsResultDTO) { - outputView.printLottoStatistic(statisticsResultDTO); + public void printLottoStatistics(StatisticsResult statisticsResult) { + outputView.printLottoStatistic(statisticsResult); } public void printException(String message) { outputView.printException(message); } - public void printLottoPurchaseResult(PurchaseResultDTO purchaseResultDTO) { - outputView.printLottoCount(purchaseResultDTO.getPurchaseCount()); - outputView.printLottoSet(purchaseResultDTO.getRandomLottoSet()); + public void printLottoPurchaseResult(PurchaseResult purchaseResult) { + outputView.printLottoCount(purchaseResult.getPurchaseCount()); + outputView.printLottoSet(purchaseResult.getRandomLottoSet()); } } diff --git a/src/test/java/lotto/domain/TestWinningLotto.java b/src/test/java/lotto/domain/TestWinningLotto.java index 3aaa306b..6bdaca47 100644 --- a/src/test/java/lotto/domain/TestWinningLotto.java +++ b/src/test/java/lotto/domain/TestWinningLotto.java @@ -1,6 +1,6 @@ package lotto.domain; -import lotto.domain.dto.WinningLottoInputDTO; +import lotto.domain.dto.WinningLottoInput; public class TestWinningLotto extends WinningLotto { @@ -8,7 +8,7 @@ public class TestWinningLotto extends WinningLotto { private static final String bonusNumberInput = "7"; public TestWinningLotto() { - super(WinningLottoInputDTO.builder() + super(WinningLottoInput.builder() .winningLottoNumbers(winningNumberInput) .winningLottoBonus(bonusNumberInput) .build()); diff --git a/src/test/java/lotto/domain/WinningLottoTest.java b/src/test/java/lotto/domain/WinningLottoTest.java index 72c2f6e9..a606afcc 100644 --- a/src/test/java/lotto/domain/WinningLottoTest.java +++ b/src/test/java/lotto/domain/WinningLottoTest.java @@ -1,6 +1,6 @@ package lotto.domain; -import lotto.domain.dto.WinningLottoInputDTO; +import lotto.domain.dto.WinningLottoInput; import lotto.parser.LottoParser; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.DisplayName; @@ -15,7 +15,7 @@ public class WinningLottoTest { private final String bonusNumberInput = "10"; private static Lotto targetLotto; private final WinningLotto winningLotto = new WinningLotto( - new WinningLottoInputDTO(winningNumberInput, bonusNumberInput) + new WinningLottoInput(winningNumberInput, bonusNumberInput) ); @BeforeAll @@ -36,14 +36,14 @@ void testInputWithNonInteger() throws Exception { // given String input = "1, a, 3, 4, 5, 6"; String bonus = "10"; - WinningLottoInputDTO winningLottoInputDTO = WinningLottoInputDTO.builder() + WinningLottoInput winningLottoInput = WinningLottoInput.builder() .winningLottoNumbers(input) .winningLottoBonus(bonus) .build(); // when, then assertThrows(IllegalArgumentException.class, () -> { - new WinningLotto(winningLottoInputDTO); + new WinningLotto(winningLottoInput); }); } @@ -53,14 +53,14 @@ void testInputGreaterThanUpperBoundary() throws Exception { // given String input = "1, 2, 3, 4, 5, 46"; String bonus = "10"; - WinningLottoInputDTO winningLottoInputDTO = WinningLottoInputDTO.builder() + WinningLottoInput winningLottoInput = WinningLottoInput.builder() .winningLottoNumbers(input) .winningLottoBonus(bonus) .build(); // when, then assertThrows(IllegalArgumentException.class, () -> { - new WinningLotto(winningLottoInputDTO); + new WinningLotto(winningLottoInput); }); } @@ -70,14 +70,14 @@ void testInputSmallerThanLowerBoundary() throws Exception { // given String input = "0, 2, 3, 4, 5, 6"; String bonus = "10"; - WinningLottoInputDTO winningLottoInputDTO = WinningLottoInputDTO.builder() + WinningLottoInput winningLottoInput = WinningLottoInput.builder() .winningLottoNumbers(input) .winningLottoBonus(bonus) .build(); // when, then assertThrows(IllegalArgumentException.class, () -> { - new WinningLotto(winningLottoInputDTO); + new WinningLotto(winningLottoInput); }); } @@ -87,14 +87,14 @@ void testInputLengthLessThanStandard() throws Exception { // given String input = "1, 2, 3, 4, 5"; String bonus = "10"; - WinningLottoInputDTO winningLottoInputDTO = WinningLottoInputDTO.builder() + WinningLottoInput winningLottoInput = WinningLottoInput.builder() .winningLottoNumbers(input) .winningLottoBonus(bonus) .build(); // when, then assertThrows(IllegalArgumentException.class, () -> { - new WinningLotto(winningLottoInputDTO); + new WinningLotto(winningLottoInput); }); } @@ -120,13 +120,13 @@ void testFindPrize_firstWithoutBonus() { @DisplayName("숫자가 5개 일치하고 보너스번호도 일치할 때 2등") void testFindPrize_secondWithBonus() { //given - WinningLottoInputDTO winningLottoInputDTO = WinningLottoInputDTO.builder() + WinningLottoInput winningLottoInput = WinningLottoInput.builder() .winningLottoNumbers("1,2,3,4,5,7") .winningLottoBonus("6") .build(); // when - WinningLotto winningLotto = new WinningLotto(winningLottoInputDTO); + WinningLotto winningLotto = new WinningLotto(winningLottoInput); Prize prize = winningLotto.findPrizeCondition(targetLotto); //then @@ -137,13 +137,13 @@ void testFindPrize_secondWithBonus() { @DisplayName("숫자가 5개 일치하고 보너스 번호가 일치하지 않을 때 3등") void testFindPrize_third() { //given - WinningLottoInputDTO winningLottoInputDTO = WinningLottoInputDTO.builder() + WinningLottoInput winningLottoInput = WinningLottoInput.builder() .winningLottoNumbers("1,2,3,4,5,7") .winningLottoBonus(bonusNumberInput) .build(); // when - WinningLotto winningLotto = new WinningLotto(winningLottoInputDTO); + WinningLotto winningLotto = new WinningLotto(winningLottoInput); Prize prize = winningLotto.findPrizeCondition(targetLotto); //then @@ -154,13 +154,13 @@ void testFindPrize_third() { @DisplayName("숫자가 4개 일치할 때 4등") void testFindPrize_fourth() { //given - WinningLottoInputDTO winningLottoInputDTO = WinningLottoInputDTO.builder() + WinningLottoInput winningLottoInput = WinningLottoInput.builder() .winningLottoNumbers("4,3,2,1,8,7") .winningLottoBonus(bonusNumberInput) .build(); // when - WinningLotto winningLotto = new WinningLotto(winningLottoInputDTO); + WinningLotto winningLotto = new WinningLotto(winningLottoInput); Prize prize = winningLotto.findPrizeCondition(targetLotto); //then @@ -171,13 +171,13 @@ void testFindPrize_fourth() { @DisplayName("숫자가 3개 일치할 때 5등") void testFindPrize_fifth() { //given - WinningLottoInputDTO winningLottoInputDTO = WinningLottoInputDTO.builder() + WinningLottoInput winningLottoInput = WinningLottoInput.builder() .winningLottoNumbers("4,3,2,9,8,7") .winningLottoBonus(bonusNumberInput) .build(); // when - WinningLotto winningLotto = new WinningLotto(winningLottoInputDTO); + WinningLotto winningLotto = new WinningLotto(winningLottoInput); Prize prize = winningLotto.findPrizeCondition(targetLotto); //then From 3e67178b7eaddab08504569eb71a8d823ec2d308 Mon Sep 17 00:00:00 2001 From: Jiwoo Kim Date: Mon, 19 Jul 2021 14:25:24 +0900 Subject: [PATCH 37/55] refactor: remove unnecessary method wrapping --- src/main/java/lotto/view/ConsoleInputView.java | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/main/java/lotto/view/ConsoleInputView.java b/src/main/java/lotto/view/ConsoleInputView.java index 123d7f6e..52ecb927 100644 --- a/src/main/java/lotto/view/ConsoleInputView.java +++ b/src/main/java/lotto/view/ConsoleInputView.java @@ -6,19 +6,15 @@ public class ConsoleInputView implements InputView { private final Scanner scanner = new Scanner(System.in); - private String nextLine() { - return scanner.nextLine(); - } - public String getPurchaseCost() { - return nextLine(); + return scanner.nextLine(); } public String getWinningLottoNumbers() { - return nextLine(); + return scanner.nextLine(); } public String getWinningLottoBonus() { - return nextLine(); + return scanner.nextLine(); } } From 8d185daa74dac092756669307d0bee5038b1f39f Mon Sep 17 00:00:00 2001 From: Jiwoo Kim Date: Mon, 19 Jul 2021 14:31:04 +0900 Subject: [PATCH 38/55] refactor: remove unncessary constants --- src/main/java/lotto/view/ConsoleOutputView.java | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/src/main/java/lotto/view/ConsoleOutputView.java b/src/main/java/lotto/view/ConsoleOutputView.java index 4e23f28b..3fa79d3d 100644 --- a/src/main/java/lotto/view/ConsoleOutputView.java +++ b/src/main/java/lotto/view/ConsoleOutputView.java @@ -6,11 +6,6 @@ public class ConsoleOutputView implements OutputView { private static final String ERROR_HEADER = "[ERROR] "; - private static final String COMMA = ", "; - private static final String PARENTHESIS_LEFT = "["; - private static final String PARENTHESIS_RIGHT = "]"; - private static final String UNIT = "개"; - private static final String NEW_LINE = "\n"; private void print(String contents) { System.out.println(contents); @@ -28,17 +23,17 @@ public void printLottoSet(RandomLottoSet lottoSet) { } private void printLotto(Lotto lotto) { - String result = PARENTHESIS_LEFT; + String result = "["; for (LottoNumber lottoNumber : lotto.getLottoNumbers()) { - result += lottoNumber.getLottoNumber() + COMMA; + result += lottoNumber.getLottoNumber() + ", "; } - result = removeCommaAtTheEnd(result) + PARENTHESIS_RIGHT; + result = removeCommaAtTheEnd(result) + "]"; print(result); } private String removeCommaAtTheEnd(String str) { - return str.substring(0, str.length() - COMMA.length()); + return str.substring(0, str.length() - 2); } public void askPurchaseCost() { @@ -54,7 +49,7 @@ public void askWinningLottoBonus() { } public void printLottoStatistic(StatisticsResult lottoStatistics) { - String result = "당첨 통계" + NEW_LINE + "---------" + NEW_LINE + + String result = "당첨 통계\n---------\n" + generatePrizeResultMessage(Prize.FIFTH, lottoStatistics.getPrizeCount().getCountFifth()) + generatePrizeResultMessage(Prize.FOURTH, lottoStatistics.getPrizeCount().getCountFourth()) + generatePrizeResultMessage(Prize.THIRD, lottoStatistics.getPrizeCount().getCountThird()) + @@ -65,7 +60,7 @@ public void printLottoStatistic(StatisticsResult lottoStatistics) { } private String generatePrizeResultMessage(Prize prize, int prizeCount) { - return generatePrizeResultMessage(prize) + prizeCount + UNIT + NEW_LINE; + return generatePrizeResultMessage(prize) + prizeCount + "개\n"; } private String generatePrizeResultMessage(Prize prize) { From 60c4c70140b2be7985d4c948377670b9ab622f66 Mon Sep 17 00:00:00 2001 From: Jiwoo Kim Date: Mon, 19 Jul 2021 14:34:32 +0900 Subject: [PATCH 39/55] refactor: unwrap DefaultNumbers class into constant field of RandomLotto --- src/main/java/lotto/domain/DefaultNumbers.java | 17 ----------------- src/main/java/lotto/domain/RandomLotto.java | 7 ++++++- src/main/java/lotto/service/LottoService.java | 2 +- 3 files changed, 7 insertions(+), 19 deletions(-) delete mode 100644 src/main/java/lotto/domain/DefaultNumbers.java diff --git a/src/main/java/lotto/domain/DefaultNumbers.java b/src/main/java/lotto/domain/DefaultNumbers.java deleted file mode 100644 index 7692faea..00000000 --- a/src/main/java/lotto/domain/DefaultNumbers.java +++ /dev/null @@ -1,17 +0,0 @@ -package lotto.domain; - -import lombok.Getter; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.stream.IntStream; - -public class DefaultNumbers { - - @Getter - private static final List defaultNumbers = Collections.unmodifiableList(new ArrayList() {{ - IntStream.range(LottoNumber.LOWER_BOUND, LottoNumber.UPPER_BOUND) - .forEach(this::add); - }}); -} diff --git a/src/main/java/lotto/domain/RandomLotto.java b/src/main/java/lotto/domain/RandomLotto.java index e6841404..f7442cbc 100644 --- a/src/main/java/lotto/domain/RandomLotto.java +++ b/src/main/java/lotto/domain/RandomLotto.java @@ -4,18 +4,23 @@ import java.util.Collections; import java.util.List; import java.util.stream.Collectors; +import java.util.stream.IntStream; public class RandomLotto extends Lotto { private static final int INDEX_LOWER_BOUND = 0; private static final int INDEX_UPPER_BOUND = 6; + private static final List DEFAULT_NUMBERS = Collections.unmodifiableList(new ArrayList() {{ + IntStream.range(LottoNumber.LOWER_BOUND, LottoNumber.UPPER_BOUND) + .forEach(this::add); + }}); public RandomLotto() { super(generateRandomLottoNumbers()); } private static List generateRandomLottoNumbers() throws IllegalArgumentException { - List defaultNumbers = new ArrayList<>(DefaultNumbers.getDefaultNumbers()); + List defaultNumbers = new ArrayList<>(DEFAULT_NUMBERS); Collections.shuffle(defaultNumbers); return defaultNumbers.subList(INDEX_LOWER_BOUND, INDEX_UPPER_BOUND) .stream() diff --git a/src/main/java/lotto/service/LottoService.java b/src/main/java/lotto/service/LottoService.java index cd3ba862..d5c345ae 100644 --- a/src/main/java/lotto/service/LottoService.java +++ b/src/main/java/lotto/service/LottoService.java @@ -20,7 +20,7 @@ public PurchaseResult purchase(PurchasePriceInput purchasePriceInput) { public StatisticsResult calculateResult(PurchaseResult purchaseResult, WinningLottoInput winningLottoInput) { WinningLotto winningLotto = WinningLotto.builder() - .winningLottoInputDTO(winningLottoInput) + .winningLottoInput(winningLottoInput) .build(); PrizeCount prizeCount = PrizeCount.builder() .lottoset(purchaseResult.getRandomLottoSet()) From 2119239dac9a452d1bfbbd80251ac18714c3c332 Mon Sep 17 00:00:00 2001 From: Jiwoo Kim Date: Mon, 19 Jul 2021 14:37:10 +0900 Subject: [PATCH 40/55] test: change Exception class type --- src/test/java/lotto/domain/PurchaseCountTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/lotto/domain/PurchaseCountTest.java b/src/test/java/lotto/domain/PurchaseCountTest.java index b5a10e92..c3a696f1 100644 --- a/src/test/java/lotto/domain/PurchaseCountTest.java +++ b/src/test/java/lotto/domain/PurchaseCountTest.java @@ -54,7 +54,7 @@ void testInputNotNumber() throws Exception { // then assertThatThrownBy(() -> new PurchaseCount(input)) - .isInstanceOf(RuntimeException.class); + .isInstanceOf(IllegalArgumentException.class); } @Test From 41d2147e002c66c6b166ee99b201efd707f79b45 Mon Sep 17 00:00:00 2001 From: Jiwoo Kim Date: Mon, 19 Jul 2021 14:43:50 +0900 Subject: [PATCH 41/55] refactor: rename non-negative integer validation --- src/main/java/lotto/domain/LottoNumber.java | 4 ++-- src/main/java/lotto/domain/PurchaseCount.java | 8 ++++---- src/main/java/lotto/exception/ExceptionMessage.java | 2 +- src/main/java/lotto/util/NumberValidateUtils.java | 2 +- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/main/java/lotto/domain/LottoNumber.java b/src/main/java/lotto/domain/LottoNumber.java index 38849b72..ad7f12a7 100644 --- a/src/main/java/lotto/domain/LottoNumber.java +++ b/src/main/java/lotto/domain/LottoNumber.java @@ -6,7 +6,7 @@ import static lotto.exception.ExceptionMessage.NON_INTEGER_INPUT_FOR_LOTTO_NUMBER; import static lotto.exception.ExceptionMessage.OUT_OF_BOUND_INPUT_FOR_LOTTO_NUMBER; -import static lotto.util.NumberValidateUtils.isInteger; +import static lotto.util.NumberValidateUtils.isNonNegativeInteger; public class LottoNumber { @@ -23,7 +23,7 @@ public LottoNumber(String input) throws IllegalArgumentException { } private void validate(String input) throws IllegalArgumentException { - if (!isInteger(input)) { + if (!isNonNegativeInteger(input)) { throw new IllegalArgumentException(NON_INTEGER_INPUT_FOR_LOTTO_NUMBER.getMessage()); } diff --git a/src/main/java/lotto/domain/PurchaseCount.java b/src/main/java/lotto/domain/PurchaseCount.java index 9113f5c8..8b115f70 100644 --- a/src/main/java/lotto/domain/PurchaseCount.java +++ b/src/main/java/lotto/domain/PurchaseCount.java @@ -3,9 +3,9 @@ import lombok.Builder; import lombok.Getter; -import static lotto.exception.ExceptionMessage.NON_INTEGER_INPUT_FOR_PURCHASE_MONEY; +import static lotto.exception.ExceptionMessage.NEGATIVE_INTEGER_INPUT_FOR_PURCHASE_MONEY; import static lotto.exception.ExceptionMessage.NON_MULTIPLE_OF_LOTTO_PRICE_INPUT_FOR_PURCHASE_MONEY; -import static lotto.util.NumberValidateUtils.isInteger; +import static lotto.util.NumberValidateUtils.isNonNegativeInteger; public class PurchaseCount { @@ -23,8 +23,8 @@ public PurchaseCount(String input) { } private void validate(String input) { - if (!isInteger(input)) { - throw new IllegalArgumentException(NON_INTEGER_INPUT_FOR_PURCHASE_MONEY.getMessage()); + if (!isNonNegativeInteger(input)) { + throw new IllegalArgumentException(NEGATIVE_INTEGER_INPUT_FOR_PURCHASE_MONEY.getMessage()); } int purchaseCount = Integer.parseInt(input); diff --git a/src/main/java/lotto/exception/ExceptionMessage.java b/src/main/java/lotto/exception/ExceptionMessage.java index 9e6c56e9..fb1a83e9 100644 --- a/src/main/java/lotto/exception/ExceptionMessage.java +++ b/src/main/java/lotto/exception/ExceptionMessage.java @@ -9,7 +9,7 @@ public enum ExceptionMessage { NON_INTEGER_INPUT_FOR_LOTTO_NUMBER("로또 번호는 숫자를 입력해주세요."), OUT_OF_BOUND_INPUT_FOR_LOTTO_NUMBER("로또 번호는 1 이상 45 이하의 값을 입력해주세요."), - NON_INTEGER_INPUT_FOR_PURCHASE_MONEY("구입금액은 숫자로 입력해주세요."), + NEGATIVE_INTEGER_INPUT_FOR_PURCHASE_MONEY("구입금액은 숫자로 입력해주세요."), NON_MULTIPLE_OF_LOTTO_PRICE_INPUT_FOR_PURCHASE_MONEY("구입금액은" + Lotto.PRICE + "의 배수로 입력해주세요."), INVALID_LENGTH_INPUT_FOR_WINNING_LOTTO("로또 번호는 6개를 입력해주세요"); diff --git a/src/main/java/lotto/util/NumberValidateUtils.java b/src/main/java/lotto/util/NumberValidateUtils.java index 56ee0b79..2348d907 100644 --- a/src/main/java/lotto/util/NumberValidateUtils.java +++ b/src/main/java/lotto/util/NumberValidateUtils.java @@ -6,7 +6,7 @@ public class NumberValidateUtils { private static final String NUMBER_REGEX = "^[0-9]*$"; - public static boolean isInteger(String input) { + public static boolean isNonNegativeInteger(String input) { return Pattern.matches(NUMBER_REGEX, input); } } From 36c204bca27c4608e1e0b656ea789b4c3c4dc504 Mon Sep 17 00:00:00 2001 From: Jiwoo Kim Date: Mon, 19 Jul 2021 14:46:17 +0900 Subject: [PATCH 42/55] refactor: seperate exception cases of input for PurchaseCount --- src/main/java/lotto/domain/PurchaseCount.java | 14 ++++++-------- .../java/lotto/exception/ExceptionMessage.java | 3 ++- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/main/java/lotto/domain/PurchaseCount.java b/src/main/java/lotto/domain/PurchaseCount.java index 8b115f70..121bb3f5 100644 --- a/src/main/java/lotto/domain/PurchaseCount.java +++ b/src/main/java/lotto/domain/PurchaseCount.java @@ -3,8 +3,7 @@ import lombok.Builder; import lombok.Getter; -import static lotto.exception.ExceptionMessage.NEGATIVE_INTEGER_INPUT_FOR_PURCHASE_MONEY; -import static lotto.exception.ExceptionMessage.NON_MULTIPLE_OF_LOTTO_PRICE_INPUT_FOR_PURCHASE_MONEY; +import static lotto.exception.ExceptionMessage.*; import static lotto.util.NumberValidateUtils.isNonNegativeInteger; public class PurchaseCount { @@ -27,16 +26,15 @@ private void validate(String input) { throw new IllegalArgumentException(NEGATIVE_INTEGER_INPUT_FOR_PURCHASE_MONEY.getMessage()); } - int purchaseCount = Integer.parseInt(input); - if (notMatchesCondition(purchaseCount)) { + int inputValue = Integer.parseInt(input); + if (inputValue < MINIMUM_INPUT) { + throw new IllegalArgumentException(LESS_THAN_LOTTO_PRICE_INPUT_FOR_PURCHASE_MONEY.getMessage()); + } + if (notMultipleOfLottoPrice(inputValue)) { throw new IllegalArgumentException(NON_MULTIPLE_OF_LOTTO_PRICE_INPUT_FOR_PURCHASE_MONEY.getMessage()); } } - private boolean notMatchesCondition(int input) { - return input < MINIMUM_INPUT || notMultipleOfLottoPrice(input); - } - private boolean notMultipleOfLottoPrice(int input) { return input % Lotto.PRICE != 0; } diff --git a/src/main/java/lotto/exception/ExceptionMessage.java b/src/main/java/lotto/exception/ExceptionMessage.java index fb1a83e9..56cabad7 100644 --- a/src/main/java/lotto/exception/ExceptionMessage.java +++ b/src/main/java/lotto/exception/ExceptionMessage.java @@ -10,7 +10,8 @@ public enum ExceptionMessage { NON_INTEGER_INPUT_FOR_LOTTO_NUMBER("로또 번호는 숫자를 입력해주세요."), OUT_OF_BOUND_INPUT_FOR_LOTTO_NUMBER("로또 번호는 1 이상 45 이하의 값을 입력해주세요."), NEGATIVE_INTEGER_INPUT_FOR_PURCHASE_MONEY("구입금액은 숫자로 입력해주세요."), - NON_MULTIPLE_OF_LOTTO_PRICE_INPUT_FOR_PURCHASE_MONEY("구입금액은" + Lotto.PRICE + "의 배수로 입력해주세요."), + LESS_THAN_LOTTO_PRICE_INPUT_FOR_PURCHASE_MONEY("구입금액은 " + Lotto.PRICE + " 이상의 값을 입력해주세요."), + NON_MULTIPLE_OF_LOTTO_PRICE_INPUT_FOR_PURCHASE_MONEY("구입금액은 " + Lotto.PRICE + "의 배수로 입력해주세요."), INVALID_LENGTH_INPUT_FOR_WINNING_LOTTO("로또 번호는 6개를 입력해주세요"); @Getter From 1084404bc7b7f60a54040b0c2a274aec53431325 Mon Sep 17 00:00:00 2001 From: Jiwoo Kim Date: Mon, 19 Jul 2021 15:13:45 +0900 Subject: [PATCH 43/55] refactor: merge RandomLottoSet into LottoSet --- src/main/java/lotto/domain/LottoSet.java | 13 +++++++++++ .../java/lotto/domain/RandomLottoSet.java | 19 ---------------- .../java/lotto/domain/dto/PurchaseResult.java | 4 ++-- src/main/java/lotto/service/LottoService.java | 6 ++--- .../java/lotto/view/ConsoleOutputView.java | 2 +- src/main/java/lotto/view/OutputView.java | 4 ++-- src/main/java/lotto/view/View.java | 2 +- ...domLottoSetTest.java => LottoSetTest.java} | 8 +++---- .../java/lotto/domain/RandomLottoTest.java | 22 ------------------- 9 files changed, 26 insertions(+), 54 deletions(-) delete mode 100644 src/main/java/lotto/domain/RandomLottoSet.java rename src/test/java/lotto/domain/{RandomLottoSetTest.java => LottoSetTest.java} (65%) delete mode 100644 src/test/java/lotto/domain/RandomLottoTest.java diff --git a/src/main/java/lotto/domain/LottoSet.java b/src/main/java/lotto/domain/LottoSet.java index 18e52ff0..ea9058f2 100644 --- a/src/main/java/lotto/domain/LottoSet.java +++ b/src/main/java/lotto/domain/LottoSet.java @@ -3,6 +3,7 @@ import lombok.Getter; import java.util.Collections; +import java.util.HashSet; import java.util.Set; public class LottoSet { @@ -13,4 +14,16 @@ public class LottoSet { public LottoSet(Set lottoSet) { this.lottoSet = Collections.unmodifiableSet(lottoSet); } + + public LottoSet(PurchaseCount purchaseCount) { + this(generateRandomLottoSetWithSize(purchaseCount)); + } + + private static Set generateRandomLottoSetWithSize(PurchaseCount purchaseCount) { + Set lottoSet = new HashSet<>(); + for (int i = 0; i < purchaseCount.getPurchaseCount(); i++) { + lottoSet.add(new RandomLotto()); + } + return lottoSet; + } } diff --git a/src/main/java/lotto/domain/RandomLottoSet.java b/src/main/java/lotto/domain/RandomLottoSet.java deleted file mode 100644 index 8e8d4114..00000000 --- a/src/main/java/lotto/domain/RandomLottoSet.java +++ /dev/null @@ -1,19 +0,0 @@ -package lotto.domain; - -import java.util.HashSet; -import java.util.Set; - -public class RandomLottoSet extends LottoSet { - - public RandomLottoSet(PurchaseCount purchaseCount) { - super(generateRandomLottoSetWithSize(purchaseCount.getPurchaseCount())); - } - - private static Set generateRandomLottoSetWithSize(int size) { - Set lottoSet = new HashSet<>(); - for (int i = 0; i < size; i++) { - lottoSet.add(new RandomLotto()); - } - return lottoSet; - } -} diff --git a/src/main/java/lotto/domain/dto/PurchaseResult.java b/src/main/java/lotto/domain/dto/PurchaseResult.java index 4f2912e6..40efb485 100644 --- a/src/main/java/lotto/domain/dto/PurchaseResult.java +++ b/src/main/java/lotto/domain/dto/PurchaseResult.java @@ -3,8 +3,8 @@ import lombok.Builder; import lombok.Getter; import lombok.RequiredArgsConstructor; +import lotto.domain.LottoSet; import lotto.domain.PurchaseCount; -import lotto.domain.RandomLottoSet; @Getter @RequiredArgsConstructor @@ -12,5 +12,5 @@ public class PurchaseResult { private final PurchaseCount purchaseCount; - private final RandomLottoSet randomLottoSet; + private final LottoSet lottoSet; } diff --git a/src/main/java/lotto/service/LottoService.java b/src/main/java/lotto/service/LottoService.java index d5c345ae..3f631ce0 100644 --- a/src/main/java/lotto/service/LottoService.java +++ b/src/main/java/lotto/service/LottoService.java @@ -10,11 +10,11 @@ public class LottoService { public PurchaseResult purchase(PurchasePriceInput purchasePriceInput) { PurchaseCount purchaseCount = new PurchaseCount(purchasePriceInput.getInput()); - RandomLottoSet randomLottoSet = new RandomLottoSet(purchaseCount); + LottoSet lottoSet = new LottoSet(purchaseCount); return PurchaseResult.builder() .purchaseCount(purchaseCount) - .randomLottoSet(randomLottoSet) + .lottoSet(lottoSet) .build(); } @@ -23,7 +23,7 @@ public StatisticsResult calculateResult(PurchaseResult purchaseResult, WinningLo .winningLottoInput(winningLottoInput) .build(); PrizeCount prizeCount = PrizeCount.builder() - .lottoset(purchaseResult.getRandomLottoSet()) + .lottoset(purchaseResult.getLottoSet()) .winningLotto(winningLotto) .build(); LottoStatistics lottoStatistics = LottoStatistics.builder() diff --git a/src/main/java/lotto/view/ConsoleOutputView.java b/src/main/java/lotto/view/ConsoleOutputView.java index 3fa79d3d..2453d6bc 100644 --- a/src/main/java/lotto/view/ConsoleOutputView.java +++ b/src/main/java/lotto/view/ConsoleOutputView.java @@ -16,7 +16,7 @@ public void printLottoCount(PurchaseCount purchaseCount) { print(count + "개를 구매했습니다."); } - public void printLottoSet(RandomLottoSet lottoSet) { + public void printLottoSet(LottoSet lottoSet) { for (Lotto lotto : lottoSet.getLottoSet()) { printLotto(lotto); } diff --git a/src/main/java/lotto/view/OutputView.java b/src/main/java/lotto/view/OutputView.java index ad773c29..731e0116 100644 --- a/src/main/java/lotto/view/OutputView.java +++ b/src/main/java/lotto/view/OutputView.java @@ -1,7 +1,7 @@ package lotto.view; +import lotto.domain.LottoSet; import lotto.domain.PurchaseCount; -import lotto.domain.RandomLottoSet; import lotto.domain.dto.StatisticsResult; public interface OutputView { @@ -9,7 +9,7 @@ public interface OutputView { void printLottoCount(PurchaseCount purchaseCount); - void printLottoSet(RandomLottoSet randomLottoSet); + void printLottoSet(LottoSet LottoSet); void askWinningLottoNumbers(); diff --git a/src/main/java/lotto/view/View.java b/src/main/java/lotto/view/View.java index 19b1d82a..7c2c5ae8 100644 --- a/src/main/java/lotto/view/View.java +++ b/src/main/java/lotto/view/View.java @@ -46,6 +46,6 @@ public void printException(String message) { public void printLottoPurchaseResult(PurchaseResult purchaseResult) { outputView.printLottoCount(purchaseResult.getPurchaseCount()); - outputView.printLottoSet(purchaseResult.getRandomLottoSet()); + outputView.printLottoSet(purchaseResult.getLottoSet()); } } diff --git a/src/test/java/lotto/domain/RandomLottoSetTest.java b/src/test/java/lotto/domain/LottoSetTest.java similarity index 65% rename from src/test/java/lotto/domain/RandomLottoSetTest.java rename to src/test/java/lotto/domain/LottoSetTest.java index 035af028..e61b0d50 100644 --- a/src/test/java/lotto/domain/RandomLottoSetTest.java +++ b/src/test/java/lotto/domain/LottoSetTest.java @@ -5,19 +5,19 @@ import static org.assertj.core.api.Assertions.assertThat; -public class RandomLottoSetTest { +public class LottoSetTest { @Test @DisplayName("개수를 입력받으면 개수만큼 랜덤 로또를 생성한다") - void testGenerateLottoSet() throws Exception { + void testGenerateRandomLottoSet() throws Exception { // given int targetSize = 10; PurchaseCount purchaseCount = new PurchaseCount(String.valueOf(targetSize * Lotto.PRICE)); // when - RandomLottoSet randomLottoSet = new RandomLottoSet(purchaseCount); + LottoSet lottoSet = new LottoSet(purchaseCount); // then - assertThat(randomLottoSet.getLottoSet().size()).isEqualTo(targetSize); + assertThat(lottoSet.getLottoSet().size()).isEqualTo(targetSize); } } diff --git a/src/test/java/lotto/domain/RandomLottoTest.java b/src/test/java/lotto/domain/RandomLottoTest.java deleted file mode 100644 index 087324bd..00000000 --- a/src/test/java/lotto/domain/RandomLottoTest.java +++ /dev/null @@ -1,22 +0,0 @@ -package lotto.domain; - -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; - -import static org.assertj.core.api.Assertions.assertThat; - -public class RandomLottoTest { - - @Test - @DisplayName("숫자가 6개인 로또를 생성한다") - void testGenerateLotto() throws Exception { - // given - RandomLotto randomLotto = new RandomLotto(); - - // when - int size = randomLotto.getLottoNumbers().size(); - - // then - assertThat(size).isEqualTo(Lotto.LOTTO_NUMBER_SIZE); - } -} From 2056c66c430629faa8f24f2e9ccf1f401af3d9f3 Mon Sep 17 00:00:00 2001 From: Jiwoo Kim Date: Mon, 19 Jul 2021 16:14:18 +0900 Subject: [PATCH 44/55] fix: prevent duplication of Lottos in LottoSet --- src/main/java/lotto/domain/Lotto.java | 18 +++++++++++-- src/main/java/lotto/domain/LottoSet.java | 6 ++--- src/test/java/lotto/domain/LottoTest.java | 32 +++++++++++++++++++++++ 3 files changed, 51 insertions(+), 5 deletions(-) create mode 100644 src/test/java/lotto/domain/LottoTest.java diff --git a/src/main/java/lotto/domain/Lotto.java b/src/main/java/lotto/domain/Lotto.java index 513132a8..6d60cb7b 100644 --- a/src/main/java/lotto/domain/Lotto.java +++ b/src/main/java/lotto/domain/Lotto.java @@ -2,7 +2,7 @@ import lombok.Getter; -import java.util.List; +import java.util.*; public class Lotto { @@ -13,6 +13,20 @@ public class Lotto { protected List lottoNumbers; public Lotto(List lottoNumbers) { - this.lottoNumbers = lottoNumbers; + lottoNumbers.sort(Comparator.comparingInt(LottoNumber::getLottoNumber)); + this.lottoNumbers = Collections.unmodifiableList(new ArrayList<>(lottoNumbers)); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Lotto lotto = (Lotto) o; + return Objects.equals(lottoNumbers, lotto.lottoNumbers); + } + + @Override + public int hashCode() { + return Objects.hash(lottoNumbers); } } diff --git a/src/main/java/lotto/domain/LottoSet.java b/src/main/java/lotto/domain/LottoSet.java index ea9058f2..0dfe79c2 100644 --- a/src/main/java/lotto/domain/LottoSet.java +++ b/src/main/java/lotto/domain/LottoSet.java @@ -16,12 +16,12 @@ public LottoSet(Set lottoSet) { } public LottoSet(PurchaseCount purchaseCount) { - this(generateRandomLottoSetWithSize(purchaseCount)); + this(generateRandomLottoSetWithSize(purchaseCount.getPurchaseCount())); } - private static Set generateRandomLottoSetWithSize(PurchaseCount purchaseCount) { + private static Set generateRandomLottoSetWithSize(int targetSize) { Set lottoSet = new HashSet<>(); - for (int i = 0; i < purchaseCount.getPurchaseCount(); i++) { + while (lottoSet.size() < targetSize) { lottoSet.add(new RandomLotto()); } return lottoSet; diff --git a/src/test/java/lotto/domain/LottoTest.java b/src/test/java/lotto/domain/LottoTest.java new file mode 100644 index 00000000..0f75a55a --- /dev/null +++ b/src/test/java/lotto/domain/LottoTest.java @@ -0,0 +1,32 @@ +package lotto.domain; + +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; + +public class LottoTest { + + @Test + void testEqualityOfLottos() { + // given + List lottoNumbersOne = new ArrayList<>(); + lottoNumbersOne.add(new LottoNumber("1")); + lottoNumbersOne.add(new LottoNumber("30")); + lottoNumbersOne.add(new LottoNumber("22")); + + List lottoNumbersTwo = new ArrayList<>(); + lottoNumbersTwo.add(new LottoNumber("22")); + lottoNumbersTwo.add(new LottoNumber("30")); + lottoNumbersTwo.add(new LottoNumber("1")); + + // when + Lotto one = new Lotto(lottoNumbersOne); + Lotto two = new Lotto(lottoNumbersTwo); + + // then + assertThat(one).isEqualTo(two); + } +} From a44450bf40688f14aa029abc3a627a612b6076ca Mon Sep 17 00:00:00 2001 From: Jiwoo Kim Date: Mon, 19 Jul 2021 17:30:50 +0900 Subject: [PATCH 45/55] refactor: validate input type at InputView not domain --- .../lotto/controller/LottoController.java | 2 + src/main/java/lotto/domain/Lotto.java | 23 ++- src/main/java/lotto/domain/LottoNumber.java | 15 +- src/main/java/lotto/domain/LottoSet.java | 6 +- src/main/java/lotto/domain/PurchaseCount.java | 24 +-- src/main/java/lotto/domain/RandomLotto.java | 9 +- src/main/java/lotto/domain/WinningLotto.java | 9 +- .../lotto/domain/dto/PurchasePriceInput.java | 2 +- .../lotto/domain/dto/WinningLottoInput.java | 6 +- src/main/java/lotto/parser/LottoParser.java | 32 ---- src/main/java/lotto/service/LottoService.java | 2 +- .../java/lotto/view/ConsoleInputView.java | 23 ++- src/main/java/lotto/view/InputView.java | 8 +- src/main/java/lotto/view/View.java | 16 +- .../java/lotto/domain/LottoNumberTest.java | 18 +- src/test/java/lotto/domain/LottoSetTest.java | 2 +- .../lotto/domain/LottoStatisticsTest.java | 4 +- src/test/java/lotto/domain/LottoTest.java | 24 ++- .../java/lotto/domain/PrizeCountTest.java | 12 +- src/test/java/lotto/domain/PrizeTest.java | 1 - .../java/lotto/domain/PurchaseCountTest.java | 70 +------- src/test/java/lotto/domain/TestLottoSet.java | 42 ----- .../java/lotto/domain/TestWinningLotto.java | 16 -- .../java/lotto/domain/WinningLottoTest.java | 157 ++++-------------- src/test/java/lotto/fixture/TestLottoSet.java | 43 +++++ .../java/lotto/fixture/TestWinningLotto.java | 23 +++ 26 files changed, 216 insertions(+), 373 deletions(-) delete mode 100644 src/main/java/lotto/parser/LottoParser.java delete mode 100644 src/test/java/lotto/domain/TestLottoSet.java delete mode 100644 src/test/java/lotto/domain/TestWinningLotto.java create mode 100644 src/test/java/lotto/fixture/TestLottoSet.java create mode 100644 src/test/java/lotto/fixture/TestWinningLotto.java diff --git a/src/main/java/lotto/controller/LottoController.java b/src/main/java/lotto/controller/LottoController.java index 706eba55..0ee73abf 100644 --- a/src/main/java/lotto/controller/LottoController.java +++ b/src/main/java/lotto/controller/LottoController.java @@ -26,6 +26,8 @@ public void start() throws IllegalArgumentException { WinningLottoInput winningLottoInput = view.getWinningLottoAndBonus(); StatisticsResult statisticsResult = lottoService.calculateResult(purchaseResult, winningLottoInput); view.printLottoStatistics(statisticsResult); + } catch (NumberFormatException e) { + view.printException(e.getMessage()); } catch (IllegalArgumentException e) { view.printException(e.getMessage()); } diff --git a/src/main/java/lotto/domain/Lotto.java b/src/main/java/lotto/domain/Lotto.java index 6d60cb7b..9fa87be9 100644 --- a/src/main/java/lotto/domain/Lotto.java +++ b/src/main/java/lotto/domain/Lotto.java @@ -2,7 +2,12 @@ import lombok.Getter; -import java.util.*; +import java.util.Collections; +import java.util.List; +import java.util.Objects; +import java.util.stream.Collectors; + +import static lotto.exception.ExceptionMessage.INVALID_LENGTH_INPUT_FOR_WINNING_LOTTO; public class Lotto { @@ -12,9 +17,19 @@ public class Lotto { @Getter protected List lottoNumbers; - public Lotto(List lottoNumbers) { - lottoNumbers.sort(Comparator.comparingInt(LottoNumber::getLottoNumber)); - this.lottoNumbers = Collections.unmodifiableList(new ArrayList<>(lottoNumbers)); + public Lotto(List numbers) { + validate(numbers); + + numbers.sort(Integer::compare); + this.lottoNumbers = Collections.unmodifiableList(numbers.stream() + .map(LottoNumber::new) + .collect(Collectors.toList())); + } + + private void validate(List numbers) throws IllegalArgumentException { + if (numbers.size() != Lotto.LOTTO_NUMBER_SIZE) { + throw new IllegalArgumentException(INVALID_LENGTH_INPUT_FOR_WINNING_LOTTO.getMessage()); + } } @Override diff --git a/src/main/java/lotto/domain/LottoNumber.java b/src/main/java/lotto/domain/LottoNumber.java index ad7f12a7..16439c31 100644 --- a/src/main/java/lotto/domain/LottoNumber.java +++ b/src/main/java/lotto/domain/LottoNumber.java @@ -4,9 +4,7 @@ import java.util.Objects; -import static lotto.exception.ExceptionMessage.NON_INTEGER_INPUT_FOR_LOTTO_NUMBER; import static lotto.exception.ExceptionMessage.OUT_OF_BOUND_INPUT_FOR_LOTTO_NUMBER; -import static lotto.util.NumberValidateUtils.isNonNegativeInteger; public class LottoNumber { @@ -16,18 +14,13 @@ public class LottoNumber { @Getter private final int lottoNumber; - public LottoNumber(String input) throws IllegalArgumentException { - validate(input); + public LottoNumber(int lottoNumber) throws IllegalArgumentException { + validate(lottoNumber); - this.lottoNumber = Integer.parseInt(input); + this.lottoNumber = lottoNumber; } - private void validate(String input) throws IllegalArgumentException { - if (!isNonNegativeInteger(input)) { - throw new IllegalArgumentException(NON_INTEGER_INPUT_FOR_LOTTO_NUMBER.getMessage()); - } - - int lottoNumber = Integer.parseInt(input); + private void validate(int lottoNumber) throws IllegalArgumentException { if (isOutOfBound(lottoNumber)) { throw new IllegalArgumentException(OUT_OF_BOUND_INPUT_FOR_LOTTO_NUMBER.getMessage()); } diff --git a/src/main/java/lotto/domain/LottoSet.java b/src/main/java/lotto/domain/LottoSet.java index 0dfe79c2..41be7d6f 100644 --- a/src/main/java/lotto/domain/LottoSet.java +++ b/src/main/java/lotto/domain/LottoSet.java @@ -16,12 +16,12 @@ public LottoSet(Set lottoSet) { } public LottoSet(PurchaseCount purchaseCount) { - this(generateRandomLottoSetWithSize(purchaseCount.getPurchaseCount())); + this(generateRandomLottoSet(purchaseCount)); } - private static Set generateRandomLottoSetWithSize(int targetSize) { + private static Set generateRandomLottoSet(PurchaseCount purchaseCount) { Set lottoSet = new HashSet<>(); - while (lottoSet.size() < targetSize) { + while (lottoSet.size() < purchaseCount.getPurchaseCount()) { lottoSet.add(new RandomLotto()); } return lottoSet; diff --git a/src/main/java/lotto/domain/PurchaseCount.java b/src/main/java/lotto/domain/PurchaseCount.java index 121bb3f5..9096169e 100644 --- a/src/main/java/lotto/domain/PurchaseCount.java +++ b/src/main/java/lotto/domain/PurchaseCount.java @@ -3,34 +3,28 @@ import lombok.Builder; import lombok.Getter; -import static lotto.exception.ExceptionMessage.*; -import static lotto.util.NumberValidateUtils.isNonNegativeInteger; +import static lotto.exception.ExceptionMessage.LESS_THAN_LOTTO_PRICE_INPUT_FOR_PURCHASE_MONEY; +import static lotto.exception.ExceptionMessage.NON_MULTIPLE_OF_LOTTO_PRICE_INPUT_FOR_PURCHASE_MONEY; public class PurchaseCount { private static final int MINIMUM_INPUT = 1000; @Getter - private final int purchaseCount; + private final Integer purchaseCount; @Builder - public PurchaseCount(String input) { - input = input.trim(); - validate(input); + public PurchaseCount(int purchasePrice) { + validate(purchasePrice); - this.purchaseCount = Integer.parseInt(input) / Lotto.PRICE; + this.purchaseCount = purchasePrice / Lotto.PRICE; } - private void validate(String input) { - if (!isNonNegativeInteger(input)) { - throw new IllegalArgumentException(NEGATIVE_INTEGER_INPUT_FOR_PURCHASE_MONEY.getMessage()); - } - - int inputValue = Integer.parseInt(input); - if (inputValue < MINIMUM_INPUT) { + private void validate(int purchasePrice) { + if (purchasePrice < MINIMUM_INPUT) { throw new IllegalArgumentException(LESS_THAN_LOTTO_PRICE_INPUT_FOR_PURCHASE_MONEY.getMessage()); } - if (notMultipleOfLottoPrice(inputValue)) { + if (notMultipleOfLottoPrice(purchasePrice)) { throw new IllegalArgumentException(NON_MULTIPLE_OF_LOTTO_PRICE_INPUT_FOR_PURCHASE_MONEY.getMessage()); } } diff --git a/src/main/java/lotto/domain/RandomLotto.java b/src/main/java/lotto/domain/RandomLotto.java index f7442cbc..fd0748ec 100644 --- a/src/main/java/lotto/domain/RandomLotto.java +++ b/src/main/java/lotto/domain/RandomLotto.java @@ -3,7 +3,6 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; -import java.util.stream.Collectors; import java.util.stream.IntStream; public class RandomLotto extends Lotto { @@ -19,13 +18,9 @@ public RandomLotto() { super(generateRandomLottoNumbers()); } - private static List generateRandomLottoNumbers() throws IllegalArgumentException { + private static List generateRandomLottoNumbers() throws IllegalArgumentException { List defaultNumbers = new ArrayList<>(DEFAULT_NUMBERS); Collections.shuffle(defaultNumbers); - return defaultNumbers.subList(INDEX_LOWER_BOUND, INDEX_UPPER_BOUND) - .stream() - .map(String::valueOf) - .map(LottoNumber::new) - .collect(Collectors.toList()); + return new ArrayList<>(defaultNumbers.subList(INDEX_LOWER_BOUND, INDEX_UPPER_BOUND)); } } diff --git a/src/main/java/lotto/domain/WinningLotto.java b/src/main/java/lotto/domain/WinningLotto.java index 63dcdef5..437076fa 100644 --- a/src/main/java/lotto/domain/WinningLotto.java +++ b/src/main/java/lotto/domain/WinningLotto.java @@ -3,9 +3,6 @@ import lombok.Builder; import lombok.Getter; import lotto.domain.dto.WinningLottoInput; -import lotto.parser.LottoParser; - -import java.util.Comparator; public class WinningLotto extends Lotto { @@ -14,9 +11,8 @@ public class WinningLotto extends Lotto { @Builder public WinningLotto(WinningLottoInput winningLottoInput) throws IllegalArgumentException { - super(LottoParser.parseInputIntoLottoNumbers(winningLottoInput.getWinningLottoNumbers())); - this.bonusNumber = new LottoNumber(winningLottoInput.getWinningLottoBonus()); - this.lottoNumbers.sort(Comparator.comparingInt(LottoNumber::getLottoNumber)); + super(winningLottoInput.getNumbers()); + this.bonusNumber = new LottoNumber(winningLottoInput.getBonus()); } public Prize findPrizeCondition(Lotto targetLotto) { @@ -24,7 +20,6 @@ public Prize findPrizeCondition(Lotto targetLotto) { } private int getMatchNumbersCount(Lotto targetLotto) { - targetLotto.getLottoNumbers().sort(Comparator.comparingInt(LottoNumber::getLottoNumber)); int targetIdx = 0, winningIdx = 0; int matchNumbersCount = 0; while (targetIdx < targetLotto.lottoNumbers.size() && winningIdx < this.lottoNumbers.size()) { diff --git a/src/main/java/lotto/domain/dto/PurchasePriceInput.java b/src/main/java/lotto/domain/dto/PurchasePriceInput.java index 9f7052bd..87d2e460 100644 --- a/src/main/java/lotto/domain/dto/PurchasePriceInput.java +++ b/src/main/java/lotto/domain/dto/PurchasePriceInput.java @@ -9,5 +9,5 @@ @RequiredArgsConstructor public class PurchasePriceInput { - private final String input; + private final Integer price; } diff --git a/src/main/java/lotto/domain/dto/WinningLottoInput.java b/src/main/java/lotto/domain/dto/WinningLottoInput.java index 0a975eea..65fd3eb3 100644 --- a/src/main/java/lotto/domain/dto/WinningLottoInput.java +++ b/src/main/java/lotto/domain/dto/WinningLottoInput.java @@ -4,11 +4,13 @@ import lombok.Getter; import lombok.RequiredArgsConstructor; +import java.util.List; + @Getter @Builder @RequiredArgsConstructor public class WinningLottoInput { - private final String winningLottoNumbers; - private final String winningLottoBonus; + private final List numbers; + private final Integer bonus; } diff --git a/src/main/java/lotto/parser/LottoParser.java b/src/main/java/lotto/parser/LottoParser.java deleted file mode 100644 index 08668628..00000000 --- a/src/main/java/lotto/parser/LottoParser.java +++ /dev/null @@ -1,32 +0,0 @@ -package lotto.parser; - -import lotto.domain.Lotto; -import lotto.domain.LottoNumber; - -import java.util.Arrays; -import java.util.List; -import java.util.stream.Collectors; - -import static lotto.exception.ExceptionMessage.INVALID_LENGTH_INPUT_FOR_WINNING_LOTTO; - -public class LottoParser { - - private static final String DELIMITER = ","; - - public static List parseInputIntoLottoNumbers(String numberInput) throws IllegalArgumentException { - List lottoNumbers = Arrays.stream(numberInput.split(DELIMITER)) - .map(String::trim) - .map(LottoNumber::new) - .collect(Collectors.toList()); - - validate(lottoNumbers); - - return lottoNumbers; - } - - private static void validate(List lottoNumbers) throws IllegalArgumentException { - if (lottoNumbers.size() != Lotto.LOTTO_NUMBER_SIZE) { - throw new IllegalArgumentException(INVALID_LENGTH_INPUT_FOR_WINNING_LOTTO.getMessage()); - } - } -} diff --git a/src/main/java/lotto/service/LottoService.java b/src/main/java/lotto/service/LottoService.java index 3f631ce0..8f9c2e8e 100644 --- a/src/main/java/lotto/service/LottoService.java +++ b/src/main/java/lotto/service/LottoService.java @@ -9,7 +9,7 @@ public class LottoService { public PurchaseResult purchase(PurchasePriceInput purchasePriceInput) { - PurchaseCount purchaseCount = new PurchaseCount(purchasePriceInput.getInput()); + PurchaseCount purchaseCount = new PurchaseCount(purchasePriceInput.getPrice()); LottoSet lottoSet = new LottoSet(purchaseCount); return PurchaseResult.builder() diff --git a/src/main/java/lotto/view/ConsoleInputView.java b/src/main/java/lotto/view/ConsoleInputView.java index 52ecb927..97660a45 100644 --- a/src/main/java/lotto/view/ConsoleInputView.java +++ b/src/main/java/lotto/view/ConsoleInputView.java @@ -1,20 +1,31 @@ package lotto.view; +import java.util.Arrays; +import java.util.List; import java.util.Scanner; +import java.util.stream.Collectors; public class ConsoleInputView implements InputView { + private static final String LOTTO_NUMBERS_INPUT_DELIMITER = ","; + private final Scanner scanner = new Scanner(System.in); - public String getPurchaseCost() { - return scanner.nextLine(); + public Integer getPurchaseCost() throws NumberFormatException { + String input = scanner.nextLine().trim(); + return Integer.parseInt(input); } - public String getWinningLottoNumbers() { - return scanner.nextLine(); + public List getWinningLottoNumbers() throws NumberFormatException { + String input = scanner.nextLine(); + return Arrays.stream(input.split(LOTTO_NUMBERS_INPUT_DELIMITER)) + .map(String::trim) + .map(Integer::new) + .collect(Collectors.toList()); } - public String getWinningLottoBonus() { - return scanner.nextLine(); + public Integer getWinningLottoBonus() throws NumberFormatException { + String input = scanner.nextLine().trim(); + return Integer.parseInt(input); } } diff --git a/src/main/java/lotto/view/InputView.java b/src/main/java/lotto/view/InputView.java index 8aed9a18..c08ea3c0 100644 --- a/src/main/java/lotto/view/InputView.java +++ b/src/main/java/lotto/view/InputView.java @@ -1,9 +1,11 @@ package lotto.view; +import java.util.List; + public interface InputView { - String getPurchaseCost(); + Integer getPurchaseCost(); - String getWinningLottoNumbers(); + List getWinningLottoNumbers(); - String getWinningLottoBonus(); + Integer getWinningLottoBonus(); } diff --git a/src/main/java/lotto/view/View.java b/src/main/java/lotto/view/View.java index 7c2c5ae8..5b6d270a 100644 --- a/src/main/java/lotto/view/View.java +++ b/src/main/java/lotto/view/View.java @@ -7,6 +7,8 @@ import lotto.domain.dto.StatisticsResult; import lotto.domain.dto.WinningLottoInput; +import java.util.List; + @RequiredArgsConstructor @Builder public class View { @@ -16,23 +18,23 @@ public class View { public PurchasePriceInput getPurchaseCost() { outputView.askPurchaseCost(); - String purchaseCountInput = inputView.getPurchaseCost(); + Integer purchaseCountInput = inputView.getPurchaseCost(); return PurchasePriceInput.builder() - .input(purchaseCountInput) + .price(purchaseCountInput) .build(); } - public WinningLottoInput getWinningLottoAndBonus() { + public WinningLottoInput getWinningLottoAndBonus() throws NumberFormatException { outputView.askWinningLottoNumbers(); - String winningLottoNumbers = inputView.getWinningLottoNumbers(); + List numbers = inputView.getWinningLottoNumbers(); outputView.askWinningLottoBonus(); - String winningLottoBonus = inputView.getWinningLottoBonus(); + Integer bonus = inputView.getWinningLottoBonus(); return WinningLottoInput.builder() - .winningLottoNumbers(winningLottoNumbers) - .winningLottoBonus(winningLottoBonus) + .numbers(numbers) + .bonus(bonus) .build(); } diff --git a/src/test/java/lotto/domain/LottoNumberTest.java b/src/test/java/lotto/domain/LottoNumberTest.java index 2e084c51..d3842c1d 100644 --- a/src/test/java/lotto/domain/LottoNumberTest.java +++ b/src/test/java/lotto/domain/LottoNumberTest.java @@ -15,22 +15,22 @@ public class LottoNumberTest { @DisplayName("주어진 1~45 숫자로 LottoNumber 객체를 생성한다") void testGenerateLottoNumberWithInteger() { // given - List inputNumbers = new ArrayList<>(); - inputNumbers.add("1"); - inputNumbers.add("2"); - inputNumbers.add("44"); - inputNumbers.add("45"); + List inputNumbers = new ArrayList<>(); + inputNumbers.add(1); + inputNumbers.add(2); + inputNumbers.add(44); + inputNumbers.add(45); List lottoNumbers = new ArrayList<>(); // when - for (String inputNumber : inputNumbers) { + for (Integer inputNumber : inputNumbers) { lottoNumbers.add(new LottoNumber(inputNumber)); } // then for (int i = 0; i { @@ -50,7 +50,7 @@ void testGenerateLottoNumberWithIntegerLessThanLowerBound() { @DisplayName("45보다 큰 숫자로 LottoNumber 생성 시 예외를 던진다") void testGenerateLottoNumberWithIntegerGreaterThanUpperBound() { //given - String inputNumber = "46"; + int inputNumber = 46; //when, then assertThrows(IllegalArgumentException.class, ()-> { diff --git a/src/test/java/lotto/domain/LottoSetTest.java b/src/test/java/lotto/domain/LottoSetTest.java index e61b0d50..f58b6b73 100644 --- a/src/test/java/lotto/domain/LottoSetTest.java +++ b/src/test/java/lotto/domain/LottoSetTest.java @@ -12,7 +12,7 @@ public class LottoSetTest { void testGenerateRandomLottoSet() throws Exception { // given int targetSize = 10; - PurchaseCount purchaseCount = new PurchaseCount(String.valueOf(targetSize * Lotto.PRICE)); + PurchaseCount purchaseCount = new PurchaseCount(targetSize * Lotto.PRICE); // when LottoSet lottoSet = new LottoSet(purchaseCount); diff --git a/src/test/java/lotto/domain/LottoStatisticsTest.java b/src/test/java/lotto/domain/LottoStatisticsTest.java index 58a9e348..99f37b7d 100644 --- a/src/test/java/lotto/domain/LottoStatisticsTest.java +++ b/src/test/java/lotto/domain/LottoStatisticsTest.java @@ -1,5 +1,7 @@ package lotto.domain; +import lotto.fixture.TestLottoSet; +import lotto.fixture.TestWinningLotto; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -17,7 +19,7 @@ public class LottoStatisticsTest { void testLottoStatistics() { //given, when LottoStatistics lottoStatistics = new LottoStatistics(PRIZE_COUNT, - new PurchaseCount(String.valueOf(PURCHASE_MONEY))); + new PurchaseCount(PURCHASE_MONEY)); // then assertThat(lottoStatistics.calculateProfitRate()).isEqualTo(RESULT); diff --git a/src/test/java/lotto/domain/LottoTest.java b/src/test/java/lotto/domain/LottoTest.java index 0f75a55a..b7c26c9c 100644 --- a/src/test/java/lotto/domain/LottoTest.java +++ b/src/test/java/lotto/domain/LottoTest.java @@ -12,15 +12,21 @@ public class LottoTest { @Test void testEqualityOfLottos() { // given - List lottoNumbersOne = new ArrayList<>(); - lottoNumbersOne.add(new LottoNumber("1")); - lottoNumbersOne.add(new LottoNumber("30")); - lottoNumbersOne.add(new LottoNumber("22")); - - List lottoNumbersTwo = new ArrayList<>(); - lottoNumbersTwo.add(new LottoNumber("22")); - lottoNumbersTwo.add(new LottoNumber("30")); - lottoNumbersTwo.add(new LottoNumber("1")); + List lottoNumbersOne = new ArrayList<>(); + lottoNumbersOne.add(1); + lottoNumbersOne.add(30); + lottoNumbersOne.add(22); + lottoNumbersOne.add(5); + lottoNumbersOne.add(42); + lottoNumbersOne.add(17); + + List lottoNumbersTwo = new ArrayList<>(); + lottoNumbersTwo.add(17); + lottoNumbersTwo.add(22); + lottoNumbersTwo.add(5); + lottoNumbersTwo.add(30); + lottoNumbersTwo.add(1); + lottoNumbersTwo.add(42); // when Lotto one = new Lotto(lottoNumbersOne); diff --git a/src/test/java/lotto/domain/PrizeCountTest.java b/src/test/java/lotto/domain/PrizeCountTest.java index ee616052..ff1900c0 100644 --- a/src/test/java/lotto/domain/PrizeCountTest.java +++ b/src/test/java/lotto/domain/PrizeCountTest.java @@ -1,5 +1,7 @@ package lotto.domain; +import lotto.fixture.TestLottoSet; +import lotto.fixture.TestWinningLotto; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -8,7 +10,7 @@ public class PrizeCountTest { @Test - @DisplayName("로또 여러 게임의 당첨 통계를 구한다") + @DisplayName("로또 게임의 당첨 통계를 구한다") void testCountPrizesOfLottoSet() { //given TestLottoSet lottoSet = new TestLottoSet(); @@ -19,9 +21,9 @@ void testCountPrizesOfLottoSet() { //then assertThat(prizeCount.getCountFirst()).isEqualTo(1); - assertThat(prizeCount.getCountSecond()).isEqualTo(2); - assertThat(prizeCount.getCountThird()).isEqualTo(3); - assertThat(prizeCount.getCountFourth()).isEqualTo(4); - assertThat(prizeCount.getCountFifth()).isEqualTo(5); + assertThat(prizeCount.getCountSecond()).isEqualTo(1); + assertThat(prizeCount.getCountThird()).isEqualTo(2); + assertThat(prizeCount.getCountFourth()).isEqualTo(2); + assertThat(prizeCount.getCountFifth()).isEqualTo(2); } } diff --git a/src/test/java/lotto/domain/PrizeTest.java b/src/test/java/lotto/domain/PrizeTest.java index 777168ce..7d817deb 100644 --- a/src/test/java/lotto/domain/PrizeTest.java +++ b/src/test/java/lotto/domain/PrizeTest.java @@ -1,6 +1,5 @@ package lotto.domain; -import lotto.domain.Prize; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; diff --git a/src/test/java/lotto/domain/PurchaseCountTest.java b/src/test/java/lotto/domain/PurchaseCountTest.java index c3a696f1..dbc49be8 100644 --- a/src/test/java/lotto/domain/PurchaseCountTest.java +++ b/src/test/java/lotto/domain/PurchaseCountTest.java @@ -1,12 +1,10 @@ package lotto.domain; -import lotto.domain.PurchaseCount; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertThrows; public class PurchaseCountTest { @@ -15,11 +13,11 @@ public class PurchaseCountTest { @DisplayName("구매금액 입력값이 1000 미만이면 예외를 던진다") void testInputValueUnder1000() throws Exception { // given - String input = "800"; + int input = 800; // when, then assertThrows(IllegalArgumentException.class, () -> { - new PurchaseCount(input);; + new PurchaseCount(input); }); } @@ -27,73 +25,23 @@ void testInputValueUnder1000() throws Exception { @DisplayName("구매금액 입력값이 1000 이상이면 정상처리된다") void testInputValueNotUnder1000() throws Exception { // given - String input = "2000"; + int input = 2000; - // when, // then - assertDoesNotThrow(() -> new PurchaseCount(input)); + // when + PurchaseCount purchaseCount = new PurchaseCount(input); + + // then + assertThat(purchaseCount.getPurchaseCount()).isEqualTo(input / Lotto.PRICE); } @Test @DisplayName("구매금액 입력값이 1000의 배수가 아니면 예외를 던진다") void testInputValueNotMultiple1000() throws Exception { // given - String input = "1234"; + int input = 1234; // when, then assertThatThrownBy(() -> new PurchaseCount(input)) .isInstanceOf(IllegalArgumentException.class); } - - @Test - @DisplayName("구매금액 입력값이 숫자가 아니면 예외를 던진다") - void testInputNotNumber() throws Exception { - // given - String input = "error"; - - // when - - // then - assertThatThrownBy(() -> new PurchaseCount(input)) - .isInstanceOf(IllegalArgumentException.class); - } - - @Test - @DisplayName("공백이 왼쪽에 있을 때 정상 처리된다") - void testPriceInputWithLeftBlank() throws Exception { - // given - String input = " 1000"; - - // when - PurchaseCount purchaseCount = new PurchaseCount(input); - - // then - assertThat(purchaseCount.getPurchaseCount()).isEqualTo(1); - } - - @Test - @DisplayName("공백이 오른쪽에 있을 때 정상 처리된다") - void testPriceInputWithRightBlank() throws Exception { - // given - String input = "1000 "; - - // when - PurchaseCount purchaseCount = new PurchaseCount(input); - - // then - assertThat(purchaseCount.getPurchaseCount()).isEqualTo(1); - } - - @Test - @DisplayName("공백이 양쪽에 있을 때 정상 처리된다") - void testPriceInputWithLeftAndRightBlank() throws Exception { - // given - String input = " 1000 "; - - // when - PurchaseCount purchaseCount = new PurchaseCount(input); - - // then - assertThat(purchaseCount.getPurchaseCount()).isEqualTo(1); - } - } diff --git a/src/test/java/lotto/domain/TestLottoSet.java b/src/test/java/lotto/domain/TestLottoSet.java deleted file mode 100644 index 02353ae5..00000000 --- a/src/test/java/lotto/domain/TestLottoSet.java +++ /dev/null @@ -1,42 +0,0 @@ -package lotto.domain; - -import lotto.parser.LottoParser; - -import java.util.HashSet; - -public class TestLottoSet extends LottoSet { - - public TestLottoSet() { - super(generateLottoSet()); - } - - public static HashSet generateLottoSet() throws IllegalArgumentException { - HashSet lottos = new HashSet<>(); - lottos.add(new Lotto(LottoParser.parseInputIntoLottoNumbers("1, 2, 3, 4, 5, 6"))); - - lottos.add(new Lotto(LottoParser.parseInputIntoLottoNumbers("1, 2, 3, 4, 5, 7"))); - lottos.add(new Lotto(LottoParser.parseInputIntoLottoNumbers("1, 2, 3, 4, 5, 7"))); - - lottos.add(new Lotto(LottoParser.parseInputIntoLottoNumbers("1, 2, 3, 4, 5, 10"))); - lottos.add(new Lotto(LottoParser.parseInputIntoLottoNumbers("1, 2, 3, 4, 5, 10"))); - lottos.add(new Lotto(LottoParser.parseInputIntoLottoNumbers("1, 2, 3, 4, 5, 10"))); - - lottos.add(new Lotto(LottoParser.parseInputIntoLottoNumbers("1, 2, 3, 4, 10, 11"))); - lottos.add(new Lotto(LottoParser.parseInputIntoLottoNumbers("1, 2, 3, 4, 10, 11"))); - lottos.add(new Lotto(LottoParser.parseInputIntoLottoNumbers("1, 2, 3, 4, 10, 11"))); - lottos.add(new Lotto(LottoParser.parseInputIntoLottoNumbers("1, 2, 3, 4, 10, 11"))); - - lottos.add(new Lotto(LottoParser.parseInputIntoLottoNumbers("1, 2, 3, 10, 11, 12"))); - lottos.add(new Lotto(LottoParser.parseInputIntoLottoNumbers("1, 2, 3, 10, 11, 12"))); - lottos.add(new Lotto(LottoParser.parseInputIntoLottoNumbers("1, 2, 3, 10, 11, 12"))); - lottos.add(new Lotto(LottoParser.parseInputIntoLottoNumbers("1, 2, 3, 10, 11, 12"))); - lottos.add(new Lotto(LottoParser.parseInputIntoLottoNumbers("1, 2, 3, 10, 11, 12"))); - - lottos.add(new Lotto(LottoParser.parseInputIntoLottoNumbers("1, 2, 10, 11, 12, 13"))); - lottos.add(new Lotto(LottoParser.parseInputIntoLottoNumbers("1, 10, 11, 12, 13, 14"))); - lottos.add(new Lotto(LottoParser.parseInputIntoLottoNumbers("19, 10, 11, 12, 13, 14"))); - - return lottos; - } - -} diff --git a/src/test/java/lotto/domain/TestWinningLotto.java b/src/test/java/lotto/domain/TestWinningLotto.java deleted file mode 100644 index 6bdaca47..00000000 --- a/src/test/java/lotto/domain/TestWinningLotto.java +++ /dev/null @@ -1,16 +0,0 @@ -package lotto.domain; - -import lotto.domain.dto.WinningLottoInput; - -public class TestWinningLotto extends WinningLotto { - - private static final String winningNumberInput = "1, 2, 3, 4, 5, 6"; - private static final String bonusNumberInput = "7"; - - public TestWinningLotto() { - super(WinningLottoInput.builder() - .winningLottoNumbers(winningNumberInput) - .winningLottoBonus(bonusNumberInput) - .build()); - } -} diff --git a/src/test/java/lotto/domain/WinningLottoTest.java b/src/test/java/lotto/domain/WinningLottoTest.java index a606afcc..b504adb9 100644 --- a/src/test/java/lotto/domain/WinningLottoTest.java +++ b/src/test/java/lotto/domain/WinningLottoTest.java @@ -1,61 +1,46 @@ package lotto.domain; import lotto.domain.dto.WinningLottoInput; -import lotto.parser.LottoParser; -import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertThrows; public class WinningLottoTest { - private final String winningNumberInput = "1, 2, 3, 4, 5, 6"; - private final String bonusNumberInput = "10"; - private static Lotto targetLotto; - private final WinningLotto winningLotto = new WinningLotto( - new WinningLottoInput(winningNumberInput, bonusNumberInput) - ); - - @BeforeAll - private static void generateTargetLotto() { - targetLotto = new Lotto(LottoParser.parseInputIntoLottoNumbers("1, 2, 3, 4, 5, 6")); - } - @Test @DisplayName("숫자가 6개인지 확인한다") void testWinningNumberSize() throws Exception { - // given, when, then - assertThat(winningLotto.getLottoNumbers().size()).isEqualTo(Lotto.LOTTO_NUMBER_SIZE); - } - - @Test - @DisplayName("숫자가 아닌 값 포함할 때 예외를 던진다") - void testInputWithNonInteger() throws Exception { // given - String input = "1, a, 3, 4, 5, 6"; - String bonus = "10"; - WinningLottoInput winningLottoInput = WinningLottoInput.builder() - .winningLottoNumbers(input) - .winningLottoBonus(bonus) - .build(); + List winningNumberInput = Arrays.stream(new int[]{1, 2, 3, 4, 5, 6}) + .boxed() + .collect(Collectors.toList()); + Integer bonusNumberInput = 10; - // when, then - assertThrows(IllegalArgumentException.class, () -> { - new WinningLotto(winningLottoInput); - }); + // when + WinningLotto winningLotto = new WinningLotto( + new WinningLottoInput(winningNumberInput, bonusNumberInput) + ); + int size = winningLotto.getLottoNumbers().size(); + + // then + assertThat(size).isEqualTo(Lotto.LOTTO_NUMBER_SIZE); } @Test @DisplayName("45 초과인 값 포함할 때 예외를 던진다") void testInputGreaterThanUpperBoundary() throws Exception { // given - String input = "1, 2, 3, 4, 5, 46"; - String bonus = "10"; + List input = Arrays.stream(new int[]{1, 2, 3, 4, 5, 46}).boxed().collect(Collectors.toList()); + Integer bonus = 10; WinningLottoInput winningLottoInput = WinningLottoInput.builder() - .winningLottoNumbers(input) - .winningLottoBonus(bonus) + .numbers(input) + .bonus(bonus) .build(); // when, then @@ -68,11 +53,11 @@ void testInputGreaterThanUpperBoundary() throws Exception { @DisplayName("1 미만인 값 포함할 때 예외를 던진다") void testInputSmallerThanLowerBoundary() throws Exception { // given - String input = "0, 2, 3, 4, 5, 6"; - String bonus = "10"; + List input = Arrays.stream(new int[]{0, 2, 3, 4, 5, 6}).boxed().collect(Collectors.toList()); + Integer bonus = 10; WinningLottoInput winningLottoInput = WinningLottoInput.builder() - .winningLottoNumbers(input) - .winningLottoBonus(bonus) + .numbers(input) + .bonus(bonus) .build(); // when, then @@ -85,11 +70,11 @@ void testInputSmallerThanLowerBoundary() throws Exception { @DisplayName("6개 이하의 숫자가 입력됐을 때 예외를 던진다") void testInputLengthLessThanStandard() throws Exception { // given - String input = "1, 2, 3, 4, 5"; - String bonus = "10"; + List input = Arrays.stream(new int[]{1, 2, 3, 4, 5}).boxed().collect(Collectors.toList()); + Integer bonus = 10; WinningLottoInput winningLottoInput = WinningLottoInput.builder() - .winningLottoNumbers(input) - .winningLottoBonus(bonus) + .numbers(input) + .bonus(bonus) .build(); // when, then @@ -97,90 +82,4 @@ void testInputLengthLessThanStandard() throws Exception { new WinningLotto(winningLottoInput); }); } - - @Test - @DisplayName("보너스 번호 입력값을 확인한다") - void testBonusInput() { - //given, when, then - assertThat(winningLotto.getBonusNumber().getLottoNumber()) - .isEqualTo(Integer.parseInt(bonusNumberInput)); - } - - @Test - @DisplayName("숫자가 6개 일치할 때 1등") - void testFindPrize_firstWithoutBonus() { - //given, when - Prize prize = winningLotto.findPrizeCondition(targetLotto); - - //then - assertThat(prize).isEqualTo(Prize.FIRST); - } - - @Test - @DisplayName("숫자가 5개 일치하고 보너스번호도 일치할 때 2등") - void testFindPrize_secondWithBonus() { - //given - WinningLottoInput winningLottoInput = WinningLottoInput.builder() - .winningLottoNumbers("1,2,3,4,5,7") - .winningLottoBonus("6") - .build(); - - // when - WinningLotto winningLotto = new WinningLotto(winningLottoInput); - Prize prize = winningLotto.findPrizeCondition(targetLotto); - - //then - assertThat(prize).isEqualTo(Prize.SECOND); - } - - @Test - @DisplayName("숫자가 5개 일치하고 보너스 번호가 일치하지 않을 때 3등") - void testFindPrize_third() { - //given - WinningLottoInput winningLottoInput = WinningLottoInput.builder() - .winningLottoNumbers("1,2,3,4,5,7") - .winningLottoBonus(bonusNumberInput) - .build(); - - // when - WinningLotto winningLotto = new WinningLotto(winningLottoInput); - Prize prize = winningLotto.findPrizeCondition(targetLotto); - - //then - assertThat(prize).isEqualTo(Prize.THIRD); - } - - @Test - @DisplayName("숫자가 4개 일치할 때 4등") - void testFindPrize_fourth() { - //given - WinningLottoInput winningLottoInput = WinningLottoInput.builder() - .winningLottoNumbers("4,3,2,1,8,7") - .winningLottoBonus(bonusNumberInput) - .build(); - - // when - WinningLotto winningLotto = new WinningLotto(winningLottoInput); - Prize prize = winningLotto.findPrizeCondition(targetLotto); - - //then - assertThat(prize).isEqualTo(Prize.FOURTH); - } - - @Test - @DisplayName("숫자가 3개 일치할 때 5등") - void testFindPrize_fifth() { - //given - WinningLottoInput winningLottoInput = WinningLottoInput.builder() - .winningLottoNumbers("4,3,2,9,8,7") - .winningLottoBonus(bonusNumberInput) - .build(); - - // when - WinningLotto winningLotto = new WinningLotto(winningLottoInput); - Prize prize = winningLotto.findPrizeCondition(targetLotto); - - //then - assertThat(prize).isEqualTo(Prize.FIFTH); - } } diff --git a/src/test/java/lotto/fixture/TestLottoSet.java b/src/test/java/lotto/fixture/TestLottoSet.java new file mode 100644 index 00000000..27a1cd93 --- /dev/null +++ b/src/test/java/lotto/fixture/TestLottoSet.java @@ -0,0 +1,43 @@ +package lotto.fixture; + +import lotto.domain.Lotto; +import lotto.domain.LottoSet; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.stream.Collectors; + +public class TestLottoSet extends LottoSet { + + public TestLottoSet() { + super(generateLottoSet()); + } + + public static HashSet generateLottoSet() throws IllegalArgumentException { + HashSet lottos = new HashSet<>(); + // 1등 + lottos.add(new Lotto(Arrays.stream(new int[]{1, 2, 3, 4, 5, 6}).boxed().collect(Collectors.toList()))); + + // 2등 + lottos.add(new Lotto(Arrays.stream(new int[]{1, 2, 3, 4, 5, 7}).boxed().collect(Collectors.toList()))); + + // 3등 + lottos.add(new Lotto(Arrays.stream(new int[]{1, 2, 3, 4, 5, 10}).boxed().collect(Collectors.toList()))); + lottos.add(new Lotto(Arrays.stream(new int[]{2, 3, 4, 5, 6, 10}).boxed().collect(Collectors.toList()))); + + // 4등 + lottos.add(new Lotto(Arrays.stream(new int[]{1, 2, 3, 4, 10, 11}).boxed().collect(Collectors.toList()))); + lottos.add(new Lotto(Arrays.stream(new int[]{1, 3, 5, 6, 10, 11}).boxed().collect(Collectors.toList()))); + + // 5등 + lottos.add(new Lotto(Arrays.stream(new int[]{2, 4, 6, 10, 11, 12}).boxed().collect(Collectors.toList()))); + lottos.add(new Lotto(Arrays.stream(new int[]{3, 4, 5, 10, 11, 12}).boxed().collect(Collectors.toList()))); + + // 꽝 + lottos.add(new Lotto(Arrays.stream(new int[]{1, 12, 13, 14, 15, 16}).boxed().collect(Collectors.toList()))); + lottos.add(new Lotto(Arrays.stream(new int[]{11, 12, 13, 14, 15, 16}).boxed().collect(Collectors.toList()))); + + return lottos; + } + +} diff --git a/src/test/java/lotto/fixture/TestWinningLotto.java b/src/test/java/lotto/fixture/TestWinningLotto.java new file mode 100644 index 00000000..3791bcb6 --- /dev/null +++ b/src/test/java/lotto/fixture/TestWinningLotto.java @@ -0,0 +1,23 @@ +package lotto.fixture; + +import lotto.domain.WinningLotto; +import lotto.domain.dto.WinningLottoInput; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +public class TestWinningLotto extends WinningLotto { + + private static final List winningNumberInput = Arrays.stream(new int[]{1, 2, 3, 4, 5, 6}) + .boxed() + .collect(Collectors.toList()); + private static final Integer bonusNumberInput = 7; + + public TestWinningLotto() { + super(WinningLottoInput.builder() + .numbers(winningNumberInput) + .bonus(bonusNumberInput) + .build()); + } +} From fb57be66ce9b47c19d7e27f9822de4d4bf8e2e2f Mon Sep 17 00:00:00 2001 From: Jiwoo Kim Date: Mon, 19 Jul 2021 17:38:12 +0900 Subject: [PATCH 46/55] feat: validate duplication of LottoNumber in one Lotto --- src/main/java/lotto/domain/Lotto.java | 8 ++++++-- src/main/java/lotto/exception/ExceptionMessage.java | 3 ++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/main/java/lotto/domain/Lotto.java b/src/main/java/lotto/domain/Lotto.java index 9fa87be9..8a560ed7 100644 --- a/src/main/java/lotto/domain/Lotto.java +++ b/src/main/java/lotto/domain/Lotto.java @@ -7,7 +7,8 @@ import java.util.Objects; import java.util.stream.Collectors; -import static lotto.exception.ExceptionMessage.INVALID_LENGTH_INPUT_FOR_WINNING_LOTTO; +import static lotto.exception.ExceptionMessage.DUPLICATE_LOTTO_NUMBER_INPUT_FOR_LOTTO; +import static lotto.exception.ExceptionMessage.INVALID_LENGTH_INPUT_FOR_LOTTO; public class Lotto { @@ -28,7 +29,10 @@ public Lotto(List numbers) { private void validate(List numbers) throws IllegalArgumentException { if (numbers.size() != Lotto.LOTTO_NUMBER_SIZE) { - throw new IllegalArgumentException(INVALID_LENGTH_INPUT_FOR_WINNING_LOTTO.getMessage()); + throw new IllegalArgumentException(INVALID_LENGTH_INPUT_FOR_LOTTO.getMessage()); + } + if (numbers.size() != numbers.stream().distinct().count()) { + throw new IllegalArgumentException(DUPLICATE_LOTTO_NUMBER_INPUT_FOR_LOTTO.getMessage()); } } diff --git a/src/main/java/lotto/exception/ExceptionMessage.java b/src/main/java/lotto/exception/ExceptionMessage.java index 56cabad7..43a60399 100644 --- a/src/main/java/lotto/exception/ExceptionMessage.java +++ b/src/main/java/lotto/exception/ExceptionMessage.java @@ -12,7 +12,8 @@ public enum ExceptionMessage { NEGATIVE_INTEGER_INPUT_FOR_PURCHASE_MONEY("구입금액은 숫자로 입력해주세요."), LESS_THAN_LOTTO_PRICE_INPUT_FOR_PURCHASE_MONEY("구입금액은 " + Lotto.PRICE + " 이상의 값을 입력해주세요."), NON_MULTIPLE_OF_LOTTO_PRICE_INPUT_FOR_PURCHASE_MONEY("구입금액은 " + Lotto.PRICE + "의 배수로 입력해주세요."), - INVALID_LENGTH_INPUT_FOR_WINNING_LOTTO("로또 번호는 6개를 입력해주세요"); + INVALID_LENGTH_INPUT_FOR_LOTTO("로또 번호는 6개를 입력해주세요"), + DUPLICATE_LOTTO_NUMBER_INPUT_FOR_LOTTO("로또 번호는 중복되지 않는 6개의 숫자를 입력해주세요"); @Getter private final String message; From a4809aac31d7e649246e1dc08881d2759f555b67 Mon Sep 17 00:00:00 2001 From: Jiwoo Kim Date: Mon, 19 Jul 2021 17:40:49 +0900 Subject: [PATCH 47/55] refactor: rename fields of PrizeCount --- src/main/java/lotto/domain/Prize.java | 10 +++---- src/main/java/lotto/domain/PrizeCount.java | 30 +++++++++---------- .../java/lotto/view/ConsoleOutputView.java | 10 +++---- .../java/lotto/domain/PrizeCountTest.java | 10 +++---- 4 files changed, 30 insertions(+), 30 deletions(-) diff --git a/src/main/java/lotto/domain/Prize.java b/src/main/java/lotto/domain/Prize.java index 2502b91b..90a43226 100644 --- a/src/main/java/lotto/domain/Prize.java +++ b/src/main/java/lotto/domain/Prize.java @@ -46,10 +46,10 @@ private static Prize dissolveSecondOrThird(boolean isBonus) { } public static long sumOfPrizeMoney(PrizeCount prizeCount) { - return prizeCount.getCountFirst() * FIRST.prizeMoney - + prizeCount.getCountSecond() * SECOND.prizeMoney - + prizeCount.getCountThird() * THIRD.prizeMoney - + prizeCount.getCountFourth() * FOURTH.prizeMoney - + prizeCount.getCountFifth() * FIFTH.prizeMoney; + return prizeCount.getFirst() * FIRST.prizeMoney + + prizeCount.getSecond() * SECOND.prizeMoney + + prizeCount.getThird() * THIRD.prizeMoney + + prizeCount.getFourth() * FOURTH.prizeMoney + + prizeCount.getFifth() * FIFTH.prizeMoney; } } diff --git a/src/main/java/lotto/domain/PrizeCount.java b/src/main/java/lotto/domain/PrizeCount.java index cdd0f544..ea6a0a6c 100644 --- a/src/main/java/lotto/domain/PrizeCount.java +++ b/src/main/java/lotto/domain/PrizeCount.java @@ -6,11 +6,11 @@ @Getter public class PrizeCount { - private int countFirst; - private int countSecond; - private int countThird; - private int countFourth; - private int countFifth; + private int first; + private int second; + private int third; + private int fourth; + private int fifth; @Builder public PrizeCount(LottoSet lottoset, WinningLotto winningLotto) { @@ -20,24 +20,24 @@ public PrizeCount(LottoSet lottoset, WinningLotto winningLotto) { } private void updateCounts(Prize prize) { - if (prize.equals(Prize.FIRST)) { - countFirst++; + if (prize == Prize.FIRST) { + first++; return; } - if (prize.equals(Prize.SECOND)) { - countSecond++; + if (prize == Prize.SECOND) { + second++; return; } - if (prize.equals(Prize.THIRD)) { - countThird++; + if (prize == Prize.THIRD) { + third++; return; } - if (prize.equals(Prize.FOURTH)) { - countFourth++; + if (prize == Prize.FOURTH) { + fourth++; return; } - if (prize.equals(Prize.FIFTH)) { - countFifth++; + if (prize == Prize.FIFTH) { + fifth++; } } } diff --git a/src/main/java/lotto/view/ConsoleOutputView.java b/src/main/java/lotto/view/ConsoleOutputView.java index 2453d6bc..0e526f73 100644 --- a/src/main/java/lotto/view/ConsoleOutputView.java +++ b/src/main/java/lotto/view/ConsoleOutputView.java @@ -50,11 +50,11 @@ public void askWinningLottoBonus() { public void printLottoStatistic(StatisticsResult lottoStatistics) { String result = "당첨 통계\n---------\n" + - generatePrizeResultMessage(Prize.FIFTH, lottoStatistics.getPrizeCount().getCountFifth()) + - generatePrizeResultMessage(Prize.FOURTH, lottoStatistics.getPrizeCount().getCountFourth()) + - generatePrizeResultMessage(Prize.THIRD, lottoStatistics.getPrizeCount().getCountThird()) + - generatePrizeResultMessage(Prize.SECOND, lottoStatistics.getPrizeCount().getCountSecond()) + - generatePrizeResultMessage(Prize.FIRST, lottoStatistics.getPrizeCount().getCountFirst()) + + generatePrizeResultMessage(Prize.FIFTH, lottoStatistics.getPrizeCount().getFifth()) + + generatePrizeResultMessage(Prize.FOURTH, lottoStatistics.getPrizeCount().getFourth()) + + generatePrizeResultMessage(Prize.THIRD, lottoStatistics.getPrizeCount().getThird()) + + generatePrizeResultMessage(Prize.SECOND, lottoStatistics.getPrizeCount().getSecond()) + + generatePrizeResultMessage(Prize.FIRST, lottoStatistics.getPrizeCount().getFirst()) + generateProfitRateMessage(lottoStatistics.getProfitRate()); print(result); } diff --git a/src/test/java/lotto/domain/PrizeCountTest.java b/src/test/java/lotto/domain/PrizeCountTest.java index ff1900c0..8ab167cc 100644 --- a/src/test/java/lotto/domain/PrizeCountTest.java +++ b/src/test/java/lotto/domain/PrizeCountTest.java @@ -20,10 +20,10 @@ void testCountPrizesOfLottoSet() { PrizeCount prizeCount = new PrizeCount(lottoSet, winningLotto); //then - assertThat(prizeCount.getCountFirst()).isEqualTo(1); - assertThat(prizeCount.getCountSecond()).isEqualTo(1); - assertThat(prizeCount.getCountThird()).isEqualTo(2); - assertThat(prizeCount.getCountFourth()).isEqualTo(2); - assertThat(prizeCount.getCountFifth()).isEqualTo(2); + assertThat(prizeCount.getFirst()).isEqualTo(1); + assertThat(prizeCount.getSecond()).isEqualTo(1); + assertThat(prizeCount.getThird()).isEqualTo(2); + assertThat(prizeCount.getFourth()).isEqualTo(2); + assertThat(prizeCount.getFifth()).isEqualTo(2); } } From 4357c37a59d3fefb2ba506bdb8fa3e4d217015da Mon Sep 17 00:00:00 2001 From: Jiwoo Kim Date: Mon, 19 Jul 2021 18:08:35 +0900 Subject: [PATCH 48/55] refactor: remove unnecessary throws Exception --- .../java/lotto/controller/LottoController.java | 2 +- src/main/java/lotto/domain/Lotto.java | 2 +- src/main/java/lotto/domain/RandomLotto.java | 2 +- src/main/java/lotto/view/View.java | 2 +- src/test/java/lotto/domain/LottoSetTest.java | 2 +- src/test/java/lotto/domain/PurchaseCountTest.java | 14 +++++++------- src/test/java/lotto/fixture/TestLottoSet.java | 2 +- 7 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/main/java/lotto/controller/LottoController.java b/src/main/java/lotto/controller/LottoController.java index 0ee73abf..48c661ec 100644 --- a/src/main/java/lotto/controller/LottoController.java +++ b/src/main/java/lotto/controller/LottoController.java @@ -17,7 +17,7 @@ public LottoController(View view) { this.lottoService = new LottoService(); } - public void start() throws IllegalArgumentException { + public void start() { try { PurchasePriceInput purchasePriceInput = view.getPurchaseCost(); PurchaseResult purchaseResult = lottoService.purchase(purchasePriceInput); diff --git a/src/main/java/lotto/domain/Lotto.java b/src/main/java/lotto/domain/Lotto.java index 8a560ed7..39ceae12 100644 --- a/src/main/java/lotto/domain/Lotto.java +++ b/src/main/java/lotto/domain/Lotto.java @@ -18,7 +18,7 @@ public class Lotto { @Getter protected List lottoNumbers; - public Lotto(List numbers) { + public Lotto(List numbers) throws IllegalArgumentException { validate(numbers); numbers.sort(Integer::compare); diff --git a/src/main/java/lotto/domain/RandomLotto.java b/src/main/java/lotto/domain/RandomLotto.java index fd0748ec..1a79222c 100644 --- a/src/main/java/lotto/domain/RandomLotto.java +++ b/src/main/java/lotto/domain/RandomLotto.java @@ -18,7 +18,7 @@ public RandomLotto() { super(generateRandomLottoNumbers()); } - private static List generateRandomLottoNumbers() throws IllegalArgumentException { + private static List generateRandomLottoNumbers() { List defaultNumbers = new ArrayList<>(DEFAULT_NUMBERS); Collections.shuffle(defaultNumbers); return new ArrayList<>(defaultNumbers.subList(INDEX_LOWER_BOUND, INDEX_UPPER_BOUND)); diff --git a/src/main/java/lotto/view/View.java b/src/main/java/lotto/view/View.java index 5b6d270a..53cfa4e4 100644 --- a/src/main/java/lotto/view/View.java +++ b/src/main/java/lotto/view/View.java @@ -16,7 +16,7 @@ public class View { private final InputView inputView; private final OutputView outputView; - public PurchasePriceInput getPurchaseCost() { + public PurchasePriceInput getPurchaseCost() throws NumberFormatException { outputView.askPurchaseCost(); Integer purchaseCountInput = inputView.getPurchaseCost(); diff --git a/src/test/java/lotto/domain/LottoSetTest.java b/src/test/java/lotto/domain/LottoSetTest.java index f58b6b73..d8ccc269 100644 --- a/src/test/java/lotto/domain/LottoSetTest.java +++ b/src/test/java/lotto/domain/LottoSetTest.java @@ -9,7 +9,7 @@ public class LottoSetTest { @Test @DisplayName("개수를 입력받으면 개수만큼 랜덤 로또를 생성한다") - void testGenerateRandomLottoSet() throws Exception { + void testGenerateRandomLottoSet() { // given int targetSize = 10; PurchaseCount purchaseCount = new PurchaseCount(targetSize * Lotto.PRICE); diff --git a/src/test/java/lotto/domain/PurchaseCountTest.java b/src/test/java/lotto/domain/PurchaseCountTest.java index dbc49be8..f8a959ab 100644 --- a/src/test/java/lotto/domain/PurchaseCountTest.java +++ b/src/test/java/lotto/domain/PurchaseCountTest.java @@ -3,15 +3,14 @@ import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.assertj.core.api.Assertions.*; import static org.junit.jupiter.api.Assertions.assertThrows; public class PurchaseCountTest { @Test @DisplayName("구매금액 입력값이 1000 미만이면 예외를 던진다") - void testInputValueUnder1000() throws Exception { + void testInputValueUnder1000() { // given int input = 800; @@ -23,7 +22,7 @@ void testInputValueUnder1000() throws Exception { @Test @DisplayName("구매금액 입력값이 1000 이상이면 정상처리된다") - void testInputValueNotUnder1000() throws Exception { + void testInputValueNotUnder1000() { // given int input = 2000; @@ -36,12 +35,13 @@ void testInputValueNotUnder1000() throws Exception { @Test @DisplayName("구매금액 입력값이 1000의 배수가 아니면 예외를 던진다") - void testInputValueNotMultiple1000() throws Exception { + void testInputValueNotMultiple1000() { // given int input = 1234; // when, then - assertThatThrownBy(() -> new PurchaseCount(input)) - .isInstanceOf(IllegalArgumentException.class); + assertThrows(IllegalArgumentException.class, () ->{ + new PurchaseCount(input); + }); } } diff --git a/src/test/java/lotto/fixture/TestLottoSet.java b/src/test/java/lotto/fixture/TestLottoSet.java index 27a1cd93..c8608495 100644 --- a/src/test/java/lotto/fixture/TestLottoSet.java +++ b/src/test/java/lotto/fixture/TestLottoSet.java @@ -13,7 +13,7 @@ public TestLottoSet() { super(generateLottoSet()); } - public static HashSet generateLottoSet() throws IllegalArgumentException { + public static HashSet generateLottoSet() { HashSet lottos = new HashSet<>(); // 1등 lottos.add(new Lotto(Arrays.stream(new int[]{1, 2, 3, 4, 5, 6}).boxed().collect(Collectors.toList()))); From d1e674006653a846146e017aee21505ee7734b8c Mon Sep 17 00:00:00 2001 From: Jiwoo Kim Date: Mon, 19 Jul 2021 18:09:54 +0900 Subject: [PATCH 49/55] test: move tests from WinningLottoTest to LottoTest and add validation test cases --- src/test/java/lotto/domain/LottoTest.java | 109 ++++++++++++++++++ .../java/lotto/domain/WinningLottoTest.java | 85 -------------- 2 files changed, 109 insertions(+), 85 deletions(-) delete mode 100644 src/test/java/lotto/domain/WinningLottoTest.java diff --git a/src/test/java/lotto/domain/LottoTest.java b/src/test/java/lotto/domain/LottoTest.java index b7c26c9c..79b1c7a9 100644 --- a/src/test/java/lotto/domain/LottoTest.java +++ b/src/test/java/lotto/domain/LottoTest.java @@ -1,14 +1,123 @@ package lotto.domain; +import lotto.domain.dto.WinningLottoInput; +import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; +import java.util.stream.Collectors; import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; public class LottoTest { + @Test + @DisplayName("숫자가 6개인지 확인한다") + void testWinningNumberSize() { + // given + List winningNumberInput = Arrays.stream(new int[]{1, 2, 3, 4, 5, 6}) + .boxed() + .collect(Collectors.toList()); + Integer bonusNumberInput = 10; + + // when + WinningLotto winningLotto = new WinningLotto( + new WinningLottoInput(winningNumberInput, bonusNumberInput) + ); + int size = winningLotto.getLottoNumbers().size(); + + // then + assertThat(size).isEqualTo(Lotto.LOTTO_NUMBER_SIZE); + } + + @Test + @DisplayName("45 초과인 값 포함할 때 예외를 던진다") + void testInputGreaterThanUpperBoundary() { + // given + List input = Arrays.stream(new int[]{1, 2, 3, 4, 5, 46}).boxed().collect(Collectors.toList()); + Integer bonus = 10; + WinningLottoInput winningLottoInput = WinningLottoInput.builder() + .numbers(input) + .bonus(bonus) + .build(); + + // when, then + assertThrows(IllegalArgumentException.class, () -> { + new WinningLotto(winningLottoInput); + }); + } + + @Test + @DisplayName("1 미만인 값 포함할 때 예외를 던진다") + void testInputSmallerThanLowerBoundary() { + // given + List input = Arrays.stream(new int[]{0, 2, 3, 4, 5, 6}).boxed().collect(Collectors.toList()); + Integer bonus = 10; + WinningLottoInput winningLottoInput = WinningLottoInput.builder() + .numbers(input) + .bonus(bonus) + .build(); + + // when, then + assertThrows(IllegalArgumentException.class, () -> { + new WinningLotto(winningLottoInput); + }); + } + + @Test + @DisplayName("6개 이하의 숫자가 입력됐을 때 예외를 던진다") + void testInputLengthLessThanStandard() { + // given + List input = Arrays.stream(new int[]{1, 2, 3, 4, 5}).boxed().collect(Collectors.toList()); + Integer bonus = 10; + WinningLottoInput winningLottoInput = WinningLottoInput.builder() + .numbers(input) + .bonus(bonus) + .build(); + + // when, then + assertThrows(IllegalArgumentException.class, () -> { + new WinningLotto(winningLottoInput); + }); + } + + @Test + @DisplayName("6개 이상의 숫자가 입력됐을 때 예외를 던진다") + void testInputLengthGreaterThanStandard() { + // given + List input = Arrays.stream(new int[]{1, 2, 3, 4, 5, 6, 7}).boxed().collect(Collectors.toList()); + Integer bonus = 10; + WinningLottoInput winningLottoInput = WinningLottoInput.builder() + .numbers(input) + .bonus(bonus) + .build(); + + // when, then + assertThrows(IllegalArgumentException.class, () -> { + new WinningLotto(winningLottoInput); + }); + } + + @Test + @DisplayName("중복된 숫자가 입력됐을 때 예외를 던진다") + void testInputWithDuplicatedNumbers() { + // given + List input = Arrays.stream(new int[]{1, 2, 3, 3, 5, 6}).boxed().collect(Collectors.toList()); + Integer bonus = 10; + WinningLottoInput winningLottoInput = WinningLottoInput.builder() + .numbers(input) + .bonus(bonus) + .build(); + + // when, then + assertThrows(IllegalArgumentException.class, () -> { + new WinningLotto(winningLottoInput); + }); + } + @Test void testEqualityOfLottos() { // given diff --git a/src/test/java/lotto/domain/WinningLottoTest.java b/src/test/java/lotto/domain/WinningLottoTest.java deleted file mode 100644 index b504adb9..00000000 --- a/src/test/java/lotto/domain/WinningLottoTest.java +++ /dev/null @@ -1,85 +0,0 @@ -package lotto.domain; - -import lotto.domain.dto.WinningLottoInput; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; - -import java.util.Arrays; -import java.util.List; -import java.util.stream.Collectors; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.jupiter.api.Assertions.assertThrows; - -public class WinningLottoTest { - - @Test - @DisplayName("숫자가 6개인지 확인한다") - void testWinningNumberSize() throws Exception { - // given - List winningNumberInput = Arrays.stream(new int[]{1, 2, 3, 4, 5, 6}) - .boxed() - .collect(Collectors.toList()); - Integer bonusNumberInput = 10; - - // when - WinningLotto winningLotto = new WinningLotto( - new WinningLottoInput(winningNumberInput, bonusNumberInput) - ); - int size = winningLotto.getLottoNumbers().size(); - - // then - assertThat(size).isEqualTo(Lotto.LOTTO_NUMBER_SIZE); - } - - @Test - @DisplayName("45 초과인 값 포함할 때 예외를 던진다") - void testInputGreaterThanUpperBoundary() throws Exception { - // given - List input = Arrays.stream(new int[]{1, 2, 3, 4, 5, 46}).boxed().collect(Collectors.toList()); - Integer bonus = 10; - WinningLottoInput winningLottoInput = WinningLottoInput.builder() - .numbers(input) - .bonus(bonus) - .build(); - - // when, then - assertThrows(IllegalArgumentException.class, () -> { - new WinningLotto(winningLottoInput); - }); - } - - @Test - @DisplayName("1 미만인 값 포함할 때 예외를 던진다") - void testInputSmallerThanLowerBoundary() throws Exception { - // given - List input = Arrays.stream(new int[]{0, 2, 3, 4, 5, 6}).boxed().collect(Collectors.toList()); - Integer bonus = 10; - WinningLottoInput winningLottoInput = WinningLottoInput.builder() - .numbers(input) - .bonus(bonus) - .build(); - - // when, then - assertThrows(IllegalArgumentException.class, () -> { - new WinningLotto(winningLottoInput); - }); - } - - @Test - @DisplayName("6개 이하의 숫자가 입력됐을 때 예외를 던진다") - void testInputLengthLessThanStandard() throws Exception { - // given - List input = Arrays.stream(new int[]{1, 2, 3, 4, 5}).boxed().collect(Collectors.toList()); - Integer bonus = 10; - WinningLottoInput winningLottoInput = WinningLottoInput.builder() - .numbers(input) - .bonus(bonus) - .build(); - - // when, then - assertThrows(IllegalArgumentException.class, () -> { - new WinningLotto(winningLottoInput); - }); - } -} From 0f0371ecb746a1facf032c940496ed419e8f915d Mon Sep 17 00:00:00 2001 From: Jiwoo Kim Date: Mon, 19 Jul 2021 18:46:53 +0900 Subject: [PATCH 50/55] refactor: add LottoMatcher to compare LottoSet and WinningLotto --- .../lotto/controller/LottoController.java | 6 +-- src/main/java/lotto/domain/Lotto.java | 2 +- src/main/java/lotto/domain/LottoMatcher.java | 44 +++++++++++++++++++ src/main/java/lotto/domain/Prize.java | 2 +- src/main/java/lotto/domain/PrizeCount.java | 10 +---- src/main/java/lotto/domain/WinningLotto.java | 28 ------------ ...StatisticsResult.java => LottoResult.java} | 2 +- src/main/java/lotto/service/LottoService.java | 22 ++++------ .../java/lotto/view/ConsoleOutputView.java | 4 +- src/main/java/lotto/view/OutputView.java | 4 +- src/main/java/lotto/view/View.java | 6 +-- ...zeCountTest.java => LottoMatcherTest.java} | 7 +-- .../lotto/domain/LottoStatisticsTest.java | 20 +++++---- src/test/java/lotto/domain/PrizeTest.java | 28 ++++++------ 14 files changed, 96 insertions(+), 89 deletions(-) create mode 100644 src/main/java/lotto/domain/LottoMatcher.java rename src/main/java/lotto/domain/dto/{StatisticsResult.java => LottoResult.java} (89%) rename src/test/java/lotto/domain/{PrizeCountTest.java => LottoMatcherTest.java} (76%) diff --git a/src/main/java/lotto/controller/LottoController.java b/src/main/java/lotto/controller/LottoController.java index 48c661ec..924e3918 100644 --- a/src/main/java/lotto/controller/LottoController.java +++ b/src/main/java/lotto/controller/LottoController.java @@ -2,7 +2,7 @@ import lotto.domain.dto.PurchasePriceInput; import lotto.domain.dto.PurchaseResult; -import lotto.domain.dto.StatisticsResult; +import lotto.domain.dto.LottoResult; import lotto.domain.dto.WinningLottoInput; import lotto.service.LottoService; import lotto.view.View; @@ -24,8 +24,8 @@ public void start() { view.printLottoPurchaseResult(purchaseResult); WinningLottoInput winningLottoInput = view.getWinningLottoAndBonus(); - StatisticsResult statisticsResult = lottoService.calculateResult(purchaseResult, winningLottoInput); - view.printLottoStatistics(statisticsResult); + LottoResult lottoResult = lottoService.calculateResult(purchaseResult, winningLottoInput); + view.printLottoStatistics(lottoResult); } catch (NumberFormatException e) { view.printException(e.getMessage()); } catch (IllegalArgumentException e) { diff --git a/src/main/java/lotto/domain/Lotto.java b/src/main/java/lotto/domain/Lotto.java index 39ceae12..c7132302 100644 --- a/src/main/java/lotto/domain/Lotto.java +++ b/src/main/java/lotto/domain/Lotto.java @@ -16,7 +16,7 @@ public class Lotto { public static final int LOTTO_NUMBER_SIZE = 6; @Getter - protected List lottoNumbers; + private List lottoNumbers; public Lotto(List numbers) throws IllegalArgumentException { validate(numbers); diff --git a/src/main/java/lotto/domain/LottoMatcher.java b/src/main/java/lotto/domain/LottoMatcher.java new file mode 100644 index 00000000..8425f428 --- /dev/null +++ b/src/main/java/lotto/domain/LottoMatcher.java @@ -0,0 +1,44 @@ +package lotto.domain; + +public class LottoMatcher { + + private final WinningLotto winningLotto; + private final LottoSet lottoSet; + + public LottoMatcher(WinningLotto winningLotto, LottoSet lottoSet) { + this.winningLotto = winningLotto; + this.lottoSet = lottoSet; + } + + public PrizeCount countPrizes() { + PrizeCount prizeCount = new PrizeCount(); + for (Lotto lotto : lottoSet.getLottoSet()) { + Prize prize = Prize.getPrize(getMatchNumbersCount(lotto), isBonusMatch(lotto)); + prizeCount.addPrize(prize); + } + return prizeCount; + } + + private int getMatchNumbersCount(Lotto targetLotto) { + int targetIdx = 0, winningIdx = 0; + int matchNumbersCount = 0; + while (targetIdx < targetLotto.getLottoNumbers().size() && winningIdx < winningLotto.getLottoNumbers().size()) { + int targetNumber = targetLotto.getLottoNumbers().get(targetIdx).getLottoNumber(); + int winningNumber = winningLotto.getLottoNumbers().get(winningIdx).getLottoNumber(); + if (targetNumber == winningNumber) { + matchNumbersCount++; + targetIdx++; + winningIdx++; + } else if (targetNumber > winningNumber) { + winningIdx++; + } else { + targetIdx++; + } + } + return matchNumbersCount; + } + + private boolean isBonusMatch(Lotto targetLotto) { + return targetLotto.getLottoNumbers().contains(winningLotto.getBonusNumber()); + } +} \ No newline at end of file diff --git a/src/main/java/lotto/domain/Prize.java b/src/main/java/lotto/domain/Prize.java index 90a43226..5c32f2d7 100644 --- a/src/main/java/lotto/domain/Prize.java +++ b/src/main/java/lotto/domain/Prize.java @@ -22,7 +22,7 @@ public enum Prize { this.prizeMoney = prizeMoney; } - public static Prize findPrize(int matchNumbersCount, boolean isBonus) { + public static Prize getPrize(int matchNumbersCount, boolean isBonus) { if (matchNumbersCount <= LOSE.matchNumbersCount) { return LOSE; } diff --git a/src/main/java/lotto/domain/PrizeCount.java b/src/main/java/lotto/domain/PrizeCount.java index ea6a0a6c..0c4546d9 100644 --- a/src/main/java/lotto/domain/PrizeCount.java +++ b/src/main/java/lotto/domain/PrizeCount.java @@ -1,6 +1,5 @@ package lotto.domain; -import lombok.Builder; import lombok.Getter; @Getter @@ -12,14 +11,7 @@ public class PrizeCount { private int fourth; private int fifth; - @Builder - public PrizeCount(LottoSet lottoset, WinningLotto winningLotto) { - for (Lotto lotto : lottoset.getLottoSet()) { - updateCounts(winningLotto.findPrizeCondition(lotto)); - } - } - - private void updateCounts(Prize prize) { + public void addPrize(Prize prize) { if (prize == Prize.FIRST) { first++; return; diff --git a/src/main/java/lotto/domain/WinningLotto.java b/src/main/java/lotto/domain/WinningLotto.java index 437076fa..6ca10ae5 100644 --- a/src/main/java/lotto/domain/WinningLotto.java +++ b/src/main/java/lotto/domain/WinningLotto.java @@ -14,32 +14,4 @@ public WinningLotto(WinningLottoInput winningLottoInput) throws IllegalArgumentE super(winningLottoInput.getNumbers()); this.bonusNumber = new LottoNumber(winningLottoInput.getBonus()); } - - public Prize findPrizeCondition(Lotto targetLotto) { - return Prize.findPrize(getMatchNumbersCount(targetLotto), isBonusMatch(targetLotto)); - } - - private int getMatchNumbersCount(Lotto targetLotto) { - int targetIdx = 0, winningIdx = 0; - int matchNumbersCount = 0; - while (targetIdx < targetLotto.lottoNumbers.size() && winningIdx < this.lottoNumbers.size()) { - int targetNumber = targetLotto.lottoNumbers.get(targetIdx).getLottoNumber(); - int winningNumber = this.lottoNumbers.get(winningIdx).getLottoNumber(); - if (targetNumber == winningNumber) { - matchNumbersCount++; - targetIdx++; - winningIdx++; - } else if (targetNumber > winningNumber) { - winningIdx++; - } else { - targetIdx++; - } - } - return matchNumbersCount; - } - - private boolean isBonusMatch(Lotto targetLotto) { - return targetLotto.getLottoNumbers().contains(bonusNumber); - } - } diff --git a/src/main/java/lotto/domain/dto/StatisticsResult.java b/src/main/java/lotto/domain/dto/LottoResult.java similarity index 89% rename from src/main/java/lotto/domain/dto/StatisticsResult.java rename to src/main/java/lotto/domain/dto/LottoResult.java index 6c31e973..127e4326 100644 --- a/src/main/java/lotto/domain/dto/StatisticsResult.java +++ b/src/main/java/lotto/domain/dto/LottoResult.java @@ -8,7 +8,7 @@ @Getter @RequiredArgsConstructor @Builder -public class StatisticsResult { +public class LottoResult { private final PrizeCount prizeCount; private final double profitRate; diff --git a/src/main/java/lotto/service/LottoService.java b/src/main/java/lotto/service/LottoService.java index 8f9c2e8e..e6fccea8 100644 --- a/src/main/java/lotto/service/LottoService.java +++ b/src/main/java/lotto/service/LottoService.java @@ -3,7 +3,7 @@ import lotto.domain.*; import lotto.domain.dto.PurchasePriceInput; import lotto.domain.dto.PurchaseResult; -import lotto.domain.dto.StatisticsResult; +import lotto.domain.dto.LottoResult; import lotto.domain.dto.WinningLottoInput; public class LottoService { @@ -18,20 +18,14 @@ public PurchaseResult purchase(PurchasePriceInput purchasePriceInput) { .build(); } - public StatisticsResult calculateResult(PurchaseResult purchaseResult, WinningLottoInput winningLottoInput) { - WinningLotto winningLotto = WinningLotto.builder() - .winningLottoInput(winningLottoInput) - .build(); - PrizeCount prizeCount = PrizeCount.builder() - .lottoset(purchaseResult.getLottoSet()) - .winningLotto(winningLotto) - .build(); - LottoStatistics lottoStatistics = LottoStatistics.builder() - .prizeCount(prizeCount) - .purchaseCount(purchaseResult.getPurchaseCount()) - .build(); + public LottoResult calculateResult(PurchaseResult purchaseResult, WinningLottoInput winningLottoInput) { + LottoMatcher lottoMatcher = new LottoMatcher( + new WinningLotto(winningLottoInput), purchaseResult.getLottoSet() + ); + PrizeCount prizeCount = lottoMatcher.countPrizes(); + LottoStatistics lottoStatistics = new LottoStatistics(prizeCount, purchaseResult.getPurchaseCount()); - return StatisticsResult.builder() + return LottoResult.builder() .prizeCount(prizeCount) .profitRate(lottoStatistics.calculateProfitRate()) .build(); diff --git a/src/main/java/lotto/view/ConsoleOutputView.java b/src/main/java/lotto/view/ConsoleOutputView.java index 0e526f73..2e6f5698 100644 --- a/src/main/java/lotto/view/ConsoleOutputView.java +++ b/src/main/java/lotto/view/ConsoleOutputView.java @@ -1,7 +1,7 @@ package lotto.view; import lotto.domain.*; -import lotto.domain.dto.StatisticsResult; +import lotto.domain.dto.LottoResult; public class ConsoleOutputView implements OutputView { @@ -48,7 +48,7 @@ public void askWinningLottoBonus() { print("보너스 볼을 입력해 주세요."); } - public void printLottoStatistic(StatisticsResult lottoStatistics) { + public void printLottoStatistic(LottoResult lottoStatistics) { String result = "당첨 통계\n---------\n" + generatePrizeResultMessage(Prize.FIFTH, lottoStatistics.getPrizeCount().getFifth()) + generatePrizeResultMessage(Prize.FOURTH, lottoStatistics.getPrizeCount().getFourth()) + diff --git a/src/main/java/lotto/view/OutputView.java b/src/main/java/lotto/view/OutputView.java index 731e0116..0f36ab55 100644 --- a/src/main/java/lotto/view/OutputView.java +++ b/src/main/java/lotto/view/OutputView.java @@ -2,7 +2,7 @@ import lotto.domain.LottoSet; import lotto.domain.PurchaseCount; -import lotto.domain.dto.StatisticsResult; +import lotto.domain.dto.LottoResult; public interface OutputView { void askPurchaseCost(); @@ -15,7 +15,7 @@ public interface OutputView { void askWinningLottoBonus(); - void printLottoStatistic(StatisticsResult statisticsResult); + void printLottoStatistic(LottoResult lottoResult); void printException(String message); } diff --git a/src/main/java/lotto/view/View.java b/src/main/java/lotto/view/View.java index 53cfa4e4..09acabf9 100644 --- a/src/main/java/lotto/view/View.java +++ b/src/main/java/lotto/view/View.java @@ -4,7 +4,7 @@ import lombok.RequiredArgsConstructor; import lotto.domain.dto.PurchasePriceInput; import lotto.domain.dto.PurchaseResult; -import lotto.domain.dto.StatisticsResult; +import lotto.domain.dto.LottoResult; import lotto.domain.dto.WinningLottoInput; import java.util.List; @@ -38,8 +38,8 @@ public WinningLottoInput getWinningLottoAndBonus() throws NumberFormatException .build(); } - public void printLottoStatistics(StatisticsResult statisticsResult) { - outputView.printLottoStatistic(statisticsResult); + public void printLottoStatistics(LottoResult lottoResult) { + outputView.printLottoStatistic(lottoResult); } public void printException(String message) { diff --git a/src/test/java/lotto/domain/PrizeCountTest.java b/src/test/java/lotto/domain/LottoMatcherTest.java similarity index 76% rename from src/test/java/lotto/domain/PrizeCountTest.java rename to src/test/java/lotto/domain/LottoMatcherTest.java index 8ab167cc..14d2ef47 100644 --- a/src/test/java/lotto/domain/PrizeCountTest.java +++ b/src/test/java/lotto/domain/LottoMatcherTest.java @@ -7,17 +7,18 @@ import static org.assertj.core.api.Assertions.assertThat; -public class PrizeCountTest { +public class LottoMatcherTest { @Test - @DisplayName("로또 게임의 당첨 통계를 구한다") + @DisplayName("로또의 당첨 개수를 구한다") void testCountPrizesOfLottoSet() { //given TestLottoSet lottoSet = new TestLottoSet(); TestWinningLotto winningLotto = new TestWinningLotto(); //when - PrizeCount prizeCount = new PrizeCount(lottoSet, winningLotto); + LottoMatcher lottoMatcher = new LottoMatcher(winningLotto, lottoSet); + PrizeCount prizeCount = lottoMatcher.countPrizes(); //then assertThat(prizeCount.getFirst()).isEqualTo(1); diff --git a/src/test/java/lotto/domain/LottoStatisticsTest.java b/src/test/java/lotto/domain/LottoStatisticsTest.java index 99f37b7d..e56c92fa 100644 --- a/src/test/java/lotto/domain/LottoStatisticsTest.java +++ b/src/test/java/lotto/domain/LottoStatisticsTest.java @@ -9,19 +9,23 @@ public class LottoStatisticsTest { - private static final PrizeCount PRIZE_COUNT = new PrizeCount(new TestLottoSet(), new TestWinningLotto()); - private static final long PRIZE_SUM = Prize.sumOfPrizeMoney(PRIZE_COUNT); - private static final int PURCHASE_MONEY = 18000; - private static final double RESULT = (double) PRIZE_SUM / PURCHASE_MONEY; @Test @DisplayName("로또 구매 개수와 당첨 로또 개수를 입력받으면 수익률을 반환한다") void testLottoStatistics() { - //given, when - LottoStatistics lottoStatistics = new LottoStatistics(PRIZE_COUNT, - new PurchaseCount(PURCHASE_MONEY)); + //given + LottoMatcher lottoMatcher = new LottoMatcher(new TestWinningLotto(), new TestLottoSet()); + PrizeCount prizeCount = lottoMatcher.countPrizes(); + long prizeSum = Prize.sumOfPrizeMoney(prizeCount); + int purchaseMoney = 18000; + double result = (double) prizeSum / purchaseMoney; + + // when + LottoStatistics lottoStatistics = new LottoStatistics( + prizeCount, new PurchaseCount(purchaseMoney) + ); // then - assertThat(lottoStatistics.calculateProfitRate()).isEqualTo(RESULT); + assertThat(lottoStatistics.calculateProfitRate()).isEqualTo(result); } } diff --git a/src/test/java/lotto/domain/PrizeTest.java b/src/test/java/lotto/domain/PrizeTest.java index 7d817deb..8c8080d2 100644 --- a/src/test/java/lotto/domain/PrizeTest.java +++ b/src/test/java/lotto/domain/PrizeTest.java @@ -14,9 +14,9 @@ void firstPrize() { int matchNumbersCount = 6; //when, then - assertThat(Prize.findPrize(matchNumbersCount, true)) + assertThat(Prize.getPrize(matchNumbersCount, true)) .isEqualTo(Prize.FIRST); - assertThat(Prize.findPrize(matchNumbersCount, false)) + assertThat(Prize.getPrize(matchNumbersCount, false)) .isEqualTo(Prize.FIRST); } @@ -28,7 +28,7 @@ void secondPrize() { boolean isBonus = true; //when - Prize prize = Prize.findPrize(matchNumbersCount, isBonus); + Prize prize = Prize.getPrize(matchNumbersCount, isBonus); //then assertThat(prize).isEqualTo(Prize.SECOND); @@ -42,7 +42,7 @@ void thirdPrize() { boolean isBonus = false; //when, then - assertThat(Prize.findPrize(matchNumbersCount, isBonus)) + assertThat(Prize.getPrize(matchNumbersCount, isBonus)) .isEqualTo(Prize.THIRD); } @@ -53,9 +53,9 @@ void fourthPrize() { int matchNumbersCount = 4; //when, then - assertThat(Prize.findPrize(matchNumbersCount, false)) + assertThat(Prize.getPrize(matchNumbersCount, false)) .isEqualTo(Prize.FOURTH); - assertThat(Prize.findPrize(matchNumbersCount, true)) + assertThat(Prize.getPrize(matchNumbersCount, true)) .isEqualTo(Prize.FOURTH); } @@ -66,9 +66,9 @@ void fifthPrize() { int matchNumbersCount = 3; //when, then - assertThat(Prize.findPrize(matchNumbersCount, false)) + assertThat(Prize.getPrize(matchNumbersCount, false)) .isEqualTo(Prize.FIFTH); - assertThat(Prize.findPrize(matchNumbersCount, true)) + assertThat(Prize.getPrize(matchNumbersCount, true)) .isEqualTo(Prize.FIFTH); } @@ -79,9 +79,9 @@ void loseMatchesTwo() { int matchNumbersCount = 2; //when, then - assertThat(Prize.findPrize(matchNumbersCount, false)) + assertThat(Prize.getPrize(matchNumbersCount, false)) .isEqualTo(Prize.LOSE); - assertThat(Prize.findPrize(matchNumbersCount, true)) + assertThat(Prize.getPrize(matchNumbersCount, true)) .isEqualTo(Prize.LOSE); } @@ -92,9 +92,9 @@ void loseMatchesOne() { int matchNumbersCount = 1; //when, then - assertThat(Prize.findPrize(matchNumbersCount, false)) + assertThat(Prize.getPrize(matchNumbersCount, false)) .isEqualTo(Prize.LOSE); - assertThat(Prize.findPrize(matchNumbersCount, true)) + assertThat(Prize.getPrize(matchNumbersCount, true)) .isEqualTo(Prize.LOSE); } @@ -105,9 +105,9 @@ void lose() { int matchNumbersCount = 0; //when, then - assertThat(Prize.findPrize(matchNumbersCount, false)) + assertThat(Prize.getPrize(matchNumbersCount, false)) .isEqualTo(Prize.LOSE); - assertThat(Prize.findPrize(matchNumbersCount, true)) + assertThat(Prize.getPrize(matchNumbersCount, true)) .isEqualTo(Prize.LOSE); } } From 42ddc765d55a941050ace67708e77f8910529f3f Mon Sep 17 00:00:00 2001 From: Jiwoo Kim Date: Mon, 19 Jul 2021 18:54:37 +0900 Subject: [PATCH 51/55] refactor: rename findPrize into getMatchPrize --- src/main/java/lotto/domain/LottoMatcher.java | 2 +- src/main/java/lotto/domain/Prize.java | 2 +- src/test/java/lotto/domain/PrizeTest.java | 28 ++++++++++---------- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/main/java/lotto/domain/LottoMatcher.java b/src/main/java/lotto/domain/LottoMatcher.java index 8425f428..8b52cea6 100644 --- a/src/main/java/lotto/domain/LottoMatcher.java +++ b/src/main/java/lotto/domain/LottoMatcher.java @@ -13,7 +13,7 @@ public LottoMatcher(WinningLotto winningLotto, LottoSet lottoSet) { public PrizeCount countPrizes() { PrizeCount prizeCount = new PrizeCount(); for (Lotto lotto : lottoSet.getLottoSet()) { - Prize prize = Prize.getPrize(getMatchNumbersCount(lotto), isBonusMatch(lotto)); + Prize prize = Prize.getMatchPrize(getMatchNumbersCount(lotto), isBonusMatch(lotto)); prizeCount.addPrize(prize); } return prizeCount; diff --git a/src/main/java/lotto/domain/Prize.java b/src/main/java/lotto/domain/Prize.java index 5c32f2d7..0584789f 100644 --- a/src/main/java/lotto/domain/Prize.java +++ b/src/main/java/lotto/domain/Prize.java @@ -22,7 +22,7 @@ public enum Prize { this.prizeMoney = prizeMoney; } - public static Prize getPrize(int matchNumbersCount, boolean isBonus) { + public static Prize getMatchPrize(int matchNumbersCount, boolean isBonus) { if (matchNumbersCount <= LOSE.matchNumbersCount) { return LOSE; } diff --git a/src/test/java/lotto/domain/PrizeTest.java b/src/test/java/lotto/domain/PrizeTest.java index 8c8080d2..eee318ed 100644 --- a/src/test/java/lotto/domain/PrizeTest.java +++ b/src/test/java/lotto/domain/PrizeTest.java @@ -14,9 +14,9 @@ void firstPrize() { int matchNumbersCount = 6; //when, then - assertThat(Prize.getPrize(matchNumbersCount, true)) + assertThat(Prize.getMatchPrize(matchNumbersCount, true)) .isEqualTo(Prize.FIRST); - assertThat(Prize.getPrize(matchNumbersCount, false)) + assertThat(Prize.getMatchPrize(matchNumbersCount, false)) .isEqualTo(Prize.FIRST); } @@ -28,7 +28,7 @@ void secondPrize() { boolean isBonus = true; //when - Prize prize = Prize.getPrize(matchNumbersCount, isBonus); + Prize prize = Prize.getMatchPrize(matchNumbersCount, isBonus); //then assertThat(prize).isEqualTo(Prize.SECOND); @@ -42,7 +42,7 @@ void thirdPrize() { boolean isBonus = false; //when, then - assertThat(Prize.getPrize(matchNumbersCount, isBonus)) + assertThat(Prize.getMatchPrize(matchNumbersCount, isBonus)) .isEqualTo(Prize.THIRD); } @@ -53,9 +53,9 @@ void fourthPrize() { int matchNumbersCount = 4; //when, then - assertThat(Prize.getPrize(matchNumbersCount, false)) + assertThat(Prize.getMatchPrize(matchNumbersCount, false)) .isEqualTo(Prize.FOURTH); - assertThat(Prize.getPrize(matchNumbersCount, true)) + assertThat(Prize.getMatchPrize(matchNumbersCount, true)) .isEqualTo(Prize.FOURTH); } @@ -66,9 +66,9 @@ void fifthPrize() { int matchNumbersCount = 3; //when, then - assertThat(Prize.getPrize(matchNumbersCount, false)) + assertThat(Prize.getMatchPrize(matchNumbersCount, false)) .isEqualTo(Prize.FIFTH); - assertThat(Prize.getPrize(matchNumbersCount, true)) + assertThat(Prize.getMatchPrize(matchNumbersCount, true)) .isEqualTo(Prize.FIFTH); } @@ -79,9 +79,9 @@ void loseMatchesTwo() { int matchNumbersCount = 2; //when, then - assertThat(Prize.getPrize(matchNumbersCount, false)) + assertThat(Prize.getMatchPrize(matchNumbersCount, false)) .isEqualTo(Prize.LOSE); - assertThat(Prize.getPrize(matchNumbersCount, true)) + assertThat(Prize.getMatchPrize(matchNumbersCount, true)) .isEqualTo(Prize.LOSE); } @@ -92,9 +92,9 @@ void loseMatchesOne() { int matchNumbersCount = 1; //when, then - assertThat(Prize.getPrize(matchNumbersCount, false)) + assertThat(Prize.getMatchPrize(matchNumbersCount, false)) .isEqualTo(Prize.LOSE); - assertThat(Prize.getPrize(matchNumbersCount, true)) + assertThat(Prize.getMatchPrize(matchNumbersCount, true)) .isEqualTo(Prize.LOSE); } @@ -105,9 +105,9 @@ void lose() { int matchNumbersCount = 0; //when, then - assertThat(Prize.getPrize(matchNumbersCount, false)) + assertThat(Prize.getMatchPrize(matchNumbersCount, false)) .isEqualTo(Prize.LOSE); - assertThat(Prize.getPrize(matchNumbersCount, true)) + assertThat(Prize.getMatchPrize(matchNumbersCount, true)) .isEqualTo(Prize.LOSE); } } From b65a98a1031dddbcb5201cad3683c15608a38f7e Mon Sep 17 00:00:00 2001 From: Jiwoo Kim Date: Mon, 19 Jul 2021 19:54:47 +0900 Subject: [PATCH 52/55] refactor: remove dependency between view and controller --- src/main/java/lotto/LottoApplication.java | 20 +++++++++----- .../lotto/controller/LottoController.java | 27 ++++++------------- ...hasePriceInput.java => PurchaseInput.java} | 2 +- src/main/java/lotto/service/LottoService.java | 8 +++--- src/main/java/lotto/view/View.java | 6 ++--- 5 files changed, 30 insertions(+), 33 deletions(-) rename src/main/java/lotto/domain/dto/{PurchasePriceInput.java => PurchaseInput.java} (84%) diff --git a/src/main/java/lotto/LottoApplication.java b/src/main/java/lotto/LottoApplication.java index ef112d31..935d8cc4 100644 --- a/src/main/java/lotto/LottoApplication.java +++ b/src/main/java/lotto/LottoApplication.java @@ -1,6 +1,10 @@ package lotto; import lotto.controller.LottoController; +import lotto.domain.dto.LottoResult; +import lotto.domain.dto.PurchaseInput; +import lotto.domain.dto.PurchaseResult; +import lotto.domain.dto.WinningLottoInput; import lotto.view.ConsoleInputView; import lotto.view.ConsoleOutputView; import lotto.view.View; @@ -8,11 +12,15 @@ public class LottoApplication { public static void main(String[] args) { - View view = View.builder() - .inputView(new ConsoleInputView()) - .outputView(new ConsoleOutputView()) - .build(); - LottoController lottoController = new LottoController(view); - lottoController.start(); + View view = new View(new ConsoleInputView(), new ConsoleOutputView()); + LottoController lottoController = new LottoController(); + + PurchaseInput purchaseInput = view.getPurchasePrice(); + PurchaseResult purchaseResult = lottoController.purchase(purchaseInput); + view.printLottoPurchaseResult(purchaseResult); + + WinningLottoInput winningLottoInput = view.getWinningLottoAndBonus(); + LottoResult lottoResult = lottoController.calculateResult(purchaseResult, winningLottoInput); + view.printLottoStatistics(lottoResult); } } diff --git a/src/main/java/lotto/controller/LottoController.java b/src/main/java/lotto/controller/LottoController.java index 924e3918..0b15fc7b 100644 --- a/src/main/java/lotto/controller/LottoController.java +++ b/src/main/java/lotto/controller/LottoController.java @@ -1,35 +1,24 @@ package lotto.controller; -import lotto.domain.dto.PurchasePriceInput; -import lotto.domain.dto.PurchaseResult; import lotto.domain.dto.LottoResult; +import lotto.domain.dto.PurchaseInput; +import lotto.domain.dto.PurchaseResult; import lotto.domain.dto.WinningLottoInput; import lotto.service.LottoService; -import lotto.view.View; public class LottoController { - private final View view; private final LottoService lottoService; - public LottoController(View view) { - this.view = view; + public LottoController() { this.lottoService = new LottoService(); } - public void start() { - try { - PurchasePriceInput purchasePriceInput = view.getPurchaseCost(); - PurchaseResult purchaseResult = lottoService.purchase(purchasePriceInput); - view.printLottoPurchaseResult(purchaseResult); + public PurchaseResult purchase(PurchaseInput purchaseInput) { + return lottoService.purchase(purchaseInput); + } - WinningLottoInput winningLottoInput = view.getWinningLottoAndBonus(); - LottoResult lottoResult = lottoService.calculateResult(purchaseResult, winningLottoInput); - view.printLottoStatistics(lottoResult); - } catch (NumberFormatException e) { - view.printException(e.getMessage()); - } catch (IllegalArgumentException e) { - view.printException(e.getMessage()); - } + public LottoResult calculateResult(PurchaseResult purchaseResult, WinningLottoInput winningLottoInput) { + return lottoService.calculateResult(purchaseResult, winningLottoInput); } } diff --git a/src/main/java/lotto/domain/dto/PurchasePriceInput.java b/src/main/java/lotto/domain/dto/PurchaseInput.java similarity index 84% rename from src/main/java/lotto/domain/dto/PurchasePriceInput.java rename to src/main/java/lotto/domain/dto/PurchaseInput.java index 87d2e460..3e70ff23 100644 --- a/src/main/java/lotto/domain/dto/PurchasePriceInput.java +++ b/src/main/java/lotto/domain/dto/PurchaseInput.java @@ -7,7 +7,7 @@ @Getter @Builder @RequiredArgsConstructor -public class PurchasePriceInput { +public class PurchaseInput { private final Integer price; } diff --git a/src/main/java/lotto/service/LottoService.java b/src/main/java/lotto/service/LottoService.java index e6fccea8..c8da7215 100644 --- a/src/main/java/lotto/service/LottoService.java +++ b/src/main/java/lotto/service/LottoService.java @@ -1,15 +1,15 @@ package lotto.service; import lotto.domain.*; -import lotto.domain.dto.PurchasePriceInput; -import lotto.domain.dto.PurchaseResult; import lotto.domain.dto.LottoResult; +import lotto.domain.dto.PurchaseInput; +import lotto.domain.dto.PurchaseResult; import lotto.domain.dto.WinningLottoInput; public class LottoService { - public PurchaseResult purchase(PurchasePriceInput purchasePriceInput) { - PurchaseCount purchaseCount = new PurchaseCount(purchasePriceInput.getPrice()); + public PurchaseResult purchase(PurchaseInput purchaseInput) { + PurchaseCount purchaseCount = new PurchaseCount(purchaseInput.getPrice()); LottoSet lottoSet = new LottoSet(purchaseCount); return PurchaseResult.builder() diff --git a/src/main/java/lotto/view/View.java b/src/main/java/lotto/view/View.java index 09acabf9..dcb18988 100644 --- a/src/main/java/lotto/view/View.java +++ b/src/main/java/lotto/view/View.java @@ -2,7 +2,7 @@ import lombok.Builder; import lombok.RequiredArgsConstructor; -import lotto.domain.dto.PurchasePriceInput; +import lotto.domain.dto.PurchaseInput; import lotto.domain.dto.PurchaseResult; import lotto.domain.dto.LottoResult; import lotto.domain.dto.WinningLottoInput; @@ -16,11 +16,11 @@ public class View { private final InputView inputView; private final OutputView outputView; - public PurchasePriceInput getPurchaseCost() throws NumberFormatException { + public PurchaseInput getPurchasePrice() throws NumberFormatException { outputView.askPurchaseCost(); Integer purchaseCountInput = inputView.getPurchaseCost(); - return PurchasePriceInput.builder() + return PurchaseInput.builder() .price(purchaseCountInput) .build(); } From 0f2c076f63c7cad7f7cd69acb56f565bb25a5fc0 Mon Sep 17 00:00:00 2001 From: Jiwoo Kim Date: Mon, 19 Jul 2021 19:56:12 +0900 Subject: [PATCH 53/55] refactor: make Lotto unmodifiable --- src/main/java/lotto/domain/Lotto.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/lotto/domain/Lotto.java b/src/main/java/lotto/domain/Lotto.java index c7132302..04f0a8a3 100644 --- a/src/main/java/lotto/domain/Lotto.java +++ b/src/main/java/lotto/domain/Lotto.java @@ -16,7 +16,7 @@ public class Lotto { public static final int LOTTO_NUMBER_SIZE = 6; @Getter - private List lottoNumbers; + private final List lottoNumbers; public Lotto(List numbers) throws IllegalArgumentException { validate(numbers); From eda9666d09b807ca05f697a34f1c6b1c1d4eece3 Mon Sep 17 00:00:00 2001 From: Jiwoo Kim Date: Mon, 19 Jul 2021 20:08:49 +0900 Subject: [PATCH 54/55] refactor: exception handling and print exception message --- src/main/java/lotto/LottoApplication.java | 19 +++++++++++++------ .../lotto/controller/LottoController.java | 4 ++-- .../lotto/exception/ExceptionMessage.java | 7 +++---- src/main/java/lotto/service/LottoService.java | 4 ++-- 4 files changed, 20 insertions(+), 14 deletions(-) diff --git a/src/main/java/lotto/LottoApplication.java b/src/main/java/lotto/LottoApplication.java index 935d8cc4..23498223 100644 --- a/src/main/java/lotto/LottoApplication.java +++ b/src/main/java/lotto/LottoApplication.java @@ -5,6 +5,7 @@ import lotto.domain.dto.PurchaseInput; import lotto.domain.dto.PurchaseResult; import lotto.domain.dto.WinningLottoInput; +import lotto.exception.ExceptionMessage; import lotto.view.ConsoleInputView; import lotto.view.ConsoleOutputView; import lotto.view.View; @@ -15,12 +16,18 @@ public static void main(String[] args) { View view = new View(new ConsoleInputView(), new ConsoleOutputView()); LottoController lottoController = new LottoController(); - PurchaseInput purchaseInput = view.getPurchasePrice(); - PurchaseResult purchaseResult = lottoController.purchase(purchaseInput); - view.printLottoPurchaseResult(purchaseResult); + try { + PurchaseInput purchaseInput = view.getPurchasePrice(); + PurchaseResult purchaseResult = lottoController.purchase(purchaseInput); + view.printLottoPurchaseResult(purchaseResult); - WinningLottoInput winningLottoInput = view.getWinningLottoAndBonus(); - LottoResult lottoResult = lottoController.calculateResult(purchaseResult, winningLottoInput); - view.printLottoStatistics(lottoResult); + WinningLottoInput winningLottoInput = view.getWinningLottoAndBonus(); + LottoResult lottoResult = lottoController.calculateResult(purchaseResult, winningLottoInput); + view.printLottoStatistics(lottoResult); + } catch (NumberFormatException e) { + view.printException(ExceptionMessage.NON_NUMBER_INPUT.getMessage()); + } catch (IllegalArgumentException e) { + view.printException(e.getMessage()); + } } } diff --git a/src/main/java/lotto/controller/LottoController.java b/src/main/java/lotto/controller/LottoController.java index 0b15fc7b..608ca7eb 100644 --- a/src/main/java/lotto/controller/LottoController.java +++ b/src/main/java/lotto/controller/LottoController.java @@ -14,11 +14,11 @@ public LottoController() { this.lottoService = new LottoService(); } - public PurchaseResult purchase(PurchaseInput purchaseInput) { + public PurchaseResult purchase(PurchaseInput purchaseInput) throws IllegalArgumentException { return lottoService.purchase(purchaseInput); } - public LottoResult calculateResult(PurchaseResult purchaseResult, WinningLottoInput winningLottoInput) { + public LottoResult calculateResult(PurchaseResult purchaseResult, WinningLottoInput winningLottoInput) throws IllegalArgumentException { return lottoService.calculateResult(purchaseResult, winningLottoInput); } } diff --git a/src/main/java/lotto/exception/ExceptionMessage.java b/src/main/java/lotto/exception/ExceptionMessage.java index 43a60399..1218cb0b 100644 --- a/src/main/java/lotto/exception/ExceptionMessage.java +++ b/src/main/java/lotto/exception/ExceptionMessage.java @@ -7,13 +7,12 @@ @RequiredArgsConstructor public enum ExceptionMessage { - NON_INTEGER_INPUT_FOR_LOTTO_NUMBER("로또 번호는 숫자를 입력해주세요."), - OUT_OF_BOUND_INPUT_FOR_LOTTO_NUMBER("로또 번호는 1 이상 45 이하의 값을 입력해주세요."), - NEGATIVE_INTEGER_INPUT_FOR_PURCHASE_MONEY("구입금액은 숫자로 입력해주세요."), + NON_NUMBER_INPUT("숫자를 입력해주세요."), LESS_THAN_LOTTO_PRICE_INPUT_FOR_PURCHASE_MONEY("구입금액은 " + Lotto.PRICE + " 이상의 값을 입력해주세요."), NON_MULTIPLE_OF_LOTTO_PRICE_INPUT_FOR_PURCHASE_MONEY("구입금액은 " + Lotto.PRICE + "의 배수로 입력해주세요."), INVALID_LENGTH_INPUT_FOR_LOTTO("로또 번호는 6개를 입력해주세요"), - DUPLICATE_LOTTO_NUMBER_INPUT_FOR_LOTTO("로또 번호는 중복되지 않는 6개의 숫자를 입력해주세요"); + DUPLICATE_LOTTO_NUMBER_INPUT_FOR_LOTTO("로또 번호는 중복되지 않는 6개의 숫자를 입력해주세요"), + OUT_OF_BOUND_INPUT_FOR_LOTTO_NUMBER("로또 번호는 1 이상 45 이하의 값을 입력해주세요."); @Getter private final String message; diff --git a/src/main/java/lotto/service/LottoService.java b/src/main/java/lotto/service/LottoService.java index c8da7215..fedd9024 100644 --- a/src/main/java/lotto/service/LottoService.java +++ b/src/main/java/lotto/service/LottoService.java @@ -8,7 +8,7 @@ public class LottoService { - public PurchaseResult purchase(PurchaseInput purchaseInput) { + public PurchaseResult purchase(PurchaseInput purchaseInput) throws IllegalArgumentException { PurchaseCount purchaseCount = new PurchaseCount(purchaseInput.getPrice()); LottoSet lottoSet = new LottoSet(purchaseCount); @@ -18,7 +18,7 @@ public PurchaseResult purchase(PurchaseInput purchaseInput) { .build(); } - public LottoResult calculateResult(PurchaseResult purchaseResult, WinningLottoInput winningLottoInput) { + public LottoResult calculateResult(PurchaseResult purchaseResult, WinningLottoInput winningLottoInput) throws IllegalArgumentException { LottoMatcher lottoMatcher = new LottoMatcher( new WinningLotto(winningLottoInput), purchaseResult.getLottoSet() ); From ca563547623f840fcd19e953b49ecd2f113179c1 Mon Sep 17 00:00:00 2001 From: Jiwoo Kim Date: Mon, 19 Jul 2021 20:26:43 +0900 Subject: [PATCH 55/55] refactor: count match numbers with stream api --- src/main/java/lotto/domain/LottoMatcher.java | 19 +++---------------- src/main/java/lotto/domain/LottoNumber.java | 4 ++++ 2 files changed, 7 insertions(+), 16 deletions(-) diff --git a/src/main/java/lotto/domain/LottoMatcher.java b/src/main/java/lotto/domain/LottoMatcher.java index 8b52cea6..600fbf25 100644 --- a/src/main/java/lotto/domain/LottoMatcher.java +++ b/src/main/java/lotto/domain/LottoMatcher.java @@ -20,22 +20,9 @@ public PrizeCount countPrizes() { } private int getMatchNumbersCount(Lotto targetLotto) { - int targetIdx = 0, winningIdx = 0; - int matchNumbersCount = 0; - while (targetIdx < targetLotto.getLottoNumbers().size() && winningIdx < winningLotto.getLottoNumbers().size()) { - int targetNumber = targetLotto.getLottoNumbers().get(targetIdx).getLottoNumber(); - int winningNumber = winningLotto.getLottoNumbers().get(winningIdx).getLottoNumber(); - if (targetNumber == winningNumber) { - matchNumbersCount++; - targetIdx++; - winningIdx++; - } else if (targetNumber > winningNumber) { - winningIdx++; - } else { - targetIdx++; - } - } - return matchNumbersCount; + return (int) targetLotto.getLottoNumbers().stream() + .filter(lottoNumber -> winningLotto.getLottoNumbers().contains(lottoNumber)) + .count(); } private boolean isBonusMatch(Lotto targetLotto) { diff --git a/src/main/java/lotto/domain/LottoNumber.java b/src/main/java/lotto/domain/LottoNumber.java index 16439c31..c4512fd6 100644 --- a/src/main/java/lotto/domain/LottoNumber.java +++ b/src/main/java/lotto/domain/LottoNumber.java @@ -30,6 +30,10 @@ private boolean isOutOfBound(int lottoNumber) { return lottoNumber < LOWER_BOUND || lottoNumber > UPPER_BOUND; } + public boolean isGreaterThan(LottoNumber winningNumber) { + return this.getLottoNumber() > winningNumber.getLottoNumber(); + } + @Override public boolean equals(Object o) { if (this == o) {