From b5efec7b0e9a3a9b396b0d4f7410db87820acbf6 Mon Sep 17 00:00:00 2001 From: jgw1202 Date: Tue, 9 Jul 2024 13:28:48 +0900 Subject: [PATCH 01/14] =?UTF-8?q?feat=20:=20"=EC=A0=84=EC=B2=B4=20?= =?UTF-8?q?=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84=20=EB=B0=8F=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8=20=EC=99=84=EB=A3=8C"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/lotto/Application.java | 160 +++++++++++++++++++++++++++ src/main/java/lotto/Lotto.java | 19 +++- 2 files changed, 178 insertions(+), 1 deletion(-) diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java index d190922ba44..602d3465c02 100644 --- a/src/main/java/lotto/Application.java +++ b/src/main/java/lotto/Application.java @@ -1,7 +1,167 @@ package lotto; +import camp.nextstep.edu.missionutils.Randoms; +import camp.nextstep.edu.missionutils.Console; + +import java.util.*; + public class Application { + + private static final int LOTTO_PRICE = 1000; + private static final int LOTTO_NUMBERS_COUNT = 6; + private static final int LOTTO_NUMBER_MIN = 1; + private static final int LOTTO_NUMBER_MAX = 45; + private static final Map PRIZES = Map.of( + 6, 2_000_000_000, + 5, 1_500_000, + 4, 50_000, + 3, 5_000 + ); + public static void main(String[] args) { // TODO: 프로그램 구현 + + try { + int purchaseAmount = getPurchaseAmount(); + int numberOfLottos = purchaseAmount / LOTTO_PRICE; + + List purchasedLottos = generateLottos(numberOfLottos); + printPurchasedLottos(purchasedLottos); + + Set winningNumbers = getWinningNumbers(); + int bonusNumber = getBonusNumber(winningNumbers); + + Map winningResult = checkWinning(purchasedLottos, winningNumbers, bonusNumber); + printWinningResult(winningResult); + + double profitRate = calculateProfitRate(winningResult, purchaseAmount); + System.out.printf("총 수익률은 %.1f%%입니다.%n", profitRate); + } catch (IllegalArgumentException e) { + System.out.println("[ERROR] " + e.getMessage()); + } + } + private static int getPurchaseAmount() { + System.out.println("구입금액을 입력해 주세요."); + String input = Console.readLine(); + int purchaseAmount; + try { + purchaseAmount = Integer.parseInt(input); + } catch (NumberFormatException e) { + throw new IllegalArgumentException("구입 금액은 숫자여야 합니다."); + } + if (purchaseAmount % LOTTO_PRICE != 0) { + throw new IllegalArgumentException("구입 금액은 1,000원 단위여야 합니다."); + } + return purchaseAmount; + } + + private static List generateLottos(int count) { + List lottos = new ArrayList<>(); + + for (int i = 0; i < count; i++) { + List numbers = Randoms.pickUniqueNumbersInRange(LOTTO_NUMBER_MIN, LOTTO_NUMBER_MAX, LOTTO_NUMBERS_COUNT); + lottos.add(new Lotto(numbers)); + } + return lottos; + } + + private static void printPurchasedLottos(List lottos) { + System.out.printf("%d개를 구매했습니다.%n", lottos.size()); + for (Lotto lotto : lottos) { + System.out.println(lotto.getNumbers()); + } } + + private static Set getWinningNumbers() { + System.out.println("당첨 번호를 입력해 주세요."); + String input = Console.readLine(); + String[] split = input.split(","); + if (split.length != LOTTO_NUMBERS_COUNT) { + throw new IllegalArgumentException("당첨 번호는 6개여야 합니다."); + } + + Set winningNumbers = new HashSet<>(); + for (String number : split) { + int num; + try { + num = Integer.parseInt(number); + } catch (NumberFormatException e) { + throw new IllegalArgumentException("당첨 번호는 숫자여야 합니다."); + } + if (num < LOTTO_NUMBER_MIN || num > LOTTO_NUMBER_MAX) { + throw new IllegalArgumentException("로또 번호는 1부터 45 사이의 숫자여야 합니다."); + } + winningNumbers.add(num); + } + if (winningNumbers.size() != LOTTO_NUMBERS_COUNT) { + throw new IllegalArgumentException("당첨 번호는 중복되지 않는 6개의 숫자여야 합니다."); + } + return winningNumbers; + } + + private static int getBonusNumber(Set winningNumbers) { + System.out.println("보너스 번호를 입력해 주세요."); + String input = Console.readLine(); + int bonusNumber; + try { + bonusNumber = Integer.parseInt(input); + } catch (NumberFormatException e) { + throw new IllegalArgumentException("보너스 번호는 숫자여야 합니다."); + } + if (bonusNumber < LOTTO_NUMBER_MIN || bonusNumber > LOTTO_NUMBER_MAX) { + throw new IllegalArgumentException("로또 번호는 1부터 45 사이의 숫자여야 합니다."); + } + if (winningNumbers.contains(bonusNumber)) { + throw new IllegalArgumentException("보너스 번호는 당첨 번호와 중복되지 않아야 합니다."); + } + return bonusNumber; + } + + private static Map checkWinning(List purchasedLottos, Set winningNumbers, int bonusNumber) { + Map result = new HashMap<>(); + result.put("3개", 0); + result.put("4개", 0); + result.put("5개", 0); + result.put("5개+보너스", 0); + result.put("6개", 0); + + for (Lotto lotto : purchasedLottos) { + Set intersection = new HashSet<>(lotto.getNumbers()); + intersection.retainAll(winningNumbers); + int matchCount = intersection.size(); + + if (matchCount == 6) { + result.put("6개", result.get("6개") + 1); + } else if (matchCount == 5 && lotto.getNumbers().contains(bonusNumber)) { + result.put("5개+보너스", result.get("5개+보너스") + 1); + } else if (matchCount == 5) { + result.put("5개", result.get("5개") + 1); + } else if (matchCount == 4) { + result.put("4개", result.get("4개") + 1); + } else if (matchCount == 3) { + result.put("3개", result.get("3개") + 1); + } + } + return result; + } + + private static void printWinningResult(Map winningResult) { + System.out.println("당첨 통계\n---"); + System.out.printf("3개 일치 (5,000원) - %d개%n", winningResult.get("3개")); + System.out.printf("4개 일치 (50,000원) - %d개%n", winningResult.get("4개")); + System.out.printf("5개 일치 (1,500,000원) - %d개%n", winningResult.get("5개")); + System.out.printf("5개 일치, 보너스 볼 일치 (30,000,000원) - %d개%n", winningResult.get("5개+보너스")); + System.out.printf("6개 일치 (2,000,000,000원) - %d개%n", winningResult.get("6개")); + } + + private static double calculateProfitRate(Map winningResult, int purchaseAmount) { + int totalWinnings = winningResult.get("6개") * PRIZES.get(6) + + winningResult.get("5개+보너스") * 30_000_000 + + winningResult.get("5개") * PRIZES.get(5) + + winningResult.get("4개") * PRIZES.get(4) + + winningResult.get("3개") * PRIZES.get(3); + + return ((double) totalWinnings / purchaseAmount) * 100; + } + } diff --git a/src/main/java/lotto/Lotto.java b/src/main/java/lotto/Lotto.java index 519793d1f73..ca4643bbe0c 100644 --- a/src/main/java/lotto/Lotto.java +++ b/src/main/java/lotto/Lotto.java @@ -1,6 +1,11 @@ package lotto; +import camp.nextstep.edu.missionutils.Randoms; +import camp.nextstep.edu.missionutils.Console; + +import java.util.HashSet; import java.util.List; +import java.util.Set; public class Lotto { private final List numbers; @@ -12,9 +17,21 @@ public Lotto(List numbers) { private void validate(List numbers) { if (numbers.size() != 6) { - throw new IllegalArgumentException(); + throw new IllegalArgumentException("로또 번호는 6개여야 합니다."); + } + Set numberSet = new HashSet<>(); + for (int number : numbers) { + if (number < 1 || number > 45) { + throw new IllegalArgumentException("로또 번호는 1부터 45 사이의 숫자여야 합니다."); + } + if (!numberSet.add(number)) { + throw new IllegalArgumentException("로또 번호는 중복될 수 없습니다."); + } } } // TODO: 추가 기능 구현 + public List getNumbers() { + return numbers; + } } From 911d97bf1c7c97243579e07568cd5a31a444f30e Mon Sep 17 00:00:00 2001 From: jgw1202 Date: Tue, 9 Jul 2024 13:39:00 +0900 Subject: [PATCH 02/14] =?UTF-8?q?refactor=20:=20"=EA=B8=B0=EB=8A=A5?= =?UTF-8?q?=EB=B3=84=EB=A1=9C=20=ED=81=B4=EB=9E=98=EC=8A=A4=20=ED=8C=8C?= =?UTF-8?q?=EC=9D=BC=20=EB=B6=84=ED=95=A0"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/lotto/Application.java | 167 ++++--------------------- src/main/java/lotto/ConsoleInput.java | 68 ++++++++++ src/main/java/lotto/LottoMachine.java | 21 ++++ src/main/java/lotto/WinningResult.java | 61 +++++++++ 4 files changed, 176 insertions(+), 141 deletions(-) create mode 100644 src/main/java/lotto/ConsoleInput.java create mode 100644 src/main/java/lotto/LottoMachine.java create mode 100644 src/main/java/lotto/WinningResult.java diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java index 602d3465c02..50061555692 100644 --- a/src/main/java/lotto/Application.java +++ b/src/main/java/lotto/Application.java @@ -1,167 +1,52 @@ package lotto; -import camp.nextstep.edu.missionutils.Randoms; -import camp.nextstep.edu.missionutils.Console; - -import java.util.*; +import java.util.List; +import java.util.Set; public class Application { + private final ConsoleInput consoleInput; + private final LottoMachine lottoMachine; + private final WinningResult winningResult; - private static final int LOTTO_PRICE = 1000; - private static final int LOTTO_NUMBERS_COUNT = 6; - private static final int LOTTO_NUMBER_MIN = 1; - private static final int LOTTO_NUMBER_MAX = 45; - private static final Map PRIZES = Map.of( - 6, 2_000_000_000, - 5, 1_500_000, - 4, 50_000, - 3, 5_000 - ); - - public static void main(String[] args) { - // TODO: 프로그램 구현 + public Application(ConsoleInput consoleInput, LottoMachine lottoMachine, WinningResult winningResult) { + this.consoleInput = consoleInput; + this.lottoMachine = lottoMachine; + this.winningResult = winningResult; + } + public void run() { try { - int purchaseAmount = getPurchaseAmount(); - int numberOfLottos = purchaseAmount / LOTTO_PRICE; + int purchaseAmount = consoleInput.getPurchaseAmount(); + int numberOfLottos = purchaseAmount / 1000; - List purchasedLottos = generateLottos(numberOfLottos); + List purchasedLottos = lottoMachine.generateLottos(numberOfLottos); printPurchasedLottos(purchasedLottos); - Set winningNumbers = getWinningNumbers(); - int bonusNumber = getBonusNumber(winningNumbers); + Set winningNumbers = consoleInput.getWinningNumbers(); + int bonusNumber = consoleInput.getBonusNumber(winningNumbers); - Map winningResult = checkWinning(purchasedLottos, winningNumbers, bonusNumber); - printWinningResult(winningResult); + winningResult.checkWinning(purchasedLottos, winningNumbers, bonusNumber); + winningResult.printWinningResult(); - double profitRate = calculateProfitRate(winningResult, purchaseAmount); + double profitRate = winningResult.calculateProfitRate(purchaseAmount); System.out.printf("총 수익률은 %.1f%%입니다.%n", profitRate); } catch (IllegalArgumentException e) { System.out.println("[ERROR] " + e.getMessage()); } } - private static int getPurchaseAmount() { - System.out.println("구입금액을 입력해 주세요."); - String input = Console.readLine(); - int purchaseAmount; - try { - purchaseAmount = Integer.parseInt(input); - } catch (NumberFormatException e) { - throw new IllegalArgumentException("구입 금액은 숫자여야 합니다."); - } - if (purchaseAmount % LOTTO_PRICE != 0) { - throw new IllegalArgumentException("구입 금액은 1,000원 단위여야 합니다."); - } - return purchaseAmount; - } - - private static List generateLottos(int count) { - List lottos = new ArrayList<>(); - for (int i = 0; i < count; i++) { - List numbers = Randoms.pickUniqueNumbersInRange(LOTTO_NUMBER_MIN, LOTTO_NUMBER_MAX, LOTTO_NUMBERS_COUNT); - lottos.add(new Lotto(numbers)); - } - return lottos; - } - - private static void printPurchasedLottos(List lottos) { + private void printPurchasedLottos(List lottos) { System.out.printf("%d개를 구매했습니다.%n", lottos.size()); for (Lotto lotto : lottos) { System.out.println(lotto.getNumbers()); } } - private static Set getWinningNumbers() { - System.out.println("당첨 번호를 입력해 주세요."); - String input = Console.readLine(); - String[] split = input.split(","); - if (split.length != LOTTO_NUMBERS_COUNT) { - throw new IllegalArgumentException("당첨 번호는 6개여야 합니다."); - } - - Set winningNumbers = new HashSet<>(); - for (String number : split) { - int num; - try { - num = Integer.parseInt(number); - } catch (NumberFormatException e) { - throw new IllegalArgumentException("당첨 번호는 숫자여야 합니다."); - } - if (num < LOTTO_NUMBER_MIN || num > LOTTO_NUMBER_MAX) { - throw new IllegalArgumentException("로또 번호는 1부터 45 사이의 숫자여야 합니다."); - } - winningNumbers.add(num); - } - if (winningNumbers.size() != LOTTO_NUMBERS_COUNT) { - throw new IllegalArgumentException("당첨 번호는 중복되지 않는 6개의 숫자여야 합니다."); - } - return winningNumbers; - } - - private static int getBonusNumber(Set winningNumbers) { - System.out.println("보너스 번호를 입력해 주세요."); - String input = Console.readLine(); - int bonusNumber; - try { - bonusNumber = Integer.parseInt(input); - } catch (NumberFormatException e) { - throw new IllegalArgumentException("보너스 번호는 숫자여야 합니다."); - } - if (bonusNumber < LOTTO_NUMBER_MIN || bonusNumber > LOTTO_NUMBER_MAX) { - throw new IllegalArgumentException("로또 번호는 1부터 45 사이의 숫자여야 합니다."); - } - if (winningNumbers.contains(bonusNumber)) { - throw new IllegalArgumentException("보너스 번호는 당첨 번호와 중복되지 않아야 합니다."); - } - return bonusNumber; - } - - private static Map checkWinning(List purchasedLottos, Set winningNumbers, int bonusNumber) { - Map result = new HashMap<>(); - result.put("3개", 0); - result.put("4개", 0); - result.put("5개", 0); - result.put("5개+보너스", 0); - result.put("6개", 0); - - for (Lotto lotto : purchasedLottos) { - Set intersection = new HashSet<>(lotto.getNumbers()); - intersection.retainAll(winningNumbers); - int matchCount = intersection.size(); - - if (matchCount == 6) { - result.put("6개", result.get("6개") + 1); - } else if (matchCount == 5 && lotto.getNumbers().contains(bonusNumber)) { - result.put("5개+보너스", result.get("5개+보너스") + 1); - } else if (matchCount == 5) { - result.put("5개", result.get("5개") + 1); - } else if (matchCount == 4) { - result.put("4개", result.get("4개") + 1); - } else if (matchCount == 3) { - result.put("3개", result.get("3개") + 1); - } - } - return result; - } - - private static void printWinningResult(Map winningResult) { - System.out.println("당첨 통계\n---"); - System.out.printf("3개 일치 (5,000원) - %d개%n", winningResult.get("3개")); - System.out.printf("4개 일치 (50,000원) - %d개%n", winningResult.get("4개")); - System.out.printf("5개 일치 (1,500,000원) - %d개%n", winningResult.get("5개")); - System.out.printf("5개 일치, 보너스 볼 일치 (30,000,000원) - %d개%n", winningResult.get("5개+보너스")); - System.out.printf("6개 일치 (2,000,000,000원) - %d개%n", winningResult.get("6개")); - } - - private static double calculateProfitRate(Map winningResult, int purchaseAmount) { - int totalWinnings = winningResult.get("6개") * PRIZES.get(6) - + winningResult.get("5개+보너스") * 30_000_000 - + winningResult.get("5개") * PRIZES.get(5) - + winningResult.get("4개") * PRIZES.get(4) - + winningResult.get("3개") * PRIZES.get(3); - - return ((double) totalWinnings / purchaseAmount) * 100; + public static void main(String[] args) { + ConsoleInput consoleInput = new ConsoleInput(); + LottoMachine lottoMachine = new LottoMachine(); + WinningResult winningResult = new WinningResult(); + Application lottoGame = new Application(consoleInput, lottoMachine, winningResult); + lottoGame.run(); } - } diff --git a/src/main/java/lotto/ConsoleInput.java b/src/main/java/lotto/ConsoleInput.java new file mode 100644 index 00000000000..f447fe30ca3 --- /dev/null +++ b/src/main/java/lotto/ConsoleInput.java @@ -0,0 +1,68 @@ +package lotto; + +import camp.nextstep.edu.missionutils.Console; + +import java.util.HashSet; +import java.util.Set; + +public class ConsoleInput { + public int getPurchaseAmount() { + System.out.println("구입금액을 입력해 주세요."); + String input = Console.readLine(); + int purchaseAmount; + try { + purchaseAmount = Integer.parseInt(input); + } catch (NumberFormatException e) { + throw new IllegalArgumentException("구입 금액은 숫자여야 합니다."); + } + if (purchaseAmount % 1000 != 0) { + throw new IllegalArgumentException("구입 금액은 1,000원 단위여야 합니다."); + } + return purchaseAmount; + } + + public Set getWinningNumbers() { + System.out.println("당첨 번호를 입력해 주세요."); + String input = Console.readLine(); + String[] split = input.split(","); + if (split.length != 6) { + throw new IllegalArgumentException("당첨 번호는 6개여야 합니다."); + } + + Set winningNumbers = new HashSet<>(); + for (String number : split) { + int num; + try { + num = Integer.parseInt(number); + } catch (NumberFormatException e) { + throw new IllegalArgumentException("당첨 번호는 숫자여야 합니다."); + } + if (num < 1 || num > 45) { + throw new IllegalArgumentException("로또 번호는 1부터 45 사이의 숫자여야 합니다."); + } + winningNumbers.add(num); + } + if (winningNumbers.size() != 6) { + throw new IllegalArgumentException("당첨 번호는 중복되지 않는 6개의 숫자여야 합니다."); + } + return winningNumbers; + } + + public int getBonusNumber(Set winningNumbers) { + System.out.println("보너스 번호를 입력해 주세요."); + String input = Console.readLine(); + int bonusNumber; + try { + bonusNumber = Integer.parseInt(input); + } catch (NumberFormatException e) { + throw new IllegalArgumentException("보너스 번호는 숫자여야 합니다."); + } + if (bonusNumber < 1 || bonusNumber > 45) { + throw new IllegalArgumentException("로또 번호는 1부터 45 사이의 숫자여야 합니다."); + } + if (winningNumbers.contains(bonusNumber)) { + throw new IllegalArgumentException("보너스 번호는 당첨 번호와 중복되지 않아야 합니다."); + } + return bonusNumber; + } +} diff --git a/src/main/java/lotto/LottoMachine.java b/src/main/java/lotto/LottoMachine.java new file mode 100644 index 00000000000..5dd6462c163 --- /dev/null +++ b/src/main/java/lotto/LottoMachine.java @@ -0,0 +1,21 @@ +package lotto; + +import camp.nextstep.edu.missionutils.Randoms; + +import java.util.ArrayList; +import java.util.List; + +public class LottoMachine { + private static final int LOTTO_NUMBER_MIN = 1; + private static final int LOTTO_NUMBER_MAX = 45; + private static final int LOTTO_NUMBERS_COUNT = 6; + + public List generateLottos(int count) { + List lottos = new ArrayList<>(); + for (int i = 0; i < count; i++) { + List numbers = Randoms.pickUniqueNumbersInRange(LOTTO_NUMBER_MIN, LOTTO_NUMBER_MAX, LOTTO_NUMBERS_COUNT); + lottos.add(new Lotto(numbers)); + } + return lottos; + } +} diff --git a/src/main/java/lotto/WinningResult.java b/src/main/java/lotto/WinningResult.java new file mode 100644 index 00000000000..64c1ef7ea7e --- /dev/null +++ b/src/main/java/lotto/WinningResult.java @@ -0,0 +1,61 @@ +package lotto; + +import java.util.*; + +public class WinningResult { + private static final Map PRIZES = Map.of( + 6, 2_000_000_000, + 5, 1_500_000, + 4, 50_000, + 3, 5_000 + ); + + private final Map result = new HashMap<>(); + + public WinningResult() { + result.put("3개", 0); + result.put("4개", 0); + result.put("5개", 0); + result.put("5개+보너스", 0); + result.put("6개", 0); + } + + public void checkWinning(List purchasedLottos, Set winningNumbers, int bonusNumber) { + for (Lotto lotto : purchasedLottos) { + Set intersection = new HashSet<>(lotto.getNumbers()); + intersection.retainAll(winningNumbers); + int matchCount = intersection.size(); + + if (matchCount == 6) { + result.put("6개", result.get("6개") + 1); + } else if (matchCount == 5 && lotto.getNumbers().contains(bonusNumber)) { + result.put("5개+보너스", result.get("5개+보너스") + 1); + } else if (matchCount == 5) { + result.put("5개", result.get("5개") + 1); + } else if (matchCount == 4) { + result.put("4개", result.get("4개") + 1); + } else if (matchCount == 3) { + result.put("3개", result.get("3개") + 1); + } + } + } + + public void printWinningResult() { + System.out.println("당첨 통계\n---"); + System.out.printf("3개 일치 (5,000원) - %d개%n", result.get("3개")); + System.out.printf("4개 일치 (50,000원) - %d개%n", result.get("4개")); + System.out.printf("5개 일치 (1,500,000원) - %d개%n", result.get("5개")); + System.out.printf("5개 일치, 보너스 볼 일치 (30,000,000원) - %d개%n", result.get("5개+보너스")); + System.out.printf("6개 일치 (2,000,000,000원) - %d개%n", result.get("6개")); + } + + public double calculateProfitRate(int purchaseAmount) { + int totalWinnings = result.get("6개") * PRIZES.get(6) + + result.get("5개+보너스") * 30_000_000 + + result.get("5개") * PRIZES.get(5) + + result.get("4개") * PRIZES.get(4) + + result.get("3개") * PRIZES.get(3); + + return ((double) totalWinnings / purchaseAmount) * 100; + } +} From 2d4a2097a90f7c112f483ac394c84d59d16cb9b8 Mon Sep 17 00:00:00 2001 From: jgw1202 Date: Tue, 9 Jul 2024 13:43:13 +0900 Subject: [PATCH 03/14] =?UTF-8?q?feat=20/=20fix=20:=20"=EC=9D=98=EC=A1=B4?= =?UTF-8?q?=EC=84=B1=20=EC=A3=BC=EC=9E=85=EC=9D=84=20=EC=9C=84=ED=95=9C=20?= =?UTF-8?q?appConfig=20=ED=81=B4=EB=9E=98=EC=8A=A4=20=EC=83=9D=EC=84=B1=20?= =?UTF-8?q?=EB=B0=8F=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/lotto/AppConfig.java | 19 +++++++++++++++++++ src/main/java/lotto/Application.java | 6 ++---- 2 files changed, 21 insertions(+), 4 deletions(-) create mode 100644 src/main/java/lotto/AppConfig.java diff --git a/src/main/java/lotto/AppConfig.java b/src/main/java/lotto/AppConfig.java new file mode 100644 index 00000000000..c9b69b51dd6 --- /dev/null +++ b/src/main/java/lotto/AppConfig.java @@ -0,0 +1,19 @@ +package lotto; + +public class AppConfig { + public ConsoleInput consoleInput() { + return new ConsoleInput(); + } + + public LottoMachine lottoMachine() { + return new LottoMachine(); + } + + public WinningResult winningResult() { + return new WinningResult(); + } + + public Application application() { + return new Application(consoleInput(), lottoMachine(), winningResult()); + } +} diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java index 50061555692..f77a5dcdd04 100644 --- a/src/main/java/lotto/Application.java +++ b/src/main/java/lotto/Application.java @@ -43,10 +43,8 @@ private void printPurchasedLottos(List lottos) { } public static void main(String[] args) { - ConsoleInput consoleInput = new ConsoleInput(); - LottoMachine lottoMachine = new LottoMachine(); - WinningResult winningResult = new WinningResult(); - Application lottoGame = new Application(consoleInput, lottoMachine, winningResult); + AppConfig appConfig = new AppConfig(); + Application lottoGame = appConfig.application(); lottoGame.run(); } } From d37310db4626422dd1b8eba10c9f98a9c37c0509 Mon Sep 17 00:00:00 2001 From: jgw1202 Date: Tue, 9 Jul 2024 14:29:00 +0900 Subject: [PATCH 04/14] test commit --- src/main/java/lotto/Application.java | 167 ++++---------------------- src/main/java/lotto/Lotto.java | 2 +- src/main/java/lotto/LottoMachine.java | 21 ++++ 3 files changed, 47 insertions(+), 143 deletions(-) create mode 100644 src/main/java/lotto/LottoMachine.java diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java index 602d3465c02..912a988e006 100644 --- a/src/main/java/lotto/Application.java +++ b/src/main/java/lotto/Application.java @@ -1,167 +1,50 @@ package lotto; -import camp.nextstep.edu.missionutils.Randoms; -import camp.nextstep.edu.missionutils.Console; - -import java.util.*; +import java.util.List; +import java.util.Set; public class Application { + private final ConsoleInput consoleInput; + private final LottoMachine lottoMachine; + private final WinningResult winningResult; - private static final int LOTTO_PRICE = 1000; - private static final int LOTTO_NUMBERS_COUNT = 6; - private static final int LOTTO_NUMBER_MIN = 1; - private static final int LOTTO_NUMBER_MAX = 45; - private static final Map PRIZES = Map.of( - 6, 2_000_000_000, - 5, 1_500_000, - 4, 50_000, - 3, 5_000 - ); - - public static void main(String[] args) { - // TODO: 프로그램 구현 + public Application(ConsoleInput consoleInput, LottoMachine lottoMachine, WinningResult winningResult) { + this.consoleInput = consoleInput; + this.lottoMachine = lottoMachine; + this.winningResult = winningResult; + } + public void run() { try { - int purchaseAmount = getPurchaseAmount(); - int numberOfLottos = purchaseAmount / LOTTO_PRICE; + int purchaseAmount = consoleInput.getPurchaseAmount(); + int numberOfLottos = purchaseAmount / 1000; - List purchasedLottos = generateLottos(numberOfLottos); + List purchasedLottos = lottoMachine.generateLottos(numberOfLottos); printPurchasedLottos(purchasedLottos); - Set winningNumbers = getWinningNumbers(); - int bonusNumber = getBonusNumber(winningNumbers); + Set winningNumbers = consoleInput.getWinningNumbers(); + int bonusNumber = consoleInput.getBonusNumber(winningNumbers); - Map winningResult = checkWinning(purchasedLottos, winningNumbers, bonusNumber); - printWinningResult(winningResult); + winningResult.checkWinning(purchasedLottos, winningNumbers, bonusNumber); + winningResult.printWinningResult(); - double profitRate = calculateProfitRate(winningResult, purchaseAmount); + double profitRate = winningResult.calculateProfitRate(purchaseAmount); System.out.printf("총 수익률은 %.1f%%입니다.%n", profitRate); } catch (IllegalArgumentException e) { System.out.println("[ERROR] " + e.getMessage()); } } - private static int getPurchaseAmount() { - System.out.println("구입금액을 입력해 주세요."); - String input = Console.readLine(); - int purchaseAmount; - try { - purchaseAmount = Integer.parseInt(input); - } catch (NumberFormatException e) { - throw new IllegalArgumentException("구입 금액은 숫자여야 합니다."); - } - if (purchaseAmount % LOTTO_PRICE != 0) { - throw new IllegalArgumentException("구입 금액은 1,000원 단위여야 합니다."); - } - return purchaseAmount; - } - - private static List generateLottos(int count) { - List lottos = new ArrayList<>(); - for (int i = 0; i < count; i++) { - List numbers = Randoms.pickUniqueNumbersInRange(LOTTO_NUMBER_MIN, LOTTO_NUMBER_MAX, LOTTO_NUMBERS_COUNT); - lottos.add(new Lotto(numbers)); - } - return lottos; - } - - private static void printPurchasedLottos(List lottos) { + private void printPurchasedLottos(List lottos) { System.out.printf("%d개를 구매했습니다.%n", lottos.size()); for (Lotto lotto : lottos) { System.out.println(lotto.getNumbers()); } } - private static Set getWinningNumbers() { - System.out.println("당첨 번호를 입력해 주세요."); - String input = Console.readLine(); - String[] split = input.split(","); - if (split.length != LOTTO_NUMBERS_COUNT) { - throw new IllegalArgumentException("당첨 번호는 6개여야 합니다."); - } - - Set winningNumbers = new HashSet<>(); - for (String number : split) { - int num; - try { - num = Integer.parseInt(number); - } catch (NumberFormatException e) { - throw new IllegalArgumentException("당첨 번호는 숫자여야 합니다."); - } - if (num < LOTTO_NUMBER_MIN || num > LOTTO_NUMBER_MAX) { - throw new IllegalArgumentException("로또 번호는 1부터 45 사이의 숫자여야 합니다."); - } - winningNumbers.add(num); - } - if (winningNumbers.size() != LOTTO_NUMBERS_COUNT) { - throw new IllegalArgumentException("당첨 번호는 중복되지 않는 6개의 숫자여야 합니다."); - } - return winningNumbers; - } - - private static int getBonusNumber(Set winningNumbers) { - System.out.println("보너스 번호를 입력해 주세요."); - String input = Console.readLine(); - int bonusNumber; - try { - bonusNumber = Integer.parseInt(input); - } catch (NumberFormatException e) { - throw new IllegalArgumentException("보너스 번호는 숫자여야 합니다."); - } - if (bonusNumber < LOTTO_NUMBER_MIN || bonusNumber > LOTTO_NUMBER_MAX) { - throw new IllegalArgumentException("로또 번호는 1부터 45 사이의 숫자여야 합니다."); - } - if (winningNumbers.contains(bonusNumber)) { - throw new IllegalArgumentException("보너스 번호는 당첨 번호와 중복되지 않아야 합니다."); - } - return bonusNumber; - } - - private static Map checkWinning(List purchasedLottos, Set winningNumbers, int bonusNumber) { - Map result = new HashMap<>(); - result.put("3개", 0); - result.put("4개", 0); - result.put("5개", 0); - result.put("5개+보너스", 0); - result.put("6개", 0); - - for (Lotto lotto : purchasedLottos) { - Set intersection = new HashSet<>(lotto.getNumbers()); - intersection.retainAll(winningNumbers); - int matchCount = intersection.size(); - - if (matchCount == 6) { - result.put("6개", result.get("6개") + 1); - } else if (matchCount == 5 && lotto.getNumbers().contains(bonusNumber)) { - result.put("5개+보너스", result.get("5개+보너스") + 1); - } else if (matchCount == 5) { - result.put("5개", result.get("5개") + 1); - } else if (matchCount == 4) { - result.put("4개", result.get("4개") + 1); - } else if (matchCount == 3) { - result.put("3개", result.get("3개") + 1); - } - } - return result; - } - - private static void printWinningResult(Map winningResult) { - System.out.println("당첨 통계\n---"); - System.out.printf("3개 일치 (5,000원) - %d개%n", winningResult.get("3개")); - System.out.printf("4개 일치 (50,000원) - %d개%n", winningResult.get("4개")); - System.out.printf("5개 일치 (1,500,000원) - %d개%n", winningResult.get("5개")); - System.out.printf("5개 일치, 보너스 볼 일치 (30,000,000원) - %d개%n", winningResult.get("5개+보너스")); - System.out.printf("6개 일치 (2,000,000,000원) - %d개%n", winningResult.get("6개")); - } - - private static double calculateProfitRate(Map winningResult, int purchaseAmount) { - int totalWinnings = winningResult.get("6개") * PRIZES.get(6) - + winningResult.get("5개+보너스") * 30_000_000 - + winningResult.get("5개") * PRIZES.get(5) - + winningResult.get("4개") * PRIZES.get(4) - + winningResult.get("3개") * PRIZES.get(3); - - return ((double) totalWinnings / purchaseAmount) * 100; + public static void main(String[] args) { + AppConfig appConfig = new AppConfig(); + Application lottoGame = appConfig.application(); + lottoGame.run(); } - -} +} \ No newline at end of file diff --git a/src/main/java/lotto/Lotto.java b/src/main/java/lotto/Lotto.java index ca4643bbe0c..0963fa051ec 100644 --- a/src/main/java/lotto/Lotto.java +++ b/src/main/java/lotto/Lotto.java @@ -34,4 +34,4 @@ private void validate(List numbers) { public List getNumbers() { return numbers; } -} +} \ No newline at end of file diff --git a/src/main/java/lotto/LottoMachine.java b/src/main/java/lotto/LottoMachine.java new file mode 100644 index 00000000000..b3d716de014 --- /dev/null +++ b/src/main/java/lotto/LottoMachine.java @@ -0,0 +1,21 @@ +package lotto; + +import camp.nextstep.edu.missionutils.Randoms; + +import java.util.ArrayList; +import java.util.List; + +public class LottoMachine { + private static final int LOTTO_NUMBER_MIN = 1; + private static final int LOTTO_NUMBER_MAX = 45; + private static final int LOTTO_NUMBERS_COUNT = 6; + + public List generateLottos(int count) { + List lottos = new ArrayList<>(); + for (int i = 0; i < count; i++) { + List numbers = Randoms.pickUniqueNumbersInRange(LOTTO_NUMBER_MIN, LOTTO_NUMBER_MAX, LOTTO_NUMBERS_COUNT); + lottos.add(new Lotto(numbers)); + } + return lottos; + } +} \ No newline at end of file From be2892d775e80cc4fedbea01da02fafcd13e7146 Mon Sep 17 00:00:00 2001 From: jgw1202 Date: Tue, 9 Jul 2024 14:39:43 +0900 Subject: [PATCH 05/14] test commit --- src/main/java/lotto/AppConfig.java | 19 ++++++++ src/main/java/lotto/ConsoleInput.java | 67 ++++++++++++++++++++++++++ src/main/java/lotto/WinningResult.java | 61 +++++++++++++++++++++++ 3 files changed, 147 insertions(+) create mode 100644 src/main/java/lotto/AppConfig.java create mode 100644 src/main/java/lotto/ConsoleInput.java create mode 100644 src/main/java/lotto/WinningResult.java diff --git a/src/main/java/lotto/AppConfig.java b/src/main/java/lotto/AppConfig.java new file mode 100644 index 00000000000..1f24c66cf9b --- /dev/null +++ b/src/main/java/lotto/AppConfig.java @@ -0,0 +1,19 @@ +package lotto; + +public class AppConfig { + public ConsoleInput consoleInput() { + return new ConsoleInput(); + } + + public LottoMachine lottoMachine() { + return new LottoMachine(); + } + + public WinningResult winningResult() { + return new WinningResult(); + } + + public Application application() { + return new Application(consoleInput(), lottoMachine(), winningResult()); + } +} \ No newline at end of file diff --git a/src/main/java/lotto/ConsoleInput.java b/src/main/java/lotto/ConsoleInput.java new file mode 100644 index 00000000000..9795a1438b2 --- /dev/null +++ b/src/main/java/lotto/ConsoleInput.java @@ -0,0 +1,67 @@ +package lotto; + +import camp.nextstep.edu.missionutils.Console; +import java.util.HashSet; +import java.util.Set; + +public class ConsoleInput { + public int getPurchaseAmount() { + System.out.println("구입금액을 입력해 주세요."); + String input = Console.readLine(); + int purchaseAmount; + try { + purchaseAmount = Integer.parseInt(input); + } catch (NumberFormatException e) { + throw new IllegalArgumentException("구입 금액은 숫자여야 합니다."); + } + if (purchaseAmount % 1000 != 0) { + throw new IllegalArgumentException("구입 금액은 1,000원 단위여야 합니다."); + } + return purchaseAmount; + } + + public Set getWinningNumbers() { + System.out.println("당첨 번호를 입력해 주세요."); + String input = Console.readLine(); + String[] split = input.split(","); + if (split.length != 6) { + throw new IllegalArgumentException("당첨 번호는 6개여야 합니다."); + } + + Set winningNumbers = new HashSet<>(); + for (String number : split) { + int num; + try { + num = Integer.parseInt(number); + } catch (NumberFormatException e) { + throw new IllegalArgumentException("당첨 번호는 숫자여야 합니다."); + } + if (num < 1 || num > 45) { + throw new IllegalArgumentException("로또 번호는 1부터 45 사이의 숫자여야 합니다."); + } + winningNumbers.add(num); + } + if (winningNumbers.size() != 6) { + throw new IllegalArgumentException("당첨 번호는 중복되지 않는 6개의 숫자여야 합니다."); + } + return winningNumbers; + } + + public int getBonusNumber(Set winningNumbers) { + System.out.println("보너스 번호를 입력해 주세요."); + String input = Console.readLine(); + int bonusNumber; + try { + bonusNumber = Integer.parseInt(input); + } catch (NumberFormatException e) { + throw new IllegalArgumentException("보너스 번호는 숫자여야 합니다."); + } + if (bonusNumber < 1 || bonusNumber > 45) { + throw new IllegalArgumentException("로또 번호는 1부터 45 사이의 숫자여야 합니다."); + } + if (winningNumbers.contains(bonusNumber)) { + throw new IllegalArgumentException("보너스 번호는 당첨 번호와 중복되지 않아야 합니다."); + } + return bonusNumber; + } +} \ No newline at end of file diff --git a/src/main/java/lotto/WinningResult.java b/src/main/java/lotto/WinningResult.java new file mode 100644 index 00000000000..ff3f9d5bf06 --- /dev/null +++ b/src/main/java/lotto/WinningResult.java @@ -0,0 +1,61 @@ +package lotto; + +import java.util.*; + +public class WinningResult { + private static final Map PRIZES = Map.of( + 6, 2_000_000_000, + 5, 1_500_000, + 4, 50_000, + 3, 5_000 + ); + + private final Map result = new HashMap<>(); + + public WinningResult() { + result.put("3개", 0); + result.put("4개", 0); + result.put("5개", 0); + result.put("5개+보너스", 0); + result.put("6개", 0); + } + + public void checkWinning(List purchasedLottos, Set winningNumbers, int bonusNumber) { + for (Lotto lotto : purchasedLottos) { + Set intersection = new HashSet<>(lotto.getNumbers()); + intersection.retainAll(winningNumbers); + int matchCount = intersection.size(); + + if (matchCount == 6) { + result.put("6개", result.get("6개") + 1); + } else if (matchCount == 5 && lotto.getNumbers().contains(bonusNumber)) { + result.put("5개+보너스", result.get("5개+보너스") + 1); + } else if (matchCount == 5) { + result.put("5개", result.get("5개") + 1); + } else if (matchCount == 4) { + result.put("4개", result.get("4개") + 1); + } else if (matchCount == 3) { + result.put("3개", result.get("3개") + 1); + } + } + } + + public void printWinningResult() { + System.out.println("당첨 통계\n---"); + System.out.printf("3개 일치 (5,000원) - %d개%n", result.get("3개")); + System.out.printf("4개 일치 (50,000원) - %d개%n", result.get("4개")); + System.out.printf("5개 일치 (1,500,000원) - %d개%n", result.get("5개")); + System.out.printf("5개 일치, 보너스 볼 일치 (30,000,000원) - %d개%n", result.get("5개+보너스")); + System.out.printf("6개 일치 (2,000,000,000원) - %d개%n", result.get("6개")); + } + + public double calculateProfitRate(int purchaseAmount) { + int totalWinnings = result.get("6개") * PRIZES.get(6) + + result.get("5개+보너스") * 30_000_000 + + result.get("5개") * PRIZES.get(5) + + result.get("4개") * PRIZES.get(4) + + result.get("3개") * PRIZES.get(3); + + return ((double) totalWinnings / purchaseAmount) * 100; + } +} \ No newline at end of file From 32df54b748aa292326eddf27f1443aae37835cbb Mon Sep 17 00:00:00 2001 From: jgw1202 Date: Tue, 9 Jul 2024 14:41:14 +0900 Subject: [PATCH 06/14] =?UTF-8?q?fix=20:=20cannot=20resolve=20symbol=20?= =?UTF-8?q?=EC=98=A4=EB=A5=98=20=ED=95=B4=EA=B2=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/lotto/Lotto.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/lotto/Lotto.java b/src/main/java/lotto/Lotto.java index 0963fa051ec..3763ccdbda1 100644 --- a/src/main/java/lotto/Lotto.java +++ b/src/main/java/lotto/Lotto.java @@ -2,6 +2,7 @@ import camp.nextstep.edu.missionutils.Randoms; import camp.nextstep.edu.missionutils.Console; +// cannot reslove symbol -> gradle rebuild로 해결 import java.util.HashSet; import java.util.List; From 8bbdd8153e228735a52c39ae4008b6cce4ccee52 Mon Sep 17 00:00:00 2001 From: jgw1202 Date: Wed, 10 Jul 2024 17:02:05 +0900 Subject: [PATCH 07/14] =?UTF-8?q?refactor=20:=20enum=20=EC=82=AC=EC=9A=A9?= =?UTF-8?q?=ED=95=98=EB=8F=84=EB=A1=9D=20=EC=BD=94=EB=93=9C=20=EB=A6=AC?= =?UTF-8?q?=ED=8C=A9=ED=86=A0=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/lotto/RankEnum.java | 45 +++++++++++++++++++++++ src/main/java/lotto/WinningResult.java | 50 ++++++++------------------ 2 files changed, 60 insertions(+), 35 deletions(-) create mode 100644 src/main/java/lotto/RankEnum.java diff --git a/src/main/java/lotto/RankEnum.java b/src/main/java/lotto/RankEnum.java new file mode 100644 index 00000000000..f84e4139b2a --- /dev/null +++ b/src/main/java/lotto/RankEnum.java @@ -0,0 +1,45 @@ +package lotto; + +public enum RankEnum { + FIRST(6, 2_000_000_000), + SECOND(5, 30_000_000), + THIRD(5, 1_500_000), + FOURTH(4, 50_000), + FIFTH(3, 5_000), + MISS(0, 0); + + private final int matchCount; + private final int prize; + + RankEnum(int matchCount, int prize) { + this.matchCount = matchCount; + this.prize = prize; + } + + public int getMatchCount() { + return matchCount; + } + + public int getPrize() { + return prize; + } + + public static RankEnum valueOf(int matchCount, boolean matchBonus) { + if (matchCount == 6) { + return FIRST; + } + if (matchCount == 5 && matchBonus) { + return SECOND; + } + if (matchCount == 5) { + return THIRD; + } + if (matchCount == 4) { + return FOURTH; + } + if (matchCount == 3) { + return FIFTH; + } + return MISS; + } +} diff --git a/src/main/java/lotto/WinningResult.java b/src/main/java/lotto/WinningResult.java index 64c1ef7ea7e..497025715a4 100644 --- a/src/main/java/lotto/WinningResult.java +++ b/src/main/java/lotto/WinningResult.java @@ -3,21 +3,12 @@ import java.util.*; public class WinningResult { - private static final Map PRIZES = Map.of( - 6, 2_000_000_000, - 5, 1_500_000, - 4, 50_000, - 3, 5_000 - ); - - private final Map result = new HashMap<>(); + private final Map result = new EnumMap<>(RankEnum.class); public WinningResult() { - result.put("3개", 0); - result.put("4개", 0); - result.put("5개", 0); - result.put("5개+보너스", 0); - result.put("6개", 0); + for (RankEnum rank : RankEnum.values()) { + result.put(rank, 0); + } } public void checkWinning(List purchasedLottos, Set winningNumbers, int bonusNumber) { @@ -25,37 +16,26 @@ public void checkWinning(List purchasedLottos, Set winningNumber Set intersection = new HashSet<>(lotto.getNumbers()); intersection.retainAll(winningNumbers); int matchCount = intersection.size(); + boolean matchBonus = lotto.getNumbers().contains(bonusNumber); - if (matchCount == 6) { - result.put("6개", result.get("6개") + 1); - } else if (matchCount == 5 && lotto.getNumbers().contains(bonusNumber)) { - result.put("5개+보너스", result.get("5개+보너스") + 1); - } else if (matchCount == 5) { - result.put("5개", result.get("5개") + 1); - } else if (matchCount == 4) { - result.put("4개", result.get("4개") + 1); - } else if (matchCount == 3) { - result.put("3개", result.get("3개") + 1); - } + RankEnum rank = RankEnum.valueOf(matchCount, matchBonus); + result.put(rank, result.get(rank) + 1); } } public void printWinningResult() { System.out.println("당첨 통계\n---"); - System.out.printf("3개 일치 (5,000원) - %d개%n", result.get("3개")); - System.out.printf("4개 일치 (50,000원) - %d개%n", result.get("4개")); - System.out.printf("5개 일치 (1,500,000원) - %d개%n", result.get("5개")); - System.out.printf("5개 일치, 보너스 볼 일치 (30,000,000원) - %d개%n", result.get("5개+보너스")); - System.out.printf("6개 일치 (2,000,000,000원) - %d개%n", result.get("6개")); + System.out.printf("3개 일치 (5,000원) - %d개%n", result.get(RankEnum.FIFTH)); + System.out.printf("4개 일치 (50,000원) - %d개%n", result.get(RankEnum.FOURTH)); + System.out.printf("5개 일치 (1,500,000원) - %d개%n", result.get(RankEnum.THIRD)); + System.out.printf("5개 일치, 보너스 볼 일치 (30,000,000원) - %d개%n", result.get(RankEnum.SECOND)); + System.out.printf("6개 일치 (2,000,000,000원) - %d개%n", result.get(RankEnum.FIRST)); } public double calculateProfitRate(int purchaseAmount) { - int totalWinnings = result.get("6개") * PRIZES.get(6) - + result.get("5개+보너스") * 30_000_000 - + result.get("5개") * PRIZES.get(5) - + result.get("4개") * PRIZES.get(4) - + result.get("3개") * PRIZES.get(3); - + int totalWinnings = result.entrySet().stream() + .mapToInt(entry -> entry.getKey().getPrize() * entry.getValue()) + .sum(); return ((double) totalWinnings / purchaseAmount) * 100; } } From 7816e9f264a4599e113f1d2ca80a84728f34649f Mon Sep 17 00:00:00 2001 From: jgw1202 Date: Fri, 12 Jul 2024 15:22:29 +0900 Subject: [PATCH 08/14] =?UTF-8?q?refactor=20:=20appConfig=20=ED=81=B4?= =?UTF-8?q?=EB=9E=98=EC=8A=A4=EB=AA=85=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/lotto/Application.java | 4 ++-- src/main/java/lotto/{AppConfig.java => InstanceManager.java} | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) rename src/main/java/lotto/{AppConfig.java => InstanceManager.java} (92%) diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java index f77a5dcdd04..e1f2f5f19bd 100644 --- a/src/main/java/lotto/Application.java +++ b/src/main/java/lotto/Application.java @@ -43,8 +43,8 @@ private void printPurchasedLottos(List lottos) { } public static void main(String[] args) { - AppConfig appConfig = new AppConfig(); - Application lottoGame = appConfig.application(); + InstanceManager instanceManager = new InstanceManager(); + Application lottoGame = instanceManager.application(); lottoGame.run(); } } diff --git a/src/main/java/lotto/AppConfig.java b/src/main/java/lotto/InstanceManager.java similarity index 92% rename from src/main/java/lotto/AppConfig.java rename to src/main/java/lotto/InstanceManager.java index 1f24c66cf9b..83ac7a518b3 100644 --- a/src/main/java/lotto/AppConfig.java +++ b/src/main/java/lotto/InstanceManager.java @@ -1,6 +1,6 @@ package lotto; -public class AppConfig { +public class InstanceManager { public ConsoleInput consoleInput() { return new ConsoleInput(); } From 4ed9c12d50451a5e9b0a4c74f8a341a0e8ed4a5a Mon Sep 17 00:00:00 2001 From: jgw1202 Date: Fri, 12 Jul 2024 15:23:17 +0900 Subject: [PATCH 09/14] =?UTF-8?q?fix=20:=20=EC=99=80=EC=9D=BC=EB=93=9C?= =?UTF-8?q?=EC=B9=B4=EB=93=9C=20import=20=EB=AF=B8=EC=82=AC=EC=9A=A9?= =?UTF-8?q?=EC=9C=BC=EB=A1=9C=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/lotto/WinningResult.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/main/java/lotto/WinningResult.java b/src/main/java/lotto/WinningResult.java index 497025715a4..7d9d332bd0e 100644 --- a/src/main/java/lotto/WinningResult.java +++ b/src/main/java/lotto/WinningResult.java @@ -1,6 +1,11 @@ package lotto; -import java.util.*; + +import java.util.EnumMap; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.HashSet; public class WinningResult { private final Map result = new EnumMap<>(RankEnum.class); From 9fb463d8a31b4eed8132b8ba6161a61467a2fbc4 Mon Sep 17 00:00:00 2001 From: jgw1202 Date: Fri, 12 Jul 2024 15:27:29 +0900 Subject: [PATCH 10/14] =?UTF-8?q?refactor=20:=20=EB=A9=94=EC=86=8C?= =?UTF-8?q?=EB=93=9C=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/lotto/Application.java | 41 ++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 11 deletions(-) diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java index e1f2f5f19bd..6a28915bb63 100644 --- a/src/main/java/lotto/Application.java +++ b/src/main/java/lotto/Application.java @@ -16,25 +16,28 @@ public Application(ConsoleInput consoleInput, LottoMachine lottoMachine, Winning public void run() { try { - int purchaseAmount = consoleInput.getPurchaseAmount(); - int numberOfLottos = purchaseAmount / 1000; - - List purchasedLottos = lottoMachine.generateLottos(numberOfLottos); + int purchaseAmount = getPurchaseAmount(); + List purchasedLottos = generateLottos(purchaseAmount); printPurchasedLottos(purchasedLottos); - Set winningNumbers = consoleInput.getWinningNumbers(); - int bonusNumber = consoleInput.getBonusNumber(winningNumbers); - - winningResult.checkWinning(purchasedLottos, winningNumbers, bonusNumber); - winningResult.printWinningResult(); + Set winningNumbers = getWinningNumbers(); + int bonusNumber = getBonusNumber(winningNumbers); - double profitRate = winningResult.calculateProfitRate(purchaseAmount); - System.out.printf("총 수익률은 %.1f%%입니다.%n", profitRate); + checkAndPrintWinningResult(purchasedLottos, winningNumbers, bonusNumber, purchaseAmount); } catch (IllegalArgumentException e) { System.out.println("[ERROR] " + e.getMessage()); } } + private int getPurchaseAmount() { + return consoleInput.getPurchaseAmount(); + } + + private List generateLottos(int purchaseAmount) { + int numberOfLottos = purchaseAmount / 1000; + return lottoMachine.generateLottos(numberOfLottos); + } + private void printPurchasedLottos(List lottos) { System.out.printf("%d개를 구매했습니다.%n", lottos.size()); for (Lotto lotto : lottos) { @@ -42,6 +45,22 @@ private void printPurchasedLottos(List lottos) { } } + private Set getWinningNumbers() { + return consoleInput.getWinningNumbers(); + } + + private int getBonusNumber(Set winningNumbers) { + return consoleInput.getBonusNumber(winningNumbers); + } + + private void checkAndPrintWinningResult(List purchasedLottos, Set winningNumbers, int bonusNumber, int purchaseAmount) { + winningResult.checkWinning(purchasedLottos, winningNumbers, bonusNumber); + winningResult.printWinningResult(); + + double profitRate = winningResult.calculateProfitRate(purchaseAmount); + System.out.printf("총 수익률은 %.1f%%입니다.%n", profitRate); + } + public static void main(String[] args) { InstanceManager instanceManager = new InstanceManager(); Application lottoGame = instanceManager.application(); From facb0f589fe75eaa904b0a9d68f7e2b4ec439306 Mon Sep 17 00:00:00 2001 From: jgw1202 Date: Fri, 12 Jul 2024 15:31:32 +0900 Subject: [PATCH 11/14] =?UTF-8?q?refactor=20:=20=ED=81=B4=EB=9E=98?= =?UTF-8?q?=EC=8A=A4=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/lotto/Application.java | 64 +----------------------- src/main/java/lotto/InstanceManager.java | 22 ++++---- src/main/java/lotto/LottoGame.java | 63 +++++++++++++++++++++++ 3 files changed, 75 insertions(+), 74 deletions(-) create mode 100644 src/main/java/lotto/LottoGame.java diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java index 6a28915bb63..5dea07127a9 100644 --- a/src/main/java/lotto/Application.java +++ b/src/main/java/lotto/Application.java @@ -1,69 +1,9 @@ package lotto; -import java.util.List; -import java.util.Set; - public class Application { - private final ConsoleInput consoleInput; - private final LottoMachine lottoMachine; - private final WinningResult winningResult; - - public Application(ConsoleInput consoleInput, LottoMachine lottoMachine, WinningResult winningResult) { - this.consoleInput = consoleInput; - this.lottoMachine = lottoMachine; - this.winningResult = winningResult; - } - - public void run() { - try { - int purchaseAmount = getPurchaseAmount(); - List purchasedLottos = generateLottos(purchaseAmount); - printPurchasedLottos(purchasedLottos); - - Set winningNumbers = getWinningNumbers(); - int bonusNumber = getBonusNumber(winningNumbers); - - checkAndPrintWinningResult(purchasedLottos, winningNumbers, bonusNumber, purchaseAmount); - } catch (IllegalArgumentException e) { - System.out.println("[ERROR] " + e.getMessage()); - } - } - - private int getPurchaseAmount() { - return consoleInput.getPurchaseAmount(); - } - - private List generateLottos(int purchaseAmount) { - int numberOfLottos = purchaseAmount / 1000; - return lottoMachine.generateLottos(numberOfLottos); - } - - private void printPurchasedLottos(List lottos) { - System.out.printf("%d개를 구매했습니다.%n", lottos.size()); - for (Lotto lotto : lottos) { - System.out.println(lotto.getNumbers()); - } - } - - private Set getWinningNumbers() { - return consoleInput.getWinningNumbers(); - } - - private int getBonusNumber(Set winningNumbers) { - return consoleInput.getBonusNumber(winningNumbers); - } - - private void checkAndPrintWinningResult(List purchasedLottos, Set winningNumbers, int bonusNumber, int purchaseAmount) { - winningResult.checkWinning(purchasedLottos, winningNumbers, bonusNumber); - winningResult.printWinningResult(); - - double profitRate = winningResult.calculateProfitRate(purchaseAmount); - System.out.printf("총 수익률은 %.1f%%입니다.%n", profitRate); - } - public static void main(String[] args) { InstanceManager instanceManager = new InstanceManager(); - Application lottoGame = instanceManager.application(); - lottoGame.run(); + LottoGame lottoGame = instanceManager.lottoGame(); + lottoGame.start(); } } diff --git a/src/main/java/lotto/InstanceManager.java b/src/main/java/lotto/InstanceManager.java index 83ac7a518b3..5ffb69e7b7e 100644 --- a/src/main/java/lotto/InstanceManager.java +++ b/src/main/java/lotto/InstanceManager.java @@ -1,19 +1,17 @@ package lotto; public class InstanceManager { - public ConsoleInput consoleInput() { - return new ConsoleInput(); - } - - public LottoMachine lottoMachine() { - return new LottoMachine(); - } + private final ConsoleInput consoleInput; + private final LottoMachine lottoMachine; + private final WinningResult winningResult; - public WinningResult winningResult() { - return new WinningResult(); + public InstanceManager() { + this.consoleInput = new ConsoleInput(); + this.lottoMachine = new LottoMachine(); + this.winningResult = new WinningResult(); } - public Application application() { - return new Application(consoleInput(), lottoMachine(), winningResult()); + public LottoGame lottoGame() { + return new LottoGame(consoleInput, lottoMachine, winningResult); } -} \ No newline at end of file +} diff --git a/src/main/java/lotto/LottoGame.java b/src/main/java/lotto/LottoGame.java new file mode 100644 index 00000000000..b06a936a632 --- /dev/null +++ b/src/main/java/lotto/LottoGame.java @@ -0,0 +1,63 @@ +package lotto; + +import java.util.List; +import java.util.Set; + +public class LottoGame { + private final ConsoleInput consoleInput; + private final LottoMachine lottoMachine; + private final WinningResult winningResult; + + public LottoGame(ConsoleInput consoleInput, LottoMachine lottoMachine, WinningResult winningResult) { + this.consoleInput = consoleInput; + this.lottoMachine = lottoMachine; + this.winningResult = winningResult; + } + + public void start() { + try { + int purchaseAmount = getPurchaseAmount(); + List purchasedLottos = generateLottos(purchaseAmount); + printPurchasedLottos(purchasedLottos); + + Set winningNumbers = getWinningNumbers(); + int bonusNumber = getBonusNumber(winningNumbers); + + checkAndPrintWinningResult(purchasedLottos, winningNumbers, bonusNumber, purchaseAmount); + } catch (IllegalArgumentException e) { + System.out.println("[ERROR] " + e.getMessage()); + } + } + + private int getPurchaseAmount() { + return consoleInput.getPurchaseAmount(); + } + + private List generateLottos(int purchaseAmount) { + int numberOfLottos = purchaseAmount / 1000; + return lottoMachine.generateLottos(numberOfLottos); + } + + private void printPurchasedLottos(List lottos) { + System.out.printf("%d개를 구매했습니다.%n", lottos.size()); + for (Lotto lotto : lottos) { + System.out.println(lotto.getNumbers()); + } + } + + private Set getWinningNumbers() { + return consoleInput.getWinningNumbers(); + } + + private int getBonusNumber(Set winningNumbers) { + return consoleInput.getBonusNumber(winningNumbers); + } + + private void checkAndPrintWinningResult(List purchasedLottos, Set winningNumbers, int bonusNumber, int purchaseAmount) { + winningResult.checkWinning(purchasedLottos, winningNumbers, bonusNumber); + winningResult.printWinningResult(); + + double profitRate = winningResult.calculateProfitRate(purchaseAmount); + System.out.printf("총 수익률은 %.1f%%입니다.%n", profitRate); + } +} From 32db4040dac161745b2dab3639e34dd7717bad86 Mon Sep 17 00:00:00 2001 From: jgw1202 Date: Fri, 12 Jul 2024 15:35:20 +0900 Subject: [PATCH 12/14] =?UTF-8?q?refactor=20:=20=EB=A1=9C=EB=98=90=20?= =?UTF-8?q?=EA=B0=80=EA=B2=A9=20=EC=83=81=EC=88=98=ED=99=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/lotto/LottoGame.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/lotto/LottoGame.java b/src/main/java/lotto/LottoGame.java index b06a936a632..7f88b57287b 100644 --- a/src/main/java/lotto/LottoGame.java +++ b/src/main/java/lotto/LottoGame.java @@ -4,6 +4,8 @@ import java.util.Set; public class LottoGame { + + private static final int LOTTO_PRICE = 1000; private final ConsoleInput consoleInput; private final LottoMachine lottoMachine; private final WinningResult winningResult; @@ -34,7 +36,7 @@ private int getPurchaseAmount() { } private List generateLottos(int purchaseAmount) { - int numberOfLottos = purchaseAmount / 1000; + int numberOfLottos = purchaseAmount / LOTTO_PRICE; return lottoMachine.generateLottos(numberOfLottos); } From b2c68f9bfb46f9914fcaef448f1a3c513699d0a3 Mon Sep 17 00:00:00 2001 From: jgw1202 Date: Tue, 16 Jul 2024 18:16:48 +0900 Subject: [PATCH 13/14] =?UTF-8?q?refactor=20:=20MVC=20=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/lotto/Application.java | 3 +++ src/main/java/lotto/{ => controller}/InstanceManager.java | 7 ++++++- src/main/java/lotto/{ => model}/Lotto.java | 2 +- src/main/java/lotto/{ => model}/LottoMachine.java | 3 ++- src/main/java/lotto/{ => model}/RankEnum.java | 2 +- src/main/java/lotto/{ => model}/WinningResult.java | 2 +- src/main/java/lotto/{ => view}/ConsoleInput.java | 2 +- src/main/java/lotto/{ => view}/LottoGame.java | 7 ++++++- src/test/java/lotto/LottoTest.java | 1 + 9 files changed, 22 insertions(+), 7 deletions(-) rename src/main/java/lotto/{ => controller}/InstanceManager.java (75%) rename src/main/java/lotto/{ => model}/Lotto.java (98%) rename src/main/java/lotto/{ => model}/LottoMachine.java (93%) rename src/main/java/lotto/{ => model}/RankEnum.java (97%) rename src/main/java/lotto/{ => model}/WinningResult.java (98%) rename src/main/java/lotto/{ => view}/ConsoleInput.java (99%) rename src/main/java/lotto/{ => view}/LottoGame.java (93%) diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java index 5dea07127a9..6f0191f2865 100644 --- a/src/main/java/lotto/Application.java +++ b/src/main/java/lotto/Application.java @@ -1,5 +1,8 @@ package lotto; +import lotto.controller.InstanceManager; +import lotto.view.LottoGame; + public class Application { public static void main(String[] args) { InstanceManager instanceManager = new InstanceManager(); diff --git a/src/main/java/lotto/InstanceManager.java b/src/main/java/lotto/controller/InstanceManager.java similarity index 75% rename from src/main/java/lotto/InstanceManager.java rename to src/main/java/lotto/controller/InstanceManager.java index 5ffb69e7b7e..84edcf9541c 100644 --- a/src/main/java/lotto/InstanceManager.java +++ b/src/main/java/lotto/controller/InstanceManager.java @@ -1,4 +1,9 @@ -package lotto; +package lotto.controller; + +import lotto.model.LottoMachine; +import lotto.model.WinningResult; +import lotto.view.ConsoleInput; +import lotto.view.LottoGame; public class InstanceManager { private final ConsoleInput consoleInput; diff --git a/src/main/java/lotto/Lotto.java b/src/main/java/lotto/model/Lotto.java similarity index 98% rename from src/main/java/lotto/Lotto.java rename to src/main/java/lotto/model/Lotto.java index 3763ccdbda1..6d781f60cdc 100644 --- a/src/main/java/lotto/Lotto.java +++ b/src/main/java/lotto/model/Lotto.java @@ -1,4 +1,4 @@ -package lotto; +package lotto.model; import camp.nextstep.edu.missionutils.Randoms; import camp.nextstep.edu.missionutils.Console; diff --git a/src/main/java/lotto/LottoMachine.java b/src/main/java/lotto/model/LottoMachine.java similarity index 93% rename from src/main/java/lotto/LottoMachine.java rename to src/main/java/lotto/model/LottoMachine.java index 5dd6462c163..b604bb319da 100644 --- a/src/main/java/lotto/LottoMachine.java +++ b/src/main/java/lotto/model/LottoMachine.java @@ -1,6 +1,7 @@ -package lotto; +package lotto.model; import camp.nextstep.edu.missionutils.Randoms; +import lotto.model.Lotto; import java.util.ArrayList; import java.util.List; diff --git a/src/main/java/lotto/RankEnum.java b/src/main/java/lotto/model/RankEnum.java similarity index 97% rename from src/main/java/lotto/RankEnum.java rename to src/main/java/lotto/model/RankEnum.java index f84e4139b2a..be3cde1cd21 100644 --- a/src/main/java/lotto/RankEnum.java +++ b/src/main/java/lotto/model/RankEnum.java @@ -1,4 +1,4 @@ -package lotto; +package lotto.model; public enum RankEnum { FIRST(6, 2_000_000_000), diff --git a/src/main/java/lotto/WinningResult.java b/src/main/java/lotto/model/WinningResult.java similarity index 98% rename from src/main/java/lotto/WinningResult.java rename to src/main/java/lotto/model/WinningResult.java index 7d9d332bd0e..951cce1fa90 100644 --- a/src/main/java/lotto/WinningResult.java +++ b/src/main/java/lotto/model/WinningResult.java @@ -1,4 +1,4 @@ -package lotto; +package lotto.model; import java.util.EnumMap; diff --git a/src/main/java/lotto/ConsoleInput.java b/src/main/java/lotto/view/ConsoleInput.java similarity index 99% rename from src/main/java/lotto/ConsoleInput.java rename to src/main/java/lotto/view/ConsoleInput.java index e35b2930d21..9ed19aaa980 100644 --- a/src/main/java/lotto/ConsoleInput.java +++ b/src/main/java/lotto/view/ConsoleInput.java @@ -1,4 +1,4 @@ -package lotto; +package lotto.view; import camp.nextstep.edu.missionutils.Console; import java.util.HashSet; diff --git a/src/main/java/lotto/LottoGame.java b/src/main/java/lotto/view/LottoGame.java similarity index 93% rename from src/main/java/lotto/LottoGame.java rename to src/main/java/lotto/view/LottoGame.java index 7f88b57287b..3d99ba88d2f 100644 --- a/src/main/java/lotto/LottoGame.java +++ b/src/main/java/lotto/view/LottoGame.java @@ -1,4 +1,9 @@ -package lotto; +package lotto.view; + +import lotto.model.Lotto; +import lotto.model.LottoMachine; +import lotto.model.WinningResult; +import lotto.view.ConsoleInput; import java.util.List; import java.util.Set; diff --git a/src/test/java/lotto/LottoTest.java b/src/test/java/lotto/LottoTest.java index 9f5dfe7eb83..33472c95fd0 100644 --- a/src/test/java/lotto/LottoTest.java +++ b/src/test/java/lotto/LottoTest.java @@ -1,5 +1,6 @@ package lotto; +import lotto.model.Lotto; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; From 6b9eb5ebfdbd8055bc96ef53a983fed4355da04f Mon Sep 17 00:00:00 2001 From: jgw1202 Date: Thu, 18 Jul 2024 19:39:23 +0900 Subject: [PATCH 14/14] =?UTF-8?q?refactor=20:=20MVC=20=EC=A0=81=EC=9A=A9?= =?UTF-8?q?=20=EB=A6=AC=ED=8C=A9=ED=86=A0=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lotto/controller/InstanceManager.java | 12 +++++----- ...ttoMachine.java => LottoMachineModel.java} | 9 ++++---- .../model/{Lotto.java => LottoModel.java} | 6 ++--- ...ingResult.java => WinningResultModel.java} | 8 +++---- src/main/java/lotto/view/LottoGame.java | 23 +++++++++---------- src/test/java/lotto/LottoTest.java | 6 ++--- 6 files changed, 30 insertions(+), 34 deletions(-) rename src/main/java/lotto/model/{LottoMachine.java => LottoMachineModel.java} (71%) rename src/main/java/lotto/model/{Lotto.java => LottoModel.java} (86%) rename src/main/java/lotto/model/{WinningResult.java => WinningResultModel.java} (87%) diff --git a/src/main/java/lotto/controller/InstanceManager.java b/src/main/java/lotto/controller/InstanceManager.java index 84edcf9541c..7988ad6d8ab 100644 --- a/src/main/java/lotto/controller/InstanceManager.java +++ b/src/main/java/lotto/controller/InstanceManager.java @@ -1,19 +1,19 @@ package lotto.controller; -import lotto.model.LottoMachine; -import lotto.model.WinningResult; +import lotto.model.LottoMachineModel; +import lotto.model.WinningResultModel; import lotto.view.ConsoleInput; import lotto.view.LottoGame; public class InstanceManager { private final ConsoleInput consoleInput; - private final LottoMachine lottoMachine; - private final WinningResult winningResult; + private final LottoMachineModel lottoMachine; + private final WinningResultModel winningResult; public InstanceManager() { this.consoleInput = new ConsoleInput(); - this.lottoMachine = new LottoMachine(); - this.winningResult = new WinningResult(); + this.lottoMachine = new LottoMachineModel(); + this.winningResult = new WinningResultModel(); } public LottoGame lottoGame() { diff --git a/src/main/java/lotto/model/LottoMachine.java b/src/main/java/lotto/model/LottoMachineModel.java similarity index 71% rename from src/main/java/lotto/model/LottoMachine.java rename to src/main/java/lotto/model/LottoMachineModel.java index b604bb319da..d470236e31c 100644 --- a/src/main/java/lotto/model/LottoMachine.java +++ b/src/main/java/lotto/model/LottoMachineModel.java @@ -1,21 +1,20 @@ package lotto.model; import camp.nextstep.edu.missionutils.Randoms; -import lotto.model.Lotto; import java.util.ArrayList; import java.util.List; -public class LottoMachine { +public class LottoMachineModel { private static final int LOTTO_NUMBER_MIN = 1; private static final int LOTTO_NUMBER_MAX = 45; private static final int LOTTO_NUMBERS_COUNT = 6; - public List generateLottos(int count) { - List lottos = new ArrayList<>(); + public List generateLottos(int count) { + List lottos = new ArrayList<>(); for (int i = 0; i < count; i++) { List numbers = Randoms.pickUniqueNumbersInRange(LOTTO_NUMBER_MIN, LOTTO_NUMBER_MAX, LOTTO_NUMBERS_COUNT); - lottos.add(new Lotto(numbers)); + lottos.add(new LottoModel(numbers)); } return lottos; } diff --git a/src/main/java/lotto/model/Lotto.java b/src/main/java/lotto/model/LottoModel.java similarity index 86% rename from src/main/java/lotto/model/Lotto.java rename to src/main/java/lotto/model/LottoModel.java index 6d781f60cdc..ece39ebdded 100644 --- a/src/main/java/lotto/model/Lotto.java +++ b/src/main/java/lotto/model/LottoModel.java @@ -1,17 +1,15 @@ package lotto.model; -import camp.nextstep.edu.missionutils.Randoms; -import camp.nextstep.edu.missionutils.Console; // cannot reslove symbol -> gradle rebuild로 해결 import java.util.HashSet; import java.util.List; import java.util.Set; -public class Lotto { +public class LottoModel { private final List numbers; - public Lotto(List numbers) { + public LottoModel(List numbers) { validate(numbers); this.numbers = numbers; } diff --git a/src/main/java/lotto/model/WinningResult.java b/src/main/java/lotto/model/WinningResultModel.java similarity index 87% rename from src/main/java/lotto/model/WinningResult.java rename to src/main/java/lotto/model/WinningResultModel.java index 951cce1fa90..b74a8380373 100644 --- a/src/main/java/lotto/model/WinningResult.java +++ b/src/main/java/lotto/model/WinningResultModel.java @@ -7,17 +7,17 @@ import java.util.Set; import java.util.HashSet; -public class WinningResult { +public class WinningResultModel { private final Map result = new EnumMap<>(RankEnum.class); - public WinningResult() { + public WinningResultModel() { for (RankEnum rank : RankEnum.values()) { result.put(rank, 0); } } - public void checkWinning(List purchasedLottos, Set winningNumbers, int bonusNumber) { - for (Lotto lotto : purchasedLottos) { + public void checkWinning(List purchasedLottos, Set winningNumbers, int bonusNumber) { + for (LottoModel lotto : purchasedLottos) { Set intersection = new HashSet<>(lotto.getNumbers()); intersection.retainAll(winningNumbers); int matchCount = intersection.size(); diff --git a/src/main/java/lotto/view/LottoGame.java b/src/main/java/lotto/view/LottoGame.java index 3d99ba88d2f..5917de56e8d 100644 --- a/src/main/java/lotto/view/LottoGame.java +++ b/src/main/java/lotto/view/LottoGame.java @@ -1,9 +1,8 @@ package lotto.view; -import lotto.model.Lotto; -import lotto.model.LottoMachine; -import lotto.model.WinningResult; -import lotto.view.ConsoleInput; +import lotto.model.LottoModel; +import lotto.model.LottoMachineModel; +import lotto.model.WinningResultModel; import java.util.List; import java.util.Set; @@ -12,10 +11,10 @@ public class LottoGame { private static final int LOTTO_PRICE = 1000; private final ConsoleInput consoleInput; - private final LottoMachine lottoMachine; - private final WinningResult winningResult; + private final LottoMachineModel lottoMachine; + private final WinningResultModel winningResult; - public LottoGame(ConsoleInput consoleInput, LottoMachine lottoMachine, WinningResult winningResult) { + public LottoGame(ConsoleInput consoleInput, LottoMachineModel lottoMachine, WinningResultModel winningResult) { this.consoleInput = consoleInput; this.lottoMachine = lottoMachine; this.winningResult = winningResult; @@ -24,7 +23,7 @@ public LottoGame(ConsoleInput consoleInput, LottoMachine lottoMachine, WinningRe public void start() { try { int purchaseAmount = getPurchaseAmount(); - List purchasedLottos = generateLottos(purchaseAmount); + List purchasedLottos = generateLottos(purchaseAmount); printPurchasedLottos(purchasedLottos); Set winningNumbers = getWinningNumbers(); @@ -40,14 +39,14 @@ private int getPurchaseAmount() { return consoleInput.getPurchaseAmount(); } - private List generateLottos(int purchaseAmount) { + private List generateLottos(int purchaseAmount) { int numberOfLottos = purchaseAmount / LOTTO_PRICE; return lottoMachine.generateLottos(numberOfLottos); } - private void printPurchasedLottos(List lottos) { + private void printPurchasedLottos(List lottos) { System.out.printf("%d개를 구매했습니다.%n", lottos.size()); - for (Lotto lotto : lottos) { + for (LottoModel lotto : lottos) { System.out.println(lotto.getNumbers()); } } @@ -60,7 +59,7 @@ private int getBonusNumber(Set winningNumbers) { return consoleInput.getBonusNumber(winningNumbers); } - private void checkAndPrintWinningResult(List purchasedLottos, Set winningNumbers, int bonusNumber, int purchaseAmount) { + private void checkAndPrintWinningResult(List purchasedLottos, Set winningNumbers, int bonusNumber, int purchaseAmount) { winningResult.checkWinning(purchasedLottos, winningNumbers, bonusNumber); winningResult.printWinningResult(); diff --git a/src/test/java/lotto/LottoTest.java b/src/test/java/lotto/LottoTest.java index 33472c95fd0..d1ca7d29f78 100644 --- a/src/test/java/lotto/LottoTest.java +++ b/src/test/java/lotto/LottoTest.java @@ -1,6 +1,6 @@ package lotto; -import lotto.model.Lotto; +import lotto.model.LottoModel; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -12,7 +12,7 @@ class LottoTest { @DisplayName("로또 번호의 개수가 6개가 넘어가면 예외가 발생한다.") @Test void createLottoByOverSize() { - assertThatThrownBy(() -> new Lotto(List.of(1, 2, 3, 4, 5, 6, 7))) + assertThatThrownBy(() -> new LottoModel(List.of(1, 2, 3, 4, 5, 6, 7))) .isInstanceOf(IllegalArgumentException.class); } @@ -20,7 +20,7 @@ void createLottoByOverSize() { @Test void createLottoByDuplicatedNumber() { // TODO: 이 테스트가 통과할 수 있게 구현 코드 작성 - assertThatThrownBy(() -> new Lotto(List.of(1, 2, 3, 4, 5, 5))) + assertThatThrownBy(() -> new LottoModel(List.of(1, 2, 3, 4, 5, 5))) .isInstanceOf(IllegalArgumentException.class); }