From 45d2af7714c92fc86d8de3bef20ca4d65b0bc213 Mon Sep 17 00:00:00 2001 From: EunSeop Date: Mon, 16 Sep 2024 16:03:39 +0900 Subject: [PATCH 1/6] =?UTF-8?q?[Feat]=20=EB=A1=9C=EB=98=90=20=EA=B5=AC?= =?UTF-8?q?=EB=A7=A4=EA=B8=B0=EB=8A=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/lotto/BuyingLotto.java | 39 ++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 src/main/java/lotto/BuyingLotto.java diff --git a/src/main/java/lotto/BuyingLotto.java b/src/main/java/lotto/BuyingLotto.java new file mode 100644 index 00000000000..c306217310f --- /dev/null +++ b/src/main/java/lotto/BuyingLotto.java @@ -0,0 +1,39 @@ +package lotto; + +import camp.nextstep.edu.missionutils.Console; + +public class BuyingLotto { + static int lottoCount; + + public BuyingLotto() { + buyLotto(); + } + + private void buyLotto() { + boolean validInput = false; + + while (!validInput) { + try { + System.out.println("구입금액을 입력해 주세요."); + String input = Console.readLine(); + int money = Integer.parseInt(input); + validateInput(money); + lottoCount = money / 1000; + System.out.println(lottoCount + "개를 구매했습니다."); + validInput = true; + } catch (IllegalArgumentException e) { + System.out.println("[ERROR] 금액은 1000원 단위여야 합니다. 다시 입력해 주세요."); + } + } + } + + public int getLottoCount() { + return lottoCount; + } + + private void validateInput(int money) { + if (money % 1000 == 0) return; + throw new IllegalArgumentException(); + } + +} From 2d3032bd161d83911c8af89f9caf37e689f327d4 Mon Sep 17 00:00:00 2001 From: EunSeop Date: Mon, 16 Sep 2024 16:24:37 +0900 Subject: [PATCH 2/6] =?UTF-8?q?[Feat]=20=EB=A1=9C=EB=98=90=20=EB=A7=8C?= =?UTF-8?q?=EB=93=A4=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/lotto/LottoCreator.java | 26 +++++++++++++++++++ src/main/java/lotto/LottoNumberGenerator.java | 24 +++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 src/main/java/lotto/LottoCreator.java create mode 100644 src/main/java/lotto/LottoNumberGenerator.java diff --git a/src/main/java/lotto/LottoCreator.java b/src/main/java/lotto/LottoCreator.java new file mode 100644 index 00000000000..1ebe5e4e348 --- /dev/null +++ b/src/main/java/lotto/LottoCreator.java @@ -0,0 +1,26 @@ +package lotto; + +import camp.nextstep.edu.missionutils.Randoms; + +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +public class LottoCreator { + private List lottoList; + + public LottoCreator(int count) { + createLotto(count); + } + + private void createLotto(int count) { + lottoList = IntStream.range(0, count) + .mapToObj(i -> new Lotto(LottoNumberGenerator.generateLottoNumber())) + .collect(Collectors.toList()); + + } + + public List getLottoList() { + return lottoList; + } +} diff --git a/src/main/java/lotto/LottoNumberGenerator.java b/src/main/java/lotto/LottoNumberGenerator.java new file mode 100644 index 00000000000..c2c666c5ddc --- /dev/null +++ b/src/main/java/lotto/LottoNumberGenerator.java @@ -0,0 +1,24 @@ +package lotto; + +import camp.nextstep.edu.missionutils.Randoms; + +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.Random; + +public class LottoNumberGenerator { + private static final int MAX_NUM = 45; + private static final int MIN_NUM = 1; + private static final int COUNT = 6; + + private LottoNumberGenerator() { + throw new UnsupportedOperationException("Utility class"); + } + + public static List generateLottoNumber() { + List numbers = Randoms.pickUniqueNumbersInRange(MIN_NUM, MAX_NUM, COUNT); + Collections.sort(numbers); + return numbers; + } +} From 622b94198d442ee540f3d3acd800ca10e155a6a6 Mon Sep 17 00:00:00 2001 From: EunSeop Date: Mon, 16 Sep 2024 16:49:31 +0900 Subject: [PATCH 3/6] =?UTF-8?q?[Feat]=EB=8B=B9=EC=B2=A8=EB=B2=88=ED=98=B8?= =?UTF-8?q?=20=EC=9E=85=EB=A0=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/lotto/WinningNumberInput.java | 2 ++ src/main/java/lotto/WinningNumberValidator.java | 2 ++ 2 files changed, 4 insertions(+) create mode 100644 src/main/java/lotto/WinningNumberInput.java create mode 100644 src/main/java/lotto/WinningNumberValidator.java diff --git a/src/main/java/lotto/WinningNumberInput.java b/src/main/java/lotto/WinningNumberInput.java new file mode 100644 index 00000000000..9d098b85eb3 --- /dev/null +++ b/src/main/java/lotto/WinningNumberInput.java @@ -0,0 +1,2 @@ +package lotto;public class WinningNumberInput { +} diff --git a/src/main/java/lotto/WinningNumberValidator.java b/src/main/java/lotto/WinningNumberValidator.java new file mode 100644 index 00000000000..36da634df19 --- /dev/null +++ b/src/main/java/lotto/WinningNumberValidator.java @@ -0,0 +1,2 @@ +package lotto;public class WinningNumberValidator { +} From f049f85461cf83ca65f0b3c66f6aca3eeb5dd1fc Mon Sep 17 00:00:00 2001 From: EunSeop Date: Mon, 16 Sep 2024 16:49:49 +0900 Subject: [PATCH 4/6] =?UTF-8?q?[Feat]=EB=8B=B9=EC=B2=A8=EB=B2=88=ED=98=B8?= =?UTF-8?q?=20=EC=9E=85=EB=A0=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/lotto/WinningNumberInput.java | 33 ++++++++++++++++++- .../java/lotto/WinningNumberValidator.java | 33 ++++++++++++++++++- 2 files changed, 64 insertions(+), 2 deletions(-) diff --git a/src/main/java/lotto/WinningNumberInput.java b/src/main/java/lotto/WinningNumberInput.java index 9d098b85eb3..6339b76c7c9 100644 --- a/src/main/java/lotto/WinningNumberInput.java +++ b/src/main/java/lotto/WinningNumberInput.java @@ -1,2 +1,33 @@ -package lotto;public class WinningNumberInput { +package lotto; + +import camp.nextstep.edu.missionutils.Console; + +import java.util.List; + +public class WinningNumberInput { + + private List winningNumbers; + private int bonusNumber; + + public WinningNumberInput() { + setWinningNumber(); + } + + private void setWinningNumber() { + System.out.println("당첨 번호를 입력해 주세요."); + String input = Console.readLine(); + winningNumbers = WinningNumberValidator.parseAndValidateWinningNumbers(input); + + System.out.println("보너스 번호를 입력해 주세요."); + String bonusInput = Console.readLine(); + bonusNumber = WinningNumberValidator.validateBonusNumber(bonusInput, winningNumbers); + } + + public List getWinningNumbers() { + return winningNumbers; + } + + public int getBonusNumber() { + return bonusNumber; + } } diff --git a/src/main/java/lotto/WinningNumberValidator.java b/src/main/java/lotto/WinningNumberValidator.java index 36da634df19..5857a1e67d1 100644 --- a/src/main/java/lotto/WinningNumberValidator.java +++ b/src/main/java/lotto/WinningNumberValidator.java @@ -1,2 +1,33 @@ -package lotto;public class WinningNumberValidator { +package lotto; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +public class WinningNumberValidator { + + public static List parseAndValidateWinningNumbers(String input) { + List numbers = Arrays.stream(input.split(",")) + .map(String::trim) + .map(Integer::parseInt) + .collect(Collectors.toList()); + + if (numbers.size() != 6 || !numbers.stream().allMatch(WinningNumberValidator::isValidNumber)) { + throw new IllegalArgumentException("당첨 번호 오류"); + } + + return numbers; + } + + public static int validateBonusNumber(String input, List winningNumbers) { + int number = Integer.parseInt(input); + if (!isValidNumber(number) || winningNumbers.contains(number)) { + throw new IllegalArgumentException("보너스 번호 오류"); + } + return number; + } + + private static boolean isValidNumber(int number) { + return number >= 1 && number <= 45; + } } From 2d144801ee5efe11b2275ffb539ab8a50b223d92 Mon Sep 17 00:00:00 2001 From: EunSeop Date: Mon, 16 Sep 2024 23:33:34 +0900 Subject: [PATCH 5/6] =?UTF-8?q?[Feat]=EB=A1=9C=EB=98=90=20=EA=B2=B0?= =?UTF-8?q?=EA=B3=BC=20=EA=B3=84=EC=82=B0,=20=EC=B6=9C=EB=A0=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/lotto/Lotto.java | 4 ++ src/main/java/lotto/LottoResult.java | 60 +++++++++++++++++++ .../java/lotto/WinningNumberValidator.java | 4 +- 3 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 src/main/java/lotto/LottoResult.java diff --git a/src/main/java/lotto/Lotto.java b/src/main/java/lotto/Lotto.java index 519793d1f73..7ad68abbd01 100644 --- a/src/main/java/lotto/Lotto.java +++ b/src/main/java/lotto/Lotto.java @@ -16,5 +16,9 @@ private void validate(List numbers) { } } + public List getNumbers() { + return numbers; + } + // TODO: 추가 기능 구현 } diff --git a/src/main/java/lotto/LottoResult.java b/src/main/java/lotto/LottoResult.java new file mode 100644 index 00000000000..2fffdf6a718 --- /dev/null +++ b/src/main/java/lotto/LottoResult.java @@ -0,0 +1,60 @@ +package lotto; + +import java.util.ArrayList; +import java.util.List; + +public class LottoResult { + public int[] resultArray = {0, 0, 0, 0, 0}; + private int attempts; + + public LottoResult(List lottoList) { + attempts = lottoList.size(); + checkLotto(lottoList); + } + + private void checkLotto(List lottoList) { + WinningNumberInput winningNumberInput = new WinningNumberInput(); + + for (Lotto lotto : lottoList) { + List common = new ArrayList<>(winningNumberInput.getWinningNumbers()); + common.retainAll(lotto.getNumbers()); + + checkCondition(common.size(), lotto.getNumbers(), winningNumberInput); + } + } + + private void checkCondition(int commonSize, List lottoNumbers, WinningNumberInput winningNumberInput) { + if (commonSize == 6) { + resultArray[4]++; + return; + } + if (commonSize == 5 && lottoNumbers.contains(winningNumberInput.getBonusNumber())) { + resultArray[3]++; + return; + } + if (commonSize >= 3 && commonSize <= 5) { + resultArray[commonSize - 3]++; + return; + } + } + + private double CalculateReturn() { + int allResult = resultArray[0] * 5000 + resultArray[1] * 50000 + resultArray[2] * 1500000 + resultArray[3] * 30000000 + resultArray[4] * 200000000; + double result = (double) allResult / (attempts * 10); + + // 소수점 둘째자리에서 반올림 + return Math.round(result * 100) / 100.0; + } + + public void printResult() { + System.out.println("당첨 통계"); + System.out.println("---"); + System.out.println("3개 일치(5000원) - " + resultArray[0] + "개"); + System.out.println("4개 일치(50,000원) - " + resultArray[1] + "개"); + System.out.println("5개 일치(1,500,000원) - " + resultArray[2] + "개"); + System.out.println("5개 일치, 보너스 볼 일치 (30,000,000원) - " + resultArray[3] + "개"); + System.out.println("6개 일치(2,000,000,000원) - " + resultArray[4] + "개"); + System.out.println("총 수익률은 " + CalculateReturn() + "%입니다."); + } + +} diff --git a/src/main/java/lotto/WinningNumberValidator.java b/src/main/java/lotto/WinningNumberValidator.java index 5857a1e67d1..4f05b2a34df 100644 --- a/src/main/java/lotto/WinningNumberValidator.java +++ b/src/main/java/lotto/WinningNumberValidator.java @@ -13,7 +13,7 @@ public static List parseAndValidateWinningNumbers(String input) { .collect(Collectors.toList()); if (numbers.size() != 6 || !numbers.stream().allMatch(WinningNumberValidator::isValidNumber)) { - throw new IllegalArgumentException("당첨 번호 오류"); + throw new IllegalArgumentException("[ERROR] 당첨 번호 오류"); } return numbers; @@ -22,7 +22,7 @@ public static List parseAndValidateWinningNumbers(String input) { public static int validateBonusNumber(String input, List winningNumbers) { int number = Integer.parseInt(input); if (!isValidNumber(number) || winningNumbers.contains(number)) { - throw new IllegalArgumentException("보너스 번호 오류"); + throw new IllegalArgumentException("[ERROR] 보너스 번호 오류"); } return number; } From 96b6de2e0051215b4831741099e9a4af6a9b9afd Mon Sep 17 00:00:00 2001 From: EunSeop Date: Tue, 17 Sep 2024 21:00:07 +0900 Subject: [PATCH 6/6] =?UTF-8?q?[Feat]=EA=B5=AC=EB=A7=A4=ED=95=9C=20?= =?UTF-8?q?=EB=A1=9C=EB=98=90=20=EC=B6=9C=EB=A0=A5=EA=B8=B0=EB=8A=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/lotto/Lotto.java | 4 ++++ src/main/java/lotto/LottoCreator.java | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/src/main/java/lotto/Lotto.java b/src/main/java/lotto/Lotto.java index 7ad68abbd01..a876a73cce7 100644 --- a/src/main/java/lotto/Lotto.java +++ b/src/main/java/lotto/Lotto.java @@ -20,5 +20,9 @@ public List getNumbers() { return numbers; } + public void printLotto() { + System.out.println(numbers); + } + // TODO: 추가 기능 구현 } diff --git a/src/main/java/lotto/LottoCreator.java b/src/main/java/lotto/LottoCreator.java index 1ebe5e4e348..1c49e060510 100644 --- a/src/main/java/lotto/LottoCreator.java +++ b/src/main/java/lotto/LottoCreator.java @@ -23,4 +23,10 @@ private void createLotto(int count) { public List getLottoList() { return lottoList; } + + public void printLottoList() { + for (Lotto lotto : lottoList) { + lotto.printLotto(); + } + } }