diff --git a/README.md b/README.md index 5fa2560..62b6030 100644 --- a/README.md +++ b/README.md @@ -1 +1,7 @@ # java-lotto-precourse +1. 로또 구입 금앱 입력받기 및 예외처리 +2. 로또 갯수 구하기 +3. 로또 발행하기 및 오름차순으로 정렬 +4. 당첨 번호 입력 받기 +5. 보너스 번호 입력 받기 +6. 당첨 내역 출력 \ No newline at end of file diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java index d190922..357919c 100644 --- a/src/main/java/lotto/Application.java +++ b/src/main/java/lotto/Application.java @@ -1,7 +1,9 @@ package lotto; +import lotto.controller.LottoController; + public class Application { public static void main(String[] args) { - // TODO: 프로그램 구현 + new LottoController().run(); } } diff --git a/src/main/java/lotto/controller/LottoController.java b/src/main/java/lotto/controller/LottoController.java new file mode 100644 index 0000000..b2870be --- /dev/null +++ b/src/main/java/lotto/controller/LottoController.java @@ -0,0 +1,16 @@ +package lotto.controller; + +import lotto.service.LottoService; +import lotto.view.InputView; + +public class LottoController { + public void run() { + int money = InputView.readMoney(); + int count = money / 1000; + + LottoService lottoService = new LottoService(); + lottoService.buyLottos(count); + lottoService.inputWinningNumbers(); + lottoService.checkResults(); + } +} \ No newline at end of file diff --git a/src/main/java/lotto/model/Lotto.java b/src/main/java/lotto/model/Lotto.java new file mode 100644 index 0000000..a5622b3 --- /dev/null +++ b/src/main/java/lotto/model/Lotto.java @@ -0,0 +1,18 @@ +package lotto.model; + +import camp.nextstep.edu.missionutils.Randoms; +import java.util.Collections; +import java.util.List; + +public class Lotto { + private final List numbers; + + public Lotto() { + this.numbers = Randoms.pickUniqueNumbersInRange(1, 45, 6); + Collections.sort(numbers); // 오름차순 정렬 + } + + public List getNumbers() { + return numbers; + } +} diff --git a/src/main/java/lotto/service/LottoService.java b/src/main/java/lotto/service/LottoService.java new file mode 100644 index 0000000..06596f6 --- /dev/null +++ b/src/main/java/lotto/service/LottoService.java @@ -0,0 +1,54 @@ +package lotto.service; + +import lotto.model.Lotto; +import lotto.view.InputView; +import lotto.view.OutputView; +import java.util.*; + +public class LottoService { + private List userLottos; // 사용자가 구매한 로또 리스트 + private List winningNumbers; // 당첨 번호 + private int bonusNumber; // 보너스 번호 + + public void buyLottos(int count) { + userLottos = new ArrayList<>(); + for (int i = 0; i < count; i++) { + userLottos.add(new Lotto()); + } + OutputView.printLottos(userLottos); + } + + public void inputWinningNumbers() { + winningNumbers = InputView.getWinningNumbers(); + bonusNumber = InputView.getBonusNumber(winningNumbers); + } + + public void checkResults() { + Map results = new HashMap<>(); + results.put(3, 0); + results.put(4, 0); + results.put(5, 0); + results.put(6, 0); + results.put(7, 0); // 5개 + 보너스 + + for (Lotto lotto : userLottos) { + int matchCount = countMatches(lotto.getNumbers()); + boolean hasBonus = lotto.getNumbers().contains(bonusNumber); + + if (matchCount == 5 && hasBonus) results.put(7, results.get(7) + 1); + else if (results.containsKey(matchCount)) results.put(matchCount, results.get(matchCount) + 1); + } + + OutputView.printResults(results); + } + + private int countMatches(List userNumbers) { + int count = 0; + for (int num : userNumbers) { + if (winningNumbers.contains(num)) { + count++; + } + } + return count; + } +} diff --git a/src/main/java/lotto/view/InputView.java b/src/main/java/lotto/view/InputView.java new file mode 100644 index 0000000..3de9213 --- /dev/null +++ b/src/main/java/lotto/view/InputView.java @@ -0,0 +1,51 @@ +package lotto.view; + +import static camp.nextstep.edu.missionutils.Console.readLine; +import java.util.*; + +public class InputView { + public static int readMoney() { + System.out.println("구입금액을 입력해 주세요."); + return validateMoney(readLine()); + } + + public static List getWinningNumbers() { + System.out.println("당첨 번호를 입력해 주세요."); + return validateNumbers(readLine().split(",")); + } + + public static int getBonusNumber(List winningNumbers) { + System.out.println("보너스 번호를 입력해 주세요."); + while (true) { + try { + int bonus = Integer.parseInt(readLine()); + if (winningNumbers.contains(bonus)) throw new IllegalArgumentException("보너스 번호는 당첨 번호와 중복될 수 없습니다."); + return bonus; + } catch (Exception e) { + System.out.println("[ERROR] " + e.getMessage()); + } + } + } + + private static int validateMoney(String input) { + if (!input.matches("\\d+") || Integer.parseInt(input) % 1000 != 0) { + throw new IllegalArgumentException("구입 금액은 1,000원 단위여야 합니다."); + } + return Integer.parseInt(input); + } + + private static List validateNumbers(String[] input) { + if (input.length != 6) throw new IllegalArgumentException("당첨 번호는 6개여야 합니다."); + + List numbers = new ArrayList<>(); + for (String num : input) { + int number = Integer.parseInt(num.trim()); + if (number < 1 || number > 45 || numbers.contains(number)) { + throw new IllegalArgumentException("유효하지 않은 로또 번호입니다."); + } + numbers.add(number); + } + return numbers; + } +} + diff --git a/src/main/java/lotto/view/OutputView.java b/src/main/java/lotto/view/OutputView.java new file mode 100644 index 0000000..a4b680a --- /dev/null +++ b/src/main/java/lotto/view/OutputView.java @@ -0,0 +1,25 @@ +package lotto.view; + +import lotto.model.Lotto; +import java.util.List; +import java.util.Map; + +public class OutputView { + public static void printLottos(List lottos) { + System.out.println(lottos.size() + "개를 구매했습니다."); + for (Lotto lotto : lottos) { + System.out.println(lotto.getNumbers()); + } + } + + public static void printResults(Map results) { + System.out.println("\n당첨 통계"); + System.out.println("---"); + System.out.println("3개 일치 (5,000원) - " + results.get(3) + "개"); + System.out.println("4개 일치 (50,000원) - " + results.get(4) + "개"); + System.out.println("5개 일치 (1,500,000원) - " + results.get(5) + "개"); + System.out.println("5개 + 보너스 볼 일치 (30,000,000원) - " + results.get(7) + "개"); + System.out.println("6개 일치 (2,000,000,000원) - " + results.get(6) + "개"); + } +} +