diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java index d190922..c9fae81 100644 --- a/src/main/java/lotto/Application.java +++ b/src/main/java/lotto/Application.java @@ -1,7 +1,96 @@ package lotto; +import java.util.*; public class Application { public static void main(String[] args) { - // TODO: 프로그램 구현 + + try{ + List purchasedLottos = new ArrayList<>(); + int[] result; + + //구입 금액 입력 받은 후 로또 개수 구하기 + int purchaseAmount = InputHelper.getIntInput("구입금액을 입력해 주세요."); + int numLotto = Lotto.getNumberOfLotto(purchaseAmount); + System.out.println(numLotto + "개를 구매했습니다."); + + //로또 생성 후 생성한 로또들 출력 + Lotto.generateLotto(purchasedLottos, numLotto); + Application.printPurchasedLottos(purchasedLottos); + + //당첨번호 입력받기 + List winningNumbers = new ArrayList<>(getWinningNumbers()); + + //보너스 번호 입력받기 + int bonus = getBonusNumber(); + Lotto.checkWinningNumberBonus(winningNumbers, bonus); + + + //로또 비교하기 + result = getResult(purchasedLottos, winningNumbers, bonus); + + //결과 출력하기 + Application.printWinningResults(result, purchaseAmount); + + } catch(IllegalArgumentException e){ + System.out.println(e.getMessage()); + } + + } + + + //구매한 로또 목록 출력하는 메소드 + private static void printPurchasedLottos(List purchased){ + for(int i = 0; i getWinningNumbers() { + List winningNumbers = new ArrayList<>(InputHelper.getListInput("당첨 번호를 입력해 주세요.")); + Collections.sort(winningNumbers); + if(!Lotto.checkWinningNumbersRange(winningNumbers)){ + throw new IllegalArgumentException("[ERROR]로또 번호 개수는 6개입니다."); + } + if (!Lotto.checkWinningNumbersCount(winningNumbers)) { + throw new IllegalArgumentException("[ERROR]번호의 범위는 1~45입니다."); + } + if(!Lotto.checkWinningNumbersDuplication(winningNumbers)){ + throw new IllegalArgumentException("[ERROR]로또 번호에 중복이 있습니다."); + } + return winningNumbers; + } + + //보너스 번호 입력 받는 메소드 + private static int getBonusNumber(){ + int bonusN = InputHelper.getIntInput("보너스 번호를 입력해 주세요."); + if(bonusN < 1 || bonusN > 45){ + throw new IllegalArgumentException("[ERROR]번호의 범위는 1~45입니다."); + } + return bonusN; + } + + //구매한 모든 로또에 대해 반복문을 돌면서 당첨 확인한 후 결과를 저장하는 메소드 + private static int[] getResult(List purchased, List winningNumbers, int bonus){ + int[] result = {0, 0, 0, 0, 0, 0}; + for(int i = 0; i getListInput(String prompt){ + List list = new ArrayList<>(); + System.out.println(prompt); + String line = Console.readLine(); + String[] str = line.split(","); + if(str.length == 6){ + for(int i = 0; i < str.length; i++){ + list.add(Integer.parseInt(str[i])); + } + return list; + } + throw new IllegalArgumentException("[ERROR]당첨숫자의 개수를 잘못 입력했습니다."); + } +} diff --git a/src/main/java/lotto/Lotto.java b/src/main/java/lotto/Lotto.java index 519793d..efa2439 100644 --- a/src/main/java/lotto/Lotto.java +++ b/src/main/java/lotto/Lotto.java @@ -1,9 +1,12 @@ package lotto; -import java.util.List; +import org.kokodak.Randoms; +import java.util.random.*; +import java.util.*; public class Lotto { private final List numbers; + public static final int LOTTO_PRICE = 1000; public Lotto(List numbers) { validate(numbers); @@ -15,6 +18,77 @@ private void validate(List numbers) { throw new IllegalArgumentException(); } } + + //로또 넘버 반환하는 메소드 + public List getLottoNum(){ + return numbers; + } + + //구매한 로또 개수 구하기 + public static int getNumberOfLotto(int price){ + if(price % Lotto.LOTTO_PRICE == 0) + return price / Lotto.LOTTO_PRICE; + throw new IllegalArgumentException("[ERROR]구입금액이 정수개로 나누어 떨어지지 않습니다!"); + } + + //로또 개수만큼 로또 생성하기 + public static void generateLotto(List lottoList, int num){ + for(int i = 0; i numberList = new ArrayList<>(Randoms.pickUniqueNumbersInRange(1, 45, 6)); + Collections.sort(numberList); + Lotto lotto = new Lotto(numberList); + lottoList.add(lotto); + } + } + + //입력받은 당첨번호의 범위 확인하기 + public static boolean checkWinningNumbersCount(List numbers){ + return numbers.size() == 6; + } + + public static boolean checkWinningNumbersRange(List numbers){ + for(int i = 0; i < numbers.size(); i++){ + if(numbers.get(i) < 1 || numbers.get(i) > 45){ + return false; + } + } + return true; + } + + public static boolean checkWinningNumbersDuplication(List numbers){ + Set set = new HashSet<>(numbers); + if(set.size() != numbers.size()) + return false; + return true; + } + + public static void checkWinningNumberBonus(List numbers, int bonus){ + Set s = new HashSet<>(numbers); + List list = new ArrayList<>(numbers); + s.add(bonus); + list.add(bonus); + if(s.size() != list.size()){ + throw new IllegalArgumentException("[ERROR]로또 번호에 중복이 있습니다."); + } + } + + + //로또 번호 비교하는 메소드 + public static int checkWinningResults(List winningNums, Lotto lottos, int bonusN){ + int numOfWinnings = 0; + for (int number : lottos.numbers) { + if (winningNums.contains(number)) numOfWinnings++; + } + if(numOfWinnings == 0 || numOfWinnings == 1 || numOfWinnings == 2) return 0; + if(numOfWinnings == 3) return 5; + if(numOfWinnings == 4) return 4; + if(numOfWinnings == 5){ + if (lottos.numbers.contains(bonusN)) + return 2; + return 3; + } + return 1; + } + - // TODO: 추가 기능 구현 }