diff --git a/README.md b/README.md index 5fa2560..c031812 100644 --- a/README.md +++ b/README.md @@ -1 +1,138 @@ -# 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` ๋“ฑ๊ณผ ๊ฐ™์€ ๋ช…ํ™•ํ•œ ์œ ํ˜•์„ ์ฒ˜๋ฆฌํ•œ๋‹ค. + +- `Exception`์ด ์•„๋‹Œ `IllegalArgumentException`, `IllegalStateException` ๋“ฑ๊ณผ ๊ฐ™์€ ๋ช…ํ™•ํ•œ ์œ ํ˜•์„ ์ฒ˜๋ฆฌํ•œ๋‹ค. + +### ์ž…์ถœ๋ ฅ ์š”๊ตฌ ์‚ฌํ•ญ + +### ์ž…๋ ฅ + +- ๋กœ๋˜ ๊ตฌ์ž… ๊ธˆ์•ก์„ ์ž…๋ ฅ ๋ฐ›๋Š”๋‹ค. ๊ตฌ์ž… ๊ธˆ์•ก์€ 1,000์› ๋‹จ์œ„๋กœ ์ž…๋ ฅ ๋ฐ›์œผ๋ฉฐ 1,000์›์œผ๋กœ ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง€์ง€ ์•Š๋Š” ๊ฒฝ์šฐ ์˜ˆ์™ธ ์ฒ˜๋ฆฌํ•œ๋‹ค. + +``` +14000 +``` + +- ๋‹น์ฒจ ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅ ๋ฐ›๋Š”๋‹ค. ๋ฒˆํ˜ธ๋Š” ์‰ผํ‘œ(,)๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๊ตฌ๋ถ„ํ•œ๋‹ค. + +``` +1,2,3,4,5,6 +``` + +- ๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅ ๋ฐ›๋Š”๋‹ค. + +``` +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 ์‚ฌ์ด์˜ ์ˆซ์ž์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค. + +์‹คํ–‰ ๊ฒฐ๊ณผ ์˜ˆ์‹œ +``` +๊ตฌ์ž…๊ธˆ์•ก์„ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”. +8000 + +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] + +๋‹น์ฒจ ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”. +1,2,3,4,5,6 + +๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”. +7 + +๋‹น์ฒจ ํ†ต๊ณ„ +--- +3๊ฐœ ์ผ์น˜ (5,000์›) - 1๊ฐœ +4๊ฐœ ์ผ์น˜ (50,000์›) - 0๊ฐœ +5๊ฐœ ์ผ์น˜ (1,500,000์›) - 0๊ฐœ +5๊ฐœ ์ผ์น˜, ๋ณด๋„ˆ์Šค ๋ณผ ์ผ์น˜ (30,000,000์›) - 0๊ฐœ +6๊ฐœ ์ผ์น˜ (2,000,000,000์›) - 0๊ฐœ +์ด ์ˆ˜์ต๋ฅ ์€ 62.5%์ž…๋‹ˆ๋‹ค. +``` + + +## ๐ŸŽฏ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์š”๊ตฌ ์‚ฌํ•ญ +- indent(์ธ๋ดํŠธ, ๋“ค์—ฌ์“ฐ๊ธฐ) depth๋ฅผ 3์ด ๋„˜์ง€ ์•Š๋„๋ก ๊ตฌํ˜„ํ•œ๋‹ค. 2๊นŒ์ง€๋งŒ ํ—ˆ์šฉํ•œ๋‹ค. + - ์˜ˆ๋ฅผ ๋“ค์–ด while๋ฌธ ์•ˆ์— if๋ฌธ์ด ์žˆ์œผ๋ฉด ๋“ค์—ฌ์“ฐ๊ธฐ๋Š” 2์ด๋‹ค. + - ํžŒํŠธ: indent(์ธ๋ดํŠธ, ๋“ค์—ฌ์“ฐ๊ธฐ) depth๋ฅผ ์ค„์ด๋Š” ์ข‹์€ ๋ฐฉ๋ฒ•์€ ํ•จ์ˆ˜(๋˜๋Š” ๋ฉ”์„œ๋“œ)๋ฅผ ๋ถ„๋ฆฌํ•˜๋ฉด ๋œ๋‹ค. +- 3ํ•ญ ์—ฐ์‚ฐ์ž๋ฅผ ์“ฐ์ง€ ์•Š๋Š”๋‹ค. +- ํ•จ์ˆ˜(๋˜๋Š” ๋ฉ”์„œ๋“œ)๊ฐ€ ํ•œ ๊ฐ€์ง€ ์ผ๋งŒ ํ•˜๋„๋ก ์ตœ๋Œ€ํ•œ ์ž‘๊ฒŒ ๋งŒ๋“ค์–ด๋ผ. +- JUnit 5์™€ AssertJ๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ณธ์ธ์ด ์ •๋ฆฌํ•œ ๊ธฐ๋Šฅ ๋ชฉ๋ก์ด ์ •์ƒ ๋™์ž‘ํ•จ์„ ํ…Œ์ŠคํŠธ ์ฝ”๋“œ๋กœ ํ™•์ธํ•œ๋‹ค. + +### ์ถ”๊ฐ€๋œ ์š”๊ตฌ ์‚ฌํ•ญ + +- ํ•จ์ˆ˜(๋˜๋Š” ๋ฉ”์„œ๋“œ)์˜ ๊ธธ์ด๊ฐ€ 15๋ผ์ธ์„ ๋„˜์–ด๊ฐ€์ง€ ์•Š๋„๋ก ๊ตฌํ˜„ํ•œ๋‹ค. + - ํ•จ์ˆ˜(๋˜๋Š” ๋ฉ”์„œ๋“œ)๊ฐ€ ํ•œ ๊ฐ€์ง€ ์ผ๋งŒ ์ž˜ ํ•˜๋„๋ก ๊ตฌํ˜„ํ•œ๋‹ค. +- else ์˜ˆ์•ฝ์–ด๋ฅผ ์“ฐ์ง€ ์•Š๋Š”๋‹ค. + - ํžŒํŠธ: if ์กฐ๊ฑด์ ˆ์—์„œ ๊ฐ’์„ returnํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ๊ตฌํ˜„ํ•˜๋ฉด else๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค. + - else๋ฅผ ์“ฐ์ง€ ๋ง๋ผ๊ณ  ํ•˜๋‹ˆ switch/case๋กœ ๊ตฌํ˜„ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋Š”๋ฐ switch/case๋„ ํ—ˆ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค. +- Java Enum์„ ์ ์šฉํ•œ๋‹ค. +- ๋„๋ฉ”์ธ ๋กœ์ง์— ๋‹จ์œ„ ํ…Œ์ŠคํŠธ๋ฅผ ๊ตฌํ˜„ํ•ด์•ผ ํ•œ๋‹ค. ๋‹จ, UI(System.out, [System.in](http://system.in/), Scanner) ๋กœ์ง์€ ์ œ์™ธํ•œ๋‹ค. + - ํ•ต์‹ฌ ๋กœ์ง์„ ๊ตฌํ˜„ํ•˜๋Š” ์ฝ”๋“œ์™€ UI๋ฅผ ๋‹ด๋‹นํ•˜๋Š” ๋กœ์ง์„ ๋ถ„๋ฆฌํ•ด ๊ตฌํ˜„ํ•œ๋‹ค. + - ๋‹จ์œ„ ํ…Œ์ŠคํŠธ ์ž‘์„ฑ์ด ์ต์ˆ™ํ•˜์ง€ ์•Š๋‹ค๋ฉด `test/java/lottoList/LottoTest`๋ฅผ ์ฐธ๊ณ ํ•˜์—ฌ ํ•™์Šตํ•œ ํ›„ ํ…Œ์ŠคํŠธ๋ฅผ ๊ตฌํ˜„ํ•œ๋‹ค. + +### ๊ตฌํ˜„ํ•  ๊ธฐ๋Šฅ +- ๊ตฌ์ž… ๊ธˆ์•ก ์ž…๋ ฅ +- ๋ฒˆํ˜ธ 6๊ฐœ์™€ ๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ ์ž…๋ ฅ +- ๋ฒˆํ˜ธ ์ผ์น˜ํ•˜๋Š”์ง€ ํŒ๋‹จ +- ๋ช‡๋“ฑ์ธ์ง€ ํŒ๋‹จ +- ๋‹น์ฒจ ๋‚ด์—ญ ๋ฐ ์ˆ˜์ต๋ฅ ์„ ์ถœ๋ ฅ + +### ๋น„์ง€๋‹ˆ์Šค ๋กœ์ง +- ๋กœ๋˜ ๊ตฌ์ž…ํ•ด์„œ ๋ฒˆํ˜ธ ์ž…๋ ฅํ•˜๊ธฐ +- ๋‹น์ฒจ ๋ฒˆํ˜ธ ์ž…๋ ฅํ•˜๊ธฐ +- ๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ ์ž…๋ ฅํ•˜๊ธฐ +- ๋ฒˆํ˜ธ ๋งž์ถฐ๋ณด๊ธฐ +- ๋“ฑ์ˆ˜ ํŒ๋‹จํ•˜๊ธฐ +- ๊ฒฐ๊ณผ ์ถœ๋ ฅํ•˜๊ธฐ \ No newline at end of file diff --git a/src/main/java/lotto/AppConfig.java b/src/main/java/lotto/AppConfig.java new file mode 100644 index 0000000..ee8f77e --- /dev/null +++ b/src/main/java/lotto/AppConfig.java @@ -0,0 +1,52 @@ +package lotto; + + +import lotto.controller.LottoController; +import lotto.service.AdjustmentLotto; +import lotto.service.CalculateTotalBenefit; +import lotto.service.CountCorrectNumber; +import lotto.service.LottoList; +import lotto.util.Validator; +import lotto.view.UserInput; +import lotto.view.UserOutput; + +public class AppConfig { + public UserInput userInput(){ + return new UserInput(); + }; + + public UserOutput userOutput(){ + return new UserOutput(); + } + + public Validator validator(){ + return new Validator(); + } + + public LottoList makeRandomLotto(){ + return new LottoList(userInput().getNumberOfLotto()); + } + + public CountCorrectNumber countCorrectNumber(){ + return new CountCorrectNumber(makeRandomLotto().getLottoContainer()); + } + + public AdjustmentLotto adjustmentLotto(){ + return new AdjustmentLotto(countCorrectNumber().getLottoList(), userInput().getBonusNumber()); + } + + public CalculateTotalBenefit calculateTotalBenefit(){ + return new CalculateTotalBenefit(adjustmentLotto().getLotto(), userInput().getBudget()); + } + public LottoController lottoController(){ + return new LottoController( + userInput(), + userOutput(), + validator(), + makeRandomLotto(), + countCorrectNumber(), + adjustmentLotto(), + calculateTotalBenefit() + ); + } +} diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java index d190922..80e3799 100644 --- a/src/main/java/lotto/Application.java +++ b/src/main/java/lotto/Application.java @@ -1,7 +1,15 @@ package lotto; +import lotto.controller.LottoController; + public class Application { + static AppConfig appConfig; + static LottoController lottoController; public static void main(String[] args) { // TODO: ํ”„๋กœ๊ทธ๋žจ ๊ตฌํ˜„ + appConfig = new AppConfig(); + lottoController = appConfig.lottoController(); + + lottoController.lottoPlaying(); } } diff --git a/src/main/java/lotto/Lotto.java b/src/main/java/lotto/Lotto.java deleted file mode 100644 index 88fc5cf..0000000 --- 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/controller/LottoController.java b/src/main/java/lotto/controller/LottoController.java new file mode 100644 index 0000000..a128a3b --- /dev/null +++ b/src/main/java/lotto/controller/LottoController.java @@ -0,0 +1,78 @@ +package lotto.controller; + +import lotto.model.Lotto; +import lotto.service.AdjustmentLotto; +import lotto.service.CalculateTotalBenefit; +import lotto.service.CountCorrectNumber; +import lotto.service.LottoList; +import lotto.view.UserInput; +import lotto.view.UserOutput; +import lotto.util.Validator; + +public class LottoController { + UserInput userInput; + UserOutput userOutput; + Validator validator; + LottoList lottoList; + CountCorrectNumber countCorrectNumber; + AdjustmentLotto adjustmentLotto; + CalculateTotalBenefit calculateTotalBenefit; + + public LottoController( + UserInput userInput, + UserOutput userOutput, + Validator validator, + LottoList lottoList, + CountCorrectNumber countCorrectNumber, + AdjustmentLotto adjustmentLotto, + CalculateTotalBenefit calculateTotalBenefit) { + this.userInput = userInput; + this.userOutput = userOutput; + this.validator = validator; + this.lottoList = lottoList; + this.countCorrectNumber = countCorrectNumber; + this.adjustmentLotto = adjustmentLotto; + this.calculateTotalBenefit = calculateTotalBenefit; + } + + public void lottoPlaying(){ + buyLotto(); + executeLotto(); + winningPriceLotto(); + } + + public void buyLotto(){ + userInput.setBudget(); + lottoList = new LottoList(userInput.getNumberOfLotto()); + + userOutput.printNumberOfLotto(userInput.getNumberOfLotto()); + userOutput.printNumberOfLottoNumbers(lottoList.getLottoContainer()); + } + + public void executeLotto(){ + userInput.setWinningNumbers(); + Lotto winningLotto = new Lotto(userInput.getWinningLotto().getNumbers()); + validator.numberCountValidator(winningLotto.getNumbers()); + + userInput.setBonusNumber(); + validator.numberDuplicateWithBonusNumberValidator(winningLotto, userInput.getBonusNumber()); + + countCorrectNumber.listCorrectNumber(lottoList.getLottoContainer(), winningLotto); + + } + + public void winningPriceLotto(){ + adjustmentLotto.countCorrectNumber(); + userOutput.printWinningLotto(adjustmentLotto.getLotto()); + + calculateTotalBenefit.sumLotto(); + calculateTotalBenefit.calculateTotalBenefit(); + } + + + + + + + +} diff --git a/src/main/java/lotto/model/Lotto.java b/src/main/java/lotto/model/Lotto.java new file mode 100644 index 0000000..3410386 --- /dev/null +++ b/src/main/java/lotto/model/Lotto.java @@ -0,0 +1,30 @@ +package lotto.model; + +import java.util.List; + + +public class Lotto { + private final List numbers; + private int correctNumbers; + + public Lotto(List lotto) { + correctNumbers = 0; + numbers = lotto; + } + + public List getNumbers() { + return numbers; + } + + public int getCorrectNumbers() { + return correctNumbers; + } + + public void setNumbers(int correctNumbers) { + this.correctNumbers = correctNumbers; + } + + public void plusCorrectNumbers() { + correctNumbers++; + } +} diff --git a/src/main/java/lotto/service/AdjustmentLotto.java b/src/main/java/lotto/service/AdjustmentLotto.java new file mode 100644 index 0000000..0d629d3 --- /dev/null +++ b/src/main/java/lotto/service/AdjustmentLotto.java @@ -0,0 +1,43 @@ +package lotto.service; + +import lotto.model.Lotto; + +import java.util.ArrayList; +import java.util.List; + + +public class AdjustmentLotto { + private int[] lotto = new int[5]; + private List lottoList = new ArrayList(); + private int bonus; + + public AdjustmentLotto(List lottoList, int bonus) { + this.lottoList = lottoList; + this.bonus = bonus; + for (int l : lotto) { lotto[l] = 0; } + } + + public int[] getLotto() { + return lotto; + } + + public void countCorrectNumber(){ + for (Lotto l : lottoList){ + countCorrectNumber(l); + } + } + + public void countCorrectNumber(Lotto winningLotto) { + if (winningLotto.getCorrectNumbers() == 3) { lotto[0]++; } + if (winningLotto.getCorrectNumbers() == 4) { lotto[1]++; } + if (winningLotto.getCorrectNumbers() == 5) { + CountCorrectNumber countCorrectNumber = new CountCorrectNumber(lottoList); + if (countCorrectNumber.sameNumber(winningLotto, bonus)){ + lotto[3]++; + return; + } + lotto[4]++; + } + if (winningLotto.getCorrectNumbers() == 6) { lotto[4]++; } + } +} diff --git a/src/main/java/lotto/service/CalculateTotalBenefit.java b/src/main/java/lotto/service/CalculateTotalBenefit.java new file mode 100644 index 0000000..ec72f04 --- /dev/null +++ b/src/main/java/lotto/service/CalculateTotalBenefit.java @@ -0,0 +1,33 @@ +package lotto.service; + +import lotto.util.MessageConstants; + +public class CalculateTotalBenefit { + private int[] lotto; + private int budget; + private double totalBenefit = 0; + public CalculateTotalBenefit(int[] lotto, int budget) { + this.lotto = lotto; + this.budget = budget; + } + + public void sumLotto(){ + lotto[0] *= 5000; + lotto[1] *= 50000; + lotto[2] *= 1500000; + lotto[3] *= 30000000; + lotto[4] *= 2000000000; + for (int i = 0; i < lotto.length; i++) { + totalBenefit += (double)lotto[i]; + } + } + + public void calculateTotalBenefit() { + if (totalBenefit != 0) { + totalBenefit /= (double)budget; + System.out.println(MessageConstants.TOTAL_BENEFIT.getMessage(totalBenefit)); + return; + } + System.out.println(MessageConstants.TOTAL_BENEFIT.getMessage(0)); + } +} diff --git a/src/main/java/lotto/service/CountCorrectNumber.java b/src/main/java/lotto/service/CountCorrectNumber.java new file mode 100644 index 0000000..9c99b17 --- /dev/null +++ b/src/main/java/lotto/service/CountCorrectNumber.java @@ -0,0 +1,37 @@ +package lotto.service; + +import lotto.model.Lotto; + +import java.util.List; + +public class CountCorrectNumber { + private List lottoList; + + public List getLottoList(){ + return lottoList; + } + + public CountCorrectNumber(List lottoList) { + this.lottoList = lottoList; + } + + public void listCorrectNumber(List myLottos, Lotto winningLotto){ + for (Lotto myLotto : myLottos) { + chooseCorrectNumber(myLotto, winningLotto); + } + } + + public void chooseCorrectNumber(Lotto myLotto, Lotto winningLotto) { + for (int winningLottoNumber : winningLotto.getNumbers()) { + sameNumber(myLotto, winningLottoNumber); + } + } + + public boolean sameNumber(Lotto myLotto, int winningLottoNumber) { + if (myLotto.getNumbers().contains(winningLottoNumber)) { + myLotto.plusCorrectNumbers(); + return true; + } + return false; + } +} diff --git a/src/main/java/lotto/service/LottoList.java b/src/main/java/lotto/service/LottoList.java new file mode 100644 index 0000000..032cc06 --- /dev/null +++ b/src/main/java/lotto/service/LottoList.java @@ -0,0 +1,25 @@ +package lotto.service; + +import java.util.ArrayList; +import java.util.List; +import camp.nextstep.edu.missionutils.Randoms; +import lotto.model.Lotto; +import lotto.util.Validator; + + +public class LottoList { + private List lottoContainer = new ArrayList<>(); + + public LottoList(int numberOfTrials) { + for (int i = 0; i < numberOfTrials; i++) { + List lottoNumbers = Randoms.pickUniqueNumbersInRange(1, 45, 6); + Validator validator = new Validator(); + validator.numberDuplicateValidator(lottoNumbers); + lottoContainer.add(new Lotto(lottoNumbers) ); + } + } + + public List getLottoContainer() { + return lottoContainer; + } +} diff --git a/src/main/java/lotto/util/MessageConstants.java b/src/main/java/lotto/util/MessageConstants.java new file mode 100644 index 0000000..e764b2d --- /dev/null +++ b/src/main/java/lotto/util/MessageConstants.java @@ -0,0 +1,35 @@ +package lotto.util; + +public enum MessageConstants{ + THREE_MESSAGE("3๊ฐœ ์ผ์น˜ (5,000์›) - "), + FOUR_MESSAGE("4๊ฐœ ์ผ์น˜ (50,000์›) - "), + FIVE_MESSAGE("5๊ฐœ ์ผ์น˜ (1,500,000์›) - "), + FIVE_AND_BONUS_MASSAGE("5๊ฐœ ์ผ์น˜, ๋ณด๋„ˆ์Šค ๋ณผ ์ผ์น˜ (30,000,000์›) - "), + SIX_MASSAGE("6๊ฐœ ์ผ์น˜ (2,000,000,000์›) - "), + + PURCHASE_MESSAGE("๊ฐœ๋ฅผ ๊ตฌ๋งคํ–ˆ์Šต๋‹ˆ๋‹ค"), + REQUEST_WINNING_NUMBER("๋‹น์ฒจ ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."), + REQUEST_BONUS_NUMBER("๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."), + REQUEST_PURCHASE_AMOUNT("๊ตฌ์ž… ๊ธˆ์•ก์„ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."), + WINNING_STATISTICS("๋‹น์ฒจ ํ†ต๊ณ„\n---"), + TOTAL_BENEFIT("์ด ์ˆ˜์ต๋ฅ ์€ %.1f%%์ž…๋‹ˆ๋‹ค."), + + INOUTPUT_ERROR("[Error] ์ž…์ถœ๋ ฅ ๊ณผ์ •์—์„œ ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค."), + NUMBER_COUNT_ERROR("[ERROR] ๋กœ๋˜ ๋ฒˆํ˜ธ๋Š” 6๊ฐœ์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค."), + NUMBER_LIMIT_ERROR("[ERROR] ๋กœ๋˜ ๋ฒˆํ˜ธ๋Š” 1๋ถ€ํ„ฐ 45 ์‚ฌ์ด์˜ ์ˆซ์ž์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค."), + NUMBER_DUPLICATE_ERROR("[ERROR] ์ค‘๋ณต๋œ ๊ฐ’์„ ์ž…๋ ฅ๋ฐ›์•˜์Šต๋‹ˆ๋‹ค!"), + NOT_NUMBER_ERROR("[ERROR] ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”."); + + + private String message; + MessageConstants(String message){ + this.message = message; + } + + public String getMessage(double totalBenefit){ + return String.format(message, totalBenefit); + } + public String getMessage(){ + return message; + } +} \ No newline at end of file diff --git a/src/main/java/lotto/util/Validator.java b/src/main/java/lotto/util/Validator.java new file mode 100644 index 0000000..b043c86 --- /dev/null +++ b/src/main/java/lotto/util/Validator.java @@ -0,0 +1,41 @@ +package lotto.util; + +import lotto.model.Lotto; + +import java.util.*; + +public class Validator { + public void numberCountValidator(List numbers) { + if (numbers.size() != 6) { + throw new IllegalArgumentException(MessageConstants.NUMBER_COUNT_ERROR.getMessage()); + } + } + + public void numberLimitValidator(List numbers) { + numbers.forEach(number -> { + if (number > 45 || number < 1) { + throw new IllegalArgumentException(MessageConstants.NUMBER_LIMIT_ERROR.getMessage()); + } + }); + } + + public void numberDuplicateValidator(List numbers) { + Set seen = new HashSet<>(); + for (Integer number : numbers) { + if (!seen.add(number)) { + throw new IllegalArgumentException(MessageConstants.NUMBER_DUPLICATE_ERROR.getMessage()); + } + } + } + public void numberDuplicateWithBonusNumberValidator(Lotto winningLotto, int bonusNumber) { + Set seen = new HashSet<>(); + for (int i = 0; i < winningLotto.getNumbers().size(); i++) { + if (!seen.add(winningLotto.getNumbers().get(i))) { + throw new IllegalArgumentException(MessageConstants.NUMBER_DUPLICATE_ERROR.getMessage()); + } + } + if (!seen.add(bonusNumber)) { + throw new IllegalArgumentException(MessageConstants.NUMBER_DUPLICATE_ERROR.getMessage()); + } + } +} diff --git a/src/main/java/lotto/view/UserInput.java b/src/main/java/lotto/view/UserInput.java new file mode 100644 index 0000000..9062d45 --- /dev/null +++ b/src/main/java/lotto/view/UserInput.java @@ -0,0 +1,61 @@ +package lotto.view; + +import camp.nextstep.edu.missionutils.Console; +import lotto.model.Lotto; +import lotto.util.MessageConstants; + +import java.util.ArrayList; + +public class UserInput { + + private int budget; + private Lotto winningLotto; + private int bonusNumber; + + + public UserInput() { + budget = 0; + bonusNumber = 0; + } + + public int getNumberOfLotto() { + return budget/1000; + } + public Lotto getWinningLotto() { return winningLotto; } + public int getBonusNumber() { return bonusNumber; } + public int getBudget() { return budget; } + + + public void setBudget() { + try { + System.out.println(MessageConstants.REQUEST_PURCHASE_AMOUNT.getMessage()); + budget = Integer.parseInt(Console.readLine()); + } catch (Exception exception) { + throw new IllegalArgumentException(MessageConstants.INOUTPUT_ERROR.getMessage()); + } + } + + public void setWinningNumbers() { + try { + System.out.println(MessageConstants.REQUEST_WINNING_NUMBER.getMessage()); + ArrayList numbers = new ArrayList<>(); + String winningNums = Console.readLine(); + for (String num : winningNums.split(",")) { + numbers.add(Integer.parseInt(num.trim())); + } + + winningLotto = new Lotto(numbers); + } catch (Exception exception) { + throw new IllegalArgumentException(MessageConstants.INOUTPUT_ERROR.getMessage()); + } + } + + public void setBonusNumber(){ + try { + System.out.println(MessageConstants.REQUEST_BONUS_NUMBER.getMessage()); + bonusNumber = Integer.parseInt(Console.readLine()); + } catch (Exception exception) { + throw new IllegalArgumentException(MessageConstants.INOUTPUT_ERROR.getMessage()); + } + } +} diff --git a/src/main/java/lotto/view/UserOutput.java b/src/main/java/lotto/view/UserOutput.java new file mode 100644 index 0000000..787f9b8 --- /dev/null +++ b/src/main/java/lotto/view/UserOutput.java @@ -0,0 +1,27 @@ +package lotto.view; + +import lotto.model.Lotto; +import java.util.List; +import lotto.util.MessageConstants; + + + +public class UserOutput { + public void printNumberOfLotto(int numberOfLotto){ + System.out.println(numberOfLotto + MessageConstants.PURCHASE_MESSAGE.getMessage()); + } + + public void printNumberOfLottoNumbers(List lottoList) { + for (Lotto lotto : lottoList) { + System.out.println(lotto.getNumbers()); + } + } + + public void printWinningLotto(int[] lotto){ + System.out.println(MessageConstants.THREE_MESSAGE.getMessage() + lotto[0] + "๊ฐœ"); + System.out.println(MessageConstants.FOUR_MESSAGE.getMessage() + lotto[1] + "๊ฐœ"); + System.out.println(MessageConstants.FIVE_MESSAGE.getMessage() + lotto[2] + "๊ฐœ"); + System.out.println(MessageConstants.FIVE_AND_BONUS_MASSAGE.getMessage() + lotto[3] + "๊ฐœ"); + System.out.println(MessageConstants.SIX_MASSAGE.getMessage() + lotto[4] + "๊ฐœ"); + } +} diff --git a/src/test/java/lotto/LottoTest.java b/src/test/java/lotto/LottoTest.java index 309f4e5..c8ca9d6 100644 --- a/src/test/java/lotto/LottoTest.java +++ b/src/test/java/lotto/LottoTest.java @@ -1,5 +1,6 @@ package lotto; +import lotto.model.Lotto; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test;