From 8ba883a97d5e18ca7d0e28e4c94c48f1c3ceeba8 Mon Sep 17 00:00:00 2001 From: nonactress Date: Thu, 25 Sep 2025 01:14:12 +0900 Subject: [PATCH 01/42] feat(view) : making view --- src/main/java/View/InputView.java | 21 +++++++++++++++++++++ src/main/java/View/OutView.java | 18 ++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 src/main/java/View/InputView.java create mode 100644 src/main/java/View/OutView.java diff --git a/src/main/java/View/InputView.java b/src/main/java/View/InputView.java new file mode 100644 index 000000000..a2311161e --- /dev/null +++ b/src/main/java/View/InputView.java @@ -0,0 +1,21 @@ +package View; + +import Model.Jackpot; +import java.util.Scanner; + +public class InputView { + Scanner scanner = new Scanner(System.in); + + public int getMoney() + { + System.out.println("구입금액을 입력해 주세요."); + return scanner.nextInt(); + } + + public String[] getJackpot() + { + scanner.nextLine(); + System.out.println("정답 로또번호를 입력해주세요( ','로 구분해서 입력)"); + return scanner.nextLine().split(","); + } +} diff --git a/src/main/java/View/OutView.java b/src/main/java/View/OutView.java new file mode 100644 index 000000000..3e028f99a --- /dev/null +++ b/src/main/java/View/OutView.java @@ -0,0 +1,18 @@ +package View; + +import Controller.LottoController; +import Model.Lottos; + +public class OutView { + + public void printLottoMatcher(int[] matchCounts,double rate) + { + System.out.println("당첨 통계"); + System.out.println("------"); + System.out.println("3개 일치(5,000원): " + matchCounts[3] + "개"); + System.out.println("4개 일치(50,000원): " + matchCounts[4] + "개"); + System.out.println("5개 일치(1,500,000원): " + matchCounts[5] + "개"); + System.out.println("6개 일치(2,000,000,000원): " + matchCounts[6] + "개"); + System.out.printf("총 수익률은 %.2f입니다.", rate); + } +} From 8ae5422ecdbe0b4b4c47235bb57c9f299d4a7332 Mon Sep 17 00:00:00 2001 From: nonactress Date: Thu, 25 Sep 2025 01:14:37 +0900 Subject: [PATCH 02/42] feat(model) : making Model --- src/main/java/Model/Jackpot.java | 27 +++++++++++++++ src/main/java/Model/Lotto.java | 31 ++++++++++++++++++ src/main/java/Model/LottoMatcher.java | 47 +++++++++++++++++++++++++++ src/main/java/Model/Lottos.java | 32 ++++++++++++++++++ 4 files changed, 137 insertions(+) create mode 100644 src/main/java/Model/Jackpot.java create mode 100644 src/main/java/Model/Lotto.java create mode 100644 src/main/java/Model/LottoMatcher.java create mode 100644 src/main/java/Model/Lottos.java diff --git a/src/main/java/Model/Jackpot.java b/src/main/java/Model/Jackpot.java new file mode 100644 index 000000000..0d36ec54b --- /dev/null +++ b/src/main/java/Model/Jackpot.java @@ -0,0 +1,27 @@ +package Model; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class Jackpot { + private List jackpot; + + public Jackpot(String[] splits) { + this.jackpot = new ArrayList<>(); // 리스트 초기화 + + for (int i = 0; i < 6; i++) { + jackpot.add(Integer.parseInt(splits[i])); + } + Collections.sort(this.jackpot); + } + + public List getJackpot() { + return jackpot; + } + + @Override + public String toString() { + return jackpot.toString(); + } +} diff --git a/src/main/java/Model/Lotto.java b/src/main/java/Model/Lotto.java new file mode 100644 index 000000000..742033eb3 --- /dev/null +++ b/src/main/java/Model/Lotto.java @@ -0,0 +1,31 @@ +package Model; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class Lotto { + private List lotto; + + public Lotto() { + List numbers = new ArrayList<>(); + + for(int i = 1; i <= 45; i++) { + numbers.add(i); + } + + Collections.shuffle(numbers); + this.lotto = new ArrayList<>(numbers.subList(0, 6)); + + Collections.sort(this.lotto); // 뽑은 6개의 숫자를 오름차순으로 정렬 + } + + public List getLotto() { + return lotto; + } + + @Override + public String toString() { + return lotto.toString(); + } +} diff --git a/src/main/java/Model/LottoMatcher.java b/src/main/java/Model/LottoMatcher.java new file mode 100644 index 000000000..1660a418c --- /dev/null +++ b/src/main/java/Model/LottoMatcher.java @@ -0,0 +1,47 @@ +package Model; + +import java.util.List; + +public class LottoMatcher { + private final int[] matchCounts; + + public LottoMatcher(Lottos lottos, Jackpot jackpot) { + this.matchCounts = new int[7]; + + for (Lotto lotto : lottos.getLottoList()) { + int matches = countMatches(jackpot.getJackpot(), lotto.getLotto()); + matchCounts[matches]++; + } + } + + private int countMatches(List jackpotNumbers, List lottoNumbers) { + int count = 0; + for (int number : lottoNumbers) { + count += getMatchValue(jackpotNumbers, number); + } + return count; + } + + private int getMatchValue(List jackpotNumbers, int number) { + if (jackpotNumbers.contains(number)) { + return 1; + } + return 0; + } + + public int[] getMatchCounts() { + return matchCounts; + } + + public double calculateTotalEarnings() { + return matchCounts[3] * 5000 + + matchCounts[4] * 50000 + + matchCounts[5] * 1500000 + + matchCounts[6] * 2000000000; + } + + public double getRate(int money) + { + return (double)calculateTotalEarnings()/money; + } +} diff --git a/src/main/java/Model/Lottos.java b/src/main/java/Model/Lottos.java new file mode 100644 index 000000000..b6db81bc4 --- /dev/null +++ b/src/main/java/Model/Lottos.java @@ -0,0 +1,32 @@ +package Model; + +import java.util.ArrayList; +import java.util.List; + +public class Lottos { + private List lottoList; + + + public Lottos(int money) { + int COST_PER_TICKET = 1000; + int numberOfLottos=money/COST_PER_TICKET; + this.lottoList = new ArrayList<>(); + + for (int i = 0; i < numberOfLottos; i++) { + lottoList.add(new Lotto()); // new Lotto()가 호출될 때마다 새로운 로또 번호 6개가 생성됩니다. + } + } + + public void printLottos() { + + for (Lotto lotto : lottoList) { + System.out.println(lotto); + } + } + + public List getLottoList() { + return lottoList; + } + + +} From 8dd8277ecc5193f29ada497e14cafeb20072dd72 Mon Sep 17 00:00:00 2001 From: nonactress Date: Thu, 25 Sep 2025 01:14:54 +0900 Subject: [PATCH 03/42] feat(controller) : making controller --- src/main/java/Controller/LottoController.java | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 src/main/java/Controller/LottoController.java diff --git a/src/main/java/Controller/LottoController.java b/src/main/java/Controller/LottoController.java new file mode 100644 index 000000000..d375300f0 --- /dev/null +++ b/src/main/java/Controller/LottoController.java @@ -0,0 +1,25 @@ +package Controller; + +import Model.Jackpot; +import Model.LottoMatcher; +import Model.Lottos; +import View.InputView; +import View.OutView; + +public class LottoController { + public void startLotto() + { + InputView inputView = new InputView(); + int money = inputView.getMoney(); + + Lottos lottos = new Lottos(money); + lottos.printLottos(); + + Jackpot jackpot = new Jackpot(inputView.getJackpot()); + + LottoMatcher lottoMatcher = new LottoMatcher(lottos,jackpot); + + OutView outView = new OutView(); + outView.printLottoMatcher(lottoMatcher.getMatchCounts(), lottoMatcher.getRate(money)); + } +} From 4c65cb710652788ae59af2cccecf4d905864b096 Mon Sep 17 00:00:00 2001 From: nonactress Date: Thu, 25 Sep 2025 01:15:16 +0900 Subject: [PATCH 04/42] feat(application) : making Application --- src/main/java/Application.java | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 src/main/java/Application.java diff --git a/src/main/java/Application.java b/src/main/java/Application.java new file mode 100644 index 000000000..23aa0f0a7 --- /dev/null +++ b/src/main/java/Application.java @@ -0,0 +1,8 @@ +import Controller.LottoController; + +public class Application { + public static void main(String[] args) { + LottoController lottoController = new LottoController(); + lottoController.startLotto(); + } +} From 128dee666c19b54679c7a405538f4a5bdfd61b6d Mon Sep 17 00:00:00 2001 From: nonactress Date: Thu, 25 Sep 2025 09:27:27 +0900 Subject: [PATCH 05/42] refactor(view) : 'OutView' --- src/main/java/View/OutView.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/main/java/View/OutView.java b/src/main/java/View/OutView.java index 3e028f99a..35fc2db4d 100644 --- a/src/main/java/View/OutView.java +++ b/src/main/java/View/OutView.java @@ -1,6 +1,7 @@ package View; import Controller.LottoController; +import Model.Lotto; import Model.Lottos; public class OutView { @@ -15,4 +16,11 @@ public void printLottoMatcher(int[] matchCounts,double rate) System.out.println("6개 일치(2,000,000,000원): " + matchCounts[6] + "개"); System.out.printf("총 수익률은 %.2f입니다.", rate); } + public void printLottos(Lottos lottos) + { + for(Lotto lotto : lottos.getLottoList()) + { + System.out.println(lotto); + } + } } From daa8759b534418d2778bfcf62f2ce9c40e766a14 Mon Sep 17 00:00:00 2001 From: nonactress Date: Thu, 25 Sep 2025 09:28:15 +0900 Subject: [PATCH 06/42] refactor(model,controller) : adapting 'OutView' --- src/main/java/Controller/LottoController.java | 6 ++++-- src/main/java/Model/Lottos.java | 10 ---------- 2 files changed, 4 insertions(+), 12 deletions(-) diff --git a/src/main/java/Controller/LottoController.java b/src/main/java/Controller/LottoController.java index d375300f0..7728d622a 100644 --- a/src/main/java/Controller/LottoController.java +++ b/src/main/java/Controller/LottoController.java @@ -13,13 +13,15 @@ public void startLotto() int money = inputView.getMoney(); Lottos lottos = new Lottos(money); - lottos.printLottos(); + + OutView outView = new OutView(); + + outView.printLottos(lottos); Jackpot jackpot = new Jackpot(inputView.getJackpot()); LottoMatcher lottoMatcher = new LottoMatcher(lottos,jackpot); - OutView outView = new OutView(); outView.printLottoMatcher(lottoMatcher.getMatchCounts(), lottoMatcher.getRate(money)); } } diff --git a/src/main/java/Model/Lottos.java b/src/main/java/Model/Lottos.java index b6db81bc4..9a0f4c042 100644 --- a/src/main/java/Model/Lottos.java +++ b/src/main/java/Model/Lottos.java @@ -6,7 +6,6 @@ public class Lottos { private List lottoList; - public Lottos(int money) { int COST_PER_TICKET = 1000; int numberOfLottos=money/COST_PER_TICKET; @@ -17,16 +16,7 @@ public Lottos(int money) { } } - public void printLottos() { - - for (Lotto lotto : lottoList) { - System.out.println(lotto); - } - } - public List getLottoList() { return lottoList; } - - } From 28b7703f2fb8642ef40d06822de965b6ed0d1f63 Mon Sep 17 00:00:00 2001 From: nonactress Date: Thu, 25 Sep 2025 09:33:25 +0900 Subject: [PATCH 07/42] refactor(All) : clean up code line --- src/main/java/Controller/LottoController.java | 5 ++--- src/main/java/Model/Lotto.java | 2 +- src/main/java/Model/LottoMatcher.java | 6 +++--- src/main/java/Model/Lottos.java | 2 +- src/main/java/View/InputView.java | 7 +++---- src/main/java/View/OutView.java | 10 ++++------ 6 files changed, 14 insertions(+), 18 deletions(-) diff --git a/src/main/java/Controller/LottoController.java b/src/main/java/Controller/LottoController.java index 7728d622a..3e39de3be 100644 --- a/src/main/java/Controller/LottoController.java +++ b/src/main/java/Controller/LottoController.java @@ -7,8 +7,7 @@ import View.OutView; public class LottoController { - public void startLotto() - { + public void startLotto() { InputView inputView = new InputView(); int money = inputView.getMoney(); @@ -20,7 +19,7 @@ public void startLotto() Jackpot jackpot = new Jackpot(inputView.getJackpot()); - LottoMatcher lottoMatcher = new LottoMatcher(lottos,jackpot); + LottoMatcher lottoMatcher = new LottoMatcher(lottos, jackpot); outView.printLottoMatcher(lottoMatcher.getMatchCounts(), lottoMatcher.getRate(money)); } diff --git a/src/main/java/Model/Lotto.java b/src/main/java/Model/Lotto.java index 742033eb3..ef526b930 100644 --- a/src/main/java/Model/Lotto.java +++ b/src/main/java/Model/Lotto.java @@ -10,7 +10,7 @@ public class Lotto { public Lotto() { List numbers = new ArrayList<>(); - for(int i = 1; i <= 45; i++) { + for (int i = 1; i <= 45; i++) { numbers.add(i); } diff --git a/src/main/java/Model/LottoMatcher.java b/src/main/java/Model/LottoMatcher.java index 1660a418c..fdb68574f 100644 --- a/src/main/java/Model/LottoMatcher.java +++ b/src/main/java/Model/LottoMatcher.java @@ -16,6 +16,7 @@ public LottoMatcher(Lottos lottos, Jackpot jackpot) { private int countMatches(List jackpotNumbers, List lottoNumbers) { int count = 0; + for (int number : lottoNumbers) { count += getMatchValue(jackpotNumbers, number); } @@ -40,8 +41,7 @@ public double calculateTotalEarnings() { + matchCounts[6] * 2000000000; } - public double getRate(int money) - { - return (double)calculateTotalEarnings()/money; + public double getRate(int money) { + return (double) calculateTotalEarnings() / money; } } diff --git a/src/main/java/Model/Lottos.java b/src/main/java/Model/Lottos.java index 9a0f4c042..5a903c840 100644 --- a/src/main/java/Model/Lottos.java +++ b/src/main/java/Model/Lottos.java @@ -8,7 +8,7 @@ public class Lottos { public Lottos(int money) { int COST_PER_TICKET = 1000; - int numberOfLottos=money/COST_PER_TICKET; + int numberOfLottos = money / COST_PER_TICKET; this.lottoList = new ArrayList<>(); for (int i = 0; i < numberOfLottos; i++) { diff --git a/src/main/java/View/InputView.java b/src/main/java/View/InputView.java index a2311161e..2168141ca 100644 --- a/src/main/java/View/InputView.java +++ b/src/main/java/View/InputView.java @@ -1,19 +1,18 @@ package View; import Model.Jackpot; + import java.util.Scanner; public class InputView { Scanner scanner = new Scanner(System.in); - public int getMoney() - { + public int getMoney() { System.out.println("구입금액을 입력해 주세요."); return scanner.nextInt(); } - public String[] getJackpot() - { + public String[] getJackpot() { scanner.nextLine(); System.out.println("정답 로또번호를 입력해주세요( ','로 구분해서 입력)"); return scanner.nextLine().split(","); diff --git a/src/main/java/View/OutView.java b/src/main/java/View/OutView.java index 35fc2db4d..e9adc017c 100644 --- a/src/main/java/View/OutView.java +++ b/src/main/java/View/OutView.java @@ -6,8 +6,7 @@ public class OutView { - public void printLottoMatcher(int[] matchCounts,double rate) - { + public void printLottoMatcher(int[] matchCounts, double rate) { System.out.println("당첨 통계"); System.out.println("------"); System.out.println("3개 일치(5,000원): " + matchCounts[3] + "개"); @@ -16,10 +15,9 @@ public void printLottoMatcher(int[] matchCounts,double rate) System.out.println("6개 일치(2,000,000,000원): " + matchCounts[6] + "개"); System.out.printf("총 수익률은 %.2f입니다.", rate); } - public void printLottos(Lottos lottos) - { - for(Lotto lotto : lottos.getLottoList()) - { + + public void printLottos(Lottos lottos) { + for (Lotto lotto : lottos.getLottoList()) { System.out.println(lotto); } } From f63efd334fea4d608b0f752b3873b1189d062788 Mon Sep 17 00:00:00 2001 From: nonactress Date: Sun, 28 Sep 2025 12:36:11 +0900 Subject: [PATCH 08/42] feat : READ.ME --- README.md | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 000000000..66db098d6 --- /dev/null +++ b/README.md @@ -0,0 +1,54 @@ +# 로또 + +## 📖 개요 + +사용자는 구입 금액을 입력하고, 그에 따라 로또가 발급됩니다. 마지막으로 당첨 번호를 입력하여 당첨 통계와 수익률을 확인할 수 있습니다. + +## 📂 프로젝트 구조 + +``` +. +├── src +│ ├── main +│ │ ├── java +│ │ │ ├── Application.java # 애플리케이션 시작점 +│ │ │ ├── Controller +│ │ │ │ └── LottoController.java # 게임 로직 제어 +│ │ │ ├── Model +│ │ │ │ ├── Jackpot.java # 당첨 번호 +│ │ │ │ ├── Lotto.java # 로또 1장 +│ │ │ │ ├── Lottos.java # 발급된 전체 로또 +│ │ │ │ └── LottoMatcher.java # 로또 당첨 확인 +│ │ │ └── View +│ │ │ ├── InputView.java # 사용자 입력 +│ │ │ └── OutView.java # 결과 출력 +│ │ └── resources +│ └── test +│ └── java +└── build.gradle +``` + +## ✨ 주요 기능 및 동작 원리 + +1. **애플리케이션 시작 (`Application.java`)** + - `main` 메소드에서 `LottoController`를 생성하고 `startLotto()` 메소드를 호출하여 로또 게임을 시작합니다. + +2. **입력 (`InputView.java`)** + - 사용자로부터 로또 구입 금액을 입력받습니다. + - 사용자로부터 당첨 번호를 쉼표(`,`)로 구분하여 입력받습니다. + +3. **로직 처리 (`Controller` & `Model`)** + - **`LottoController`**: + - `InputView`를 통해 구입 금액을 받아, 금액만큼의 `Lotto` 객체를 생성합니다. (`Lottos`) + - `InputView`를 통해 당첨 번호를 받아 `Jackpot` 객체를 생성합니다. + - `LottoMatcher`를 통해 발급된 로또와 당첨 번호를 비교하여 당첨 통계를 계산합니다. + - 계산된 결과를 `OutView`를 통해 출력합니다. + - **`Lotto`**: 1부터 45 사이의 중복되지 않는 6개의 숫자로 이루어진 로또 한 장을 의미합니다. + - **`Lottos`**: 사용자가 구입한 금액만큼의 `Lotto` 객체들을 리스트로 관리합니다. + - **`Jackpot`**: 당첨 번호 6개를 저장합니다. + - **`LottoMatcher`**: `Lottos`와 `Jackpot`을 비교하여, 일치하는 번호의 개수를 세고, 당첨 통계와 수익률을 계산합니다. + +4. **출력 (`OutView.java`)** + - 발급된 로또 번호들을 출력합니다. + - 최종 당첨 통계 (3개 일치 ~ 6개 일치)와 총수익률을 계산하여 출력합니다. + From 52ef26b13396cb29518452b01a81838c351dd640 Mon Sep 17 00:00:00 2001 From: nonactress Date: Sun, 28 Sep 2025 20:26:41 +0900 Subject: [PATCH 09/42] refacting : clean line --- src/main/java/Model/LottoMatcher.java | 1 - src/main/java/Model/Lottos.java | 1 + src/main/java/View/InputView.java | 2 -- src/main/java/View/OutView.java | 2 +- 4 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/main/java/Model/LottoMatcher.java b/src/main/java/Model/LottoMatcher.java index fdb68574f..7f71fddf8 100644 --- a/src/main/java/Model/LottoMatcher.java +++ b/src/main/java/Model/LottoMatcher.java @@ -16,7 +16,6 @@ public LottoMatcher(Lottos lottos, Jackpot jackpot) { private int countMatches(List jackpotNumbers, List lottoNumbers) { int count = 0; - for (int number : lottoNumbers) { count += getMatchValue(jackpotNumbers, number); } diff --git a/src/main/java/Model/Lottos.java b/src/main/java/Model/Lottos.java index 5a903c840..463f1d193 100644 --- a/src/main/java/Model/Lottos.java +++ b/src/main/java/Model/Lottos.java @@ -1,6 +1,7 @@ package Model; import java.util.ArrayList; +import java.util.Iterator; import java.util.List; public class Lottos { diff --git a/src/main/java/View/InputView.java b/src/main/java/View/InputView.java index 2168141ca..084e81e2e 100644 --- a/src/main/java/View/InputView.java +++ b/src/main/java/View/InputView.java @@ -1,7 +1,5 @@ package View; -import Model.Jackpot; - import java.util.Scanner; public class InputView { diff --git a/src/main/java/View/OutView.java b/src/main/java/View/OutView.java index e9adc017c..d0476cd08 100644 --- a/src/main/java/View/OutView.java +++ b/src/main/java/View/OutView.java @@ -1,6 +1,5 @@ package View; -import Controller.LottoController; import Model.Lotto; import Model.Lottos; @@ -16,6 +15,7 @@ public void printLottoMatcher(int[] matchCounts, double rate) { System.out.printf("총 수익률은 %.2f입니다.", rate); } + public void printLottos(Lottos lottos) { for (Lotto lotto : lottos.getLottoList()) { System.out.println(lotto); From af6c2f512d85446e2678f9c507b0501d4581acdb Mon Sep 17 00:00:00 2001 From: nonactress Date: Sun, 28 Sep 2025 20:27:16 +0900 Subject: [PATCH 10/42] refacting : renaming "input" , "output" --- src/main/java/Controller/LottoController.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/main/java/Controller/LottoController.java b/src/main/java/Controller/LottoController.java index 3e39de3be..26d9e8209 100644 --- a/src/main/java/Controller/LottoController.java +++ b/src/main/java/Controller/LottoController.java @@ -8,19 +8,19 @@ public class LottoController { public void startLotto() { - InputView inputView = new InputView(); - int money = inputView.getMoney(); + InputView input = new InputView(); + int money = input.getMoney(); Lottos lottos = new Lottos(money); - OutView outView = new OutView(); + OutView output = new OutView(); - outView.printLottos(lottos); + output.printLottos(lottos); - Jackpot jackpot = new Jackpot(inputView.getJackpot()); + Jackpot jackpot = new Jackpot(input.getJackpot()); LottoMatcher lottoMatcher = new LottoMatcher(lottos, jackpot); - outView.printLottoMatcher(lottoMatcher.getMatchCounts(), lottoMatcher.getRate(money)); + output.printLottoMatcher(lottoMatcher.getMatchCounts(), lottoMatcher.getRate(money)); } } From dfb1e774d72b1a79ddfb6191fec7279bf5b3cb8c Mon Sep 17 00:00:00 2001 From: nonactress Date: Sun, 28 Sep 2025 20:43:55 +0900 Subject: [PATCH 11/42] feat : enum "PrizeMoney" --- src/main/java/Model/PrizeMoney.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 src/main/java/Model/PrizeMoney.java diff --git a/src/main/java/Model/PrizeMoney.java b/src/main/java/Model/PrizeMoney.java new file mode 100644 index 000000000..ddf63d934 --- /dev/null +++ b/src/main/java/Model/PrizeMoney.java @@ -0,0 +1,18 @@ +package Model; + +public enum PrizeMoney { + THREE(5000), + FOUR(50000), + FIVE(1500000), + SIX(2000000000); // 상수 목록 끝에 세미콜론 + + private final int money; + + private PrizeMoney(int money) { + this.money = money; + } + + public int getMoney() { + return money; + } +} From bcdad0aaec94739d19464d9e17ccb1ee4d938a45 Mon Sep 17 00:00:00 2001 From: nonactress Date: Sun, 28 Sep 2025 20:44:14 +0900 Subject: [PATCH 12/42] refactoring : using enum "PrizeMoney" --- src/main/java/Model/LottoMatcher.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/Model/LottoMatcher.java b/src/main/java/Model/LottoMatcher.java index 7f71fddf8..ff26ad626 100644 --- a/src/main/java/Model/LottoMatcher.java +++ b/src/main/java/Model/LottoMatcher.java @@ -34,10 +34,10 @@ public int[] getMatchCounts() { } public double calculateTotalEarnings() { - return matchCounts[3] * 5000 - + matchCounts[4] * 50000 - + matchCounts[5] * 1500000 - + matchCounts[6] * 2000000000; + return matchCounts[3] * PrizeMoney.THREE.getMoney() + + matchCounts[4] * PrizeMoney.FOUR.getMoney() + + matchCounts[5] * PrizeMoney.FIVE.getMoney() + + matchCounts[6] * PrizeMoney.SIX.getMoney(); } public double getRate(int money) { From ab6a0ee022792d2175200ae53acf7e1425ee0d1b Mon Sep 17 00:00:00 2001 From: nonactress Date: Sun, 28 Sep 2025 20:55:41 +0900 Subject: [PATCH 13/42] refactoring(inputview) : exception handling --- src/main/java/View/InputView.java | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/src/main/java/View/InputView.java b/src/main/java/View/InputView.java index 084e81e2e..32067ef7e 100644 --- a/src/main/java/View/InputView.java +++ b/src/main/java/View/InputView.java @@ -1,13 +1,41 @@ package View; import java.util.Scanner; +import java.util.InputMismatchException; public class InputView { Scanner scanner = new Scanner(System.in); public int getMoney() { System.out.println("구입금액을 입력해 주세요."); - return scanner.nextInt(); + while(true) + { + try + { + int money = scanner.nextInt(); + validate(money); + return money; + } + catch (InputMismatchException e) + { + System.out.println("금액은 숫자로 입력해주세요!"); + } + catch (IllegalArgumentException e) + { + System.out.println(e.getMessage()); + } + } + + + } + + private void validate(int money) + { + if(money<0) + throw new IllegalArgumentException("금액이 0보다 작을 순 없습니다!"); + if(money%1000==0) + throw new IllegalArgumentException("1000원 단위로 입력해주세요!"); + } public String[] getJackpot() { From 201caa721db9ee59a095b2b7cc49fae68db3f87e Mon Sep 17 00:00:00 2001 From: nonactress Date: Sun, 28 Sep 2025 21:09:43 +0900 Subject: [PATCH 14/42] refactoring(model) : magic number "COST_PER_TICKET" --- src/main/java/Model/Lottos.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/Model/Lottos.java b/src/main/java/Model/Lottos.java index 463f1d193..39499005a 100644 --- a/src/main/java/Model/Lottos.java +++ b/src/main/java/Model/Lottos.java @@ -6,9 +6,9 @@ public class Lottos { private List lottoList; + private static int COST_PER_TICKET = 1000; public Lottos(int money) { - int COST_PER_TICKET = 1000; int numberOfLottos = money / COST_PER_TICKET; this.lottoList = new ArrayList<>(); From 04a10e1c2b7e56fd07d5485147c2b17a8ab1a362 Mon Sep 17 00:00:00 2001 From: nonactress Date: Sun, 28 Sep 2025 21:12:21 +0900 Subject: [PATCH 15/42] refactoring(model) : renaming constructor parameter "inputJackpot" --- src/main/java/Model/Jackpot.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/Model/Jackpot.java b/src/main/java/Model/Jackpot.java index 0d36ec54b..c9faec3b4 100644 --- a/src/main/java/Model/Jackpot.java +++ b/src/main/java/Model/Jackpot.java @@ -7,11 +7,11 @@ public class Jackpot { private List jackpot; - public Jackpot(String[] splits) { + public Jackpot(String[] inputJackpot) { this.jackpot = new ArrayList<>(); // 리스트 초기화 for (int i = 0; i < 6; i++) { - jackpot.add(Integer.parseInt(splits[i])); + jackpot.add(Integer.parseInt(inputJackpot[i])); } Collections.sort(this.jackpot); } From 165964ffa1f04355e97a59947ce63e9283c89bdb Mon Sep 17 00:00:00 2001 From: nonactress Date: Sun, 28 Sep 2025 21:42:21 +0900 Subject: [PATCH 16/42] refactoring : in getter, using only read method --- src/main/java/Model/Jackpot.java | 2 +- src/main/java/Model/Lotto.java | 2 +- src/main/java/Model/Lottos.java | 4 ++-- src/main/java/View/InputView.java | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/Model/Jackpot.java b/src/main/java/Model/Jackpot.java index c9faec3b4..b16e9fb3b 100644 --- a/src/main/java/Model/Jackpot.java +++ b/src/main/java/Model/Jackpot.java @@ -17,7 +17,7 @@ public Jackpot(String[] inputJackpot) { } public List getJackpot() { - return jackpot; + return Collections.unmodifiableList(jackpot); } @Override diff --git a/src/main/java/Model/Lotto.java b/src/main/java/Model/Lotto.java index ef526b930..9d97d9b62 100644 --- a/src/main/java/Model/Lotto.java +++ b/src/main/java/Model/Lotto.java @@ -21,7 +21,7 @@ public Lotto() { } public List getLotto() { - return lotto; + return Collections.unmodifiableList(lotto); } @Override diff --git a/src/main/java/Model/Lottos.java b/src/main/java/Model/Lottos.java index 39499005a..9431e8645 100644 --- a/src/main/java/Model/Lottos.java +++ b/src/main/java/Model/Lottos.java @@ -1,7 +1,7 @@ package Model; import java.util.ArrayList; -import java.util.Iterator; +import java.util.Collections; import java.util.List; public class Lottos { @@ -18,6 +18,6 @@ public Lottos(int money) { } public List getLottoList() { - return lottoList; + return Collections.unmodifiableList(lottoList); } } diff --git a/src/main/java/View/InputView.java b/src/main/java/View/InputView.java index 32067ef7e..404b8bdd3 100644 --- a/src/main/java/View/InputView.java +++ b/src/main/java/View/InputView.java @@ -20,7 +20,7 @@ public int getMoney() { { System.out.println("금액은 숫자로 입력해주세요!"); } - catch (IllegalArgumentException e) + catch (IllegalArgumentException e) { System.out.println(e.getMessage()); } From 70d4cbbea7bb61a42d53ebf96662e7f3f6f49519 Mon Sep 17 00:00:00 2001 From: nonactress Date: Mon, 29 Sep 2025 12:09:22 +0900 Subject: [PATCH 17/42] refactoring(controller) : renaming "intputView","outputView --- src/main/java/Controller/LottoController.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/main/java/Controller/LottoController.java b/src/main/java/Controller/LottoController.java index 26d9e8209..3e39de3be 100644 --- a/src/main/java/Controller/LottoController.java +++ b/src/main/java/Controller/LottoController.java @@ -8,19 +8,19 @@ public class LottoController { public void startLotto() { - InputView input = new InputView(); - int money = input.getMoney(); + InputView inputView = new InputView(); + int money = inputView.getMoney(); Lottos lottos = new Lottos(money); - OutView output = new OutView(); + OutView outView = new OutView(); - output.printLottos(lottos); + outView.printLottos(lottos); - Jackpot jackpot = new Jackpot(input.getJackpot()); + Jackpot jackpot = new Jackpot(inputView.getJackpot()); LottoMatcher lottoMatcher = new LottoMatcher(lottos, jackpot); - output.printLottoMatcher(lottoMatcher.getMatchCounts(), lottoMatcher.getRate(money)); + outView.printLottoMatcher(lottoMatcher.getMatchCounts(), lottoMatcher.getRate(money)); } } From 68ce019e360158151638824f032266b262c1e449 Mon Sep 17 00:00:00 2001 From: nonactress Date: Mon, 29 Sep 2025 12:23:00 +0900 Subject: [PATCH 18/42] refactoring(model) : "COST_PER_TICKET" --- src/main/java/Model/Lottos.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/Model/Lottos.java b/src/main/java/Model/Lottos.java index 9431e8645..e56169c3a 100644 --- a/src/main/java/Model/Lottos.java +++ b/src/main/java/Model/Lottos.java @@ -6,7 +6,7 @@ public class Lottos { private List lottoList; - private static int COST_PER_TICKET = 1000; + private static final int COST_PER_TICKET = 1000; public Lottos(int money) { int numberOfLottos = money / COST_PER_TICKET; From 1e7dc538cb70be2eb773b3f4118ffeb908ee5bd2 Mon Sep 17 00:00:00 2001 From: nonactress Date: Mon, 29 Sep 2025 12:24:33 +0900 Subject: [PATCH 19/42] refactoring(model) : fixing rogic of "validate" --- src/main/java/View/InputView.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/View/InputView.java b/src/main/java/View/InputView.java index 404b8bdd3..5a244b8ba 100644 --- a/src/main/java/View/InputView.java +++ b/src/main/java/View/InputView.java @@ -33,7 +33,7 @@ private void validate(int money) { if(money<0) throw new IllegalArgumentException("금액이 0보다 작을 순 없습니다!"); - if(money%1000==0) + if(money%1000!=0) throw new IllegalArgumentException("1000원 단위로 입력해주세요!"); } From 793e9a32406fae0384d0040667fa8a519be5a793 Mon Sep 17 00:00:00 2001 From: nonactress Date: Mon, 29 Sep 2025 14:01:25 +0900 Subject: [PATCH 20/42] refactoring(inputView) : adding "nextLine()" to break out of infinite loop --- src/main/java/View/InputView.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/View/InputView.java b/src/main/java/View/InputView.java index 5a244b8ba..83d07bfcb 100644 --- a/src/main/java/View/InputView.java +++ b/src/main/java/View/InputView.java @@ -19,8 +19,9 @@ public int getMoney() { catch (InputMismatchException e) { System.out.println("금액은 숫자로 입력해주세요!"); + scanner.nextLine(); } - catch (IllegalArgumentException e) + catch (IllegalArgumentException e) { System.out.println(e.getMessage()); } From 2358d2242d09360ebb42adf51d299555706d7684 Mon Sep 17 00:00:00 2001 From: nonactress Date: Wed, 1 Oct 2025 16:08:25 +0900 Subject: [PATCH 21/42] feat(model) : class "NumberGenerate" --- src/main/java/Model/NumberGenerate.java | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 src/main/java/Model/NumberGenerate.java diff --git a/src/main/java/Model/NumberGenerate.java b/src/main/java/Model/NumberGenerate.java new file mode 100644 index 000000000..dce647c0d --- /dev/null +++ b/src/main/java/Model/NumberGenerate.java @@ -0,0 +1,21 @@ +package Model; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class NumberGenerate { + private List number; + + public NumberGenerate(String[] splits) { + number = new ArrayList<>(); + + for (int i = 0; i < 6; i++) { + number.add(Integer.parseInt(splits[i])); + } + + Collections.sort(this.number); + } + + public List getNumber() { return Collections.unmodifiableList(number);} +} From 29060b44cbf67af343c4045201307c3d712e4c45 Mon Sep 17 00:00:00 2001 From: nonactress Date: Thu, 2 Oct 2025 02:46:01 +0900 Subject: [PATCH 22/42] feat(model) : class "AutoLotto" --- src/main/java/Model/AutoLotto.java | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 src/main/java/Model/AutoLotto.java diff --git a/src/main/java/Model/AutoLotto.java b/src/main/java/Model/AutoLotto.java new file mode 100644 index 000000000..954d1cc40 --- /dev/null +++ b/src/main/java/Model/AutoLotto.java @@ -0,0 +1,22 @@ +package Model; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class AutoLotto extends Lotto { + + public AutoLotto() { + List numbers = new ArrayList<>(); + + for (int i = 1; i <= 45; i++) { + numbers.add(i); + } + + Collections.shuffle(numbers); + this.numbers = new ArrayList<>(numbers.subList(0, 6)); + + Collections.sort(this.numbers); + } + +} From a472ddf791820c0fa61b5f67bfba2b1af4fb8542 Mon Sep 17 00:00:00 2001 From: nonactress Date: Thu, 2 Oct 2025 02:46:27 +0900 Subject: [PATCH 23/42] feat(model) : class "ManualLotto" --- src/main/java/Model/ManualLotto.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 src/main/java/Model/ManualLotto.java diff --git a/src/main/java/Model/ManualLotto.java b/src/main/java/Model/ManualLotto.java new file mode 100644 index 000000000..52da57e55 --- /dev/null +++ b/src/main/java/Model/ManualLotto.java @@ -0,0 +1,18 @@ +package Model; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +public class ManualLotto extends Lotto { + + public ManualLotto(String rawNumbers) { + String[] numberSplits = rawNumbers.split(","); + + this.numbers = Arrays.stream(numberSplits) + .map(String::trim) // " 21" -> "21" + .map(Integer::parseInt) // "21" -> 21 + .collect(Collectors.toList()); + } +} From 1bc5b7d9955ee3fbbc4216ff95f6ede78cc19aad Mon Sep 17 00:00:00 2001 From: nonactress Date: Thu, 2 Oct 2025 02:51:31 +0900 Subject: [PATCH 24/42] refactor(view) : class "OutView","InputView" --- src/main/java/View/InputView.java | 55 +++++++++++++++++++------------ src/main/java/View/OutView.java | 17 +++++++--- 2 files changed, 46 insertions(+), 26 deletions(-) diff --git a/src/main/java/View/InputView.java b/src/main/java/View/InputView.java index 83d07bfcb..fb56f5d9b 100644 --- a/src/main/java/View/InputView.java +++ b/src/main/java/View/InputView.java @@ -1,47 +1,60 @@ package View; -import java.util.Scanner; +import java.util.ArrayList; import java.util.InputMismatchException; +import java.util.List; +import java.util.Scanner; public class InputView { Scanner scanner = new Scanner(System.in); public int getMoney() { System.out.println("구입금액을 입력해 주세요."); - while(true) - { - try - { - int money = scanner.nextInt(); + while (true) { + try { + int money = scanner.nextInt(); validate(money); return money; - } - catch (InputMismatchException e) - { + } catch (InputMismatchException e) { System.out.println("금액은 숫자로 입력해주세요!"); scanner.nextLine(); - } - catch (IllegalArgumentException e) - { + } catch (IllegalArgumentException e) { System.out.println(e.getMessage()); } } - - } - private void validate(int money) - { - if(money<0) + private void validate(int money) { + if (money < 0) throw new IllegalArgumentException("금액이 0보다 작을 순 없습니다!"); - if(money%1000!=0) + if (money % 1000 != 0) throw new IllegalArgumentException("1000원 단위로 입력해주세요!"); - } - public String[] getJackpot() { - scanner.nextLine(); + public String[] getJackpotNumber() { System.out.println("정답 로또번호를 입력해주세요( ','로 구분해서 입력)"); return scanner.nextLine().split(","); } + + + public int getManualLottoNumber() { + System.out.println("수동으로 구매할 로또 수를 입력해 주세요."); + return scanner.nextInt(); + } + + public List manualLotto(int manualLottoNumber) { + List manualLottoLines = new ArrayList<>(); + + scanner.nextLine(); + System.out.println("수동으로 구매할 번호를 입력해 주세요."); + for (int i = 0; i < manualLottoNumber; i++) { + manualLottoLines.add(scanner.nextLine()); + } + return manualLottoLines; + } + + public int getBonus() { + System.out.println("보너스 볼을 입력해 주세요."); + return scanner.nextInt(); + } } diff --git a/src/main/java/View/OutView.java b/src/main/java/View/OutView.java index d0476cd08..c8b994952 100644 --- a/src/main/java/View/OutView.java +++ b/src/main/java/View/OutView.java @@ -3,18 +3,25 @@ import Model.Lotto; import Model.Lottos; +import java.util.Map; + public class OutView { - public void printLottoMatcher(int[] matchCounts, double rate) { + public void printLottoMatcher(Map matchCounts, double rate) { System.out.println("당첨 통계"); System.out.println("------"); - System.out.println("3개 일치(5,000원): " + matchCounts[3] + "개"); - System.out.println("4개 일치(50,000원): " + matchCounts[4] + "개"); - System.out.println("5개 일치(1,500,000원): " + matchCounts[5] + "개"); - System.out.println("6개 일치(2,000,000,000원): " + matchCounts[6] + "개"); + System.out.println("3개 일치(5,000원): " + matchCounts.getOrDefault(3, 0) + "개"); + System.out.println("4개 일치(50,000원): " + matchCounts.getOrDefault(4, 0) + "개"); + System.out.println("5개 일치(1,500,000원): " + matchCounts.getOrDefault(5, 0) + "개"); + System.out.println("5개 일치, 보너스 볼 일치(30,000,000원): " + matchCounts.getOrDefault(7, 0) + "개"); + System.out.println("6개 일치(2,000,000,000원): " + matchCounts.getOrDefault(6, 0) + "개"); System.out.printf("총 수익률은 %.2f입니다.", rate); } + public void printLottosStaus(int AutoLottoNumber, int manualLottoNumber) { + System.out.println(); + System.out.println("수동으로 " + manualLottoNumber + "장, 자동으로" + AutoLottoNumber + "개를 구매했습니다."); + } public void printLottos(Lottos lottos) { for (Lotto lotto : lottos.getLottoList()) { From bb86d4fff12ff1fa645e8540a8d623b462b0544b Mon Sep 17 00:00:00 2001 From: nonactress Date: Thu, 2 Oct 2025 02:53:45 +0900 Subject: [PATCH 25/42] refactor(view) : field "Bonus" --- src/main/java/Model/PrizeMoney.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/Model/PrizeMoney.java b/src/main/java/Model/PrizeMoney.java index ddf63d934..507a35825 100644 --- a/src/main/java/Model/PrizeMoney.java +++ b/src/main/java/Model/PrizeMoney.java @@ -4,7 +4,8 @@ public enum PrizeMoney { THREE(5000), FOUR(50000), FIVE(1500000), - SIX(2000000000); // 상수 목록 끝에 세미콜론 + SIX(2000000000), + BONUS(30000000); private final int money; From 4659d1a354d31ccec72cc40b5aa6016f437619e0 Mon Sep 17 00:00:00 2001 From: nonactress Date: Thu, 2 Oct 2025 02:56:29 +0900 Subject: [PATCH 26/42] refactor(model) : Implement auto/manual lottery number generation logic --- src/main/java/Model/Lottos.java | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/main/java/Model/Lottos.java b/src/main/java/Model/Lottos.java index e56169c3a..56adb4fef 100644 --- a/src/main/java/Model/Lottos.java +++ b/src/main/java/Model/Lottos.java @@ -8,16 +8,26 @@ public class Lottos { private List lottoList; private static final int COST_PER_TICKET = 1000; - public Lottos(int money) { - int numberOfLottos = money / COST_PER_TICKET; + public Lottos(int money, List manualLottoLines) { this.lottoList = new ArrayList<>(); + int manualLottosNumber = manualLottoLines.size(); - for (int i = 0; i < numberOfLottos; i++) { - lottoList.add(new Lotto()); // new Lotto()가 호출될 때마다 새로운 로또 번호 6개가 생성됩니다. + // 수동 로또 생성 + for (String line : manualLottoLines) { + lottoList.add(new ManualLotto(line)); + } + + // 자동 로또 생성 + for (int i = 0; i < getAutoLottoCount(money,manualLottosNumber); i++) { + lottoList.add(new AutoLotto()); } } public List getLottoList() { return Collections.unmodifiableList(lottoList); } + + public int getAutoLottoCount(int money,int manualLottosNumber) { + return money / COST_PER_TICKET - manualLottosNumber; + } } From 46091b51cd26e1e5c43176ad3d6bf52ef3c9c052 Mon Sep 17 00:00:00 2001 From: nonactress Date: Thu, 2 Oct 2025 02:57:24 +0900 Subject: [PATCH 27/42] refactor(model) : adding bonus ball calculation --- src/main/java/Model/LottoMatcher.java | 33 +++++++++++++++++++-------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/src/main/java/Model/LottoMatcher.java b/src/main/java/Model/LottoMatcher.java index ff26ad626..ed54ba1e7 100644 --- a/src/main/java/Model/LottoMatcher.java +++ b/src/main/java/Model/LottoMatcher.java @@ -1,17 +1,28 @@ package Model; +import java.util.HashMap; import java.util.List; +import java.util.Map; public class LottoMatcher { - private final int[] matchCounts; + private final Map matchCounts; public LottoMatcher(Lottos lottos, Jackpot jackpot) { - this.matchCounts = new int[7]; + this.matchCounts = new HashMap<>(); for (Lotto lotto : lottos.getLottoList()) { int matches = countMatches(jackpot.getJackpot(), lotto.getLotto()); - matchCounts[matches]++; + checkBonus(jackpot, lotto, matches); } + + } + + private void checkBonus(Jackpot jackpot, Lotto lotto, int matches) { + if (matches == 5 && lotto.getLotto().contains(jackpot.getBounsNumber())) { + matchCounts.put(7, matchCounts.getOrDefault(7, 0) + 1); + return; + } + matchCounts.put(matches, matchCounts.getOrDefault(matches, 0) + 1); } private int countMatches(List jackpotNumbers, List lottoNumbers) { @@ -22,6 +33,7 @@ private int countMatches(List jackpotNumbers, List lottoNumber return count; } + private int getMatchValue(List jackpotNumbers, int number) { if (jackpotNumbers.contains(number)) { return 1; @@ -29,18 +41,21 @@ private int getMatchValue(List jackpotNumbers, int number) { return 0; } - public int[] getMatchCounts() { + public Map getMatchCounts() { return matchCounts; } public double calculateTotalEarnings() { - return matchCounts[3] * PrizeMoney.THREE.getMoney() - + matchCounts[4] * PrizeMoney.FOUR.getMoney() - + matchCounts[5] * PrizeMoney.FIVE.getMoney() - + matchCounts[6] * PrizeMoney.SIX.getMoney(); + + return matchCounts.getOrDefault(3, 0) * PrizeMoney.THREE.getMoney() + + matchCounts.getOrDefault(4, 0) * PrizeMoney.FOUR.getMoney() + + matchCounts.getOrDefault(5, 0) * PrizeMoney.FIVE.getMoney() + + matchCounts.getOrDefault(7, 0) * PrizeMoney.BONUS.getMoney() + + matchCounts.getOrDefault(6, 0) * PrizeMoney.SIX.getMoney(); } public double getRate(int money) { - return (double) calculateTotalEarnings() / money; + return calculateTotalEarnings() / money; } + } From 53d1b3851246119c6046557c95b71b47bcead71f Mon Sep 17 00:00:00 2001 From: nonactress Date: Thu, 2 Oct 2025 02:58:46 +0900 Subject: [PATCH 28/42] refactor(model) : field "bonusNumber" --- src/main/java/Model/Jackpot.java | 14 ++++++------ src/main/java/Model/JackpotGenerator.java | 27 +++++++++++++++++++++++ 2 files changed, 34 insertions(+), 7 deletions(-) create mode 100644 src/main/java/Model/JackpotGenerator.java diff --git a/src/main/java/Model/Jackpot.java b/src/main/java/Model/Jackpot.java index b16e9fb3b..de2fe3a7e 100644 --- a/src/main/java/Model/Jackpot.java +++ b/src/main/java/Model/Jackpot.java @@ -1,25 +1,25 @@ package Model; -import java.util.ArrayList; import java.util.Collections; import java.util.List; public class Jackpot { private List jackpot; + private int bounsNumber; - public Jackpot(String[] inputJackpot) { - this.jackpot = new ArrayList<>(); // 리스트 초기화 + public Jackpot(String[] inputJackpot,int bounsNumber) { + JackpotGenerator from = JackpotGenerator.from(inputJackpot); + this.jackpot = from.getNumber(); - for (int i = 0; i < 6; i++) { - jackpot.add(Integer.parseInt(inputJackpot[i])); - } - Collections.sort(this.jackpot); + this.bounsNumber = bounsNumber; } public List getJackpot() { return Collections.unmodifiableList(jackpot); } + public int getBounsNumber() {return bounsNumber;} + @Override public String toString() { return jackpot.toString(); diff --git a/src/main/java/Model/JackpotGenerator.java b/src/main/java/Model/JackpotGenerator.java new file mode 100644 index 000000000..b15962560 --- /dev/null +++ b/src/main/java/Model/JackpotGenerator.java @@ -0,0 +1,27 @@ +package Model; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class JackpotGenerator { + private final List number; + + private JackpotGenerator(String[] splits) { + List tempNumbers = new ArrayList<>(); + for (String s : splits) { + tempNumbers.add(Integer.parseInt(s)); + } + Collections.sort(tempNumbers); + this.number = Collections.unmodifiableList(tempNumbers); + } + + public static JackpotGenerator from(String[] splits) { + //추가적인 유효성 검사 로직( splits의 길이가 6이 맞는지 등) 있어야 함. + return new JackpotGenerator(splits); + } + + public List getNumber() { + return number; + } +} From 28ce4b9c04a07313a8cfe232905e1cb57ae1c667 Mon Sep 17 00:00:00 2001 From: nonactress Date: Thu, 2 Oct 2025 02:59:41 +0900 Subject: [PATCH 29/42] refactor(model) : use inherited parent object --- src/main/java/Model/Lotto.java | 20 +++----------------- 1 file changed, 3 insertions(+), 17 deletions(-) diff --git a/src/main/java/Model/Lotto.java b/src/main/java/Model/Lotto.java index 9d97d9b62..b6b96630d 100644 --- a/src/main/java/Model/Lotto.java +++ b/src/main/java/Model/Lotto.java @@ -1,31 +1,17 @@ package Model; -import java.util.ArrayList; import java.util.Collections; import java.util.List; public class Lotto { - private List lotto; - - public Lotto() { - List numbers = new ArrayList<>(); - - for (int i = 1; i <= 45; i++) { - numbers.add(i); - } - - Collections.shuffle(numbers); - this.lotto = new ArrayList<>(numbers.subList(0, 6)); - - Collections.sort(this.lotto); // 뽑은 6개의 숫자를 오름차순으로 정렬 - } + protected List numbers; public List getLotto() { - return Collections.unmodifiableList(lotto); + return Collections.unmodifiableList(numbers); } @Override public String toString() { - return lotto.toString(); + return numbers.toString(); } } From ee3d5459e9c22e9ae0ef26bf821716bf8e7a9e92 Mon Sep 17 00:00:00 2001 From: nonactress Date: Thu, 2 Oct 2025 03:00:28 +0900 Subject: [PATCH 30/42] refactor(model) : - Apply updates from latest changes --- src/main/java/Controller/LottoController.java | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/main/java/Controller/LottoController.java b/src/main/java/Controller/LottoController.java index 3e39de3be..f678a5441 100644 --- a/src/main/java/Controller/LottoController.java +++ b/src/main/java/Controller/LottoController.java @@ -6,18 +6,29 @@ import View.InputView; import View.OutView; +import java.util.List; + public class LottoController { public void startLotto() { InputView inputView = new InputView(); + int money = inputView.getMoney(); + int manualLottoNumber = inputView.getManualLottoNumber(); + + List rawNumbersList = inputView.manualLotto(manualLottoNumber); - Lottos lottos = new Lottos(money); + Lottos lottos = new Lottos(money, rawNumbersList); OutView outView = new OutView(); + outView.printLottosStaus(lottos.getAutoLottoCount(money, manualLottoNumber), manualLottoNumber); + outView.printLottos(lottos); - Jackpot jackpot = new Jackpot(inputView.getJackpot()); + String[] winningNumber = inputView.getJackpotNumber(); + int bonusNumber = inputView.getBonus(); + + Jackpot jackpot = new Jackpot(winningNumber, bonusNumber); LottoMatcher lottoMatcher = new LottoMatcher(lottos, jackpot); From 517dcee2489ea0fb7b4c5bf5c36cad87dd716ef5 Mon Sep 17 00:00:00 2001 From: nonactress Date: Thu, 2 Oct 2025 03:00:54 +0900 Subject: [PATCH 31/42] delete(model) : NumberGenerate.java --- src/main/java/Model/NumberGenerate.java | 21 --------------------- 1 file changed, 21 deletions(-) delete mode 100644 src/main/java/Model/NumberGenerate.java diff --git a/src/main/java/Model/NumberGenerate.java b/src/main/java/Model/NumberGenerate.java deleted file mode 100644 index dce647c0d..000000000 --- a/src/main/java/Model/NumberGenerate.java +++ /dev/null @@ -1,21 +0,0 @@ -package Model; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -public class NumberGenerate { - private List number; - - public NumberGenerate(String[] splits) { - number = new ArrayList<>(); - - for (int i = 0; i < 6; i++) { - number.add(Integer.parseInt(splits[i])); - } - - Collections.sort(this.number); - } - - public List getNumber() { return Collections.unmodifiableList(number);} -} From a8751ae3c8eb02441bae0a46df7871d562a1fe09 Mon Sep 17 00:00:00 2001 From: nonactress Date: Thu, 2 Oct 2025 03:08:20 +0900 Subject: [PATCH 32/42] refactor(READ.ME) --- README.md | 91 +++++++++++++++++++++++++++++++------------------------ 1 file changed, 51 insertions(+), 40 deletions(-) diff --git a/README.md b/README.md index 66db098d6..af9837031 100644 --- a/README.md +++ b/README.md @@ -1,54 +1,65 @@ -# 로또 +# Java Lotto Clean Playground ## 📖 개요 -사용자는 구입 금액을 입력하고, 그에 따라 로또가 발급됩니다. 마지막으로 당첨 번호를 입력하여 당첨 통계와 수익률을 확인할 수 있습니다. +## ✨ 주요 기능 -## 📂 프로젝트 구조 +- **로또 구매**: 입력한 금액에 맞춰 1,000원 단위로 로또를 구매합니다. +- **수동/자동 구매**: 원하는 개수만큼 로또 번호를 직접 선택하는 수동 구매와 나머지 개수를 자동으로 발급받는 기능을 지원합니다. +- **구매 내역 출력**: 구매한 모든 로또(수동, 자동)의 번호를 출력합니다. +- **당첨 통계**: 사용자가 입력한 당첨 번호 및 보너스 번호를 기반으로 당첨 내역을 계산합니다. + - 1등: 6개 번호 일치 + - 2등: 5개 번호 + 보너스 번호 일치 + - 3등: 5개 번호 일치 + - 4등: 4개 번호 일치 + - 5등: 3개 번호 일치 +- **수익률 계산**: 총 구매 금액 대비 당첨금 총액을 기반으로 한 수익률을 소수점 둘째 자리까지 계산하여 출력합니다. ``` . -├── src -│ ├── main -│ │ ├── java -│ │ │ ├── Application.java # 애플리케이션 시작점 -│ │ │ ├── Controller -│ │ │ │ └── LottoController.java # 게임 로직 제어 -│ │ │ ├── Model -│ │ │ │ ├── Jackpot.java # 당첨 번호 -│ │ │ │ ├── Lotto.java # 로또 1장 -│ │ │ │ ├── Lottos.java # 발급된 전체 로또 -│ │ │ │ └── LottoMatcher.java # 로또 당첨 확인 -│ │ │ └── View -│ │ │ ├── InputView.java # 사용자 입력 -│ │ │ └── OutView.java # 결과 출력 -│ │ └── resources -│ └── test -│ └── java -└── build.gradle +└── src + └── main + └── java + ├── Application.java # 애플리케이션 시작점 + ├── Controller + │ └── LottoController.java # 게임 흐름 제어 + ├── Model + │ ├── Lotto.java # 로또 1장 + │ ├── Lottos.java # 발급된 전체 로또 + │ ├── Jackpot.java # 당첨 번호와 보너스 번호 + │ ├── LottoMatcher.java # 로또 당첨 여부 확인 및 통계 계산 + │ ├── PrizeMoney.java # 등수별 상금 Enum + │ ├── AutoLotto.java # 자동 로또 번호 생성 + │ └── ManualLotto.java # 수동 로또 번호 관리 + └── View + ├── InputView.java # 사용자 입력 처리 + └── OutView.java # 결과 출력 담당 + ``` +## ⚙️ 핵심 로직 상세 -## ✨ 주요 기능 및 동작 원리 +### 1. 로또 생성 (`Lottos.java`) -1. **애플리케이션 시작 (`Application.java`)** - - `main` 메소드에서 `LottoController`를 생성하고 `startLotto()` 메소드를 호출하여 로또 게임을 시작합니다. +- `Lottos` 클래스는 사용자가 구매한 모든 로또 티켓을 관리합니다. +- `InputView`를 통해 받은 수동 구매 개수와 번호 문자열 리스트를 `ManualLotto`를 통해 `Lotto` 객체 리스트로 변환합니다. +- 총 구매 개수에서 수동 구매 개수를 뺀 만큼 `AutoLotto`를 통해 자동으로 로또 번호를 생성합니다. +- `AutoLotto`는 `Collections.shuffle`을 활용하여 1부터 45까지의 숫자 중 6개를 무작위로 선택하여 중복 없는 번호를 생성합니다. -2. **입력 (`InputView.java`)** - - 사용자로부터 로또 구입 금액을 입력받습니다. - - 사용자로부터 당첨 번호를 쉼표(`,`)로 구분하여 입력받습니다. +### 2. 당첨 확인 (`LottoMatcher.java`) -3. **로직 처리 (`Controller` & `Model`)** - - **`LottoController`**: - - `InputView`를 통해 구입 금액을 받아, 금액만큼의 `Lotto` 객체를 생성합니다. (`Lottos`) - - `InputView`를 통해 당첨 번호를 받아 `Jackpot` 객체를 생성합니다. - - `LottoMatcher`를 통해 발급된 로또와 당첨 번호를 비교하여 당첨 통계를 계산합니다. - - 계산된 결과를 `OutView`를 통해 출력합니다. - - **`Lotto`**: 1부터 45 사이의 중복되지 않는 6개의 숫자로 이루어진 로또 한 장을 의미합니다. - - **`Lottos`**: 사용자가 구입한 금액만큼의 `Lotto` 객체들을 리스트로 관리합니다. - - **`Jackpot`**: 당첨 번호 6개를 저장합니다. - - **`LottoMatcher`**: `Lottos`와 `Jackpot`을 비교하여, 일치하는 번호의 개수를 세고, 당첨 통계와 수익률을 계산합니다. +`LottoMatcher`는 로또 게임의 당첨 결과를 판별하고 통계를 내는, 가장 중요한 비즈니스 로직을 담당하는 클래스입니다. 객체가 생성되는 시점에 모든 핵심 연산을 수행하여 내부에 결과를 저장하고, 외부에는 요청에 따라 계산된 결과를 즉시 제공하는 방식으로 설계되었습니다. -4. **출력 (`OutView.java`)** - - 발급된 로또 번호들을 출력합니다. - - 최종 당첨 통계 (3개 일치 ~ 6개 일치)와 총수익률을 계산하여 출력합니다. +#### 동작 원리 +1. **생성 및 초기화**: `LottoController`가 `new LottoMatcher(lottos, jackpot)` 코드로 객체를 생성하면, `LottoMatcher`의 생성자가 실행되며 모든 계산이 즉시 이루어집니다. + - **데이터 준비**: 생성자는 사용자가 구매한 모든 로또 티켓 묶음(`lottos`)과 당첨 번호 정보(`jackpot`)를 인자로 받습니다. 그리고 당첨 통계를 저장할 `matchCounts`라는 `Map`을 초기화합니다. 이 맵은 `{일치 개수: 당첨된 티켓 수}` 형태로 통계를 저장하게 됩니다. + - **개별 티켓 비교**: `for` 루프를 통해 구매한 로또 티켓을 한 장씩 순회하며 각 티켓별로 당첨 등수를 판별합니다. + - 먼저 해당 티켓의 번호 6개가 당첨 번호 6개와 몇 개나 일치하는지 계산합니다. + - 만약 일치 개수가 정확히 **5개**라면, 2등 당첨 가능성이 생깁니다. 이때, 해당 로또 티켓이 **보너스 번호를 포함하고 있는지** 추가로 확인하여 2등과 3등을 구분합니다. + - 2등(5개 일치 + 보너스 번호 포함)으로 확인되면, `matchCounts` 맵에 `7`이라는 특별한 키값으로 당첨 횟수를 1 증가시킵니다. + - 그 외의 경우(3, 4, 5, 6개 일치), 계산된 일치 개수를 키로 사용하여 `matchCounts` 맵의 카운트를 1 증가시킵니다. + +2. **결과 제공**: 모든 계산이 완료된 `LottoMatcher` 객체는 `LottoController`의 요청에 따라 아래 메소드들을 통해 결과를 제공합니다. + - `getMatchCounts()`: 모든 계산이 완료된 `matchCounts` 맵을 반환합니다. `OutView`는 이 맵을 받아 최종 당첨 통계를 화면에 출력합니다. + - `calculateTotalEarnings()`: `matchCounts` 맵과 `PrizeMoney` Enum을 기반으로 총상금을 계산합니다. + - `getRate()`: `calculateTotalEarnings()`로 계산된 총상금을 사용자의 총 구매 금액으로 나누어 최종 수익률을 계산합니다. From 6cc626c907f95ea67ca923680bee04c395bb63ad Mon Sep 17 00:00:00 2001 From: nonactress Date: Thu, 2 Oct 2025 03:14:29 +0900 Subject: [PATCH 33/42] refactor(model) : adding validate rogic --- src/main/java/Model/Jackpot.java | 17 +++++++-- src/main/java/Model/JackpotGenerator.java | 43 ++++++++++++++++++----- 2 files changed, 49 insertions(+), 11 deletions(-) diff --git a/src/main/java/Model/Jackpot.java b/src/main/java/Model/Jackpot.java index de2fe3a7e..191159e94 100644 --- a/src/main/java/Model/Jackpot.java +++ b/src/main/java/Model/Jackpot.java @@ -7,18 +7,29 @@ public class Jackpot { private List jackpot; private int bounsNumber; - public Jackpot(String[] inputJackpot,int bounsNumber) { + public Jackpot(String[] inputJackpot, int bounsNumber) { JackpotGenerator from = JackpotGenerator.from(inputJackpot); this.jackpot = from.getNumber(); - + validateBonusNumber(bounsNumber, this.jackpot); this.bounsNumber = bounsNumber; } + private void validateBonusNumber(int bounsNumber, List jackpot) { + if (bounsNumber < 1 || bounsNumber > 45) { + throw new IllegalArgumentException("보너스 번호는 1과 45 사이의 숫자여야 합니다."); + } + if (jackpot.contains(bounsNumber)) { + throw new IllegalArgumentException("보너스 번호는 당첨 번호와 중복될 수 없습니다."); + } + } + public List getJackpot() { return Collections.unmodifiableList(jackpot); } - public int getBounsNumber() {return bounsNumber;} + public int getBounsNumber() { + return bounsNumber; + } @Override public String toString() { diff --git a/src/main/java/Model/JackpotGenerator.java b/src/main/java/Model/JackpotGenerator.java index b15962560..81c2c287f 100644 --- a/src/main/java/Model/JackpotGenerator.java +++ b/src/main/java/Model/JackpotGenerator.java @@ -2,23 +2,50 @@ import java.util.ArrayList; import java.util.Collections; +import java.util.HashSet; import java.util.List; +import java.util.Set; public class JackpotGenerator { private final List number; - private JackpotGenerator(String[] splits) { - List tempNumbers = new ArrayList<>(); + private JackpotGenerator(List numbers) { + this.number = numbers; + } + + public static JackpotGenerator from(String[] splits) { + if (splits.length != 6) { + throw new IllegalArgumentException("당첨 번호는 6개여야 합니다."); + } + List numbers = parseNumbers(splits); + validateNumbers(numbers); + Collections.sort(numbers); + return new JackpotGenerator(Collections.unmodifiableList(numbers)); + } + + private static List parseNumbers(String[] splits) { + List numbers = new ArrayList<>(); for (String s : splits) { - tempNumbers.add(Integer.parseInt(s)); + try { + numbers.add(Integer.parseInt(s.trim())); + } catch (NumberFormatException e) { + throw new IllegalArgumentException("당첨 번호는 숫자여야 합니다."); + } } - Collections.sort(tempNumbers); - this.number = Collections.unmodifiableList(tempNumbers); + return numbers; } - public static JackpotGenerator from(String[] splits) { - //추가적인 유효성 검사 로직( splits의 길이가 6이 맞는지 등) 있어야 함. - return new JackpotGenerator(splits); + private static void validateNumbers(List numbers) { + Set uniqueNumbers = new HashSet<>(numbers); + if (uniqueNumbers.size() != 6) { + throw new IllegalArgumentException("당첨 번호는 중복될 수 없습니다."); + } + + for (int number : numbers) { + if (number < 1 || number > 45) { + throw new IllegalArgumentException("당첨 번호는 1과 45 사이의 숫자여야 합니다."); + } + } } public List getNumber() { From 43b220ad96ab55e71ad8c8a57d7ebe3411ceceeb Mon Sep 17 00:00:00 2001 From: nonactress Date: Sat, 4 Oct 2025 21:37:52 +0900 Subject: [PATCH 34/42] refactor(view) : using System.err.print --- src/main/java/View/InputView.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/View/InputView.java b/src/main/java/View/InputView.java index fb56f5d9b..f09e98f4a 100644 --- a/src/main/java/View/InputView.java +++ b/src/main/java/View/InputView.java @@ -16,7 +16,7 @@ public int getMoney() { validate(money); return money; } catch (InputMismatchException e) { - System.out.println("금액은 숫자로 입력해주세요!"); + System.err.println("금액은 숫자로 입력해주세요!"); scanner.nextLine(); } catch (IllegalArgumentException e) { System.out.println(e.getMessage()); From 418292f5c08abc94219a1fd5fc7320699734089d Mon Sep 17 00:00:00 2001 From: nonactress Date: Sun, 5 Oct 2025 12:16:48 +0900 Subject: [PATCH 35/42] refactor : unify variable names for consistency --- README.md | 1 + src/main/java/Controller/LottoController.java | 6 ++-- src/main/java/Model/ManualLotto.java | 29 +++++++++++++++---- src/main/java/View/InputView.java | 2 +- 4 files changed, 29 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index af9837031..9708dad19 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,7 @@ └── OutView.java # 결과 출력 담당 ``` + ## ⚙️ 핵심 로직 상세 ### 1. 로또 생성 (`Lottos.java`) diff --git a/src/main/java/Controller/LottoController.java b/src/main/java/Controller/LottoController.java index f678a5441..9ab09c84f 100644 --- a/src/main/java/Controller/LottoController.java +++ b/src/main/java/Controller/LottoController.java @@ -13,15 +13,15 @@ public void startLotto() { InputView inputView = new InputView(); int money = inputView.getMoney(); - int manualLottoNumber = inputView.getManualLottoNumber(); + int manualLottoCount = inputView.getManualLottoCount(); - List rawNumbersList = inputView.manualLotto(manualLottoNumber); + List rawNumbersList = inputView.manualLotto(manualLottoCount); Lottos lottos = new Lottos(money, rawNumbersList); OutView outView = new OutView(); - outView.printLottosStaus(lottos.getAutoLottoCount(money, manualLottoNumber), manualLottoNumber); + outView.printLottosStaus(lottos.getAutoLottoCount(money, manualLottoCount), manualLottoCount); outView.printLottos(lottos); diff --git a/src/main/java/Model/ManualLotto.java b/src/main/java/Model/ManualLotto.java index 52da57e55..074b0beb6 100644 --- a/src/main/java/Model/ManualLotto.java +++ b/src/main/java/Model/ManualLotto.java @@ -8,11 +8,30 @@ public class ManualLotto extends Lotto { public ManualLotto(String rawNumbers) { - String[] numberSplits = rawNumbers.split(","); + numbers = parseNumbers(rawNumbers); + validate(numbers); + + + } + private List parseNumbers(String rawNumbers) { + try { + return Arrays.stream(rawNumbers.split(",")) + .map(String::trim) + .map(Integer::parseInt) + .collect(Collectors.toList()); + } catch (NumberFormatException e) { + throw new IllegalArgumentException("로또 번호는 숫자만 입력 가능합니다."); + } + } + + private void validate (List parseNumber){ + if(numbers.size()!=6) { + System.err.println("6개의 숫자를 입력해주세요!"); + //throw new IllegalArgumentException("6개의 숫자를 입력해주세요!"); + } + } + + private void validateNumberRange(List parseNumber) { - this.numbers = Arrays.stream(numberSplits) - .map(String::trim) // " 21" -> "21" - .map(Integer::parseInt) // "21" -> 21 - .collect(Collectors.toList()); } } diff --git a/src/main/java/View/InputView.java b/src/main/java/View/InputView.java index f09e98f4a..c6fe2329c 100644 --- a/src/main/java/View/InputView.java +++ b/src/main/java/View/InputView.java @@ -37,7 +37,7 @@ public String[] getJackpotNumber() { } - public int getManualLottoNumber() { + public int getManualLottoCount() { System.out.println("수동으로 구매할 로또 수를 입력해 주세요."); return scanner.nextInt(); } From 166a97a357d82b2fad4ffe5dc13b4ea232c6aa74 Mon Sep 17 00:00:00 2001 From: nonactress Date: Sun, 5 Oct 2025 14:54:52 +0900 Subject: [PATCH 36/42] refactor : unify "iterable" interface --- src/main/java/Model/LottoMatcher.java | 2 +- src/main/java/Model/Lottos.java | 17 +++++++++-------- src/main/java/View/OutView.java | 2 +- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/main/java/Model/LottoMatcher.java b/src/main/java/Model/LottoMatcher.java index ed54ba1e7..3010186c9 100644 --- a/src/main/java/Model/LottoMatcher.java +++ b/src/main/java/Model/LottoMatcher.java @@ -10,7 +10,7 @@ public class LottoMatcher { public LottoMatcher(Lottos lottos, Jackpot jackpot) { this.matchCounts = new HashMap<>(); - for (Lotto lotto : lottos.getLottoList()) { + for (Lotto lotto : lottos) { int matches = countMatches(jackpot.getJackpot(), lotto.getLotto()); checkBonus(jackpot, lotto, matches); } diff --git a/src/main/java/Model/Lottos.java b/src/main/java/Model/Lottos.java index 56adb4fef..97e8fff39 100644 --- a/src/main/java/Model/Lottos.java +++ b/src/main/java/Model/Lottos.java @@ -1,10 +1,10 @@ package Model; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; +import java.util.*; +import java.util.function.Consumer; -public class Lottos { +// 1. 제네릭을 사용하여 Iterable로 타입 명시 +public class Lottos implements Iterable { private List lottoList; private static final int COST_PER_TICKET = 1000; @@ -23,11 +23,12 @@ public Lottos(int money, List manualLottoLines) { } } - public List getLottoList() { - return Collections.unmodifiableList(lottoList); - } - public int getAutoLottoCount(int money,int manualLottosNumber) { return money / COST_PER_TICKET - manualLottosNumber; } + + @Override + public Iterator iterator() { + return lottoList.iterator(); + } } diff --git a/src/main/java/View/OutView.java b/src/main/java/View/OutView.java index c8b994952..4dfdb68e6 100644 --- a/src/main/java/View/OutView.java +++ b/src/main/java/View/OutView.java @@ -24,7 +24,7 @@ public void printLottosStaus(int AutoLottoNumber, int manualLottoNumber) { } public void printLottos(Lottos lottos) { - for (Lotto lotto : lottos.getLottoList()) { + for (Lotto lotto : lottos) { System.out.println(lotto); } } From fbcd28229ca608ae4088334f18185d5b9212272e Mon Sep 17 00:00:00 2001 From: nonactress Date: Sun, 5 Oct 2025 16:22:41 +0900 Subject: [PATCH 37/42] refactor : validate rogic(ManualLotto) --- src/main/java/Model/ManualLotto.java | 35 +++++++++++++++++++++------- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/src/main/java/Model/ManualLotto.java b/src/main/java/Model/ManualLotto.java index 074b0beb6..ffdc98dab 100644 --- a/src/main/java/Model/ManualLotto.java +++ b/src/main/java/Model/ManualLotto.java @@ -1,18 +1,21 @@ package Model; -import java.util.ArrayList; import java.util.Arrays; +import java.util.HashSet; import java.util.List; +import java.util.Set; import java.util.stream.Collectors; public class ManualLotto extends Lotto { public ManualLotto(String rawNumbers) { - numbers = parseNumbers(rawNumbers); - validate(numbers); + List parsedNumbers = parseNumbers(rawNumbers); + validate(parsedNumbers); + this.numbers = parsedNumbers; } + private List parseNumbers(String rawNumbers) { try { return Arrays.stream(rawNumbers.split(",")) @@ -24,14 +27,30 @@ private List parseNumbers(String rawNumbers) { } } - private void validate (List parseNumber){ - if(numbers.size()!=6) { - System.err.println("6개의 숫자를 입력해주세요!"); - //throw new IllegalArgumentException("6개의 숫자를 입력해주세요!"); + private void validate(List numbers) { + validateSize(numbers); + validateDuplicates(numbers); + validateNumberRange(numbers); + } + + private void validateSize(List numbers) { + if (numbers.size() != 6) { + throw new IllegalArgumentException("로또 번호는 6개여야 합니다."); } } - private void validateNumberRange(List parseNumber) { + private void validateDuplicates(List numbers) { + Set uniqueNumbers = new HashSet<>(numbers); + if (uniqueNumbers.size() != numbers.size()) { + throw new IllegalArgumentException("로또 번호는 중복될 수 없습니다."); + } + } + private void validateNumberRange(List numbers) { + for (int number : numbers) { + if (number < 1 || number > 45) { + throw new IllegalArgumentException("로또 번호는 1과 45 사이의 숫자여야 합니다."); + } + } } } From 52e393bed2d396a59c4b5111aaac6269d8ccdbd0 Mon Sep 17 00:00:00 2001 From: nonactress Date: Tue, 7 Oct 2025 20:01:44 +0900 Subject: [PATCH 38/42] refactor : using TreeSet --- src/main/java/Model/JackpotGenerator.java | 39 +++++++++++------------ 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/src/main/java/Model/JackpotGenerator.java b/src/main/java/Model/JackpotGenerator.java index 81c2c287f..9b5666823 100644 --- a/src/main/java/Model/JackpotGenerator.java +++ b/src/main/java/Model/JackpotGenerator.java @@ -2,9 +2,9 @@ import java.util.ArrayList; import java.util.Collections; -import java.util.HashSet; import java.util.List; import java.util.Set; +import java.util.TreeSet; public class JackpotGenerator { private final List number; @@ -14,37 +14,36 @@ private JackpotGenerator(List numbers) { } public static JackpotGenerator from(String[] splits) { + Set numbers = parseAndValidateNumbers(splits); + return new JackpotGenerator(Collections.unmodifiableList(new ArrayList<>(numbers))); + } + + private static Set parseAndValidateNumbers(String[] splits) { if (splits.length != 6) { - throw new IllegalArgumentException("당첨 번호는 6개여야 합니다."); + throw new IllegalArgumentException("당첨 번호는 6개를 입력해야 합니다."); } - List numbers = parseNumbers(splits); - validateNumbers(numbers); - Collections.sort(numbers); - return new JackpotGenerator(Collections.unmodifiableList(numbers)); - } - private static List parseNumbers(String[] splits) { - List numbers = new ArrayList<>(); + Set numbers = new TreeSet<>(); for (String s : splits) { + int number; try { - numbers.add(Integer.parseInt(s.trim())); + number = Integer.parseInt(s.trim()); } catch (NumberFormatException e) { throw new IllegalArgumentException("당첨 번호는 숫자여야 합니다."); } + validateNumberRange(number); + numbers.add(number); } - return numbers; - } - - private static void validateNumbers(List numbers) { - Set uniqueNumbers = new HashSet<>(numbers); - if (uniqueNumbers.size() != 6) { + if (numbers.size() != 6) { throw new IllegalArgumentException("당첨 번호는 중복될 수 없습니다."); } - for (int number : numbers) { - if (number < 1 || number > 45) { - throw new IllegalArgumentException("당첨 번호는 1과 45 사이의 숫자여야 합니다."); - } + return numbers; + } + + private static void validateNumberRange(int number) { + if (number < 1 || number > 45) { + throw new IllegalArgumentException("당첨 번호는 1과 45 사이의 숫자여야 합니다."); } } From dae1be8cae95fcb61c8251f3978754545003f571 Mon Sep 17 00:00:00 2001 From: nonactress Date: Tue, 7 Oct 2025 20:10:55 +0900 Subject: [PATCH 39/42] refactor : move print method from inputView to outputView --- src/main/java/Controller/LottoController.java | 53 ++++++++++++++----- src/main/java/View/InputView.java | 26 ++------- src/main/java/View/OutView.java | 25 +++++++++ 3 files changed, 69 insertions(+), 35 deletions(-) diff --git a/src/main/java/Controller/LottoController.java b/src/main/java/Controller/LottoController.java index 9ab09c84f..b5fd98611 100644 --- a/src/main/java/Controller/LottoController.java +++ b/src/main/java/Controller/LottoController.java @@ -11,27 +11,56 @@ public class LottoController { public void startLotto() { InputView inputView = new InputView(); + OutView outView = new OutView(); - int money = inputView.getMoney(); - int manualLottoCount = inputView.getManualLottoCount(); + try { + int money = getValidMoney(inputView, outView); - List rawNumbersList = inputView.manualLotto(manualLottoCount); + outView.getManualLottoCountHelp(); + int manualLottoCount = inputView.getManualLottoCount(); - Lottos lottos = new Lottos(money, rawNumbersList); + outView.promptForManualLotto(); + List rawNumbersList = inputView.manualLotto(manualLottoCount); - OutView outView = new OutView(); + Lottos lottos = new Lottos(money, rawNumbersList); + + outView.printLottosStaus(lottos.getAutoLottoCount(money, manualLottoCount), manualLottoCount); + + outView.printLottos(lottos); - outView.printLottosStaus(lottos.getAutoLottoCount(money, manualLottoCount), manualLottoCount); + outView.promptForJackpot(); + String[] winningNumber = inputView.getJackpotNumber(); - outView.printLottos(lottos); + outView.promptForBonus(); + int bonusNumber = inputView.getBonus(); - String[] winningNumber = inputView.getJackpotNumber(); - int bonusNumber = inputView.getBonus(); + Jackpot jackpot = new Jackpot(winningNumber, bonusNumber); - Jackpot jackpot = new Jackpot(winningNumber, bonusNumber); + LottoMatcher lottoMatcher = new LottoMatcher(lottos, jackpot); - LottoMatcher lottoMatcher = new LottoMatcher(lottos, jackpot); + outView.printLottoMatcher(lottoMatcher.getMatchCounts(), lottoMatcher.getRate(money)); + } catch (IllegalArgumentException e){ + System.err.println("[error]" + e.getMessage()); + } + } + + private int getValidMoney(InputView inputView, OutView outView) { + while (true) { + try { + outView.getMoneyHelp(); + int money = inputView.getMoney(); + validate(money); + return money; + } catch (IllegalArgumentException e) { + System.err.println("[ERROR] " + e.getMessage()); + } + } + } - outView.printLottoMatcher(lottoMatcher.getMatchCounts(), lottoMatcher.getRate(money)); + private void validate(int money) { + if (money < 0) + throw new IllegalArgumentException("금액이 0보다 작을 순 없습니다!"); + if (money % 1000 != 0) + throw new IllegalArgumentException("1000원 단위로 입력해주세요!"); } } diff --git a/src/main/java/View/InputView.java b/src/main/java/View/InputView.java index c6fe2329c..04d9ca687 100644 --- a/src/main/java/View/InputView.java +++ b/src/main/java/View/InputView.java @@ -9,36 +9,17 @@ public class InputView { Scanner scanner = new Scanner(System.in); public int getMoney() { - System.out.println("구입금액을 입력해 주세요."); - while (true) { - try { - int money = scanner.nextInt(); - validate(money); - return money; - } catch (InputMismatchException e) { - System.err.println("금액은 숫자로 입력해주세요!"); - scanner.nextLine(); - } catch (IllegalArgumentException e) { - System.out.println(e.getMessage()); - } - } + return scanner.nextInt(); } - private void validate(int money) { - if (money < 0) - throw new IllegalArgumentException("금액이 0보다 작을 순 없습니다!"); - if (money % 1000 != 0) - throw new IllegalArgumentException("1000원 단위로 입력해주세요!"); - } + public String[] getJackpotNumber() { - System.out.println("정답 로또번호를 입력해주세요( ','로 구분해서 입력)"); return scanner.nextLine().split(","); } public int getManualLottoCount() { - System.out.println("수동으로 구매할 로또 수를 입력해 주세요."); return scanner.nextInt(); } @@ -46,7 +27,7 @@ public List manualLotto(int manualLottoNumber) { List manualLottoLines = new ArrayList<>(); scanner.nextLine(); - System.out.println("수동으로 구매할 번호를 입력해 주세요."); + for (int i = 0; i < manualLottoNumber; i++) { manualLottoLines.add(scanner.nextLine()); } @@ -54,7 +35,6 @@ public List manualLotto(int manualLottoNumber) { } public int getBonus() { - System.out.println("보너스 볼을 입력해 주세요."); return scanner.nextInt(); } } diff --git a/src/main/java/View/OutView.java b/src/main/java/View/OutView.java index 4dfdb68e6..e1a68ac62 100644 --- a/src/main/java/View/OutView.java +++ b/src/main/java/View/OutView.java @@ -28,4 +28,29 @@ public void printLottos(Lottos lottos) { System.out.println(lotto); } } + + public void getMoneyHelp() { + System.out.println("구입금액을 입력해 주세요."); + } + + public void getManualLottoCountHelp() { + System.out.println("수동으로 구매할 로또 수를 입력해 주세요."); + } + + public void promptForManualLotto() { + System.out.println("수동으로 구매할 번호를 입력해 주세요."); + } + + public void promptForJackpot() { + System.out.println("정답 로또번호를 입력해주세요( ','로 구분해서 입력)"); + } + + public void promptForBonus() { + System.out.println("보너스 볼을 입력해 주세요."); + } + + public void printError(IllegalArgumentException e) + { + System.out.println("[ERROR] : " + e); + } } From c341fa72e8441df1d2a672e126be27c65df06632 Mon Sep 17 00:00:00 2001 From: nonactress Date: Tue, 7 Oct 2025 23:30:20 +0900 Subject: [PATCH 40/42] refactor : ManualLotto validate rogic --- src/main/java/Controller/LottoController.java | 20 +++++++++++++----- src/main/java/Model/Lottos.java | 21 ++++++++++--------- src/main/java/Model/ManualLotto.java | 10 ++++++--- src/main/java/View/InputView.java | 2 ++ src/main/java/View/OutView.java | 5 +++++ 5 files changed, 40 insertions(+), 18 deletions(-) diff --git a/src/main/java/Controller/LottoController.java b/src/main/java/Controller/LottoController.java index b5fd98611..3729d1ffd 100644 --- a/src/main/java/Controller/LottoController.java +++ b/src/main/java/Controller/LottoController.java @@ -1,8 +1,6 @@ package Controller; -import Model.Jackpot; -import Model.LottoMatcher; -import Model.Lottos; +import Model.*; import View.InputView; import View.OutView; @@ -22,9 +20,21 @@ public void startLotto() { outView.promptForManualLotto(); List rawNumbersList = inputView.manualLotto(manualLottoCount); - Lottos lottos = new Lottos(money, rawNumbersList); + Lottos lottos = new Lottos(); - outView.printLottosStaus(lottos.getAutoLottoCount(money, manualLottoCount), manualLottoCount); + for (String s : rawNumbersList) { + try{ + lottos.addLotto(new ManualLotto(s)); + } + catch (IllegalArgumentException e) + { + System.err.println("[error] :" + e.getMessage()); + } + } + int autoLottoCount = lottos.getAutoLottoCount(money, manualLottoCount); + lottos.makeAutoLotto(autoLottoCount); + + outView.printLottosStaus(autoLottoCount, manualLottoCount); outView.printLottos(lottos); diff --git a/src/main/java/Model/Lottos.java b/src/main/java/Model/Lottos.java index 97e8fff39..d1126e739 100644 --- a/src/main/java/Model/Lottos.java +++ b/src/main/java/Model/Lottos.java @@ -8,25 +8,26 @@ public class Lottos implements Iterable { private List lottoList; private static final int COST_PER_TICKET = 1000; - public Lottos(int money, List manualLottoLines) { + public Lottos() { this.lottoList = new ArrayList<>(); - int manualLottosNumber = manualLottoLines.size(); + } - // 수동 로또 생성 - for (String line : manualLottoLines) { - lottoList.add(new ManualLotto(line)); - } - // 자동 로또 생성 - for (int i = 0; i < getAutoLottoCount(money,manualLottosNumber); i++) { - lottoList.add(new AutoLotto()); - } + public void addLotto (Lotto tmp){ + lottoList.add(tmp); } public int getAutoLottoCount(int money,int manualLottosNumber) { return money / COST_PER_TICKET - manualLottosNumber; } + public void makeAutoLotto(int autoLottosNumber){ + for (int i = 0; i < autoLottosNumber; i++) { + AutoLotto autoLotto = new AutoLotto(); + lottoList.add(autoLotto); + } + } + @Override public Iterator iterator() { return lottoList.iterator(); diff --git a/src/main/java/Model/ManualLotto.java b/src/main/java/Model/ManualLotto.java index ffdc98dab..716d40eb5 100644 --- a/src/main/java/Model/ManualLotto.java +++ b/src/main/java/Model/ManualLotto.java @@ -28,9 +28,13 @@ private List parseNumbers(String rawNumbers) { } private void validate(List numbers) { - validateSize(numbers); - validateDuplicates(numbers); - validateNumberRange(numbers); + try { + validateSize(numbers); + validateDuplicates(numbers); + validateNumberRange(numbers); + } catch (IllegalArgumentException e) { + System.err.println("[error]" + e.getMessage()); + } } private void validateSize(List numbers) { diff --git a/src/main/java/View/InputView.java b/src/main/java/View/InputView.java index 04d9ca687..36fc758db 100644 --- a/src/main/java/View/InputView.java +++ b/src/main/java/View/InputView.java @@ -37,4 +37,6 @@ public List manualLotto(int manualLottoNumber) { public int getBonus() { return scanner.nextInt(); } + + } diff --git a/src/main/java/View/OutView.java b/src/main/java/View/OutView.java index e1a68ac62..fba89435f 100644 --- a/src/main/java/View/OutView.java +++ b/src/main/java/View/OutView.java @@ -53,4 +53,9 @@ public void printError(IllegalArgumentException e) { System.out.println("[ERROR] : " + e); } + + public void errorPrint(IllegalArgumentException e) + { + System.err.println("[error]" + e.getMessage()); + } } From acc8b99822b636669a7412d24b8cb74031ffeb28 Mon Sep 17 00:00:00 2001 From: nonactress Date: Wed, 8 Oct 2025 22:53:53 +0900 Subject: [PATCH 41/42] refactor : get and check ManualLotto one by one --- src/main/java/Controller/LottoController.java | 63 ++++++++++--------- src/main/java/Model/ManualLotto.java | 4 -- src/main/java/View/InputView.java | 14 ++--- 3 files changed, 39 insertions(+), 42 deletions(-) diff --git a/src/main/java/Controller/LottoController.java b/src/main/java/Controller/LottoController.java index 3729d1ffd..f04222dbf 100644 --- a/src/main/java/Controller/LottoController.java +++ b/src/main/java/Controller/LottoController.java @@ -11,47 +11,40 @@ public void startLotto() { InputView inputView = new InputView(); OutView outView = new OutView(); - try { - int money = getValidMoney(inputView, outView); - outView.getManualLottoCountHelp(); - int manualLottoCount = inputView.getManualLottoCount(); + int money = getValidMoney(inputView, outView); - outView.promptForManualLotto(); - List rawNumbersList = inputView.manualLotto(manualLottoCount); + outView.getManualLottoCountHelp(); + int manualLottoCount = inputView.getManualLottoCount(); - Lottos lottos = new Lottos(); + outView.promptForManualLotto(); + Lottos lottos = new Lottos(); - for (String s : rawNumbersList) { - try{ - lottos.addLotto(new ManualLotto(s)); - } - catch (IllegalArgumentException e) - { - System.err.println("[error] :" + e.getMessage()); - } - } - int autoLottoCount = lottos.getAutoLottoCount(money, manualLottoCount); - lottos.makeAutoLotto(autoLottoCount); + inputView.resetBuffer(); + for (int i = 0; i < manualLottoCount; i++) { + Lotto validManualLotto = getValidManualLotto(inputView); + lottos.addLotto(validManualLotto); + } - outView.printLottosStaus(autoLottoCount, manualLottoCount); + int autoLottoCount = lottos.getAutoLottoCount(money, manualLottoCount); + lottos.makeAutoLotto(autoLottoCount); - outView.printLottos(lottos); + outView.printLottosStaus(autoLottoCount, manualLottoCount); - outView.promptForJackpot(); - String[] winningNumber = inputView.getJackpotNumber(); + outView.printLottos(lottos); - outView.promptForBonus(); - int bonusNumber = inputView.getBonus(); + outView.promptForJackpot(); + String[] winningNumber = inputView.getJackpotNumber(); - Jackpot jackpot = new Jackpot(winningNumber, bonusNumber); + outView.promptForBonus(); + int bonusNumber = inputView.getBonus(); - LottoMatcher lottoMatcher = new LottoMatcher(lottos, jackpot); + Jackpot jackpot = new Jackpot(winningNumber, bonusNumber); + + LottoMatcher lottoMatcher = new LottoMatcher(lottos, jackpot); + + outView.printLottoMatcher(lottoMatcher.getMatchCounts(), lottoMatcher.getRate(money)); - outView.printLottoMatcher(lottoMatcher.getMatchCounts(), lottoMatcher.getRate(money)); - } catch (IllegalArgumentException e){ - System.err.println("[error]" + e.getMessage()); - } } private int getValidMoney(InputView inputView, OutView outView) { @@ -67,6 +60,16 @@ private int getValidMoney(InputView inputView, OutView outView) { } } + private Lotto getValidManualLotto(InputView inputView){ + while(true){ + try{ + return new ManualLotto(inputView.manualLotto()); + } catch (IllegalArgumentException e) { + System.err.println("[ERROR] " + e.getMessage()); + } + } + } + private void validate(int money) { if (money < 0) throw new IllegalArgumentException("금액이 0보다 작을 순 없습니다!"); diff --git a/src/main/java/Model/ManualLotto.java b/src/main/java/Model/ManualLotto.java index 716d40eb5..1e79aa044 100644 --- a/src/main/java/Model/ManualLotto.java +++ b/src/main/java/Model/ManualLotto.java @@ -28,13 +28,9 @@ private List parseNumbers(String rawNumbers) { } private void validate(List numbers) { - try { validateSize(numbers); validateDuplicates(numbers); validateNumberRange(numbers); - } catch (IllegalArgumentException e) { - System.err.println("[error]" + e.getMessage()); - } } private void validateSize(List numbers) { diff --git a/src/main/java/View/InputView.java b/src/main/java/View/InputView.java index 36fc758db..96fad8972 100644 --- a/src/main/java/View/InputView.java +++ b/src/main/java/View/InputView.java @@ -18,20 +18,18 @@ public String[] getJackpotNumber() { return scanner.nextLine().split(","); } + public void resetBuffer() + { + scanner.nextLine(); + } public int getManualLottoCount() { return scanner.nextInt(); } - public List manualLotto(int manualLottoNumber) { - List manualLottoLines = new ArrayList<>(); - - scanner.nextLine(); + public String manualLotto() { - for (int i = 0; i < manualLottoNumber; i++) { - manualLottoLines.add(scanner.nextLine()); - } - return manualLottoLines; + return scanner.nextLine(); } public int getBonus() { From 36937b06eb835d5e83552f420c36cebf94f83c4f Mon Sep 17 00:00:00 2001 From: nonactress Date: Wed, 8 Oct 2025 23:45:44 +0900 Subject: [PATCH 42/42] refactor : using return value of method 'add' --- src/main/java/Controller/LottoController.java | 10 +++++++--- src/main/java/Model/JackpotGenerator.java | 9 +++------ 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/main/java/Controller/LottoController.java b/src/main/java/Controller/LottoController.java index f04222dbf..1b2e07ec6 100644 --- a/src/main/java/Controller/LottoController.java +++ b/src/main/java/Controller/LottoController.java @@ -39,11 +39,15 @@ public void startLotto() { outView.promptForBonus(); int bonusNumber = inputView.getBonus(); - Jackpot jackpot = new Jackpot(winningNumber, bonusNumber); + try { + Jackpot jackpot = new Jackpot(winningNumber, bonusNumber); - LottoMatcher lottoMatcher = new LottoMatcher(lottos, jackpot); + LottoMatcher lottoMatcher = new LottoMatcher(lottos, jackpot); - outView.printLottoMatcher(lottoMatcher.getMatchCounts(), lottoMatcher.getRate(money)); + outView.printLottoMatcher(lottoMatcher.getMatchCounts(), lottoMatcher.getRate(money)); + } catch (IllegalArgumentException e) { + System.err.println("[ERROR] " + e.getMessage()); + } } diff --git a/src/main/java/Model/JackpotGenerator.java b/src/main/java/Model/JackpotGenerator.java index 9b5666823..72fae504f 100644 --- a/src/main/java/Model/JackpotGenerator.java +++ b/src/main/java/Model/JackpotGenerator.java @@ -22,7 +22,6 @@ private static Set parseAndValidateNumbers(String[] splits) { if (splits.length != 6) { throw new IllegalArgumentException("당첨 번호는 6개를 입력해야 합니다."); } - Set numbers = new TreeSet<>(); for (String s : splits) { int number; @@ -32,12 +31,10 @@ private static Set parseAndValidateNumbers(String[] splits) { throw new IllegalArgumentException("당첨 번호는 숫자여야 합니다."); } validateNumberRange(number); - numbers.add(number); - } - if (numbers.size() != 6) { - throw new IllegalArgumentException("당첨 번호는 중복될 수 없습니다."); + if (!numbers.add(number)) { + throw new IllegalArgumentException("당첨 번호는 중복될 수 없습니다."); + } } - return numbers; }