From 07464d3a5548d2adc1bded59879996c66b40f675 Mon Sep 17 00:00:00 2001 From: anhye0n Date: Mon, 6 May 2024 21:17:42 +0900 Subject: [PATCH 1/5] step2 --- src/main/java/Main.java | 28 +++++++++++++ src/main/java/domain/GenerateLotto.java | 35 +++++++++++++++++ src/main/java/domain/GenerateRandom.java | 12 ++++++ src/main/java/domain/Input.java | 37 ++++++++++++++++++ src/main/java/domain/WinningLotto.java | 50 ++++++++++++++++++++++++ src/main/java/view/Print.java | 31 +++++++++++++++ 6 files changed, 193 insertions(+) create mode 100644 src/main/java/Main.java create mode 100644 src/main/java/domain/GenerateLotto.java create mode 100644 src/main/java/domain/GenerateRandom.java create mode 100644 src/main/java/domain/Input.java create mode 100644 src/main/java/domain/WinningLotto.java create mode 100644 src/main/java/view/Print.java diff --git a/src/main/java/Main.java b/src/main/java/Main.java new file mode 100644 index 00000000..406dd4a0 --- /dev/null +++ b/src/main/java/Main.java @@ -0,0 +1,28 @@ +import domain.Input; +import domain.GenerateLotto; +import domain.WinningLotto; +import view.Print; + +import java.util.ArrayList; + +public class Main { + public static void main(String[] args) { + GenerateLotto lotto = new GenerateLotto(); + WinningLotto winningLotto = new WinningLotto(); + Print print = new Print(); + Input input = new Input(); + + int purchaseAmount = input.setPurchaseAmount(); + + ArrayList> totalLottos = lotto.getLotto(purchaseAmount); + + print.printPurchasedLottoCount(totalLottos); + + ArrayList winningNumbers = input.setWinningNumber(); + + winningLotto.totalCheckLotto(totalLottos, winningNumbers); + + print.printWinningCount(winningLotto.winningCount, winningLotto.calculateRate(purchaseAmount)); + + } +} \ No newline at end of file diff --git a/src/main/java/domain/GenerateLotto.java b/src/main/java/domain/GenerateLotto.java new file mode 100644 index 00000000..f116c3d7 --- /dev/null +++ b/src/main/java/domain/GenerateLotto.java @@ -0,0 +1,35 @@ +package domain; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashSet; +import java.util.Set; + +public class GenerateLotto { + ArrayList> totalLotto = new ArrayList<>(); + + private ArrayList getOne() { + GenerateRandom random = new GenerateRandom(); + + Set set = new HashSet<>(); + + while (set.size() != 6) { + set.add(random.generateRandom()); + } + ArrayList lotto = new ArrayList<>(set); + + + Collections.sort(lotto); + + return lotto; + } + + public ArrayList> getLotto(int money) { + for (int i = 0; i < money; i += 1000) { + totalLotto.add(this.getOne()); + } + + Collections.shuffle(totalLotto); + return totalLotto; + } +} diff --git a/src/main/java/domain/GenerateRandom.java b/src/main/java/domain/GenerateRandom.java new file mode 100644 index 00000000..e3e1f70b --- /dev/null +++ b/src/main/java/domain/GenerateRandom.java @@ -0,0 +1,12 @@ +package domain; + +import java.util.Random; + +public class GenerateRandom { + public int generateRandom() { + Random random = new Random(); + + return random.nextInt(45) + 1; + } + +} diff --git a/src/main/java/domain/Input.java b/src/main/java/domain/Input.java new file mode 100644 index 00000000..9b3d7025 --- /dev/null +++ b/src/main/java/domain/Input.java @@ -0,0 +1,37 @@ +package domain; + +import java.util.ArrayList; +import java.util.Scanner; + +public class Input { + + public int setPurchaseAmount() { + System.out.println("구입금액을 입력해주세요."); + Scanner scanner = new Scanner(System.in); + + return scanner.nextInt(); + } + + public ArrayList setWinningNumber() { + System.out.println("지난 주 당첨 번호를 입력해 주세요."); + Scanner scanner = new Scanner(System.in); + + String line = scanner.nextLine(); // 전체 한 줄을 입력받음 + + String[] numberStrings = line.split(", "); // 공백을 기준으로 문자열 분리 + ArrayList numbers = new ArrayList<>(); + + for (String numberString : numberStrings) { + numbers.add(Integer.parseInt(numberString)); + } + + return numbers; + } + + public int setBonusNumber() { + System.out.println("보너스 볼을 입력해 주세요"); + Scanner scanner = new Scanner(System.in); + + return scanner.nextInt(); + } +} diff --git a/src/main/java/domain/WinningLotto.java b/src/main/java/domain/WinningLotto.java new file mode 100644 index 00000000..b2ea5a6e --- /dev/null +++ b/src/main/java/domain/WinningLotto.java @@ -0,0 +1,50 @@ +package domain; + +import java.util.ArrayList; +import java.util.Arrays; + +public class WinningLotto { + public ArrayList winningCount = new ArrayList<>(Arrays.asList(0, 0, 0, 0)); + + private int checkLotto(ArrayList lotto, ArrayList winningNumbers) { + int count = 0; + + for (int winningNumber : winningNumbers) { + + if (lotto.contains(winningNumber)) { + count++; + } + } + + return count; + } + + private void addWinningCount(int count) { + + if (count == 3) { + winningCount.set(0, winningCount.get(0) + 1); + return; + } + if (count == 4) { + winningCount.set(1, winningCount.get(1) + 1); + return; + } + if (count == 5) { + winningCount.set(2, winningCount.get(2) + 1); + return; + } + if (count == 6) { + winningCount.set(3, winningCount.get(3) + 1); + } + } + + public void totalCheckLotto(ArrayList> totalLottes, ArrayList winningNumbers) { + for (ArrayList totalLotto : totalLottes) { + addWinningCount(checkLotto(totalLotto, winningNumbers)); + } + } + + public double calculateRate(int money) { + return (double) (5000 * winningCount.get(0) + 50000 * winningCount.get(1) + 1500000 * winningCount.get(2) + 2000000000 * winningCount.get(3)) / money; + } +} diff --git a/src/main/java/view/Print.java b/src/main/java/view/Print.java new file mode 100644 index 00000000..f09b2ca2 --- /dev/null +++ b/src/main/java/view/Print.java @@ -0,0 +1,31 @@ +package view; + +import java.util.ArrayList; + +public class Print { + + // 1~6개 일치하는 갯수 + public void printPurchasedLottoCount(ArrayList> totalLottos) { + System.out.printf("\n%d개를 구매했습니다.\n", totalLottos.size()); + for (ArrayList totalLotto : totalLottos) { + System.out.println(totalLotto); + } + + // for +// System.out.printf("%d개 일치 (%d원)- %d개", ); + } + + public void printWinningCount(ArrayList winningCount, double rate) { + System.out.println("\n당첨 통계"); + System.out.println("---------"); + + System.out.printf("3개 일치 (5000원)- %d개\n", winningCount.get(0)); + System.out.printf("4개 일치 (50000원)- %d개\n", winningCount.get(1)); + System.out.printf("5개 일치 (1500000원)- %d개\n", winningCount.get(2)); + System.out.printf("6개 일치 (2000000000원)- %d개\n", winningCount.get(3)); + + System.out.printf("총 수익률은 %.2f입니다.(기준이 1이기 때문에 결과적으로 손해라는 의미임)\n", rate); + + } + +} From a5d38989885904375675e1c6da5afec1d864d9da Mon Sep 17 00:00:00 2001 From: anhye0n Date: Mon, 6 May 2024 22:09:19 +0900 Subject: [PATCH 2/5] step3 --- src/main/java/Main.java | 5 ++++- src/main/java/domain/WinningLotto.java | 22 ++++++++++++++----- src/main/java/domain/WinningNumbers.java | 28 ++++++++++++++++++++++++ src/main/java/view/Print.java | 11 ++++++---- 4 files changed, 55 insertions(+), 11 deletions(-) create mode 100644 src/main/java/domain/WinningNumbers.java diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 406dd4a0..94afda6c 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,6 +1,7 @@ import domain.Input; import domain.GenerateLotto; import domain.WinningLotto; +import domain.WinningNumbers; import view.Print; import java.util.ArrayList; @@ -20,7 +21,9 @@ public static void main(String[] args) { ArrayList winningNumbers = input.setWinningNumber(); - winningLotto.totalCheckLotto(totalLottos, winningNumbers); + int bonusNumber = input.setBonusNumber(); + + winningLotto.totalCheckLotto(totalLottos, winningNumbers, bonusNumber); print.printWinningCount(winningLotto.winningCount, winningLotto.calculateRate(purchaseAmount)); diff --git a/src/main/java/domain/WinningLotto.java b/src/main/java/domain/WinningLotto.java index b2ea5a6e..7b3dd9cd 100644 --- a/src/main/java/domain/WinningLotto.java +++ b/src/main/java/domain/WinningLotto.java @@ -4,7 +4,7 @@ import java.util.Arrays; public class WinningLotto { - public ArrayList winningCount = new ArrayList<>(Arrays.asList(0, 0, 0, 0)); + public ArrayList winningCount = new ArrayList<>(Arrays.asList(0, 0, 0, 0, 0)); private int checkLotto(ArrayList lotto, ArrayList winningNumbers) { int count = 0; @@ -19,7 +19,7 @@ private int checkLotto(ArrayList lotto, ArrayList winningNumbe return count; } - private void addWinningCount(int count) { + private void addWinningCount(ArrayList totalLotto, int count, int bonusNumber) { if (count == 3) { winningCount.set(0, winningCount.get(0) + 1); @@ -30,21 +30,31 @@ private void addWinningCount(int count) { return; } if (count == 5) { - winningCount.set(2, winningCount.get(2) + 1); + addBonusCount(totalLotto, bonusNumber); return; } if (count == 6) { + winningCount.set(4, winningCount.get(4) + 1); + } + } + + private void addBonusCount(ArrayList lotto, int bonusNumber) { + if (lotto.contains(bonusNumber)) { winningCount.set(3, winningCount.get(3) + 1); + return; } + + winningCount.set(2, winningCount.get(2) + 1); } - public void totalCheckLotto(ArrayList> totalLottes, ArrayList winningNumbers) { + public void totalCheckLotto(ArrayList> totalLottes, ArrayList winningNumbers, int bonusNumber) { for (ArrayList totalLotto : totalLottes) { - addWinningCount(checkLotto(totalLotto, winningNumbers)); + addWinningCount(totalLotto, checkLotto(totalLotto, winningNumbers), bonusNumber); } } public double calculateRate(int money) { - return (double) (5000 * winningCount.get(0) + 50000 * winningCount.get(1) + 1500000 * winningCount.get(2) + 2000000000 * winningCount.get(3)) / money; + return (double) (WinningNumbers.findByIndex(0) * winningCount.get(0) + WinningNumbers.findByIndex(1) * winningCount.get(1) + WinningNumbers.findByIndex(2) * winningCount.get(2) + WinningNumbers.findByIndex(3) * winningCount.get(3) + WinningNumbers.findByIndex(4) * winningCount.get(4)) / money; } } + diff --git a/src/main/java/domain/WinningNumbers.java b/src/main/java/domain/WinningNumbers.java new file mode 100644 index 00000000..1d95e5c8 --- /dev/null +++ b/src/main/java/domain/WinningNumbers.java @@ -0,0 +1,28 @@ +package domain; + +public enum WinningNumbers { + FIFTH(5000), + FOURTH(50000), + THIRD(1500000), + SECOND(30000000), + FIRST(2000000000); + + private final int amount; + + WinningNumbers(int amount) { + this.amount = amount; + } + + public int amount() { + return amount; + } + + public static int findByIndex(int index) { + if (index >= 0 && index < values().length) { + return values()[index].amount(); + } + + throw new IllegalArgumentException(); + } + +} diff --git a/src/main/java/view/Print.java b/src/main/java/view/Print.java index f09b2ca2..612b791b 100644 --- a/src/main/java/view/Print.java +++ b/src/main/java/view/Print.java @@ -1,5 +1,7 @@ package view; +import domain.WinningNumbers; + import java.util.ArrayList; public class Print { @@ -19,10 +21,11 @@ public void printWinningCount(ArrayList winningCount, double rate) { System.out.println("\n당첨 통계"); System.out.println("---------"); - System.out.printf("3개 일치 (5000원)- %d개\n", winningCount.get(0)); - System.out.printf("4개 일치 (50000원)- %d개\n", winningCount.get(1)); - System.out.printf("5개 일치 (1500000원)- %d개\n", winningCount.get(2)); - System.out.printf("6개 일치 (2000000000원)- %d개\n", winningCount.get(3)); + System.out.printf("3개 일치 (%d원)- %d개\n", WinningNumbers.findByIndex(0), winningCount.get(0)); + System.out.printf("4개 일치 (%d원)- %d개\n", WinningNumbers.findByIndex(1), winningCount.get(1)); + System.out.printf("5개 일치 (%d원)- %d개\n", WinningNumbers.findByIndex(2), winningCount.get(2)); + System.out.printf("5개 일치, 보너스 볼 일치(%d원) - %d개\n", WinningNumbers.findByIndex(3), winningCount.get(3)); + System.out.printf("6개 일치 (%d원)- %d개\n", WinningNumbers.findByIndex(4), winningCount.get(4)); System.out.printf("총 수익률은 %.2f입니다.(기준이 1이기 때문에 결과적으로 손해라는 의미임)\n", rate); From 5f2878cfdeb8eed2efc947954992cfd6bc482cc6 Mon Sep 17 00:00:00 2001 From: anhye0n Date: Mon, 6 May 2024 23:34:39 +0900 Subject: [PATCH 3/5] step4 --- src/main/java/Main.java | 13 +++-- src/main/java/domain/GenerateLotto.java | 46 ++++++++++++++- src/main/java/domain/Input.java | 74 ++++++++++++++++++++++--- src/main/java/view/Print.java | 6 +- 4 files changed, 123 insertions(+), 16 deletions(-) diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 94afda6c..c2746cb4 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,7 +1,6 @@ import domain.Input; import domain.GenerateLotto; import domain.WinningLotto; -import domain.WinningNumbers; import view.Print; import java.util.ArrayList; @@ -15,13 +14,19 @@ public static void main(String[] args) { int purchaseAmount = input.setPurchaseAmount(); - ArrayList> totalLottos = lotto.getLotto(purchaseAmount); + int manualCount = input.setManualCount(); - print.printPurchasedLottoCount(totalLottos); + if (manualCount > 0) { + lotto.getManualLotto(purchaseAmount, manualCount); + } + + ArrayList> totalLottos = lotto.getLotto(lotto.getRemainingMoney(purchaseAmount, manualCount)); + + print.printPurchasedLottoCount(manualCount, totalLottos); ArrayList winningNumbers = input.setWinningNumber(); - int bonusNumber = input.setBonusNumber(); + int bonusNumber = input.setBonusNumber(winningNumbers); winningLotto.totalCheckLotto(totalLottos, winningNumbers, bonusNumber); diff --git a/src/main/java/domain/GenerateLotto.java b/src/main/java/domain/GenerateLotto.java index f116c3d7..da869439 100644 --- a/src/main/java/domain/GenerateLotto.java +++ b/src/main/java/domain/GenerateLotto.java @@ -29,7 +29,51 @@ public ArrayList> getLotto(int money) { totalLotto.add(this.getOne()); } - Collections.shuffle(totalLotto); return totalLotto; } + + public ArrayList getManualOne() { + + Input input = new Input(); + + ArrayList manualOne = input.setManualNumber(); + + for (int one: manualOne){ + validateLottoRange(one); + } + + if (manualOne.stream().distinct().count() != 6) { + throw new IllegalArgumentException("로또 번호 중복 입력은 불가능합니다."); + } + + Collections.sort(manualOne); + + return manualOne; + } + + public void getManualLotto(int purchaseAmount, int manualCount) { + if (checkRemainingMoney(purchaseAmount, manualCount)) { + throw new IllegalArgumentException("구입 금액을 넘은 갯수 입니다."); + } + System.out.println("\n수동으로 구매할 번호를 입력해 주세요."); + + for (int i = 0; i < manualCount; i++) { + totalLotto.add(this.getManualOne()); + } + } + + public int getRemainingMoney(int money, int manualCount) { + return money - (1000 * manualCount); + } + + public boolean checkRemainingMoney(int money, int manualCount) { + return money < manualCount * 1000; + } + + public void validateLottoRange(int number) { + if (number < 1 || number > 45) { + throw new IllegalArgumentException("1 ~ 45사이의 값만 입력할 수 있습니다."); + } + + } } diff --git a/src/main/java/domain/Input.java b/src/main/java/domain/Input.java index 9b3d7025..af618b86 100644 --- a/src/main/java/domain/Input.java +++ b/src/main/java/domain/Input.java @@ -9,16 +9,72 @@ public int setPurchaseAmount() { System.out.println("구입금액을 입력해주세요."); Scanner scanner = new Scanner(System.in); - return scanner.nextInt(); + int money = scanner.nextInt(); + + if (validateNegativeNumber(money)) { + throw new IllegalArgumentException(); + } + + return money; } public ArrayList setWinningNumber() { - System.out.println("지난 주 당첨 번호를 입력해 주세요."); + System.out.println("\n지난 주 당첨 번호를 입력해 주세요."); Scanner scanner = new Scanner(System.in); - String line = scanner.nextLine(); // 전체 한 줄을 입력받음 + String line = scanner.nextLine(); + + int count = separateNumber(line).size(); + + if ( + validateNegativeNumber(count) + || + validateLottoLength(count) + || + separateNumber(line).stream().distinct().count() != 6 + ) { + throw new IllegalArgumentException(); + } + + return separateNumber(line); + } - String[] numberStrings = line.split(", "); // 공백을 기준으로 문자열 분리 + public int setBonusNumber(ArrayList winningNumbers) { + System.out.println("\n보너스 볼을 입력해 주세요"); + Scanner scanner = new Scanner(System.in); + int bonusNumber = scanner.nextInt(); + + winningNumbers.add(bonusNumber); + + if (winningNumbers.stream().distinct().count() != 7) { + throw new IllegalArgumentException("보너스 숫자는 중복이 불가능합니다."); + } + return bonusNumber; + } + + public int setManualCount() { + System.out.println("\n수동으로 구매할 로또 수를 입력해 주세요."); + Scanner scanner = new Scanner(System.in); + + int count = scanner.nextInt(); + + if (validateNegativeNumber(count)) { + throw new IllegalArgumentException(); + } + return count; + + } + + public ArrayList setManualNumber() { + Scanner scanner = new Scanner(System.in); + + String line = scanner.nextLine(); + + return separateNumber(line); + } + + public ArrayList separateNumber(String raw) { + String[] numberStrings = raw.split(", "); ArrayList numbers = new ArrayList<>(); for (String numberString : numberStrings) { @@ -28,10 +84,12 @@ public ArrayList setWinningNumber() { return numbers; } - public int setBonusNumber() { - System.out.println("보너스 볼을 입력해 주세요"); - Scanner scanner = new Scanner(System.in); + public boolean validateNegativeNumber(int number) { + return number < 0; + } - return scanner.nextInt(); + public boolean validateLottoLength(int number) { + return number > 6; } + } diff --git a/src/main/java/view/Print.java b/src/main/java/view/Print.java index 612b791b..e4ce3f1d 100644 --- a/src/main/java/view/Print.java +++ b/src/main/java/view/Print.java @@ -7,8 +7,8 @@ public class Print { // 1~6개 일치하는 갯수 - public void printPurchasedLottoCount(ArrayList> totalLottos) { - System.out.printf("\n%d개를 구매했습니다.\n", totalLottos.size()); + public void printPurchasedLottoCount(int manualCount, ArrayList> totalLottos) { + System.out.printf("\n수동으로 %d장, 자동으로 %d개를 구매했습니다.\n", manualCount, totalLottos.size() - manualCount); for (ArrayList totalLotto : totalLottos) { System.out.println(totalLotto); } @@ -27,7 +27,7 @@ public void printWinningCount(ArrayList winningCount, double rate) { System.out.printf("5개 일치, 보너스 볼 일치(%d원) - %d개\n", WinningNumbers.findByIndex(3), winningCount.get(3)); System.out.printf("6개 일치 (%d원)- %d개\n", WinningNumbers.findByIndex(4), winningCount.get(4)); - System.out.printf("총 수익률은 %.2f입니다.(기준이 1이기 때문에 결과적으로 손해라는 의미임)\n", rate); + System.out.printf("총 수익률은 %.2f입니다.\n", rate); } From ac165995efa392481c3ead6370178ad395be439f Mon Sep 17 00:00:00 2001 From: anhye0n Date: Tue, 21 May 2024 13:21:35 +0900 Subject: [PATCH 4/5] =?UTF-8?q?feat:=20record=20=EB=A6=AC=ED=8C=A9?= =?UTF-8?q?=ED=86=A0=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/Main.java | 2 + src/main/java/domain/GenerateLotto.java | 37 ++++++----------- src/main/java/domain/Input.java | 47 +++++----------------- src/main/java/exception/BonusNumber.java | 16 ++++++++ src/main/java/exception/Lotto.java | 31 ++++++++++++++ src/main/java/exception/ManualNumber.java | 29 +++++++++++++ src/main/java/exception/Money.java | 15 +++++++ src/main/java/exception/WinningNumber.java | 4 ++ 8 files changed, 118 insertions(+), 63 deletions(-) create mode 100644 src/main/java/exception/BonusNumber.java create mode 100644 src/main/java/exception/Lotto.java create mode 100644 src/main/java/exception/ManualNumber.java create mode 100644 src/main/java/exception/Money.java create mode 100644 src/main/java/exception/WinningNumber.java diff --git a/src/main/java/Main.java b/src/main/java/Main.java index c2746cb4..7f52b65f 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,6 +1,8 @@ import domain.Input; import domain.GenerateLotto; import domain.WinningLotto; +import exception.Lotto; +import exception.Money; import view.Print; import java.util.ArrayList; diff --git a/src/main/java/domain/GenerateLotto.java b/src/main/java/domain/GenerateLotto.java index da869439..71a6ecfd 100644 --- a/src/main/java/domain/GenerateLotto.java +++ b/src/main/java/domain/GenerateLotto.java @@ -1,5 +1,8 @@ package domain; +import exception.Lotto; +import exception.ManualNumber; + import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; @@ -8,6 +11,8 @@ public class GenerateLotto { ArrayList> totalLotto = new ArrayList<>(); + private final int LOTTO_PRICE = 1000; + private ArrayList getOne() { GenerateRandom random = new GenerateRandom(); @@ -25,7 +30,7 @@ private ArrayList getOne() { } public ArrayList> getLotto(int money) { - for (int i = 0; i < money; i += 1000) { + for (int i = 0; i < money; i += LOTTO_PRICE) { totalLotto.add(this.getOne()); } @@ -38,42 +43,24 @@ public ArrayList getManualOne() { ArrayList manualOne = input.setManualNumber(); - for (int one: manualOne){ - validateLottoRange(one); - } - - if (manualOne.stream().distinct().count() != 6) { - throw new IllegalArgumentException("로또 번호 중복 입력은 불가능합니다."); - } - Collections.sort(manualOne); - return manualOne; + return new Lotto(manualOne).lotto(); } public void getManualLotto(int purchaseAmount, int manualCount) { - if (checkRemainingMoney(purchaseAmount, manualCount)) { - throw new IllegalArgumentException("구입 금액을 넘은 갯수 입니다."); - } + // TODO + ManualNumber manualNumber = new ManualNumber(purchaseAmount, manualCount); + System.out.println("\n수동으로 구매할 번호를 입력해 주세요."); - for (int i = 0; i < manualCount; i++) { + for (int i = 0; i < manualNumber.count(); i++) { totalLotto.add(this.getManualOne()); } } public int getRemainingMoney(int money, int manualCount) { - return money - (1000 * manualCount); - } - - public boolean checkRemainingMoney(int money, int manualCount) { - return money < manualCount * 1000; + return money - (LOTTO_PRICE * manualCount); } - public void validateLottoRange(int number) { - if (number < 1 || number > 45) { - throw new IllegalArgumentException("1 ~ 45사이의 값만 입력할 수 있습니다."); - } - - } } diff --git a/src/main/java/domain/Input.java b/src/main/java/domain/Input.java index af618b86..4a80fee1 100644 --- a/src/main/java/domain/Input.java +++ b/src/main/java/domain/Input.java @@ -1,21 +1,19 @@ package domain; +import exception.BonusNumber; +import exception.Lotto; +import exception.Money; + import java.util.ArrayList; import java.util.Scanner; -public class Input { +public record Input() { public int setPurchaseAmount() { System.out.println("구입금액을 입력해주세요."); Scanner scanner = new Scanner(System.in); - int money = scanner.nextInt(); - - if (validateNegativeNumber(money)) { - throw new IllegalArgumentException(); - } - - return money; + return new Money(scanner.nextInt()).money(); } public ArrayList setWinningNumber() { @@ -24,19 +22,7 @@ public ArrayList setWinningNumber() { String line = scanner.nextLine(); - int count = separateNumber(line).size(); - - if ( - validateNegativeNumber(count) - || - validateLottoLength(count) - || - separateNumber(line).stream().distinct().count() != 6 - ) { - throw new IllegalArgumentException(); - } - - return separateNumber(line); + return new Lotto(separateNumber(line)).lotto(); } public int setBonusNumber(ArrayList winningNumbers) { @@ -46,22 +32,14 @@ public int setBonusNumber(ArrayList winningNumbers) { winningNumbers.add(bonusNumber); - if (winningNumbers.stream().distinct().count() != 7) { - throw new IllegalArgumentException("보너스 숫자는 중복이 불가능합니다."); - } - return bonusNumber; + return new BonusNumber(winningNumbers, bonusNumber).bonusNumber(); } public int setManualCount() { System.out.println("\n수동으로 구매할 로또 수를 입력해 주세요."); Scanner scanner = new Scanner(System.in); - int count = scanner.nextInt(); - - if (validateNegativeNumber(count)) { - throw new IllegalArgumentException(); - } - return count; + return scanner.nextInt(); } @@ -84,12 +62,5 @@ public ArrayList separateNumber(String raw) { return numbers; } - public boolean validateNegativeNumber(int number) { - return number < 0; - } - - public boolean validateLottoLength(int number) { - return number > 6; - } } diff --git a/src/main/java/exception/BonusNumber.java b/src/main/java/exception/BonusNumber.java new file mode 100644 index 00000000..0f636542 --- /dev/null +++ b/src/main/java/exception/BonusNumber.java @@ -0,0 +1,16 @@ +package exception; + +import java.util.ArrayList; + +public record BonusNumber(ArrayList winningNumbers, int bonusNumber) { + + public BonusNumber { + validateDuplication(winningNumbers); + } + + private static void validateDuplication(ArrayList winningNumbers) { + if (winningNumbers.stream().distinct().count() != 7) { + throw new IllegalArgumentException("숫자는 중복 불가능합니다."); + } + } +} diff --git a/src/main/java/exception/Lotto.java b/src/main/java/exception/Lotto.java new file mode 100644 index 00000000..7503b3fc --- /dev/null +++ b/src/main/java/exception/Lotto.java @@ -0,0 +1,31 @@ +package exception; + +import java.util.ArrayList; + +public record Lotto(ArrayList lotto) { + + public Lotto { + validateLottoRange(lotto); + validateDuplication(lotto); + validateLottoLength(lotto); + } + + private void validateLottoRange(ArrayList lotto) { + if (lotto.stream().allMatch(number -> number < 1 || number > 45)) { + throw new IllegalArgumentException("1 ~ 45사이의 값만 입력할 수 있습니다."); + } + } + + private static void validateDuplication(ArrayList winningNumbers) { + if (winningNumbers.stream().distinct().count() != 6) { + throw new IllegalArgumentException("로또 번호 중복 입력은 불가능합니다."); + } + } + + private void validateLottoLength(ArrayList lotto) { + if (lotto.size() != 6) { + throw new IllegalArgumentException("길이가 맞지 않습니다."); + } + } + +} diff --git a/src/main/java/exception/ManualNumber.java b/src/main/java/exception/ManualNumber.java new file mode 100644 index 00000000..cd1178ba --- /dev/null +++ b/src/main/java/exception/ManualNumber.java @@ -0,0 +1,29 @@ +package exception; + +import java.util.ArrayList; + +public record ManualNumber(int money, int count) { + private static final int LOTTO_PRICE = 1000; + + public ManualNumber { + validateNegativeNumber(count); + checkRemainingMoney(money, count); + } + + private void validateNegativeNumber(int number) { + if (number < 0) { + throw new IllegalArgumentException("음수는 입력될 수 없습니다."); + } + } + public void validateLottoRange(ArrayList lotto) { + if (lotto.stream().allMatch(number -> number >= 1 && number <= 45)) { + throw new IllegalArgumentException("1 ~ 45사이의 값만 입력할 수 있습니다."); + } + } + + public void checkRemainingMoney(int money, int manualCount) { + if (money < manualCount * LOTTO_PRICE) { + throw new IllegalArgumentException("구입 금액을 넘은 갯수 입니다."); + } + } +} diff --git a/src/main/java/exception/Money.java b/src/main/java/exception/Money.java new file mode 100644 index 00000000..53d64756 --- /dev/null +++ b/src/main/java/exception/Money.java @@ -0,0 +1,15 @@ +package exception; + +public record Money(int money) { + + public Money { + validateNegativeNumber(money); + } + + private void validateNegativeNumber(int money) { + if (money < 0) { + throw new IllegalArgumentException("구입 금액이 음수일 수 없습니다."); + } + } + +} diff --git a/src/main/java/exception/WinningNumber.java b/src/main/java/exception/WinningNumber.java new file mode 100644 index 00000000..d5980283 --- /dev/null +++ b/src/main/java/exception/WinningNumber.java @@ -0,0 +1,4 @@ +package exception; + +public record WinningNumber() { +} From b8c0186777881372469ef11eba1ddd45ea7b2834 Mon Sep 17 00:00:00 2001 From: anhye0n Date: Tue, 21 May 2024 13:30:40 +0900 Subject: [PATCH 5/5] =?UTF-8?q?refactor:=20=EB=AF=B8=EC=82=AC=EC=9A=A9=20?= =?UTF-8?q?=ED=8C=8C=EC=9D=BC=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/exception/WinningNumber.java | 4 ---- 1 file changed, 4 deletions(-) delete mode 100644 src/main/java/exception/WinningNumber.java diff --git a/src/main/java/exception/WinningNumber.java b/src/main/java/exception/WinningNumber.java deleted file mode 100644 index d5980283..00000000 --- a/src/main/java/exception/WinningNumber.java +++ /dev/null @@ -1,4 +0,0 @@ -package exception; - -public record WinningNumber() { -}