From 562d80fea521b14a2f1d13504fdfffef96bde9af Mon Sep 17 00:00:00 2001 From: seongjae6751 Date: Tue, 9 Jul 2024 00:27:27 +0900 Subject: [PATCH 1/6] step1 --- src/main/java/Lotto.java | 20 ++++++++++++++ src/main/java/LottoApp.java | 12 +++++++++ src/main/java/LottoMachine.java | 47 +++++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+) create mode 100644 src/main/java/Lotto.java create mode 100644 src/main/java/LottoApp.java create mode 100644 src/main/java/LottoMachine.java diff --git a/src/main/java/Lotto.java b/src/main/java/Lotto.java new file mode 100644 index 00000000..a69d9080 --- /dev/null +++ b/src/main/java/Lotto.java @@ -0,0 +1,20 @@ +import java.util.Collections; +import java.util.List; + +public class Lotto { + private List numbers; + + public Lotto(List numbers) { + Collections.sort(numbers); + this.numbers = numbers; + } + + public List getNumbers() { + return numbers; + } + + @Override + public String toString() { + return numbers.toString(); + } +} diff --git a/src/main/java/LottoApp.java b/src/main/java/LottoApp.java new file mode 100644 index 00000000..8b084dc4 --- /dev/null +++ b/src/main/java/LottoApp.java @@ -0,0 +1,12 @@ +import java.util.List; + +public class LottoApp { + public static void main(String[] args) { + LottoMachine lottoMachine = new LottoMachine(); + int purchaseAmount = lottoMachine.getPurchaseAmount(); + int numberOfLottos = lottoMachine.calculateNumberOfLottos(purchaseAmount); + lottoMachine.printPurchaseInfo(numberOfLottos); + List lottos = lottoMachine.generateLottos(numberOfLottos); + lottoMachine.printLottos(lottos); + } +} diff --git a/src/main/java/LottoMachine.java b/src/main/java/LottoMachine.java new file mode 100644 index 00000000..0be91cd1 --- /dev/null +++ b/src/main/java/LottoMachine.java @@ -0,0 +1,47 @@ +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Scanner; + +public class LottoMachine { + private static final int LOTTO_PRICE = 1000; + private static final int LOTTO_NUMBER_COUNT = 6; + private static final int LOTTO_MAX_NUMBER = 45; + + public int getPurchaseAmount() { + Scanner scanner = new Scanner(System.in); + System.out.println("구입금액을 입력해 주세요."); + return scanner.nextInt(); + } + + public int calculateNumberOfLottos(int purchaseAmount) { + return purchaseAmount / LOTTO_PRICE; + } + + public void printPurchaseInfo(int numberOfLottos) { + System.out.println(numberOfLottos + "개를 구매했습니다."); + } + + public List generateLottos(int numberOfLottos) { + List lottos = new ArrayList<>(); + for (int i = 0; i < numberOfLottos; i++) { + lottos.add(generateLotto()); + } + return lottos; + } + + private Lotto generateLotto() { + List numbers = new ArrayList<>(); + for (int i = 1; i <= LOTTO_MAX_NUMBER; i++) { + numbers.add(i); + } + Collections.shuffle(numbers); + return new Lotto(numbers.subList(0, LOTTO_NUMBER_COUNT)); + } + + public void printLottos(List lottos) { + for (Lotto lotto : lottos) { + System.out.println(lotto); + } + } +} From ac3edc516b428bfd893bfb72dd84cb17cd83a467 Mon Sep 17 00:00:00 2001 From: seongjae6751 Date: Tue, 9 Jul 2024 01:02:27 +0900 Subject: [PATCH 2/6] step2 --- src/main/java/InputHandler.java | 24 ++++++++++++++++ src/main/java/LottoApp.java | 15 ++++++++-- src/main/java/LottoGameResult.java | 44 ++++++++++++++++++++++++++++++ src/main/java/LottoMachine.java | 17 ------------ src/main/java/WinningNumbers.java | 22 +++++++++++++++ 5 files changed, 102 insertions(+), 20 deletions(-) create mode 100644 src/main/java/InputHandler.java create mode 100644 src/main/java/LottoGameResult.java create mode 100644 src/main/java/WinningNumbers.java diff --git a/src/main/java/InputHandler.java b/src/main/java/InputHandler.java new file mode 100644 index 00000000..be641477 --- /dev/null +++ b/src/main/java/InputHandler.java @@ -0,0 +1,24 @@ +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; + +public class InputHandler { + private Scanner scanner = new Scanner(System.in); + + public int getPurchaseAmount() { + System.out.println("구입금액을 입력해 주세요."); + return scanner.nextInt(); + } + + public WinningNumbers getWinningNumbers() { + System.out.println("지난 주 당첨 번호를 입력해 주세요."); + scanner.nextLine(); + String input = scanner.nextLine(); + String[] inputs = input.split(", "); + List winningNumbers = new ArrayList<>(); + for (String number : inputs) { + winningNumbers.add(Integer.parseInt(number)); + } + return new WinningNumbers(winningNumbers); + } +} diff --git a/src/main/java/LottoApp.java b/src/main/java/LottoApp.java index 8b084dc4..29e40f27 100644 --- a/src/main/java/LottoApp.java +++ b/src/main/java/LottoApp.java @@ -2,11 +2,20 @@ public class LottoApp { public static void main(String[] args) { + InputHandler inputHandler = new InputHandler(); LottoMachine lottoMachine = new LottoMachine(); - int purchaseAmount = lottoMachine.getPurchaseAmount(); + + int purchaseAmount = inputHandler.getPurchaseAmount(); int numberOfLottos = lottoMachine.calculateNumberOfLottos(purchaseAmount); - lottoMachine.printPurchaseInfo(numberOfLottos); + System.out.println(numberOfLottos + "개를 구매했습니다."); + List lottos = lottoMachine.generateLottos(numberOfLottos); - lottoMachine.printLottos(lottos); + for (Lotto lotto : lottos) { + System.out.println(lotto); + } + + WinningNumbers winningNumbers = inputHandler.getWinningNumbers(); + LottoGameResult gameResult = new LottoGameResult(lottos, winningNumbers); + gameResult.printWinningStatistics(); } } diff --git a/src/main/java/LottoGameResult.java b/src/main/java/LottoGameResult.java new file mode 100644 index 00000000..03434a45 --- /dev/null +++ b/src/main/java/LottoGameResult.java @@ -0,0 +1,44 @@ +import java.util.ArrayList; +import java.util.List; + +public class LottoGameResult { + private static final int LOTTO_PRICE = 1000; + + private List lottos; + private WinningNumbers winningNumbers; + + public LottoGameResult(List lottos, WinningNumbers winningNumbers) { + this.lottos = lottos; + this.winningNumbers = winningNumbers; + } + + public void printWinningStatistics() { + List matchCount = new ArrayList<>(7); // 로또 개수 저장 + for (int i = 0; i < 7; i++) { + matchCount.add(0); + } + + for (Lotto lotto : lottos) { + int match = winningNumbers.countMatchingNumbers(lotto); + matchCount.set(match, matchCount.get(match) + 1); + } + printStatistics(matchCount); + } + + private void printStatistics(List matchCount) { + System.out.println("당첨 통계"); + System.out.println("---------"); + System.out.println("3개 일치 (5000원) - " + matchCount.get(3) + "개"); + System.out.println("4개 일치 (50000원) - " + matchCount.get(4) + "개"); + System.out.println("5개 일치 (1500000원) - " + matchCount.get(5) + "개"); + System.out.println("6개 일치 (2000000000원) - " + matchCount.get(6) + "개"); + double profitRate = calculateProfitRate(matchCount); + System.out.printf("총 수익률은 %.2f입니다.%n", profitRate); + } + + private double calculateProfitRate(List matchCount) { + int totalPrize = matchCount.get(3) * 5000 + matchCount.get(4) * 50000 + matchCount.get(5) * 1500000 + matchCount.get(6) * 2000000000; + int totalSpent = LOTTO_PRICE * lottos.size(); + return (double) totalPrize / totalSpent; + } +} diff --git a/src/main/java/LottoMachine.java b/src/main/java/LottoMachine.java index 0be91cd1..e4932c8f 100644 --- a/src/main/java/LottoMachine.java +++ b/src/main/java/LottoMachine.java @@ -1,27 +1,16 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; -import java.util.Scanner; public class LottoMachine { private static final int LOTTO_PRICE = 1000; private static final int LOTTO_NUMBER_COUNT = 6; private static final int LOTTO_MAX_NUMBER = 45; - public int getPurchaseAmount() { - Scanner scanner = new Scanner(System.in); - System.out.println("구입금액을 입력해 주세요."); - return scanner.nextInt(); - } - public int calculateNumberOfLottos(int purchaseAmount) { return purchaseAmount / LOTTO_PRICE; } - public void printPurchaseInfo(int numberOfLottos) { - System.out.println(numberOfLottos + "개를 구매했습니다."); - } - public List generateLottos(int numberOfLottos) { List lottos = new ArrayList<>(); for (int i = 0; i < numberOfLottos; i++) { @@ -38,10 +27,4 @@ private Lotto generateLotto() { Collections.shuffle(numbers); return new Lotto(numbers.subList(0, LOTTO_NUMBER_COUNT)); } - - public void printLottos(List lottos) { - for (Lotto lotto : lottos) { - System.out.println(lotto); - } - } } diff --git a/src/main/java/WinningNumbers.java b/src/main/java/WinningNumbers.java new file mode 100644 index 00000000..e1d102e1 --- /dev/null +++ b/src/main/java/WinningNumbers.java @@ -0,0 +1,22 @@ +import java.util.Collections; +import java.util.List; + +public class WinningNumbers { + private List numbers; + + public WinningNumbers(List numbers) { + Collections.sort(numbers); + this.numbers = numbers; + } + + public int countMatchingNumbers(Lotto lotto) { + List lottoNumbers = lotto.getNumbers(); + int matchCount = 0; + for (int number : lottoNumbers) { + if (numbers.contains(number)) { + matchCount++; + } + } + return matchCount; + } +} From 68ddf738fb9527f7dcd947ce69be85a80de9e61d Mon Sep 17 00:00:00 2001 From: seongjae6751 Date: Tue, 9 Jul 2024 01:30:19 +0900 Subject: [PATCH 3/6] step3 --- src/main/java/InputHandler.java | 4 +++- src/main/java/LottoGameResult.java | 21 +++++++++++++++++---- src/main/java/WinningNumbers.java | 8 +++++++- src/main/java/WinningRank.java | 24 ++++++++++++++++++++++++ 4 files changed, 51 insertions(+), 6 deletions(-) create mode 100644 src/main/java/WinningRank.java diff --git a/src/main/java/InputHandler.java b/src/main/java/InputHandler.java index be641477..6a444285 100644 --- a/src/main/java/InputHandler.java +++ b/src/main/java/InputHandler.java @@ -19,6 +19,8 @@ public WinningNumbers getWinningNumbers() { for (String number : inputs) { winningNumbers.add(Integer.parseInt(number)); } - return new WinningNumbers(winningNumbers); + System.out.println("보너스 볼을 입력해 주세요."); + int bonusNumber = scanner.nextInt(); + return new WinningNumbers(winningNumbers, bonusNumber); } } diff --git a/src/main/java/LottoGameResult.java b/src/main/java/LottoGameResult.java index 03434a45..ae449985 100644 --- a/src/main/java/LottoGameResult.java +++ b/src/main/java/LottoGameResult.java @@ -13,14 +13,21 @@ public LottoGameResult(List lottos, WinningNumbers winningNumbers) { } public void printWinningStatistics() { - List matchCount = new ArrayList<>(7); // 로또 개수 저장 + List matchCount = new ArrayList<>(7); for (int i = 0; i < 7; i++) { matchCount.add(0); } for (Lotto lotto : lottos) { int match = winningNumbers.countMatchingNumbers(lotto); - matchCount.set(match, matchCount.get(match) + 1); + if (match >= 3) { + if (match == 5 && winningNumbers.containsBonusNumber(lotto)) { + matchCount.set(5, matchCount.get(5) + 1); + matchCount.set(6, matchCount.get(6) + 1); + } else { + matchCount.set(match, matchCount.get(match) + 1); + } + } } printStatistics(matchCount); } @@ -31,13 +38,19 @@ private void printStatistics(List matchCount) { System.out.println("3개 일치 (5000원) - " + matchCount.get(3) + "개"); System.out.println("4개 일치 (50000원) - " + matchCount.get(4) + "개"); System.out.println("5개 일치 (1500000원) - " + matchCount.get(5) + "개"); + System.out.println("5개 일치, 보너스 볼 일치(30000000원) - " + matchCount.get(6) + "개"); System.out.println("6개 일치 (2000000000원) - " + matchCount.get(6) + "개"); double profitRate = calculateProfitRate(matchCount); - System.out.printf("총 수익률은 %.2f입니다.%n", profitRate); + System.out.printf("총 수익률은 %.4f입니다.%n", profitRate); } private double calculateProfitRate(List matchCount) { - int totalPrize = matchCount.get(3) * 5000 + matchCount.get(4) * 50000 + matchCount.get(5) * 1500000 + matchCount.get(6) * 2000000000; + int totalPrize = 0; + totalPrize += matchCount.get(3) * WinningRank.THREE.getPrize(); + totalPrize += matchCount.get(4) * WinningRank.FOUR.getPrize(); + totalPrize += matchCount.get(5) * WinningRank.FIVE.getPrize(); + totalPrize += matchCount.get(6) * WinningRank.FIVE_WITH_BONUS.getPrize(); + totalPrize += matchCount.get(6) * WinningRank.SIX.getPrize(); int totalSpent = LOTTO_PRICE * lottos.size(); return (double) totalPrize / totalSpent; } diff --git a/src/main/java/WinningNumbers.java b/src/main/java/WinningNumbers.java index e1d102e1..f6bf9496 100644 --- a/src/main/java/WinningNumbers.java +++ b/src/main/java/WinningNumbers.java @@ -3,10 +3,12 @@ public class WinningNumbers { private List numbers; + private int bonusNumber; - public WinningNumbers(List numbers) { + public WinningNumbers(List numbers, int bonusNumber) { Collections.sort(numbers); this.numbers = numbers; + this.bonusNumber = bonusNumber; } public int countMatchingNumbers(Lotto lotto) { @@ -19,4 +21,8 @@ public int countMatchingNumbers(Lotto lotto) { } return matchCount; } + + public boolean containsBonusNumber(Lotto lotto) { + return lotto.getNumbers().contains(bonusNumber); + } } diff --git a/src/main/java/WinningRank.java b/src/main/java/WinningRank.java new file mode 100644 index 00000000..43f8c781 --- /dev/null +++ b/src/main/java/WinningRank.java @@ -0,0 +1,24 @@ +public enum WinningRank { + THREE(3, 5000), + FOUR(4, 50000), + FIVE(5, 1500000), + FIVE_WITH_BONUS(5, 30000000), + SIX(6, 2000000000), + NONE(0, 0); + + private final int matchCount; + private final int prize; + + WinningRank(int matchCount, int prize) { + this.matchCount = matchCount; + this.prize = prize; + } + + public int getMatchCount() { + return matchCount; + } + + public int getPrize() { + return prize; + } +} From dce809f11c9cef11bffeef8342f1e18f7910cd1f Mon Sep 17 00:00:00 2001 From: seongjae6751 Date: Tue, 9 Jul 2024 15:31:02 +0900 Subject: [PATCH 4/6] step4 --- src/main/java/InputHandler.java | 27 +++++++++++-- src/main/java/Lotto.java | 20 ++++++--- src/main/java/LottoApp.java | 20 ++++++--- src/main/java/LottoGameResult.java | 65 ++++++++++++++++++------------ src/main/java/LottoMachine.java | 17 ++++++-- src/main/java/LottoNumber.java | 32 +++++++++++++++ src/main/java/WinningNumbers.java | 30 ++++++++------ 7 files changed, 155 insertions(+), 56 deletions(-) create mode 100644 src/main/java/LottoNumber.java diff --git a/src/main/java/InputHandler.java b/src/main/java/InputHandler.java index 6a444285..4500b5a1 100644 --- a/src/main/java/InputHandler.java +++ b/src/main/java/InputHandler.java @@ -3,16 +3,37 @@ import java.util.Scanner; public class InputHandler { - private Scanner scanner = new Scanner(System.in); + private final Scanner scanner = new Scanner(System.in); public int getPurchaseAmount() { System.out.println("구입금액을 입력해 주세요."); return scanner.nextInt(); } - public WinningNumbers getWinningNumbers() { - System.out.println("지난 주 당첨 번호를 입력해 주세요."); + public int getManualLottoCount() { + System.out.println("수동으로 구매할 로또 수를 입력해 주세요."); + int manualCount = scanner.nextInt(); scanner.nextLine(); + return manualCount; + } + + public List getManualLottos(int manualCount) { + List manualLottos = new ArrayList<>(); + System.out.println("수동으로 구매할 번호를 입력해 주세요."); + for (int i = 0; i < manualCount; i++) { + String input = scanner.nextLine(); + String[] inputs = input.split(", "); + List numbers = new ArrayList<>(); + for (String number : inputs) { + numbers.add(Integer.parseInt(number)); + } + manualLottos.add(new Lotto(numbers)); + } + return manualLottos; + } + + public WinningNumbers getWinningNumbers() { + System.out.println("지난 주 당첨 번호를 입력해 주세요. (예: 1, 2, 3, 4, 5, 6)"); String input = scanner.nextLine(); String[] inputs = input.split(", "); List winningNumbers = new ArrayList<>(); diff --git a/src/main/java/Lotto.java b/src/main/java/Lotto.java index a69d9080..664928ae 100644 --- a/src/main/java/Lotto.java +++ b/src/main/java/Lotto.java @@ -1,16 +1,26 @@ import java.util.Collections; +import java.util.HashSet; import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; public class Lotto { - private List numbers; + private final Set numbers; public Lotto(List numbers) { - Collections.sort(numbers); - this.numbers = numbers; + if (numbers.size() != 6) { + throw new IllegalArgumentException("로또 번호는 정확히 6개여야 합니다."); + } + this.numbers = numbers.stream() + .map(LottoNumber::new) + .collect(Collectors.toSet()); + if (this.numbers.size() != 6) { + throw new IllegalArgumentException("로또 번호는 중복되지 않아야 합니다."); + } } - public List getNumbers() { - return numbers; + public Set getNumbers() { + return Collections.unmodifiableSet(numbers); } @Override diff --git a/src/main/java/LottoApp.java b/src/main/java/LottoApp.java index 29e40f27..19ab3532 100644 --- a/src/main/java/LottoApp.java +++ b/src/main/java/LottoApp.java @@ -1,3 +1,4 @@ +import java.util.ArrayList; import java.util.List; public class LottoApp { @@ -6,16 +7,25 @@ public static void main(String[] args) { LottoMachine lottoMachine = new LottoMachine(); int purchaseAmount = inputHandler.getPurchaseAmount(); - int numberOfLottos = lottoMachine.calculateNumberOfLottos(purchaseAmount); - System.out.println(numberOfLottos + "개를 구매했습니다."); + int totalLottos = lottoMachine.calculateNumberOfLottos(purchaseAmount); - List lottos = lottoMachine.generateLottos(numberOfLottos); - for (Lotto lotto : lottos) { + int manualLottoCount = inputHandler.getManualLottoCount(); + List manualLottos = inputHandler.getManualLottos(manualLottoCount); + + int automaticLottoCount = totalLottos - manualLottoCount; + List automaticLottos = lottoMachine.generateAutomaticLottos(automaticLottoCount); + + List allLottos = new ArrayList<>(); + allLottos.addAll(manualLottos); + allLottos.addAll(automaticLottos); + + System.out.println("수동으로 " + manualLottoCount + "장, 자동으로 " + automaticLottoCount + "개를 구매했습니다."); + for (Lotto lotto : allLottos) { System.out.println(lotto); } WinningNumbers winningNumbers = inputHandler.getWinningNumbers(); - LottoGameResult gameResult = new LottoGameResult(lottos, winningNumbers); + LottoGameResult gameResult = new LottoGameResult(allLottos, winningNumbers); gameResult.printWinningStatistics(); } } diff --git a/src/main/java/LottoGameResult.java b/src/main/java/LottoGameResult.java index ae449985..67287976 100644 --- a/src/main/java/LottoGameResult.java +++ b/src/main/java/LottoGameResult.java @@ -1,11 +1,13 @@ import java.util.ArrayList; +import java.util.EnumMap; import java.util.List; +import java.util.Map; public class LottoGameResult { private static final int LOTTO_PRICE = 1000; - private List lottos; - private WinningNumbers winningNumbers; + private final List lottos; + private final WinningNumbers winningNumbers; public LottoGameResult(List lottos, WinningNumbers winningNumbers) { this.lottos = lottos; @@ -13,44 +15,55 @@ public LottoGameResult(List lottos, WinningNumbers winningNumbers) { } public void printWinningStatistics() { - List matchCount = new ArrayList<>(7); - for (int i = 0; i < 7; i++) { - matchCount.add(0); + Map matchCount = new EnumMap<>(WinningRank.class); + for (WinningRank rank : WinningRank.values()) { + matchCount.put(rank, 0); } for (Lotto lotto : lottos) { - int match = winningNumbers.countMatchingNumbers(lotto); - if (match >= 3) { - if (match == 5 && winningNumbers.containsBonusNumber(lotto)) { - matchCount.set(5, matchCount.get(5) + 1); - matchCount.set(6, matchCount.get(6) + 1); - } else { - matchCount.set(match, matchCount.get(match) + 1); - } - } + WinningRank rank = determineRank(lotto); + matchCount.put(rank, matchCount.get(rank) + 1); } printStatistics(matchCount); } - private void printStatistics(List matchCount) { + private WinningRank determineRank(Lotto lotto) { + int match = winningNumbers.countMatchingNumbers(lotto); + if (match == 6) { + return WinningRank.SIX; + } + if (match == 5 && winningNumbers.containsBonusNumber(lotto)) { + return WinningRank.FIVE_WITH_BONUS; + } + if (match == 5) { + return WinningRank.FIVE; + } + if (match == 4) { + return WinningRank.FOUR; + } + if (match == 3) { + return WinningRank.THREE; + } + return WinningRank.NONE; + } + + private void printStatistics(Map matchCount) { System.out.println("당첨 통계"); System.out.println("---------"); - System.out.println("3개 일치 (5000원) - " + matchCount.get(3) + "개"); - System.out.println("4개 일치 (50000원) - " + matchCount.get(4) + "개"); - System.out.println("5개 일치 (1500000원) - " + matchCount.get(5) + "개"); - System.out.println("5개 일치, 보너스 볼 일치(30000000원) - " + matchCount.get(6) + "개"); - System.out.println("6개 일치 (2000000000원) - " + matchCount.get(6) + "개"); + System.out.println("3개 일치 (5000원) - " + matchCount.get(WinningRank.THREE) + "개"); + System.out.println("4개 일치 (50000원) - " + matchCount.get(WinningRank.FOUR) + "개"); + System.out.println("5개 일치 (1500000원) - " + matchCount.get(WinningRank.FIVE) + "개"); + System.out.println("5개 일치, 보너스 볼 일치 (30000000원) - " + matchCount.get(WinningRank.FIVE_WITH_BONUS) + "개"); + System.out.println("6개 일치 (2000000000원) - " + matchCount.get(WinningRank.SIX) + "개"); double profitRate = calculateProfitRate(matchCount); System.out.printf("총 수익률은 %.4f입니다.%n", profitRate); } - private double calculateProfitRate(List matchCount) { + private double calculateProfitRate(Map matchCount) { int totalPrize = 0; - totalPrize += matchCount.get(3) * WinningRank.THREE.getPrize(); - totalPrize += matchCount.get(4) * WinningRank.FOUR.getPrize(); - totalPrize += matchCount.get(5) * WinningRank.FIVE.getPrize(); - totalPrize += matchCount.get(6) * WinningRank.FIVE_WITH_BONUS.getPrize(); - totalPrize += matchCount.get(6) * WinningRank.SIX.getPrize(); + for (WinningRank rank : WinningRank.values()) { + totalPrize += matchCount.get(rank) * rank.getPrize(); + } int totalSpent = LOTTO_PRICE * lottos.size(); return (double) totalPrize / totalSpent; } diff --git a/src/main/java/LottoMachine.java b/src/main/java/LottoMachine.java index e4932c8f..e341922f 100644 --- a/src/main/java/LottoMachine.java +++ b/src/main/java/LottoMachine.java @@ -1,6 +1,8 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.IntStream; public class LottoMachine { private static final int LOTTO_PRICE = 1000; @@ -20,11 +22,18 @@ public List generateLottos(int numberOfLottos) { } private Lotto generateLotto() { - List numbers = new ArrayList<>(); - for (int i = 1; i <= LOTTO_MAX_NUMBER; i++) { - numbers.add(i); - } + List numbers = IntStream.rangeClosed(1, LOTTO_MAX_NUMBER) + .boxed() + .collect(Collectors.toList()); Collections.shuffle(numbers); return new Lotto(numbers.subList(0, LOTTO_NUMBER_COUNT)); } + + public List generateAutomaticLottos(int automaticCount) { + List lottos = new ArrayList<>(); + for (int i = 0; i < automaticCount; i++) { + lottos.add(generateLotto()); + } + return lottos; + } } diff --git a/src/main/java/LottoNumber.java b/src/main/java/LottoNumber.java new file mode 100644 index 00000000..b0a78d30 --- /dev/null +++ b/src/main/java/LottoNumber.java @@ -0,0 +1,32 @@ +public class LottoNumber { + private final int number; + + public LottoNumber(int number) { + if (number < 1 || number > 45) { + throw new IllegalArgumentException("로또 번호는 1부터 45 사이의 숫자여야 합니다."); + } + this.number = number; + } + + public int getNumber() { + return 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; + } + + @Override + public int hashCode() { + return Integer.hashCode(number); + } + + @Override + public String toString() { + return String.valueOf(number); + } +} diff --git a/src/main/java/WinningNumbers.java b/src/main/java/WinningNumbers.java index f6bf9496..621e8b21 100644 --- a/src/main/java/WinningNumbers.java +++ b/src/main/java/WinningNumbers.java @@ -1,25 +1,29 @@ import java.util.Collections; import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; public class WinningNumbers { - private List numbers; - private int bonusNumber; + private final Set numbers; + private final LottoNumber bonusNumber; public WinningNumbers(List numbers, int bonusNumber) { - Collections.sort(numbers); - this.numbers = numbers; - this.bonusNumber = bonusNumber; + if (numbers.size() != 6) { + throw new IllegalArgumentException("당첨 번호는 정확히 6개여야 합니다."); + } + this.numbers = numbers.stream() + .map(LottoNumber::new) + .collect(Collectors.toSet()); + if (this.numbers.size() != 6) { + throw new IllegalArgumentException("당첨 번호는 중복되지 않아야 합니다."); + } + this.bonusNumber = new LottoNumber(bonusNumber); } public int countMatchingNumbers(Lotto lotto) { - List lottoNumbers = lotto.getNumbers(); - int matchCount = 0; - for (int number : lottoNumbers) { - if (numbers.contains(number)) { - matchCount++; - } - } - return matchCount; + return (int) lotto.getNumbers().stream() + .filter(numbers::contains) + .count(); } public boolean containsBonusNumber(Lotto lotto) { From f1c4e17baeec84f6a83c0819af384e9523f17aa5 Mon Sep 17 00:00:00 2001 From: seongjae6751 Date: Tue, 9 Jul 2024 16:28:48 +0900 Subject: [PATCH 5/6] step4 --- src/main/java/.gitkeep | 0 src/main/java/{LottoApp.java => Application.java} | 7 ++++++- src/main/java/InputHandler.java | 3 +++ src/main/java/{ => domain}/Lotto.java | 3 ++- src/main/java/{ => domain}/LottoGameResult.java | 3 ++- src/main/java/{ => domain}/LottoMachine.java | 2 ++ src/main/java/{ => domain}/LottoNumber.java | 2 ++ src/main/java/{ => domain}/WinningNumbers.java | 3 ++- src/main/java/{ => domain}/WinningRank.java | 2 ++ 9 files changed, 21 insertions(+), 4 deletions(-) delete mode 100644 src/main/java/.gitkeep rename src/main/java/{LottoApp.java => Application.java} (90%) rename src/main/java/{ => domain}/Lotto.java (96%) rename src/main/java/{ => domain}/LottoGameResult.java (98%) rename src/main/java/{ => domain}/LottoMachine.java (98%) rename src/main/java/{ => domain}/LottoNumber.java (97%) rename src/main/java/{ => domain}/WinningNumbers.java (97%) rename src/main/java/{ => domain}/WinningRank.java (96%) diff --git a/src/main/java/.gitkeep b/src/main/java/.gitkeep deleted file mode 100644 index e69de29b..00000000 diff --git a/src/main/java/LottoApp.java b/src/main/java/Application.java similarity index 90% rename from src/main/java/LottoApp.java rename to src/main/java/Application.java index 19ab3532..24a361c7 100644 --- a/src/main/java/LottoApp.java +++ b/src/main/java/Application.java @@ -1,7 +1,12 @@ import java.util.ArrayList; import java.util.List; -public class LottoApp { +import domain.Lotto; +import domain.LottoGameResult; +import domain.LottoMachine; +import domain.WinningNumbers; + +public class Application { public static void main(String[] args) { InputHandler inputHandler = new InputHandler(); LottoMachine lottoMachine = new LottoMachine(); diff --git a/src/main/java/InputHandler.java b/src/main/java/InputHandler.java index 4500b5a1..4ca527ea 100644 --- a/src/main/java/InputHandler.java +++ b/src/main/java/InputHandler.java @@ -2,6 +2,9 @@ import java.util.List; import java.util.Scanner; +import domain.Lotto; +import domain.WinningNumbers; + public class InputHandler { private final Scanner scanner = new Scanner(System.in); diff --git a/src/main/java/Lotto.java b/src/main/java/domain/Lotto.java similarity index 96% rename from src/main/java/Lotto.java rename to src/main/java/domain/Lotto.java index 664928ae..ba15fdde 100644 --- a/src/main/java/Lotto.java +++ b/src/main/java/domain/Lotto.java @@ -1,5 +1,6 @@ +package domain; + import java.util.Collections; -import java.util.HashSet; import java.util.List; import java.util.Set; import java.util.stream.Collectors; diff --git a/src/main/java/LottoGameResult.java b/src/main/java/domain/LottoGameResult.java similarity index 98% rename from src/main/java/LottoGameResult.java rename to src/main/java/domain/LottoGameResult.java index 67287976..763c5f4b 100644 --- a/src/main/java/LottoGameResult.java +++ b/src/main/java/domain/LottoGameResult.java @@ -1,4 +1,5 @@ -import java.util.ArrayList; +package domain; + import java.util.EnumMap; import java.util.List; import java.util.Map; diff --git a/src/main/java/LottoMachine.java b/src/main/java/domain/LottoMachine.java similarity index 98% rename from src/main/java/LottoMachine.java rename to src/main/java/domain/LottoMachine.java index e341922f..2c3899fa 100644 --- a/src/main/java/LottoMachine.java +++ b/src/main/java/domain/LottoMachine.java @@ -1,3 +1,5 @@ +package domain; + import java.util.ArrayList; import java.util.Collections; import java.util.List; diff --git a/src/main/java/LottoNumber.java b/src/main/java/domain/LottoNumber.java similarity index 97% rename from src/main/java/LottoNumber.java rename to src/main/java/domain/LottoNumber.java index b0a78d30..f8795cef 100644 --- a/src/main/java/LottoNumber.java +++ b/src/main/java/domain/LottoNumber.java @@ -1,3 +1,5 @@ +package domain; + public class LottoNumber { private final int number; diff --git a/src/main/java/WinningNumbers.java b/src/main/java/domain/WinningNumbers.java similarity index 97% rename from src/main/java/WinningNumbers.java rename to src/main/java/domain/WinningNumbers.java index 621e8b21..e5c64c46 100644 --- a/src/main/java/WinningNumbers.java +++ b/src/main/java/domain/WinningNumbers.java @@ -1,4 +1,5 @@ -import java.util.Collections; +package domain; + import java.util.List; import java.util.Set; import java.util.stream.Collectors; diff --git a/src/main/java/WinningRank.java b/src/main/java/domain/WinningRank.java similarity index 96% rename from src/main/java/WinningRank.java rename to src/main/java/domain/WinningRank.java index 43f8c781..1d3e81b6 100644 --- a/src/main/java/WinningRank.java +++ b/src/main/java/domain/WinningRank.java @@ -1,3 +1,5 @@ +package domain; + public enum WinningRank { THREE(3, 5000), FOUR(4, 50000), From b6896477285870942a2903468ceb502ee1307953 Mon Sep 17 00:00:00 2001 From: seongjae6751 Date: Tue, 9 Jul 2024 21:58:21 +0900 Subject: [PATCH 6/6] =?UTF-8?q?chore:=20=ED=94=BC=EB=93=9C=EB=B0=B1=20?= =?UTF-8?q?=EB=B0=98=EC=98=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/Application.java | 34 +---------- src/main/java/controller/LottoController.java | 60 +++++++++++++++++++ src/main/java/domain/LottoGameResult.java | 30 +++------- src/main/java/domain/LottoMachine.java | 33 ++++------ src/main/java/domain/WinningRank.java | 35 +++++++---- src/main/java/{ => view}/InputHandler.java | 36 +++++------ 6 files changed, 124 insertions(+), 104 deletions(-) create mode 100644 src/main/java/controller/LottoController.java rename src/main/java/{ => view}/InputHandler.java (53%) diff --git a/src/main/java/Application.java b/src/main/java/Application.java index 24a361c7..1f2fe1ba 100644 --- a/src/main/java/Application.java +++ b/src/main/java/Application.java @@ -1,36 +1,8 @@ -import java.util.ArrayList; -import java.util.List; - -import domain.Lotto; -import domain.LottoGameResult; -import domain.LottoMachine; -import domain.WinningNumbers; +import controller.LottoController; public class Application { public static void main(String[] args) { - InputHandler inputHandler = new InputHandler(); - LottoMachine lottoMachine = new LottoMachine(); - - int purchaseAmount = inputHandler.getPurchaseAmount(); - int totalLottos = lottoMachine.calculateNumberOfLottos(purchaseAmount); - - int manualLottoCount = inputHandler.getManualLottoCount(); - List manualLottos = inputHandler.getManualLottos(manualLottoCount); - - int automaticLottoCount = totalLottos - manualLottoCount; - List automaticLottos = lottoMachine.generateAutomaticLottos(automaticLottoCount); - - List allLottos = new ArrayList<>(); - allLottos.addAll(manualLottos); - allLottos.addAll(automaticLottos); - - System.out.println("수동으로 " + manualLottoCount + "장, 자동으로 " + automaticLottoCount + "개를 구매했습니다."); - for (Lotto lotto : allLottos) { - System.out.println(lotto); - } - - WinningNumbers winningNumbers = inputHandler.getWinningNumbers(); - LottoGameResult gameResult = new LottoGameResult(allLottos, winningNumbers); - gameResult.printWinningStatistics(); + LottoController lottoController = new LottoController(); + lottoController.run(); } } diff --git a/src/main/java/controller/LottoController.java b/src/main/java/controller/LottoController.java new file mode 100644 index 00000000..8b7407a4 --- /dev/null +++ b/src/main/java/controller/LottoController.java @@ -0,0 +1,60 @@ +package controller; + +import java.util.ArrayList; +import java.util.List; + +import domain.Lotto; +import domain.LottoGameResult; +import domain.LottoMachine; +import domain.WinningNumbers; +import view.InputHandler; + +public class LottoController { + private final InputHandler inputHandler = new InputHandler(); + private final LottoMachine lottoMachine = new LottoMachine(); + + public void run() { + int purchaseAmount = inputHandler.getPurchaseAmount(); + + int totalLottos = lottoMachine.calculateNumberOfLottos(purchaseAmount); + + int manualLottoCount = inputHandler.getManualLottoCount(); + + List manualLottoInputs = inputHandler.getManualLottoInputs(manualLottoCount); + List manualLottos = createManualLottos(manualLottoInputs); + + int automaticLottoCount = totalLottos - manualLottoCount; + + List automaticLottos = lottoMachine.generateAutomaticLottos(automaticLottoCount); + + List allLottos = new ArrayList<>(); + allLottos.addAll(manualLottos); + allLottos.addAll(automaticLottos); + + System.out.println("수동으로 " + manualLottoCount + "장, 자동으로 " + automaticLottoCount + "개를 구매했습니다."); + for (Lotto lotto : allLottos) { + System.out.println(lotto); + } + + List winningNumbers = inputHandler.getWinningNumbers(); + int bonusNumber = inputHandler.getBonusNumber(); + WinningNumbers winNums = new WinningNumbers(winningNumbers, bonusNumber); + System.out.println("당첨 번호: " + winNums); + + LottoGameResult gameResult = new LottoGameResult(allLottos, winNums); + gameResult.printWinningStatistics(); + } + + private List createManualLottos(List manualLottoInputs) { + List manualLottos = new ArrayList<>(); + for (String input : manualLottoInputs) { + String[] inputs = input.split("[,\\s]+"); + List numbers = new ArrayList<>(); + for (String number : inputs) { + numbers.add(Integer.parseInt(number)); + } + manualLottos.add(new Lotto(numbers)); + } + return manualLottos; + } +} diff --git a/src/main/java/domain/LottoGameResult.java b/src/main/java/domain/LottoGameResult.java index 763c5f4b..c8d06d19 100644 --- a/src/main/java/domain/LottoGameResult.java +++ b/src/main/java/domain/LottoGameResult.java @@ -30,32 +30,20 @@ public void printWinningStatistics() { private WinningRank determineRank(Lotto lotto) { int match = winningNumbers.countMatchingNumbers(lotto); - if (match == 6) { - return WinningRank.SIX; - } - if (match == 5 && winningNumbers.containsBonusNumber(lotto)) { - return WinningRank.FIVE_WITH_BONUS; - } - if (match == 5) { - return WinningRank.FIVE; - } - if (match == 4) { - return WinningRank.FOUR; - } - if (match == 3) { - return WinningRank.THREE; - } - return WinningRank.NONE; + boolean containsBonus = winningNumbers.containsBonusNumber(lotto); + return WinningRank.valueOf(match, containsBonus); } private void printStatistics(Map matchCount) { System.out.println("당첨 통계"); System.out.println("---------"); - System.out.println("3개 일치 (5000원) - " + matchCount.get(WinningRank.THREE) + "개"); - System.out.println("4개 일치 (50000원) - " + matchCount.get(WinningRank.FOUR) + "개"); - System.out.println("5개 일치 (1500000원) - " + matchCount.get(WinningRank.FIVE) + "개"); - System.out.println("5개 일치, 보너스 볼 일치 (30000000원) - " + matchCount.get(WinningRank.FIVE_WITH_BONUS) + "개"); - System.out.println("6개 일치 (2000000000원) - " + matchCount.get(WinningRank.SIX) + "개"); + + for (WinningRank rank : WinningRank.values()) { + if (rank != WinningRank.NONE) { + System.out.println(rank.getMessage() + " - " + matchCount.get(rank) + "개"); + } + } + double profitRate = calculateProfitRate(matchCount); System.out.printf("총 수익률은 %.4f입니다.%n", profitRate); } diff --git a/src/main/java/domain/LottoMachine.java b/src/main/java/domain/LottoMachine.java index 2c3899fa..628831dd 100644 --- a/src/main/java/domain/LottoMachine.java +++ b/src/main/java/domain/LottoMachine.java @@ -1,41 +1,34 @@ package domain; import java.util.ArrayList; -import java.util.Collections; import java.util.List; -import java.util.stream.Collectors; -import java.util.stream.IntStream; +import java.util.Random; public class LottoMachine { private static final int LOTTO_PRICE = 1000; private static final int LOTTO_NUMBER_COUNT = 6; - private static final int LOTTO_MAX_NUMBER = 45; public int calculateNumberOfLottos(int purchaseAmount) { return purchaseAmount / LOTTO_PRICE; } - public List generateLottos(int numberOfLottos) { + public List generateAutomaticLottos(int count) { List lottos = new ArrayList<>(); - for (int i = 0; i < numberOfLottos; i++) { - lottos.add(generateLotto()); + for (int i = 0; i < count; i++) { + lottos.add(generateRandomLotto()); } return lottos; } - private Lotto generateLotto() { - List numbers = IntStream.rangeClosed(1, LOTTO_MAX_NUMBER) - .boxed() - .collect(Collectors.toList()); - Collections.shuffle(numbers); - return new Lotto(numbers.subList(0, LOTTO_NUMBER_COUNT)); - } - - public List generateAutomaticLottos(int automaticCount) { - List lottos = new ArrayList<>(); - for (int i = 0; i < automaticCount; i++) { - lottos.add(generateLotto()); + private Lotto generateRandomLotto() { + Random random = new Random(); + List numbers = new ArrayList<>(); + while (numbers.size() < LOTTO_NUMBER_COUNT) { + int number = random.nextInt(45) + 1; + if (!numbers.contains(number)) { + numbers.add(number); + } } - return lottos; + return new Lotto(numbers); } } diff --git a/src/main/java/domain/WinningRank.java b/src/main/java/domain/WinningRank.java index 1d3e81b6..5cd2dbd2 100644 --- a/src/main/java/domain/WinningRank.java +++ b/src/main/java/domain/WinningRank.java @@ -1,26 +1,39 @@ package domain; public enum WinningRank { - THREE(3, 5000), - FOUR(4, 50000), - FIVE(5, 1500000), - FIVE_WITH_BONUS(5, 30000000), - SIX(6, 2000000000), - NONE(0, 0); + THREE(3, false, 5000, "3개 일치 (5000원)"), + FOUR(4, false, 50000, "4개 일치 (50000원)"), + FIVE(5, false, 1500000, "5개 일치 (1500000원)"), + FIVE_WITH_BONUS(5, true, 30000000, "5개 일치, 보너스 볼 일치 (30000000원)"), + SIX(6, false, 2000000000, "6개 일치 (2000000000원)"), + NONE(0, false, 0, ""); private final int matchCount; + private final boolean requiresBonus; private final int prize; + private final String message; - WinningRank(int matchCount, int prize) { + WinningRank(int matchCount, boolean requiresBonus, int prize, String message) { this.matchCount = matchCount; + this.requiresBonus = requiresBonus; this.prize = prize; - } - - public int getMatchCount() { - return matchCount; + this.message = message; } public int getPrize() { return prize; } + + public String getMessage() { + return message; + } + + public static WinningRank valueOf(int matchCount, boolean bonus) { + for (WinningRank rank : values()) { + if (rank.matchCount == matchCount && rank.requiresBonus == bonus) { + return rank; + } + } + return NONE; + } } diff --git a/src/main/java/InputHandler.java b/src/main/java/view/InputHandler.java similarity index 53% rename from src/main/java/InputHandler.java rename to src/main/java/view/InputHandler.java index 4ca527ea..fd41afb3 100644 --- a/src/main/java/InputHandler.java +++ b/src/main/java/view/InputHandler.java @@ -1,50 +1,44 @@ +package view; + import java.util.ArrayList; import java.util.List; import java.util.Scanner; -import domain.Lotto; -import domain.WinningNumbers; - public class InputHandler { private final Scanner scanner = new Scanner(System.in); public int getPurchaseAmount() { System.out.println("구입금액을 입력해 주세요."); - return scanner.nextInt(); + return Integer.parseInt(scanner.nextLine()); } public int getManualLottoCount() { System.out.println("수동으로 구매할 로또 수를 입력해 주세요."); - int manualCount = scanner.nextInt(); - scanner.nextLine(); - return manualCount; + return Integer.parseInt(scanner.nextLine()); } - public List getManualLottos(int manualCount) { - List manualLottos = new ArrayList<>(); + public List getManualLottoInputs(int manualCount) { + List manualLottoInputs = new ArrayList<>(); System.out.println("수동으로 구매할 번호를 입력해 주세요."); for (int i = 0; i < manualCount; i++) { - String input = scanner.nextLine(); - String[] inputs = input.split(", "); - List numbers = new ArrayList<>(); - for (String number : inputs) { - numbers.add(Integer.parseInt(number)); - } - manualLottos.add(new Lotto(numbers)); + manualLottoInputs.add(scanner.nextLine()); } - return manualLottos; + return manualLottoInputs; } - public WinningNumbers getWinningNumbers() { + public List getWinningNumbers() { System.out.println("지난 주 당첨 번호를 입력해 주세요. (예: 1, 2, 3, 4, 5, 6)"); String input = scanner.nextLine(); - String[] inputs = input.split(", "); + String[] inputs = input.split("[,\\s]+"); List winningNumbers = new ArrayList<>(); for (String number : inputs) { winningNumbers.add(Integer.parseInt(number)); } + return winningNumbers; + } + + public int getBonusNumber() { System.out.println("보너스 볼을 입력해 주세요."); - int bonusNumber = scanner.nextInt(); - return new WinningNumbers(winningNumbers, bonusNumber); + return Integer.parseInt(scanner.nextLine()); } }