diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java index d190922ba44..e0655554300 100644 --- a/src/main/java/lotto/Application.java +++ b/src/main/java/lotto/Application.java @@ -1,7 +1,10 @@ package lotto; +import camp.nextstep.edu.missionutils.Console; + public class Application { public static void main(String[] args) { - // TODO: 프로그램 구현 + RunApplication runApplication = new RunApplication(); + runApplication.run(); } } 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/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/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/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 new file mode 100644 index 00000000000..107ad27ec98 --- /dev/null +++ b/src/main/java/lotto/RandomLotto.java @@ -0,0 +1,45 @@ +package lotto; + +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]; + Insert insert = new Insert(); + + void setBonus() { + insert.insertBonus(); + } + + // 구입 금액만큼 로또 번호를 랜덤으로 출력 + 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() { + numbers = insert.insertNumber(); + } + + //당첨 통계 계산 + void produceStatistics() { + int bonusNumber = insert.getBonusNumber(); + StatisticsCalculator statisticsCalculator = new StatisticsCalculator(); + statisticsCalculator.produceStatistics(allLottoNumbers, numbers, winningCount, bonusNumber); + } + + //당첨 내역과 수익률 + 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; + } +}