diff --git a/README.md b/README.md index 5fa2560b46..13907455d5 100644 --- a/README.md +++ b/README.md @@ -1 +1,106 @@ # java-lotto-precourse + +## ๐Ÿ‘€ ๋ฏธ์…˜ ์ƒ์„ธ + +๊ฐ„๋‹จํ•œ ๋กœ๋˜ ๋ฐœ๋งค๊ธฐ๋ฅผ ๊ตฌํ˜„ํ•œ๋‹ค. + +๋กœ๋˜ ๋ฒˆํ˜ธ์˜ ์ˆซ์ž ๋ฒ”์œ„๋Š” 1~45๊นŒ์ง€์ด๋‹ค.
+1๊ฐœ์˜ ๋กœ๋˜๋ฅผ ๋ฐœํ–‰ํ•  ๋•Œ ์ค‘๋ณต๋˜์ง€ ์•Š๋Š” 6๊ฐœ์˜ ์ˆซ์ž๋ฅผ ๋ฝ‘๋Š”๋‹ค.
+๋‹น์ฒจ ๋ฒˆํ˜ธ ์ถ”์ฒจ ์‹œ ์ค‘๋ณต๋˜์ง€ ์•Š๋Š” ์ˆซ์ž 6๊ฐœ์™€ ๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ 1๊ฐœ๋ฅผ ๋ฝ‘๋Š”๋‹ค.
+๋‹น์ฒจ์€ 1๋“ฑ๋ถ€ํ„ฐ 5๋“ฑ๊นŒ์ง€ ์žˆ๋‹ค.
+๋‹น์ฒจ ๊ธฐ์ค€๊ณผ ๊ธˆ์•ก์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค. +- 1๋“ฑ: 6๊ฐœ ๋ฒˆํ˜ธ ์ผ์น˜ / 2,000,000,000์› +- 2๋“ฑ: 5๊ฐœ ๋ฒˆํ˜ธ + ๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ ์ผ์น˜ / 30,000,000์› +- 3๋“ฑ: 5๊ฐœ ๋ฒˆํ˜ธ ์ผ์น˜ / 1,500,000์› +- 4๋“ฑ: 4๊ฐœ ๋ฒˆํ˜ธ ์ผ์น˜ / 50,000์› +- 5๋“ฑ: 3๊ฐœ ๋ฒˆํ˜ธ ์ผ์น˜ / 5,000์› + +๋กœ๋˜ ๊ตฌ์ž… ๊ธˆ์•ก์„ ์ž…๋ ฅํ•˜๋ฉด ๊ตฌ์ž… ๊ธˆ์•ก์— ํ•ด๋‹นํ•˜๋Š” ๋งŒํผ ๋กœ๋˜๋ฅผ ๋ฐœํ–‰ํ•ด์•ผ ํ•œ๋‹ค.
+๋กœ๋˜ 1์žฅ์˜ ๊ฐ€๊ฒฉ์€ 1,000์›์ด๋‹ค.
+๋‹น์ฒจ ๋ฒˆํ˜ธ์™€ ๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค.
+์‚ฌ์šฉ์ž๊ฐ€ ๊ตฌ๋งคํ•œ ๋กœ๋˜ ๋ฒˆํ˜ธ์™€ ๋‹น์ฒจ ๋ฒˆํ˜ธ๋ฅผ ๋น„๊ตํ•˜์—ฌ ๋‹น์ฒจ ๋‚ด์—ญ ๋ฐ ์ˆ˜์ต๋ฅ ์„ ์ถœ๋ ฅํ•˜๊ณ  ๋กœ๋˜ ๊ฒŒ์ž„์„ ์ข…๋ฃŒํ•œ๋‹ค.
+ +์‚ฌ์šฉ์ž๊ฐ€ ์ž˜๋ชป๋œ ๊ฐ’์„ ์ž…๋ ฅํ•  ๊ฒฝ์šฐ `IllegalArgumentException`์„ ๋ฐœ์ƒ์‹œํ‚ค๊ณ , "[ERROR]"๋กœ ์‹œ์ž‘ํ•˜๋Š” ์—๋Ÿฌ ๋ฉ”์‹œ์ง€๋ฅผ ์ถœ๋ ฅ ํ›„ ๊ทธ ๋ถ€๋ถ„๋ถ€ํ„ฐ ์ž…๋ ฅ์„ ๋‹ค์‹œ ๋ฐ›๋Š”๋‹ค. +Exception์ด ์•„๋‹Œ `IllegalArgumentException`, `IllegalStateException` ๋“ฑ๊ณผ ๊ฐ™์€ ๋ช…ํ™•ํ•œ ์œ ํ˜•์„ ์ฒ˜๋ฆฌํ•œ๋‹ค. + + +### ์ž…๋ ฅ +1. ๋กœ๋˜ ๊ตฌ์ž… ๊ธˆ์•ก์„ ์ž…๋ ฅ ๋ฐ›๋Š”๋‹ค. ๊ตฌ์ž… ๊ธˆ์•ก์€ 1,000์› ๋‹จ์œ„๋กœ ์ž…๋ ฅ ๋ฐ›์œผ๋ฉฐ 1,000์›์œผ๋กœ ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง€์ง€ ์•Š๋Š” ๊ฒฝ์šฐ ์˜ˆ์™ธ ์ฒ˜๋ฆฌํ•œ๋‹ค. +``` +14000 +``` +2. ๋‹น์ฒจ ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅ ๋ฐ›๋Š”๋‹ค. ๋ฒˆํ˜ธ๋Š” ์‰ผํ‘œ(,)๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๊ตฌ๋ถ„ํ•œ๋‹ค. +``` +1,2,3,4,5,6 +``` +3. ๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅ ๋ฐ›๋Š”๋‹ค. +``` +7 +``` + +### ์ถœ๋ ฅ + +- ๋ฐœํ–‰ํ•œ ๋กœ๋˜ ์ˆ˜๋Ÿ‰ ๋ฐ ๋ฒˆํ˜ธ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. ๋กœ๋˜ ๋ฒˆํ˜ธ๋Š” ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜์—ฌ ๋ณด์—ฌ์ค€๋‹ค +``` +8๊ฐœ๋ฅผ ๊ตฌ๋งคํ–ˆ์Šต๋‹ˆ๋‹ค. +[8, 21, 23, 41, 42, 43] +[3, 5, 11, 16, 32, 38] +[7, 11, 16, 35, 36, 44] +[1, 8, 11, 31, 41, 42] +[13, 14, 16, 38, 42, 45] +[7, 11, 30, 40, 42, 43] +[2, 13, 22, 32, 38, 45] +[1, 3, 5, 14, 22, 45] +``` +- ๋‹น์ฒจ ๋‚ด์—ญ์„ ์ถœ๋ ฅํ•œ๋‹ค. +``` +3๊ฐœ ์ผ์น˜ (5,000์›) - 1๊ฐœ +4๊ฐœ ์ผ์น˜ (50,000์›) - 0๊ฐœ +5๊ฐœ ์ผ์น˜ (1,500,000์›) - 0๊ฐœ +5๊ฐœ ์ผ์น˜, ๋ณด๋„ˆ์Šค ๋ณผ ์ผ์น˜ (30,000,000์›) - 0๊ฐœ +6๊ฐœ ์ผ์น˜ (2,000,000,000์›) - 0๊ฐœ +``` +- ์ˆ˜์ต๋ฅ ์€ ์†Œ์ˆ˜์  ๋‘˜์งธ ์ž๋ฆฌ์—์„œ ๋ฐ˜์˜ฌ๋ฆผํ•œ๋‹ค. (ex. 100.0%, 51.5%, 1,000,000.0%) +``` +์ด ์ˆ˜์ต๋ฅ ์€ 62.5%์ž…๋‹ˆ๋‹ค. +``` +์˜ˆ์™ธ ์ƒํ™ฉ ์‹œ ์—๋Ÿฌ ๋ฌธ๊ตฌ๋ฅผ ์ถœ๋ ฅํ•ด์•ผ ํ•œ๋‹ค. ๋‹จ, ์—๋Ÿฌ ๋ฌธ๊ตฌ๋Š” "[ERROR]"๋กœ ์‹œ์ž‘ํ•ด์•ผ ํ•œ๋‹ค. +``` +[ERROR] ๋กœ๋˜ ๋ฒˆํ˜ธ๋Š” 1๋ถ€ํ„ฐ 45 ์‚ฌ์ด์˜ ์ˆซ์ž์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค. +``` + + +## ๐ŸŒˆ ๊ธฐ๋Šฅ ๊ตฌํ˜„ ๋ชฉ๋ก + +- [x] **๋กœ๋˜ ๊ตฌ์ž… ๊ธˆ์•ก ์ž…๋ ฅ ๋ฐ›๊ธฐ** + - [x] 1,000์› ๋‹จ์œ„๋กœ ์ž…๋ ฅ๋œ ๊ธˆ์•ก์ธ์ง€ ํ™•์ธ + - [x] ์ž…๋ ฅ ๊ธˆ์•ก์ด 1,000์›์œผ๋กœ ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง€์ง€ ์•Š๋Š”์ง€ ๊ฒ€์ฆ + +- [x] **๋กœ๋˜ ๋ฒˆํ˜ธ ์ƒ์„ฑ ๋ฐ ๋ฐœํ–‰** + - [x] ์ž…๋ ฅ๋œ ๊ธˆ์•ก์— ๋”ฐ๋ผ ๋ฐœํ–‰ํ•  ๋กœ๋˜ ์ˆ˜๋Ÿ‰ ๊ณ„์‚ฐ + - [x] ๋กœ๋˜ ๋ฒˆํ˜ธ ๋ฒ”์œ„(1~45) ๋‚ด์—์„œ ์ค‘๋ณต๋˜์ง€ ์•Š๋Š” 6๊ฐœ์˜ ๋ฒˆํ˜ธ ์ƒ์„ฑ + - [x] ์ƒ์„ฑ๋œ ๋กœ๋˜ ๋ฒˆํ˜ธ๋ฅผ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ + - [x] ๋ฐœํ–‰๋œ ๋กœ๋˜ ์ˆ˜๋Ÿ‰๊ณผ ๊ฐ๊ฐ์˜ ๋กœ๋˜ ๋ฒˆํ˜ธ๋ฅผ ์ถœ๋ ฅ + +- [x] **๋‹น์ฒจ ๋ฒˆํ˜ธ์™€ ๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ ์ž…๋ ฅ ๋ฐ›๊ธฐ** + - [x] ๋‹น์ฒจ ๋ฒˆํ˜ธ 6๊ฐœ๋ฅผ ์‰ผํ‘œ(,)๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ ์ž…๋ ฅ ๋ฐ›๊ธฐ + - [x] ์ž…๋ ฅ๋œ ๋ฒˆํ˜ธ๋“ค์ด 1~45 ๋ฒ”์œ„ ๋‚ด์˜ ์ˆซ์ž์ธ์ง€ ๊ฒ€์ฆ + - [x] ๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ๊ฐ€ 1~45 ๋ฒ”์œ„ ๋‚ด์˜ ์ˆซ์ž์ธ์ง€ ๊ฒ€์ฆ + - [x] ๋‹น์ฒจ ๋ฒˆํ˜ธ์™€ ์ค‘๋ณต๋˜์ง€ ์•Š๋Š”์ง€ ํ™•์ธ + - [x] ์ž…๋ ฅ๊ฐ’์ด ์œ ํšจํ•˜์ง€ ์•Š๋Š”์ง€ ๊ฒ€์ฆ + +- [x] **๋กœ๋˜ ๋‹น์ฒจ ํ™•์ธ** + - [x] ์‚ฌ์šฉ์ž๊ฐ€ ๊ตฌ๋งคํ•œ ๊ฐ ๋กœ๋˜ ๋ฒˆํ˜ธ์™€ ๋‹น์ฒจ ๋ฒˆํ˜ธ๋ฅผ ๋น„๊ต + - [x] ๋‹น์ฒจ ๊ธฐ์ค€์— ๋”ฐ๋ผ 1๋“ฑ๋ถ€ํ„ฐ 5๋“ฑ๊นŒ์ง€ ๋‹น์ฒจ ์—ฌ๋ถ€์™€ ๊ฐœ์ˆ˜๋ฅผ ํŒ๋ณ„ + +- [x] **๋‹น์ฒจ ๋‚ด์—ญ ์ถœ๋ ฅ** + - [x] ๊ฐ ๋“ฑ์ˆ˜๋ณ„ ์ผ์น˜ํ•˜๋Š” ๋กœ๋˜ ๊ฐœ์ˆ˜๋ฅผ ์ถœ๋ ฅ + - [x] ๋‹น์ฒจ ๊ธˆ์•ก์„ ๊ฐ ๋“ฑ์ˆ˜๋ณ„๋กœ ์ถœ๋ ฅ ํ˜•์‹์— ๋งž๊ฒŒ ํ‘œ์‹œ + +- [x] **์ˆ˜์ต๋ฅ  ๊ณ„์‚ฐ** + - [x] ์‚ฌ์šฉ์ž๊ฐ€ ๊ตฌ๋งคํ•œ ๋กœ๋˜ ์ด ๊ธˆ์•ก๊ณผ ๋‹น์ฒจ ๊ธˆ์•ก์„ ๋น„๊ตํ•˜์—ฌ ์ˆ˜์ต๋ฅ  ๊ณ„์‚ฐ + - [x] ์ˆ˜์ต๋ฅ ์„ ์†Œ์ˆ˜์  ๋‘˜์งธ ์ž๋ฆฌ์—์„œ ๋ฐ˜์˜ฌ๋ฆผํ•˜์—ฌ ์ถœ๋ ฅ + +- [x] **์˜ˆ์™ธ ์ฒ˜๋ฆฌ** + - [x] ์ž…๋ ฅ ๊ฐ’์ด ์ž˜๋ชป๋˜์—ˆ์„ ๋•Œ `IllegalArgumentException`์„ ๋ฐœ์ƒ์‹œํ‚ค๊ณ  `[ERROR]` ๋ฉ”์‹œ์ง€๋ฅผ ์ถœ๋ ฅ + - [x] ์ž˜๋ชป๋œ ๊ฐ’์ด ์ž…๋ ฅ๋  ๊ฒฝ์šฐ ํ•ด๋‹น ๋ถ€๋ถ„๋ถ€ํ„ฐ ์ž…๋ ฅ์„ ๋‹ค์‹œ ๋ฐ›๋„๋ก ๊ตฌํ˜„ \ No newline at end of file diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java index d190922ba4..6242293005 100644 --- a/src/main/java/lotto/Application.java +++ b/src/main/java/lotto/Application.java @@ -1,7 +1,9 @@ package lotto; +import lotto.runner.Runner; + public class Application { public static void main(String[] args) { - // TODO: ํ”„๋กœ๊ทธ๋žจ ๊ตฌํ˜„ + Runner.run(); } } diff --git a/src/main/java/lotto/Lotto.java b/src/main/java/lotto/Lotto.java deleted file mode 100644 index 88fc5cf12b..0000000000 --- a/src/main/java/lotto/Lotto.java +++ /dev/null @@ -1,20 +0,0 @@ -package lotto; - -import java.util.List; - -public class Lotto { - private final List numbers; - - public Lotto(List numbers) { - validate(numbers); - this.numbers = numbers; - } - - private void validate(List numbers) { - if (numbers.size() != 6) { - throw new IllegalArgumentException("[ERROR] ๋กœ๋˜ ๋ฒˆํ˜ธ๋Š” 6๊ฐœ์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค."); - } - } - - // TODO: ์ถ”๊ฐ€ ๊ธฐ๋Šฅ ๊ตฌํ˜„ -} diff --git a/src/main/java/lotto/application/LottoGenerator.java b/src/main/java/lotto/application/LottoGenerator.java new file mode 100644 index 0000000000..06332aa660 --- /dev/null +++ b/src/main/java/lotto/application/LottoGenerator.java @@ -0,0 +1,27 @@ +package lotto.application; + +import camp.nextstep.edu.missionutils.Randoms; +import lotto.domain.Lotto; + +import java.util.List; + +public class LottoGenerator { + + private static LottoGenerator instance; + + private LottoGenerator() { + + } + + public static LottoGenerator getInstance() { + if (instance == null) { + instance = new LottoGenerator(); + } + return instance; + } + + public Lotto generate() { + List LottoNumbers = Randoms.pickUniqueNumbersInRange(1, 45, 6); + return Lotto.of(LottoNumbers); + } +} diff --git a/src/main/java/lotto/application/LottoService.java b/src/main/java/lotto/application/LottoService.java new file mode 100644 index 0000000000..688e0b8970 --- /dev/null +++ b/src/main/java/lotto/application/LottoService.java @@ -0,0 +1,55 @@ +package lotto.application; + +import lotto.domain.Lotto; +import lotto.global.LottoPrize; + +import java.util.ArrayList; +import java.util.List; + +public class LottoService { + + private static final int LOTTO_PRICE = 1000; + private static LottoService instance; + private final LottoGenerator lottoGenerator; + + private LottoService(LottoGenerator lottoGenerator) { + this.lottoGenerator = lottoGenerator; + } + + public static LottoService getInstance(LottoGenerator lottoGenerator) { + if (instance == null) { + instance = new LottoService(lottoGenerator); + } + return instance; + } + + public List buyLottos(int price) { + LottoValidator.validatePurchasePrice(price); + List lottos = new ArrayList<>(); + + for(int i = 0; i < price / LOTTO_PRICE; i++) { + lottos.add(lottoGenerator.generate()); + } + + return lottos; + } + + public LottoPrize calculatePrize(Lotto myLotto, Lotto lottoResult, int bonusNumber) { + LottoValidator.validateBonusNumber(bonusNumber, lottoResult.getNumbers()); + long matchLottoCount = myLotto.getNumbers().stream() + .filter(lottoResult.getNumbers()::contains) + .count(); + + boolean matchBonusNumber = myLotto.getNumbers().contains(bonusNumber); + + return LottoPrize.from(matchLottoCount, matchBonusNumber); + } + + public Double calculateProfitRate(List results, int purchaseQuantity) { + int totalPrize = results.stream() + .map(result -> result.getPrizeMoney().replace(",", "")) + .mapToInt(Integer::parseInt) + .sum(); + return (double) totalPrize / purchaseQuantity; + } +} diff --git a/src/main/java/lotto/application/LottoValidator.java b/src/main/java/lotto/application/LottoValidator.java new file mode 100644 index 0000000000..0c16af4152 --- /dev/null +++ b/src/main/java/lotto/application/LottoValidator.java @@ -0,0 +1,27 @@ +package lotto.application; + +import java.util.List; + +public class LottoValidator { + + private static final int LOTTO_PRICE = 1000; + private static final int MIN_NUMBER = 1; + private static final int MAX_NUMBER = 45; + + + public static void validatePurchasePrice(int price) { + if (price <= 0 || price % LOTTO_PRICE != 0) { + throw new IllegalArgumentException("[ERROR] ๊ธˆ์•ก์€ 1,000์› ๋‹จ์œ„๋กœ ์ž…๋ ฅํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค."); + } + } + + public static void validateBonusNumber(int bonusNumber, List winningNumbers) { + if (bonusNumber < MIN_NUMBER || bonusNumber > MAX_NUMBER) { + throw new IllegalArgumentException("[ERROR] ๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ๋Š” 1๋ถ€ํ„ฐ 45 ์‚ฌ์ด์˜ ์ˆซ์ž์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค."); + } + + if (winningNumbers.contains(bonusNumber)) { + throw new IllegalArgumentException("[ERROR] ๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ๋Š” ๋‹น์ฒจ ๋ฒˆํ˜ธ์™€ ์ค‘๋ณต๋  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."); + } + } +} diff --git a/src/main/java/lotto/domain/Lotto.java b/src/main/java/lotto/domain/Lotto.java new file mode 100644 index 0000000000..4a25c037b1 --- /dev/null +++ b/src/main/java/lotto/domain/Lotto.java @@ -0,0 +1,51 @@ +package lotto.domain; + +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +public class Lotto { + private final List numbers; + + public Lotto(List numbers) { + validateQuantity(numbers); + validateNumberRange(numbers); + validateDuplicate(numbers); + this.numbers = numbers; + sortNumbers(this.numbers); + } + + public static Lotto of(List numbers) { + return new Lotto(numbers); + } + + public List getNumbers() { + return numbers; + } + + private void validateQuantity(List numbers) { + if (numbers.size() != 6) { + throw new IllegalArgumentException("[ERROR] ๋กœ๋˜ ๋ฒˆํ˜ธ๋Š” 6๊ฐœ์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค."); + } + } + + private void validateNumberRange(List numbers) { + for(Integer number : numbers) { + if (number < 0 || number > 45) { + throw new IllegalArgumentException("[ERROR] ๋กœ๋˜ ๋ฒˆํ˜ธ๋Š” 1๋ถ€ํ„ฐ 45 ์‚ฌ์ด์˜ ์ˆซ์ž์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค."); + } + } + } + + private void sortNumbers(List numbers) { + Collections.sort(numbers); + } + + private void validateDuplicate(List numbers) { + Set uniqueNumbers = new HashSet<>(numbers); + if (uniqueNumbers.size() != numbers.size()) { + throw new IllegalArgumentException("[ERROR] ๋กœ๋˜ ๋ฒˆํ˜ธ์— ์ค‘๋ณต๋œ ์ˆซ์ž๊ฐ€ ์žˆ์œผ๋ฉด ์•ˆ๋ฉ๋‹ˆ๋‹ค."); + } + } +} diff --git a/src/main/java/lotto/global/LottoPrize.java b/src/main/java/lotto/global/LottoPrize.java new file mode 100644 index 0000000000..25a1b5c3df --- /dev/null +++ b/src/main/java/lotto/global/LottoPrize.java @@ -0,0 +1,45 @@ +package lotto.global; + +public enum LottoPrize { + + FIRST(6, false, "2,000,000,000"), + SECOND(5, true, "30,000,000"), + THIRD(5, false, "1,500,000"), + FOURTH(4, false, "50,000"), + FIFTH(3, false, "5,000"), + NONE(0, false, "0"); + + private final int matchNumberCount; + private final boolean matchBonusNumber; + private final String prizeMoney; + + LottoPrize(int matchNumberCount, boolean matchBonusNumber, String prizeMoney) { + this.matchNumberCount = matchNumberCount; + this.matchBonusNumber = matchBonusNumber; + this.prizeMoney = prizeMoney; + } + + public int getMatchNumberCount() { + return matchNumberCount; + } + + public String getPrizeMoney() { + return prizeMoney; + } + + public static LottoPrize from(long matchNumberCount, boolean matchBonusNumber) { + for (LottoPrize prize : values()) { + if (prize.matchNumberCount == matchNumberCount && prize.matchBonusNumber == matchBonusNumber) { + return prize; + } + } + return NONE; + } + + public String getMatchDescription() { + if (matchBonusNumber) { + return matchNumberCount + "๊ฐœ ์ผ์น˜, ๋ณด๋„ˆ์Šค ๋ณผ ์ผ์น˜"; + } + return matchNumberCount + "๊ฐœ ์ผ์น˜"; + } +} diff --git a/src/main/java/lotto/parser/InputParser.java b/src/main/java/lotto/parser/InputParser.java new file mode 100644 index 0000000000..593d6eabb7 --- /dev/null +++ b/src/main/java/lotto/parser/InputParser.java @@ -0,0 +1,23 @@ +package lotto.parser; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +public class InputParser { + + public static int parseLottoPrice(String lottoPrice) { + return Integer.parseInt(lottoPrice); + } + + public static List parseLottoNumber(String lottoNumber) { + List lottoNumbers = Arrays.asList(lottoNumber.split(",")); + return lottoNumbers.stream() + .map(Integer::parseInt) + .collect(Collectors.toList()); + } + + public static int parseBonusNumber(String bonusNumber) { + return Integer.parseInt(bonusNumber); + } +} diff --git a/src/main/java/lotto/runner/Runner.java b/src/main/java/lotto/runner/Runner.java new file mode 100644 index 0000000000..2739adcd6e --- /dev/null +++ b/src/main/java/lotto/runner/Runner.java @@ -0,0 +1,52 @@ +package lotto.runner; + +import lotto.application.LottoGenerator; +import lotto.application.LottoService; +import lotto.domain.Lotto; +import lotto.global.LottoPrize; +import lotto.parser.InputParser; +import lotto.view.InputView; +import lotto.view.OutputView; + +import java.util.List; +import java.util.stream.Collectors; + +public class Runner { + + public static void run() { + LottoGenerator lottoGenerator = LottoGenerator.getInstance(); + LottoService lottoService = LottoService.getInstance(lottoGenerator); + + int lottoPrice = InputParser.parseLottoPrice(InputView.inputLottoPurchase()); + List purchasedLottos = getLottos(lottoService, lottoPrice); + + List winningLottoNumbers = InputParser.parseLottoNumber(InputView.inputLottoNumber()); + Lotto winningLotto = Lotto.of(winningLottoNumbers); + + int bonusNumber = InputParser.parseBonusNumber(InputView.inputBonusNumber()); + + List results = getLottoPrizes(purchasedLottos, lottoService, winningLotto, bonusNumber); + + getProfitRate(lottoService, results, lottoPrice); + } + + private static List getLottos(LottoService lottoService, int lottoPrice) { + List purchasedLottos = lottoService.buyLottos(lottoPrice); + OutputView.printLottos(purchasedLottos); + return purchasedLottos; + } + + private static void getProfitRate(LottoService lottoService, List results, int lottoPrice) { + double profitRate = lottoService.calculateProfitRate(results, lottoPrice); + OutputView.printProfit(profitRate); + } + + private static List getLottoPrizes(List purchasedLottos, LottoService lottoService, Lotto winningLotto, int bonusNumber) { + List results = purchasedLottos.stream() + .map(lotto -> lottoService.calculatePrize(lotto, winningLotto, bonusNumber)) + .collect(Collectors.toList()); + + OutputView.printResult(results); + return results; + } +} diff --git a/src/main/java/lotto/view/InputView.java b/src/main/java/lotto/view/InputView.java new file mode 100644 index 0000000000..d84b781de5 --- /dev/null +++ b/src/main/java/lotto/view/InputView.java @@ -0,0 +1,21 @@ +package lotto.view; + +import camp.nextstep.edu.missionutils.Console; + +public class InputView { + + public static String inputLottoPurchase() { + System.out.println("๊ตฌ์ž… ๊ธˆ์•ก์„ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”."); + return Console.readLine(); + } + + public static String inputLottoNumber() { + System.out.println("๋‹น์ฒจ ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”."); + return Console.readLine(); + } + + public static String inputBonusNumber() { + System.out.println("๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”"); + return Console.readLine(); + } +} diff --git a/src/main/java/lotto/view/OutputView.java b/src/main/java/lotto/view/OutputView.java new file mode 100644 index 0000000000..a9b2215264 --- /dev/null +++ b/src/main/java/lotto/view/OutputView.java @@ -0,0 +1,31 @@ +package lotto.view; + +import lotto.domain.Lotto; +import lotto.global.LottoPrize; + +import java.util.Arrays; +import java.util.Comparator; +import java.util.List; + +public class OutputView { + + public static void printLottos(List lottos) { + System.out.println(lottos.size() + "๊ฐœ๋ฅผ ๊ตฌ๋งคํ–ˆ์Šต๋‹ˆ๋‹ค."); + lottos.forEach(lotto -> System.out.println(lotto.getNumbers())); + } + + public static void printResult(List results) { + System.out.println("๋‹น์ฒจ ํ†ต๊ณ„"); + System.out.println("---"); + Arrays.stream(LottoPrize.values()) + .filter(prize -> prize != LottoPrize.NONE) + .forEach(prize -> { + long count = results.stream().filter(prize::equals).count(); + System.out.println(prize.getMatchDescription() + " (" + prize.getPrizeMoney() + "์›) - " + count + "๊ฐœ"); + }); + } + + public static void printProfit(double profit) { + System.out.printf("์ด ์ˆ˜์ต๋ฅ ์€ %.1f%%์ž…๋‹ˆ๋‹ค.%n", profit * 100); + } +} diff --git a/src/test/java/lotto/LottoTest.java b/src/test/java/lotto/LottoTest.java index 309f4e50ae..1d0756794f 100644 --- a/src/test/java/lotto/LottoTest.java +++ b/src/test/java/lotto/LottoTest.java @@ -1,10 +1,15 @@ package lotto; +import lotto.application.LottoService; +import lotto.domain.Lotto; +import lotto.global.LottoPrize; +import lotto.parser.InputParser; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import java.util.List; +import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; class LottoTest { @@ -14,12 +19,16 @@ class LottoTest { .isInstanceOf(IllegalArgumentException.class); } - @DisplayName("๋กœ๋˜ ๋ฒˆํ˜ธ์— ์ค‘๋ณต๋œ ์ˆซ์ž๊ฐ€ ์žˆ์œผ๋ฉด ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.") @Test void ๋กœ๋˜_๋ฒˆํ˜ธ์—_์ค‘๋ณต๋œ_์ˆซ์ž๊ฐ€_์žˆ์œผ๋ฉด_์˜ˆ์™ธ๊ฐ€_๋ฐœ์ƒํ•œ๋‹ค() { assertThatThrownBy(() -> new Lotto(List.of(1, 2, 3, 4, 5, 5))) .isInstanceOf(IllegalArgumentException.class); } - // TODO: ์ถ”๊ฐ€ ๊ธฐ๋Šฅ ๊ตฌํ˜„์— ๋”ฐ๋ฅธ ํ…Œ์ŠคํŠธ ์ฝ”๋“œ ์ž‘์„ฑ + @Test + void ๋กœ๋˜_๋ฒˆํ˜ธ๊ฐ€_6๊ฐœ๊ฐ€_์•„๋‹ˆ๋ฉด_์˜ˆ์™ธ๊ฐ€_๋ฐœ์ƒํ•œ๋‹ค() { + assertThatThrownBy(() -> new Lotto(List.of(1, 2, 3, 4, 5))) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("๋กœ๋˜ ๋ฒˆํ˜ธ๋Š” 6๊ฐœ์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค."); + } }