diff --git a/README.md b/README.md new file mode 100644 index 00000000..9708dad1 --- /dev/null +++ b/README.md @@ -0,0 +1,66 @@ +# 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 + โ”‚ โ”œโ”€โ”€ Lotto.java # ๋กœ๋˜ 1์žฅ + โ”‚ โ”œโ”€โ”€ Lottos.java # ๋ฐœ๊ธ‰๋œ ์ „์ฒด ๋กœ๋˜ + โ”‚ โ”œโ”€โ”€ Jackpot.java # ๋‹น์ฒจ ๋ฒˆํ˜ธ์™€ ๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ + โ”‚ โ”œโ”€โ”€ LottoMatcher.java # ๋กœ๋˜ ๋‹น์ฒจ ์—ฌ๋ถ€ ํ™•์ธ ๋ฐ ํ†ต๊ณ„ ๊ณ„์‚ฐ + โ”‚ โ”œโ”€โ”€ PrizeMoney.java # ๋“ฑ์ˆ˜๋ณ„ ์ƒ๊ธˆ Enum + โ”‚ โ”œโ”€โ”€ AutoLotto.java # ์ž๋™ ๋กœ๋˜ ๋ฒˆํ˜ธ ์ƒ์„ฑ + โ”‚ โ””โ”€โ”€ ManualLotto.java # ์ˆ˜๋™ ๋กœ๋˜ ๋ฒˆํ˜ธ ๊ด€๋ฆฌ + โ””โ”€โ”€ View + โ”œโ”€โ”€ InputView.java # ์‚ฌ์šฉ์ž ์ž…๋ ฅ ์ฒ˜๋ฆฌ + โ””โ”€โ”€ OutView.java # ๊ฒฐ๊ณผ ์ถœ๋ ฅ ๋‹ด๋‹น + +``` + +## โš™๏ธ ํ•ต์‹ฌ ๋กœ์ง ์ƒ์„ธ + +### 1. ๋กœ๋˜ ์ƒ์„ฑ (`Lottos.java`) + +- `Lottos` ํด๋ž˜์Šค๋Š” ์‚ฌ์šฉ์ž๊ฐ€ ๊ตฌ๋งคํ•œ ๋ชจ๋“  ๋กœ๋˜ ํ‹ฐ์ผ“์„ ๊ด€๋ฆฌํ•ฉ๋‹ˆ๋‹ค. +- `InputView`๋ฅผ ํ†ตํ•ด ๋ฐ›์€ ์ˆ˜๋™ ๊ตฌ๋งค ๊ฐœ์ˆ˜์™€ ๋ฒˆํ˜ธ ๋ฌธ์ž์—ด ๋ฆฌ์ŠคํŠธ๋ฅผ `ManualLotto`๋ฅผ ํ†ตํ•ด `Lotto` ๊ฐ์ฒด ๋ฆฌ์ŠคํŠธ๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. +- ์ด ๊ตฌ๋งค ๊ฐœ์ˆ˜์—์„œ ์ˆ˜๋™ ๊ตฌ๋งค ๊ฐœ์ˆ˜๋ฅผ ๋บ€ ๋งŒํผ `AutoLotto`๋ฅผ ํ†ตํ•ด ์ž๋™์œผ๋กœ ๋กœ๋˜ ๋ฒˆํ˜ธ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. +- `AutoLotto`๋Š” `Collections.shuffle`์„ ํ™œ์šฉํ•˜์—ฌ 1๋ถ€ํ„ฐ 45๊นŒ์ง€์˜ ์ˆซ์ž ์ค‘ 6๊ฐœ๋ฅผ ๋ฌด์ž‘์œ„๋กœ ์„ ํƒํ•˜์—ฌ ์ค‘๋ณต ์—†๋Š” ๋ฒˆํ˜ธ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. + +### 2. ๋‹น์ฒจ ํ™•์ธ (`LottoMatcher.java`) + +`LottoMatcher`๋Š” ๋กœ๋˜ ๊ฒŒ์ž„์˜ ๋‹น์ฒจ ๊ฒฐ๊ณผ๋ฅผ ํŒ๋ณ„ํ•˜๊ณ  ํ†ต๊ณ„๋ฅผ ๋‚ด๋Š”, ๊ฐ€์žฅ ์ค‘์š”ํ•œ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์„ ๋‹ด๋‹นํ•˜๋Š” ํด๋ž˜์Šค์ž…๋‹ˆ๋‹ค. ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋˜๋Š” ์‹œ์ ์— ๋ชจ๋“  ํ•ต์‹ฌ ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•˜์—ฌ ๋‚ด๋ถ€์— ๊ฒฐ๊ณผ๋ฅผ ์ €์žฅํ•˜๊ณ , ์™ธ๋ถ€์—๋Š” ์š”์ฒญ์— ๋”ฐ๋ผ ๊ณ„์‚ฐ๋œ ๊ฒฐ๊ณผ๋ฅผ ์ฆ‰์‹œ ์ œ๊ณตํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ์„ค๊ณ„๋˜์—ˆ์Šต๋‹ˆ๋‹ค. + +#### ๋™์ž‘ ์›๋ฆฌ + +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()`๋กœ ๊ณ„์‚ฐ๋œ ์ด์ƒ๊ธˆ์„ ์‚ฌ์šฉ์ž์˜ ์ด ๊ตฌ๋งค ๊ธˆ์•ก์œผ๋กœ ๋‚˜๋ˆ„์–ด ์ตœ์ข… ์ˆ˜์ต๋ฅ ์„ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค. diff --git a/src/main/java/Application.java b/src/main/java/Application.java new file mode 100644 index 00000000..23aa0f0a --- /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(); + } +} diff --git a/src/main/java/Controller/LottoController.java b/src/main/java/Controller/LottoController.java new file mode 100644 index 00000000..1b2e07ec --- /dev/null +++ b/src/main/java/Controller/LottoController.java @@ -0,0 +1,83 @@ +package Controller; + +import Model.*; +import View.InputView; +import View.OutView; + +import java.util.List; + +public class LottoController { + public void startLotto() { + InputView inputView = new InputView(); + OutView outView = new OutView(); + + + int money = getValidMoney(inputView, outView); + + outView.getManualLottoCountHelp(); + int manualLottoCount = inputView.getManualLottoCount(); + + outView.promptForManualLotto(); + Lottos lottos = new Lottos(); + + inputView.resetBuffer(); + for (int i = 0; i < manualLottoCount; i++) { + Lotto validManualLotto = getValidManualLotto(inputView); + lottos.addLotto(validManualLotto); + } + + int autoLottoCount = lottos.getAutoLottoCount(money, manualLottoCount); + lottos.makeAutoLotto(autoLottoCount); + + outView.printLottosStaus(autoLottoCount, manualLottoCount); + + outView.printLottos(lottos); + + outView.promptForJackpot(); + String[] winningNumber = inputView.getJackpotNumber(); + + outView.promptForBonus(); + int bonusNumber = inputView.getBonus(); + + try { + Jackpot jackpot = new Jackpot(winningNumber, bonusNumber); + + 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()); + } + } + } + + 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๋ณด๋‹ค ์ž‘์„ ์ˆœ ์—†์Šต๋‹ˆ๋‹ค!"); + if (money % 1000 != 0) + throw new IllegalArgumentException("1000์› ๋‹จ์œ„๋กœ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”!"); + } +} diff --git a/src/main/java/Model/AutoLotto.java b/src/main/java/Model/AutoLotto.java new file mode 100644 index 00000000..954d1cc4 --- /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); + } + +} diff --git a/src/main/java/Model/Jackpot.java b/src/main/java/Model/Jackpot.java new file mode 100644 index 00000000..191159e9 --- /dev/null +++ b/src/main/java/Model/Jackpot.java @@ -0,0 +1,38 @@ +package Model; + +import java.util.Collections; +import java.util.List; + +public class Jackpot { + private List jackpot; + private 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; + } + + @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 00000000..72fae504 --- /dev/null +++ b/src/main/java/Model/JackpotGenerator.java @@ -0,0 +1,50 @@ +package Model; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Set; +import java.util.TreeSet; + +public class JackpotGenerator { + private final List number; + + private JackpotGenerator(List numbers) { + this.number = 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๊ฐœ๋ฅผ ์ž…๋ ฅํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค."); + } + Set numbers = new TreeSet<>(); + for (String s : splits) { + int number; + try { + number = Integer.parseInt(s.trim()); + } catch (NumberFormatException e) { + throw new IllegalArgumentException("๋‹น์ฒจ ๋ฒˆํ˜ธ๋Š” ์ˆซ์ž์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค."); + } + validateNumberRange(number); + if (!numbers.add(number)) { + throw new IllegalArgumentException("๋‹น์ฒจ ๋ฒˆํ˜ธ๋Š” ์ค‘๋ณต๋  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."); + } + } + return numbers; + } + + private static void validateNumberRange(int number) { + if (number < 1 || number > 45) { + throw new IllegalArgumentException("๋‹น์ฒจ ๋ฒˆํ˜ธ๋Š” 1๊ณผ 45 ์‚ฌ์ด์˜ ์ˆซ์ž์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค."); + } + } + + public List getNumber() { + return number; + } +} diff --git a/src/main/java/Model/Lotto.java b/src/main/java/Model/Lotto.java new file mode 100644 index 00000000..b6b96630 --- /dev/null +++ b/src/main/java/Model/Lotto.java @@ -0,0 +1,17 @@ +package Model; + +import java.util.Collections; +import java.util.List; + +public class Lotto { + protected List numbers; + + public List getLotto() { + return Collections.unmodifiableList(numbers); + } + + @Override + public String toString() { + return numbers.toString(); + } +} diff --git a/src/main/java/Model/LottoMatcher.java b/src/main/java/Model/LottoMatcher.java new file mode 100644 index 00000000..3010186c --- /dev/null +++ b/src/main/java/Model/LottoMatcher.java @@ -0,0 +1,61 @@ +package Model; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class LottoMatcher { + private final Map matchCounts; + + public LottoMatcher(Lottos lottos, Jackpot jackpot) { + this.matchCounts = new HashMap<>(); + + for (Lotto lotto : lottos) { + int matches = countMatches(jackpot.getJackpot(), lotto.getLotto()); + 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) { + 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 Map getMatchCounts() { + return matchCounts; + } + + public double calculateTotalEarnings() { + + 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 calculateTotalEarnings() / money; + } + +} diff --git a/src/main/java/Model/Lottos.java b/src/main/java/Model/Lottos.java new file mode 100644 index 00000000..d1126e73 --- /dev/null +++ b/src/main/java/Model/Lottos.java @@ -0,0 +1,35 @@ +package Model; + +import java.util.*; +import java.util.function.Consumer; + +// 1. ์ œ๋„ค๋ฆญ์„ ์‚ฌ์šฉํ•˜์—ฌ Iterable๋กœ ํƒ€์ž… ๋ช…์‹œ +public class Lottos implements Iterable { + private List lottoList; + private static final int COST_PER_TICKET = 1000; + + public Lottos() { + this.lottoList = new ArrayList<>(); + } + + + 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 new file mode 100644 index 00000000..1e79aa04 --- /dev/null +++ b/src/main/java/Model/ManualLotto.java @@ -0,0 +1,56 @@ +package Model; + +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) { + List parsedNumbers = parseNumbers(rawNumbers); + + validate(parsedNumbers); + + this.numbers = parsedNumbers; + } + + 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 numbers) { + validateSize(numbers); + validateDuplicates(numbers); + validateNumberRange(numbers); + } + + private void validateSize(List numbers) { + if (numbers.size() != 6) { + throw new IllegalArgumentException("๋กœ๋˜ ๋ฒˆํ˜ธ๋Š” 6๊ฐœ์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค."); + } + } + + 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 ์‚ฌ์ด์˜ ์ˆซ์ž์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค."); + } + } + } +} diff --git a/src/main/java/Model/PrizeMoney.java b/src/main/java/Model/PrizeMoney.java new file mode 100644 index 00000000..507a3582 --- /dev/null +++ b/src/main/java/Model/PrizeMoney.java @@ -0,0 +1,19 @@ +package Model; + +public enum PrizeMoney { + THREE(5000), + FOUR(50000), + FIVE(1500000), + SIX(2000000000), + BONUS(30000000); + + private final int money; + + private PrizeMoney(int money) { + this.money = money; + } + + public int getMoney() { + return money; + } +} diff --git a/src/main/java/View/InputView.java b/src/main/java/View/InputView.java new file mode 100644 index 00000000..96fad897 --- /dev/null +++ b/src/main/java/View/InputView.java @@ -0,0 +1,40 @@ +package View; + +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() { + return scanner.nextInt(); + } + + + + public String[] getJackpotNumber() { + return scanner.nextLine().split(","); + } + + public void resetBuffer() + { + scanner.nextLine(); + } + + public int getManualLottoCount() { + return scanner.nextInt(); + } + + public String manualLotto() { + + return scanner.nextLine(); + } + + public int getBonus() { + return scanner.nextInt(); + } + + +} diff --git a/src/main/java/View/OutView.java b/src/main/java/View/OutView.java new file mode 100644 index 00000000..fba89435 --- /dev/null +++ b/src/main/java/View/OutView.java @@ -0,0 +1,61 @@ +package View; + +import Model.Lotto; +import Model.Lottos; + +import java.util.Map; + +public class OutView { + + public void printLottoMatcher(Map matchCounts, double rate) { + System.out.println("๋‹น์ฒจ ํ†ต๊ณ„"); + System.out.println("------"); + 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) { + 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); + } + + public void errorPrint(IllegalArgumentException e) + { + System.err.println("[error]" + e.getMessage()); + } +}