From 96a4705a9d0ed1678574d0ccf6c6d9c92eae70ec Mon Sep 17 00:00:00 2001 From: doooyeon Date: Sun, 19 Jan 2020 03:12:25 +0900 Subject: [PATCH 01/10] feat: Rank countOfBonusMatch --- src/main/java/Rank.java | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/main/java/Rank.java b/src/main/java/Rank.java index 3144bf9..64ccfa9 100644 --- a/src/main/java/Rank.java +++ b/src/main/java/Rank.java @@ -1,20 +1,23 @@ public enum Rank { - FIRST(6, 2000000000), - SECOND(5, 1500000), - THIRD(4, 50000), - FOURTH(3, 5000); + FIRST(6, 0, 2000000000), + SECOND(5, 1, 30000000), + THIRD(5, 0, 1500000), + FOURTH(4, 0, 50000), + FIFTH(3, 0, 5000); private int countOfMatch; + private int countOfBonusMatch; private int prizeMoney; - Rank(final int countOfMatch, final int prizeMoney) { + Rank(final int countOfMatch, final int countOfBonusMatch, final int prizeMoney) { this.countOfMatch = countOfMatch; + this.countOfBonusMatch = countOfBonusMatch; this.prizeMoney = prizeMoney; } - public static Rank valueOf(final int countOfMatch) { + public static Rank valueOf(final int countOfMatch, final int countOfBonusMatch) { for (final Rank rank : Rank.values()) { - if (rank.countOfMatch == countOfMatch) { + if (rank.countOfMatch == countOfMatch && rank.countOfBonusMatch == countOfBonusMatch) { return rank; } } From ae662ec7fbd5c66d12d698a112f5f0cc568647e4 Mon Sep 17 00:00:00 2001 From: doooyeon Date: Sun, 19 Jan 2020 03:12:59 +0900 Subject: [PATCH 02/10] feat: askBonusNumber --- src/main/java/InputView.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/main/java/InputView.java b/src/main/java/InputView.java index 8178e74..b698346 100644 --- a/src/main/java/InputView.java +++ b/src/main/java/InputView.java @@ -14,4 +14,10 @@ public static String askWinningNumbers() { System.out.println("\n지난 주 당첨 번호를 입력해 주세요."); return scanner.nextLine(); } + + public static int askBonusNumber() { + scanner = new Scanner(System.in); + System.out.println("\n보너스 볼을 입력해 주세요."); + return scanner.nextInt(); + } } From 871a410b314934c812ae982c74f2c19b44e67b2e Mon Sep 17 00:00:00 2001 From: doooyeon Date: Sun, 19 Jan 2020 03:13:39 +0900 Subject: [PATCH 03/10] feat: ticket bonums number match --- src/main/java/LottoGame.java | 4 ++-- src/main/java/Main.java | 2 +- src/main/java/Ticket.java | 7 ++++++- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/main/java/LottoGame.java b/src/main/java/LottoGame.java index 25dcff8..aa5b771 100644 --- a/src/main/java/LottoGame.java +++ b/src/main/java/LottoGame.java @@ -7,10 +7,10 @@ public LottoGame(final List tickets) { this.tickets = tickets; } - public LottoResult matchTickets(final WinningTicket winningTicket) { + public LottoResult matchTickets(final WinningTicket winningTicket, final LottoNumber bonusNumber) { LottoResult lottoResult = new LottoResult(); for (Ticket ticket : tickets) { - lottoResult.add(Rank.valueOf(ticket.match(winningTicket))); + lottoResult.add(Rank.valueOf(ticket.matchCount(winningTicket), ticket.matchCount(bonusNumber))); } return lottoResult; diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 7887304..a46f463 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -14,7 +14,7 @@ public static void main(String[] args) { WinningTicket winningTicket = new WinningTicket(LottoNumbers.of(InputView.askWinningNumbers())); LottoGame game = new LottoGame(tickets); - LottoResult result = game.matchTickets(winningTicket); + LottoResult result = game.matchTickets(winningTicket, LottoNumber.of(InputView.askBonusNumber())); ResultView.printStatistics(result); ResultView.printRateOfRevenue(game.calculateRevenue(money, result)); } diff --git a/src/main/java/Ticket.java b/src/main/java/Ticket.java index f08ab6f..f1f1cb0 100644 --- a/src/main/java/Ticket.java +++ b/src/main/java/Ticket.java @@ -7,10 +7,15 @@ public Ticket(final LottoNumbers numbers) { this.numbers = numbers; } - public int match(final WinningTicket winningTicket) { + public int matchCount(final WinningTicket winningTicket) { return Math.toIntExact(numbers.toList().stream().filter(winningTicket::contains).count()); } + public int matchCount(final LottoNumber lottoNumber) { + if (this.contains(lottoNumber)) return 1; + else return 0; + } + public boolean contains(final LottoNumber lottoNumber) { return this.numbers.toList().contains(lottoNumber); } From f4b9a22131799ede6872c5da24537148d6f73eab Mon Sep 17 00:00:00 2001 From: doooyeon Date: Sun, 19 Jan 2020 03:27:11 +0900 Subject: [PATCH 04/10] =?UTF-8?q?refactor:=20bonus=20number=20match=20bool?= =?UTF-8?q?ean=EC=9C=BC=EB=A1=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/LottoGame.java | 2 +- src/main/java/Rank.java | 21 ++++++++++----------- src/main/java/Ticket.java | 5 ----- 3 files changed, 11 insertions(+), 17 deletions(-) diff --git a/src/main/java/LottoGame.java b/src/main/java/LottoGame.java index aa5b771..51ef810 100644 --- a/src/main/java/LottoGame.java +++ b/src/main/java/LottoGame.java @@ -10,7 +10,7 @@ public LottoGame(final List tickets) { public LottoResult matchTickets(final WinningTicket winningTicket, final LottoNumber bonusNumber) { LottoResult lottoResult = new LottoResult(); for (Ticket ticket : tickets) { - lottoResult.add(Rank.valueOf(ticket.matchCount(winningTicket), ticket.matchCount(bonusNumber))); + lottoResult.add(Rank.valueOf(ticket.matchCount(winningTicket), ticket.contains(bonusNumber))); } return lottoResult; diff --git a/src/main/java/Rank.java b/src/main/java/Rank.java index 64ccfa9..4e7a8ec 100644 --- a/src/main/java/Rank.java +++ b/src/main/java/Rank.java @@ -1,23 +1,23 @@ public enum Rank { - FIRST(6, 0, 2000000000), - SECOND(5, 1, 30000000), - THIRD(5, 0, 1500000), - FOURTH(4, 0, 50000), - FIFTH(3, 0, 5000); + FIRST(6, false, 2000000000), + SECOND(5, true, 30000000), + THIRD(5, false, 1500000), + FOURTH(4, false, 50000), + FIFTH(3, false, 5000); private int countOfMatch; - private int countOfBonusMatch; + private boolean isBonusMatch; private int prizeMoney; - Rank(final int countOfMatch, final int countOfBonusMatch, final int prizeMoney) { + Rank(final int countOfMatch, final boolean isBonusMatch, final int prizeMoney) { this.countOfMatch = countOfMatch; - this.countOfBonusMatch = countOfBonusMatch; + this.isBonusMatch = isBonusMatch; this.prizeMoney = prizeMoney; } - public static Rank valueOf(final int countOfMatch, final int countOfBonusMatch) { + public static Rank valueOf(final int countOfMatch, final boolean isBonusMatch) { for (final Rank rank : Rank.values()) { - if (rank.countOfMatch == countOfMatch && rank.countOfBonusMatch == countOfBonusMatch) { + if (rank.countOfMatch == countOfMatch && rank.isBonusMatch == isBonusMatch) { return rank; } } @@ -28,5 +28,4 @@ public static Rank valueOf(final int countOfMatch, final int countOfBonusMatch) public int getPrizeMoney() { return prizeMoney; } - } diff --git a/src/main/java/Ticket.java b/src/main/java/Ticket.java index f1f1cb0..145c6ff 100644 --- a/src/main/java/Ticket.java +++ b/src/main/java/Ticket.java @@ -11,11 +11,6 @@ public int matchCount(final WinningTicket winningTicket) { return Math.toIntExact(numbers.toList().stream().filter(winningTicket::contains).count()); } - public int matchCount(final LottoNumber lottoNumber) { - if (this.contains(lottoNumber)) return 1; - else return 0; - } - public boolean contains(final LottoNumber lottoNumber) { return this.numbers.toList().contains(lottoNumber); } From 01fe55315b7a3224507ab4d4c361c6cb8dc40d53 Mon Sep 17 00:00:00 2001 From: doooyeon Date: Sun, 19 Jan 2020 03:39:55 +0900 Subject: [PATCH 05/10] =?UTF-8?q?feat:=20=EA=B5=AC=EC=9E=85=EA=B8=88?= =?UTF-8?q?=EC=95=A1=20=EC=98=88=EC=99=B8=20=EC=B2=98=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/LottoSeller.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/LottoSeller.java b/src/main/java/LottoSeller.java index 89f5b15..6fccd3e 100644 --- a/src/main/java/LottoSeller.java +++ b/src/main/java/LottoSeller.java @@ -11,6 +11,8 @@ public LottoSeller(final NumberGenerator numberGenerator) { } public List buyTickets(final int money) { + if (money < PRICE) throw new IllegalArgumentException(PRICE + "원 이상 구매해야 합니다"); + List tickets = new ArrayList<>(); for (int i = 0; i < money / PRICE; i++) { tickets.add(new Ticket(LottoNumbers.of(numberGenerator.generate()))); From baa60b7173e69d01da308e622cad80931554c14a Mon Sep 17 00:00:00 2001 From: doooyeon Date: Sun, 19 Jan 2020 03:41:38 +0900 Subject: [PATCH 06/10] feat: Rank toString --- src/main/java/LottoResult.java | 4 +++- src/main/java/Rank.java | 15 ++++++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/main/java/LottoResult.java b/src/main/java/LottoResult.java index 4fcc981..a0f3c27 100644 --- a/src/main/java/LottoResult.java +++ b/src/main/java/LottoResult.java @@ -26,7 +26,9 @@ public int calculatePrizeMoney() { public String toString() { StringBuilder stringBuilder = new StringBuilder(); for (Rank rank : rankCountMap.keySet()) { - stringBuilder.append(rankCountMap.get(rank) * rank.getPrizeMoney()); + stringBuilder.append(rank.toString()) + .append(String.format(" - %d개", rankCountMap.get(rank))) + .append("\n"); } return stringBuilder.toString(); } diff --git a/src/main/java/Rank.java b/src/main/java/Rank.java index 4e7a8ec..03adec6 100644 --- a/src/main/java/Rank.java +++ b/src/main/java/Rank.java @@ -3,7 +3,10 @@ public enum Rank { SECOND(5, true, 30000000), THIRD(5, false, 1500000), FOURTH(4, false, 50000), - FIFTH(3, false, 5000); + FIFTH(3, false, 5000), + MISS(0, false, 0); + + private static final int WINNING_MIN_COUNT = 3; private int countOfMatch; private boolean isBonusMatch; @@ -16,6 +19,10 @@ public enum Rank { } public static Rank valueOf(final int countOfMatch, final boolean isBonusMatch) { + if (countOfMatch < WINNING_MIN_COUNT) { + return MISS; + } + for (final Rank rank : Rank.values()) { if (rank.countOfMatch == countOfMatch && rank.isBonusMatch == isBonusMatch) { return rank; @@ -28,4 +35,10 @@ public static Rank valueOf(final int countOfMatch, final boolean isBonusMatch) { public int getPrizeMoney() { return prizeMoney; } + + @Override + public String toString() { + if (isBonusMatch) return String.format("%d개 일치, 보너스 볼 일치 (%d원)", countOfMatch, prizeMoney); + return String.format("%d개 일치 (%d원)", countOfMatch, prizeMoney); + } } From af9a159bf75be058475d4557adf5388cf1564635 Mon Sep 17 00:00:00 2001 From: doooyeon Date: Sun, 19 Jan 2020 03:50:07 +0900 Subject: [PATCH 07/10] refactor: exception messgae --- src/main/java/LottoNumber.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/LottoNumber.java b/src/main/java/LottoNumber.java index 08b6b63..0a2d463 100644 --- a/src/main/java/LottoNumber.java +++ b/src/main/java/LottoNumber.java @@ -12,7 +12,7 @@ public static LottoNumber of(final int number) { public LottoNumber(final int value) { if (value < MIN_VALUE || value > MAX_VALUE) { - throw new IllegalArgumentException(); + throw new IllegalArgumentException("로또 번호는 " + MIN_VALUE + "이상 " + MAX_VALUE + "이하 이어야 합니다."); } this.value = value; } From 663022166822f58d61a27cb03c335a42c182c301 Mon Sep 17 00:00:00 2001 From: doooyeon Date: Sun, 19 Jan 2020 03:50:54 +0900 Subject: [PATCH 08/10] =?UTF-8?q?feat:=20money=20=EC=98=88=EC=99=B8=20?= =?UTF-8?q?=EC=B2=98=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/LottoSeller.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/LottoSeller.java b/src/main/java/LottoSeller.java index 6fccd3e..1f2d769 100644 --- a/src/main/java/LottoSeller.java +++ b/src/main/java/LottoSeller.java @@ -2,7 +2,7 @@ import java.util.List; public class LottoSeller { - private static final int PRICE = 1000; + public static final int UNIT_PRICE = 1000; private final NumberGenerator numberGenerator; @@ -11,10 +11,10 @@ public LottoSeller(final NumberGenerator numberGenerator) { } public List buyTickets(final int money) { - if (money < PRICE) throw new IllegalArgumentException(PRICE + "원 이상 구매해야 합니다"); + if (money < UNIT_PRICE) throw new IllegalArgumentException(UNIT_PRICE + "원 이상 구매해야 합니다"); List tickets = new ArrayList<>(); - for (int i = 0; i < money / PRICE; i++) { + for (int i = 0; i < money / UNIT_PRICE; i++) { tickets.add(new Ticket(LottoNumbers.of(numberGenerator.generate()))); } return tickets; From 7f1d8e0af7f95d475eede0727d4603308281d88a Mon Sep 17 00:00:00 2001 From: doooyeon Date: Sun, 19 Jan 2020 03:51:21 +0900 Subject: [PATCH 09/10] feat: printLottoCount --- src/main/java/Main.java | 1 + src/main/java/ResultView.java | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/src/main/java/Main.java b/src/main/java/Main.java index a46f463..276d71c 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -7,6 +7,7 @@ public static void main(String[] args) { LottoSeller seller = new LottoSeller(new RandomNumberGenerator(LOTTO_NUMBER_SIZE)); int money = InputView.askPurchasingAmount(); + ResultView.printLottoCount(money / LottoSeller.UNIT_PRICE); List tickets = seller.buyTickets(money); ResultView.printTickets(tickets); diff --git a/src/main/java/ResultView.java b/src/main/java/ResultView.java index 4c53196..a981c58 100644 --- a/src/main/java/ResultView.java +++ b/src/main/java/ResultView.java @@ -1,6 +1,10 @@ import java.util.List; public class ResultView { + public static void printLottoCount(final int count) { + System.out.println(String.format("%d개를 구입했습니다.", count)); + } + public static void printTickets(final List tickets) { tickets.forEach(ticket -> System.out.println(ticket.toString())); } From edba06add9adbdc4c325a0ec0706d70875e91282 Mon Sep 17 00:00:00 2001 From: doooyeon Date: Sun, 19 Jan 2020 12:23:35 +0900 Subject: [PATCH 10/10] =?UTF-8?q?refactor:=20rank=20toString=20=EC=A1=B0?= =?UTF-8?q?=EA=B1=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/Main.java | 3 +-- src/main/java/Rank.java | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 276d71c..03f0a32 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -4,11 +4,10 @@ public class Main { private static final int LOTTO_NUMBER_SIZE = 6; public static void main(String[] args) { - LottoSeller seller = new LottoSeller(new RandomNumberGenerator(LOTTO_NUMBER_SIZE)); - int money = InputView.askPurchasingAmount(); ResultView.printLottoCount(money / LottoSeller.UNIT_PRICE); + LottoSeller seller = new LottoSeller(new RandomNumberGenerator(LOTTO_NUMBER_SIZE)); List tickets = seller.buyTickets(money); ResultView.printTickets(tickets); diff --git a/src/main/java/Rank.java b/src/main/java/Rank.java index 03adec6..e7f61c2 100644 --- a/src/main/java/Rank.java +++ b/src/main/java/Rank.java @@ -38,7 +38,7 @@ public int getPrizeMoney() { @Override public String toString() { - if (isBonusMatch) return String.format("%d개 일치, 보너스 볼 일치 (%d원)", countOfMatch, prizeMoney); + if (this.equals(SECOND)) return String.format("%d개 일치, 보너스 볼 일치 (%d원)", countOfMatch, prizeMoney); return String.format("%d개 일치 (%d원)", countOfMatch, prizeMoney); } }