diff --git a/src/main/java/lotto/LottoDisplay.java b/src/main/java/lotto/LottoDisplay.java index 2a77c9524e..e3333616d2 100644 --- a/src/main/java/lotto/LottoDisplay.java +++ b/src/main/java/lotto/LottoDisplay.java @@ -2,6 +2,7 @@ import lotto.entity.LottoMoney; import lotto.controller.LottoMachine; +import lotto.entity.LottoNumber; import lotto.entity.Lottos; import lotto.entity.PrizePolicy; import lotto.view.InputView; @@ -27,7 +28,8 @@ public static void main(String[] args) { resultView.printLottoTickets(totalLottoTickets); List matchingCountResult = lottoMachine.checkMatchingNumber(inputView.inputWinningNumbers()); - Map matchingResult = lottoMachine.winningResult(matchingCountResult); + LottoNumber bonusBall = inputView.inputBonusBall(); + Map matchingResult = lottoMachine.winningResult(matchingCountResult, bonusBall); int totalPrizeMoney = resultView.printTotalResult(matchingResult); diff --git a/src/main/java/lotto/controller/LottoMachine.java b/src/main/java/lotto/controller/LottoMachine.java index 2168579eff..03b6e4f70e 100644 --- a/src/main/java/lotto/controller/LottoMachine.java +++ b/src/main/java/lotto/controller/LottoMachine.java @@ -34,8 +34,8 @@ public List checkMatchingNumber(List inputWinningNumbers) return totalLottoTicket.getMatchingCounts(new Lotto(inputWinningNumbers)); } - public Map winningResult(List matchingLottoTickets) { - return totalLottoTicket.calculateWinLottoTicket(matchingLottoTickets); + public Map winningResult(List matchingLottoTickets, LottoNumber bonusBall) { + return totalLottoTicket.calculateWinLottoTicket(matchingLottoTickets, bonusBall); } public double rateOfReturnResult(int totalMoney, int totalPrizeMoney) { diff --git a/src/main/java/lotto/entity/Lotto.java b/src/main/java/lotto/entity/Lotto.java index 6dc52d0716..6a1b69000e 100644 --- a/src/main/java/lotto/entity/Lotto.java +++ b/src/main/java/lotto/entity/Lotto.java @@ -49,4 +49,8 @@ public int matchCount(Lotto winningLotto) { .filter(winningLotto.getLottoNumbers()::contains) .count(); } + + public boolean containsBonusBall(LottoNumber bonusBall) { + return lottoNumbers.contains(bonusBall); + } } diff --git a/src/main/java/lotto/entity/Lottos.java b/src/main/java/lotto/entity/Lottos.java index d56b4c29fd..916529332f 100644 --- a/src/main/java/lotto/entity/Lottos.java +++ b/src/main/java/lotto/entity/Lottos.java @@ -25,10 +25,13 @@ public List getMatchingCounts(Lotto winningLotto) { .collect(Collectors.toList()); } - public Map calculateWinLottoTicket(List matchingLottoTickets) { + public Map calculateWinLottoTicket(List matchingLottoTickets, LottoNumber bonusBall) { Map winLottoTicket = new HashMap<>(); - for (Integer matchCount : matchingLottoTickets) { - inputWinLottoTicket(winLottoTicket, matchCount); + for (int i = 0; i < matchingLottoTickets.size(); i++) { + int matchCount = matchingLottoTickets.get(i); + Lotto lottoTicket = values.get(i); + PrizePolicy prizePolicy = PrizePolicy.fromMatchCountAndBonus(matchCount, lottoTicket.containsBonusBall(bonusBall)); + winLottoTicket.put(prizePolicy, winLottoTicket.getOrDefault(prizePolicy, 0) + 1); } return winLottoTicket; } diff --git a/src/main/java/lotto/entity/PrizePolicy.java b/src/main/java/lotto/entity/PrizePolicy.java index 567caae104..55d6dbb96e 100644 --- a/src/main/java/lotto/entity/PrizePolicy.java +++ b/src/main/java/lotto/entity/PrizePolicy.java @@ -3,9 +3,13 @@ import java.util.Arrays; public enum PrizePolicy { + ZERO(0, 0), + ONE(1, 0), + TWO(2, 0), THREE(3, 5_000), FOUR(4, 50_000), FIVE(5, 1_500_000), + FIVE_BONUS(5, 30_000_000), SIX(6, 2_000_000_000); private final int matchCount; @@ -34,4 +38,11 @@ public static PrizePolicy fromMatchCount(int matchCount) { .findFirst() .orElseThrow(() -> new IllegalArgumentException("유효하지 않은 매칭 숫자: " + matchCount)); } + + public static PrizePolicy fromMatchCountAndBonus(int matchCount, boolean bonusMatch) { + if (matchCount == 5 && bonusMatch) { + return FIVE_BONUS; + } + return fromMatchCount(matchCount); + } } diff --git a/src/main/java/lotto/view/InputView.java b/src/main/java/lotto/view/InputView.java index 836deeba2d..28a96af309 100644 --- a/src/main/java/lotto/view/InputView.java +++ b/src/main/java/lotto/view/InputView.java @@ -13,19 +13,26 @@ public class InputView { public int inputTotalAmount() { System.out.println("구입금액을 입력해 주세요."); - return SCANNER.nextInt(); + int totalAmount = Integer.parseInt(SCANNER.nextLine()); // 금액을 입력 받음 + return totalAmount; } public List inputWinningNumbers() { System.out.println("지난 주 당첨 번호를 입력해 주세요."); - String winningNumbers = SCANNER.next(); - return inputNumberChangeArray(winningNumbers) + String winningNumbers = SCANNER.nextLine(); + return inputNumberChangeArray(winningNumbers.trim()) .map(LottoNumber::new) .collect(Collectors.toList()); } public static Stream inputNumberChangeArray(String winningNumbers) { - return Arrays.stream(winningNumbers.replace(" ","").split(",")) + return Arrays.stream(winningNumbers.split(",\\s*")) .map(Integer::parseInt); } + + public LottoNumber inputBonusBall() { + System.out.println("보너스 볼을 입력해 주세요."); + int bonusBall = Integer.parseInt(SCANNER.nextLine()); // 여기서 nextLine()으로 보너스 볼을 받음 + return new LottoNumber(bonusBall); + } } diff --git a/src/main/java/lotto/view/ResultView.java b/src/main/java/lotto/view/ResultView.java index 3db429da3a..4aaa1f7aad 100644 --- a/src/main/java/lotto/view/ResultView.java +++ b/src/main/java/lotto/view/ResultView.java @@ -6,6 +6,9 @@ import java.util.Map; public class ResultView { + + public static final int MIN_MATCH_COUNT = 3; + public void printTotalLottoTicketCount(int totalLottoTicketCount) { System.out.println(totalLottoTicketCount + "개를 구매했습니다."); } @@ -21,8 +24,14 @@ public int printTotalResult(Map matchingResult) { int matchingTicketCount = matchingResult.getOrDefault(prizePolicy, 0); int prize = prizePolicy.getPrize(); int matchCount = prizePolicy.getMatchCount(); - - System.out.println(matchCount + "개 일치 - (" + prize + "원) - " + matchingTicketCount + "개"); + if (matchCount >= MIN_MATCH_COUNT) { + String bonusMessage = (prizePolicy == PrizePolicy.FIVE_BONUS) ? ", 보너스 볼 일치" : ""; + System.out.println(String.format("%d개 일치%s - (%d원) - %d개", + matchCount, + bonusMessage, + prize, + matchingTicketCount)); + } totalPrizeMoney += prize * matchingTicketCount; }