From 693ec1dd8a855e7ca3eb44eb60c216f9132cd78b Mon Sep 17 00:00:00 2001 From: Jihyun Date: Sun, 14 Jul 2024 02:47:29 +0900 Subject: [PATCH 1/2] java-lotto-2 --- src/main/java/lotto/Application.java | 33 +++++++- src/main/java/lotto/ENUM.java | 33 ++++++++ src/main/java/lotto/ExceptionOfLength.java | 9 +++ src/main/java/lotto/ExceptionOfNumber.java | 17 ++++ src/main/java/lotto/ExceptionOfPrice.java | 10 +++ src/main/java/lotto/InsertPrice.java | 10 +++ src/main/java/lotto/Lotto.java | 16 ++-- src/main/java/lotto/RandomLotto.java | 90 ++++++++++++++++++++++ 8 files changed, 208 insertions(+), 10 deletions(-) create mode 100644 src/main/java/lotto/ENUM.java create mode 100644 src/main/java/lotto/ExceptionOfLength.java create mode 100644 src/main/java/lotto/ExceptionOfNumber.java create mode 100644 src/main/java/lotto/ExceptionOfPrice.java create mode 100644 src/main/java/lotto/InsertPrice.java create mode 100644 src/main/java/lotto/RandomLotto.java diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java index d190922ba44..b9e65849879 100644 --- a/src/main/java/lotto/Application.java +++ b/src/main/java/lotto/Application.java @@ -1,7 +1,38 @@ package lotto; +import camp.nextstep.edu.missionutils.Console; + public class Application { public static void main(String[] args) { - // TODO: 프로그램 구현 + while(true) { + try { + System.out.println("구입금액을 입력해 주세요."); + String price = Console.readLine(); + int lottoPrice = Integer.parseInt(price); + + //로또 구입 금액 입력 + InsertPrice insertPrice = new InsertPrice(); + insertPrice.insertPrice(lottoPrice); + + //구입 금액만큼 랜덤 로또 번호 출력 + RandomLotto randomLotto = new RandomLotto(); + randomLotto.randomLotto(lottoPrice); + + //당첨 번호, 보너스 번호 입력 + randomLotto.insertNumber(); + + //당첨 통계 + randomLotto.winningStatistics(lottoPrice); + //당첨 내역 출력 + randomLotto.printStatistics(); + //수익률 출력 + randomLotto.earningRate(lottoPrice); + + break; + + } catch (IllegalArgumentException e) { + System.out.println(e.getMessage()); + } + } } } diff --git a/src/main/java/lotto/ENUM.java b/src/main/java/lotto/ENUM.java new file mode 100644 index 00000000000..fd2f3b991f0 --- /dev/null +++ b/src/main/java/lotto/ENUM.java @@ -0,0 +1,33 @@ +package lotto; + +public class ENUM { + public enum WinningEnum { + three(3, 5000, "3개 일치 (5,000원) - "), + four(4, 50000, "4개 일치 (50,000원) - "), + five(5, 1500000, "5개 일치 (1,500,000원) - "), + bonus(7, 30000000, "5개 일치, 보너스 볼 일치 (30,000,000원) - "), + six(6, 2000000000, "6개 일치 (2,000,000,000원) - "); + + private final int index; + private final int price; + private final String message; + + WinningEnum(int index, int price, String message) { + this.index = index; + this.price = price; + this.message = message; + } + + public int getIndex() { + return index; + } + + public int getPrice() { + return price; + } + + public String getMessage() { + return message; + } + } +} diff --git a/src/main/java/lotto/ExceptionOfLength.java b/src/main/java/lotto/ExceptionOfLength.java new file mode 100644 index 00000000000..510eb810d5e --- /dev/null +++ b/src/main/java/lotto/ExceptionOfLength.java @@ -0,0 +1,9 @@ +package lotto; + +public class ExceptionOfLength { + void exceptionOfLength(String[] numbers){ + if(numbers.length != 6) { + throw new IllegalArgumentException("[ERROR] 로또 번호는 6개여야 합니다."); + } + } +} diff --git a/src/main/java/lotto/ExceptionOfNumber.java b/src/main/java/lotto/ExceptionOfNumber.java new file mode 100644 index 00000000000..9d9b8b77e87 --- /dev/null +++ b/src/main/java/lotto/ExceptionOfNumber.java @@ -0,0 +1,17 @@ +package lotto; + +public class ExceptionOfNumber { + // 당첨번호 6개가 1-45사이 숫자가 아닌 경우 예외처리 + void exceptionOfNumber(String[] numbers, int bonusNumber) { + for (String n : numbers) { + int number = Integer.parseInt(n); + if (number < 1 || number > 45) { + throw new IllegalArgumentException("[ERROR] 로또 번호는 1부터 45 사이의 숫자여야 합니다."); + } + } + + if(bonusNumber < 1 || bonusNumber > 45) { + throw new IllegalArgumentException("[ERROR] 로또 번호는 1부터 45 사이의 숫자여야 합니다."); + } + } +} diff --git a/src/main/java/lotto/ExceptionOfPrice.java b/src/main/java/lotto/ExceptionOfPrice.java new file mode 100644 index 00000000000..91f8e6d6d12 --- /dev/null +++ b/src/main/java/lotto/ExceptionOfPrice.java @@ -0,0 +1,10 @@ +package lotto; + +public class ExceptionOfPrice { + // 구입 금액이 1000원 단위가 아닌 경우 예외처리 + void ExceptionOfPrice(int lottoPrice) { + if (lottoPrice % 1000 != 0) { + throw new IllegalArgumentException("[ERROR] 구입 금액은 1,000원 단위여야 합니다."); + } + } +} diff --git a/src/main/java/lotto/InsertPrice.java b/src/main/java/lotto/InsertPrice.java new file mode 100644 index 00000000000..53ee3d0cbed --- /dev/null +++ b/src/main/java/lotto/InsertPrice.java @@ -0,0 +1,10 @@ +package lotto; + +public class InsertPrice { + //구입 금액 입력 + void insertPrice(int lottoPrice) { + ExceptionOfPrice exceptionOfPrice = new ExceptionOfPrice(); + exceptionOfPrice.ExceptionOfPrice(lottoPrice); + System.out.println(lottoPrice / 1000 + "개를 구매했습니다."); + } +} \ No newline at end of file diff --git a/src/main/java/lotto/Lotto.java b/src/main/java/lotto/Lotto.java index 519793d1f73..be49f0498ef 100644 --- a/src/main/java/lotto/Lotto.java +++ b/src/main/java/lotto/Lotto.java @@ -3,18 +3,16 @@ import java.util.List; public class Lotto { - private final List numbers; + private final List lottoNumbers; - public Lotto(List numbers) { - validate(numbers); - this.numbers = numbers; + public Lotto(List lottoNumbers) { + validate(lottoNumbers); + this.lottoNumbers = lottoNumbers; } - private void validate(List numbers) { - if (numbers.size() != 6) { + private void validate(List lottoNumbers) { + if (lottoNumbers.size() != 6) { throw new IllegalArgumentException(); } } - - // TODO: 추가 기능 구현 -} +} \ No newline at end of file diff --git a/src/main/java/lotto/RandomLotto.java b/src/main/java/lotto/RandomLotto.java new file mode 100644 index 00000000000..e754b301c2e --- /dev/null +++ b/src/main/java/lotto/RandomLotto.java @@ -0,0 +1,90 @@ +package lotto; + +import camp.nextstep.edu.missionutils.Console; +import camp.nextstep.edu.missionutils.Randoms; +import java.util.*; + +public class RandomLotto { + List> allLottoNumbers = new ArrayList<>(); //로또 리스트들을 저장하는 리스트 + String[] numbers = new String[6]; + int[] winningCount = new int[8]; + int bonusNumber; + + void getBonus() { + System.out.println("보너스 번호를 입력해 주세요."); + String bonus = Console.readLine(); + bonusNumber = Integer.parseInt(bonus); + } + + // 구입 금액만큼 로또 번호를 랜덤으로 출력 + void randomLotto(int lottoPrice) { + for (int i = 0; i < lottoPrice / 1000; i++) { + List lottoNumbers = Randoms.pickUniqueNumbersInRange(1, 45, 6); + Lotto lotto = new Lotto(lottoNumbers); //list 가 유효한지 검증 + Collections.sort(lottoNumbers); //오름차순 정렬 + allLottoNumbers.add(lottoNumbers); + System.out.println(lottoNumbers); + } + } + + //당첨 번호와 보너스 번호 입력받기 + void insertNumber() { + while(true) { + try { + System.out.println("당첨 번호를 입력해 주세요."); + String luckyNumber = Console.readLine(); + numbers = luckyNumber.split(","); + getBonus(); + + //1-45 사이 숫자 아닌 경우 예외처리 + ExceptionOfNumber exceptionOfNumber = new ExceptionOfNumber(); + exceptionOfNumber.exceptionOfNumber(numbers, bonusNumber); + //6개가 아닌 경우 예외처리 + ExceptionOfLength exceptionOfLength = new ExceptionOfLength(); + exceptionOfLength.exceptionOfLength(numbers); + + break; + } catch (IllegalArgumentException e) { + System.out.println(e.getMessage()); + } + } + } + + //당첨 통계 계산 + void winningStatistics(int lottoPrice) { + System.out.println("당첨 통계"); + System.out.println("---"); + + for (List lottoNumbers : allLottoNumbers) { + int count = 0; + for (String n : numbers) { + if (lottoNumbers.contains(Integer.parseInt(n))) { + count++; + } + } + if (count == 5 && lottoNumbers.contains(bonusNumber)) { + winningCount[7]++; + return; + } + winningCount[count]++; + } + } + + //당첨 내역 출력 + void printStatistics() { + for (ENUM.WinningEnum winning : ENUM.WinningEnum.values()) { + System.out.println(winning.getMessage() + winningCount[winning.getIndex()] + "개"); + } + } + + //수익률 출력 + void earningRate(int lottoPrice) { + int totalPrice = 0; + for (ENUM.WinningEnum winning : ENUM.WinningEnum.values()) { + totalPrice += (winning.getPrice() * winningCount[winning.getIndex()]); + } + double e = (double) totalPrice / lottoPrice; + double earningRate = (Math.round(e*1000)/10.0); + System.out.println("총 수익률은 " + earningRate + "%입니다."); + } +} From 09ffe0fcfb36038344362f18ace982080fe4109c Mon Sep 17 00:00:00 2001 From: Jihyun Date: Wed, 17 Jul 2024 21:24:05 +0900 Subject: [PATCH 2/2] refactoring --- src/main/java/lotto/Application.java | 32 +------- src/main/java/lotto/ENUM.java | 33 -------- src/main/java/lotto/Exception.java | 30 ++++++++ src/main/java/lotto/ExceptionOfLength.java | 9 --- src/main/java/lotto/ExceptionOfNumber.java | 17 ---- src/main/java/lotto/ExceptionOfPrice.java | 10 --- src/main/java/lotto/Insert.java | 59 ++++++++++++++ src/main/java/lotto/InsertPrice.java | 10 --- src/main/java/lotto/Print.java | 20 +++++ src/main/java/lotto/RandomLotto.java | 77 ++++--------------- src/main/java/lotto/RunApplication.java | 36 +++++++++ src/main/java/lotto/StatisticsCalculator.java | 25 ++++++ src/main/java/lotto/WinningEnum.java | 31 ++++++++ 13 files changed, 219 insertions(+), 170 deletions(-) delete mode 100644 src/main/java/lotto/ENUM.java create mode 100644 src/main/java/lotto/Exception.java delete mode 100644 src/main/java/lotto/ExceptionOfLength.java delete mode 100644 src/main/java/lotto/ExceptionOfNumber.java delete mode 100644 src/main/java/lotto/ExceptionOfPrice.java create mode 100644 src/main/java/lotto/Insert.java delete mode 100644 src/main/java/lotto/InsertPrice.java create mode 100644 src/main/java/lotto/Print.java create mode 100644 src/main/java/lotto/RunApplication.java create mode 100644 src/main/java/lotto/StatisticsCalculator.java create mode 100644 src/main/java/lotto/WinningEnum.java diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java index b9e65849879..e0655554300 100644 --- a/src/main/java/lotto/Application.java +++ b/src/main/java/lotto/Application.java @@ -4,35 +4,7 @@ public class Application { public static void main(String[] args) { - while(true) { - try { - System.out.println("구입금액을 입력해 주세요."); - String price = Console.readLine(); - int lottoPrice = Integer.parseInt(price); - - //로또 구입 금액 입력 - InsertPrice insertPrice = new InsertPrice(); - insertPrice.insertPrice(lottoPrice); - - //구입 금액만큼 랜덤 로또 번호 출력 - RandomLotto randomLotto = new RandomLotto(); - randomLotto.randomLotto(lottoPrice); - - //당첨 번호, 보너스 번호 입력 - randomLotto.insertNumber(); - - //당첨 통계 - randomLotto.winningStatistics(lottoPrice); - //당첨 내역 출력 - randomLotto.printStatistics(); - //수익률 출력 - randomLotto.earningRate(lottoPrice); - - break; - - } catch (IllegalArgumentException e) { - System.out.println(e.getMessage()); - } - } + RunApplication runApplication = new RunApplication(); + runApplication.run(); } } diff --git a/src/main/java/lotto/ENUM.java b/src/main/java/lotto/ENUM.java deleted file mode 100644 index fd2f3b991f0..00000000000 --- a/src/main/java/lotto/ENUM.java +++ /dev/null @@ -1,33 +0,0 @@ -package lotto; - -public class ENUM { - public enum WinningEnum { - three(3, 5000, "3개 일치 (5,000원) - "), - four(4, 50000, "4개 일치 (50,000원) - "), - five(5, 1500000, "5개 일치 (1,500,000원) - "), - bonus(7, 30000000, "5개 일치, 보너스 볼 일치 (30,000,000원) - "), - six(6, 2000000000, "6개 일치 (2,000,000,000원) - "); - - private final int index; - private final int price; - private final String message; - - WinningEnum(int index, int price, String message) { - this.index = index; - this.price = price; - this.message = message; - } - - public int getIndex() { - return index; - } - - public int getPrice() { - return price; - } - - public String getMessage() { - return message; - } - } -} diff --git a/src/main/java/lotto/Exception.java b/src/main/java/lotto/Exception.java new file mode 100644 index 00000000000..b2d0f873d09 --- /dev/null +++ b/src/main/java/lotto/Exception.java @@ -0,0 +1,30 @@ +package lotto; + +public class Exception { + void exceptionOfLength(String[] numbers){ + if(numbers.length != 6) { + throw new IllegalArgumentException("[ERROR] 로또 번호는 6개여야 합니다."); + } + } + + void exceptionOfNumber(String[] numbers) { + for (String n : numbers) { + int number = Integer.parseInt(n); + if (number < 1 || number > 45) { + throw new IllegalArgumentException("[ERROR] 로또 번호는 1부터 45 사이의 숫자여야 합니다."); + } + } + } + + void exceptionOfBonus(int bonusNumber){ + if(bonusNumber < 1 || bonusNumber > 45) { + throw new IllegalArgumentException("[ERROR] 로또 번호는 1부터 45 사이의 숫자여야 합니다."); + } + } + + void ExceptionOfPrice(int lottoPrice) { + if (lottoPrice % 1000 != 0) { + throw new IllegalArgumentException("[ERROR] 구입 금액은 1,000원 단위여야 합니다."); + } + } +} diff --git a/src/main/java/lotto/ExceptionOfLength.java b/src/main/java/lotto/ExceptionOfLength.java deleted file mode 100644 index 510eb810d5e..00000000000 --- a/src/main/java/lotto/ExceptionOfLength.java +++ /dev/null @@ -1,9 +0,0 @@ -package lotto; - -public class ExceptionOfLength { - void exceptionOfLength(String[] numbers){ - if(numbers.length != 6) { - throw new IllegalArgumentException("[ERROR] 로또 번호는 6개여야 합니다."); - } - } -} diff --git a/src/main/java/lotto/ExceptionOfNumber.java b/src/main/java/lotto/ExceptionOfNumber.java deleted file mode 100644 index 9d9b8b77e87..00000000000 --- a/src/main/java/lotto/ExceptionOfNumber.java +++ /dev/null @@ -1,17 +0,0 @@ -package lotto; - -public class ExceptionOfNumber { - // 당첨번호 6개가 1-45사이 숫자가 아닌 경우 예외처리 - void exceptionOfNumber(String[] numbers, int bonusNumber) { - for (String n : numbers) { - int number = Integer.parseInt(n); - if (number < 1 || number > 45) { - throw new IllegalArgumentException("[ERROR] 로또 번호는 1부터 45 사이의 숫자여야 합니다."); - } - } - - if(bonusNumber < 1 || bonusNumber > 45) { - throw new IllegalArgumentException("[ERROR] 로또 번호는 1부터 45 사이의 숫자여야 합니다."); - } - } -} diff --git a/src/main/java/lotto/ExceptionOfPrice.java b/src/main/java/lotto/ExceptionOfPrice.java deleted file mode 100644 index 91f8e6d6d12..00000000000 --- a/src/main/java/lotto/ExceptionOfPrice.java +++ /dev/null @@ -1,10 +0,0 @@ -package lotto; - -public class ExceptionOfPrice { - // 구입 금액이 1000원 단위가 아닌 경우 예외처리 - void ExceptionOfPrice(int lottoPrice) { - if (lottoPrice % 1000 != 0) { - throw new IllegalArgumentException("[ERROR] 구입 금액은 1,000원 단위여야 합니다."); - } - } -} diff --git a/src/main/java/lotto/Insert.java b/src/main/java/lotto/Insert.java new file mode 100644 index 00000000000..ff8b8081415 --- /dev/null +++ b/src/main/java/lotto/Insert.java @@ -0,0 +1,59 @@ +package lotto; + +import camp.nextstep.edu.missionutils.Console; + +public class Insert { + String[] numbers = new String[6]; + int bonusNumber; + + //구입 금액 입력 + void insertPrice(int lottoPrice) { + Exception exceptionOfPrice = new Exception(); + exceptionOfPrice.ExceptionOfPrice(lottoPrice); + System.out.println(lottoPrice / 1000 + "개를 구매했습니다."); + } + + String[] insertNumber() { + while(true) { + try { + System.out.println("당첨 번호를 입력해 주세요."); + String luckyNumber = Console.readLine(); + numbers = luckyNumber.split(","); + + //1-45 사이 숫자 아닌 경우 예외처리 + Exception exceptionOfNumber = new Exception(); + exceptionOfNumber.exceptionOfNumber(numbers); + //6개가 아닌 경우 예외처리 + Exception exceptionOfLength = new Exception(); + exceptionOfLength.exceptionOfLength(numbers); + + break; + } catch (IllegalArgumentException e) { + System.out.println(e.getMessage()); + } + } + return numbers; + } + + void insertBonus() { + while (true) { + try { + System.out.println("보너스 번호를 입력해 주세요."); + String bonus = Console.readLine(); + bonusNumber = Integer.parseInt(bonus); + + Exception exceptionOfBonus = new Exception(); + exceptionOfBonus.exceptionOfBonus(bonusNumber); + + break; + + } catch (IllegalArgumentException e) { + System.out.println(e.getMessage()); + } + } + } + + int getBonusNumber(){ + return bonusNumber; + } +} \ No newline at end of file diff --git a/src/main/java/lotto/InsertPrice.java b/src/main/java/lotto/InsertPrice.java deleted file mode 100644 index 53ee3d0cbed..00000000000 --- a/src/main/java/lotto/InsertPrice.java +++ /dev/null @@ -1,10 +0,0 @@ -package lotto; - -public class InsertPrice { - //구입 금액 입력 - void insertPrice(int lottoPrice) { - ExceptionOfPrice exceptionOfPrice = new ExceptionOfPrice(); - exceptionOfPrice.ExceptionOfPrice(lottoPrice); - System.out.println(lottoPrice / 1000 + "개를 구매했습니다."); - } -} \ No newline at end of file diff --git a/src/main/java/lotto/Print.java b/src/main/java/lotto/Print.java new file mode 100644 index 00000000000..90d5a8e5518 --- /dev/null +++ b/src/main/java/lotto/Print.java @@ -0,0 +1,20 @@ +package lotto; + +public class Print { + void printStatistics(int[] winningCount) { + for (WinningEnum winning : WinningEnum.values()) { + System.out.println(winning.getMessage() + winningCount[winning.getCount()] + "개"); + } + } + + //수익률 출력 + void printEarningRate(int[] winningCount, int lottoPrice) { + int totalPrice = 0; + for (WinningEnum winning : WinningEnum.values()) { + totalPrice += (winning.getPrice() * winningCount[winning.getCount()]); + } + double e = (double) totalPrice / lottoPrice; + double earningRate = (Math.round(e*1000)/10.0); + System.out.println("총 수익률은 " + earningRate + "%입니다."); + } +} diff --git a/src/main/java/lotto/RandomLotto.java b/src/main/java/lotto/RandomLotto.java index e754b301c2e..107ad27ec98 100644 --- a/src/main/java/lotto/RandomLotto.java +++ b/src/main/java/lotto/RandomLotto.java @@ -1,19 +1,17 @@ package lotto; -import camp.nextstep.edu.missionutils.Console; import camp.nextstep.edu.missionutils.Randoms; + import java.util.*; public class RandomLotto { List> allLottoNumbers = new ArrayList<>(); //로또 리스트들을 저장하는 리스트 String[] numbers = new String[6]; int[] winningCount = new int[8]; - int bonusNumber; + Insert insert = new Insert(); - void getBonus() { - System.out.println("보너스 번호를 입력해 주세요."); - String bonus = Console.readLine(); - bonusNumber = Integer.parseInt(bonus); + void setBonus() { + insert.insertBonus(); } // 구입 금액만큼 로또 번호를 랜덤으로 출력 @@ -27,64 +25,21 @@ void randomLotto(int lottoPrice) { } } - //당첨 번호와 보너스 번호 입력받기 void insertNumber() { - while(true) { - try { - System.out.println("당첨 번호를 입력해 주세요."); - String luckyNumber = Console.readLine(); - numbers = luckyNumber.split(","); - getBonus(); - - //1-45 사이 숫자 아닌 경우 예외처리 - ExceptionOfNumber exceptionOfNumber = new ExceptionOfNumber(); - exceptionOfNumber.exceptionOfNumber(numbers, bonusNumber); - //6개가 아닌 경우 예외처리 - ExceptionOfLength exceptionOfLength = new ExceptionOfLength(); - exceptionOfLength.exceptionOfLength(numbers); - - break; - } catch (IllegalArgumentException e) { - System.out.println(e.getMessage()); - } - } - } - - //당첨 통계 계산 - void winningStatistics(int lottoPrice) { - System.out.println("당첨 통계"); - System.out.println("---"); - - for (List lottoNumbers : allLottoNumbers) { - int count = 0; - for (String n : numbers) { - if (lottoNumbers.contains(Integer.parseInt(n))) { - count++; - } - } - if (count == 5 && lottoNumbers.contains(bonusNumber)) { - winningCount[7]++; - return; - } - winningCount[count]++; - } + numbers = insert.insertNumber(); } - //당첨 내역 출력 - void printStatistics() { - for (ENUM.WinningEnum winning : ENUM.WinningEnum.values()) { - System.out.println(winning.getMessage() + winningCount[winning.getIndex()] + "개"); - } + //당첨 통계 계산 + void produceStatistics() { + int bonusNumber = insert.getBonusNumber(); + StatisticsCalculator statisticsCalculator = new StatisticsCalculator(); + statisticsCalculator.produceStatistics(allLottoNumbers, numbers, winningCount, bonusNumber); } - - //수익률 출력 - void earningRate(int lottoPrice) { - int totalPrice = 0; - for (ENUM.WinningEnum winning : ENUM.WinningEnum.values()) { - totalPrice += (winning.getPrice() * winningCount[winning.getIndex()]); - } - double e = (double) totalPrice / lottoPrice; - double earningRate = (Math.round(e*1000)/10.0); - System.out.println("총 수익률은 " + earningRate + "%입니다."); + + //당첨 내역과 수익률 + void printAll(int lottoPrice) { + Print print = new Print(); + print.printStatistics(winningCount); + print.printEarningRate(winningCount, lottoPrice); } } diff --git a/src/main/java/lotto/RunApplication.java b/src/main/java/lotto/RunApplication.java new file mode 100644 index 00000000000..28bcce49b51 --- /dev/null +++ b/src/main/java/lotto/RunApplication.java @@ -0,0 +1,36 @@ +package lotto; + +import camp.nextstep.edu.missionutils.Console; +public class RunApplication { + void run() { + while(true) { + try { + System.out.println("구입금액을 입력해 주세요."); + String price = Console.readLine(); + int lottoPrice = Integer.parseInt(price); + + //로또 구입 금액 입력 + Insert insert = new Insert(); + insert.insertPrice(lottoPrice); + + //구입 금액만큼 랜덤 로또 번호 출력 + RandomLotto randomLotto = new RandomLotto(); + randomLotto.randomLotto(lottoPrice); + + //당첨 번호, 보너스 번호 입력 + randomLotto.insertNumber(); + randomLotto.setBonus(); + + //당첨 통계 + randomLotto.produceStatistics(); + //당첨 내역과 수익률 출력 + randomLotto.printAll(lottoPrice); + + break; + + } catch (IllegalArgumentException e) { + System.out.println(e.getMessage()); + } + } + } +} diff --git a/src/main/java/lotto/StatisticsCalculator.java b/src/main/java/lotto/StatisticsCalculator.java new file mode 100644 index 00000000000..837fed7344c --- /dev/null +++ b/src/main/java/lotto/StatisticsCalculator.java @@ -0,0 +1,25 @@ +package lotto; + +import java.util.List; + +public class StatisticsCalculator { + void produceStatistics(List> allLottoNumbers, String[] numbers, int[] winningCount, int bonusNumber) { + //int bonusNumber = insert.getBonusNumber(); + System.out.println("당첨 통계"); + System.out.println("---"); + + for (List lottoNumbers : allLottoNumbers) { + int count = 0; + for (String n : numbers) { + if (lottoNumbers.contains(Integer.parseInt(n))) { + count++; + } + } + if (count == 5 && lottoNumbers.contains(bonusNumber)) { + winningCount[7]++; + return; + } + winningCount[count]++; + } + } +} diff --git a/src/main/java/lotto/WinningEnum.java b/src/main/java/lotto/WinningEnum.java new file mode 100644 index 00000000000..ad2b7136ce0 --- /dev/null +++ b/src/main/java/lotto/WinningEnum.java @@ -0,0 +1,31 @@ +package lotto; + +public enum WinningEnum { + three(3, 5000, "3개 일치 (5,000원) - "), + four(4, 50000, "4개 일치 (50,000원) - "), + five(5, 1500000, "5개 일치 (1,500,000원) - "), + bonus(7, 30000000, "5개 일치, 보너스 볼 일치 (30,000,000원) - "), + six(6, 2000000000, "6개 일치 (2,000,000,000원) - "); + + private final int count; + private final int price; + private final String message; + + WinningEnum(int count, int price, String message) { + this.count = count; + this.price = price; + this.message = message; + } + + public int getCount() { + return count; + } + + public int getPrice() { + return price; + } + + public String getMessage() { + return message; + } +}