From 83f6878e0ebfcb814055d6a598d189b4e99840f0 Mon Sep 17 00:00:00 2001 From: KimSeongGyu1 Date: Tue, 18 Feb 2020 15:26:09 +0900 Subject: [PATCH 01/23] =?UTF-8?q?[docs]=20README=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index edd124ad18..ee65b24037 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,28 @@ +# README + # java-lotto -로또 미션 진행을 위한 저장소 + +> 기능 요구사항 + +- [ ] 구입금액을 입력받는다. + - [ ] (예외) 1000원 이하 단위일 경우 + - [ ] (예외) 숫자가 아닐 경우 + - [ ] (예외) 50000원 이상 구매할 경우 +- [ ] 구매한 로또 장 수와 번호를 출력한다. +- [ ] 지난 주 당첨 번호를 입력받는다. + - [ ] (예외) 1~45 이외의 숫자가 입력되는 경우 + - [ ] (예외) 올바른 입력 형식이 아닌 경우 + - [ ] (예외) 6개가 아닌 경우 + - [ ] (예외) 공백이 입력되는 경우 +- [ ] 보너스 볼을 입력받는다. + - [ ] (예외) 위와 동일 +- [ ] 당첨 번호를 대조해서 결과를 얻는다. +- [ ] 당첨 통계와 수익률을 출력한다. + +> 프로그래밍 요구사항 + +- indent(인덴트, 들여쓰기) depth를 2단계에서 1단계로 줄여라. +- else를 사용하지 마라. +- 메소드의 크기가 최대 10라인을 넘지 않도록 구현한다. +- 배열 대신 ArrayList를 사용한다. +- enum을 적용해 프로그래밍을 구현한다. \ No newline at end of file From 263b9af92684900564f347790f58fa92862f927a Mon Sep 17 00:00:00 2001 From: KimSeongGyu1 Date: Tue, 18 Feb 2020 16:11:18 +0900 Subject: [PATCH 02/23] =?UTF-8?q?[feature]=20=EB=A1=9C=EB=98=90=20?= =?UTF-8?q?=EC=88=AB=EC=9E=90=20=EA=B0=9D=EC=B2=B4=20=EB=B9=84=EA=B5=90=20?= =?UTF-8?q?=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/domain/LottoNumber.java | 19 ++++++++++++++++ .../java/lotto/WebUILottoApplication.java | 22 ------------------- src/main/resources/templates/index.html | 10 --------- src/test/java/domain/LottoNumberTest.java | 17 ++++++++++++++ src/test/java/empty.txt | 0 5 files changed, 36 insertions(+), 32 deletions(-) create mode 100644 src/main/java/domain/LottoNumber.java delete mode 100644 src/main/java/lotto/WebUILottoApplication.java delete mode 100644 src/main/resources/templates/index.html create mode 100644 src/test/java/domain/LottoNumberTest.java delete mode 100644 src/test/java/empty.txt diff --git a/src/main/java/domain/LottoNumber.java b/src/main/java/domain/LottoNumber.java new file mode 100644 index 0000000000..8f6ed8bcb9 --- /dev/null +++ b/src/main/java/domain/LottoNumber.java @@ -0,0 +1,19 @@ +package domain; + +import java.util.Objects; + +public class LottoNumber { + private final int number; + + public LottoNumber(int number) { + this.number = number; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + LottoNumber that = (LottoNumber) o; + return number == that.number; + } +} diff --git a/src/main/java/lotto/WebUILottoApplication.java b/src/main/java/lotto/WebUILottoApplication.java deleted file mode 100644 index 1926550119..0000000000 --- a/src/main/java/lotto/WebUILottoApplication.java +++ /dev/null @@ -1,22 +0,0 @@ -package lotto; - -import spark.ModelAndView; -import spark.template.handlebars.HandlebarsTemplateEngine; - -import java.util.HashMap; -import java.util.Map; - -import static spark.Spark.get; - -public class WebUILottoApplication { - public static void main(String[] args) { - get("/", (req, res) -> { - Map model = new HashMap<>(); - return render(model, "index.html"); - }); - } - - private static String render(Map model, String templatePath) { - return new HandlebarsTemplateEngine().render(new ModelAndView(model, templatePath)); - } -} diff --git a/src/main/resources/templates/index.html b/src/main/resources/templates/index.html deleted file mode 100644 index 3a7dbfecdf..0000000000 --- a/src/main/resources/templates/index.html +++ /dev/null @@ -1,10 +0,0 @@ - - - - - 로또 - - -Hello World!! - - \ No newline at end of file diff --git a/src/test/java/domain/LottoNumberTest.java b/src/test/java/domain/LottoNumberTest.java new file mode 100644 index 0000000000..eefb6a79fa --- /dev/null +++ b/src/test/java/domain/LottoNumberTest.java @@ -0,0 +1,17 @@ +package domain; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +@DisplayName("로또 숫자 테스트") +public class LottoNumberTest { + @Test + @DisplayName("로또 숫자 비교 테스트") + void lottoNumberEqualTest() { + LottoNumber number = new LottoNumber(5); + assertThat(number.equals(new LottoNumber(5))).isTrue(); + assertThat(number.equals(new LottoNumber(4))).isFalse(); + } +} diff --git a/src/test/java/empty.txt b/src/test/java/empty.txt deleted file mode 100644 index e69de29bb2..0000000000 From 2395c337bcef386719b3a72062bcb3c7f51b4560 Mon Sep 17 00:00:00 2001 From: KimSeongGyu1 Date: Tue, 18 Feb 2020 16:20:23 +0900 Subject: [PATCH 03/23] =?UTF-8?q?[feature]=20=EB=A1=9C=EB=98=90=20?= =?UTF-8?q?=EC=88=AB=EC=9E=90=20=EC=83=9D=EC=84=B1=20=EC=98=88=EC=99=B8?= =?UTF-8?q?=EC=B2=98=EB=A6=AC=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/domain/LottoNumber.java | 9 +++++++++ src/test/java/domain/LottoNumberTest.java | 9 +++++++++ 2 files changed, 18 insertions(+) diff --git a/src/main/java/domain/LottoNumber.java b/src/main/java/domain/LottoNumber.java index 8f6ed8bcb9..5769f08b0d 100644 --- a/src/main/java/domain/LottoNumber.java +++ b/src/main/java/domain/LottoNumber.java @@ -3,12 +3,21 @@ import java.util.Objects; public class LottoNumber { + private static final int MIN_LOTTO_NUMBER = 1; + private static final int MAX_LOTTO_NUMBER = 45; private final int number; public LottoNumber(int number) { + validateLottoNumber(number); this.number = number; } + private void validateLottoNumber(int number) { + if (number < MIN_LOTTO_NUMBER || number > MAX_LOTTO_NUMBER) { + throw new IllegalArgumentException("1~45 사이의 숫자만 가능합니다."); + } + } + @Override public boolean equals(Object o) { if (this == o) return true; diff --git a/src/test/java/domain/LottoNumberTest.java b/src/test/java/domain/LottoNumberTest.java index eefb6a79fa..3fd222cf7c 100644 --- a/src/test/java/domain/LottoNumberTest.java +++ b/src/test/java/domain/LottoNumberTest.java @@ -4,9 +4,18 @@ import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; @DisplayName("로또 숫자 테스트") public class LottoNumberTest { + @Test + @DisplayName("로또 숫자 생성 예외처리 테스트") + void lottoNumberTest() { + assertThatThrownBy(() -> new LottoNumber(46)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("1~45 사이의 숫자만 가능합니다."); + } + @Test @DisplayName("로또 숫자 비교 테스트") void lottoNumberEqualTest() { From 97e02d3ecbfa225706a200f9164876dd3df3018f Mon Sep 17 00:00:00 2001 From: KimSeongGyu1 Date: Tue, 18 Feb 2020 17:16:58 +0900 Subject: [PATCH 04/23] =?UTF-8?q?[feature]=20=EB=8F=88=20=EA=B0=9D?= =?UTF-8?q?=EC=B2=B4=20=EC=83=9D=EC=84=B1=EC=9E=90=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 예외조건 처리 구현 --- README.md | 2 +- src/main/java/domain/Money.java | 27 +++++++++++++++++++++++ src/test/java/domain/LottoNumberTest.java | 6 ++--- src/test/java/domain/MoneyTest.java | 20 +++++++++++++++++ 4 files changed, 51 insertions(+), 4 deletions(-) create mode 100644 src/main/java/domain/Money.java create mode 100644 src/test/java/domain/MoneyTest.java diff --git a/README.md b/README.md index ee65b24037..28a0398fe1 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ - [ ] 구입금액을 입력받는다. - [ ] (예외) 1000원 이하 단위일 경우 - [ ] (예외) 숫자가 아닐 경우 - - [ ] (예외) 50000원 이상 구매할 경우 + - [ ] (예외) 음수 또는 50000원 이상 입력될 경우 - [ ] 구매한 로또 장 수와 번호를 출력한다. - [ ] 지난 주 당첨 번호를 입력받는다. - [ ] (예외) 1~45 이외의 숫자가 입력되는 경우 diff --git a/src/main/java/domain/Money.java b/src/main/java/domain/Money.java new file mode 100644 index 0000000000..c365e6e763 --- /dev/null +++ b/src/main/java/domain/Money.java @@ -0,0 +1,27 @@ +package domain; + +public class Money { + private final int money; + + public Money(int money) { + validateMoney(money); + this.money = money; + } + + private void validateMoney(int money) { + validateMoneyRange(money); + validateMoneyUnit(money); + } + + private void validateMoneyRange(int money) { + if (money < 0 || money > 50000) { + throw new IllegalArgumentException("0원 이상, 5만원 이하 금액만 구매 가능합니다."); + } + } + + private void validateMoneyUnit(int money) { + if (money % 1000 != 0) { + throw new IllegalArgumentException("천 원 단위로만 구매 가능합니다."); + } + } +} diff --git a/src/test/java/domain/LottoNumberTest.java b/src/test/java/domain/LottoNumberTest.java index 3fd222cf7c..4b1a87fbe3 100644 --- a/src/test/java/domain/LottoNumberTest.java +++ b/src/test/java/domain/LottoNumberTest.java @@ -9,15 +9,15 @@ @DisplayName("로또 숫자 테스트") public class LottoNumberTest { @Test - @DisplayName("로또 숫자 생성 예외처리 테스트") - void lottoNumberTest() { + @DisplayName("로또 숫자 생성 예외처리") + void lottoNumberConstructorTest() { assertThatThrownBy(() -> new LottoNumber(46)) .isInstanceOf(IllegalArgumentException.class) .hasMessage("1~45 사이의 숫자만 가능합니다."); } @Test - @DisplayName("로또 숫자 비교 테스트") + @DisplayName("로또 숫자 비교") void lottoNumberEqualTest() { LottoNumber number = new LottoNumber(5); assertThat(number.equals(new LottoNumber(5))).isTrue(); diff --git a/src/test/java/domain/MoneyTest.java b/src/test/java/domain/MoneyTest.java new file mode 100644 index 0000000000..922f7e3e43 --- /dev/null +++ b/src/test/java/domain/MoneyTest.java @@ -0,0 +1,20 @@ +package domain; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +@DisplayName("돈 객체 테스트") +class MoneyTest { + @ParameterizedTest + @ValueSource(ints = {1700, -1, 50001}) + @DisplayName("돈 객체 생성자") + void moneyConstructorTest(int money) { + assertThatThrownBy(() -> new Money(money)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageEndingWith("만 구매 가능합니다."); + } +} \ No newline at end of file From f26dcb64da9cbef8991e173246341c290bce0db9 Mon Sep 17 00:00:00 2001 From: KimSeongGyu1 Date: Tue, 18 Feb 2020 17:48:58 +0900 Subject: [PATCH 05/23] =?UTF-8?q?[feature]=20InputView=20=EA=B5=AC?= =?UTF-8?q?=EC=9E=85=EA=B8=88=EC=95=A1=20=EC=9E=85=EB=A0=A5=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 6 +++--- src/main/java/LottoApplication.java | 19 +++++++++++++++++++ src/main/java/domain/LottoNumber.java | 1 + src/main/java/view/InputView.java | 23 +++++++++++++++++++++++ 4 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 src/main/java/LottoApplication.java create mode 100644 src/main/java/view/InputView.java diff --git a/README.md b/README.md index 28a0398fe1..600fad0e27 100644 --- a/README.md +++ b/README.md @@ -5,9 +5,9 @@ > 기능 요구사항 - [ ] 구입금액을 입력받는다. - - [ ] (예외) 1000원 이하 단위일 경우 - - [ ] (예외) 숫자가 아닐 경우 - - [ ] (예외) 음수 또는 50000원 이상 입력될 경우 + - [x] (예외) 1000원 이하 단위일 경우 + - [x] (예외) 숫자가 아닐 경우 + - [x] (예외) 음수 또는 50000원 이상 입력될 경우 - [ ] 구매한 로또 장 수와 번호를 출력한다. - [ ] 지난 주 당첨 번호를 입력받는다. - [ ] (예외) 1~45 이외의 숫자가 입력되는 경우 diff --git a/src/main/java/LottoApplication.java b/src/main/java/LottoApplication.java new file mode 100644 index 0000000000..24d9faaefd --- /dev/null +++ b/src/main/java/LottoApplication.java @@ -0,0 +1,19 @@ +import domain.Money; +import view.InputView; + +public class LottoApplication { + + public static void main(String[] args) { + Money money = enterMoney(); + } + + private static Money enterMoney() { + while (true) { + try { + return InputView.enterMoney(); + } catch (Exception e) { + System.out.println(e.getMessage()); + } + } + } +} \ No newline at end of file diff --git a/src/main/java/domain/LottoNumber.java b/src/main/java/domain/LottoNumber.java index 5769f08b0d..49a32984ea 100644 --- a/src/main/java/domain/LottoNumber.java +++ b/src/main/java/domain/LottoNumber.java @@ -5,6 +5,7 @@ public class LottoNumber { private static final int MIN_LOTTO_NUMBER = 1; private static final int MAX_LOTTO_NUMBER = 45; + private final int number; public LottoNumber(int number) { diff --git a/src/main/java/view/InputView.java b/src/main/java/view/InputView.java new file mode 100644 index 0000000000..af53b00ad6 --- /dev/null +++ b/src/main/java/view/InputView.java @@ -0,0 +1,23 @@ +package view; + +import domain.Money; + +import java.util.Scanner; + +public class InputView { + private static Scanner scanner = new Scanner(System.in); + + public static Money enterMoney() { + System.out.println("구입 금액을 입력해주세요."); + int money = parseMoney(scanner.nextLine()); + return new Money(money); + } + + private static int parseMoney(String input) { + try { + return Integer.parseInt(input); + } catch (NumberFormatException e) { + throw new NumberFormatException("숫자를 입력해주세요."); + } + } +} From 088692bf1ea86296d31ac727c185fe8f8ee5abfe Mon Sep 17 00:00:00 2001 From: KimSeongGyu1 Date: Tue, 18 Feb 2020 19:22:22 +0900 Subject: [PATCH 06/23] =?UTF-8?q?[feature]=20=ED=8B=B0=EC=BC=93=20?= =?UTF-8?q?=EA=B0=9D=EC=B2=B4=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 6개 숫자가 적절히 들어오지 않을 때 예외처리 구현 --- src/main/java/domain/LottoNumber.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/main/java/domain/LottoNumber.java b/src/main/java/domain/LottoNumber.java index 49a32984ea..4cf2c53286 100644 --- a/src/main/java/domain/LottoNumber.java +++ b/src/main/java/domain/LottoNumber.java @@ -26,4 +26,9 @@ public boolean equals(Object o) { LottoNumber that = (LottoNumber) o; return number == that.number; } + + @Override + public int hashCode() { + return Objects.hash(number); + } } From 1071d9a206f24ec0fa0fb5ea0c9e487c42fc4367 Mon Sep 17 00:00:00 2001 From: KimSeongGyu1 Date: Tue, 18 Feb 2020 20:10:37 +0900 Subject: [PATCH 07/23] =?UTF-8?q?[refactor]=20LottoNumber=20=ED=81=B4?= =?UTF-8?q?=EB=9E=98=EC=8A=A4=20enum=20type=EC=9C=BC=EB=A1=9C=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 지난 커밋 때 add하지 않은 Ticket 클래스 추가 * --- src/main/java/domain/LottoNumber.java | 86 +++++++++++++++++------ src/main/java/domain/Ticket.java | 21 ++++++ src/test/java/domain/LottoNumberTest.java | 12 +--- src/test/java/domain/TicketTest.java | 49 +++++++++++++ 4 files changed, 136 insertions(+), 32 deletions(-) create mode 100644 src/main/java/domain/Ticket.java create mode 100644 src/test/java/domain/TicketTest.java diff --git a/src/main/java/domain/LottoNumber.java b/src/main/java/domain/LottoNumber.java index 4cf2c53286..9b151d1899 100644 --- a/src/main/java/domain/LottoNumber.java +++ b/src/main/java/domain/LottoNumber.java @@ -1,34 +1,76 @@ package domain; -import java.util.Objects; +import java.util.Arrays; +import java.util.List; -public class LottoNumber { - private static final int MIN_LOTTO_NUMBER = 1; - private static final int MAX_LOTTO_NUMBER = 45; +public enum LottoNumber { + ONE(1), + TWO(2), + THREE(3), + FOUR(4), + FIVE(5), + SIX(6), + SEVEN(7), + EIGHT(8), + NINE(9), + TEN(10), + ELEVEN(11), + TWELVE(12), + THIRTEEN(13), + FOURTEEN(14), + FIFTEEN(15), + SIXTEEN(16), + SEVENTEEN(17), + EIGHTEEN(18), + NINETEEN(19), + TWENTY(20), + TWENTYONE(21), + TWENTYTWO(22), + TWENTYTHREE(23), + TWENTYFOUR(24), + TWENTYFIVE(25), + TWENTYSIX(26), + TWENTYSEVEN(27), + TWENTYEIGHT(28), + TWENTYNINE(29), + THIRTY(30), + THIRTYONE(31), + THIRTYTWO(32), + THIRTYTHREE(33), + THIRTYFOUR(34), + THIRTYFIVE(35), + THIRTYSIX(36), + THIRTYSEVEN(37), + THIRTYEIGHT(38), + THIRTYNINE(39), + FOURTY(40), + FOURTYONE(41), + FOURTYTWO(42), + FOURTYTHREE(43), + FOURTYFOUR(44), + FOURTYFIVE(45); + +// private static final int MIN_LOTTO_NUMBER = 1; +// private static final int MAX_LOTTO_NUMBER = 45; private final int number; - public LottoNumber(int number) { - validateLottoNumber(number); + LottoNumber(int number) { this.number = number; } - private void validateLottoNumber(int number) { - if (number < MIN_LOTTO_NUMBER || number > MAX_LOTTO_NUMBER) { - throw new IllegalArgumentException("1~45 사이의 숫자만 가능합니다."); - } - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - LottoNumber that = (LottoNumber) o; - return number == that.number; - } + public static LottoNumber getLottoNumber(int number) { + List lottoNumbers = Arrays.asList(LottoNumber.values()); - @Override - public int hashCode() { - return Objects.hash(number); + return lottoNumbers.stream() + .filter(lottoNumber -> lottoNumber.number == number) + .findFirst() + .orElseThrow(() -> new IllegalArgumentException("1부터 45사이의 숫자를 입력해주세요.")); } +// +// private static void validateNumber(int number) { +// if (number < MIN_LOTTO_NUMBER || number > MAX_LOTTO_NUMBER) { +// throw new IllegalArgumentException("1부터 45사이의 숫자를 입력해주세요."); +// } +// } } diff --git a/src/main/java/domain/Ticket.java b/src/main/java/domain/Ticket.java new file mode 100644 index 0000000000..a1e5b23892 --- /dev/null +++ b/src/main/java/domain/Ticket.java @@ -0,0 +1,21 @@ +package domain; + +import java.util.List; +import java.util.Set; + +public class Ticket { + private static final int TICKET_SIZE = 6; + + private final Set lottoNumbers; + + public Ticket(Set lottoNumbers) { + validateSize(lottoNumbers); + this.lottoNumbers = lottoNumbers; + } + + private void validateSize(Set lottoNumbers) { + if (lottoNumbers.size() != TICKET_SIZE) { + throw new IllegalArgumentException("6개의 숫자를 입력해주세요."); + } + } +} diff --git a/src/test/java/domain/LottoNumberTest.java b/src/test/java/domain/LottoNumberTest.java index 4b1a87fbe3..7168ffac5f 100644 --- a/src/test/java/domain/LottoNumberTest.java +++ b/src/test/java/domain/LottoNumberTest.java @@ -11,16 +11,8 @@ public class LottoNumberTest { @Test @DisplayName("로또 숫자 생성 예외처리") void lottoNumberConstructorTest() { - assertThatThrownBy(() -> new LottoNumber(46)) + assertThatThrownBy(() -> LottoNumber.getLottoNumber(46)) .isInstanceOf(IllegalArgumentException.class) - .hasMessage("1~45 사이의 숫자만 가능합니다."); - } - - @Test - @DisplayName("로또 숫자 비교") - void lottoNumberEqualTest() { - LottoNumber number = new LottoNumber(5); - assertThat(number.equals(new LottoNumber(5))).isTrue(); - assertThat(number.equals(new LottoNumber(4))).isFalse(); + .hasMessage("1부터 45사이의 숫자를 입력해주세요."); } } diff --git a/src/test/java/domain/TicketTest.java b/src/test/java/domain/TicketTest.java new file mode 100644 index 0000000000..9e6ac2689a --- /dev/null +++ b/src/test/java/domain/TicketTest.java @@ -0,0 +1,49 @@ +package domain; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +@DisplayName("티켓 객체 테스트") +public class TicketTest { + @Test + @DisplayName("티켓 생성") + void ticketConstructor() { + Set lottoNumberList = new HashSet<>(Arrays.asList(LottoNumber.getLottoNumber(1), + LottoNumber.getLottoNumber(2), + LottoNumber.getLottoNumber(3), + LottoNumber.getLottoNumber(4), + LottoNumber.getLottoNumber(5))); + assertThatThrownBy(() -> new Ticket(lottoNumberList)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("6개의 숫자를 입력해주세요."); + } + +// @Test +// @DisplayName("두 티켓의 같은 숫자 비교") +// void compareTwoTickets() { +// Set lottoNumberSet1 = createLottoNumber(1, 2, 3, 4, 5, 6); +// Set lottoNumberSet2 = createLottoNumber(4, 5, 6, 7, 8, 9); +// Ticket ticket1 = new Ticket(lottoNumberSet1); +// Ticket ticket2 = new Ticket(lottoNumberSet2); +// assertThat(ticket1.getWith(ticket2)).isEqualTo(3); +// } +// +// private Set createLottoNumber(int number1, int number2, int number3, int number4, int number5, int number6) { +// Set lottoNumberList = new HashSet<>(Arrays.asList(LottoNumber.getLottoNumber(number1), +// LottoNumber.getLottoNumber(number2), +// LottoNumber.getLottoNumber(number3), +// LottoNumber.getLottoNumber(number4), +// LottoNumber.getLottoNumber(number5), +// LottoNumber.getLottoNumber(number6))); +// return lottoNumberList; +// } +} From 3b46dbf05204e15d6ff9d45c4bc11368309a7ca1 Mon Sep 17 00:00:00 2001 From: KimSeongGyu1 Date: Tue, 18 Feb 2020 20:38:28 +0900 Subject: [PATCH 08/23] =?UTF-8?q?[feature]=20=EB=8B=B9=EC=B2=A8=20?= =?UTF-8?q?=EB=B2=88=ED=98=B8=20=EA=B0=9D=EC=B2=B4=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 6개의 숫자를 체크하는 추상 클래스 구현 후 Ticket과 WinningNumbers에서 상속 --- README.md | 5 ++- .../java/domain/AbstractNumbersContainer.java | 13 ++++++++ src/main/java/domain/Ticket.java | 9 +---- src/main/java/domain/WinningNumbers.java | 15 +++++++++ src/test/java/domain/WinningNumbersTest.java | 33 +++++++++++++++++++ 5 files changed, 66 insertions(+), 9 deletions(-) create mode 100644 src/main/java/domain/AbstractNumbersContainer.java create mode 100644 src/main/java/domain/WinningNumbers.java create mode 100644 src/test/java/domain/WinningNumbersTest.java diff --git a/README.md b/README.md index 600fad0e27..88adb55756 100644 --- a/README.md +++ b/README.md @@ -25,4 +25,7 @@ - else를 사용하지 마라. - 메소드의 크기가 최대 10라인을 넘지 않도록 구현한다. - 배열 대신 ArrayList를 사용한다. -- enum을 적용해 프로그래밍을 구현한다. \ No newline at end of file +- enum을 적용해 프로그래밍을 구현한다. +- 규칙 3: 모든 원시값과 문자열을 포장한다. +- 규칙 5: 줄여쓰지 않는다(축약 금지). +- 규칙 8: 일급 콜렉션을 쓴다. \ No newline at end of file diff --git a/src/main/java/domain/AbstractNumbersContainer.java b/src/main/java/domain/AbstractNumbersContainer.java new file mode 100644 index 0000000000..7569e7ab6a --- /dev/null +++ b/src/main/java/domain/AbstractNumbersContainer.java @@ -0,0 +1,13 @@ +package domain; + +import java.util.Set; + +abstract class AbstractNumbersContainer { + private static final int TICKET_SIZE = 6; + + protected void validateSize(Set lottoNumbers) { + if (lottoNumbers.size() != TICKET_SIZE) { + throw new IllegalArgumentException("6개의 숫자를 입력해주세요."); + } + } +} diff --git a/src/main/java/domain/Ticket.java b/src/main/java/domain/Ticket.java index a1e5b23892..25baa42a5a 100644 --- a/src/main/java/domain/Ticket.java +++ b/src/main/java/domain/Ticket.java @@ -3,8 +3,7 @@ import java.util.List; import java.util.Set; -public class Ticket { - private static final int TICKET_SIZE = 6; +public class Ticket extends AbstractNumbersContainer { private final Set lottoNumbers; @@ -12,10 +11,4 @@ public Ticket(Set lottoNumbers) { validateSize(lottoNumbers); this.lottoNumbers = lottoNumbers; } - - private void validateSize(Set lottoNumbers) { - if (lottoNumbers.size() != TICKET_SIZE) { - throw new IllegalArgumentException("6개의 숫자를 입력해주세요."); - } - } } diff --git a/src/main/java/domain/WinningNumbers.java b/src/main/java/domain/WinningNumbers.java new file mode 100644 index 0000000000..10d87376ae --- /dev/null +++ b/src/main/java/domain/WinningNumbers.java @@ -0,0 +1,15 @@ +package domain; + +import java.util.Set; + +public class WinningNumbers extends AbstractNumbersContainer { + + private final Set lastWeekNumbers; + private final LottoNumber bonusNumber; + + public WinningNumbers(Set lastWeekNumbers, LottoNumber bonusNumber) { + validateSize(lastWeekNumbers); + this.lastWeekNumbers = lastWeekNumbers; + this.bonusNumber = bonusNumber; + } +} \ No newline at end of file diff --git a/src/test/java/domain/WinningNumbersTest.java b/src/test/java/domain/WinningNumbersTest.java new file mode 100644 index 0000000000..86e3663faa --- /dev/null +++ b/src/test/java/domain/WinningNumbersTest.java @@ -0,0 +1,33 @@ +package domain; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +@DisplayName("당첨 번호 객체 테스트") +public class WinningNumbersTest { + @Test + @DisplayName("당첨 번호 객체 생성") + void winningNumberConstructor() { + Set lastWeekNumbers = createLastWeekNumbers(1, 2, 3, 4, 5, 5); + LottoNumber bonusNumber = LottoNumber.getLottoNumber(7); + assertThatThrownBy(() -> new WinningNumbers(lastWeekNumbers, bonusNumber)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("6개의 숫자를 입력해주세요."); + } + + private Set createLastWeekNumbers(int number1, int number2, int number3, int number4, int number5, int number6) { + Set lottoNumberList = new HashSet<>(Arrays.asList(LottoNumber.getLottoNumber(number1), + LottoNumber.getLottoNumber(number2), + LottoNumber.getLottoNumber(number3), + LottoNumber.getLottoNumber(number4), + LottoNumber.getLottoNumber(number5), + LottoNumber.getLottoNumber(number6))); + return lottoNumberList; + } +} From 25b425ce87c29a460db68f2ccc526adab35e455a Mon Sep 17 00:00:00 2001 From: KimSeongGyu1 Date: Wed, 19 Feb 2020 12:54:33 +0900 Subject: [PATCH 09/23] =?UTF-8?q?[refactor]=20LottoNumbersDto=20=EC=A0=81?= =?UTF-8?q?=EC=9A=A9=20=EB=B0=8F=20=EA=B4=80=EB=A0=A8=20=EB=A1=9C=EC=A7=81?= =?UTF-8?q?=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Ticket 객체와 WinningNumbers 객체의 생성자에 적용 --- README.md | 2 +- src/main/java/domain/LottoNumber.java | 1 + ...tainer.java => LottoNumbersContainer.java} | 9 +++++- src/main/java/domain/LottoNumbersDto.java | 25 ++++++++++++++++ src/main/java/domain/Ticket.java | 11 ++----- src/main/java/domain/WinningNumbers.java | 30 ++++++++++++++----- src/main/java/view/InputView.java | 11 +++++-- src/test/java/domain/TicketTest.java | 28 +++++++---------- src/test/java/domain/WinningNumbersTest.java | 13 ++++---- 9 files changed, 85 insertions(+), 45 deletions(-) rename src/main/java/domain/{AbstractNumbersContainer.java => LottoNumbersContainer.java} (51%) create mode 100644 src/main/java/domain/LottoNumbersDto.java diff --git a/README.md b/README.md index 88adb55756..57999d2ed4 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ > 기능 요구사항 -- [ ] 구입금액을 입력받는다. +- [x] 구입금액을 입력받는다. - [x] (예외) 1000원 이하 단위일 경우 - [x] (예외) 숫자가 아닐 경우 - [x] (예외) 음수 또는 50000원 이상 입력될 경우 diff --git a/src/main/java/domain/LottoNumber.java b/src/main/java/domain/LottoNumber.java index 9b151d1899..18b6307a4f 100644 --- a/src/main/java/domain/LottoNumber.java +++ b/src/main/java/domain/LottoNumber.java @@ -4,6 +4,7 @@ import java.util.List; public enum LottoNumber { + ERROR(-1), ONE(1), TWO(2), THREE(3), diff --git a/src/main/java/domain/AbstractNumbersContainer.java b/src/main/java/domain/LottoNumbersContainer.java similarity index 51% rename from src/main/java/domain/AbstractNumbersContainer.java rename to src/main/java/domain/LottoNumbersContainer.java index 7569e7ab6a..47cceb197b 100644 --- a/src/main/java/domain/AbstractNumbersContainer.java +++ b/src/main/java/domain/LottoNumbersContainer.java @@ -2,9 +2,16 @@ import java.util.Set; -abstract class AbstractNumbersContainer { +abstract class LottoNumbersContainer { private static final int TICKET_SIZE = 6; + protected final Set lottoSixNumbers; + + public LottoNumbersContainer(LottoNumbersDto lottoNumbersDto) { + validateSize(lottoNumbersDto.getSixNumbers()); + this.lottoSixNumbers = lottoNumbersDto.getSixNumbers(); + } + protected void validateSize(Set lottoNumbers) { if (lottoNumbers.size() != TICKET_SIZE) { throw new IllegalArgumentException("6개의 숫자를 입력해주세요."); diff --git a/src/main/java/domain/LottoNumbersDto.java b/src/main/java/domain/LottoNumbersDto.java new file mode 100644 index 0000000000..ce8ed49c25 --- /dev/null +++ b/src/main/java/domain/LottoNumbersDto.java @@ -0,0 +1,25 @@ +package domain; + +import java.util.Set; + +public class LottoNumbersDto { + private final Set sixNumbers; + private final LottoNumber bonusNumber; + + public LottoNumbersDto(Set sixNumbers) { + this(sixNumbers, LottoNumber.ERROR); + } + + public LottoNumbersDto(Set sixNumbers, LottoNumber bonusNumber) { + this.sixNumbers = sixNumbers; + this.bonusNumber = bonusNumber; + } + + public Set getSixNumbers() { + return sixNumbers; + } + + public LottoNumber getBonusNumber() { + return bonusNumber; + } +} diff --git a/src/main/java/domain/Ticket.java b/src/main/java/domain/Ticket.java index 25baa42a5a..c6262c3ba4 100644 --- a/src/main/java/domain/Ticket.java +++ b/src/main/java/domain/Ticket.java @@ -1,14 +1,9 @@ package domain; -import java.util.List; import java.util.Set; -public class Ticket extends AbstractNumbersContainer { - - private final Set lottoNumbers; - - public Ticket(Set lottoNumbers) { - validateSize(lottoNumbers); - this.lottoNumbers = lottoNumbers; +public class Ticket extends LottoNumbersContainer { + public Ticket(LottoNumbersDto lottoNumbersDto) { + super(lottoNumbersDto); } } diff --git a/src/main/java/domain/WinningNumbers.java b/src/main/java/domain/WinningNumbers.java index 10d87376ae..ba563f1d6c 100644 --- a/src/main/java/domain/WinningNumbers.java +++ b/src/main/java/domain/WinningNumbers.java @@ -1,15 +1,29 @@ package domain; -import java.util.Set; +public class WinningNumbers extends LottoNumbersContainer { -public class WinningNumbers extends AbstractNumbersContainer { - - private final Set lastWeekNumbers; private final LottoNumber bonusNumber; - public WinningNumbers(Set lastWeekNumbers, LottoNumber bonusNumber) { - validateSize(lastWeekNumbers); - this.lastWeekNumbers = lastWeekNumbers; - this.bonusNumber = bonusNumber; + public WinningNumbers(LottoNumbersDto lottoNumbersDto) { + super(lottoNumbersDto); + validateBonusNumber(lottoNumbersDto.getBonusNumber()); + this.bonusNumber = lottoNumbersDto.getBonusNumber(); + } + + private void validateBonusNumber(LottoNumber bonusNumber) { + validateErrorBonusNumber(bonusNumber); + validateDuplicatedBonusNumber(bonusNumber); + } + + private void validateErrorBonusNumber(LottoNumber bonusNumber) { + if (bonusNumber != LottoNumber.ERROR) { + throw new IllegalArgumentException("잘못된 보너스 번호를 입력했습니다."); + } + } + + private void validateDuplicatedBonusNumber(LottoNumber bonusNumber) { + if (this.lottoSixNumbers.contains(bonusNumber)) { + throw new IllegalArgumentException("당첨번호와 보너스 번호가 중복됩니다."); + } } } \ No newline at end of file diff --git a/src/main/java/view/InputView.java b/src/main/java/view/InputView.java index af53b00ad6..0ea277d7bb 100644 --- a/src/main/java/view/InputView.java +++ b/src/main/java/view/InputView.java @@ -5,6 +5,8 @@ import java.util.Scanner; public class InputView { + private static final String NUMBER_REGEX = "^[0-9]+$"; + private static Scanner scanner = new Scanner(System.in); public static Money enterMoney() { @@ -14,9 +16,12 @@ public static Money enterMoney() { } private static int parseMoney(String input) { - try { - return Integer.parseInt(input); - } catch (NumberFormatException e) { + validateNumber(input); + return Integer.parseInt(input); + } + + private static void validateNumber(String input) { + if (!input.matches(NUMBER_REGEX)) { throw new NumberFormatException("숫자를 입력해주세요."); } } diff --git a/src/test/java/domain/TicketTest.java b/src/test/java/domain/TicketTest.java index 9e6ac2689a..54bec88381 100644 --- a/src/test/java/domain/TicketTest.java +++ b/src/test/java/domain/TicketTest.java @@ -5,9 +5,6 @@ import java.util.Arrays; import java.util.HashSet; -import java.util.List; -import java.util.Set; -import java.util.stream.Collectors; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; @@ -17,12 +14,8 @@ public class TicketTest { @Test @DisplayName("티켓 생성") void ticketConstructor() { - Set lottoNumberList = new HashSet<>(Arrays.asList(LottoNumber.getLottoNumber(1), - LottoNumber.getLottoNumber(2), - LottoNumber.getLottoNumber(3), - LottoNumber.getLottoNumber(4), - LottoNumber.getLottoNumber(5))); - assertThatThrownBy(() -> new Ticket(lottoNumberList)) + LottoNumbersDto lottoNumbersDto = createLottoNumberDto(1,2,3,4,5,5); + assertThatThrownBy(() -> new Ticket(lottoNumbersDto)) .isInstanceOf(IllegalArgumentException.class) .hasMessage("6개의 숫자를 입력해주세요."); } @@ -37,13 +30,12 @@ void ticketConstructor() { // assertThat(ticket1.getWith(ticket2)).isEqualTo(3); // } // -// private Set createLottoNumber(int number1, int number2, int number3, int number4, int number5, int number6) { -// Set lottoNumberList = new HashSet<>(Arrays.asList(LottoNumber.getLottoNumber(number1), -// LottoNumber.getLottoNumber(number2), -// LottoNumber.getLottoNumber(number3), -// LottoNumber.getLottoNumber(number4), -// LottoNumber.getLottoNumber(number5), -// LottoNumber.getLottoNumber(number6))); -// return lottoNumberList; -// } + private LottoNumbersDto createLottoNumberDto(int number1, int number2, int number3, int number4, int number5, int number6) { + return new LottoNumbersDto(new HashSet<>(Arrays.asList(LottoNumber.getLottoNumber(number1), + LottoNumber.getLottoNumber(number2), + LottoNumber.getLottoNumber(number3), + LottoNumber.getLottoNumber(number4), + LottoNumber.getLottoNumber(number5), + LottoNumber.getLottoNumber(number6)))); + } } diff --git a/src/test/java/domain/WinningNumbersTest.java b/src/test/java/domain/WinningNumbersTest.java index 86e3663faa..54dfabaff9 100644 --- a/src/test/java/domain/WinningNumbersTest.java +++ b/src/test/java/domain/WinningNumbersTest.java @@ -14,20 +14,21 @@ public class WinningNumbersTest { @Test @DisplayName("당첨 번호 객체 생성") void winningNumberConstructor() { - Set lastWeekNumbers = createLastWeekNumbers(1, 2, 3, 4, 5, 5); - LottoNumber bonusNumber = LottoNumber.getLottoNumber(7); - assertThatThrownBy(() -> new WinningNumbers(lastWeekNumbers, bonusNumber)) + LottoNumbersDto lottoNumbersDto = createLottoNumberDto(1, 2, 3, 4, 5, 5, 7); + assertThatThrownBy(() -> new WinningNumbers(lottoNumbersDto)) .isInstanceOf(IllegalArgumentException.class) .hasMessage("6개의 숫자를 입력해주세요."); } - private Set createLastWeekNumbers(int number1, int number2, int number3, int number4, int number5, int number6) { - Set lottoNumberList = new HashSet<>(Arrays.asList(LottoNumber.getLottoNumber(number1), + private LottoNumbersDto createLottoNumberDto(int number1, int number2, int number3, int number4, int number5, int number6, int number7) { + Set sixNumbers = new HashSet<>(Arrays.asList(LottoNumber.getLottoNumber(number1), LottoNumber.getLottoNumber(number2), LottoNumber.getLottoNumber(number3), LottoNumber.getLottoNumber(number4), LottoNumber.getLottoNumber(number5), LottoNumber.getLottoNumber(number6))); - return lottoNumberList; + LottoNumber bonusNumber = LottoNumber.getLottoNumber(number7); + + return new LottoNumbersDto(sixNumbers, bonusNumber); } } From 62f3572e3d8e82ebc21072cb57a12120bdeb8ad9 Mon Sep 17 00:00:00 2001 From: KimSeongGyu1 Date: Wed, 19 Feb 2020 13:37:58 +0900 Subject: [PATCH 10/23] =?UTF-8?q?[feature]=20=ED=8B=B0=EC=BC=93=EA=B3=BC?= =?UTF-8?q?=20=EB=8B=B9=EC=B2=A8=20=EB=B2=88=ED=98=B8=20=EC=A4=91=EB=B3=B5?= =?UTF-8?q?=20=ED=99=95=EC=9D=B8=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/domain/LottoNumber.java | 2 +- src/main/java/domain/Ticket.java | 8 +++--- src/main/java/domain/WinningNumbers.java | 16 +++++++++--- src/test/java/domain/TicketTest.java | 21 ++++++++-------- src/test/java/domain/WinningNumbersTest.java | 26 +++++++++++++++++++- 5 files changed, 54 insertions(+), 19 deletions(-) diff --git a/src/main/java/domain/LottoNumber.java b/src/main/java/domain/LottoNumber.java index 18b6307a4f..ba61d2d4d1 100644 --- a/src/main/java/domain/LottoNumber.java +++ b/src/main/java/domain/LottoNumber.java @@ -66,7 +66,7 @@ public static LottoNumber getLottoNumber(int number) { return lottoNumbers.stream() .filter(lottoNumber -> lottoNumber.number == number) .findFirst() - .orElseThrow(() -> new IllegalArgumentException("1부터 45사이의 숫자를 입력해주세요.")); + .orElse(LottoNumber.ERROR); } // // private static void validateNumber(int number) { diff --git a/src/main/java/domain/Ticket.java b/src/main/java/domain/Ticket.java index c6262c3ba4..99e673baf9 100644 --- a/src/main/java/domain/Ticket.java +++ b/src/main/java/domain/Ticket.java @@ -1,9 +1,11 @@ package domain; -import java.util.Set; - public class Ticket extends LottoNumbersContainer { public Ticket(LottoNumbersDto lottoNumbersDto) { super(lottoNumbersDto); } -} + + public boolean contains(LottoNumber number) { + return this.lottoSixNumbers.contains(number); + } +} \ No newline at end of file diff --git a/src/main/java/domain/WinningNumbers.java b/src/main/java/domain/WinningNumbers.java index ba563f1d6c..8f0203abae 100644 --- a/src/main/java/domain/WinningNumbers.java +++ b/src/main/java/domain/WinningNumbers.java @@ -1,5 +1,8 @@ package domain; +import java.util.Iterator; +import java.util.Set; + public class WinningNumbers extends LottoNumbersContainer { private final LottoNumber bonusNumber; @@ -16,14 +19,21 @@ private void validateBonusNumber(LottoNumber bonusNumber) { } private void validateErrorBonusNumber(LottoNumber bonusNumber) { - if (bonusNumber != LottoNumber.ERROR) { - throw new IllegalArgumentException("잘못된 보너스 번호를 입력했습니다."); + if (bonusNumber == LottoNumber.ERROR) { + throw new IllegalArgumentException("1~45 사이의 보너스 번호를 입력해주세요."); } } private void validateDuplicatedBonusNumber(LottoNumber bonusNumber) { if (this.lottoSixNumbers.contains(bonusNumber)) { - throw new IllegalArgumentException("당첨번호와 보너스 번호가 중복됩니다."); + throw new IllegalArgumentException("당첨 번호와 중복되지 않는 보너스 번호를 입력해주세요."); } } + + public int findDuplicatedNumbers(Ticket ticket) { + return this.lottoSixNumbers.stream() + .filter(number -> ticket.contains(number)) + .mapToInt(number -> 1) + .sum(); + } } \ No newline at end of file diff --git a/src/test/java/domain/TicketTest.java b/src/test/java/domain/TicketTest.java index 54bec88381..cc180b2efa 100644 --- a/src/test/java/domain/TicketTest.java +++ b/src/test/java/domain/TicketTest.java @@ -5,6 +5,7 @@ import java.util.Arrays; import java.util.HashSet; +import java.util.Set; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; @@ -14,22 +15,20 @@ public class TicketTest { @Test @DisplayName("티켓 생성") void ticketConstructor() { - LottoNumbersDto lottoNumbersDto = createLottoNumberDto(1,2,3,4,5,5); + LottoNumbersDto lottoNumbersDto = createLottoNumberDto(1, 2, 3, 4, 5, 5); assertThatThrownBy(() -> new Ticket(lottoNumbersDto)) .isInstanceOf(IllegalArgumentException.class) .hasMessage("6개의 숫자를 입력해주세요."); } -// @Test -// @DisplayName("두 티켓의 같은 숫자 비교") -// void compareTwoTickets() { -// Set lottoNumberSet1 = createLottoNumber(1, 2, 3, 4, 5, 6); -// Set lottoNumberSet2 = createLottoNumber(4, 5, 6, 7, 8, 9); -// Ticket ticket1 = new Ticket(lottoNumberSet1); -// Ticket ticket2 = new Ticket(lottoNumberSet2); -// assertThat(ticket1.getWith(ticket2)).isEqualTo(3); -// } -// + @Test + @DisplayName("두 티켓의 같은 숫자 비교") + void compareTwoTickets() { + LottoNumbersDto lottoNumbersDto = createLottoNumberDto(1, 2, 3, 4, 5, 6); + Ticket ticket = new Ticket(lottoNumbersDto); + assertThat(ticket.contains(LottoNumber.THREE)).isTrue(); + } + private LottoNumbersDto createLottoNumberDto(int number1, int number2, int number3, int number4, int number5, int number6) { return new LottoNumbersDto(new HashSet<>(Arrays.asList(LottoNumber.getLottoNumber(number1), LottoNumber.getLottoNumber(number2), diff --git a/src/test/java/domain/WinningNumbersTest.java b/src/test/java/domain/WinningNumbersTest.java index 54dfabaff9..6863809178 100644 --- a/src/test/java/domain/WinningNumbersTest.java +++ b/src/test/java/domain/WinningNumbersTest.java @@ -2,11 +2,15 @@ import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; +import org.junit.jupiter.params.provider.ValueSource; import java.util.Arrays; import java.util.HashSet; import java.util.Set; +import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; @DisplayName("당첨 번호 객체 테스트") @@ -20,6 +24,26 @@ void winningNumberConstructor() { .hasMessage("6개의 숫자를 입력해주세요."); } + @ParameterizedTest + @ValueSource(ints = {-1, 1}) + @DisplayName("유효한 보너스 번호인지 검증") + void validateBonusNumber(int input) { + LottoNumbersDto lottoNumbersDto = createLottoNumberDto(1, 2, 3, 4, 5, 6, input); + assertThatThrownBy(() -> new WinningNumbers(lottoNumbersDto)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageEndingWith("보너스 번호를 입력해주세요."); + } + + @Test + @DisplayName("Ticket과 당첨 번호 비교") + void name() { + LottoNumbersDto lottoNumbersDto1 = createLottoNumberDto(1, 2, 3, 4, 5, 6, 7); + LottoNumbersDto lottoNumbersDto2 = createLottoNumberDto(4, 5, 6, 7, 8, 9, 10); + WinningNumbers winningNumbers = new WinningNumbers(lottoNumbersDto1); + Ticket ticket = new Ticket(lottoNumbersDto2); + assertThat((winningNumbers.findDuplicatedNumbers(ticket))).isEqualTo(3); + } + private LottoNumbersDto createLottoNumberDto(int number1, int number2, int number3, int number4, int number5, int number6, int number7) { Set sixNumbers = new HashSet<>(Arrays.asList(LottoNumber.getLottoNumber(number1), LottoNumber.getLottoNumber(number2), @@ -31,4 +55,4 @@ private LottoNumbersDto createLottoNumberDto(int number1, int number2, int numbe return new LottoNumbersDto(sixNumbers, bonusNumber); } -} +} \ No newline at end of file From 34361aba02be9b1791de4a09500f03f89c6034f0 Mon Sep 17 00:00:00 2001 From: KimSeongGyu1 Date: Wed, 19 Feb 2020 15:10:22 +0900 Subject: [PATCH 11/23] =?UTF-8?q?[feature]=20=EB=9E=9C=EB=8D=A4=20?= =?UTF-8?q?=ED=8C=A9=ED=86=A0=EB=A6=AC=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Dto 반환하는 팩토리 interface 기반 구현 --- src/main/java/domain/LottoNumber.java | 48 +++++++++---------- .../factory/LottoNumbersDtoFactory.java | 7 +++ .../java/domain/factory/RandomFactory.java | 32 +++++++++++++ .../LottoNumbersContainer.java | 4 +- .../LottoNumbersDto.java | 4 +- .../domain/{ => numberscontainer}/Ticket.java | 4 +- .../WinningNumbers.java | 5 +- .../factory/LottoNumbersDtoFactoryTest.java | 18 +++++++ .../{ => numberscontainer}/TicketTest.java | 6 ++- .../WinningNumbersTest.java | 9 ++-- 10 files changed, 102 insertions(+), 35 deletions(-) create mode 100644 src/main/java/domain/factory/LottoNumbersDtoFactory.java create mode 100644 src/main/java/domain/factory/RandomFactory.java rename src/main/java/domain/{ => numberscontainer}/LottoNumbersContainer.java (90%) rename src/main/java/domain/{ => numberscontainer}/LottoNumbersDto.java (90%) rename src/main/java/domain/{ => numberscontainer}/Ticket.java (80%) rename src/main/java/domain/{ => numberscontainer}/WinningNumbers.java (95%) create mode 100644 src/test/java/domain/factory/LottoNumbersDtoFactoryTest.java rename src/test/java/domain/{ => numberscontainer}/TicketTest.java (91%) rename src/test/java/domain/{ => numberscontainer}/WinningNumbersTest.java (91%) diff --git a/src/main/java/domain/LottoNumber.java b/src/main/java/domain/LottoNumber.java index ba61d2d4d1..5bf015420d 100644 --- a/src/main/java/domain/LottoNumber.java +++ b/src/main/java/domain/LottoNumber.java @@ -25,31 +25,31 @@ public enum LottoNumber { EIGHTEEN(18), NINETEEN(19), TWENTY(20), - TWENTYONE(21), - TWENTYTWO(22), - TWENTYTHREE(23), - TWENTYFOUR(24), - TWENTYFIVE(25), - TWENTYSIX(26), - TWENTYSEVEN(27), - TWENTYEIGHT(28), - TWENTYNINE(29), + TWENTY_ONE(21), + TWENTY_TWO(22), + TWENTY_THREE(23), + TWENTY_FOUR(24), + TWENTY_FIVE(25), + TWENTY_SIX(26), + TWENTY_SEVEN(27), + TWENTY_EIGHT(28), + TWENTY_NINE(29), THIRTY(30), - THIRTYONE(31), - THIRTYTWO(32), - THIRTYTHREE(33), - THIRTYFOUR(34), - THIRTYFIVE(35), - THIRTYSIX(36), - THIRTYSEVEN(37), - THIRTYEIGHT(38), - THIRTYNINE(39), - FOURTY(40), - FOURTYONE(41), - FOURTYTWO(42), - FOURTYTHREE(43), - FOURTYFOUR(44), - FOURTYFIVE(45); + THIRTY_ONE(31), + THIRTY_TWO(32), + THIRTY_THREE(33), + THIRTY_FOUR(34), + THIRTY_FIVE(35), + THIRTY_SIX(36), + THIRTY_SEVEN(37), + THIRTY_EIGHT(38), + THIRTY_NINE(39), + FORTY(40), + FORTY_ONE(41), + FORTY_TWO(42), + FORTY_THREE(43), + FORTY_FOUR(44), + FORTY_FIVE(45); // private static final int MIN_LOTTO_NUMBER = 1; // private static final int MAX_LOTTO_NUMBER = 45; diff --git a/src/main/java/domain/factory/LottoNumbersDtoFactory.java b/src/main/java/domain/factory/LottoNumbersDtoFactory.java new file mode 100644 index 0000000000..e83b90613e --- /dev/null +++ b/src/main/java/domain/factory/LottoNumbersDtoFactory.java @@ -0,0 +1,7 @@ +package domain.factory; + +import domain.numberscontainer.LottoNumbersDto; + +public interface LottoNumbersDtoFactory { + LottoNumbersDto generate(boolean containsBonus); +} diff --git a/src/main/java/domain/factory/RandomFactory.java b/src/main/java/domain/factory/RandomFactory.java new file mode 100644 index 0000000000..11bbb0fa27 --- /dev/null +++ b/src/main/java/domain/factory/RandomFactory.java @@ -0,0 +1,32 @@ +package domain.factory; + +import domain.LottoNumber; +import domain.numberscontainer.LottoNumbersDto; + +import java.util.*; + +public class RandomFactory implements LottoNumbersDtoFactory{ + + private static final int FIRST_INDEX = 0; + private static final int SIXTH_INDEX = 6; + private static final int EXCEPT_ERROR_FIRST_INDEX = 1; + + @Override + public LottoNumbersDto generate(boolean containsBonus) { + List lottoNumbers = getShuffledList(); + + Set sixNumbers = new HashSet<>(lottoNumbers.subList(FIRST_INDEX, SIXTH_INDEX)); + LottoNumber bonus = lottoNumbers.get(SIXTH_INDEX); + + if (containsBonus) { + return new LottoNumbersDto(sixNumbers, bonus); + } + return new LottoNumbersDto(sixNumbers); + } + + private List getShuffledList() { + List lottoNumbers = Arrays.asList(LottoNumber.values()).subList(EXCEPT_ERROR_FIRST_INDEX, LottoNumber.values().length); + Collections.shuffle(lottoNumbers); + return lottoNumbers; + } +} diff --git a/src/main/java/domain/LottoNumbersContainer.java b/src/main/java/domain/numberscontainer/LottoNumbersContainer.java similarity index 90% rename from src/main/java/domain/LottoNumbersContainer.java rename to src/main/java/domain/numberscontainer/LottoNumbersContainer.java index 47cceb197b..9f8d6eff25 100644 --- a/src/main/java/domain/LottoNumbersContainer.java +++ b/src/main/java/domain/numberscontainer/LottoNumbersContainer.java @@ -1,4 +1,6 @@ -package domain; +package domain.numberscontainer; + +import domain.LottoNumber; import java.util.Set; diff --git a/src/main/java/domain/LottoNumbersDto.java b/src/main/java/domain/numberscontainer/LottoNumbersDto.java similarity index 90% rename from src/main/java/domain/LottoNumbersDto.java rename to src/main/java/domain/numberscontainer/LottoNumbersDto.java index ce8ed49c25..d0d67ea22c 100644 --- a/src/main/java/domain/LottoNumbersDto.java +++ b/src/main/java/domain/numberscontainer/LottoNumbersDto.java @@ -1,4 +1,6 @@ -package domain; +package domain.numberscontainer; + +import domain.LottoNumber; import java.util.Set; diff --git a/src/main/java/domain/Ticket.java b/src/main/java/domain/numberscontainer/Ticket.java similarity index 80% rename from src/main/java/domain/Ticket.java rename to src/main/java/domain/numberscontainer/Ticket.java index 99e673baf9..e3435bd76e 100644 --- a/src/main/java/domain/Ticket.java +++ b/src/main/java/domain/numberscontainer/Ticket.java @@ -1,4 +1,6 @@ -package domain; +package domain.numberscontainer; + +import domain.LottoNumber; public class Ticket extends LottoNumbersContainer { public Ticket(LottoNumbersDto lottoNumbersDto) { diff --git a/src/main/java/domain/WinningNumbers.java b/src/main/java/domain/numberscontainer/WinningNumbers.java similarity index 95% rename from src/main/java/domain/WinningNumbers.java rename to src/main/java/domain/numberscontainer/WinningNumbers.java index 8f0203abae..d746d13cd1 100644 --- a/src/main/java/domain/WinningNumbers.java +++ b/src/main/java/domain/numberscontainer/WinningNumbers.java @@ -1,7 +1,6 @@ -package domain; +package domain.numberscontainer; -import java.util.Iterator; -import java.util.Set; +import domain.LottoNumber; public class WinningNumbers extends LottoNumbersContainer { diff --git a/src/test/java/domain/factory/LottoNumbersDtoFactoryTest.java b/src/test/java/domain/factory/LottoNumbersDtoFactoryTest.java new file mode 100644 index 0000000000..8cc0f371be --- /dev/null +++ b/src/test/java/domain/factory/LottoNumbersDtoFactoryTest.java @@ -0,0 +1,18 @@ +package domain.factory; + +import domain.numberscontainer.Ticket; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThatCode; + +class LottoNumbersDtoFactoryTest { + + @Test + @DisplayName("랜덤 티켓 생성") + void generateRandomTicket() { + RandomFactory randomFactory = new RandomFactory(); + assertThatCode(() -> new Ticket(randomFactory.generate(false))) + .doesNotThrowAnyException(); + } +} \ No newline at end of file diff --git a/src/test/java/domain/TicketTest.java b/src/test/java/domain/numberscontainer/TicketTest.java similarity index 91% rename from src/test/java/domain/TicketTest.java rename to src/test/java/domain/numberscontainer/TicketTest.java index cc180b2efa..fe2a36fd1f 100644 --- a/src/test/java/domain/TicketTest.java +++ b/src/test/java/domain/numberscontainer/TicketTest.java @@ -1,11 +1,13 @@ -package domain; +package domain.numberscontainer; +import domain.LottoNumber; +import domain.numberscontainer.LottoNumbersDto; +import domain.numberscontainer.Ticket; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import java.util.Arrays; import java.util.HashSet; -import java.util.Set; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; diff --git a/src/test/java/domain/WinningNumbersTest.java b/src/test/java/domain/numberscontainer/WinningNumbersTest.java similarity index 91% rename from src/test/java/domain/WinningNumbersTest.java rename to src/test/java/domain/numberscontainer/WinningNumbersTest.java index 6863809178..0c21f8b49e 100644 --- a/src/test/java/domain/WinningNumbersTest.java +++ b/src/test/java/domain/numberscontainer/WinningNumbersTest.java @@ -1,9 +1,12 @@ -package domain; +package domain.numberscontainer; +import domain.LottoNumber; +import domain.numberscontainer.LottoNumbersDto; +import domain.numberscontainer.Ticket; +import domain.numberscontainer.WinningNumbers; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.CsvSource; import org.junit.jupiter.params.provider.ValueSource; import java.util.Arrays; @@ -36,7 +39,7 @@ void validateBonusNumber(int input) { @Test @DisplayName("Ticket과 당첨 번호 비교") - void name() { + void findDuplicatedNumbers() { LottoNumbersDto lottoNumbersDto1 = createLottoNumberDto(1, 2, 3, 4, 5, 6, 7); LottoNumbersDto lottoNumbersDto2 = createLottoNumberDto(4, 5, 6, 7, 8, 9, 10); WinningNumbers winningNumbers = new WinningNumbers(lottoNumbersDto1); From 9518660585cb6409a1fc28639f881fcfa22e04bd Mon Sep 17 00:00:00 2001 From: KimSeongGyu1 Date: Wed, 19 Feb 2020 15:33:33 +0900 Subject: [PATCH 12/23] =?UTF-8?q?[feature]=20=EA=B3=A0=EC=A0=95=EA=B0=92?= =?UTF-8?q?=EC=9C=BC=EB=A1=9C=20=EB=8B=B9=EC=B2=A8=EB=B2=88=ED=98=B8=20?= =?UTF-8?q?=EC=83=9D=EC=84=B1=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...omFactory.java => LottoNumberFactory.java} | 16 +++++----- .../factory/LottoNumbersDtoFactory.java | 7 ----- .../numberscontainer/LottoNumbersDto.java | 2 +- .../factory/LottoNumbersDtoFactoryTest.java | 30 +++++++++++++++++-- 4 files changed, 36 insertions(+), 19 deletions(-) rename src/main/java/domain/factory/{RandomFactory.java => LottoNumberFactory.java} (72%) delete mode 100644 src/main/java/domain/factory/LottoNumbersDtoFactory.java diff --git a/src/main/java/domain/factory/RandomFactory.java b/src/main/java/domain/factory/LottoNumberFactory.java similarity index 72% rename from src/main/java/domain/factory/RandomFactory.java rename to src/main/java/domain/factory/LottoNumberFactory.java index 11bbb0fa27..5ea5527e20 100644 --- a/src/main/java/domain/factory/RandomFactory.java +++ b/src/main/java/domain/factory/LottoNumberFactory.java @@ -5,22 +5,16 @@ import java.util.*; -public class RandomFactory implements LottoNumbersDtoFactory{ +public class LottoNumberFactory { private static final int FIRST_INDEX = 0; private static final int SIXTH_INDEX = 6; private static final int EXCEPT_ERROR_FIRST_INDEX = 1; - @Override - public LottoNumbersDto generate(boolean containsBonus) { + public LottoNumbersDto generateRandomTicketDto() { List lottoNumbers = getShuffledList(); - Set sixNumbers = new HashSet<>(lottoNumbers.subList(FIRST_INDEX, SIXTH_INDEX)); - LottoNumber bonus = lottoNumbers.get(SIXTH_INDEX); - if (containsBonus) { - return new LottoNumbersDto(sixNumbers, bonus); - } return new LottoNumbersDto(sixNumbers); } @@ -29,4 +23,8 @@ private List getShuffledList() { Collections.shuffle(lottoNumbers); return lottoNumbers; } -} + + public LottoNumbersDto generateFixedNumber(Set sixNumbers, LottoNumber bonusNumber) { + return new LottoNumbersDto(sixNumbers, bonusNumber); + } +} \ No newline at end of file diff --git a/src/main/java/domain/factory/LottoNumbersDtoFactory.java b/src/main/java/domain/factory/LottoNumbersDtoFactory.java deleted file mode 100644 index e83b90613e..0000000000 --- a/src/main/java/domain/factory/LottoNumbersDtoFactory.java +++ /dev/null @@ -1,7 +0,0 @@ -package domain.factory; - -import domain.numberscontainer.LottoNumbersDto; - -public interface LottoNumbersDtoFactory { - LottoNumbersDto generate(boolean containsBonus); -} diff --git a/src/main/java/domain/numberscontainer/LottoNumbersDto.java b/src/main/java/domain/numberscontainer/LottoNumbersDto.java index d0d67ea22c..62fa79cd68 100644 --- a/src/main/java/domain/numberscontainer/LottoNumbersDto.java +++ b/src/main/java/domain/numberscontainer/LottoNumbersDto.java @@ -24,4 +24,4 @@ public Set getSixNumbers() { public LottoNumber getBonusNumber() { return bonusNumber; } -} +} \ No newline at end of file diff --git a/src/test/java/domain/factory/LottoNumbersDtoFactoryTest.java b/src/test/java/domain/factory/LottoNumbersDtoFactoryTest.java index 8cc0f371be..5b2683b7ec 100644 --- a/src/test/java/domain/factory/LottoNumbersDtoFactoryTest.java +++ b/src/test/java/domain/factory/LottoNumbersDtoFactoryTest.java @@ -1,9 +1,16 @@ package domain.factory; +import domain.LottoNumber; +import domain.numberscontainer.LottoNumbersDto; import domain.numberscontainer.Ticket; +import domain.numberscontainer.WinningNumbers; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + import static org.assertj.core.api.Assertions.assertThatCode; class LottoNumbersDtoFactoryTest { @@ -11,8 +18,27 @@ class LottoNumbersDtoFactoryTest { @Test @DisplayName("랜덤 티켓 생성") void generateRandomTicket() { - RandomFactory randomFactory = new RandomFactory(); - assertThatCode(() -> new Ticket(randomFactory.generate(false))) + LottoNumberFactory lottoNumberFactory = new LottoNumberFactory(); + assertThatCode(() -> new Ticket(lottoNumberFactory.generateRandomTicketDto())) + .doesNotThrowAnyException(); + } + + @Test + @DisplayName("고정값 당첨 번호 생성") + void generateFixedWinningNumber() { + LottoNumberFactory lottoNumberFactory = new LottoNumberFactory(); + assertThatCode(() -> new WinningNumbers(lottoNumberFactory.generateFixedNumber(createSixNumbers(1, 2, 3, 4, 5, 6), LottoNumber.SEVEN))) .doesNotThrowAnyException(); } + + private Set createSixNumbers(int number1, int number2, int number3, int number4, int number5, int number6) { + Set sixNumbers = new HashSet<>(Arrays.asList(LottoNumber.getLottoNumber(number1), + LottoNumber.getLottoNumber(number2), + LottoNumber.getLottoNumber(number3), + LottoNumber.getLottoNumber(number4), + LottoNumber.getLottoNumber(number5), + LottoNumber.getLottoNumber(number6))); + + return sixNumbers; + } } \ No newline at end of file From 44404dcf8253cce1a659fe7312888717e34c2aaa Mon Sep 17 00:00:00 2001 From: KimSeongGyu1 Date: Wed, 19 Feb 2020 15:55:55 +0900 Subject: [PATCH 13/23] =?UTF-8?q?[feature]=20InputView=20=EB=8B=B9?= =?UTF-8?q?=EC=B2=A8=EB=B2=88=ED=98=B8=EC=99=80=20=EB=B3=B4=EB=84=88?= =?UTF-8?q?=EC=8A=A4=EB=B3=BC=20=EC=9E=85=EB=A0=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/LottoApplication.java | 26 ++++++++++++++++++++++++ src/main/java/view/InputView.java | 31 ++++++++++++++++++++++++++--- 2 files changed, 54 insertions(+), 3 deletions(-) diff --git a/src/main/java/LottoApplication.java b/src/main/java/LottoApplication.java index 24d9faaefd..f4629151ff 100644 --- a/src/main/java/LottoApplication.java +++ b/src/main/java/LottoApplication.java @@ -1,10 +1,16 @@ +import domain.LottoNumber; import domain.Money; import view.InputView; +import java.util.Set; + public class LottoApplication { public static void main(String[] args) { Money money = enterMoney(); + Set lastWeekWinningNumbers = enterLastWeekWinningNumber(); + LottoNumber bonusNumber = enterBonusNumber(); + } private static Money enterMoney() { @@ -16,4 +22,24 @@ private static Money enterMoney() { } } } + + private static Set enterLastWeekWinningNumber() { + while (true) { + try { + return InputView.enterLastWeekWinningNumbers(); + } catch (Exception e) { + System.out.println(e.getMessage()); + } + } + } + + private static LottoNumber enterBonusNumber() { + while (true) { + try { + return InputView.enterBonusNumber(); + } catch (Exception e) { + System.out.println(e.getMessage()); + } + } + } } \ No newline at end of file diff --git a/src/main/java/view/InputView.java b/src/main/java/view/InputView.java index 0ea277d7bb..b9a322aa6b 100644 --- a/src/main/java/view/InputView.java +++ b/src/main/java/view/InputView.java @@ -1,8 +1,10 @@ package view; +import domain.LottoNumber; import domain.Money; -import java.util.Scanner; +import java.util.*; +import java.util.stream.Collectors; public class InputView { private static final String NUMBER_REGEX = "^[0-9]+$"; @@ -11,11 +13,11 @@ public class InputView { public static Money enterMoney() { System.out.println("구입 금액을 입력해주세요."); - int money = parseMoney(scanner.nextLine()); + int money = parseInt(scanner.nextLine()); return new Money(money); } - private static int parseMoney(String input) { + private static int parseInt(String input) { validateNumber(input); return Integer.parseInt(input); } @@ -25,4 +27,27 @@ private static void validateNumber(String input) { throw new NumberFormatException("숫자를 입력해주세요."); } } + + public static Set enterLastWeekWinningNumbers() { + System.out.println("지난 주 당첨 번호를 입력해 주세요."); + Set lastWeekWinningNumbers = parseWinningNumbers(scanner.nextLine()); + return lastWeekWinningNumbers; + } + + private static Set parseWinningNumbers(String input) { + List lottoNumbers = Arrays.asList(input.split(",")).stream() + .map(InputView::parseLottoNumber) + .collect(Collectors.toList()); + return new HashSet<>(lottoNumbers); + } + + public static LottoNumber enterBonusNumber() { + System.out.println("보너스 볼을 입력해 주세요."); + return parseLottoNumber(scanner.nextLine()); + } + + private static LottoNumber parseLottoNumber(String input) { + int parsedInt = parseInt(input); + return LottoNumber.getLottoNumber(parsedInt); + } } From 164be4e2cdab4cca9ec55e55f1914f7f4354ca55 Mon Sep 17 00:00:00 2001 From: KimSeongGyu1 Date: Wed, 19 Feb 2020 17:51:54 +0900 Subject: [PATCH 14/23] =?UTF-8?q?[feature]=20OutputView=20=ED=8B=B0?= =?UTF-8?q?=EC=BC=93=20=EA=B5=AC=EB=A7=A4=20=EC=B6=9C=EB=A0=A5=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/LottoApplication.java | 34 +++++++++++++++++-- src/main/java/domain/LottoNumber.java | 4 +++ src/main/java/domain/Money.java | 4 +++ .../domain/factory/LottoNumberFactory.java | 6 ++-- .../LottoNumbersContainer.java | 4 +-- .../java/domain/numberscontainer/Ticket.java | 17 +++++++++- .../numberscontainer/WinningNumbers.java | 4 +-- src/main/java/view/OutputView.java | 22 ++++++++++++ .../factory/LottoNumbersDtoFactoryTest.java | 3 +- 9 files changed, 86 insertions(+), 12 deletions(-) create mode 100644 src/main/java/view/OutputView.java diff --git a/src/main/java/LottoApplication.java b/src/main/java/LottoApplication.java index f4629151ff..9dd16eb98a 100644 --- a/src/main/java/LottoApplication.java +++ b/src/main/java/LottoApplication.java @@ -1,16 +1,46 @@ import domain.LottoNumber; import domain.Money; +import domain.factory.LottoNumberFactory; +import domain.numberscontainer.LottoNumbersDto; +import domain.numberscontainer.Ticket; +import domain.numberscontainer.WinningNumbers; import view.InputView; +import view.OutputView; +import java.rmi.ServerError; +import java.util.ArrayList; +import java.util.List; import java.util.Set; public class LottoApplication { public static void main(String[] args) { Money money = enterMoney(); - Set lastWeekWinningNumbers = enterLastWeekWinningNumber(); - LottoNumber bonusNumber = enterBonusNumber(); + OutputView.printNumberOfTickets(money); + int ticketSize = money.getNumberOfTickets(); + List tickets = new ArrayList<>(); + for (int i = 0; i < ticketSize; i++) { + LottoNumbersDto lottoNumbersDto = LottoNumberFactory.generateRandomTicketDto(); + tickets.add(new Ticket(lottoNumbersDto)); + } + OutputView.printTickets(tickets); + + WinningNumbers winningNumbers = enterWinningNumbers(); + + } + + private static WinningNumbers enterWinningNumbers() { + while (true) { + try { + Set lastWeekWinningNumbers = enterLastWeekWinningNumber(); + LottoNumber bonusNumber = enterBonusNumber(); + LottoNumbersDto lottoNumbersDto = LottoNumberFactory.generateFixedNumberDto(lastWeekWinningNumbers, bonusNumber); + return new WinningNumbers(lottoNumbersDto); + } catch (IllegalArgumentException e) { + System.out.println(e.getMessage()); + } + } } private static Money enterMoney() { diff --git a/src/main/java/domain/LottoNumber.java b/src/main/java/domain/LottoNumber.java index 5bf015420d..ff8b81db5f 100644 --- a/src/main/java/domain/LottoNumber.java +++ b/src/main/java/domain/LottoNumber.java @@ -68,6 +68,10 @@ public static LottoNumber getLottoNumber(int number) { .findFirst() .orElse(LottoNumber.ERROR); } + + public int getValue() { + return this.number; + } // // private static void validateNumber(int number) { // if (number < MIN_LOTTO_NUMBER || number > MAX_LOTTO_NUMBER) { diff --git a/src/main/java/domain/Money.java b/src/main/java/domain/Money.java index c365e6e763..bfb675d886 100644 --- a/src/main/java/domain/Money.java +++ b/src/main/java/domain/Money.java @@ -13,6 +13,10 @@ private void validateMoney(int money) { validateMoneyUnit(money); } + public int getNumberOfTickets() { + return this.money / 1000; + } + private void validateMoneyRange(int money) { if (money < 0 || money > 50000) { throw new IllegalArgumentException("0원 이상, 5만원 이하 금액만 구매 가능합니다."); diff --git a/src/main/java/domain/factory/LottoNumberFactory.java b/src/main/java/domain/factory/LottoNumberFactory.java index 5ea5527e20..5727e743cb 100644 --- a/src/main/java/domain/factory/LottoNumberFactory.java +++ b/src/main/java/domain/factory/LottoNumberFactory.java @@ -11,20 +11,20 @@ public class LottoNumberFactory { private static final int SIXTH_INDEX = 6; private static final int EXCEPT_ERROR_FIRST_INDEX = 1; - public LottoNumbersDto generateRandomTicketDto() { + public static LottoNumbersDto generateRandomTicketDto() { List lottoNumbers = getShuffledList(); Set sixNumbers = new HashSet<>(lottoNumbers.subList(FIRST_INDEX, SIXTH_INDEX)); return new LottoNumbersDto(sixNumbers); } - private List getShuffledList() { + private static List getShuffledList() { List lottoNumbers = Arrays.asList(LottoNumber.values()).subList(EXCEPT_ERROR_FIRST_INDEX, LottoNumber.values().length); Collections.shuffle(lottoNumbers); return lottoNumbers; } - public LottoNumbersDto generateFixedNumber(Set sixNumbers, LottoNumber bonusNumber) { + public static LottoNumbersDto generateFixedNumberDto(Set sixNumbers, LottoNumber bonusNumber) { return new LottoNumbersDto(sixNumbers, bonusNumber); } } \ No newline at end of file diff --git a/src/main/java/domain/numberscontainer/LottoNumbersContainer.java b/src/main/java/domain/numberscontainer/LottoNumbersContainer.java index 9f8d6eff25..2c0ac042c3 100644 --- a/src/main/java/domain/numberscontainer/LottoNumbersContainer.java +++ b/src/main/java/domain/numberscontainer/LottoNumbersContainer.java @@ -7,11 +7,11 @@ abstract class LottoNumbersContainer { private static final int TICKET_SIZE = 6; - protected final Set lottoSixNumbers; + protected final Set sixLottoNumbers; public LottoNumbersContainer(LottoNumbersDto lottoNumbersDto) { validateSize(lottoNumbersDto.getSixNumbers()); - this.lottoSixNumbers = lottoNumbersDto.getSixNumbers(); + this.sixLottoNumbers = lottoNumbersDto.getSixNumbers(); } protected void validateSize(Set lottoNumbers) { diff --git a/src/main/java/domain/numberscontainer/Ticket.java b/src/main/java/domain/numberscontainer/Ticket.java index e3435bd76e..f32753157f 100644 --- a/src/main/java/domain/numberscontainer/Ticket.java +++ b/src/main/java/domain/numberscontainer/Ticket.java @@ -2,12 +2,27 @@ import domain.LottoNumber; +import java.util.stream.Collectors; + public class Ticket extends LottoNumbersContainer { public Ticket(LottoNumbersDto lottoNumbersDto) { super(lottoNumbersDto); } public boolean contains(LottoNumber number) { - return this.lottoSixNumbers.contains(number); + return this.sixLottoNumbers.contains(number); + } + + @Override + public String toString() { + StringBuilder stringBuilder = new StringBuilder(); + String numbers = this.sixLottoNumbers.stream() + .map(lottoNumber -> Integer.toString(lottoNumber.getValue())) + .collect(Collectors.joining(", ")); + + stringBuilder.append("["); + stringBuilder.append(numbers); + stringBuilder.append("]"); + return stringBuilder.toString(); } } \ No newline at end of file diff --git a/src/main/java/domain/numberscontainer/WinningNumbers.java b/src/main/java/domain/numberscontainer/WinningNumbers.java index d746d13cd1..0c0c07e706 100644 --- a/src/main/java/domain/numberscontainer/WinningNumbers.java +++ b/src/main/java/domain/numberscontainer/WinningNumbers.java @@ -24,13 +24,13 @@ private void validateErrorBonusNumber(LottoNumber bonusNumber) { } private void validateDuplicatedBonusNumber(LottoNumber bonusNumber) { - if (this.lottoSixNumbers.contains(bonusNumber)) { + if (this.sixLottoNumbers.contains(bonusNumber)) { throw new IllegalArgumentException("당첨 번호와 중복되지 않는 보너스 번호를 입력해주세요."); } } public int findDuplicatedNumbers(Ticket ticket) { - return this.lottoSixNumbers.stream() + return this.sixLottoNumbers.stream() .filter(number -> ticket.contains(number)) .mapToInt(number -> 1) .sum(); diff --git a/src/main/java/view/OutputView.java b/src/main/java/view/OutputView.java new file mode 100644 index 0000000000..8841b634a9 --- /dev/null +++ b/src/main/java/view/OutputView.java @@ -0,0 +1,22 @@ +package view; + +import domain.Money; +import domain.numberscontainer.Ticket; + +import java.util.List; + +public class OutputView { + + public static void printNumberOfTickets(Money money) { + System.out.println(money.getNumberOfTickets() + "개를 구매했습니다."); + } + + public static void printTickets(List tickets) { + StringBuilder stringBuilder = new StringBuilder(); + for (Ticket ticket : tickets) { + stringBuilder.append(ticket.toString()); + stringBuilder.append("\n"); + } + System.out.println(stringBuilder.toString()); + } +} \ No newline at end of file diff --git a/src/test/java/domain/factory/LottoNumbersDtoFactoryTest.java b/src/test/java/domain/factory/LottoNumbersDtoFactoryTest.java index 5b2683b7ec..3c28d53000 100644 --- a/src/test/java/domain/factory/LottoNumbersDtoFactoryTest.java +++ b/src/test/java/domain/factory/LottoNumbersDtoFactoryTest.java @@ -1,7 +1,6 @@ package domain.factory; import domain.LottoNumber; -import domain.numberscontainer.LottoNumbersDto; import domain.numberscontainer.Ticket; import domain.numberscontainer.WinningNumbers; import org.junit.jupiter.api.DisplayName; @@ -27,7 +26,7 @@ void generateRandomTicket() { @DisplayName("고정값 당첨 번호 생성") void generateFixedWinningNumber() { LottoNumberFactory lottoNumberFactory = new LottoNumberFactory(); - assertThatCode(() -> new WinningNumbers(lottoNumberFactory.generateFixedNumber(createSixNumbers(1, 2, 3, 4, 5, 6), LottoNumber.SEVEN))) + assertThatCode(() -> new WinningNumbers(lottoNumberFactory.generateFixedNumberDto(createSixNumbers(1, 2, 3, 4, 5, 6), LottoNumber.SEVEN))) .doesNotThrowAnyException(); } From 50ebf5a3c25fd8a2a336dea85927f0fc1a98c527 Mon Sep 17 00:00:00 2001 From: KimSeongGyu1 Date: Wed, 19 Feb 2020 20:47:47 +0900 Subject: [PATCH 15/23] =?UTF-8?q?[feature]=20=EB=A1=9C=EB=98=90=20?= =?UTF-8?q?=EB=8B=B9=EC=B2=A8=20=EA=B2=B0=EA=B3=BC=20=EC=B0=BE=EA=B8=B0=20?= =?UTF-8?q?=EB=A9=94=EC=84=9C=EB=93=9C=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/LottoApplication.java | 1 - src/main/java/domain/LottoResult.java | 36 +++++++++++++++++++ .../java/domain/numberscontainer/Ticket.java | 4 +-- .../numberscontainer/WinningNumbers.java | 8 +++++ .../numberscontainer/WinningNumbersTest.java | 36 +++++++++++++++---- 5 files changed, 75 insertions(+), 10 deletions(-) create mode 100644 src/main/java/domain/LottoResult.java diff --git a/src/main/java/LottoApplication.java b/src/main/java/LottoApplication.java index 9dd16eb98a..138a648042 100644 --- a/src/main/java/LottoApplication.java +++ b/src/main/java/LottoApplication.java @@ -26,7 +26,6 @@ public static void main(String[] args) { OutputView.printTickets(tickets); WinningNumbers winningNumbers = enterWinningNumbers(); - } private static WinningNumbers enterWinningNumbers() { diff --git a/src/main/java/domain/LottoResult.java b/src/main/java/domain/LottoResult.java new file mode 100644 index 0000000000..2b65517c69 --- /dev/null +++ b/src/main/java/domain/LottoResult.java @@ -0,0 +1,36 @@ +package domain; + +import java.util.Arrays; +import java.util.List; + +public enum LottoResult { + NO_WIN(-1, -1, false), + FIRST(2000000000, 6, false), + SECOND(30000000, 5, false), + THIRD(1500000, 5, false), + FOURTH(50000, 4, true), + FIFTH(5000, 3, false); + + private int prize; + private int matchingNumbers; + private boolean isBonus; + + LottoResult(int prize, int matchingNumbers, boolean isBonus) { + this.prize = prize; + this.matchingNumbers = matchingNumbers; + this.isBonus = isBonus; + } + + public static LottoResult findLottoResult(int matchingNumbers, boolean isBonus) { + List lottoResults = Arrays.asList(LottoResult.values()); + LottoResult lottoResult = lottoResults.stream() + .filter(result -> result.matchingNumbers == matchingNumbers) + .findFirst() + .orElse(NO_WIN); + + if (lottoResult == SECOND && !isBonus) { + lottoResult = THIRD; + } + return lottoResult; + } +} \ No newline at end of file diff --git a/src/main/java/domain/numberscontainer/Ticket.java b/src/main/java/domain/numberscontainer/Ticket.java index f32753157f..d6b740653f 100644 --- a/src/main/java/domain/numberscontainer/Ticket.java +++ b/src/main/java/domain/numberscontainer/Ticket.java @@ -18,11 +18,9 @@ public String toString() { StringBuilder stringBuilder = new StringBuilder(); String numbers = this.sixLottoNumbers.stream() .map(lottoNumber -> Integer.toString(lottoNumber.getValue())) - .collect(Collectors.joining(", ")); + .collect(Collectors.joining(", ", "[", "]")); - stringBuilder.append("["); stringBuilder.append(numbers); - stringBuilder.append("]"); return stringBuilder.toString(); } } \ No newline at end of file diff --git a/src/main/java/domain/numberscontainer/WinningNumbers.java b/src/main/java/domain/numberscontainer/WinningNumbers.java index 0c0c07e706..d3f59966c9 100644 --- a/src/main/java/domain/numberscontainer/WinningNumbers.java +++ b/src/main/java/domain/numberscontainer/WinningNumbers.java @@ -1,6 +1,7 @@ package domain.numberscontainer; import domain.LottoNumber; +import domain.LottoResult; public class WinningNumbers extends LottoNumbersContainer { @@ -35,4 +36,11 @@ public int findDuplicatedNumbers(Ticket ticket) { .mapToInt(number -> 1) .sum(); } + + public LottoResult getLottoResult(Ticket ticket) { + int matchingNumber = findDuplicatedNumbers(ticket); + boolean isBonus = ticket.contains(bonusNumber); + return LottoResult.findLottoResult(matchingNumber, isBonus); + //return LottoResult.FIRST; + } } \ No newline at end of file diff --git a/src/test/java/domain/numberscontainer/WinningNumbersTest.java b/src/test/java/domain/numberscontainer/WinningNumbersTest.java index 0c21f8b49e..6b75d7c7ac 100644 --- a/src/test/java/domain/numberscontainer/WinningNumbersTest.java +++ b/src/test/java/domain/numberscontainer/WinningNumbersTest.java @@ -1,17 +1,14 @@ package domain.numberscontainer; import domain.LottoNumber; -import domain.numberscontainer.LottoNumbersDto; -import domain.numberscontainer.Ticket; -import domain.numberscontainer.WinningNumbers; +import domain.LottoResult; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; -import java.util.Arrays; -import java.util.HashSet; -import java.util.Set; +import java.util.*; +import java.util.stream.Collectors; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; @@ -58,4 +55,31 @@ private LottoNumbersDto createLottoNumberDto(int number1, int number2, int numbe return new LottoNumbersDto(sixNumbers, bonusNumber); } + + @Test + @DisplayName("당첨 결과 확인") + void getLottoResultTest() { + List tickets = Arrays.asList( + new Ticket(createLottoNumberDto(3, 4, 5, 6, 7, 8, -1)), // 1등 + new Ticket(createLottoNumberDto(3, 4, 5, 6, 7, 9, -1)), // 2등 + new Ticket(createLottoNumberDto(3, 4, 5, 6, 7, 10, -1)), // 3등 + new Ticket(createLottoNumberDto(3, 4, 5, 6, 10, 11, -1)), // 4등 + new Ticket(createLottoNumberDto(3, 4, 5, 10, 11, 12, -1)), // 5등 + new Ticket(createLottoNumberDto(4, 5, 6, 7, 8, 9, -1)), // 2등 + new Ticket(createLottoNumberDto(4, 5, 6, 7, 8, 10, -1)), // 3등 + new Ticket(createLottoNumberDto(4, 5, 6, 7, 10, 11, -1)), // 4등 + new Ticket(createLottoNumberDto(10, 11, 12, 13, 14, 15, -1))); // 당첨X + + WinningNumbers winningNumbers = new WinningNumbers(createLottoNumberDto(3, 4, 5, 6, 7, 8, 9)); + + Map map = tickets.stream() + .collect(Collectors.groupingBy(ticket -> winningNumbers.getLottoResult(ticket), Collectors.counting())); + + assertThat(map.get(LottoResult.FIRST)).isEqualTo(1); + assertThat(map.get(LottoResult.SECOND)).isEqualTo(2); + assertThat(map.get(LottoResult.THIRD)).isEqualTo(2); + assertThat(map.get(LottoResult.FOURTH)).isEqualTo(2); + assertThat(map.get(LottoResult.FIFTH)).isEqualTo(1); + assertThat(map.get(LottoResult.NO_WIN)).isEqualTo(1); + } } \ No newline at end of file From 89b3bd80dc421faa9e54edfa5b3e6000031ad378 Mon Sep 17 00:00:00 2001 From: KimSeongGyu1 Date: Thu, 20 Feb 2020 16:24:04 +0900 Subject: [PATCH 16/23] =?UTF-8?q?[refactor=20/=20feature]=20=EA=B5=AC?= =?UTF-8?q?=EC=A1=B0=20=EB=A6=AC=ED=8C=A9=ED=86=A0=EB=A7=81=20=EB=B0=8F=20?= =?UTF-8?q?=EB=A1=9C=EB=98=90=20=EB=8B=B9=EC=B2=A8=20=EA=B2=B0=EA=B3=BC=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/LottoApplication.java | 73 +++++-------------- src/main/java/domain/LottoNumber.java | 9 --- src/main/java/domain/LottoProfit.java | 24 ++++++ src/main/java/domain/LottoResult.java | 20 +++++ src/main/java/domain/LottoStore.java | 36 +++++++++ src/main/java/domain/Money.java | 8 +- src/main/java/domain/User.java | 46 ++++++++++++ .../numberscontainer/WinningNumbers.java | 1 - .../LottoNumbersDtoGenerator.java} | 12 ++- src/main/java/view/InputView.java | 26 ++++--- src/main/java/view/OutputView.java | 30 +++++++- src/test/java/domain/LottoNumberTest.java | 5 +- src/test/java/domain/LottoStoreTest.java | 15 ++++ src/test/java/domain/UserTest.java | 62 ++++++++++++++++ .../factory/LottoNumbersDtoFactoryTest.java | 43 ----------- .../numberscontainer/WinningNumbersTest.java | 31 +++----- .../util/LottoNumbersDtoGeneratorTest.java | 36 +++++++++ 17 files changed, 325 insertions(+), 152 deletions(-) create mode 100644 src/main/java/domain/LottoProfit.java create mode 100644 src/main/java/domain/LottoStore.java create mode 100644 src/main/java/domain/User.java rename src/main/java/{domain/factory/LottoNumberFactory.java => util/LottoNumbersDtoGenerator.java} (64%) create mode 100644 src/test/java/domain/LottoStoreTest.java create mode 100644 src/test/java/domain/UserTest.java delete mode 100644 src/test/java/domain/factory/LottoNumbersDtoFactoryTest.java create mode 100644 src/test/java/util/LottoNumbersDtoGeneratorTest.java diff --git a/src/main/java/LottoApplication.java b/src/main/java/LottoApplication.java index 138a648042..6b39ca24b2 100644 --- a/src/main/java/LottoApplication.java +++ b/src/main/java/LottoApplication.java @@ -1,74 +1,41 @@ -import domain.LottoNumber; import domain.Money; -import domain.factory.LottoNumberFactory; +import domain.User; import domain.numberscontainer.LottoNumbersDto; -import domain.numberscontainer.Ticket; import domain.numberscontainer.WinningNumbers; import view.InputView; import view.OutputView; -import java.rmi.ServerError; -import java.util.ArrayList; -import java.util.List; -import java.util.Set; - public class LottoApplication { public static void main(String[] args) { - Money money = enterMoney(); - OutputView.printNumberOfTickets(money); - int ticketSize = money.getNumberOfTickets(); - List tickets = new ArrayList<>(); - for (int i = 0; i < ticketSize; i++) { - LottoNumbersDto lottoNumbersDto = LottoNumberFactory.generateRandomTicketDto(); - tickets.add(new Ticket(lottoNumbersDto)); - } - OutputView.printTickets(tickets); - - WinningNumbers winningNumbers = enterWinningNumbers(); - } + User user = new User(); + user.buyTickets(enterMoney()); - private static WinningNumbers enterWinningNumbers() { - while (true) { - try { - Set lastWeekWinningNumbers = enterLastWeekWinningNumber(); - LottoNumber bonusNumber = enterBonusNumber(); + OutputView.printNumberOfTickets(user.getTickets().size()); + OutputView.printTickets(user.getTickets()); - LottoNumbersDto lottoNumbersDto = LottoNumberFactory.generateFixedNumberDto(lastWeekWinningNumbers, bonusNumber); - return new WinningNumbers(lottoNumbersDto); - } catch (IllegalArgumentException e) { - System.out.println(e.getMessage()); - } - } + WinningNumbers winningNumbers = enterWinningNumbers(); + OutputView.printLottoResults(user.confirmResult(winningNumbers)); + OutputView.printProfit(user.calculateProfit()); } private static Money enterMoney() { - while (true) { - try { - return InputView.enterMoney(); - } catch (Exception e) { - System.out.println(e.getMessage()); - } + try { + return InputView.enterMoney(); + } catch (Exception e) { + System.out.println(e.getMessage()); + return enterMoney(); } - } - private static Set enterLastWeekWinningNumber() { - while (true) { - try { - return InputView.enterLastWeekWinningNumbers(); - } catch (Exception e) { - System.out.println(e.getMessage()); - } - } } - private static LottoNumber enterBonusNumber() { - while (true) { - try { - return InputView.enterBonusNumber(); - } catch (Exception e) { - System.out.println(e.getMessage()); - } + private static WinningNumbers enterWinningNumbers() { + try { + LottoNumbersDto lottoNumbersDto = InputView.enterWinningNumbers(); + return new WinningNumbers(lottoNumbersDto); + } catch (IllegalArgumentException e) { + System.out.println(e.getMessage()); + return enterWinningNumbers(); } } } \ No newline at end of file diff --git a/src/main/java/domain/LottoNumber.java b/src/main/java/domain/LottoNumber.java index ff8b81db5f..880a50994f 100644 --- a/src/main/java/domain/LottoNumber.java +++ b/src/main/java/domain/LottoNumber.java @@ -51,9 +51,6 @@ public enum LottoNumber { FORTY_FOUR(44), FORTY_FIVE(45); -// private static final int MIN_LOTTO_NUMBER = 1; -// private static final int MAX_LOTTO_NUMBER = 45; - private final int number; LottoNumber(int number) { @@ -72,10 +69,4 @@ public static LottoNumber getLottoNumber(int number) { public int getValue() { return this.number; } -// -// private static void validateNumber(int number) { -// if (number < MIN_LOTTO_NUMBER || number > MAX_LOTTO_NUMBER) { -// throw new IllegalArgumentException("1부터 45사이의 숫자를 입력해주세요."); -// } -// } } diff --git a/src/main/java/domain/LottoProfit.java b/src/main/java/domain/LottoProfit.java new file mode 100644 index 0000000000..848cbc6400 --- /dev/null +++ b/src/main/java/domain/LottoProfit.java @@ -0,0 +1,24 @@ +package domain; + +import java.util.Map; + +public class LottoProfit { + + private double profit; + + public LottoProfit(double profit) { + this.profit = profit; + } + + public static LottoProfit getProfit(Map lottoResults, Money money) { + double totalPrize = lottoResults.keySet().stream() + .filter(object -> lottoResults.get(object) != null) + .mapToDouble(object -> object.getPrize() * lottoResults.get(object)) + .sum(); + return new LottoProfit(totalPrize / money.getMoney() * 100); + } + + public double getProfit() { + return profit; + } +} diff --git a/src/main/java/domain/LottoResult.java b/src/main/java/domain/LottoResult.java index 2b65517c69..2213a5fc5d 100644 --- a/src/main/java/domain/LottoResult.java +++ b/src/main/java/domain/LottoResult.java @@ -1,5 +1,6 @@ package domain; +import java.text.DecimalFormat; import java.util.Arrays; import java.util.List; @@ -33,4 +34,23 @@ public static LottoResult findLottoResult(int matchingNumbers, boolean isBonus) } return lottoResult; } + + public double getPrize() { + return prize; + } + + public String toString() { + if (this == NO_WIN) return ""; + StringBuilder stringBuilder = new StringBuilder(); + stringBuilder.append(this.matchingNumbers); + stringBuilder.append("개 일치 "); + if (this == SECOND) { + stringBuilder.append(", 보너스 볼 일치 "); + } + stringBuilder.append("("); + stringBuilder.append(this.prize); + stringBuilder.append("원) - "); + + return stringBuilder.toString(); + } } \ No newline at end of file diff --git a/src/main/java/domain/LottoStore.java b/src/main/java/domain/LottoStore.java new file mode 100644 index 0000000000..7fb4fd8588 --- /dev/null +++ b/src/main/java/domain/LottoStore.java @@ -0,0 +1,36 @@ +package domain; + +import domain.numberscontainer.Ticket; +import util.LottoNumbersDtoGenerator; + +import java.util.ArrayList; +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; + +public class LottoStore { + public static List generateTickets(int number) { + List tickets = new ArrayList<>(); + for (int i = 0; i < number; i++) { + tickets.add(new Ticket(LottoNumbersDtoGenerator.generateRandomTicketDto())); + } + return tickets; + } + + public static List generateTickets(int number, List> myNumbers) { + List tickets = myNumbers.stream() + .map(numbers -> LottoNumbersDtoGenerator.generateFixedNumberDto(numbers, -1)) + .map(dto -> new Ticket(dto)) + .collect(Collectors.toList()); + + int randomTicketsNumber = getRandomTicketsNumber(number, myNumbers); + for (int i = 0; i < randomTicketsNumber; i++) { + tickets.add(new Ticket(LottoNumbersDtoGenerator.generateRandomTicketDto())); + } + return tickets; + } + + private static int getRandomTicketsNumber(int number, List> myNumbers) { + return number - myNumbers.size(); + } +} diff --git a/src/main/java/domain/Money.java b/src/main/java/domain/Money.java index bfb675d886..e76b272910 100644 --- a/src/main/java/domain/Money.java +++ b/src/main/java/domain/Money.java @@ -17,15 +17,19 @@ public int getNumberOfTickets() { return this.money / 1000; } - private void validateMoneyRange(int money) { + private void validateMoneyRange(long money) { if (money < 0 || money > 50000) { throw new IllegalArgumentException("0원 이상, 5만원 이하 금액만 구매 가능합니다."); } } - private void validateMoneyUnit(int money) { + private void validateMoneyUnit(long money) { if (money % 1000 != 0) { throw new IllegalArgumentException("천 원 단위로만 구매 가능합니다."); } } + + public long getMoney() { + return this.money; + } } diff --git a/src/main/java/domain/User.java b/src/main/java/domain/User.java new file mode 100644 index 0000000000..df0e3dd343 --- /dev/null +++ b/src/main/java/domain/User.java @@ -0,0 +1,46 @@ +package domain; + +import domain.numberscontainer.Ticket; +import domain.numberscontainer.WinningNumbers; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; + +public class User { + + private Money spentMoney; + private List tickets; + private Map lottoResults; + + public User() { + this.spentMoney = new Money(0); + this.tickets = new ArrayList<>(); + } + + public void buyTickets(Money money) { + this.spentMoney = money; + this.tickets = LottoStore.generateTickets(money.getNumberOfTickets()); + } + + public void buyTicketsManually(Money money, List> myNumbers) { + this.spentMoney = money; + this.tickets = LottoStore.generateTickets(money.getNumberOfTickets(), myNumbers); + } + + public Map confirmResult(WinningNumbers winningNumbers) { + this.lottoResults = tickets.stream() + .collect(Collectors.groupingBy(ticket -> winningNumbers.getLottoResult(ticket), Collectors.counting())); + return lottoResults; + } + + public LottoProfit calculateProfit() { + return LottoProfit.getProfit(this.lottoResults, this.spentMoney); + } + + public List getTickets() { + return tickets; + } +} \ No newline at end of file diff --git a/src/main/java/domain/numberscontainer/WinningNumbers.java b/src/main/java/domain/numberscontainer/WinningNumbers.java index d3f59966c9..d299fb153c 100644 --- a/src/main/java/domain/numberscontainer/WinningNumbers.java +++ b/src/main/java/domain/numberscontainer/WinningNumbers.java @@ -41,6 +41,5 @@ public LottoResult getLottoResult(Ticket ticket) { int matchingNumber = findDuplicatedNumbers(ticket); boolean isBonus = ticket.contains(bonusNumber); return LottoResult.findLottoResult(matchingNumber, isBonus); - //return LottoResult.FIRST; } } \ No newline at end of file diff --git a/src/main/java/domain/factory/LottoNumberFactory.java b/src/main/java/util/LottoNumbersDtoGenerator.java similarity index 64% rename from src/main/java/domain/factory/LottoNumberFactory.java rename to src/main/java/util/LottoNumbersDtoGenerator.java index 5727e743cb..ffeeb1e444 100644 --- a/src/main/java/domain/factory/LottoNumberFactory.java +++ b/src/main/java/util/LottoNumbersDtoGenerator.java @@ -1,11 +1,12 @@ -package domain.factory; +package util; import domain.LottoNumber; import domain.numberscontainer.LottoNumbersDto; import java.util.*; +import java.util.stream.Collectors; -public class LottoNumberFactory { +public class LottoNumbersDtoGenerator { private static final int FIRST_INDEX = 0; private static final int SIXTH_INDEX = 6; @@ -24,7 +25,10 @@ private static List getShuffledList() { return lottoNumbers; } - public static LottoNumbersDto generateFixedNumberDto(Set sixNumbers, LottoNumber bonusNumber) { - return new LottoNumbersDto(sixNumbers, bonusNumber); + public static LottoNumbersDto generateFixedNumberDto(Set sixNumbers, int bonusNumber) { + Set sixNumbersSet = sixNumbers.stream() + .map(LottoNumber::getLottoNumber) + .collect(Collectors.toSet()); + return new LottoNumbersDto(sixNumbersSet, LottoNumber.getLottoNumber(bonusNumber)); } } \ No newline at end of file diff --git a/src/main/java/view/InputView.java b/src/main/java/view/InputView.java index b9a322aa6b..0ba6e563e7 100644 --- a/src/main/java/view/InputView.java +++ b/src/main/java/view/InputView.java @@ -2,6 +2,8 @@ import domain.LottoNumber; import domain.Money; +import domain.numberscontainer.LottoNumbersDto; +import util.LottoNumbersDtoGenerator; import java.util.*; import java.util.stream.Collectors; @@ -28,26 +30,26 @@ private static void validateNumber(String input) { } } - public static Set enterLastWeekWinningNumbers() { + public static LottoNumbersDto enterWinningNumbers() { + return LottoNumbersDtoGenerator.generateFixedNumberDto(enterLastWeekWinningNumbers(), enterBonusNumber()); + } + + private static Set enterLastWeekWinningNumbers() { System.out.println("지난 주 당첨 번호를 입력해 주세요."); - Set lastWeekWinningNumbers = parseWinningNumbers(scanner.nextLine()); + Set lastWeekWinningNumbers = parseWinningNumbers(scanner.nextLine()); return lastWeekWinningNumbers; } - private static Set parseWinningNumbers(String input) { - List lottoNumbers = Arrays.asList(input.split(",")).stream() - .map(InputView::parseLottoNumber) + private static Set parseWinningNumbers(String input) { + List lottoNumbers = Arrays.asList(input.split(",")).stream() + .map(String::trim) + .map(InputView::parseInt) .collect(Collectors.toList()); return new HashSet<>(lottoNumbers); } - public static LottoNumber enterBonusNumber() { + private static int enterBonusNumber() { System.out.println("보너스 볼을 입력해 주세요."); - return parseLottoNumber(scanner.nextLine()); - } - - private static LottoNumber parseLottoNumber(String input) { - int parsedInt = parseInt(input); - return LottoNumber.getLottoNumber(parsedInt); + return parseInt(scanner.nextLine()); } } diff --git a/src/main/java/view/OutputView.java b/src/main/java/view/OutputView.java index 8841b634a9..516685b4e1 100644 --- a/src/main/java/view/OutputView.java +++ b/src/main/java/view/OutputView.java @@ -1,14 +1,18 @@ package view; +import domain.LottoProfit; +import domain.LottoResult; import domain.Money; import domain.numberscontainer.Ticket; +import java.util.Arrays; import java.util.List; +import java.util.Map; public class OutputView { - public static void printNumberOfTickets(Money money) { - System.out.println(money.getNumberOfTickets() + "개를 구매했습니다."); + public static void printNumberOfTickets(int size) { + System.out.println(size + "개를 구매했습니다."); } public static void printTickets(List tickets) { @@ -19,4 +23,26 @@ public static void printTickets(List tickets) { } System.out.println(stringBuilder.toString()); } + + public static void printLottoResults(Map lottoResults) { + StringBuilder stringBuilder = new StringBuilder(); + stringBuilder.append("당첨 통계\n-------------\n"); + List results = Arrays.asList(LottoResult.values()).subList(1, LottoResult.values().length); + results.stream() + .forEach(result -> { + stringBuilder.append(result.toString()); + stringBuilder.append(convertNullToZero(lottoResults.get(result))); + stringBuilder.append("개\n"); + }); + System.out.println(stringBuilder.toString()); + } + + private static long convertNullToZero(Long number) { + if (number == null) return 0; + return number; + } + + public static void printProfit(LottoProfit profit) { + System.out.println("총 수익률은 " + profit.getProfit() + "%입니다."); + } } \ No newline at end of file diff --git a/src/test/java/domain/LottoNumberTest.java b/src/test/java/domain/LottoNumberTest.java index 7168ffac5f..000308a785 100644 --- a/src/test/java/domain/LottoNumberTest.java +++ b/src/test/java/domain/LottoNumberTest.java @@ -4,15 +4,12 @@ import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatThrownBy; @DisplayName("로또 숫자 테스트") public class LottoNumberTest { @Test @DisplayName("로또 숫자 생성 예외처리") void lottoNumberConstructorTest() { - assertThatThrownBy(() -> LottoNumber.getLottoNumber(46)) - .isInstanceOf(IllegalArgumentException.class) - .hasMessage("1부터 45사이의 숫자를 입력해주세요."); + assertThat(LottoNumber.getLottoNumber(46)).isEqualTo(LottoNumber.ERROR); } } diff --git a/src/test/java/domain/LottoStoreTest.java b/src/test/java/domain/LottoStoreTest.java new file mode 100644 index 0000000000..00222be498 --- /dev/null +++ b/src/test/java/domain/LottoStoreTest.java @@ -0,0 +1,15 @@ +package domain; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class LottoStoreTest { + + @Test + @DisplayName("티켓들 생성") + void generateRandomTicket() { + assertThat(LottoStore.generateTickets(10).size()).isEqualTo(10); + } +} diff --git a/src/test/java/domain/UserTest.java b/src/test/java/domain/UserTest.java new file mode 100644 index 0000000000..3e3855d0f8 --- /dev/null +++ b/src/test/java/domain/UserTest.java @@ -0,0 +1,62 @@ +package domain; + +import domain.numberscontainer.WinningNumbers; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import util.LottoNumbersDtoGenerator; + +import java.util.*; + +import static org.assertj.core.api.Assertions.assertThat; + +public class UserTest { + @Test + @DisplayName("당첨 결과 계산") + void test1() { + User user = new User(); + Money money = new Money(10000); + user.buyTicketsManually(money, createMyNumbers()); + + Set sixNumbers = createSixNumbersSet(3, 4, 5, 6, 7, 8); + WinningNumbers winningNumbers = new WinningNumbers(LottoNumbersDtoGenerator.generateFixedNumberDto(sixNumbers, 9)); + Map lottoResults = user.confirmResult(winningNumbers); + + assertThat(lottoResults.get(LottoResult.FIRST)).isEqualTo(1); + assertThat(lottoResults.get(LottoResult.SECOND)).isEqualTo(2); + assertThat(lottoResults.get(LottoResult.THIRD)).isEqualTo(2); + assertThat(lottoResults.get(LottoResult.FOURTH)).isEqualTo(2); + assertThat(lottoResults.get(LottoResult.FIFTH)).isEqualTo(1); + assertThat(lottoResults.get(LottoResult.NO_WIN)).isEqualTo(2); + } + + @Test + @DisplayName("수익률 계산") + void test2() { + User user = new User(); + Money money = new Money(10000); + user.buyTicketsManually(money, createMyNumbers()); + + Set sixNumbers = createSixNumbersSet(3, 4, 5, 6, 7, 8); + WinningNumbers winningNumbers = new WinningNumbers(LottoNumbersDtoGenerator.generateFixedNumberDto(sixNumbers, 9)); + user.confirmResult(winningNumbers); + + assertThat(user.calculateProfit().getProfit()).isEqualTo(20631050); + } + + private List> createMyNumbers() { + return Arrays.asList(createSixNumbersSet(3, 4, 5, 6, 7, 8), + createSixNumbersSet(3, 4, 5, 6, 7, 9), + createSixNumbersSet(3, 4, 5, 6, 7, 10), + createSixNumbersSet(3, 4, 5, 6, 10, 11), + createSixNumbersSet(3, 4, 5, 10, 11, 12), + createSixNumbersSet(4, 5, 6, 7, 8, 9), + createSixNumbersSet(4, 5, 6, 7, 8, 10), + createSixNumbersSet(4, 5, 6, 7, 10, 11), + createSixNumbersSet(10, 11, 12, 13, 14, 15), + createSixNumbersSet(16, 17, 18, 19, 20, 21)); + } + + private Set createSixNumbersSet(int number1, int number2, int number3, int number4, int number5, int number6) { + return new HashSet(Arrays.asList(number1, number2, number3, number4, number5, number6)); + } +} diff --git a/src/test/java/domain/factory/LottoNumbersDtoFactoryTest.java b/src/test/java/domain/factory/LottoNumbersDtoFactoryTest.java deleted file mode 100644 index 3c28d53000..0000000000 --- a/src/test/java/domain/factory/LottoNumbersDtoFactoryTest.java +++ /dev/null @@ -1,43 +0,0 @@ -package domain.factory; - -import domain.LottoNumber; -import domain.numberscontainer.Ticket; -import domain.numberscontainer.WinningNumbers; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; - -import java.util.Arrays; -import java.util.HashSet; -import java.util.Set; - -import static org.assertj.core.api.Assertions.assertThatCode; - -class LottoNumbersDtoFactoryTest { - - @Test - @DisplayName("랜덤 티켓 생성") - void generateRandomTicket() { - LottoNumberFactory lottoNumberFactory = new LottoNumberFactory(); - assertThatCode(() -> new Ticket(lottoNumberFactory.generateRandomTicketDto())) - .doesNotThrowAnyException(); - } - - @Test - @DisplayName("고정값 당첨 번호 생성") - void generateFixedWinningNumber() { - LottoNumberFactory lottoNumberFactory = new LottoNumberFactory(); - assertThatCode(() -> new WinningNumbers(lottoNumberFactory.generateFixedNumberDto(createSixNumbers(1, 2, 3, 4, 5, 6), LottoNumber.SEVEN))) - .doesNotThrowAnyException(); - } - - private Set createSixNumbers(int number1, int number2, int number3, int number4, int number5, int number6) { - Set sixNumbers = new HashSet<>(Arrays.asList(LottoNumber.getLottoNumber(number1), - LottoNumber.getLottoNumber(number2), - LottoNumber.getLottoNumber(number3), - LottoNumber.getLottoNumber(number4), - LottoNumber.getLottoNumber(number5), - LottoNumber.getLottoNumber(number6))); - - return sixNumbers; - } -} \ No newline at end of file diff --git a/src/test/java/domain/numberscontainer/WinningNumbersTest.java b/src/test/java/domain/numberscontainer/WinningNumbersTest.java index 6b75d7c7ac..caede43220 100644 --- a/src/test/java/domain/numberscontainer/WinningNumbersTest.java +++ b/src/test/java/domain/numberscontainer/WinningNumbersTest.java @@ -7,8 +7,9 @@ import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; -import java.util.*; -import java.util.stream.Collectors; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; @@ -59,27 +60,13 @@ private LottoNumbersDto createLottoNumberDto(int number1, int number2, int numbe @Test @DisplayName("당첨 결과 확인") void getLottoResultTest() { - List tickets = Arrays.asList( - new Ticket(createLottoNumberDto(3, 4, 5, 6, 7, 8, -1)), // 1등 - new Ticket(createLottoNumberDto(3, 4, 5, 6, 7, 9, -1)), // 2등 - new Ticket(createLottoNumberDto(3, 4, 5, 6, 7, 10, -1)), // 3등 - new Ticket(createLottoNumberDto(3, 4, 5, 6, 10, 11, -1)), // 4등 - new Ticket(createLottoNumberDto(3, 4, 5, 10, 11, 12, -1)), // 5등 - new Ticket(createLottoNumberDto(4, 5, 6, 7, 8, 9, -1)), // 2등 - new Ticket(createLottoNumberDto(4, 5, 6, 7, 8, 10, -1)), // 3등 - new Ticket(createLottoNumberDto(4, 5, 6, 7, 10, 11, -1)), // 4등 - new Ticket(createLottoNumberDto(10, 11, 12, 13, 14, 15, -1))); // 당첨X - WinningNumbers winningNumbers = new WinningNumbers(createLottoNumberDto(3, 4, 5, 6, 7, 8, 9)); - Map map = tickets.stream() - .collect(Collectors.groupingBy(ticket -> winningNumbers.getLottoResult(ticket), Collectors.counting())); - - assertThat(map.get(LottoResult.FIRST)).isEqualTo(1); - assertThat(map.get(LottoResult.SECOND)).isEqualTo(2); - assertThat(map.get(LottoResult.THIRD)).isEqualTo(2); - assertThat(map.get(LottoResult.FOURTH)).isEqualTo(2); - assertThat(map.get(LottoResult.FIFTH)).isEqualTo(1); - assertThat(map.get(LottoResult.NO_WIN)).isEqualTo(1); + assertThat(winningNumbers.getLottoResult(new Ticket(createLottoNumberDto(3, 4, 5, 6, 7, 8, -1)))).isEqualTo(LottoResult.FIRST); + assertThat(winningNumbers.getLottoResult(new Ticket(createLottoNumberDto(3, 4, 5, 6, 7, 9, -1)))).isEqualTo(LottoResult.SECOND); + assertThat(winningNumbers.getLottoResult(new Ticket(createLottoNumberDto(3, 4, 5, 6, 7, 10, -1)))).isEqualTo(LottoResult.THIRD); + assertThat(winningNumbers.getLottoResult(new Ticket(createLottoNumberDto(3, 4, 5, 6, 10, 11, -1)))).isEqualTo(LottoResult.FOURTH); + assertThat(winningNumbers.getLottoResult(new Ticket(createLottoNumberDto(3, 4, 5, 10, 11, 12, -1)))).isEqualTo(LottoResult.FIFTH); + assertThat(winningNumbers.getLottoResult(new Ticket(createLottoNumberDto(10, 11, 12, 13, 14, 15, -1)))).isEqualTo(LottoResult.NO_WIN); } } \ No newline at end of file diff --git a/src/test/java/util/LottoNumbersDtoGeneratorTest.java b/src/test/java/util/LottoNumbersDtoGeneratorTest.java new file mode 100644 index 0000000000..ed9d972731 --- /dev/null +++ b/src/test/java/util/LottoNumbersDtoGeneratorTest.java @@ -0,0 +1,36 @@ +package util; + +import domain.LottoNumber; +import domain.numberscontainer.Ticket; +import domain.numberscontainer.WinningNumbers; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + +import static org.assertj.core.api.Assertions.assertThatCode; + +class LottoNumbersDtoGeneratorTest { + + @Test + @DisplayName("랜덤 티켓 생성") + void generateRandomTicket() { + LottoNumbersDtoGenerator lottoNumbersDtoGenerator = new LottoNumbersDtoGenerator(); + assertThatCode(() -> new Ticket(lottoNumbersDtoGenerator.generateRandomTicketDto())) + .doesNotThrowAnyException(); + } + + @Test + @DisplayName("고정값 당첨 번호 생성") + void generateFixedWinningNumber() { + LottoNumbersDtoGenerator lottoNumbersDtoGenerator = new LottoNumbersDtoGenerator(); + assertThatCode(() -> new WinningNumbers(lottoNumbersDtoGenerator.generateFixedNumberDto(createSixNumbers(1, 2, 3, 4, 5, 6), 7))) + .doesNotThrowAnyException(); + } + + private Set createSixNumbers(int number1, int number2, int number3, int number4, int number5, int number6) { + return new HashSet<>(Arrays.asList(number1, number2, number3, number4, number5, number6)); + } +} \ No newline at end of file From efc5d5aa6cf3a1392632b01359ac137063ecf875 Mon Sep 17 00:00:00 2001 From: KimSeongGyu1 Date: Thu, 20 Feb 2020 16:33:07 +0900 Subject: [PATCH 17/23] =?UTF-8?q?[refactor]=20=EB=A6=AC=ED=8E=99=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/domain/LottoProfit.java | 1 - src/test/java/util/LottoNumbersDtoGeneratorTest.java | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/domain/LottoProfit.java b/src/main/java/domain/LottoProfit.java index 848cbc6400..760a672402 100644 --- a/src/main/java/domain/LottoProfit.java +++ b/src/main/java/domain/LottoProfit.java @@ -12,7 +12,6 @@ public LottoProfit(double profit) { public static LottoProfit getProfit(Map lottoResults, Money money) { double totalPrize = lottoResults.keySet().stream() - .filter(object -> lottoResults.get(object) != null) .mapToDouble(object -> object.getPrize() * lottoResults.get(object)) .sum(); return new LottoProfit(totalPrize / money.getMoney() * 100); diff --git a/src/test/java/util/LottoNumbersDtoGeneratorTest.java b/src/test/java/util/LottoNumbersDtoGeneratorTest.java index ed9d972731..68ad51769a 100644 --- a/src/test/java/util/LottoNumbersDtoGeneratorTest.java +++ b/src/test/java/util/LottoNumbersDtoGeneratorTest.java @@ -12,6 +12,7 @@ import static org.assertj.core.api.Assertions.assertThatCode; +@DisplayName("Dto 생성 테스트") class LottoNumbersDtoGeneratorTest { @Test From 9b9f778177991bc48257ea3852f7362e57a38f7f Mon Sep 17 00:00:00 2001 From: KimSeongGyu1 Date: Thu, 20 Feb 2020 17:02:57 +0900 Subject: [PATCH 18/23] =?UTF-8?q?[refactor]=20=EB=A6=AC=ED=8E=99=ED=86=A0?= =?UTF-8?q?=EB=A7=81,=20=EC=88=98=EC=9D=B5=EB=A5=A0=20=EC=97=90=EB=9F=AC?= =?UTF-8?q?=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/domain/LottoProfit.java | 8 ++++---- src/main/java/domain/LottoResult.java | 4 ++-- src/main/java/domain/LottoStore.java | 8 ++++---- src/main/java/domain/Money.java | 8 ++++---- src/main/java/view/OutputView.java | 2 +- src/test/java/domain/UserTest.java | 2 +- 6 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/main/java/domain/LottoProfit.java b/src/main/java/domain/LottoProfit.java index 760a672402..9e7e31d566 100644 --- a/src/main/java/domain/LottoProfit.java +++ b/src/main/java/domain/LottoProfit.java @@ -3,7 +3,6 @@ import java.util.Map; public class LottoProfit { - private double profit; public LottoProfit(double profit) { @@ -11,10 +10,11 @@ public LottoProfit(double profit) { } public static LottoProfit getProfit(Map lottoResults, Money money) { - double totalPrize = lottoResults.keySet().stream() - .mapToDouble(object -> object.getPrize() * lottoResults.get(object)) + Long totalPrize = lottoResults.keySet().stream() + .mapToLong(object -> object.getPrize() * lottoResults.get(object)) .sum(); - return new LottoProfit(totalPrize / money.getMoney() * 100); + + return new LottoProfit(totalPrize * 100 / money.getMoney()); } public double getProfit() { diff --git a/src/main/java/domain/LottoResult.java b/src/main/java/domain/LottoResult.java index 2213a5fc5d..c9555e7770 100644 --- a/src/main/java/domain/LottoResult.java +++ b/src/main/java/domain/LottoResult.java @@ -5,7 +5,7 @@ import java.util.List; public enum LottoResult { - NO_WIN(-1, -1, false), + NO_WIN(0, -1, false), FIRST(2000000000, 6, false), SECOND(30000000, 5, false), THIRD(1500000, 5, false), @@ -35,7 +35,7 @@ public static LottoResult findLottoResult(int matchingNumbers, boolean isBonus) return lottoResult; } - public double getPrize() { + public int getPrize() { return prize; } diff --git a/src/main/java/domain/LottoStore.java b/src/main/java/domain/LottoStore.java index 7fb4fd8588..94bfec5924 100644 --- a/src/main/java/domain/LottoStore.java +++ b/src/main/java/domain/LottoStore.java @@ -9,7 +9,7 @@ import java.util.stream.Collectors; public class LottoStore { - public static List generateTickets(int number) { + public static List generateTickets(long number) { List tickets = new ArrayList<>(); for (int i = 0; i < number; i++) { tickets.add(new Ticket(LottoNumbersDtoGenerator.generateRandomTicketDto())); @@ -17,20 +17,20 @@ public static List generateTickets(int number) { return tickets; } - public static List generateTickets(int number, List> myNumbers) { + public static List generateTickets(long number, List> myNumbers) { List tickets = myNumbers.stream() .map(numbers -> LottoNumbersDtoGenerator.generateFixedNumberDto(numbers, -1)) .map(dto -> new Ticket(dto)) .collect(Collectors.toList()); - int randomTicketsNumber = getRandomTicketsNumber(number, myNumbers); + long randomTicketsNumber = getRandomTicketsNumber(number, myNumbers); for (int i = 0; i < randomTicketsNumber; i++) { tickets.add(new Ticket(LottoNumbersDtoGenerator.generateRandomTicketDto())); } return tickets; } - private static int getRandomTicketsNumber(int number, List> myNumbers) { + private static long getRandomTicketsNumber(long number, List> myNumbers) { return number - myNumbers.size(); } } diff --git a/src/main/java/domain/Money.java b/src/main/java/domain/Money.java index e76b272910..4d06943953 100644 --- a/src/main/java/domain/Money.java +++ b/src/main/java/domain/Money.java @@ -1,19 +1,19 @@ package domain; public class Money { - private final int money; + private final long money; - public Money(int money) { + public Money(long money) { validateMoney(money); this.money = money; } - private void validateMoney(int money) { + private void validateMoney(long money) { validateMoneyRange(money); validateMoneyUnit(money); } - public int getNumberOfTickets() { + public long getNumberOfTickets() { return this.money / 1000; } diff --git a/src/main/java/view/OutputView.java b/src/main/java/view/OutputView.java index 516685b4e1..b5a2545b63 100644 --- a/src/main/java/view/OutputView.java +++ b/src/main/java/view/OutputView.java @@ -43,6 +43,6 @@ private static long convertNullToZero(Long number) { } public static void printProfit(LottoProfit profit) { - System.out.println("총 수익률은 " + profit.getProfit() + "%입니다."); + System.out.println("총 수익률은 " + (long)profit.getProfit() + "%입니다."); } } \ No newline at end of file diff --git a/src/test/java/domain/UserTest.java b/src/test/java/domain/UserTest.java index 3e3855d0f8..4204e8f398 100644 --- a/src/test/java/domain/UserTest.java +++ b/src/test/java/domain/UserTest.java @@ -33,7 +33,7 @@ void test1() { @DisplayName("수익률 계산") void test2() { User user = new User(); - Money money = new Money(10000); + Money money = new Money(10000L); user.buyTicketsManually(money, createMyNumbers()); Set sixNumbers = createSixNumbersSet(3, 4, 5, 6, 7, 8); From dc876fc13276055c8f052bcf0dd931cbd6fb9122 Mon Sep 17 00:00:00 2001 From: KimSeongGyu1 Date: Thu, 20 Feb 2020 17:07:02 +0900 Subject: [PATCH 19/23] =?UTF-8?q?[refactor]=20=EB=A6=AC=ED=8E=99=ED=86=A0?= =?UTF-8?q?=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 20 ++++++++++---------- src/main/java/domain/LottoResult.java | 5 ++++- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 57999d2ed4..b73177b6c2 100644 --- a/README.md +++ b/README.md @@ -8,16 +8,16 @@ - [x] (예외) 1000원 이하 단위일 경우 - [x] (예외) 숫자가 아닐 경우 - [x] (예외) 음수 또는 50000원 이상 입력될 경우 -- [ ] 구매한 로또 장 수와 번호를 출력한다. -- [ ] 지난 주 당첨 번호를 입력받는다. - - [ ] (예외) 1~45 이외의 숫자가 입력되는 경우 - - [ ] (예외) 올바른 입력 형식이 아닌 경우 - - [ ] (예외) 6개가 아닌 경우 - - [ ] (예외) 공백이 입력되는 경우 -- [ ] 보너스 볼을 입력받는다. - - [ ] (예외) 위와 동일 -- [ ] 당첨 번호를 대조해서 결과를 얻는다. -- [ ] 당첨 통계와 수익률을 출력한다. +- [x] 구매한 로또 장 수와 번호를 출력한다. +- [x] 지난 주 당첨 번호를 입력받는다. + - [x] (예외) 1~45 이외의 숫자가 입력되는 경우 + - [x] (예외) 올바른 입력 형식이 아닌 경우 + - [x] (예외) 6개가 아닌 경우 + - [x] (예외) 공백이 입력되는 경우 +- [x] 보너스 볼을 입력받는다. + - [x] (예외) 위와 동일 +- [x] 당첨 번호를 대조해서 결과를 얻는다. +- [x] 당첨 통계와 수익률을 출력한다. > 프로그래밍 요구사항 diff --git a/src/main/java/domain/LottoResult.java b/src/main/java/domain/LottoResult.java index c9555e7770..3d62c068c0 100644 --- a/src/main/java/domain/LottoResult.java +++ b/src/main/java/domain/LottoResult.java @@ -39,8 +39,11 @@ public int getPrize() { return prize; } + @Override public String toString() { - if (this == NO_WIN) return ""; + if (this == NO_WIN) { + return ""; + } StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append(this.matchingNumbers); stringBuilder.append("개 일치 "); From 40468b368cac58ec6201aa39e88cc36e8eb823b7 Mon Sep 17 00:00:00 2001 From: YebinK Date: Sun, 23 Feb 2020 18:18:37 +0900 Subject: [PATCH 20/23] =?UTF-8?q?[refactor]=20DTO=EC=99=80=20=EB=B9=84?= =?UTF-8?q?=EC=A6=88=EB=8B=88=EC=8A=A4=20=EB=A1=9C=EC=A7=81=20=EC=B1=85?= =?UTF-8?q?=EC=9E=84=20=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/LottoApplication.java | 11 ++-- src/main/java/domain/LottoProfit.java | 7 +-- src/main/java/domain/LottoResult.java | 37 ++++++------- src/main/java/domain/LottoStore.java | 29 +++++----- src/main/java/domain/Money.java | 14 ++--- src/main/java/domain/User.java | 9 +++- .../numberscontainer/BonusNumberDTO.java | 13 +++++ .../{ => numberscontainer}/LottoNumber.java | 5 +- .../LottoNumbersContainer.java | 22 -------- .../numberscontainer/LottoNumbersDto.java | 27 ---------- .../numberscontainer/SixLottoNumbers.java | 20 +++++++ .../numberscontainer/SixLottoNumbersDTO.java | 15 ++++++ .../java/domain/numberscontainer/Ticket.java | 9 ++-- .../numberscontainer/WinningNumbers.java | 39 +++++--------- .../java/util/LottoNumbersDtoGenerator.java | 34 ------------ .../java/util/SixLottoNumbersFactory.java | 31 +++++++++++ src/main/java/view/InputView.java | 20 ++++--- src/main/java/view/OutputView.java | 7 +-- src/test/java/domain/LottoNumberTest.java | 9 ++-- src/test/java/domain/UserTest.java | 17 +++--- .../domain/numberscontainer/TicketTest.java | 15 +++--- .../numberscontainer/WinningNumbersTest.java | 53 +++++++++++-------- ...t.java => SixLottoNumbersFactoryTest.java} | 14 ++--- 23 files changed, 229 insertions(+), 228 deletions(-) create mode 100644 src/main/java/domain/numberscontainer/BonusNumberDTO.java rename src/main/java/domain/{ => numberscontainer}/LottoNumber.java (86%) delete mode 100644 src/main/java/domain/numberscontainer/LottoNumbersContainer.java delete mode 100644 src/main/java/domain/numberscontainer/LottoNumbersDto.java create mode 100644 src/main/java/domain/numberscontainer/SixLottoNumbers.java create mode 100644 src/main/java/domain/numberscontainer/SixLottoNumbersDTO.java delete mode 100644 src/main/java/util/LottoNumbersDtoGenerator.java create mode 100644 src/main/java/util/SixLottoNumbersFactory.java rename src/test/java/util/{LottoNumbersDtoGeneratorTest.java => SixLottoNumbersFactoryTest.java} (57%) diff --git a/src/main/java/LottoApplication.java b/src/main/java/LottoApplication.java index 6b39ca24b2..b5db4bf573 100644 --- a/src/main/java/LottoApplication.java +++ b/src/main/java/LottoApplication.java @@ -1,6 +1,7 @@ import domain.Money; import domain.User; -import domain.numberscontainer.LottoNumbersDto; +import domain.numberscontainer.BonusNumberDTO; +import domain.numberscontainer.SixLottoNumbersDTO; import domain.numberscontainer.WinningNumbers; import view.InputView; import view.OutputView; @@ -11,7 +12,7 @@ public static void main(String[] args) { User user = new User(); user.buyTickets(enterMoney()); - OutputView.printNumberOfTickets(user.getTickets().size()); + OutputView.printNumberOfTickets(user.getTicketsSize()); OutputView.printTickets(user.getTickets()); WinningNumbers winningNumbers = enterWinningNumbers(); @@ -26,13 +27,13 @@ private static Money enterMoney() { System.out.println(e.getMessage()); return enterMoney(); } - } private static WinningNumbers enterWinningNumbers() { try { - LottoNumbersDto lottoNumbersDto = InputView.enterWinningNumbers(); - return new WinningNumbers(lottoNumbersDto); + SixLottoNumbersDTO sixLottoNumbersDTO = InputView.enterLastWeekWinningNumbers(); + BonusNumberDTO bonusNumberDTO = InputView.enterBonusNumber(); + return new WinningNumbers(sixLottoNumbersDTO, bonusNumberDTO); } catch (IllegalArgumentException e) { System.out.println(e.getMessage()); return enterWinningNumbers(); diff --git a/src/main/java/domain/LottoProfit.java b/src/main/java/domain/LottoProfit.java index 9e7e31d566..3c55d3e513 100644 --- a/src/main/java/domain/LottoProfit.java +++ b/src/main/java/domain/LottoProfit.java @@ -3,9 +3,10 @@ import java.util.Map; public class LottoProfit { + public static final int TO_PERCENTAGE = 100; private double profit; - public LottoProfit(double profit) { + private LottoProfit(double profit) { this.profit = profit; } @@ -14,10 +15,10 @@ public static LottoProfit getProfit(Map lottoResults, Money m .mapToLong(object -> object.getPrize() * lottoResults.get(object)) .sum(); - return new LottoProfit(totalPrize * 100 / money.getMoney()); + return new LottoProfit(totalPrize / money.getMoney() * TO_PERCENTAGE); } - public double getProfit() { + public double getValue() { return profit; } } diff --git a/src/main/java/domain/LottoResult.java b/src/main/java/domain/LottoResult.java index 3d62c068c0..34eee8e4bb 100644 --- a/src/main/java/domain/LottoResult.java +++ b/src/main/java/domain/LottoResult.java @@ -1,33 +1,30 @@ package domain; -import java.text.DecimalFormat; import java.util.Arrays; import java.util.List; public enum LottoResult { - NO_WIN(0, -1, false), - FIRST(2000000000, 6, false), - SECOND(30000000, 5, false), - THIRD(1500000, 5, false), - FOURTH(50000, 4, true), - FIFTH(5000, 3, false); - - private int prize; - private int matchingNumbers; - private boolean isBonus; - - LottoResult(int prize, int matchingNumbers, boolean isBonus) { + FAILED(0, -1), + FIRST(2000000000, 6), + SECOND(30000000, 5), + THIRD(1500000, 5), + FOURTH(50000, 4), + FIFTH(5000, 3); + + private final int prize; + private final int matchCount; + + LottoResult(int prize, int matchCount) { this.prize = prize; - this.matchingNumbers = matchingNumbers; - this.isBonus = isBonus; + this.matchCount = matchCount; } - public static LottoResult findLottoResult(int matchingNumbers, boolean isBonus) { + public static LottoResult findLottoResult(int matchCount, boolean isBonus) { List lottoResults = Arrays.asList(LottoResult.values()); LottoResult lottoResult = lottoResults.stream() - .filter(result -> result.matchingNumbers == matchingNumbers) + .filter(result -> result.matchCount == matchCount) .findFirst() - .orElse(NO_WIN); + .orElse(FAILED); if (lottoResult == SECOND && !isBonus) { lottoResult = THIRD; @@ -41,11 +38,11 @@ public int getPrize() { @Override public String toString() { - if (this == NO_WIN) { + if (this == FAILED) { return ""; } StringBuilder stringBuilder = new StringBuilder(); - stringBuilder.append(this.matchingNumbers); + stringBuilder.append(this.matchCount); stringBuilder.append("개 일치 "); if (this == SECOND) { stringBuilder.append(", 보너스 볼 일치 "); diff --git a/src/main/java/domain/LottoStore.java b/src/main/java/domain/LottoStore.java index 94bfec5924..9999ab31fd 100644 --- a/src/main/java/domain/LottoStore.java +++ b/src/main/java/domain/LottoStore.java @@ -1,7 +1,8 @@ package domain; +import domain.numberscontainer.SixLottoNumbersDTO; import domain.numberscontainer.Ticket; -import util.LottoNumbersDtoGenerator; +import util.SixLottoNumbersFactory; import java.util.ArrayList; import java.util.List; @@ -9,28 +10,30 @@ import java.util.stream.Collectors; public class LottoStore { - public static List generateTickets(long number) { + public static List generateTickets(int ticketSize) { List tickets = new ArrayList<>(); - for (int i = 0; i < number; i++) { - tickets.add(new Ticket(LottoNumbersDtoGenerator.generateRandomTicketDto())); + + for (int i = 0; i < ticketSize; i++) { + tickets.add(new Ticket(new SixLottoNumbersDTO(SixLottoNumbersFactory.createRandom()))); } return tickets; } - public static List generateTickets(long number, List> myNumbers) { - List tickets = myNumbers.stream() - .map(numbers -> LottoNumbersDtoGenerator.generateFixedNumberDto(numbers, -1)) - .map(dto -> new Ticket(dto)) + public static List generateTickets(int ticketSize, List> givenNumbers) { + List tickets = givenNumbers.stream() + .map(SixLottoNumbersFactory::createFixed) + .map(SixLottoNumbersDTO::new) + .map(Ticket::new) .collect(Collectors.toList()); - long randomTicketsNumber = getRandomTicketsNumber(number, myNumbers); - for (int i = 0; i < randomTicketsNumber; i++) { - tickets.add(new Ticket(LottoNumbersDtoGenerator.generateRandomTicketDto())); + int randomTicketsSize = getRandomTicketsSize(ticketSize, givenNumbers); + for (int i = 0; i < randomTicketsSize; i++) { + tickets.add(new Ticket(new SixLottoNumbersDTO(SixLottoNumbersFactory.createRandom()))); } return tickets; } - private static long getRandomTicketsNumber(long number, List> myNumbers) { - return number - myNumbers.size(); + private static int getRandomTicketsSize(int number, List> givenNumbers) { + return number - givenNumbers.size(); } } diff --git a/src/main/java/domain/Money.java b/src/main/java/domain/Money.java index 4d06943953..8c386b25f3 100644 --- a/src/main/java/domain/Money.java +++ b/src/main/java/domain/Money.java @@ -1,35 +1,35 @@ package domain; public class Money { - private final long money; + private final int money; - public Money(long money) { + public Money(int money) { validateMoney(money); this.money = money; } - private void validateMoney(long money) { + private void validateMoney(int money) { validateMoneyRange(money); validateMoneyUnit(money); } - public long getNumberOfTickets() { + public int getNumberOfTickets() { return this.money / 1000; } - private void validateMoneyRange(long money) { + private void validateMoneyRange(int money) { if (money < 0 || money > 50000) { throw new IllegalArgumentException("0원 이상, 5만원 이하 금액만 구매 가능합니다."); } } - private void validateMoneyUnit(long money) { + private void validateMoneyUnit(int money) { if (money % 1000 != 0) { throw new IllegalArgumentException("천 원 단위로만 구매 가능합니다."); } } - public long getMoney() { + public int getMoney() { return this.money; } } diff --git a/src/main/java/domain/User.java b/src/main/java/domain/User.java index df0e3dd343..46358115bf 100644 --- a/src/main/java/domain/User.java +++ b/src/main/java/domain/User.java @@ -21,13 +21,14 @@ public User() { } public void buyTickets(Money money) { + //this.spentMoney = spentMoney.getMoney() + money; this.spentMoney = money; this.tickets = LottoStore.generateTickets(money.getNumberOfTickets()); } - public void buyTicketsManually(Money money, List> myNumbers) { + public void buyTicketsManually(Money money, List> fixedNumbers) { this.spentMoney = money; - this.tickets = LottoStore.generateTickets(money.getNumberOfTickets(), myNumbers); + this.tickets = LottoStore.generateTickets(money.getNumberOfTickets(), fixedNumbers); } public Map confirmResult(WinningNumbers winningNumbers) { @@ -43,4 +44,8 @@ public LottoProfit calculateProfit() { public List getTickets() { return tickets; } + + public int getTicketsSize() { + return tickets.size(); + } } \ No newline at end of file diff --git a/src/main/java/domain/numberscontainer/BonusNumberDTO.java b/src/main/java/domain/numberscontainer/BonusNumberDTO.java new file mode 100644 index 0000000000..78401a8a1b --- /dev/null +++ b/src/main/java/domain/numberscontainer/BonusNumberDTO.java @@ -0,0 +1,13 @@ +package domain.numberscontainer; + +public class BonusNumberDTO { + private final LottoNumber bonusNumber; + + public BonusNumberDTO(LottoNumber bonusNumber) { + this.bonusNumber = bonusNumber; + } + + public LottoNumber getBonusNumber() { + return bonusNumber; + } +} diff --git a/src/main/java/domain/LottoNumber.java b/src/main/java/domain/numberscontainer/LottoNumber.java similarity index 86% rename from src/main/java/domain/LottoNumber.java rename to src/main/java/domain/numberscontainer/LottoNumber.java index 880a50994f..05b86644d8 100644 --- a/src/main/java/domain/LottoNumber.java +++ b/src/main/java/domain/numberscontainer/LottoNumber.java @@ -1,10 +1,9 @@ -package domain; +package domain.numberscontainer; import java.util.Arrays; import java.util.List; public enum LottoNumber { - ERROR(-1), ONE(1), TWO(2), THREE(3), @@ -63,7 +62,7 @@ public static LottoNumber getLottoNumber(int number) { return lottoNumbers.stream() .filter(lottoNumber -> lottoNumber.number == number) .findFirst() - .orElse(LottoNumber.ERROR); + .orElseThrow(() -> new IllegalArgumentException(String.format("%d이(가) 입력되었습니다. 1부터 45까지의 숫자를 입력해주세요.", number))); } public int getValue() { diff --git a/src/main/java/domain/numberscontainer/LottoNumbersContainer.java b/src/main/java/domain/numberscontainer/LottoNumbersContainer.java deleted file mode 100644 index 2c0ac042c3..0000000000 --- a/src/main/java/domain/numberscontainer/LottoNumbersContainer.java +++ /dev/null @@ -1,22 +0,0 @@ -package domain.numberscontainer; - -import domain.LottoNumber; - -import java.util.Set; - -abstract class LottoNumbersContainer { - private static final int TICKET_SIZE = 6; - - protected final Set sixLottoNumbers; - - public LottoNumbersContainer(LottoNumbersDto lottoNumbersDto) { - validateSize(lottoNumbersDto.getSixNumbers()); - this.sixLottoNumbers = lottoNumbersDto.getSixNumbers(); - } - - protected void validateSize(Set lottoNumbers) { - if (lottoNumbers.size() != TICKET_SIZE) { - throw new IllegalArgumentException("6개의 숫자를 입력해주세요."); - } - } -} diff --git a/src/main/java/domain/numberscontainer/LottoNumbersDto.java b/src/main/java/domain/numberscontainer/LottoNumbersDto.java deleted file mode 100644 index 62fa79cd68..0000000000 --- a/src/main/java/domain/numberscontainer/LottoNumbersDto.java +++ /dev/null @@ -1,27 +0,0 @@ -package domain.numberscontainer; - -import domain.LottoNumber; - -import java.util.Set; - -public class LottoNumbersDto { - private final Set sixNumbers; - private final LottoNumber bonusNumber; - - public LottoNumbersDto(Set sixNumbers) { - this(sixNumbers, LottoNumber.ERROR); - } - - public LottoNumbersDto(Set sixNumbers, LottoNumber bonusNumber) { - this.sixNumbers = sixNumbers; - this.bonusNumber = bonusNumber; - } - - public Set getSixNumbers() { - return sixNumbers; - } - - public LottoNumber getBonusNumber() { - return bonusNumber; - } -} \ No newline at end of file diff --git a/src/main/java/domain/numberscontainer/SixLottoNumbers.java b/src/main/java/domain/numberscontainer/SixLottoNumbers.java new file mode 100644 index 0000000000..0cc1650120 --- /dev/null +++ b/src/main/java/domain/numberscontainer/SixLottoNumbers.java @@ -0,0 +1,20 @@ +package domain.numberscontainer; + +import java.util.Set; + +public class SixLottoNumbers { + private static final int LOTTO_NUMBERS_SIZE = 6; + + protected final Set sixLottoNumbers; + + public SixLottoNumbers(SixLottoNumbersDTO sixLottoNumbersDTO) { + validateSize(sixLottoNumbersDTO.getSixNumbers()); + this.sixLottoNumbers = sixLottoNumbersDTO.getSixNumbers(); + } + + protected void validateSize(Set lottoNumbers) { + if (lottoNumbers.size() != LOTTO_NUMBERS_SIZE) { + throw new IllegalArgumentException(String.format("%d개의 숫자를 입력해주세요.", LOTTO_NUMBERS_SIZE)); + } + } +} \ No newline at end of file diff --git a/src/main/java/domain/numberscontainer/SixLottoNumbersDTO.java b/src/main/java/domain/numberscontainer/SixLottoNumbersDTO.java new file mode 100644 index 0000000000..c1e628df84 --- /dev/null +++ b/src/main/java/domain/numberscontainer/SixLottoNumbersDTO.java @@ -0,0 +1,15 @@ +package domain.numberscontainer; + +import java.util.Set; + +public class SixLottoNumbersDTO { + private final Set sixNumbers; + + public SixLottoNumbersDTO(Set sixNumbers) { + this.sixNumbers = sixNumbers; + } + + public Set getSixNumbers() { + return sixNumbers; + } +} \ No newline at end of file diff --git a/src/main/java/domain/numberscontainer/Ticket.java b/src/main/java/domain/numberscontainer/Ticket.java index d6b740653f..1aa5533d89 100644 --- a/src/main/java/domain/numberscontainer/Ticket.java +++ b/src/main/java/domain/numberscontainer/Ticket.java @@ -1,12 +1,11 @@ package domain.numberscontainer; -import domain.LottoNumber; - import java.util.stream.Collectors; -public class Ticket extends LottoNumbersContainer { - public Ticket(LottoNumbersDto lottoNumbersDto) { - super(lottoNumbersDto); +public class Ticket extends SixLottoNumbers { + + public Ticket(SixLottoNumbersDTO sixLottoNumbersDTO) { + super(sixLottoNumbersDTO); } public boolean contains(LottoNumber number) { diff --git a/src/main/java/domain/numberscontainer/WinningNumbers.java b/src/main/java/domain/numberscontainer/WinningNumbers.java index d299fb153c..4a0bcee6b9 100644 --- a/src/main/java/domain/numberscontainer/WinningNumbers.java +++ b/src/main/java/domain/numberscontainer/WinningNumbers.java @@ -1,45 +1,32 @@ package domain.numberscontainer; -import domain.LottoNumber; import domain.LottoResult; -public class WinningNumbers extends LottoNumbersContainer { +public class WinningNumbers extends SixLottoNumbers { private final LottoNumber bonusNumber; - public WinningNumbers(LottoNumbersDto lottoNumbersDto) { - super(lottoNumbersDto); - validateBonusNumber(lottoNumbersDto.getBonusNumber()); - this.bonusNumber = lottoNumbersDto.getBonusNumber(); + public WinningNumbers(SixLottoNumbersDTO sixLottoNumbersDTO, BonusNumberDTO bonusNumberDTO) { + super(sixLottoNumbersDTO); + validateBonusNumber(bonusNumberDTO.getBonusNumber()); + this.bonusNumber = bonusNumberDTO.getBonusNumber(); } private void validateBonusNumber(LottoNumber bonusNumber) { - validateErrorBonusNumber(bonusNumber); - validateDuplicatedBonusNumber(bonusNumber); - } - - private void validateErrorBonusNumber(LottoNumber bonusNumber) { - if (bonusNumber == LottoNumber.ERROR) { - throw new IllegalArgumentException("1~45 사이의 보너스 번호를 입력해주세요."); - } - } - - private void validateDuplicatedBonusNumber(LottoNumber bonusNumber) { if (this.sixLottoNumbers.contains(bonusNumber)) { throw new IllegalArgumentException("당첨 번호와 중복되지 않는 보너스 번호를 입력해주세요."); } } - public int findDuplicatedNumbers(Ticket ticket) { - return this.sixLottoNumbers.stream() - .filter(number -> ticket.contains(number)) - .mapToInt(number -> 1) - .sum(); - } - public LottoResult getLottoResult(Ticket ticket) { - int matchingNumber = findDuplicatedNumbers(ticket); + int matchCount = findDuplicatedNumbers(ticket); boolean isBonus = ticket.contains(bonusNumber); - return LottoResult.findLottoResult(matchingNumber, isBonus); + return LottoResult.findLottoResult(matchCount, isBonus); + } + + public int findDuplicatedNumbers(Ticket ticket) { + return (int) this.sixLottoNumbers.stream() + .filter(ticket::contains) + .count(); } } \ No newline at end of file diff --git a/src/main/java/util/LottoNumbersDtoGenerator.java b/src/main/java/util/LottoNumbersDtoGenerator.java deleted file mode 100644 index ffeeb1e444..0000000000 --- a/src/main/java/util/LottoNumbersDtoGenerator.java +++ /dev/null @@ -1,34 +0,0 @@ -package util; - -import domain.LottoNumber; -import domain.numberscontainer.LottoNumbersDto; - -import java.util.*; -import java.util.stream.Collectors; - -public class LottoNumbersDtoGenerator { - - private static final int FIRST_INDEX = 0; - private static final int SIXTH_INDEX = 6; - private static final int EXCEPT_ERROR_FIRST_INDEX = 1; - - public static LottoNumbersDto generateRandomTicketDto() { - List lottoNumbers = getShuffledList(); - Set sixNumbers = new HashSet<>(lottoNumbers.subList(FIRST_INDEX, SIXTH_INDEX)); - - return new LottoNumbersDto(sixNumbers); - } - - private static List getShuffledList() { - List lottoNumbers = Arrays.asList(LottoNumber.values()).subList(EXCEPT_ERROR_FIRST_INDEX, LottoNumber.values().length); - Collections.shuffle(lottoNumbers); - return lottoNumbers; - } - - public static LottoNumbersDto generateFixedNumberDto(Set sixNumbers, int bonusNumber) { - Set sixNumbersSet = sixNumbers.stream() - .map(LottoNumber::getLottoNumber) - .collect(Collectors.toSet()); - return new LottoNumbersDto(sixNumbersSet, LottoNumber.getLottoNumber(bonusNumber)); - } -} \ No newline at end of file diff --git a/src/main/java/util/SixLottoNumbersFactory.java b/src/main/java/util/SixLottoNumbersFactory.java new file mode 100644 index 0000000000..f55273b507 --- /dev/null +++ b/src/main/java/util/SixLottoNumbersFactory.java @@ -0,0 +1,31 @@ +package util; + +import domain.numberscontainer.LottoNumber; + +import java.util.*; +import java.util.stream.Collectors; + +public class SixLottoNumbersFactory { + private static final int FIRST_INDEX = 0; + private static final int SIXTH_INDEX = 6; + + public static Set createRandom() { + Set sixNumbers = new HashSet<>(getShuffledList().subList(FIRST_INDEX, SIXTH_INDEX)); + + return sixNumbers; + } + + private static List getShuffledList() { + List lottoNumbers = Arrays.asList(LottoNumber.values()); + Collections.shuffle(lottoNumbers); + + return lottoNumbers; + } + + public static Set createFixed(Set givenNumbers) { + Set sixNumbers = givenNumbers.stream() + .map(LottoNumber::getLottoNumber) + .collect(Collectors.toSet()); + return sixNumbers; + } +} \ No newline at end of file diff --git a/src/main/java/view/InputView.java b/src/main/java/view/InputView.java index 0ba6e563e7..cc3bc2b004 100644 --- a/src/main/java/view/InputView.java +++ b/src/main/java/view/InputView.java @@ -1,9 +1,10 @@ package view; -import domain.LottoNumber; +import domain.numberscontainer.LottoNumber; import domain.Money; -import domain.numberscontainer.LottoNumbersDto; -import util.LottoNumbersDtoGenerator; +import domain.numberscontainer.BonusNumberDTO; +import domain.numberscontainer.SixLottoNumbersDTO; +import util.SixLottoNumbersFactory; import java.util.*; import java.util.stream.Collectors; @@ -30,14 +31,10 @@ private static void validateNumber(String input) { } } - public static LottoNumbersDto enterWinningNumbers() { - return LottoNumbersDtoGenerator.generateFixedNumberDto(enterLastWeekWinningNumbers(), enterBonusNumber()); - } - - private static Set enterLastWeekWinningNumbers() { + public static SixLottoNumbersDTO enterLastWeekWinningNumbers() { System.out.println("지난 주 당첨 번호를 입력해 주세요."); Set lastWeekWinningNumbers = parseWinningNumbers(scanner.nextLine()); - return lastWeekWinningNumbers; + return new SixLottoNumbersDTO(SixLottoNumbersFactory.createFixed(lastWeekWinningNumbers)); } private static Set parseWinningNumbers(String input) { @@ -48,8 +45,9 @@ private static Set parseWinningNumbers(String input) { return new HashSet<>(lottoNumbers); } - private static int enterBonusNumber() { + public static BonusNumberDTO enterBonusNumber() { System.out.println("보너스 볼을 입력해 주세요."); - return parseInt(scanner.nextLine()); + int bonusNumber = parseInt(scanner.nextLine()); + return new BonusNumberDTO(LottoNumber.getLottoNumber(bonusNumber)); } } diff --git a/src/main/java/view/OutputView.java b/src/main/java/view/OutputView.java index b5a2545b63..263a99f75a 100644 --- a/src/main/java/view/OutputView.java +++ b/src/main/java/view/OutputView.java @@ -2,7 +2,6 @@ import domain.LottoProfit; import domain.LottoResult; -import domain.Money; import domain.numberscontainer.Ticket; import java.util.Arrays; @@ -38,11 +37,13 @@ public static void printLottoResults(Map lottoResults) { } private static long convertNullToZero(Long number) { - if (number == null) return 0; + if (number == null) { + return 0; + } return number; } public static void printProfit(LottoProfit profit) { - System.out.println("총 수익률은 " + (long)profit.getProfit() + "%입니다."); + System.out.println("총 수익률은 " + (long)profit.getValue() + "%입니다."); } } \ No newline at end of file diff --git a/src/test/java/domain/LottoNumberTest.java b/src/test/java/domain/LottoNumberTest.java index 000308a785..bd46c52d7c 100644 --- a/src/test/java/domain/LottoNumberTest.java +++ b/src/test/java/domain/LottoNumberTest.java @@ -1,15 +1,18 @@ package domain; +import domain.numberscontainer.LottoNumber; 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; @DisplayName("로또 숫자 테스트") public class LottoNumberTest { @Test @DisplayName("로또 숫자 생성 예외처리") void lottoNumberConstructorTest() { - assertThat(LottoNumber.getLottoNumber(46)).isEqualTo(LottoNumber.ERROR); + assertThatThrownBy(() -> LottoNumber.getLottoNumber(46)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("46이(가) 입력되었습니다. 1부터 45까지의 숫자를 입력해주세요."); } -} +} \ No newline at end of file diff --git a/src/test/java/domain/UserTest.java b/src/test/java/domain/UserTest.java index 4204e8f398..ae8904ab1f 100644 --- a/src/test/java/domain/UserTest.java +++ b/src/test/java/domain/UserTest.java @@ -1,9 +1,12 @@ package domain; +import domain.numberscontainer.BonusNumberDTO; +import domain.numberscontainer.LottoNumber; +import domain.numberscontainer.SixLottoNumbersDTO; import domain.numberscontainer.WinningNumbers; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; -import util.LottoNumbersDtoGenerator; +import util.SixLottoNumbersFactory; import java.util.*; @@ -18,7 +21,7 @@ void test1() { user.buyTicketsManually(money, createMyNumbers()); Set sixNumbers = createSixNumbersSet(3, 4, 5, 6, 7, 8); - WinningNumbers winningNumbers = new WinningNumbers(LottoNumbersDtoGenerator.generateFixedNumberDto(sixNumbers, 9)); + WinningNumbers winningNumbers = new WinningNumbers(new SixLottoNumbersDTO(SixLottoNumbersFactory.createFixed(sixNumbers)), new BonusNumberDTO(LottoNumber.NINE)); Map lottoResults = user.confirmResult(winningNumbers); assertThat(lottoResults.get(LottoResult.FIRST)).isEqualTo(1); @@ -26,21 +29,21 @@ void test1() { assertThat(lottoResults.get(LottoResult.THIRD)).isEqualTo(2); assertThat(lottoResults.get(LottoResult.FOURTH)).isEqualTo(2); assertThat(lottoResults.get(LottoResult.FIFTH)).isEqualTo(1); - assertThat(lottoResults.get(LottoResult.NO_WIN)).isEqualTo(2); + assertThat(lottoResults.get(LottoResult.FAILED)).isEqualTo(2); } @Test @DisplayName("수익률 계산") void test2() { User user = new User(); - Money money = new Money(10000L); + Money money = new Money(10000); user.buyTicketsManually(money, createMyNumbers()); Set sixNumbers = createSixNumbersSet(3, 4, 5, 6, 7, 8); - WinningNumbers winningNumbers = new WinningNumbers(LottoNumbersDtoGenerator.generateFixedNumberDto(sixNumbers, 9)); + WinningNumbers winningNumbers = new WinningNumbers(new SixLottoNumbersDTO(SixLottoNumbersFactory.createFixed(sixNumbers)), new BonusNumberDTO(LottoNumber.NINE)); user.confirmResult(winningNumbers); - assertThat(user.calculateProfit().getProfit()).isEqualTo(20631050); + assertThat(user.calculateProfit().getValue()).isEqualTo(20631050); } private List> createMyNumbers() { @@ -57,6 +60,6 @@ private List> createMyNumbers() { } private Set createSixNumbersSet(int number1, int number2, int number3, int number4, int number5, int number6) { - return new HashSet(Arrays.asList(number1, number2, number3, number4, number5, number6)); + return new HashSet<>(Arrays.asList(number1, number2, number3, number4, number5, number6)); } } diff --git a/src/test/java/domain/numberscontainer/TicketTest.java b/src/test/java/domain/numberscontainer/TicketTest.java index fe2a36fd1f..5a7248fba0 100644 --- a/src/test/java/domain/numberscontainer/TicketTest.java +++ b/src/test/java/domain/numberscontainer/TicketTest.java @@ -1,8 +1,5 @@ package domain.numberscontainer; -import domain.LottoNumber; -import domain.numberscontainer.LottoNumbersDto; -import domain.numberscontainer.Ticket; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -17,8 +14,8 @@ public class TicketTest { @Test @DisplayName("티켓 생성") void ticketConstructor() { - LottoNumbersDto lottoNumbersDto = createLottoNumberDto(1, 2, 3, 4, 5, 5); - assertThatThrownBy(() -> new Ticket(lottoNumbersDto)) + SixLottoNumbersDTO sixLottoNumbersDTO = createLottoNumberDto(1, 2, 3, 4, 5, 5); + assertThatThrownBy(() -> new Ticket(sixLottoNumbersDTO)) .isInstanceOf(IllegalArgumentException.class) .hasMessage("6개의 숫자를 입력해주세요."); } @@ -26,13 +23,13 @@ void ticketConstructor() { @Test @DisplayName("두 티켓의 같은 숫자 비교") void compareTwoTickets() { - LottoNumbersDto lottoNumbersDto = createLottoNumberDto(1, 2, 3, 4, 5, 6); - Ticket ticket = new Ticket(lottoNumbersDto); + SixLottoNumbersDTO sixLottoNumbersDTO = createLottoNumberDto(1, 2, 3, 4, 5, 6); + Ticket ticket = new Ticket(sixLottoNumbersDTO); assertThat(ticket.contains(LottoNumber.THREE)).isTrue(); } - private LottoNumbersDto createLottoNumberDto(int number1, int number2, int number3, int number4, int number5, int number6) { - return new LottoNumbersDto(new HashSet<>(Arrays.asList(LottoNumber.getLottoNumber(number1), + private SixLottoNumbersDTO createLottoNumberDto(int number1, int number2, int number3, int number4, int number5, int number6) { + return new SixLottoNumbersDTO(new HashSet<>(Arrays.asList(LottoNumber.getLottoNumber(number1), LottoNumber.getLottoNumber(number2), LottoNumber.getLottoNumber(number3), LottoNumber.getLottoNumber(number4), diff --git a/src/test/java/domain/numberscontainer/WinningNumbersTest.java b/src/test/java/domain/numberscontainer/WinningNumbersTest.java index caede43220..6e2a6846c6 100644 --- a/src/test/java/domain/numberscontainer/WinningNumbersTest.java +++ b/src/test/java/domain/numberscontainer/WinningNumbersTest.java @@ -1,6 +1,5 @@ package domain.numberscontainer; -import domain.LottoNumber; import domain.LottoResult; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -19,54 +18,64 @@ public class WinningNumbersTest { @Test @DisplayName("당첨 번호 객체 생성") void winningNumberConstructor() { - LottoNumbersDto lottoNumbersDto = createLottoNumberDto(1, 2, 3, 4, 5, 5, 7); - assertThatThrownBy(() -> new WinningNumbers(lottoNumbersDto)) + SixLottoNumbersDTO sixLottoNumbersDTO = createSixNumbersDto(1, 2, 3, 4, 5, 5); + BonusNumberDTO bonusNumberDTO = new BonusNumberDTO(LottoNumber.SEVEN); + assertThatThrownBy(() -> new WinningNumbers(sixLottoNumbersDTO, bonusNumberDTO)) .isInstanceOf(IllegalArgumentException.class) .hasMessage("6개의 숫자를 입력해주세요."); } @ParameterizedTest - @ValueSource(ints = {-1, 1}) - @DisplayName("유효한 보너스 번호인지 검증") + @ValueSource(ints = {1, 5}) + @DisplayName("중복 없는 보너스 번호인지 검증") void validateBonusNumber(int input) { - LottoNumbersDto lottoNumbersDto = createLottoNumberDto(1, 2, 3, 4, 5, 6, input); - assertThatThrownBy(() -> new WinningNumbers(lottoNumbersDto)) + SixLottoNumbersDTO sixLottoNumbersDTO = createSixNumbersDto(1, 2, 3, 4, 5, 6); + BonusNumberDTO bonusNumberDTO = new BonusNumberDTO(LottoNumber.getLottoNumber(input)); + assertThatThrownBy(() -> new WinningNumbers(sixLottoNumbersDTO, bonusNumberDTO)) .isInstanceOf(IllegalArgumentException.class) .hasMessageEndingWith("보너스 번호를 입력해주세요."); } + @ParameterizedTest + @ValueSource(ints = {-1, 46}) + @DisplayName("1부터 45까지의 보너스 번호인지 검증") + void validateBonusNumber2(int input) { + assertThatThrownBy(() -> new BonusNumberDTO(LottoNumber.getLottoNumber(input))) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageEndingWith("1부터 45까지의 숫자를 입력해주세요."); + } + @Test @DisplayName("Ticket과 당첨 번호 비교") void findDuplicatedNumbers() { - LottoNumbersDto lottoNumbersDto1 = createLottoNumberDto(1, 2, 3, 4, 5, 6, 7); - LottoNumbersDto lottoNumbersDto2 = createLottoNumberDto(4, 5, 6, 7, 8, 9, 10); - WinningNumbers winningNumbers = new WinningNumbers(lottoNumbersDto1); - Ticket ticket = new Ticket(lottoNumbersDto2); + SixLottoNumbersDTO sixLottoNumbersDTO1 = createSixNumbersDto(1, 2, 3, 4, 5, 6); + BonusNumberDTO bonusNumberDTO = new BonusNumberDTO(LottoNumber.SEVEN); + SixLottoNumbersDTO sixLottoNumbersDTO2 = createSixNumbersDto(4, 5, 6, 7, 8, 9); + WinningNumbers winningNumbers = new WinningNumbers(sixLottoNumbersDTO1, bonusNumberDTO); + Ticket ticket = new Ticket(sixLottoNumbersDTO2); assertThat((winningNumbers.findDuplicatedNumbers(ticket))).isEqualTo(3); } - private LottoNumbersDto createLottoNumberDto(int number1, int number2, int number3, int number4, int number5, int number6, int number7) { + private SixLottoNumbersDTO createSixNumbersDto(int number1, int number2, int number3, int number4, int number5, int number6) { Set sixNumbers = new HashSet<>(Arrays.asList(LottoNumber.getLottoNumber(number1), LottoNumber.getLottoNumber(number2), LottoNumber.getLottoNumber(number3), LottoNumber.getLottoNumber(number4), LottoNumber.getLottoNumber(number5), LottoNumber.getLottoNumber(number6))); - LottoNumber bonusNumber = LottoNumber.getLottoNumber(number7); - - return new LottoNumbersDto(sixNumbers, bonusNumber); + return new SixLottoNumbersDTO(sixNumbers); } @Test @DisplayName("당첨 결과 확인") void getLottoResultTest() { - WinningNumbers winningNumbers = new WinningNumbers(createLottoNumberDto(3, 4, 5, 6, 7, 8, 9)); + WinningNumbers winningNumbers = new WinningNumbers(createSixNumbersDto(3, 4, 5, 6, 7, 8), new BonusNumberDTO(LottoNumber.NINE)); - assertThat(winningNumbers.getLottoResult(new Ticket(createLottoNumberDto(3, 4, 5, 6, 7, 8, -1)))).isEqualTo(LottoResult.FIRST); - assertThat(winningNumbers.getLottoResult(new Ticket(createLottoNumberDto(3, 4, 5, 6, 7, 9, -1)))).isEqualTo(LottoResult.SECOND); - assertThat(winningNumbers.getLottoResult(new Ticket(createLottoNumberDto(3, 4, 5, 6, 7, 10, -1)))).isEqualTo(LottoResult.THIRD); - assertThat(winningNumbers.getLottoResult(new Ticket(createLottoNumberDto(3, 4, 5, 6, 10, 11, -1)))).isEqualTo(LottoResult.FOURTH); - assertThat(winningNumbers.getLottoResult(new Ticket(createLottoNumberDto(3, 4, 5, 10, 11, 12, -1)))).isEqualTo(LottoResult.FIFTH); - assertThat(winningNumbers.getLottoResult(new Ticket(createLottoNumberDto(10, 11, 12, 13, 14, 15, -1)))).isEqualTo(LottoResult.NO_WIN); + assertThat(winningNumbers.getLottoResult(new Ticket(createSixNumbersDto(3, 4, 5, 6, 7, 8)))).isEqualTo(LottoResult.FIRST); + assertThat(winningNumbers.getLottoResult(new Ticket(createSixNumbersDto(3, 4, 5, 6, 7, 9)))).isEqualTo(LottoResult.SECOND); + assertThat(winningNumbers.getLottoResult(new Ticket(createSixNumbersDto(3, 4, 5, 6, 7, 10)))).isEqualTo(LottoResult.THIRD); + assertThat(winningNumbers.getLottoResult(new Ticket(createSixNumbersDto(3, 4, 5, 6, 10, 11)))).isEqualTo(LottoResult.FOURTH); + assertThat(winningNumbers.getLottoResult(new Ticket(createSixNumbersDto(3, 4, 5, 10, 11, 12)))).isEqualTo(LottoResult.FIFTH); + assertThat(winningNumbers.getLottoResult(new Ticket(createSixNumbersDto(10, 11, 12, 13, 14, 15)))).isEqualTo(LottoResult.FAILED); } } \ No newline at end of file diff --git a/src/test/java/util/LottoNumbersDtoGeneratorTest.java b/src/test/java/util/SixLottoNumbersFactoryTest.java similarity index 57% rename from src/test/java/util/LottoNumbersDtoGeneratorTest.java rename to src/test/java/util/SixLottoNumbersFactoryTest.java index 68ad51769a..781c74594c 100644 --- a/src/test/java/util/LottoNumbersDtoGeneratorTest.java +++ b/src/test/java/util/SixLottoNumbersFactoryTest.java @@ -1,6 +1,8 @@ package util; -import domain.LottoNumber; +import domain.numberscontainer.LottoNumber; +import domain.numberscontainer.BonusNumberDTO; +import domain.numberscontainer.SixLottoNumbersDTO; import domain.numberscontainer.Ticket; import domain.numberscontainer.WinningNumbers; import org.junit.jupiter.api.DisplayName; @@ -13,21 +15,21 @@ import static org.assertj.core.api.Assertions.assertThatCode; @DisplayName("Dto 생성 테스트") -class LottoNumbersDtoGeneratorTest { +class SixLottoNumbersFactoryTest { @Test @DisplayName("랜덤 티켓 생성") void generateRandomTicket() { - LottoNumbersDtoGenerator lottoNumbersDtoGenerator = new LottoNumbersDtoGenerator(); - assertThatCode(() -> new Ticket(lottoNumbersDtoGenerator.generateRandomTicketDto())) + SixLottoNumbersFactory sixLottoNumbersFactory = new SixLottoNumbersFactory(); + assertThatCode(() -> new Ticket(new SixLottoNumbersDTO(sixLottoNumbersFactory.createRandom()))) .doesNotThrowAnyException(); } @Test @DisplayName("고정값 당첨 번호 생성") void generateFixedWinningNumber() { - LottoNumbersDtoGenerator lottoNumbersDtoGenerator = new LottoNumbersDtoGenerator(); - assertThatCode(() -> new WinningNumbers(lottoNumbersDtoGenerator.generateFixedNumberDto(createSixNumbers(1, 2, 3, 4, 5, 6), 7))) + SixLottoNumbersFactory sixLottoNumbersFactory = new SixLottoNumbersFactory(); + assertThatCode(() -> new WinningNumbers(new SixLottoNumbersDTO(sixLottoNumbersFactory.createFixed(createSixNumbers(1, 2, 3, 4, 5, 6))), new BonusNumberDTO(LottoNumber.SEVEN))) .doesNotThrowAnyException(); } From 15292ad3da6f0e2d23bc56aee3a355c2a893a9fc Mon Sep 17 00:00:00 2001 From: YebinK Date: Mon, 24 Feb 2020 19:23:35 +0900 Subject: [PATCH 21/23] =?UTF-8?q?[refactor]=20=ED=8B=B0=EC=BC=93=20?= =?UTF-8?q?=EC=83=9D=EC=84=B1=20=EB=A1=9C=EC=A7=81=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/LottoApplication.java | 20 +++--- src/main/java/domain/LottoProfit.java | 20 +++--- src/main/java/domain/LottoResult.java | 32 +++------ src/main/java/domain/LottoResultMachine.java | 21 ++++++ src/main/java/domain/LottoStore.java | 26 ++++---- src/main/java/domain/User.java | 51 --------------- .../numberscontainer/BonusNumberDTO.java | 2 +- .../java/domain/numberscontainer/Ticket.java | 6 +- .../RandomTicketFactory.java} | 19 ++---- src/main/java/strategy/TicketFactory.java | 8 +++ src/main/java/view/InputView.java | 17 ++--- src/main/java/view/OutputView.java | 38 +++++++---- .../java/domain/LottoResultMachineTest.java | 65 +++++++++++++++++++ src/test/java/domain/UserTest.java | 65 ------------------- .../numberscontainer/WinningNumbersTest.java | 20 +++--- .../strategy/RandomTicketFactoryTest.java | 26 ++++++++ .../java/util/SixLottoNumbersFactoryTest.java | 39 ----------- 17 files changed, 217 insertions(+), 258 deletions(-) create mode 100644 src/main/java/domain/LottoResultMachine.java delete mode 100644 src/main/java/domain/User.java rename src/main/java/{util/SixLottoNumbersFactory.java => strategy/RandomTicketFactory.java} (52%) create mode 100644 src/main/java/strategy/TicketFactory.java create mode 100644 src/test/java/domain/LottoResultMachineTest.java delete mode 100644 src/test/java/domain/UserTest.java create mode 100644 src/test/java/strategy/RandomTicketFactoryTest.java delete mode 100644 src/test/java/util/SixLottoNumbersFactoryTest.java diff --git a/src/main/java/LottoApplication.java b/src/main/java/LottoApplication.java index b5db4bf573..07b9a7564c 100644 --- a/src/main/java/LottoApplication.java +++ b/src/main/java/LottoApplication.java @@ -1,23 +1,27 @@ -import domain.Money; -import domain.User; +import domain.*; import domain.numberscontainer.BonusNumberDTO; import domain.numberscontainer.SixLottoNumbersDTO; +import domain.numberscontainer.Ticket; import domain.numberscontainer.WinningNumbers; import view.InputView; import view.OutputView; +import java.util.List; +import java.util.Map; + public class LottoApplication { public static void main(String[] args) { - User user = new User(); - user.buyTickets(enterMoney()); + Money money = enterMoney(); + List tickets = LottoStore.generateTickets(money.getNumberOfTickets()); - OutputView.printNumberOfTickets(user.getTicketsSize()); - OutputView.printTickets(user.getTickets()); + OutputView.printNumberOfTickets(tickets.size()); + OutputView.printTickets(tickets); WinningNumbers winningNumbers = enterWinningNumbers(); - OutputView.printLottoResults(user.confirmResult(winningNumbers)); - OutputView.printProfit(user.calculateProfit()); + Map result = LottoResultMachine.confirmResult(tickets, winningNumbers); + OutputView.printLottoResults(result); + OutputView.printProfit(LottoProfit.ofProfit(result, money)); } private static Money enterMoney() { diff --git a/src/main/java/domain/LottoProfit.java b/src/main/java/domain/LottoProfit.java index 3c55d3e513..92737e088a 100644 --- a/src/main/java/domain/LottoProfit.java +++ b/src/main/java/domain/LottoProfit.java @@ -4,21 +4,25 @@ public class LottoProfit { public static final int TO_PERCENTAGE = 100; - private double profit; - private LottoProfit(double profit) { + private int profit; + + private LottoProfit(int profit) { this.profit = profit; } - public static LottoProfit getProfit(Map lottoResults, Money money) { - Long totalPrize = lottoResults.keySet().stream() - .mapToLong(object -> object.getPrize() * lottoResults.get(object)) - .sum(); + public static LottoProfit ofProfit(Map lottoResults, Money money) { + int totalPrize = 0; + for (LottoResult result : lottoResults.keySet()) { + int key = result.getPrize(); + int value = (int) (long) lottoResults.get(result); + totalPrize += key * value; + } return new LottoProfit(totalPrize / money.getMoney() * TO_PERCENTAGE); } - public double getValue() { + public int getValue() { return profit; } -} +} \ No newline at end of file diff --git a/src/main/java/domain/LottoResult.java b/src/main/java/domain/LottoResult.java index 34eee8e4bb..90869554d5 100644 --- a/src/main/java/domain/LottoResult.java +++ b/src/main/java/domain/LottoResult.java @@ -4,12 +4,12 @@ import java.util.List; public enum LottoResult { - FAILED(0, -1), - FIRST(2000000000, 6), - SECOND(30000000, 5), - THIRD(1500000, 5), - FOURTH(50000, 4), - FIFTH(5000, 3); + FAILED(0, 0), + FIRST(2_000_000_000, 6), + SECOND(30_000_000, 5), + THIRD(1_500_000, 5), + FOURTH(50_000, 4), + FIFTH(5_000, 3); private final int prize; private final int matchCount; @@ -27,7 +27,7 @@ public static LottoResult findLottoResult(int matchCount, boolean isBonus) { .orElse(FAILED); if (lottoResult == SECOND && !isBonus) { - lottoResult = THIRD; + return THIRD; } return lottoResult; } @@ -36,21 +36,7 @@ public int getPrize() { return prize; } - @Override - public String toString() { - if (this == FAILED) { - return ""; - } - StringBuilder stringBuilder = new StringBuilder(); - stringBuilder.append(this.matchCount); - stringBuilder.append("개 일치 "); - if (this == SECOND) { - stringBuilder.append(", 보너스 볼 일치 "); - } - stringBuilder.append("("); - stringBuilder.append(this.prize); - stringBuilder.append("원) - "); - - return stringBuilder.toString(); + public int getMatchCount() { + return matchCount; } } \ No newline at end of file diff --git a/src/main/java/domain/LottoResultMachine.java b/src/main/java/domain/LottoResultMachine.java new file mode 100644 index 0000000000..fb17c5dc29 --- /dev/null +++ b/src/main/java/domain/LottoResultMachine.java @@ -0,0 +1,21 @@ +package domain; + +import domain.numberscontainer.Ticket; +import domain.numberscontainer.WinningNumbers; + +import java.util.List; +import java.util.Map; +import java.util.stream.Collector; +import java.util.stream.Collectors; + +public class LottoResultMachine { + + public static Map confirmResult(List tickets, WinningNumbers winningNumbers) { + return tickets.stream() + .collect(Collectors.groupingBy(winningNumbers::getLottoResult, countingLottoResult())); + } + + private static Collector countingLottoResult() { + return Collectors.reducing(0, e -> 1, Integer::sum); + } +} \ No newline at end of file diff --git a/src/main/java/domain/LottoStore.java b/src/main/java/domain/LottoStore.java index 9999ab31fd..de7ae67994 100644 --- a/src/main/java/domain/LottoStore.java +++ b/src/main/java/domain/LottoStore.java @@ -2,38 +2,38 @@ import domain.numberscontainer.SixLottoNumbersDTO; import domain.numberscontainer.Ticket; -import util.SixLottoNumbersFactory; +import strategy.RandomTicketFactory; import java.util.ArrayList; import java.util.List; -import java.util.Set; import java.util.stream.Collectors; +/* + * Ticket 리스트 생성 + */ public class LottoStore { + public static List generateTickets(int ticketSize) { List tickets = new ArrayList<>(); for (int i = 0; i < ticketSize; i++) { - tickets.add(new Ticket(new SixLottoNumbersDTO(SixLottoNumbersFactory.createRandom()))); + tickets.add(RandomTicketFactory.createTicket()); } return tickets; } - public static List generateTickets(int ticketSize, List> givenNumbers) { + public static List generateTickets(int ticketSize, List givenNumbers) { List tickets = givenNumbers.stream() - .map(SixLottoNumbersFactory::createFixed) - .map(SixLottoNumbersDTO::new) .map(Ticket::new) .collect(Collectors.toList()); - int randomTicketsSize = getRandomTicketsSize(ticketSize, givenNumbers); - for (int i = 0; i < randomTicketsSize; i++) { - tickets.add(new Ticket(new SixLottoNumbersDTO(SixLottoNumbersFactory.createRandom()))); - } + int randomTicketsSize = getRandomTicketSize(ticketSize, givenNumbers.size()); + tickets.addAll(generateTickets(randomTicketsSize)); + return tickets; } - private static int getRandomTicketsSize(int number, List> givenNumbers) { - return number - givenNumbers.size(); + private static int getRandomTicketSize(int totalTicketSize, int manualTicketSize) { + return totalTicketSize - manualTicketSize; } -} +} \ No newline at end of file diff --git a/src/main/java/domain/User.java b/src/main/java/domain/User.java deleted file mode 100644 index 46358115bf..0000000000 --- a/src/main/java/domain/User.java +++ /dev/null @@ -1,51 +0,0 @@ -package domain; - -import domain.numberscontainer.Ticket; -import domain.numberscontainer.WinningNumbers; - -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.stream.Collectors; - -public class User { - - private Money spentMoney; - private List tickets; - private Map lottoResults; - - public User() { - this.spentMoney = new Money(0); - this.tickets = new ArrayList<>(); - } - - public void buyTickets(Money money) { - //this.spentMoney = spentMoney.getMoney() + money; - this.spentMoney = money; - this.tickets = LottoStore.generateTickets(money.getNumberOfTickets()); - } - - public void buyTicketsManually(Money money, List> fixedNumbers) { - this.spentMoney = money; - this.tickets = LottoStore.generateTickets(money.getNumberOfTickets(), fixedNumbers); - } - - public Map confirmResult(WinningNumbers winningNumbers) { - this.lottoResults = tickets.stream() - .collect(Collectors.groupingBy(ticket -> winningNumbers.getLottoResult(ticket), Collectors.counting())); - return lottoResults; - } - - public LottoProfit calculateProfit() { - return LottoProfit.getProfit(this.lottoResults, this.spentMoney); - } - - public List getTickets() { - return tickets; - } - - public int getTicketsSize() { - return tickets.size(); - } -} \ No newline at end of file diff --git a/src/main/java/domain/numberscontainer/BonusNumberDTO.java b/src/main/java/domain/numberscontainer/BonusNumberDTO.java index 78401a8a1b..cdeb53719f 100644 --- a/src/main/java/domain/numberscontainer/BonusNumberDTO.java +++ b/src/main/java/domain/numberscontainer/BonusNumberDTO.java @@ -10,4 +10,4 @@ public BonusNumberDTO(LottoNumber bonusNumber) { public LottoNumber getBonusNumber() { return bonusNumber; } -} +} \ No newline at end of file diff --git a/src/main/java/domain/numberscontainer/Ticket.java b/src/main/java/domain/numberscontainer/Ticket.java index 1aa5533d89..098d2a0261 100644 --- a/src/main/java/domain/numberscontainer/Ticket.java +++ b/src/main/java/domain/numberscontainer/Ticket.java @@ -14,12 +14,8 @@ public boolean contains(LottoNumber number) { @Override public String toString() { - StringBuilder stringBuilder = new StringBuilder(); - String numbers = this.sixLottoNumbers.stream() + return this.sixLottoNumbers.stream() .map(lottoNumber -> Integer.toString(lottoNumber.getValue())) .collect(Collectors.joining(", ", "[", "]")); - - stringBuilder.append(numbers); - return stringBuilder.toString(); } } \ No newline at end of file diff --git a/src/main/java/util/SixLottoNumbersFactory.java b/src/main/java/strategy/RandomTicketFactory.java similarity index 52% rename from src/main/java/util/SixLottoNumbersFactory.java rename to src/main/java/strategy/RandomTicketFactory.java index f55273b507..62c59a18e8 100644 --- a/src/main/java/util/SixLottoNumbersFactory.java +++ b/src/main/java/strategy/RandomTicketFactory.java @@ -1,18 +1,18 @@ -package util; +package strategy; import domain.numberscontainer.LottoNumber; +import domain.numberscontainer.SixLottoNumbersDTO; +import domain.numberscontainer.Ticket; import java.util.*; -import java.util.stream.Collectors; -public class SixLottoNumbersFactory { +public class RandomTicketFactory { private static final int FIRST_INDEX = 0; private static final int SIXTH_INDEX = 6; - public static Set createRandom() { + public static Ticket createTicket() { Set sixNumbers = new HashSet<>(getShuffledList().subList(FIRST_INDEX, SIXTH_INDEX)); - - return sixNumbers; + return new Ticket(new SixLottoNumbersDTO(sixNumbers)); } private static List getShuffledList() { @@ -21,11 +21,4 @@ private static List getShuffledList() { return lottoNumbers; } - - public static Set createFixed(Set givenNumbers) { - Set sixNumbers = givenNumbers.stream() - .map(LottoNumber::getLottoNumber) - .collect(Collectors.toSet()); - return sixNumbers; - } } \ No newline at end of file diff --git a/src/main/java/strategy/TicketFactory.java b/src/main/java/strategy/TicketFactory.java new file mode 100644 index 0000000000..cae7440f77 --- /dev/null +++ b/src/main/java/strategy/TicketFactory.java @@ -0,0 +1,8 @@ +package strategy; + +import domain.numberscontainer.Ticket; + +public interface TicketFactory { + + Ticket createTicket(); +} \ No newline at end of file diff --git a/src/main/java/view/InputView.java b/src/main/java/view/InputView.java index cc3bc2b004..27df95f450 100644 --- a/src/main/java/view/InputView.java +++ b/src/main/java/view/InputView.java @@ -4,7 +4,7 @@ import domain.Money; import domain.numberscontainer.BonusNumberDTO; import domain.numberscontainer.SixLottoNumbersDTO; -import util.SixLottoNumbersFactory; +import strategy.TicketFactory; import java.util.*; import java.util.stream.Collectors; @@ -33,16 +33,17 @@ private static void validateNumber(String input) { public static SixLottoNumbersDTO enterLastWeekWinningNumbers() { System.out.println("지난 주 당첨 번호를 입력해 주세요."); - Set lastWeekWinningNumbers = parseWinningNumbers(scanner.nextLine()); - return new SixLottoNumbersDTO(SixLottoNumbersFactory.createFixed(lastWeekWinningNumbers)); + SixLottoNumbersDTO lastWeekWinningNumbers = parseWinningNumbers(scanner.nextLine()); + return lastWeekWinningNumbers; } - private static Set parseWinningNumbers(String input) { - List lottoNumbers = Arrays.asList(input.split(",")).stream() + private static SixLottoNumbersDTO parseWinningNumbers(String input) { + Set lottoNumbers = Arrays.asList(input.split(",")).stream() .map(String::trim) - .map(InputView::parseInt) - .collect(Collectors.toList()); - return new HashSet<>(lottoNumbers); + .map(Integer::new) + .map(LottoNumber::getLottoNumber) + .collect(Collectors.toSet()); + return new SixLottoNumbersDTO(new HashSet<>(lottoNumbers)); } public static BonusNumberDTO enterBonusNumber() { diff --git a/src/main/java/view/OutputView.java b/src/main/java/view/OutputView.java index 263a99f75a..0da03a5f56 100644 --- a/src/main/java/view/OutputView.java +++ b/src/main/java/view/OutputView.java @@ -4,11 +4,15 @@ import domain.LottoResult; import domain.numberscontainer.Ticket; -import java.util.Arrays; import java.util.List; import java.util.Map; public class OutputView { + public static final String LOTTO_RESULT_MESSAGE = "%d개 일치 (%d원)- %d개"; + public static final String LOTTO_RESULT_MESSAGE_FOR_BONUS = "%d개 일치, 보너스 볼 일치 (%d원)- %d개"; + public static final String LOTTO_RESULT_TITLE = "당첨 통계"; + public static final String LOTTO_RESULT_SEPARATOR = "---------------"; + public static final String LOTTO_PROFIT_MESSAGE = "총 수익률은 %d%%입니다."; public static void printNumberOfTickets(int size) { System.out.println(size + "개를 구매했습니다."); @@ -23,20 +27,26 @@ public static void printTickets(List tickets) { System.out.println(stringBuilder.toString()); } - public static void printLottoResults(Map lottoResults) { - StringBuilder stringBuilder = new StringBuilder(); - stringBuilder.append("당첨 통계\n-------------\n"); - List results = Arrays.asList(LottoResult.values()).subList(1, LottoResult.values().length); - results.stream() - .forEach(result -> { - stringBuilder.append(result.toString()); - stringBuilder.append(convertNullToZero(lottoResults.get(result))); - stringBuilder.append("개\n"); - }); - System.out.println(stringBuilder.toString()); + public static void printLottoResults(Map lottoResults) { + System.out.println(LOTTO_RESULT_TITLE); + System.lineSeparator(); + System.out.println(LOTTO_RESULT_SEPARATOR); + System.lineSeparator(); + + for (LottoResult result : LottoResult.values()) { + System.out.println(String.format(findProperFormat(result), result.getMatchCount(), result.getPrize(), convertNullToZero(lottoResults.get(result)))); + } + System.lineSeparator(); + } + + private static String findProperFormat(LottoResult result) { + if (result == LottoResult.SECOND) { + return LOTTO_RESULT_MESSAGE_FOR_BONUS; + } + return LOTTO_RESULT_MESSAGE; } - private static long convertNullToZero(Long number) { + private static int convertNullToZero(Integer number) { if (number == null) { return 0; } @@ -44,6 +54,6 @@ private static long convertNullToZero(Long number) { } public static void printProfit(LottoProfit profit) { - System.out.println("총 수익률은 " + (long)profit.getValue() + "%입니다."); + System.out.println(String.format(LOTTO_PROFIT_MESSAGE, profit.getValue())); } } \ No newline at end of file diff --git a/src/test/java/domain/LottoResultMachineTest.java b/src/test/java/domain/LottoResultMachineTest.java new file mode 100644 index 0000000000..cbf33a6384 --- /dev/null +++ b/src/test/java/domain/LottoResultMachineTest.java @@ -0,0 +1,65 @@ +package domain; + +import domain.numberscontainer.*; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import strategy.TicketFactory; + +import java.util.*; + +import static org.assertj.core.api.Assertions.assertThat; + +public class LottoResultMachineTest { + @Test + @DisplayName("당첨 결과 계산") + void test1() { + Money money = new Money(10000); + List tickets = LottoStore.generateTickets(money.getNumberOfTickets(), createFixedNumbersList()); + + SixLottoNumbersDTO sixNumbers = createFixedNumbers(3, 4, 5, 6, 7, 8); + WinningNumbers winningNumbers = new WinningNumbers(sixNumbers, new BonusNumberDTO(LottoNumber.NINE)); + Map lottoResults = LottoResultMachine.confirmResult(tickets, winningNumbers); + + assertThat(lottoResults.get(LottoResult.FIRST)).isEqualTo(1); + assertThat(lottoResults.get(LottoResult.SECOND)).isEqualTo(2); + assertThat(lottoResults.get(LottoResult.THIRD)).isEqualTo(2); + assertThat(lottoResults.get(LottoResult.FOURTH)).isEqualTo(2); + assertThat(lottoResults.get(LottoResult.FIFTH)).isEqualTo(1); + assertThat(lottoResults.get(LottoResult.FAILED)).isEqualTo(2); + } + + @Test + @DisplayName("수익률 계산") + void test2() { + Money money = new Money(10000); + List tickets = LottoStore.generateTickets(money.getNumberOfTickets(), createFixedNumbersList()); + + SixLottoNumbersDTO sixNumbers = createFixedNumbers(3, 4, 5, 6, 7, 8); + WinningNumbers winningNumbers = new WinningNumbers(sixNumbers, new BonusNumberDTO(LottoNumber.NINE)); + Map lottoResults = LottoResultMachine.confirmResult(tickets, winningNumbers); + LottoProfit profit = LottoProfit.ofProfit(lottoResults, money); + assertThat(profit.getValue()).isEqualTo(20631000); + } + + private List createFixedNumbersList() { + return Arrays.asList(createFixedNumbers(3, 4, 5, 6, 7, 8), //1등 + createFixedNumbers(3, 4, 5, 6, 7, 9), //2등 + createFixedNumbers(3, 4, 5, 6, 7, 10), //3등 + createFixedNumbers(3, 4, 5, 6, 10, 11), //4등 + createFixedNumbers(3, 4, 5, 10, 11, 12), //5등 + createFixedNumbers(4, 5, 6, 7, 8, 9), //2등 + createFixedNumbers(4, 5, 6, 7, 8, 10), //3등 + createFixedNumbers(4, 5, 6, 7, 10, 11), //4등 + createFixedNumbers(10, 11, 12, 13, 14, 15), //당첨 x + createFixedNumbers(16, 17, 18, 19, 20, 21)); //당첨 x + } + + private SixLottoNumbersDTO createFixedNumbers(int number1, int number2, int number3, int number4, int number5, int number6) { + return new SixLottoNumbersDTO(new HashSet<>(Arrays.asList(LottoNumber.getLottoNumber(number1), + LottoNumber.getLottoNumber(number2), + LottoNumber.getLottoNumber(number3), + LottoNumber.getLottoNumber(number4), + LottoNumber.getLottoNumber(number5), + LottoNumber.getLottoNumber(number6)))); + } +} diff --git a/src/test/java/domain/UserTest.java b/src/test/java/domain/UserTest.java deleted file mode 100644 index ae8904ab1f..0000000000 --- a/src/test/java/domain/UserTest.java +++ /dev/null @@ -1,65 +0,0 @@ -package domain; - -import domain.numberscontainer.BonusNumberDTO; -import domain.numberscontainer.LottoNumber; -import domain.numberscontainer.SixLottoNumbersDTO; -import domain.numberscontainer.WinningNumbers; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; -import util.SixLottoNumbersFactory; - -import java.util.*; - -import static org.assertj.core.api.Assertions.assertThat; - -public class UserTest { - @Test - @DisplayName("당첨 결과 계산") - void test1() { - User user = new User(); - Money money = new Money(10000); - user.buyTicketsManually(money, createMyNumbers()); - - Set sixNumbers = createSixNumbersSet(3, 4, 5, 6, 7, 8); - WinningNumbers winningNumbers = new WinningNumbers(new SixLottoNumbersDTO(SixLottoNumbersFactory.createFixed(sixNumbers)), new BonusNumberDTO(LottoNumber.NINE)); - Map lottoResults = user.confirmResult(winningNumbers); - - assertThat(lottoResults.get(LottoResult.FIRST)).isEqualTo(1); - assertThat(lottoResults.get(LottoResult.SECOND)).isEqualTo(2); - assertThat(lottoResults.get(LottoResult.THIRD)).isEqualTo(2); - assertThat(lottoResults.get(LottoResult.FOURTH)).isEqualTo(2); - assertThat(lottoResults.get(LottoResult.FIFTH)).isEqualTo(1); - assertThat(lottoResults.get(LottoResult.FAILED)).isEqualTo(2); - } - - @Test - @DisplayName("수익률 계산") - void test2() { - User user = new User(); - Money money = new Money(10000); - user.buyTicketsManually(money, createMyNumbers()); - - Set sixNumbers = createSixNumbersSet(3, 4, 5, 6, 7, 8); - WinningNumbers winningNumbers = new WinningNumbers(new SixLottoNumbersDTO(SixLottoNumbersFactory.createFixed(sixNumbers)), new BonusNumberDTO(LottoNumber.NINE)); - user.confirmResult(winningNumbers); - - assertThat(user.calculateProfit().getValue()).isEqualTo(20631050); - } - - private List> createMyNumbers() { - return Arrays.asList(createSixNumbersSet(3, 4, 5, 6, 7, 8), - createSixNumbersSet(3, 4, 5, 6, 7, 9), - createSixNumbersSet(3, 4, 5, 6, 7, 10), - createSixNumbersSet(3, 4, 5, 6, 10, 11), - createSixNumbersSet(3, 4, 5, 10, 11, 12), - createSixNumbersSet(4, 5, 6, 7, 8, 9), - createSixNumbersSet(4, 5, 6, 7, 8, 10), - createSixNumbersSet(4, 5, 6, 7, 10, 11), - createSixNumbersSet(10, 11, 12, 13, 14, 15), - createSixNumbersSet(16, 17, 18, 19, 20, 21)); - } - - private Set createSixNumbersSet(int number1, int number2, int number3, int number4, int number5, int number6) { - return new HashSet<>(Arrays.asList(number1, number2, number3, number4, number5, number6)); - } -} diff --git a/src/test/java/domain/numberscontainer/WinningNumbersTest.java b/src/test/java/domain/numberscontainer/WinningNumbersTest.java index 6e2a6846c6..3da1a25fac 100644 --- a/src/test/java/domain/numberscontainer/WinningNumbersTest.java +++ b/src/test/java/domain/numberscontainer/WinningNumbersTest.java @@ -56,16 +56,6 @@ void findDuplicatedNumbers() { assertThat((winningNumbers.findDuplicatedNumbers(ticket))).isEqualTo(3); } - private SixLottoNumbersDTO createSixNumbersDto(int number1, int number2, int number3, int number4, int number5, int number6) { - Set sixNumbers = new HashSet<>(Arrays.asList(LottoNumber.getLottoNumber(number1), - LottoNumber.getLottoNumber(number2), - LottoNumber.getLottoNumber(number3), - LottoNumber.getLottoNumber(number4), - LottoNumber.getLottoNumber(number5), - LottoNumber.getLottoNumber(number6))); - return new SixLottoNumbersDTO(sixNumbers); - } - @Test @DisplayName("당첨 결과 확인") void getLottoResultTest() { @@ -78,4 +68,14 @@ void getLottoResultTest() { assertThat(winningNumbers.getLottoResult(new Ticket(createSixNumbersDto(3, 4, 5, 10, 11, 12)))).isEqualTo(LottoResult.FIFTH); assertThat(winningNumbers.getLottoResult(new Ticket(createSixNumbersDto(10, 11, 12, 13, 14, 15)))).isEqualTo(LottoResult.FAILED); } + + private SixLottoNumbersDTO createSixNumbersDto(int number1, int number2, int number3, int number4, int number5, int number6) { + Set sixNumbers = new HashSet<>(Arrays.asList(LottoNumber.getLottoNumber(number1), + LottoNumber.getLottoNumber(number2), + LottoNumber.getLottoNumber(number3), + LottoNumber.getLottoNumber(number4), + LottoNumber.getLottoNumber(number5), + LottoNumber.getLottoNumber(number6))); + return new SixLottoNumbersDTO(sixNumbers); + } } \ No newline at end of file diff --git a/src/test/java/strategy/RandomTicketFactoryTest.java b/src/test/java/strategy/RandomTicketFactoryTest.java new file mode 100644 index 0000000000..770166f551 --- /dev/null +++ b/src/test/java/strategy/RandomTicketFactoryTest.java @@ -0,0 +1,26 @@ +package strategy; + +import domain.numberscontainer.LottoNumber; +import domain.numberscontainer.BonusNumberDTO; +import domain.numberscontainer.SixLottoNumbersDTO; +import domain.numberscontainer.Ticket; +import domain.numberscontainer.WinningNumbers; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + +import static org.assertj.core.api.Assertions.assertThatCode; + +@DisplayName("티켓 생성 테스트") +class RandomTicketFactoryTest { + + @Test + @DisplayName("랜덤 티켓 생성") + void generateRandomTicket() { + assertThatCode(() -> RandomTicketFactory.createTicket()) + .doesNotThrowAnyException(); + } +} \ No newline at end of file diff --git a/src/test/java/util/SixLottoNumbersFactoryTest.java b/src/test/java/util/SixLottoNumbersFactoryTest.java deleted file mode 100644 index 781c74594c..0000000000 --- a/src/test/java/util/SixLottoNumbersFactoryTest.java +++ /dev/null @@ -1,39 +0,0 @@ -package util; - -import domain.numberscontainer.LottoNumber; -import domain.numberscontainer.BonusNumberDTO; -import domain.numberscontainer.SixLottoNumbersDTO; -import domain.numberscontainer.Ticket; -import domain.numberscontainer.WinningNumbers; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; - -import java.util.Arrays; -import java.util.HashSet; -import java.util.Set; - -import static org.assertj.core.api.Assertions.assertThatCode; - -@DisplayName("Dto 생성 테스트") -class SixLottoNumbersFactoryTest { - - @Test - @DisplayName("랜덤 티켓 생성") - void generateRandomTicket() { - SixLottoNumbersFactory sixLottoNumbersFactory = new SixLottoNumbersFactory(); - assertThatCode(() -> new Ticket(new SixLottoNumbersDTO(sixLottoNumbersFactory.createRandom()))) - .doesNotThrowAnyException(); - } - - @Test - @DisplayName("고정값 당첨 번호 생성") - void generateFixedWinningNumber() { - SixLottoNumbersFactory sixLottoNumbersFactory = new SixLottoNumbersFactory(); - assertThatCode(() -> new WinningNumbers(new SixLottoNumbersDTO(sixLottoNumbersFactory.createFixed(createSixNumbers(1, 2, 3, 4, 5, 6))), new BonusNumberDTO(LottoNumber.SEVEN))) - .doesNotThrowAnyException(); - } - - private Set createSixNumbers(int number1, int number2, int number3, int number4, int number5, int number6) { - return new HashSet<>(Arrays.asList(number1, number2, number3, number4, number5, number6)); - } -} \ No newline at end of file From 5081b5ce453daf96b51968c9f3cfb4aba9cb3e87 Mon Sep 17 00:00:00 2001 From: YebinK Date: Tue, 25 Feb 2020 16:49:31 +0900 Subject: [PATCH 22/23] =?UTF-8?q?[refactor]=20=EA=B2=80=EC=A6=9D=20?= =?UTF-8?q?=EB=A1=9C=EC=A7=81=20=EB=8F=84=EB=A9=94=EC=9D=B8=EC=9C=BC?= =?UTF-8?q?=EB=A1=9C=20=EC=9D=B4=EB=8F=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/LottoApplication.java | 10 ++--- src/main/java/domain/LottoProfit.java | 17 ++++--- src/main/java/domain/LottoResult.java | 4 +- src/main/java/domain/LottoResultMachine.java | 7 +-- src/main/java/domain/LottoStore.java | 1 - src/main/java/domain/Money.java | 27 +++++++----- .../RandomTicketFactory.java | 9 ++-- .../numberscontainer/BonusNumberDTO.java | 17 ++++++- .../numberscontainer/SixLottoNumbersDTO.java | 15 +++++++ src/main/java/strategy/TicketFactory.java | 8 ---- src/main/java/view/InputView.java | 44 +++---------------- src/main/java/view/OutputView.java | 4 +- .../java/domain/LottoResultMachineTest.java | 16 +++---- src/test/java/domain/MoneyTest.java | 9 ++-- .../domain/numberscontainer/TicketTest.java | 7 +-- .../numberscontainer/WinningNumbersTest.java | 28 +++++------- .../strategy/RandomTicketFactoryTest.java | 10 +---- 17 files changed, 94 insertions(+), 139 deletions(-) rename src/main/java/{strategy => domain}/RandomTicketFactory.java (62%) delete mode 100644 src/main/java/strategy/TicketFactory.java diff --git a/src/main/java/LottoApplication.java b/src/main/java/LottoApplication.java index 07b9a7564c..63e063e7a9 100644 --- a/src/main/java/LottoApplication.java +++ b/src/main/java/LottoApplication.java @@ -10,11 +10,9 @@ import java.util.Map; public class LottoApplication { - public static void main(String[] args) { - Money money = enterMoney(); + Money money = new Money(enterMoney()); List tickets = LottoStore.generateTickets(money.getNumberOfTickets()); - OutputView.printNumberOfTickets(tickets.size()); OutputView.printTickets(tickets); @@ -24,7 +22,7 @@ public static void main(String[] args) { OutputView.printProfit(LottoProfit.ofProfit(result, money)); } - private static Money enterMoney() { + private static String enterMoney() { try { return InputView.enterMoney(); } catch (Exception e) { @@ -35,8 +33,8 @@ private static Money enterMoney() { private static WinningNumbers enterWinningNumbers() { try { - SixLottoNumbersDTO sixLottoNumbersDTO = InputView.enterLastWeekWinningNumbers(); - BonusNumberDTO bonusNumberDTO = InputView.enterBonusNumber(); + SixLottoNumbersDTO sixLottoNumbersDTO = new SixLottoNumbersDTO(InputView.enterLastWeekWinningNumbers()); + BonusNumberDTO bonusNumberDTO = new BonusNumberDTO(InputView.enterBonusNumber()); return new WinningNumbers(sixLottoNumbersDTO, bonusNumberDTO); } catch (IllegalArgumentException e) { System.out.println(e.getMessage()); diff --git a/src/main/java/domain/LottoProfit.java b/src/main/java/domain/LottoProfit.java index 92737e088a..0bbf01b766 100644 --- a/src/main/java/domain/LottoProfit.java +++ b/src/main/java/domain/LottoProfit.java @@ -3,26 +3,25 @@ import java.util.Map; public class LottoProfit { - public static final int TO_PERCENTAGE = 100; + private static final int TO_PERCENTAGE = 100; - private int profit; + private long profit; - private LottoProfit(int profit) { + private LottoProfit(long profit) { this.profit = profit; } public static LottoProfit ofProfit(Map lottoResults, Money money) { - int totalPrize = 0; + long totalPrize = 0; for (LottoResult result : lottoResults.keySet()) { - int key = result.getPrize(); - int value = (int) (long) lottoResults.get(result); - totalPrize += key * value; + long prize = result.getPrize(); + long matchCount = lottoResults.get(result); + totalPrize += prize * matchCount; } - return new LottoProfit(totalPrize / money.getMoney() * TO_PERCENTAGE); } - public int getValue() { + public long getValue() { return profit; } } \ No newline at end of file diff --git a/src/main/java/domain/LottoResult.java b/src/main/java/domain/LottoResult.java index 90869554d5..fbfc610d87 100644 --- a/src/main/java/domain/LottoResult.java +++ b/src/main/java/domain/LottoResult.java @@ -4,12 +4,12 @@ import java.util.List; public enum LottoResult { - FAILED(0, 0), FIRST(2_000_000_000, 6), SECOND(30_000_000, 5), THIRD(1_500_000, 5), FOURTH(50_000, 4), - FIFTH(5_000, 3); + FIFTH(5_000, 3), + FAILED(0, 0); private final int prize; private final int matchCount; diff --git a/src/main/java/domain/LottoResultMachine.java b/src/main/java/domain/LottoResultMachine.java index fb17c5dc29..ca913f4678 100644 --- a/src/main/java/domain/LottoResultMachine.java +++ b/src/main/java/domain/LottoResultMachine.java @@ -5,17 +5,12 @@ import java.util.List; import java.util.Map; -import java.util.stream.Collector; import java.util.stream.Collectors; public class LottoResultMachine { public static Map confirmResult(List tickets, WinningNumbers winningNumbers) { return tickets.stream() - .collect(Collectors.groupingBy(winningNumbers::getLottoResult, countingLottoResult())); - } - - private static Collector countingLottoResult() { - return Collectors.reducing(0, e -> 1, Integer::sum); + .collect(Collectors.groupingBy(winningNumbers::getLottoResult, Collectors.summingInt(x -> 1))); } } \ No newline at end of file diff --git a/src/main/java/domain/LottoStore.java b/src/main/java/domain/LottoStore.java index de7ae67994..16ad76c9bf 100644 --- a/src/main/java/domain/LottoStore.java +++ b/src/main/java/domain/LottoStore.java @@ -2,7 +2,6 @@ import domain.numberscontainer.SixLottoNumbersDTO; import domain.numberscontainer.Ticket; -import strategy.RandomTicketFactory; import java.util.ArrayList; import java.util.List; diff --git a/src/main/java/domain/Money.java b/src/main/java/domain/Money.java index 8c386b25f3..49546391c8 100644 --- a/src/main/java/domain/Money.java +++ b/src/main/java/domain/Money.java @@ -1,26 +1,29 @@ package domain; public class Money { + private static final String NUMBER_REGEX = "^[+-]?[0-9]+$"; + private final int money; - public Money(int money) { - validateMoney(money); + public Money(String moneyInput) { + int money = parseInt(moneyInput); + validateMoneyUnit(money); this.money = money; } - private void validateMoney(int money) { - validateMoneyRange(money); - validateMoneyUnit(money); + private static int parseInt(String input) { + validateNumber(input); + return Integer.parseInt(input); } - public int getNumberOfTickets() { - return this.money / 1000; + private static void validateNumber(String input) { + if (!input.matches(NUMBER_REGEX)) { + throw new NumberFormatException("0원 이상 숫자를 입력해주세요."); + } } - private void validateMoneyRange(int money) { - if (money < 0 || money > 50000) { - throw new IllegalArgumentException("0원 이상, 5만원 이하 금액만 구매 가능합니다."); - } + public int getNumberOfTickets() { + return this.money / 1000; } private void validateMoneyUnit(int money) { @@ -32,4 +35,4 @@ private void validateMoneyUnit(int money) { public int getMoney() { return this.money; } -} +} \ No newline at end of file diff --git a/src/main/java/strategy/RandomTicketFactory.java b/src/main/java/domain/RandomTicketFactory.java similarity index 62% rename from src/main/java/strategy/RandomTicketFactory.java rename to src/main/java/domain/RandomTicketFactory.java index 62c59a18e8..3c680e4513 100644 --- a/src/main/java/strategy/RandomTicketFactory.java +++ b/src/main/java/domain/RandomTicketFactory.java @@ -1,4 +1,4 @@ -package strategy; +package domain; import domain.numberscontainer.LottoNumber; import domain.numberscontainer.SixLottoNumbersDTO; @@ -11,14 +11,13 @@ public class RandomTicketFactory { private static final int SIXTH_INDEX = 6; public static Ticket createTicket() { - Set sixNumbers = new HashSet<>(getShuffledList().subList(FIRST_INDEX, SIXTH_INDEX)); - return new Ticket(new SixLottoNumbersDTO(sixNumbers)); + return new Ticket(new SixLottoNumbersDTO(getShuffledList())); } - private static List getShuffledList() { + private static Set getShuffledList() { List lottoNumbers = Arrays.asList(LottoNumber.values()); Collections.shuffle(lottoNumbers); - return lottoNumbers; + return new HashSet<>(lottoNumbers.subList(FIRST_INDEX, SIXTH_INDEX)); } } \ No newline at end of file diff --git a/src/main/java/domain/numberscontainer/BonusNumberDTO.java b/src/main/java/domain/numberscontainer/BonusNumberDTO.java index cdeb53719f..7b1fd66006 100644 --- a/src/main/java/domain/numberscontainer/BonusNumberDTO.java +++ b/src/main/java/domain/numberscontainer/BonusNumberDTO.java @@ -1,10 +1,23 @@ package domain.numberscontainer; public class BonusNumberDTO { + private static final String NUMBER_REGEX = "^[+-]?[0-9]+$"; + private final LottoNumber bonusNumber; - public BonusNumberDTO(LottoNumber bonusNumber) { - this.bonusNumber = bonusNumber; + public BonusNumberDTO(String bonusNumberInput) { + this.bonusNumber = LottoNumber.getLottoNumber(parseInt(bonusNumberInput)); + } + + private static int parseInt(String input) { + validateNumber(input); + return Integer.parseInt(input); + } + + private static void validateNumber(String input) { + if (!input.matches(NUMBER_REGEX)) { + throw new NumberFormatException("숫자를 입력해주세요."); + } } public LottoNumber getBonusNumber() { diff --git a/src/main/java/domain/numberscontainer/SixLottoNumbersDTO.java b/src/main/java/domain/numberscontainer/SixLottoNumbersDTO.java index c1e628df84..cd1411829e 100644 --- a/src/main/java/domain/numberscontainer/SixLottoNumbersDTO.java +++ b/src/main/java/domain/numberscontainer/SixLottoNumbersDTO.java @@ -1,14 +1,29 @@ package domain.numberscontainer; +import java.util.Arrays; import java.util.Set; +import java.util.stream.Collectors; public class SixLottoNumbersDTO { private final Set sixNumbers; + public SixLottoNumbersDTO(String sixNumbersInput) { + Set sixNumbers = parseSixNumbers(sixNumbersInput); + this.sixNumbers = sixNumbers; + } + public SixLottoNumbersDTO(Set sixNumbers) { this.sixNumbers = sixNumbers; } + private Set parseSixNumbers(String sixNumbersInput) { + return Arrays.asList(sixNumbersInput.split(",")).stream() + .map(String::trim) + .map(Integer::new) + .map(LottoNumber::getLottoNumber) + .collect(Collectors.toSet()); + } + public Set getSixNumbers() { return sixNumbers; } diff --git a/src/main/java/strategy/TicketFactory.java b/src/main/java/strategy/TicketFactory.java deleted file mode 100644 index cae7440f77..0000000000 --- a/src/main/java/strategy/TicketFactory.java +++ /dev/null @@ -1,8 +0,0 @@ -package strategy; - -import domain.numberscontainer.Ticket; - -public interface TicketFactory { - - Ticket createTicket(); -} \ No newline at end of file diff --git a/src/main/java/view/InputView.java b/src/main/java/view/InputView.java index 27df95f450..02c3ff35e4 100644 --- a/src/main/java/view/InputView.java +++ b/src/main/java/view/InputView.java @@ -1,54 +1,22 @@ package view; -import domain.numberscontainer.LottoNumber; -import domain.Money; -import domain.numberscontainer.BonusNumberDTO; -import domain.numberscontainer.SixLottoNumbersDTO; -import strategy.TicketFactory; - import java.util.*; -import java.util.stream.Collectors; public class InputView { - private static final String NUMBER_REGEX = "^[0-9]+$"; - private static Scanner scanner = new Scanner(System.in); - public static Money enterMoney() { + public static String enterMoney() { System.out.println("구입 금액을 입력해주세요."); - int money = parseInt(scanner.nextLine()); - return new Money(money); - } - - private static int parseInt(String input) { - validateNumber(input); - return Integer.parseInt(input); + return scanner.nextLine(); } - private static void validateNumber(String input) { - if (!input.matches(NUMBER_REGEX)) { - throw new NumberFormatException("숫자를 입력해주세요."); - } - } - - public static SixLottoNumbersDTO enterLastWeekWinningNumbers() { + public static String enterLastWeekWinningNumbers() { System.out.println("지난 주 당첨 번호를 입력해 주세요."); - SixLottoNumbersDTO lastWeekWinningNumbers = parseWinningNumbers(scanner.nextLine()); - return lastWeekWinningNumbers; - } - - private static SixLottoNumbersDTO parseWinningNumbers(String input) { - Set lottoNumbers = Arrays.asList(input.split(",")).stream() - .map(String::trim) - .map(Integer::new) - .map(LottoNumber::getLottoNumber) - .collect(Collectors.toSet()); - return new SixLottoNumbersDTO(new HashSet<>(lottoNumbers)); + return scanner.nextLine(); } - public static BonusNumberDTO enterBonusNumber() { + public static String enterBonusNumber() { System.out.println("보너스 볼을 입력해 주세요."); - int bonusNumber = parseInt(scanner.nextLine()); - return new BonusNumberDTO(LottoNumber.getLottoNumber(bonusNumber)); + return scanner.nextLine(); } } diff --git a/src/main/java/view/OutputView.java b/src/main/java/view/OutputView.java index 0da03a5f56..53ca169be7 100644 --- a/src/main/java/view/OutputView.java +++ b/src/main/java/view/OutputView.java @@ -21,8 +21,8 @@ public static void printNumberOfTickets(int size) { public static void printTickets(List tickets) { StringBuilder stringBuilder = new StringBuilder(); for (Ticket ticket : tickets) { - stringBuilder.append(ticket.toString()); - stringBuilder.append("\n"); + System.out.println(ticket.toString()); + System.lineSeparator(); } System.out.println(stringBuilder.toString()); } diff --git a/src/test/java/domain/LottoResultMachineTest.java b/src/test/java/domain/LottoResultMachineTest.java index cbf33a6384..b0219235f9 100644 --- a/src/test/java/domain/LottoResultMachineTest.java +++ b/src/test/java/domain/LottoResultMachineTest.java @@ -3,7 +3,6 @@ import domain.numberscontainer.*; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; -import strategy.TicketFactory; import java.util.*; @@ -13,11 +12,11 @@ public class LottoResultMachineTest { @Test @DisplayName("당첨 결과 계산") void test1() { - Money money = new Money(10000); + Money money = new Money("10000"); List tickets = LottoStore.generateTickets(money.getNumberOfTickets(), createFixedNumbersList()); SixLottoNumbersDTO sixNumbers = createFixedNumbers(3, 4, 5, 6, 7, 8); - WinningNumbers winningNumbers = new WinningNumbers(sixNumbers, new BonusNumberDTO(LottoNumber.NINE)); + WinningNumbers winningNumbers = new WinningNumbers(sixNumbers, new BonusNumberDTO("9")); Map lottoResults = LottoResultMachine.confirmResult(tickets, winningNumbers); assertThat(lottoResults.get(LottoResult.FIRST)).isEqualTo(1); @@ -31,11 +30,11 @@ void test1() { @Test @DisplayName("수익률 계산") void test2() { - Money money = new Money(10000); + Money money = new Money("10000"); List tickets = LottoStore.generateTickets(money.getNumberOfTickets(), createFixedNumbersList()); SixLottoNumbersDTO sixNumbers = createFixedNumbers(3, 4, 5, 6, 7, 8); - WinningNumbers winningNumbers = new WinningNumbers(sixNumbers, new BonusNumberDTO(LottoNumber.NINE)); + WinningNumbers winningNumbers = new WinningNumbers(sixNumbers, new BonusNumberDTO("9")); Map lottoResults = LottoResultMachine.confirmResult(tickets, winningNumbers); LottoProfit profit = LottoProfit.ofProfit(lottoResults, money); assertThat(profit.getValue()).isEqualTo(20631000); @@ -55,11 +54,6 @@ private List createFixedNumbersList() { } private SixLottoNumbersDTO createFixedNumbers(int number1, int number2, int number3, int number4, int number5, int number6) { - return new SixLottoNumbersDTO(new HashSet<>(Arrays.asList(LottoNumber.getLottoNumber(number1), - LottoNumber.getLottoNumber(number2), - LottoNumber.getLottoNumber(number3), - LottoNumber.getLottoNumber(number4), - LottoNumber.getLottoNumber(number5), - LottoNumber.getLottoNumber(number6)))); + return new SixLottoNumbersDTO(String.format("%d, %d, %d, %d, %d, %d", number1, number2, number3, number4, number5, number6)); } } diff --git a/src/test/java/domain/MoneyTest.java b/src/test/java/domain/MoneyTest.java index 922f7e3e43..73b0171634 100644 --- a/src/test/java/domain/MoneyTest.java +++ b/src/test/java/domain/MoneyTest.java @@ -1,7 +1,6 @@ package domain; import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; @@ -10,11 +9,11 @@ @DisplayName("돈 객체 테스트") class MoneyTest { @ParameterizedTest - @ValueSource(ints = {1700, -1, 50001}) + @ValueSource(strings = {"1700", "-1", "50001"}) @DisplayName("돈 객체 생성자") - void moneyConstructorTest(int money) { + void moneyConstructorTest(String money) { assertThatThrownBy(() -> new Money(money)) - .isInstanceOf(IllegalArgumentException.class) - .hasMessageEndingWith("만 구매 가능합니다."); + .isInstanceOf(IllegalArgumentException.class) + .hasMessageEndingWith("만 구매 가능합니다."); } } \ No newline at end of file diff --git a/src/test/java/domain/numberscontainer/TicketTest.java b/src/test/java/domain/numberscontainer/TicketTest.java index 5a7248fba0..1c60d7ea65 100644 --- a/src/test/java/domain/numberscontainer/TicketTest.java +++ b/src/test/java/domain/numberscontainer/TicketTest.java @@ -29,11 +29,6 @@ void compareTwoTickets() { } private SixLottoNumbersDTO createLottoNumberDto(int number1, int number2, int number3, int number4, int number5, int number6) { - return new SixLottoNumbersDTO(new HashSet<>(Arrays.asList(LottoNumber.getLottoNumber(number1), - LottoNumber.getLottoNumber(number2), - LottoNumber.getLottoNumber(number3), - LottoNumber.getLottoNumber(number4), - LottoNumber.getLottoNumber(number5), - LottoNumber.getLottoNumber(number6)))); + return new SixLottoNumbersDTO(String.format("%d, %d, %d, %d, %d, %d", number1, number2, number3, number4, number5, number6)); } } diff --git a/src/test/java/domain/numberscontainer/WinningNumbersTest.java b/src/test/java/domain/numberscontainer/WinningNumbersTest.java index 3da1a25fac..a36f34444b 100644 --- a/src/test/java/domain/numberscontainer/WinningNumbersTest.java +++ b/src/test/java/domain/numberscontainer/WinningNumbersTest.java @@ -19,37 +19,37 @@ public class WinningNumbersTest { @DisplayName("당첨 번호 객체 생성") void winningNumberConstructor() { SixLottoNumbersDTO sixLottoNumbersDTO = createSixNumbersDto(1, 2, 3, 4, 5, 5); - BonusNumberDTO bonusNumberDTO = new BonusNumberDTO(LottoNumber.SEVEN); + BonusNumberDTO bonusNumberDTO = new BonusNumberDTO("7"); assertThatThrownBy(() -> new WinningNumbers(sixLottoNumbersDTO, bonusNumberDTO)) .isInstanceOf(IllegalArgumentException.class) .hasMessage("6개의 숫자를 입력해주세요."); } @ParameterizedTest - @ValueSource(ints = {1, 5}) + @ValueSource(strings = {"1", "5"}) @DisplayName("중복 없는 보너스 번호인지 검증") - void validateBonusNumber(int input) { + void validateBonusNumber(String input) { SixLottoNumbersDTO sixLottoNumbersDTO = createSixNumbersDto(1, 2, 3, 4, 5, 6); - BonusNumberDTO bonusNumberDTO = new BonusNumberDTO(LottoNumber.getLottoNumber(input)); + BonusNumberDTO bonusNumberDTO = new BonusNumberDTO(input); assertThatThrownBy(() -> new WinningNumbers(sixLottoNumbersDTO, bonusNumberDTO)) .isInstanceOf(IllegalArgumentException.class) .hasMessageEndingWith("보너스 번호를 입력해주세요."); } @ParameterizedTest - @ValueSource(ints = {-1, 46}) + @ValueSource(strings = {"-1", "46"}) @DisplayName("1부터 45까지의 보너스 번호인지 검증") - void validateBonusNumber2(int input) { - assertThatThrownBy(() -> new BonusNumberDTO(LottoNumber.getLottoNumber(input))) + void validateBonusNumber2(String input) { + assertThatThrownBy(() -> new BonusNumberDTO(input)) .isInstanceOf(IllegalArgumentException.class) - .hasMessageEndingWith("1부터 45까지의 숫자를 입력해주세요."); + .hasMessageEndingWith("숫자를 입력해주세요."); } @Test @DisplayName("Ticket과 당첨 번호 비교") void findDuplicatedNumbers() { SixLottoNumbersDTO sixLottoNumbersDTO1 = createSixNumbersDto(1, 2, 3, 4, 5, 6); - BonusNumberDTO bonusNumberDTO = new BonusNumberDTO(LottoNumber.SEVEN); + BonusNumberDTO bonusNumberDTO = new BonusNumberDTO("7"); SixLottoNumbersDTO sixLottoNumbersDTO2 = createSixNumbersDto(4, 5, 6, 7, 8, 9); WinningNumbers winningNumbers = new WinningNumbers(sixLottoNumbersDTO1, bonusNumberDTO); Ticket ticket = new Ticket(sixLottoNumbersDTO2); @@ -59,7 +59,7 @@ void findDuplicatedNumbers() { @Test @DisplayName("당첨 결과 확인") void getLottoResultTest() { - WinningNumbers winningNumbers = new WinningNumbers(createSixNumbersDto(3, 4, 5, 6, 7, 8), new BonusNumberDTO(LottoNumber.NINE)); + WinningNumbers winningNumbers = new WinningNumbers(createSixNumbersDto(3, 4, 5, 6, 7, 8), new BonusNumberDTO("9")); assertThat(winningNumbers.getLottoResult(new Ticket(createSixNumbersDto(3, 4, 5, 6, 7, 8)))).isEqualTo(LottoResult.FIRST); assertThat(winningNumbers.getLottoResult(new Ticket(createSixNumbersDto(3, 4, 5, 6, 7, 9)))).isEqualTo(LottoResult.SECOND); @@ -70,12 +70,6 @@ void getLottoResultTest() { } private SixLottoNumbersDTO createSixNumbersDto(int number1, int number2, int number3, int number4, int number5, int number6) { - Set sixNumbers = new HashSet<>(Arrays.asList(LottoNumber.getLottoNumber(number1), - LottoNumber.getLottoNumber(number2), - LottoNumber.getLottoNumber(number3), - LottoNumber.getLottoNumber(number4), - LottoNumber.getLottoNumber(number5), - LottoNumber.getLottoNumber(number6))); - return new SixLottoNumbersDTO(sixNumbers); + return new SixLottoNumbersDTO(String.format("%d, %d, %d, %d, %d, %d", number1, number2, number3, number4, number5, number6)); } } \ No newline at end of file diff --git a/src/test/java/strategy/RandomTicketFactoryTest.java b/src/test/java/strategy/RandomTicketFactoryTest.java index 770166f551..3ab362a30f 100644 --- a/src/test/java/strategy/RandomTicketFactoryTest.java +++ b/src/test/java/strategy/RandomTicketFactoryTest.java @@ -1,17 +1,9 @@ package strategy; -import domain.numberscontainer.LottoNumber; -import domain.numberscontainer.BonusNumberDTO; -import domain.numberscontainer.SixLottoNumbersDTO; -import domain.numberscontainer.Ticket; -import domain.numberscontainer.WinningNumbers; +import domain.RandomTicketFactory; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; -import java.util.Arrays; -import java.util.HashSet; -import java.util.Set; - import static org.assertj.core.api.Assertions.assertThatCode; @DisplayName("티켓 생성 테스트") From 4ee67e90df34d86f523e0cedab0e15abf3b0c850 Mon Sep 17 00:00:00 2001 From: YebinK Date: Tue, 25 Feb 2020 17:19:11 +0900 Subject: [PATCH 23/23] =?UTF-8?q?[refactor]=20Money=20=EA=B0=9D=EC=B2=B4?= =?UTF-8?q?=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BC=80=EC=9D=B4=EC=8A=A4=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/domain/MoneyTest.java | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/test/java/domain/MoneyTest.java b/src/test/java/domain/MoneyTest.java index 73b0171634..6753a49a86 100644 --- a/src/test/java/domain/MoneyTest.java +++ b/src/test/java/domain/MoneyTest.java @@ -8,12 +8,21 @@ @DisplayName("돈 객체 테스트") class MoneyTest { + @ParameterizedTest + @ValueSource(strings = {" ", "만 원"}) + @DisplayName("돈 객체 생성자") + void moneyTest1(String money) { + assertThatThrownBy(() -> new Money(money)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("0원 이상 숫자를 입력해주세요."); + } + @ParameterizedTest @ValueSource(strings = {"1700", "-1", "50001"}) @DisplayName("돈 객체 생성자") - void moneyConstructorTest(String money) { + void moneyTest2(String money) { assertThatThrownBy(() -> new Money(money)) .isInstanceOf(IllegalArgumentException.class) - .hasMessageEndingWith("만 구매 가능합니다."); + .hasMessage("천 원 단위로만 구매 가능합니다."); } } \ No newline at end of file