From dd689276eb03007d0239e98729f27432e9650079 Mon Sep 17 00:00:00 2001 From: piamin04 Date: Sat, 15 Feb 2025 15:11:10 +0900 Subject: [PATCH 1/4] =?UTF-8?q?docs(README)=20:=20=EB=A6=AC=EB=93=9C?= =?UTF-8?q?=EB=AF=B8=20=EC=9E=91=EC=84=B1=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84=20=EC=84=A4=EB=AA=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) 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 From d2ca0b5863e9b9249e9d5661db7728e4f9ef4d49 Mon Sep 17 00:00:00 2001 From: piamin04 Date: Sun, 16 Feb 2025 22:40:05 +0900 Subject: [PATCH 2/4] =?UTF-8?q?feat(feature)=20:=201=EB=B2=88=20=EA=B8=B0?= =?UTF-8?q?=EB=8A=A5=EA=B5=AC=ED=98=84=20=EB=A1=9C=EB=98=90=20=EA=B5=AC?= =?UTF-8?q?=EC=9E=85=20=EA=B8=88=EC=95=A1=20=EC=9E=85=EB=A0=A5=20=EB=B0=8F?= =?UTF-8?q?=20=EC=98=88=EC=99=B8=20=EC=B2=98=EB=A6=AC=20=EA=B8=B0=EB=8A=A5?= =?UTF-8?q?=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/lotto/Application.java | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java index d190922..40abf8e 100644 --- a/src/main/java/lotto/Application.java +++ b/src/main/java/lotto/Application.java @@ -1,7 +1,30 @@ package lotto; +import static camp.nextstep.edu.missionutils.Console.readLine; + public class Application { + //로또 금액 예외 처리 및 로또 개수 구하기 + public static int getMoney() { + while (true) { + try { + String input = readLine(); + if (!input.matches("\\d+")) //숫자가 아닌 경우 예외처리 + throw new IllegalArgumentException("[ERROR] 숫자를 입력해주세요"); + int money = Integer.parseInt(input); + if (money % 1000 != 0) //1000 단위로 안나눠떨어질 경우 예외처리 + throw new IllegalArgumentException("[Error] 구입 금액은 1,000원 단위로 입력해주세요"); + return money; + } catch (IllegalArgumentException e) { + System.out.println(e.getMessage()); + } + } + } + public static void main(String[] args) { - // TODO: 프로그램 구현 + //1.로또 구입 금앱 입력받기 및 예외처리 + System.out.println("구입금액을 입력해 주세요."); + int lottoCount=getMoney()/1000; + System.out.println(lottoCount); + } } From 72845dc82e4b5b4b64461d544392162f30d38009 Mon Sep 17 00:00:00 2001 From: piamin04 Date: Mon, 17 Feb 2025 00:14:20 +0900 Subject: [PATCH 3/4] =?UTF-8?q?feat(feature)=20:=202,3=EB=B2=88=20?= =?UTF-8?q?=EA=B8=B0=EB=8A=A5=EA=B5=AC=ED=98=84=20=EB=A1=9C=EB=98=90=20?= =?UTF-8?q?=EA=B0=9C=EC=88=98=20=EA=B5=AC=ED=95=98=EA=B8=B0=20=EB=B0=8F=20?= =?UTF-8?q?=EB=A1=9C=EB=98=90=20=EB=B0=9C=ED=96=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/lotto/Application.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java index 40abf8e..fbd935f 100644 --- a/src/main/java/lotto/Application.java +++ b/src/main/java/lotto/Application.java @@ -1,5 +1,9 @@ package lotto; +import camp.nextstep.edu.missionutils.Randoms; + +import java.util.List; + import static camp.nextstep.edu.missionutils.Console.readLine; public class Application { @@ -20,10 +24,19 @@ public static int getMoney() { } } + //3. 로또 발행 + public static List createLotto() { + List lotto =Randoms.pickUniqueNumbersInRange(1,45,6); + return lotto; // 배열 반환 + } + public static void main(String[] args) { //1.로또 구입 금앱 입력받기 및 예외처리 System.out.println("구입금액을 입력해 주세요."); + //2. 로또 개수 구하기 int lottoCount=getMoney()/1000; + + //1번 메서드 확인용 출력 System.out.println(lottoCount); } From 54d6e5f0579c2ab66889fbb8a795f53133b7f8a0 Mon Sep 17 00:00:00 2001 From: piamin04 Date: Sun, 23 Feb 2025 19:07:38 +0900 Subject: [PATCH 4/4] =?UTF-8?q?feat(feature)=20:=20=EB=82=98=EB=A8=B8?= =?UTF-8?q?=EC=A7=80=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84=20=EB=B0=8F?= =?UTF-8?q?=20=ED=8C=8C=EC=9D=BC=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/lotto/Application.java | 38 +------------ .../lotto/controller/LottoController.java | 16 ++++++ src/main/java/lotto/model/Lotto.java | 18 +++++++ src/main/java/lotto/service/LottoService.java | 54 +++++++++++++++++++ src/main/java/lotto/view/InputView.java | 51 ++++++++++++++++++ src/main/java/lotto/view/OutputView.java | 25 +++++++++ 6 files changed, 166 insertions(+), 36 deletions(-) create mode 100644 src/main/java/lotto/controller/LottoController.java create mode 100644 src/main/java/lotto/model/Lotto.java create mode 100644 src/main/java/lotto/service/LottoService.java create mode 100644 src/main/java/lotto/view/InputView.java create mode 100644 src/main/java/lotto/view/OutputView.java diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java index fbd935f..357919c 100644 --- a/src/main/java/lotto/Application.java +++ b/src/main/java/lotto/Application.java @@ -1,43 +1,9 @@ package lotto; -import camp.nextstep.edu.missionutils.Randoms; - -import java.util.List; - -import static camp.nextstep.edu.missionutils.Console.readLine; +import lotto.controller.LottoController; public class Application { - //로또 금액 예외 처리 및 로또 개수 구하기 - public static int getMoney() { - while (true) { - try { - String input = readLine(); - if (!input.matches("\\d+")) //숫자가 아닌 경우 예외처리 - throw new IllegalArgumentException("[ERROR] 숫자를 입력해주세요"); - int money = Integer.parseInt(input); - if (money % 1000 != 0) //1000 단위로 안나눠떨어질 경우 예외처리 - throw new IllegalArgumentException("[Error] 구입 금액은 1,000원 단위로 입력해주세요"); - return money; - } catch (IllegalArgumentException e) { - System.out.println(e.getMessage()); - } - } - } - - //3. 로또 발행 - public static List createLotto() { - List lotto =Randoms.pickUniqueNumbersInRange(1,45,6); - return lotto; // 배열 반환 - } - public static void main(String[] args) { - //1.로또 구입 금앱 입력받기 및 예외처리 - System.out.println("구입금액을 입력해 주세요."); - //2. 로또 개수 구하기 - int lottoCount=getMoney()/1000; - - //1번 메서드 확인용 출력 - System.out.println(lottoCount); - + 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) + "개"); + } +} +