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(); + } } diff --git a/src/main/java/LottoGame.java b/src/main/java/LottoGame.java index 25dcff8..51ef810 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.contains(bonusNumber))); } return lottoResult; 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; } 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/LottoSeller.java b/src/main/java/LottoSeller.java index 89f5b15..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,8 +11,10 @@ public LottoSeller(final NumberGenerator numberGenerator) { } public List buyTickets(final int money) { + 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; diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 7887304..03f0a32 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -4,17 +4,17 @@ 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); 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/Rank.java b/src/main/java/Rank.java index 3144bf9..e7f61c2 100644 --- a/src/main/java/Rank.java +++ b/src/main/java/Rank.java @@ -1,20 +1,30 @@ public enum Rank { - FIRST(6, 2000000000), - SECOND(5, 1500000), - THIRD(4, 50000), - FOURTH(3, 5000); + FIRST(6, false, 2000000000), + SECOND(5, true, 30000000), + THIRD(5, false, 1500000), + FOURTH(4, false, 50000), + FIFTH(3, false, 5000), + MISS(0, false, 0); + + private static final int WINNING_MIN_COUNT = 3; private int countOfMatch; + private boolean isBonusMatch; private int prizeMoney; - Rank(final int countOfMatch, final int prizeMoney) { + Rank(final int countOfMatch, final boolean isBonusMatch, final int prizeMoney) { this.countOfMatch = countOfMatch; + this.isBonusMatch = isBonusMatch; this.prizeMoney = prizeMoney; } - public static Rank valueOf(final int countOfMatch) { + 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) { + if (rank.countOfMatch == countOfMatch && rank.isBonusMatch == isBonusMatch) { return rank; } } @@ -26,4 +36,9 @@ public int getPrizeMoney() { return prizeMoney; } + @Override + public String toString() { + if (this.equals(SECOND)) return String.format("%d개 일치, 보너스 볼 일치 (%d원)", countOfMatch, prizeMoney); + return String.format("%d개 일치 (%d원)", countOfMatch, prizeMoney); + } } 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())); } diff --git a/src/main/java/Ticket.java b/src/main/java/Ticket.java index f08ab6f..145c6ff 100644 --- a/src/main/java/Ticket.java +++ b/src/main/java/Ticket.java @@ -7,7 +7,7 @@ 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()); }