From 8d860f64adc717ace54c5138a929296c3c7a6184 Mon Sep 17 00:00:00 2001 From: sooohi <68193613+sooohi@users.noreply.github.com> Date: Tue, 7 Nov 2023 12:51:02 +0900 Subject: [PATCH 01/19] =?UTF-8?q?docs=20:=20=EA=B8=B0=EB=8A=A5=EA=B5=AC?= =?UTF-8?q?=ED=98=84=20readme=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/docs/README.md b/docs/README.md index e69de29bb..939902c1c 100644 --- a/docs/README.md +++ b/docs/README.md @@ -0,0 +1,44 @@ +# ๋ฏธ์…˜ - ๋กœ๋˜ + +## ๐Ÿš€ ๊ธฐ๋Šฅ ์š”๊ตฌ ์‚ฌํ•ญ + +๋กœ๋˜ ๊ฒŒ์ž„ ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•ด์•ผ ํ•œ๋‹ค. ๋กœ๋˜ ๊ฒŒ์ž„์€ ์•„๋ž˜์™€ ๊ฐ™์€ ๊ทœ์น™์œผ๋กœ ์ง„ํ–‰๋œ๋‹ค. + +-[ ] ๋กœ๋˜ ๋ฒˆํ˜ธ์˜ ์ˆซ์ž ๋ฒ”์œ„๋Š” 1~45๊นŒ์ง€์ด๋‹ค. +-[ ] 1๊ฐœ์˜ ๋กœ๋˜๋ฅผ ๋ฐœํ–‰ํ•  ๋•Œ ์ค‘๋ณต๋˜์ง€ ์•Š๋Š” 6๊ฐœ์˜ ์ˆซ์ž๋ฅผ ๋ฝ‘๋Š”๋‹ค. +-[ ] ๋‹น์ฒจ ๋ฒˆํ˜ธ ์ถ”์ฒจ ์‹œ ์ค‘๋ณต๋˜์ง€ ์•Š๋Š” ์ˆซ์ž 6๊ฐœ์™€ ๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ 1๊ฐœ๋ฅผ ๋ฝ‘๋Š”๋‹ค. +-[ ] ๋‹น์ฒจ์€ 1๋“ฑ๋ถ€ํ„ฐ 5๋“ฑ๊นŒ์ง€ ์žˆ๋‹ค. ๋‹น์ฒจ ๊ธฐ์ค€๊ณผ ๊ธˆ์•ก์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค. +-[ ] ๋กœ๋˜ ๊ตฌ์ž… ๊ธˆ์•ก์„ ์ž…๋ ฅํ•˜๋ฉด ๊ตฌ์ž… ๊ธˆ์•ก์— ํ•ด๋‹นํ•˜๋Š” ๋งŒํผ ๋กœ๋˜๋ฅผ ๋ฐœํ–‰ํ•ด์•ผ ํ•œ๋‹ค. +-[ ] ๋กœ๋˜ 1์žฅ์˜ ๊ฐ€๊ฒฉ์€ 1,000์›์ด๋‹ค. +-[ ] ๋‹น์ฒจ ๋ฒˆํ˜ธ์™€ ๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค. +-[ ]์‚ฌ์šฉ์ž๊ฐ€ ๊ตฌ๋งคํ•œ ๋กœ๋˜ ๋ฒˆํ˜ธ์™€ ๋‹น์ฒจ ๋ฒˆํ˜ธ๋ฅผ ๋น„๊ตํ•˜์—ฌ ๋‹น์ฒจ ๋‚ด์—ญ ๋ฐ ์ˆ˜์ต๋ฅ ์„ ์ถœ๋ ฅํ•˜๊ณ  ๋กœ๋˜ ๊ฒŒ์ž„์„ ์ข…๋ฃŒํ•œ๋‹ค. +-[ ] ์‚ฌ์šฉ์ž๊ฐ€ ์ž˜๋ชป๋œ ๊ฐ’์„ ์ž…๋ ฅํ•  ๊ฒฝ์šฐ `IllegalArgumentException`๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค๊ณ , "[ERROR]"๋กœ ์‹œ์ž‘ํ•˜๋Š” ์—๋Ÿฌ ๋ฉ”์‹œ์ง€๋ฅผ ์ถœ๋ ฅ ํ›„ ๊ทธ ๋ถ€๋ถ„๋ถ€ํ„ฐ ์ž…๋ ฅ์„ ๋‹ค์‹œ ๋ฐ›๋Š”๋‹ค. +-[ ]`Exception`์ด ์•„๋‹Œ `IllegalArgumentException`, `IllegalStateException` ๋“ฑ๊ณผ ๊ฐ™์€ ๋ช…ํ™•ํ•œ ์œ ํ˜•์„ ์ฒ˜๋ฆฌํ•œ๋‹ค. + +### ์ถ”๊ฐ€๋œ ์š”๊ตฌ ์‚ฌํ•ญ + +-[ ] ํ•จ์ˆ˜(๋˜๋Š” ๋ฉ”์„œ๋“œ)์˜ ๊ธธ์ด๊ฐ€ 15๋ผ์ธ์„ ๋„˜์–ด๊ฐ€์ง€ ์•Š๋„๋ก ๊ตฌํ˜„ํ•œ๋‹ค. + - ํ•จ์ˆ˜(๋˜๋Š” ๋ฉ”์„œ๋“œ)๊ฐ€ ํ•œ ๊ฐ€์ง€ ์ผ๋งŒ ์ž˜ ํ•˜๋„๋ก ๊ตฌํ˜„ํ•œ๋‹ค. +-[ ] else๋ฅผ ์ง€์–‘ํ•œ๋‹ค. + - ํžŒํŠธ: if ์กฐ๊ฑด์ ˆ์—์„œ ๊ฐ’์„ returnํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ๊ตฌํ˜„ํ•˜๋ฉด else๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค. + - ๋•Œ๋กœ๋Š” if/else, when๋ฌธ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๋” ๊น”๋”ํ•ด ๋ณด์ผ ์ˆ˜ ์žˆ๋‹ค. ์–ด๋Š ๊ฒฝ์šฐ์— ์“ฐ๋Š” ๊ฒƒ์ด ์ ์ ˆํ• ์ง€ ์Šค์Šค๋กœ ๊ณ ๋ฏผํ•ด ๋ณธ๋‹ค. +-[ ] Enum ํด๋ž˜์Šค๋ฅผ ์ ์šฉํ•ด ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ๊ตฌํ˜„ํ•œ๋‹ค. +-[ ] ๋„๋ฉ”์ธ ๋กœ์ง์— ๋‹จ์œ„ ํ…Œ์ŠคํŠธ๋ฅผ ๊ตฌํ˜„ํ•ด์•ผ ํ•œ๋‹ค. ๋‹จ, UI(System.out, System.in, Scanner) ๋กœ์ง์€ ์ œ์™ธํ•œ๋‹ค. + - ํ•ต์‹ฌ ๋กœ์ง์„ ๊ตฌํ˜„ํ•˜๋Š” ์ฝ”๋“œ์™€ UI๋ฅผ ๋‹ด๋‹นํ•˜๋Š” ๋กœ์ง์„ ๋ถ„๋ฆฌํ•ด ๊ตฌํ˜„ํ•œ๋‹ค. + - ๋‹จ์œ„ ํ…Œ์ŠคํŠธ ์ž‘์„ฑ์ด ์ต์ˆ™ํ•˜์ง€ ์•Š๋‹ค๋ฉด `test/kotlin/lotto/LottoTest`๋ฅผ ์ฐธ๊ณ ํ•˜์—ฌ ํ•™์Šตํ•œ ํ›„ ํ…Œ์ŠคํŠธ๋ฅผ ๊ตฌํ˜„ํ•œ๋‹ค. +-[ ] `camp.nextstep.edu.missionutils`์—์„œ ์ œ๊ณตํ•˜๋Š” `Randoms` ๋ฐ `Console` API๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ตฌํ˜„ํ•ด์•ผ ํ•œ๋‹ค. + - Random ๊ฐ’ ์ถ”์ถœ์€ `camp.nextstep.edu.missionutils.Randoms`์˜ `pickUniqueNumbersInRange()`๋ฅผ ํ™œ์šฉํ•œ๋‹ค. + - ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•˜๋Š” ๊ฐ’์€ `camp.nextstep.edu.missionutils.Console`์˜ `readLine()`์„ ํ™œ์šฉํ•œ๋‹ค. +-[ ] ์ œ๊ณต๋œ `Lotto` ํด๋ž˜์Šค๋ฅผ ํ™œ์šฉํ•ด ๊ตฌํ˜„ํ•ด์•ผ ํ•œ๋‹ค. + +## MVC๋ชจ๋ธ ํ™œ์šฉ + +### Model +- [ ] Lotto ํด๋ž˜์Šค์— ๋ฒˆํ˜ธ ์ €์žฅ + +### View +- [ ] ๋ฉ”์‹œ์ง€ ์ถœ๋ ฅ + +### Controller +- [ ] LottoController : ์ˆซ์ž ์ž…๋ ฅ ๋ฐ›์€ ๊ฒƒ ์ฒ˜๋ฆฌ ๋ฐ ๊ฒŒ์ž„ ์‹คํ–‰ +- [ ] ์‚ฌ์šฉ์ž์—๊ฒŒ ์ˆซ์ž ์ž…๋ ฅ ๋ฐ›๊ธฐ From 4c86895442910b1fd281213d9b1230f2972e7189 Mon Sep 17 00:00:00 2001 From: sooohi <68193613+sooohi@users.noreply.github.com> Date: Tue, 7 Nov 2023 22:48:45 +0900 Subject: [PATCH 02/19] =?UTF-8?q?feat=20:=20=EA=B5=AC=EB=A7=A4=EA=B8=88?= =?UTF-8?q?=EC=95=A1=20=EC=9E=85=EB=A0=A5=EB=B0=9B=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 4 ++-- src/main/kotlin/lotto/Application.kt | 10 +++++++++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/docs/README.md b/docs/README.md index 939902c1c..f5ab8afb1 100644 --- a/docs/README.md +++ b/docs/README.md @@ -4,6 +4,7 @@ ๋กœ๋˜ ๊ฒŒ์ž„ ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•ด์•ผ ํ•œ๋‹ค. ๋กœ๋˜ ๊ฒŒ์ž„์€ ์•„๋ž˜์™€ ๊ฐ™์€ ๊ทœ์น™์œผ๋กœ ์ง„ํ–‰๋œ๋‹ค. +- [x] ๊ตฌ๋งค๊ธ‰์•ก ์ž…๋ ฅ๋ฐ›๊ธฐ -[ ] ๋กœ๋˜ ๋ฒˆํ˜ธ์˜ ์ˆซ์ž ๋ฒ”์œ„๋Š” 1~45๊นŒ์ง€์ด๋‹ค. -[ ] 1๊ฐœ์˜ ๋กœ๋˜๋ฅผ ๋ฐœํ–‰ํ•  ๋•Œ ์ค‘๋ณต๋˜์ง€ ์•Š๋Š” 6๊ฐœ์˜ ์ˆซ์ž๋ฅผ ๋ฝ‘๋Š”๋‹ค. -[ ] ๋‹น์ฒจ ๋ฒˆํ˜ธ ์ถ”์ฒจ ์‹œ ์ค‘๋ณต๋˜์ง€ ์•Š๋Š” ์ˆซ์ž 6๊ฐœ์™€ ๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ 1๊ฐœ๋ฅผ ๋ฝ‘๋Š”๋‹ค. @@ -12,8 +13,7 @@ -[ ] ๋กœ๋˜ 1์žฅ์˜ ๊ฐ€๊ฒฉ์€ 1,000์›์ด๋‹ค. -[ ] ๋‹น์ฒจ ๋ฒˆํ˜ธ์™€ ๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค. -[ ]์‚ฌ์šฉ์ž๊ฐ€ ๊ตฌ๋งคํ•œ ๋กœ๋˜ ๋ฒˆํ˜ธ์™€ ๋‹น์ฒจ ๋ฒˆํ˜ธ๋ฅผ ๋น„๊ตํ•˜์—ฌ ๋‹น์ฒจ ๋‚ด์—ญ ๋ฐ ์ˆ˜์ต๋ฅ ์„ ์ถœ๋ ฅํ•˜๊ณ  ๋กœ๋˜ ๊ฒŒ์ž„์„ ์ข…๋ฃŒํ•œ๋‹ค. --[ ] ์‚ฌ์šฉ์ž๊ฐ€ ์ž˜๋ชป๋œ ๊ฐ’์„ ์ž…๋ ฅํ•  ๊ฒฝ์šฐ `IllegalArgumentException`๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค๊ณ , "[ERROR]"๋กœ ์‹œ์ž‘ํ•˜๋Š” ์—๋Ÿฌ ๋ฉ”์‹œ์ง€๋ฅผ ์ถœ๋ ฅ ํ›„ ๊ทธ ๋ถ€๋ถ„๋ถ€ํ„ฐ ์ž…๋ ฅ์„ ๋‹ค์‹œ ๋ฐ›๋Š”๋‹ค. --[ ]`Exception`์ด ์•„๋‹Œ `IllegalArgumentException`, `IllegalStateException` ๋“ฑ๊ณผ ๊ฐ™์€ ๋ช…ํ™•ํ•œ ์œ ํ˜•์„ ์ฒ˜๋ฆฌํ•œ๋‹ค. +-[x] ์‚ฌ์šฉ์ž๊ฐ€ ์ž˜๋ชป๋œ ๊ฐ’์„ ์ž…๋ ฅํ•  ๊ฒฝ์šฐ `IllegalArgumentException`๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค๊ณ , "[ERROR]"๋กœ ์‹œ์ž‘ํ•˜๋Š” ์—๋Ÿฌ ๋ฉ”์‹œ์ง€๋ฅผ ์ถœ๋ ฅ ํ›„ ๊ทธ ๋ถ€๋ถ„๋ถ€ํ„ฐ ์ž…๋ ฅ์„ ๋‹ค์‹œ ๋ฐ›๋Š”๋‹ค. `Exception`์ด ์•„๋‹Œ `IllegalArgumentException`, `IllegalStateException` ๋“ฑ๊ณผ ๊ฐ™์€ ๋ช…ํ™•ํ•œ ์œ ํ˜•์„ ์ฒ˜๋ฆฌํ•œ๋‹ค. ### ์ถ”๊ฐ€๋œ ์š”๊ตฌ ์‚ฌํ•ญ diff --git a/src/main/kotlin/lotto/Application.kt b/src/main/kotlin/lotto/Application.kt index d7168761c..9739504ce 100644 --- a/src/main/kotlin/lotto/Application.kt +++ b/src/main/kotlin/lotto/Application.kt @@ -1,5 +1,13 @@ package lotto fun main() { - TODO("ํ”„๋กœ๊ทธ๋žจ ๊ตฌํ˜„") + val inputMoney =lottoMoneyInput() +} +fun lottoMoneyInput(): Int { + println("๊ตฌ์ž…๊ธˆ์•ก์„ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.") + val inputMoney = readLine()!!.toInt() + if (inputMoney % 1000 != 0) { + throw IllegalArgumentException("1000์› ๋‹จ์œ„๋กœ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.") + } + return inputMoney } From 9b11403f2f96f6da6327b1b976cd418f52ef0189 Mon Sep 17 00:00:00 2001 From: sooohi <68193613+sooohi@users.noreply.github.com> Date: Tue, 7 Nov 2023 23:02:30 +0900 Subject: [PATCH 03/19] =?UTF-8?q?feat=20:=20=ED=9A=9F=EC=88=98=EB=A7=8C?= =?UTF-8?q?=ED=81=BC=20=EB=9E=9C=EB=8D=A4=EB=B2=88=ED=98=B8=EC=83=9D?= =?UTF-8?q?=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 10 +++++----- src/main/kotlin/lotto/Application.kt | 21 +++++++++++++++++++++ 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/docs/README.md b/docs/README.md index f5ab8afb1..f6ffef96b 100644 --- a/docs/README.md +++ b/docs/README.md @@ -4,13 +4,13 @@ ๋กœ๋˜ ๊ฒŒ์ž„ ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•ด์•ผ ํ•œ๋‹ค. ๋กœ๋˜ ๊ฒŒ์ž„์€ ์•„๋ž˜์™€ ๊ฐ™์€ ๊ทœ์น™์œผ๋กœ ์ง„ํ–‰๋œ๋‹ค. -- [x] ๊ตฌ๋งค๊ธ‰์•ก ์ž…๋ ฅ๋ฐ›๊ธฐ --[ ] ๋กœ๋˜ ๋ฒˆํ˜ธ์˜ ์ˆซ์ž ๋ฒ”์œ„๋Š” 1~45๊นŒ์ง€์ด๋‹ค. --[ ] 1๊ฐœ์˜ ๋กœ๋˜๋ฅผ ๋ฐœํ–‰ํ•  ๋•Œ ์ค‘๋ณต๋˜์ง€ ์•Š๋Š” 6๊ฐœ์˜ ์ˆซ์ž๋ฅผ ๋ฝ‘๋Š”๋‹ค. +-[x] ๊ตฌ๋งค๊ธ‰์•ก ์ž…๋ ฅ๋ฐ›๊ธฐ +-[x] ๋กœ๋˜ ๋ฒˆํ˜ธ์˜ ์ˆซ์ž ๋ฒ”์œ„๋Š” 1~45๊นŒ์ง€์ด๋‹ค. +-[x] 1๊ฐœ์˜ ๋กœ๋˜๋ฅผ ๋ฐœํ–‰ํ•  ๋•Œ ์ค‘๋ณต๋˜์ง€ ์•Š๋Š” 6๊ฐœ์˜ ์ˆซ์ž๋ฅผ ๋ฝ‘๋Š”๋‹ค. -[ ] ๋‹น์ฒจ ๋ฒˆํ˜ธ ์ถ”์ฒจ ์‹œ ์ค‘๋ณต๋˜์ง€ ์•Š๋Š” ์ˆซ์ž 6๊ฐœ์™€ ๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ 1๊ฐœ๋ฅผ ๋ฝ‘๋Š”๋‹ค. -[ ] ๋‹น์ฒจ์€ 1๋“ฑ๋ถ€ํ„ฐ 5๋“ฑ๊นŒ์ง€ ์žˆ๋‹ค. ๋‹น์ฒจ ๊ธฐ์ค€๊ณผ ๊ธˆ์•ก์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค. --[ ] ๋กœ๋˜ ๊ตฌ์ž… ๊ธˆ์•ก์„ ์ž…๋ ฅํ•˜๋ฉด ๊ตฌ์ž… ๊ธˆ์•ก์— ํ•ด๋‹นํ•˜๋Š” ๋งŒํผ ๋กœ๋˜๋ฅผ ๋ฐœํ–‰ํ•ด์•ผ ํ•œ๋‹ค. --[ ] ๋กœ๋˜ 1์žฅ์˜ ๊ฐ€๊ฒฉ์€ 1,000์›์ด๋‹ค. +-[x] ๋กœ๋˜ ๊ตฌ์ž… ๊ธˆ์•ก์„ ์ž…๋ ฅํ•˜๋ฉด ๊ตฌ์ž… ๊ธˆ์•ก์— ํ•ด๋‹นํ•˜๋Š” ๋งŒํผ ๋กœ๋˜๋ฅผ ๋ฐœํ–‰ํ•ด์•ผ ํ•œ๋‹ค. +-[x] ๋กœ๋˜ 1์žฅ์˜ ๊ฐ€๊ฒฉ์€ 1,000์›์ด๋‹ค. -[ ] ๋‹น์ฒจ ๋ฒˆํ˜ธ์™€ ๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค. -[ ]์‚ฌ์šฉ์ž๊ฐ€ ๊ตฌ๋งคํ•œ ๋กœ๋˜ ๋ฒˆํ˜ธ์™€ ๋‹น์ฒจ ๋ฒˆํ˜ธ๋ฅผ ๋น„๊ตํ•˜์—ฌ ๋‹น์ฒจ ๋‚ด์—ญ ๋ฐ ์ˆ˜์ต๋ฅ ์„ ์ถœ๋ ฅํ•˜๊ณ  ๋กœ๋˜ ๊ฒŒ์ž„์„ ์ข…๋ฃŒํ•œ๋‹ค. -[x] ์‚ฌ์šฉ์ž๊ฐ€ ์ž˜๋ชป๋œ ๊ฐ’์„ ์ž…๋ ฅํ•  ๊ฒฝ์šฐ `IllegalArgumentException`๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค๊ณ , "[ERROR]"๋กœ ์‹œ์ž‘ํ•˜๋Š” ์—๋Ÿฌ ๋ฉ”์‹œ์ง€๋ฅผ ์ถœ๋ ฅ ํ›„ ๊ทธ ๋ถ€๋ถ„๋ถ€ํ„ฐ ์ž…๋ ฅ์„ ๋‹ค์‹œ ๋ฐ›๋Š”๋‹ค. `Exception`์ด ์•„๋‹Œ `IllegalArgumentException`, `IllegalStateException` ๋“ฑ๊ณผ ๊ฐ™์€ ๋ช…ํ™•ํ•œ ์œ ํ˜•์„ ์ฒ˜๋ฆฌํ•œ๋‹ค. diff --git a/src/main/kotlin/lotto/Application.kt b/src/main/kotlin/lotto/Application.kt index 9739504ce..a0d53b22b 100644 --- a/src/main/kotlin/lotto/Application.kt +++ b/src/main/kotlin/lotto/Application.kt @@ -2,6 +2,9 @@ package lotto fun main() { val inputMoney =lottoMoneyInput() + val count = lottoCnt(inputMoney) + val generatedLottoList = lottoNumberLimit(count) + } fun lottoMoneyInput(): Int { println("๊ตฌ์ž…๊ธˆ์•ก์„ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.") @@ -11,3 +14,21 @@ fun lottoMoneyInput(): Int { } return inputMoney } + +fun lottoCnt(inputMoney: Int): Int { + return inputMoney / 1000 +} +fun lottoNumberLimit(count: Int): List> { + val comLottoList = mutableListOf>() + val randomNumber = java.util.Random() + repeat(count) { + val lotto = mutableSetOf() + while (lotto.size < 6) { + val num = randomNumber.nextInt(45) + 1 + lotto.add(num) + } + println("์ƒ์„ฑ๋œ ๋กœ๋˜ ๋ฒˆํ˜ธ: $lotto") + comLottoList.add(lotto.toList()) + } + return comLottoList +} From 8f214ed71a818aa814e3292f7cbff527b142be84 Mon Sep 17 00:00:00 2001 From: sooohi <68193613+sooohi@users.noreply.github.com> Date: Tue, 7 Nov 2023 23:12:46 +0900 Subject: [PATCH 04/19] =?UTF-8?q?feat=20:=20=ED=9A=9F=EC=88=98=20=EA=B8=B0?= =?UTF-8?q?=EB=8A=A5=20=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/lotto/Application.kt | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/lotto/Application.kt b/src/main/kotlin/lotto/Application.kt index a0d53b22b..c4d421c41 100644 --- a/src/main/kotlin/lotto/Application.kt +++ b/src/main/kotlin/lotto/Application.kt @@ -3,7 +3,8 @@ package lotto fun main() { val inputMoney =lottoMoneyInput() val count = lottoCnt(inputMoney) - val generatedLottoList = lottoNumberLimit(count) + val lottoList = lottoNumberLimit(count) + lottoNumberPrint(count, lottoList) } fun lottoMoneyInput(): Int { @@ -27,8 +28,14 @@ fun lottoNumberLimit(count: Int): List> { val num = randomNumber.nextInt(45) + 1 lotto.add(num) } - println("์ƒ์„ฑ๋œ ๋กœ๋˜ ๋ฒˆํ˜ธ: $lotto") comLottoList.add(lotto.toList()) } return comLottoList } +fun lottoNumberPrint(lottoCount: Int, lottoList: List>) { + println("$lottoCount ๊ฐœ๋ฅผ ๊ตฌ๋งคํ–ˆ์Šต๋‹ˆ๋‹ค.") + for (lotto in lottoList) { + println(lotto.sorted()) + } +} + From 92602ad2a6812a78758888468f28bdf06784fc37 Mon Sep 17 00:00:00 2001 From: sooohi <68193613+sooohi@users.noreply.github.com> Date: Tue, 7 Nov 2023 23:20:41 +0900 Subject: [PATCH 05/19] =?UTF-8?q?feat=20:=20=EB=8B=B9=EC=B2=A8=20=EB=B2=88?= =?UTF-8?q?=ED=98=B8=20=ED=99=95=EC=9D=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 3 ++- src/main/kotlin/lotto/Application.kt | 12 ++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/docs/README.md b/docs/README.md index f6ffef96b..fe2d13e60 100644 --- a/docs/README.md +++ b/docs/README.md @@ -11,7 +11,8 @@ -[ ] ๋‹น์ฒจ์€ 1๋“ฑ๋ถ€ํ„ฐ 5๋“ฑ๊นŒ์ง€ ์žˆ๋‹ค. ๋‹น์ฒจ ๊ธฐ์ค€๊ณผ ๊ธˆ์•ก์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค. -[x] ๋กœ๋˜ ๊ตฌ์ž… ๊ธˆ์•ก์„ ์ž…๋ ฅํ•˜๋ฉด ๊ตฌ์ž… ๊ธˆ์•ก์— ํ•ด๋‹นํ•˜๋Š” ๋งŒํผ ๋กœ๋˜๋ฅผ ๋ฐœํ–‰ํ•ด์•ผ ํ•œ๋‹ค. -[x] ๋กœ๋˜ 1์žฅ์˜ ๊ฐ€๊ฒฉ์€ 1,000์›์ด๋‹ค. --[ ] ๋‹น์ฒจ ๋ฒˆํ˜ธ์™€ ๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค. +-[x] ๋‹น์ฒจ ๋ฒˆํ˜ธ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค. +-[ ]๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค. -[ ]์‚ฌ์šฉ์ž๊ฐ€ ๊ตฌ๋งคํ•œ ๋กœ๋˜ ๋ฒˆํ˜ธ์™€ ๋‹น์ฒจ ๋ฒˆํ˜ธ๋ฅผ ๋น„๊ตํ•˜์—ฌ ๋‹น์ฒจ ๋‚ด์—ญ ๋ฐ ์ˆ˜์ต๋ฅ ์„ ์ถœ๋ ฅํ•˜๊ณ  ๋กœ๋˜ ๊ฒŒ์ž„์„ ์ข…๋ฃŒํ•œ๋‹ค. -[x] ์‚ฌ์šฉ์ž๊ฐ€ ์ž˜๋ชป๋œ ๊ฐ’์„ ์ž…๋ ฅํ•  ๊ฒฝ์šฐ `IllegalArgumentException`๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค๊ณ , "[ERROR]"๋กœ ์‹œ์ž‘ํ•˜๋Š” ์—๋Ÿฌ ๋ฉ”์‹œ์ง€๋ฅผ ์ถœ๋ ฅ ํ›„ ๊ทธ ๋ถ€๋ถ„๋ถ€ํ„ฐ ์ž…๋ ฅ์„ ๋‹ค์‹œ ๋ฐ›๋Š”๋‹ค. `Exception`์ด ์•„๋‹Œ `IllegalArgumentException`, `IllegalStateException` ๋“ฑ๊ณผ ๊ฐ™์€ ๋ช…ํ™•ํ•œ ์œ ํ˜•์„ ์ฒ˜๋ฆฌํ•œ๋‹ค. diff --git a/src/main/kotlin/lotto/Application.kt b/src/main/kotlin/lotto/Application.kt index c4d421c41..8c6007e22 100644 --- a/src/main/kotlin/lotto/Application.kt +++ b/src/main/kotlin/lotto/Application.kt @@ -6,6 +6,9 @@ fun main() { val lottoList = lottoNumberLimit(count) lottoNumberPrint(count, lottoList) + val lottoNumber = lottoNumberChoose() + println("๋‹น์ฒจ๋œ ๋กœ๋˜ ๋ฒˆํ˜ธ: $lottoNumber") + } fun lottoMoneyInput(): Int { println("๊ตฌ์ž…๊ธˆ์•ก์„ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.") @@ -39,3 +42,12 @@ fun lottoNumberPrint(lottoCount: Int, lottoList: List>) { } } +fun lottoNumberChoose(): List { + println("๋‹น์ฒจ ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.") + val lottoNumber = readLine()!!.split(",").map { it.toInt() } + if (lottoNumber.size != 6) { + throw IllegalArgumentException("6๊ฐœ์˜ ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.") + } + return lottoNumber +} + From d41a55de1e71560d8cf7ccfb6da4fdc0ee19a06f Mon Sep 17 00:00:00 2001 From: sooohi <68193613+sooohi@users.noreply.github.com> Date: Tue, 7 Nov 2023 23:21:52 +0900 Subject: [PATCH 06/19] =?UTF-8?q?fix=20:=20=EB=8B=B9=EC=B2=A8=20=EB=B2=88?= =?UTF-8?q?=ED=98=B8=20=EC=A4=91=EB=B3=B5=20=EC=A0=9C=EA=B1=B0=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/lotto/Application.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/lotto/Application.kt b/src/main/kotlin/lotto/Application.kt index 8c6007e22..7782bdb3b 100644 --- a/src/main/kotlin/lotto/Application.kt +++ b/src/main/kotlin/lotto/Application.kt @@ -45,8 +45,8 @@ fun lottoNumberPrint(lottoCount: Int, lottoList: List>) { fun lottoNumberChoose(): List { println("๋‹น์ฒจ ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.") val lottoNumber = readLine()!!.split(",").map { it.toInt() } - if (lottoNumber.size != 6) { - throw IllegalArgumentException("6๊ฐœ์˜ ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.") + if (lottoNumber.size != 6 || lottoNumber.toSet().size != 6) { + throw IllegalArgumentException("์ค‘๋ณต๋˜๋Š” ๋ฒˆํ˜ธ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.") } return lottoNumber } From 3336bd343232bf0431d701c3bb9bce7091429332 Mon Sep 17 00:00:00 2001 From: sooohi <68193613+sooohi@users.noreply.github.com> Date: Tue, 7 Nov 2023 23:24:08 +0900 Subject: [PATCH 07/19] =?UTF-8?q?feat=20:=20=EB=B3=B4=EB=84=88=EC=8A=A4=20?= =?UTF-8?q?=EB=B2=88=ED=98=B8=20=EC=9E=85=EB=A0=A5=EB=B0=9B=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 4 ++-- src/main/kotlin/lotto/Application.kt | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/docs/README.md b/docs/README.md index fe2d13e60..148afaf69 100644 --- a/docs/README.md +++ b/docs/README.md @@ -7,12 +7,12 @@ -[x] ๊ตฌ๋งค๊ธ‰์•ก ์ž…๋ ฅ๋ฐ›๊ธฐ -[x] ๋กœ๋˜ ๋ฒˆํ˜ธ์˜ ์ˆซ์ž ๋ฒ”์œ„๋Š” 1~45๊นŒ์ง€์ด๋‹ค. -[x] 1๊ฐœ์˜ ๋กœ๋˜๋ฅผ ๋ฐœํ–‰ํ•  ๋•Œ ์ค‘๋ณต๋˜์ง€ ์•Š๋Š” 6๊ฐœ์˜ ์ˆซ์ž๋ฅผ ๋ฝ‘๋Š”๋‹ค. --[ ] ๋‹น์ฒจ ๋ฒˆํ˜ธ ์ถ”์ฒจ ์‹œ ์ค‘๋ณต๋˜์ง€ ์•Š๋Š” ์ˆซ์ž 6๊ฐœ์™€ ๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ 1๊ฐœ๋ฅผ ๋ฝ‘๋Š”๋‹ค. +-[x] ๋‹น์ฒจ ๋ฒˆํ˜ธ ์ถ”์ฒจ ์‹œ ์ค‘๋ณต๋˜์ง€ ์•Š๋Š” ์ˆซ์ž 6๊ฐœ์™€ ๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ 1๊ฐœ๋ฅผ ๋ฝ‘๋Š”๋‹ค. -[ ] ๋‹น์ฒจ์€ 1๋“ฑ๋ถ€ํ„ฐ 5๋“ฑ๊นŒ์ง€ ์žˆ๋‹ค. ๋‹น์ฒจ ๊ธฐ์ค€๊ณผ ๊ธˆ์•ก์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค. -[x] ๋กœ๋˜ ๊ตฌ์ž… ๊ธˆ์•ก์„ ์ž…๋ ฅํ•˜๋ฉด ๊ตฌ์ž… ๊ธˆ์•ก์— ํ•ด๋‹นํ•˜๋Š” ๋งŒํผ ๋กœ๋˜๋ฅผ ๋ฐœํ–‰ํ•ด์•ผ ํ•œ๋‹ค. -[x] ๋กœ๋˜ 1์žฅ์˜ ๊ฐ€๊ฒฉ์€ 1,000์›์ด๋‹ค. -[x] ๋‹น์ฒจ ๋ฒˆํ˜ธ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค. --[ ]๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค. +-[x]๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค. -[ ]์‚ฌ์šฉ์ž๊ฐ€ ๊ตฌ๋งคํ•œ ๋กœ๋˜ ๋ฒˆํ˜ธ์™€ ๋‹น์ฒจ ๋ฒˆํ˜ธ๋ฅผ ๋น„๊ตํ•˜์—ฌ ๋‹น์ฒจ ๋‚ด์—ญ ๋ฐ ์ˆ˜์ต๋ฅ ์„ ์ถœ๋ ฅํ•˜๊ณ  ๋กœ๋˜ ๊ฒŒ์ž„์„ ์ข…๋ฃŒํ•œ๋‹ค. -[x] ์‚ฌ์šฉ์ž๊ฐ€ ์ž˜๋ชป๋œ ๊ฐ’์„ ์ž…๋ ฅํ•  ๊ฒฝ์šฐ `IllegalArgumentException`๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค๊ณ , "[ERROR]"๋กœ ์‹œ์ž‘ํ•˜๋Š” ์—๋Ÿฌ ๋ฉ”์‹œ์ง€๋ฅผ ์ถœ๋ ฅ ํ›„ ๊ทธ ๋ถ€๋ถ„๋ถ€ํ„ฐ ์ž…๋ ฅ์„ ๋‹ค์‹œ ๋ฐ›๋Š”๋‹ค. `Exception`์ด ์•„๋‹Œ `IllegalArgumentException`, `IllegalStateException` ๋“ฑ๊ณผ ๊ฐ™์€ ๋ช…ํ™•ํ•œ ์œ ํ˜•์„ ์ฒ˜๋ฆฌํ•œ๋‹ค. diff --git a/src/main/kotlin/lotto/Application.kt b/src/main/kotlin/lotto/Application.kt index 7782bdb3b..21ce433d1 100644 --- a/src/main/kotlin/lotto/Application.kt +++ b/src/main/kotlin/lotto/Application.kt @@ -8,6 +8,7 @@ fun main() { val lottoNumber = lottoNumberChoose() println("๋‹น์ฒจ๋œ ๋กœ๋˜ ๋ฒˆํ˜ธ: $lottoNumber") + val bonusNumber = lottoNumberBonus() } fun lottoMoneyInput(): Int { @@ -50,4 +51,9 @@ fun lottoNumberChoose(): List { } return lottoNumber } +fun lottoNumberBonus(): Int { + println("๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.") + return readLine()!!.toInt() +} + From 0ebb6430234626383c0476adb3fd1939992ac62c Mon Sep 17 00:00:00 2001 From: sooohi <68193613+sooohi@users.noreply.github.com> Date: Tue, 7 Nov 2023 23:42:42 +0900 Subject: [PATCH 08/19] =?UTF-8?q?feat=20:=20=EC=9D=BC=EC=B9=98=ED=95=98?= =?UTF-8?q?=EB=8A=94=20=EB=B2=88=ED=98=B8=20=EA=B0=9C=EC=88=98=20=ED=99=95?= =?UTF-8?q?=EC=9D=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 1 + src/main/kotlin/lotto/Application.kt | 28 +++++++++++++++++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/docs/README.md b/docs/README.md index 148afaf69..ee2916598 100644 --- a/docs/README.md +++ b/docs/README.md @@ -8,6 +8,7 @@ -[x] ๋กœ๋˜ ๋ฒˆํ˜ธ์˜ ์ˆซ์ž ๋ฒ”์œ„๋Š” 1~45๊นŒ์ง€์ด๋‹ค. -[x] 1๊ฐœ์˜ ๋กœ๋˜๋ฅผ ๋ฐœํ–‰ํ•  ๋•Œ ์ค‘๋ณต๋˜์ง€ ์•Š๋Š” 6๊ฐœ์˜ ์ˆซ์ž๋ฅผ ๋ฝ‘๋Š”๋‹ค. -[x] ๋‹น์ฒจ ๋ฒˆํ˜ธ ์ถ”์ฒจ ์‹œ ์ค‘๋ณต๋˜์ง€ ์•Š๋Š” ์ˆซ์ž 6๊ฐœ์™€ ๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ 1๊ฐœ๋ฅผ ๋ฝ‘๋Š”๋‹ค. +-[x] ์ผ์น˜ํ•˜๋Š” ๋กฏ๋„ ๋ฒˆํ˜ธ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•œ๋‹ค. -[ ] ๋‹น์ฒจ์€ 1๋“ฑ๋ถ€ํ„ฐ 5๋“ฑ๊นŒ์ง€ ์žˆ๋‹ค. ๋‹น์ฒจ ๊ธฐ์ค€๊ณผ ๊ธˆ์•ก์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค. -[x] ๋กœ๋˜ ๊ตฌ์ž… ๊ธˆ์•ก์„ ์ž…๋ ฅํ•˜๋ฉด ๊ตฌ์ž… ๊ธˆ์•ก์— ํ•ด๋‹นํ•˜๋Š” ๋งŒํผ ๋กœ๋˜๋ฅผ ๋ฐœํ–‰ํ•ด์•ผ ํ•œ๋‹ค. -[x] ๋กœ๋˜ 1์žฅ์˜ ๊ฐ€๊ฒฉ์€ 1,000์›์ด๋‹ค. diff --git a/src/main/kotlin/lotto/Application.kt b/src/main/kotlin/lotto/Application.kt index 21ce433d1..57316d93e 100644 --- a/src/main/kotlin/lotto/Application.kt +++ b/src/main/kotlin/lotto/Application.kt @@ -7,9 +7,13 @@ fun main() { lottoNumberPrint(count, lottoList) val lottoNumber = lottoNumberChoose() - println("๋‹น์ฒจ๋œ ๋กœ๋˜ ๋ฒˆํ˜ธ: $lottoNumber") val bonusNumber = lottoNumberBonus() + val result = lottoNumberCheck(lottoList, lottoNumber, bonusNumber) + print(result) + + + } fun lottoMoneyInput(): Int { println("๊ตฌ์ž…๊ธˆ์•ก์„ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.") @@ -57,3 +61,25 @@ fun lottoNumberBonus(): Int { } +fun lottoNumberCheck(lottoList: List>, comNumber: List, bonusNumber: Int): Map { + val lottoMoneyList = mutableMapOf(3 to 0, 4 to 0, 5 to 0, 6 to 0) + var bonusNumberCnt = 0 + for (lotto in lottoList) { + val sameNumber = lotto.filter { it in comNumber }.size + when (sameNumber) { + 3 -> lottoMoneyList[3] = lottoMoneyList.getValue(3) + 1 + 4 -> lottoMoneyList[4] = lottoMoneyList.getValue(4) + 1 + 5 -> { + if (lotto.contains(bonusNumber)) { + bonusNumberCnt++ + } else { + lottoMoneyList[5] = lottoMoneyList.getValue(5) + 1 + } + } + 6 -> lottoMoneyList[6] = lottoMoneyList.getValue(6) + 1 + } + } + lottoMoneyList[5] = bonusNumberCnt + return lottoMoneyList +} + From de374e313e52ff93f65a0c1eb85d4da4af57c512 Mon Sep 17 00:00:00 2001 From: sooohi <68193613+sooohi@users.noreply.github.com> Date: Tue, 7 Nov 2023 23:52:00 +0900 Subject: [PATCH 09/19] =?UTF-8?q?feat=20:=20=EB=8B=B9=EC=B2=A8=20=ED=86=B5?= =?UTF-8?q?=EA=B3=84=20=EB=B6=80=EB=B6=84=20=EC=9E=91=EC=97=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/lotto/Application.kt | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/lotto/Application.kt b/src/main/kotlin/lotto/Application.kt index 57316d93e..7bed24667 100644 --- a/src/main/kotlin/lotto/Application.kt +++ b/src/main/kotlin/lotto/Application.kt @@ -10,7 +10,7 @@ fun main() { val bonusNumber = lottoNumberBonus() val result = lottoNumberCheck(lottoList, lottoNumber, bonusNumber) - print(result) + lottoResultPrint(result, count) @@ -83,3 +83,11 @@ fun lottoNumberCheck(lottoList: List>, comNumber: List, bonusNumb return lottoMoneyList } +fun lottoResultPrint(result: Map, lottoCount: Int) { + println("๋‹น์ฒจ ํ†ต๊ณ„") + println("---") + for ((key, value) in result) { + println("${key}๊ฐœ ์ผ์น˜ - ${value}๊ฐœ") + } +} + From 3001d2765cb84031e0df6cffce33c611562df7b0 Mon Sep 17 00:00:00 2001 From: sooohi <68193613+sooohi@users.noreply.github.com> Date: Wed, 8 Nov 2023 00:25:52 +0900 Subject: [PATCH 10/19] =?UTF-8?q?fix=20:=205=EB=93=B1=20=EB=8B=B9=EC=B2=A8?= =?UTF-8?q?=20=EC=97=90=EB=9F=AC=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 2 +- src/main/kotlin/lotto/Application.kt | 36 +++++++++++++++------------- 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/docs/README.md b/docs/README.md index ee2916598..5e8904b45 100644 --- a/docs/README.md +++ b/docs/README.md @@ -9,7 +9,7 @@ -[x] 1๊ฐœ์˜ ๋กœ๋˜๋ฅผ ๋ฐœํ–‰ํ•  ๋•Œ ์ค‘๋ณต๋˜์ง€ ์•Š๋Š” 6๊ฐœ์˜ ์ˆซ์ž๋ฅผ ๋ฝ‘๋Š”๋‹ค. -[x] ๋‹น์ฒจ ๋ฒˆํ˜ธ ์ถ”์ฒจ ์‹œ ์ค‘๋ณต๋˜์ง€ ์•Š๋Š” ์ˆซ์ž 6๊ฐœ์™€ ๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ 1๊ฐœ๋ฅผ ๋ฝ‘๋Š”๋‹ค. -[x] ์ผ์น˜ํ•˜๋Š” ๋กฏ๋„ ๋ฒˆํ˜ธ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•œ๋‹ค. --[ ] ๋‹น์ฒจ์€ 1๋“ฑ๋ถ€ํ„ฐ 5๋“ฑ๊นŒ์ง€ ์žˆ๋‹ค. ๋‹น์ฒจ ๊ธฐ์ค€๊ณผ ๊ธˆ์•ก์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค. +-[x] ๋‹น์ฒจ์€ 1๋“ฑ๋ถ€ํ„ฐ 5๋“ฑ๊นŒ์ง€ ์žˆ๋‹ค. ๋‹น์ฒจ ๊ธฐ์ค€๊ณผ ๊ธˆ์•ก์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค. -[x] ๋กœ๋˜ ๊ตฌ์ž… ๊ธˆ์•ก์„ ์ž…๋ ฅํ•˜๋ฉด ๊ตฌ์ž… ๊ธˆ์•ก์— ํ•ด๋‹นํ•˜๋Š” ๋งŒํผ ๋กœ๋˜๋ฅผ ๋ฐœํ–‰ํ•ด์•ผ ํ•œ๋‹ค. -[x] ๋กœ๋˜ 1์žฅ์˜ ๊ฐ€๊ฒฉ์€ 1,000์›์ด๋‹ค. -[x] ๋‹น์ฒจ ๋ฒˆํ˜ธ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค. diff --git a/src/main/kotlin/lotto/Application.kt b/src/main/kotlin/lotto/Application.kt index 7bed24667..9fb6c3b65 100644 --- a/src/main/kotlin/lotto/Application.kt +++ b/src/main/kotlin/lotto/Application.kt @@ -1,7 +1,7 @@ package lotto fun main() { - val inputMoney =lottoMoneyInput() + val inputMoney = lottoMoneyInput() val count = lottoCnt(inputMoney) val lottoList = lottoNumberLimit(count) lottoNumberPrint(count, lottoList) @@ -11,10 +11,8 @@ fun main() { val result = lottoNumberCheck(lottoList, lottoNumber, bonusNumber) lottoResultPrint(result, count) - - - } + fun lottoMoneyInput(): Int { println("๊ตฌ์ž…๊ธˆ์•ก์„ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.") val inputMoney = readLine()!!.toInt() @@ -27,6 +25,7 @@ fun lottoMoneyInput(): Int { fun lottoCnt(inputMoney: Int): Int { return inputMoney / 1000 } + fun lottoNumberLimit(count: Int): List> { val comLottoList = mutableListOf>() val randomNumber = java.util.Random() @@ -40,6 +39,7 @@ fun lottoNumberLimit(count: Int): List> { } return comLottoList } + fun lottoNumberPrint(lottoCount: Int, lottoList: List>) { println("$lottoCount ๊ฐœ๋ฅผ ๊ตฌ๋งคํ–ˆ์Šต๋‹ˆ๋‹ค.") for (lotto in lottoList) { @@ -55,39 +55,41 @@ fun lottoNumberChoose(): List { } return lottoNumber } + fun lottoNumberBonus(): Int { println("๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.") return readLine()!!.toInt() } - -fun lottoNumberCheck(lottoList: List>, comNumber: List, bonusNumber: Int): Map { - val lottoMoneyList = mutableMapOf(3 to 0, 4 to 0, 5 to 0, 6 to 0) - var bonusNumberCnt = 0 +fun lottoNumberCheck(lottoList: List>, comNumber: List, bonusNumber: Int): Map { + val lottoMoneyList = mutableMapOf("3๊ฐœ ์ผ์น˜" to 0, "4๊ฐœ ์ผ์น˜" to 0, "5๊ฐœ ์ผ์น˜" to 0, "5๊ฐœ ์ผ์น˜, ๋ณด๋„ˆ์Šค ๋ณผ ์ผ์น˜" to 0, "6๊ฐœ ์ผ์น˜" to 0) for (lotto in lottoList) { val sameNumber = lotto.filter { it in comNumber }.size when (sameNumber) { - 3 -> lottoMoneyList[3] = lottoMoneyList.getValue(3) + 1 - 4 -> lottoMoneyList[4] = lottoMoneyList.getValue(4) + 1 + 3 -> lottoMoneyList["3๊ฐœ ์ผ์น˜"] = lottoMoneyList.getValue("3๊ฐœ ์ผ์น˜") + 1 + 4 -> lottoMoneyList["4๊ฐœ ์ผ์น˜"] = lottoMoneyList.getValue("4๊ฐœ ์ผ์น˜") + 1 5 -> { if (lotto.contains(bonusNumber)) { - bonusNumberCnt++ + lottoMoneyList["5๊ฐœ ์ผ์น˜, ๋ณด๋„ˆ์Šค ๋ณผ ์ผ์น˜"] = lottoMoneyList.getValue("5๊ฐœ ์ผ์น˜, ๋ณด๋„ˆ์Šค ๋ณผ ์ผ์น˜") + 1 } else { - lottoMoneyList[5] = lottoMoneyList.getValue(5) + 1 + lottoMoneyList["5๊ฐœ ์ผ์น˜"] = lottoMoneyList.getValue("5๊ฐœ ์ผ์น˜") + 1 } } - 6 -> lottoMoneyList[6] = lottoMoneyList.getValue(6) + 1 + 6 -> lottoMoneyList["6๊ฐœ ์ผ์น˜"] = lottoMoneyList.getValue("6๊ฐœ ์ผ์น˜") + 1 } } - lottoMoneyList[5] = bonusNumberCnt return lottoMoneyList } -fun lottoResultPrint(result: Map, lottoCount: Int) { +fun lottoResultPrint(result: Map, lottoCount: Int) { println("๋‹น์ฒจ ํ†ต๊ณ„") println("---") + val prizeMoney = mapOf("3๊ฐœ ์ผ์น˜" to 5000, "4๊ฐœ ์ผ์น˜" to 50000, "5๊ฐœ ์ผ์น˜" to 1500000, "5๊ฐœ ์ผ์น˜, ๋ณด๋„ˆ์Šค ๋ณผ ์ผ์น˜" to 30000000, "6๊ฐœ ์ผ์น˜" to 2000000000) for ((key, value) in result) { - println("${key}๊ฐœ ์ผ์น˜ - ${value}๊ฐœ") + val prize = when (key) { + "5๊ฐœ ์ผ์น˜" -> if (result["5๊ฐœ ์ผ์น˜, ๋ณด๋„ˆ์Šค ๋ณผ ์ผ์น˜"] == 0) "1,500,000์›" else "30,000,000์›" + else -> "${prizeMoney[key]}์›" + } + println("$key ($prize) - ${value}๊ฐœ") } } - From 95e33a24bfdab8f9361ac32ced2ea867407c624d Mon Sep 17 00:00:00 2001 From: sooohi <68193613+sooohi@users.noreply.github.com> Date: Wed, 8 Nov 2023 00:34:44 +0900 Subject: [PATCH 11/19] =?UTF-8?q?feat=20:=20=EC=B4=9D=EC=88=98=EC=9D=B5?= =?UTF-8?q?=EB=A5=A0=20=EC=B6=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 2 +- src/main/kotlin/lotto/Application.kt | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/docs/README.md b/docs/README.md index 5e8904b45..0665c8d55 100644 --- a/docs/README.md +++ b/docs/README.md @@ -14,7 +14,7 @@ -[x] ๋กœ๋˜ 1์žฅ์˜ ๊ฐ€๊ฒฉ์€ 1,000์›์ด๋‹ค. -[x] ๋‹น์ฒจ ๋ฒˆํ˜ธ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค. -[x]๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค. --[ ]์‚ฌ์šฉ์ž๊ฐ€ ๊ตฌ๋งคํ•œ ๋กœ๋˜ ๋ฒˆํ˜ธ์™€ ๋‹น์ฒจ ๋ฒˆํ˜ธ๋ฅผ ๋น„๊ตํ•˜์—ฌ ๋‹น์ฒจ ๋‚ด์—ญ ๋ฐ ์ˆ˜์ต๋ฅ ์„ ์ถœ๋ ฅํ•˜๊ณ  ๋กœ๋˜ ๊ฒŒ์ž„์„ ์ข…๋ฃŒํ•œ๋‹ค. +-[x]์‚ฌ์šฉ์ž๊ฐ€ ๊ตฌ๋งคํ•œ ๋กœ๋˜ ๋ฒˆํ˜ธ์™€ ๋‹น์ฒจ ๋ฒˆํ˜ธ๋ฅผ ๋น„๊ตํ•˜์—ฌ ๋‹น์ฒจ ๋‚ด์—ญ ๋ฐ ์ˆ˜์ต๋ฅ ์„ ์ถœ๋ ฅํ•˜๊ณ  ๋กœ๋˜ ๊ฒŒ์ž„์„ ์ข…๋ฃŒํ•œ๋‹ค. -[x] ์‚ฌ์šฉ์ž๊ฐ€ ์ž˜๋ชป๋œ ๊ฐ’์„ ์ž…๋ ฅํ•  ๊ฒฝ์šฐ `IllegalArgumentException`๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค๊ณ , "[ERROR]"๋กœ ์‹œ์ž‘ํ•˜๋Š” ์—๋Ÿฌ ๋ฉ”์‹œ์ง€๋ฅผ ์ถœ๋ ฅ ํ›„ ๊ทธ ๋ถ€๋ถ„๋ถ€ํ„ฐ ์ž…๋ ฅ์„ ๋‹ค์‹œ ๋ฐ›๋Š”๋‹ค. `Exception`์ด ์•„๋‹Œ `IllegalArgumentException`, `IllegalStateException` ๋“ฑ๊ณผ ๊ฐ™์€ ๋ช…ํ™•ํ•œ ์œ ํ˜•์„ ์ฒ˜๋ฆฌํ•œ๋‹ค. ### ์ถ”๊ฐ€๋œ ์š”๊ตฌ ์‚ฌํ•ญ diff --git a/src/main/kotlin/lotto/Application.kt b/src/main/kotlin/lotto/Application.kt index 9fb6c3b65..0a4570b3f 100644 --- a/src/main/kotlin/lotto/Application.kt +++ b/src/main/kotlin/lotto/Application.kt @@ -85,11 +85,20 @@ fun lottoResultPrint(result: Map, lottoCount: Int) { println("๋‹น์ฒจ ํ†ต๊ณ„") println("---") val prizeMoney = mapOf("3๊ฐœ ์ผ์น˜" to 5000, "4๊ฐœ ์ผ์น˜" to 50000, "5๊ฐœ ์ผ์น˜" to 1500000, "5๊ฐœ ์ผ์น˜, ๋ณด๋„ˆ์Šค ๋ณผ ์ผ์น˜" to 30000000, "6๊ฐœ ์ผ์น˜" to 2000000000) + var totalPrize = 0 for ((key, value) in result) { val prize = when (key) { "5๊ฐœ ์ผ์น˜" -> if (result["5๊ฐœ ์ผ์น˜, ๋ณด๋„ˆ์Šค ๋ณผ ์ผ์น˜"] == 0) "1,500,000์›" else "30,000,000์›" else -> "${prizeMoney[key]}์›" } println("$key ($prize) - ${value}๊ฐœ") + totalPrize += if (key == "5๊ฐœ ์ผ์น˜") { + if (result["5๊ฐœ ์ผ์น˜, ๋ณด๋„ˆ์Šค ๋ณผ ์ผ์น˜"] == 0) prizeMoney["5๊ฐœ ์ผ์น˜"]!! * value else prizeMoney["5๊ฐœ ์ผ์น˜, ๋ณด๋„ˆ์Šค ๋ณผ ์ผ์น˜"]!! * value + } else { + prizeMoney[key]!! * value + } } + val inputMoney = lottoCount * 1000 + val rateOfReturn = ((totalPrize - inputMoney) / inputMoney.toDouble() * 100).coerceAtLeast(0.0) + println("์ด ์ˆ˜์ต๋ฅ ์€ ${"%.1f".format(rateOfReturn)}%์ž…๋‹ˆ๋‹ค.") } From 63a5b5d9273c537bc2d35472c0208d348167750c Mon Sep 17 00:00:00 2001 From: sooohi <68193613+sooohi@users.noreply.github.com> Date: Wed, 8 Nov 2023 00:58:31 +0900 Subject: [PATCH 12/19] =?UTF-8?q?feat=20:=20camp.nextstep.edu.missionutils?= =?UTF-8?q?=EC=97=90=EC=84=9C=20=EC=A0=9C=EA=B3=B5=ED=95=98=EB=8A=94=20Ran?= =?UTF-8?q?doms=20=EB=B0=8F=20Console=20API=EB=A5=BC=20=EC=82=AC=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 2 +- src/main/kotlin/lotto/Application.kt | 39 ++++++++++++++++++---------- 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/docs/README.md b/docs/README.md index 0665c8d55..ed2a92e0c 100644 --- a/docs/README.md +++ b/docs/README.md @@ -28,7 +28,7 @@ -[ ] ๋„๋ฉ”์ธ ๋กœ์ง์— ๋‹จ์œ„ ํ…Œ์ŠคํŠธ๋ฅผ ๊ตฌํ˜„ํ•ด์•ผ ํ•œ๋‹ค. ๋‹จ, UI(System.out, System.in, Scanner) ๋กœ์ง์€ ์ œ์™ธํ•œ๋‹ค. - ํ•ต์‹ฌ ๋กœ์ง์„ ๊ตฌํ˜„ํ•˜๋Š” ์ฝ”๋“œ์™€ UI๋ฅผ ๋‹ด๋‹นํ•˜๋Š” ๋กœ์ง์„ ๋ถ„๋ฆฌํ•ด ๊ตฌํ˜„ํ•œ๋‹ค. - ๋‹จ์œ„ ํ…Œ์ŠคํŠธ ์ž‘์„ฑ์ด ์ต์ˆ™ํ•˜์ง€ ์•Š๋‹ค๋ฉด `test/kotlin/lotto/LottoTest`๋ฅผ ์ฐธ๊ณ ํ•˜์—ฌ ํ•™์Šตํ•œ ํ›„ ํ…Œ์ŠคํŠธ๋ฅผ ๊ตฌํ˜„ํ•œ๋‹ค. --[ ] `camp.nextstep.edu.missionutils`์—์„œ ์ œ๊ณตํ•˜๋Š” `Randoms` ๋ฐ `Console` API๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ตฌํ˜„ํ•ด์•ผ ํ•œ๋‹ค. +-[x] `camp.nextstep.edu.missionutils`์—์„œ ์ œ๊ณตํ•˜๋Š” `Randoms` ๋ฐ `Console` API๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ตฌํ˜„ํ•ด์•ผ ํ•œ๋‹ค. - Random ๊ฐ’ ์ถ”์ถœ์€ `camp.nextstep.edu.missionutils.Randoms`์˜ `pickUniqueNumbersInRange()`๋ฅผ ํ™œ์šฉํ•œ๋‹ค. - ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•˜๋Š” ๊ฐ’์€ `camp.nextstep.edu.missionutils.Console`์˜ `readLine()`์„ ํ™œ์šฉํ•œ๋‹ค. -[ ] ์ œ๊ณต๋œ `Lotto` ํด๋ž˜์Šค๋ฅผ ํ™œ์šฉํ•ด ๊ตฌํ˜„ํ•ด์•ผ ํ•œ๋‹ค. diff --git a/src/main/kotlin/lotto/Application.kt b/src/main/kotlin/lotto/Application.kt index 0a4570b3f..6d4a3ba08 100644 --- a/src/main/kotlin/lotto/Application.kt +++ b/src/main/kotlin/lotto/Application.kt @@ -1,5 +1,8 @@ package lotto +import camp.nextstep.edu.missionutils.Randoms +import camp.nextstep.edu.missionutils.Console + fun main() { val inputMoney = lottoMoneyInput() val count = lottoCnt(inputMoney) @@ -15,7 +18,7 @@ fun main() { fun lottoMoneyInput(): Int { println("๊ตฌ์ž…๊ธˆ์•ก์„ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.") - val inputMoney = readLine()!!.toInt() + val inputMoney = Console.readLine().toInt() if (inputMoney % 1000 != 0) { throw IllegalArgumentException("1000์› ๋‹จ์œ„๋กœ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.") } @@ -28,28 +31,23 @@ fun lottoCnt(inputMoney: Int): Int { fun lottoNumberLimit(count: Int): List> { val comLottoList = mutableListOf>() - val randomNumber = java.util.Random() repeat(count) { - val lotto = mutableSetOf() - while (lotto.size < 6) { - val num = randomNumber.nextInt(45) + 1 - lotto.add(num) - } - comLottoList.add(lotto.toList()) + val numbers = Randoms.pickUniqueNumbersInRange(1, 45, 6) + comLottoList.add(numbers) } return comLottoList } fun lottoNumberPrint(lottoCount: Int, lottoList: List>) { println("$lottoCount ๊ฐœ๋ฅผ ๊ตฌ๋งคํ–ˆ์Šต๋‹ˆ๋‹ค.") - for (lotto in lottoList) { + lottoList.forEach { lotto -> println(lotto.sorted()) } } fun lottoNumberChoose(): List { println("๋‹น์ฒจ ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.") - val lottoNumber = readLine()!!.split(",").map { it.toInt() } + val lottoNumber = Console.readLine().split(",").map { it.toInt() } if (lottoNumber.size != 6 || lottoNumber.toSet().size != 6) { throw IllegalArgumentException("์ค‘๋ณต๋˜๋Š” ๋ฒˆํ˜ธ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.") } @@ -58,11 +56,17 @@ fun lottoNumberChoose(): List { fun lottoNumberBonus(): Int { println("๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.") - return readLine()!!.toInt() + return Console.readLine().toInt() } fun lottoNumberCheck(lottoList: List>, comNumber: List, bonusNumber: Int): Map { - val lottoMoneyList = mutableMapOf("3๊ฐœ ์ผ์น˜" to 0, "4๊ฐœ ์ผ์น˜" to 0, "5๊ฐœ ์ผ์น˜" to 0, "5๊ฐœ ์ผ์น˜, ๋ณด๋„ˆ์Šค ๋ณผ ์ผ์น˜" to 0, "6๊ฐœ ์ผ์น˜" to 0) + val lottoMoneyList = mutableMapOf( + "3๊ฐœ ์ผ์น˜" to 0, + "4๊ฐœ ์ผ์น˜" to 0, + "5๊ฐœ ์ผ์น˜" to 0, + "5๊ฐœ ์ผ์น˜, ๋ณด๋„ˆ์Šค ๋ณผ ์ผ์น˜" to 0, + "6๊ฐœ ์ผ์น˜" to 0 + ) for (lotto in lottoList) { val sameNumber = lotto.filter { it in comNumber }.size when (sameNumber) { @@ -70,7 +74,8 @@ fun lottoNumberCheck(lottoList: List>, comNumber: List, bonusNumb 4 -> lottoMoneyList["4๊ฐœ ์ผ์น˜"] = lottoMoneyList.getValue("4๊ฐœ ์ผ์น˜") + 1 5 -> { if (lotto.contains(bonusNumber)) { - lottoMoneyList["5๊ฐœ ์ผ์น˜, ๋ณด๋„ˆ์Šค ๋ณผ ์ผ์น˜"] = lottoMoneyList.getValue("5๊ฐœ ์ผ์น˜, ๋ณด๋„ˆ์Šค ๋ณผ ์ผ์น˜") + 1 + lottoMoneyList["5๊ฐœ ์ผ์น˜, ๋ณด๋„ˆ์Šค ๋ณผ ์ผ์น˜"] = + lottoMoneyList.getValue("5๊ฐœ ์ผ์น˜, ๋ณด๋„ˆ์Šค ๋ณผ ์ผ์น˜") + 1 } else { lottoMoneyList["5๊ฐœ ์ผ์น˜"] = lottoMoneyList.getValue("5๊ฐœ ์ผ์น˜") + 1 } @@ -84,7 +89,13 @@ fun lottoNumberCheck(lottoList: List>, comNumber: List, bonusNumb fun lottoResultPrint(result: Map, lottoCount: Int) { println("๋‹น์ฒจ ํ†ต๊ณ„") println("---") - val prizeMoney = mapOf("3๊ฐœ ์ผ์น˜" to 5000, "4๊ฐœ ์ผ์น˜" to 50000, "5๊ฐœ ์ผ์น˜" to 1500000, "5๊ฐœ ์ผ์น˜, ๋ณด๋„ˆ์Šค ๋ณผ ์ผ์น˜" to 30000000, "6๊ฐœ ์ผ์น˜" to 2000000000) + val prizeMoney = mapOf( + "3๊ฐœ ์ผ์น˜" to 5000, + "4๊ฐœ ์ผ์น˜" to 50000, + "5๊ฐœ ์ผ์น˜" to 1500000, + "5๊ฐœ ์ผ์น˜, ๋ณด๋„ˆ์Šค ๋ณผ ์ผ์น˜" to 30000000, + "6๊ฐœ ์ผ์น˜" to 2000000000 + ) var totalPrize = 0 for ((key, value) in result) { val prize = when (key) { From b90769953ce58141cdb680480929d1aa12083448 Mon Sep 17 00:00:00 2001 From: sooohi <68193613+sooohi@users.noreply.github.com> Date: Wed, 8 Nov 2023 01:03:22 +0900 Subject: [PATCH 13/19] =?UTF-8?q?feat=20:=20[ERROR]=EB=A9=94=EC=8B=9C?= =?UTF-8?q?=EC=A7=80=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/lotto/Application.kt | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/src/main/kotlin/lotto/Application.kt b/src/main/kotlin/lotto/Application.kt index 6d4a3ba08..5552774b3 100644 --- a/src/main/kotlin/lotto/Application.kt +++ b/src/main/kotlin/lotto/Application.kt @@ -4,16 +4,20 @@ import camp.nextstep.edu.missionutils.Randoms import camp.nextstep.edu.missionutils.Console fun main() { - val inputMoney = lottoMoneyInput() - val count = lottoCnt(inputMoney) - val lottoList = lottoNumberLimit(count) - lottoNumberPrint(count, lottoList) + try { + val inputMoney = lottoMoneyInput() + val count = lottoCnt(inputMoney) + val lottoList = lottoNumberLimit(count) + lottoNumberPrint(count, lottoList) - val lottoNumber = lottoNumberChoose() - val bonusNumber = lottoNumberBonus() + val lottoNumber = lottoNumberChoose() + val bonusNumber = lottoNumberBonus() - val result = lottoNumberCheck(lottoList, lottoNumber, bonusNumber) - lottoResultPrint(result, count) + val result = lottoNumberCheck(lottoList, lottoNumber, bonusNumber) + lottoResultPrint(result, count) + } catch (e: IllegalArgumentException) { + println("[ERROR] ${e.message}") + } } fun lottoMoneyInput(): Int { @@ -48,9 +52,12 @@ fun lottoNumberPrint(lottoCount: Int, lottoList: List>) { fun lottoNumberChoose(): List { println("๋‹น์ฒจ ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.") val lottoNumber = Console.readLine().split(",").map { it.toInt() } - if (lottoNumber.size != 6 || lottoNumber.toSet().size != 6) { + if (lottoNumber.toSet().size != 6) { throw IllegalArgumentException("์ค‘๋ณต๋˜๋Š” ๋ฒˆํ˜ธ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.") } + if (lottoNumber.size != 6) { + throw IllegalArgumentException("6๊ฐœ์˜ ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”.") + } return lottoNumber } From a862e13b2d32905222655f666677c19a0be30aca Mon Sep 17 00:00:00 2001 From: sooohi <68193613+sooohi@users.noreply.github.com> Date: Wed, 8 Nov 2023 01:31:43 +0900 Subject: [PATCH 14/19] =?UTF-8?q?feat=20:=20Enum=20=ED=81=B4=EB=9E=98?= =?UTF-8?q?=EC=8A=A4=20=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 2 +- src/main/kotlin/lotto/Application.kt | 38 +++++++++++++++++----------- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/docs/README.md b/docs/README.md index ed2a92e0c..f93d85554 100644 --- a/docs/README.md +++ b/docs/README.md @@ -24,7 +24,7 @@ -[ ] else๋ฅผ ์ง€์–‘ํ•œ๋‹ค. - ํžŒํŠธ: if ์กฐ๊ฑด์ ˆ์—์„œ ๊ฐ’์„ returnํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ๊ตฌํ˜„ํ•˜๋ฉด else๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค. - ๋•Œ๋กœ๋Š” if/else, when๋ฌธ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๋” ๊น”๋”ํ•ด ๋ณด์ผ ์ˆ˜ ์žˆ๋‹ค. ์–ด๋Š ๊ฒฝ์šฐ์— ์“ฐ๋Š” ๊ฒƒ์ด ์ ์ ˆํ• ์ง€ ์Šค์Šค๋กœ ๊ณ ๋ฏผํ•ด ๋ณธ๋‹ค. --[ ] Enum ํด๋ž˜์Šค๋ฅผ ์ ์šฉํ•ด ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ๊ตฌํ˜„ํ•œ๋‹ค. +-[x] Enum ํด๋ž˜์Šค๋ฅผ ์ ์šฉํ•ด ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ๊ตฌํ˜„ํ•œ๋‹ค. -[ ] ๋„๋ฉ”์ธ ๋กœ์ง์— ๋‹จ์œ„ ํ…Œ์ŠคํŠธ๋ฅผ ๊ตฌํ˜„ํ•ด์•ผ ํ•œ๋‹ค. ๋‹จ, UI(System.out, System.in, Scanner) ๋กœ์ง์€ ์ œ์™ธํ•œ๋‹ค. - ํ•ต์‹ฌ ๋กœ์ง์„ ๊ตฌํ˜„ํ•˜๋Š” ์ฝ”๋“œ์™€ UI๋ฅผ ๋‹ด๋‹นํ•˜๋Š” ๋กœ์ง์„ ๋ถ„๋ฆฌํ•ด ๊ตฌํ˜„ํ•œ๋‹ค. - ๋‹จ์œ„ ํ…Œ์ŠคํŠธ ์ž‘์„ฑ์ด ์ต์ˆ™ํ•˜์ง€ ์•Š๋‹ค๋ฉด `test/kotlin/lotto/LottoTest`๋ฅผ ์ฐธ๊ณ ํ•˜์—ฌ ํ•™์Šตํ•œ ํ›„ ํ…Œ์ŠคํŠธ๋ฅผ ๊ตฌํ˜„ํ•œ๋‹ค. diff --git a/src/main/kotlin/lotto/Application.kt b/src/main/kotlin/lotto/Application.kt index 5552774b3..2055a6bad 100644 --- a/src/main/kotlin/lotto/Application.kt +++ b/src/main/kotlin/lotto/Application.kt @@ -3,6 +3,14 @@ package lotto import camp.nextstep.edu.missionutils.Randoms import camp.nextstep.edu.missionutils.Console +enum class LottoPrize(val sameCount: Int, val prizeMoney: Int, val prizeName: String) { + threeSame(3, 5000, "3๊ฐœ ์ผ์น˜"), + fourSame(4, 50000, "4๊ฐœ ์ผ์น˜"), + fiveSame(5, 1500000, "5๊ฐœ ์ผ์น˜"), + fiveSamePlusBonus(5, 30000000, "5๊ฐœ ์ผ์น˜, ๋ณด๋„ˆ์Šค ๋ณผ ์ผ์น˜"), + sixSame(6, 2000000000, "6๊ฐœ ์ผ์น˜") +} + fun main() { try { val inputMoney = lottoMoneyInput() @@ -68,26 +76,26 @@ fun lottoNumberBonus(): Int { fun lottoNumberCheck(lottoList: List>, comNumber: List, bonusNumber: Int): Map { val lottoMoneyList = mutableMapOf( - "3๊ฐœ ์ผ์น˜" to 0, - "4๊ฐœ ์ผ์น˜" to 0, - "5๊ฐœ ์ผ์น˜" to 0, - "5๊ฐœ ์ผ์น˜, ๋ณด๋„ˆ์Šค ๋ณผ ์ผ์น˜" to 0, - "6๊ฐœ ์ผ์น˜" to 0 + LottoPrize.threeSame.prizeName to 0, + LottoPrize.fourSame.prizeName to 0, + LottoPrize.fiveSame.prizeName to 0, + LottoPrize.fiveSamePlusBonus.prizeName to 0, + LottoPrize.sixSame.prizeName to 0 ) for (lotto in lottoList) { val sameNumber = lotto.filter { it in comNumber }.size when (sameNumber) { - 3 -> lottoMoneyList["3๊ฐœ ์ผ์น˜"] = lottoMoneyList.getValue("3๊ฐœ ์ผ์น˜") + 1 - 4 -> lottoMoneyList["4๊ฐœ ์ผ์น˜"] = lottoMoneyList.getValue("4๊ฐœ ์ผ์น˜") + 1 - 5 -> { + LottoPrize.threeSame.sameCount -> lottoMoneyList[LottoPrize.threeSame.prizeName] = lottoMoneyList.getValue(LottoPrize.threeSame.prizeName) + 1 + LottoPrize.fourSame.sameCount -> lottoMoneyList[LottoPrize.fourSame.prizeName] = lottoMoneyList.getValue(LottoPrize.fourSame.prizeName) + 1 + LottoPrize.fiveSame.sameCount -> { if (lotto.contains(bonusNumber)) { - lottoMoneyList["5๊ฐœ ์ผ์น˜, ๋ณด๋„ˆ์Šค ๋ณผ ์ผ์น˜"] = - lottoMoneyList.getValue("5๊ฐœ ์ผ์น˜, ๋ณด๋„ˆ์Šค ๋ณผ ์ผ์น˜") + 1 + lottoMoneyList[LottoPrize.fiveSamePlusBonus.prizeName] = + lottoMoneyList.getValue(LottoPrize.fiveSamePlusBonus.prizeName) + 1 } else { - lottoMoneyList["5๊ฐœ ์ผ์น˜"] = lottoMoneyList.getValue("5๊ฐœ ์ผ์น˜") + 1 + lottoMoneyList[LottoPrize.fiveSame.prizeName] = lottoMoneyList.getValue(LottoPrize.fiveSame.prizeName) + 1 } } - 6 -> lottoMoneyList["6๊ฐœ ์ผ์น˜"] = lottoMoneyList.getValue("6๊ฐœ ์ผ์น˜") + 1 + LottoPrize.sixSame.sameCount -> lottoMoneyList[LottoPrize.sixSame.prizeName] = lottoMoneyList.getValue(LottoPrize.sixSame.prizeName) + 1 } } return lottoMoneyList @@ -96,6 +104,7 @@ fun lottoNumberCheck(lottoList: List>, comNumber: List, bonusNumb fun lottoResultPrint(result: Map, lottoCount: Int) { println("๋‹น์ฒจ ํ†ต๊ณ„") println("---") + var totalPrize = 0 val prizeMoney = mapOf( "3๊ฐœ ์ผ์น˜" to 5000, "4๊ฐœ ์ผ์น˜" to 50000, @@ -103,15 +112,14 @@ fun lottoResultPrint(result: Map, lottoCount: Int) { "5๊ฐœ ์ผ์น˜, ๋ณด๋„ˆ์Šค ๋ณผ ์ผ์น˜" to 30000000, "6๊ฐœ ์ผ์น˜" to 2000000000 ) - var totalPrize = 0 for ((key, value) in result) { val prize = when (key) { - "5๊ฐœ ์ผ์น˜" -> if (result["5๊ฐœ ์ผ์น˜, ๋ณด๋„ˆ์Šค ๋ณผ ์ผ์น˜"] == 0) "1,500,000์›" else "30,000,000์›" + "5๊ฐœ ์ผ์น˜" -> if (result[LottoPrize.fiveSamePlusBonus.prizeName] == 0) "1,500,000์›" else "30,000,000์›" else -> "${prizeMoney[key]}์›" } println("$key ($prize) - ${value}๊ฐœ") totalPrize += if (key == "5๊ฐœ ์ผ์น˜") { - if (result["5๊ฐœ ์ผ์น˜, ๋ณด๋„ˆ์Šค ๋ณผ ์ผ์น˜"] == 0) prizeMoney["5๊ฐœ ์ผ์น˜"]!! * value else prizeMoney["5๊ฐœ ์ผ์น˜, ๋ณด๋„ˆ์Šค ๋ณผ ์ผ์น˜"]!! * value + if (result[LottoPrize.fiveSamePlusBonus.prizeName] == 0) prizeMoney["5๊ฐœ ์ผ์น˜"]!! * value else prizeMoney["5๊ฐœ ์ผ์น˜, ๋ณด๋„ˆ์Šค ๋ณผ ์ผ์น˜"]!! * value } else { prizeMoney[key]!! * value } From 39af5e592041753f572803546e0847596923bbc5 Mon Sep 17 00:00:00 2001 From: sooohi <68193613+sooohi@users.noreply.github.com> Date: Wed, 8 Nov 2023 01:53:39 +0900 Subject: [PATCH 15/19] =?UTF-8?q?refactor=20:=20Controller=20=EB=B6=84?= =?UTF-8?q?=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 4 +- src/main/kotlin/lotto/Application.kt | 128 +--------------- .../lotto/Controller/LottoController.kt | 139 ++++++++++++++++++ 3 files changed, 144 insertions(+), 127 deletions(-) create mode 100644 src/main/kotlin/lotto/Controller/LottoController.kt diff --git a/docs/README.md b/docs/README.md index f93d85554..00891955b 100644 --- a/docs/README.md +++ b/docs/README.md @@ -42,5 +42,5 @@ - [ ] ๋ฉ”์‹œ์ง€ ์ถœ๋ ฅ ### Controller -- [ ] LottoController : ์ˆซ์ž ์ž…๋ ฅ ๋ฐ›์€ ๊ฒƒ ์ฒ˜๋ฆฌ ๋ฐ ๊ฒŒ์ž„ ์‹คํ–‰ -- [ ] ์‚ฌ์šฉ์ž์—๊ฒŒ ์ˆซ์ž ์ž…๋ ฅ ๋ฐ›๊ธฐ +- [x] LottoController : ์ˆซ์ž ์ž…๋ ฅ ๋ฐ›์€ ๊ฒƒ ์ฒ˜๋ฆฌ ๋ฐ ๊ฒŒ์ž„ ์‹คํ–‰ +- [x] ์‚ฌ์šฉ์ž์—๊ฒŒ ์ˆซ์ž ์ž…๋ ฅ ๋ฐ›๊ธฐ diff --git a/src/main/kotlin/lotto/Application.kt b/src/main/kotlin/lotto/Application.kt index 2055a6bad..dd0eee40f 100644 --- a/src/main/kotlin/lotto/Application.kt +++ b/src/main/kotlin/lotto/Application.kt @@ -1,130 +1,8 @@ package lotto -import camp.nextstep.edu.missionutils.Randoms -import camp.nextstep.edu.missionutils.Console - -enum class LottoPrize(val sameCount: Int, val prizeMoney: Int, val prizeName: String) { - threeSame(3, 5000, "3๊ฐœ ์ผ์น˜"), - fourSame(4, 50000, "4๊ฐœ ์ผ์น˜"), - fiveSame(5, 1500000, "5๊ฐœ ์ผ์น˜"), - fiveSamePlusBonus(5, 30000000, "5๊ฐœ ์ผ์น˜, ๋ณด๋„ˆ์Šค ๋ณผ ์ผ์น˜"), - sixSame(6, 2000000000, "6๊ฐœ ์ผ์น˜") -} +import lotto.Controller.LottoController fun main() { - try { - val inputMoney = lottoMoneyInput() - val count = lottoCnt(inputMoney) - val lottoList = lottoNumberLimit(count) - lottoNumberPrint(count, lottoList) - - val lottoNumber = lottoNumberChoose() - val bonusNumber = lottoNumberBonus() - - val result = lottoNumberCheck(lottoList, lottoNumber, bonusNumber) - lottoResultPrint(result, count) - } catch (e: IllegalArgumentException) { - println("[ERROR] ${e.message}") - } -} - -fun lottoMoneyInput(): Int { - println("๊ตฌ์ž…๊ธˆ์•ก์„ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.") - val inputMoney = Console.readLine().toInt() - if (inputMoney % 1000 != 0) { - throw IllegalArgumentException("1000์› ๋‹จ์œ„๋กœ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.") - } - return inputMoney -} - -fun lottoCnt(inputMoney: Int): Int { - return inputMoney / 1000 -} - -fun lottoNumberLimit(count: Int): List> { - val comLottoList = mutableListOf>() - repeat(count) { - val numbers = Randoms.pickUniqueNumbersInRange(1, 45, 6) - comLottoList.add(numbers) - } - return comLottoList -} - -fun lottoNumberPrint(lottoCount: Int, lottoList: List>) { - println("$lottoCount ๊ฐœ๋ฅผ ๊ตฌ๋งคํ–ˆ์Šต๋‹ˆ๋‹ค.") - lottoList.forEach { lotto -> - println(lotto.sorted()) - } -} - -fun lottoNumberChoose(): List { - println("๋‹น์ฒจ ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.") - val lottoNumber = Console.readLine().split(",").map { it.toInt() } - if (lottoNumber.toSet().size != 6) { - throw IllegalArgumentException("์ค‘๋ณต๋˜๋Š” ๋ฒˆํ˜ธ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.") - } - if (lottoNumber.size != 6) { - throw IllegalArgumentException("6๊ฐœ์˜ ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”.") - } - return lottoNumber -} - -fun lottoNumberBonus(): Int { - println("๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.") - return Console.readLine().toInt() -} - -fun lottoNumberCheck(lottoList: List>, comNumber: List, bonusNumber: Int): Map { - val lottoMoneyList = mutableMapOf( - LottoPrize.threeSame.prizeName to 0, - LottoPrize.fourSame.prizeName to 0, - LottoPrize.fiveSame.prizeName to 0, - LottoPrize.fiveSamePlusBonus.prizeName to 0, - LottoPrize.sixSame.prizeName to 0 - ) - for (lotto in lottoList) { - val sameNumber = lotto.filter { it in comNumber }.size - when (sameNumber) { - LottoPrize.threeSame.sameCount -> lottoMoneyList[LottoPrize.threeSame.prizeName] = lottoMoneyList.getValue(LottoPrize.threeSame.prizeName) + 1 - LottoPrize.fourSame.sameCount -> lottoMoneyList[LottoPrize.fourSame.prizeName] = lottoMoneyList.getValue(LottoPrize.fourSame.prizeName) + 1 - LottoPrize.fiveSame.sameCount -> { - if (lotto.contains(bonusNumber)) { - lottoMoneyList[LottoPrize.fiveSamePlusBonus.prizeName] = - lottoMoneyList.getValue(LottoPrize.fiveSamePlusBonus.prizeName) + 1 - } else { - lottoMoneyList[LottoPrize.fiveSame.prizeName] = lottoMoneyList.getValue(LottoPrize.fiveSame.prizeName) + 1 - } - } - LottoPrize.sixSame.sameCount -> lottoMoneyList[LottoPrize.sixSame.prizeName] = lottoMoneyList.getValue(LottoPrize.sixSame.prizeName) + 1 - } - } - return lottoMoneyList -} - -fun lottoResultPrint(result: Map, lottoCount: Int) { - println("๋‹น์ฒจ ํ†ต๊ณ„") - println("---") - var totalPrize = 0 - val prizeMoney = mapOf( - "3๊ฐœ ์ผ์น˜" to 5000, - "4๊ฐœ ์ผ์น˜" to 50000, - "5๊ฐœ ์ผ์น˜" to 1500000, - "5๊ฐœ ์ผ์น˜, ๋ณด๋„ˆ์Šค ๋ณผ ์ผ์น˜" to 30000000, - "6๊ฐœ ์ผ์น˜" to 2000000000 - ) - for ((key, value) in result) { - val prize = when (key) { - "5๊ฐœ ์ผ์น˜" -> if (result[LottoPrize.fiveSamePlusBonus.prizeName] == 0) "1,500,000์›" else "30,000,000์›" - else -> "${prizeMoney[key]}์›" - } - println("$key ($prize) - ${value}๊ฐœ") - totalPrize += if (key == "5๊ฐœ ์ผ์น˜") { - if (result[LottoPrize.fiveSamePlusBonus.prizeName] == 0) prizeMoney["5๊ฐœ ์ผ์น˜"]!! * value else prizeMoney["5๊ฐœ ์ผ์น˜, ๋ณด๋„ˆ์Šค ๋ณผ ์ผ์น˜"]!! * value - } else { - prizeMoney[key]!! * value - } - } - val inputMoney = lottoCount * 1000 - val rateOfReturn = ((totalPrize - inputMoney) / inputMoney.toDouble() * 100).coerceAtLeast(0.0) - println("์ด ์ˆ˜์ต๋ฅ ์€ ${"%.1f".format(rateOfReturn)}%์ž…๋‹ˆ๋‹ค.") + val lottoController = LottoController() + lottoController.startGame() } diff --git a/src/main/kotlin/lotto/Controller/LottoController.kt b/src/main/kotlin/lotto/Controller/LottoController.kt new file mode 100644 index 000000000..124725cbe --- /dev/null +++ b/src/main/kotlin/lotto/Controller/LottoController.kt @@ -0,0 +1,139 @@ +package lotto.Controller + +import camp.nextstep.edu.missionutils.Randoms +import camp.nextstep.edu.missionutils.Console + +enum class LottoPrize(val sameCount: Int, val prizeMoney: Int, val prizeName: String) { + threeSame(3, 5000, "3๊ฐœ ์ผ์น˜"), + fourSame(4, 50000, "4๊ฐœ ์ผ์น˜"), + fiveSame(5, 1500000, "5๊ฐœ ์ผ์น˜"), + fiveSamePlusBonus(5, 30000000, "5๊ฐœ ์ผ์น˜, ๋ณด๋„ˆ์Šค ๋ณผ ์ผ์น˜"), + sixSame(6, 2000000000, "6๊ฐœ ์ผ์น˜") +} + +class LottoController { + fun startGame() { + try { + val inputMoney = lottoMoneyInput() + val count = lottoCnt(inputMoney) + val lottoList = lottoNumberLimit(count) + lottoNumberPrint(count, lottoList) + + val lottoNumber = lottoNumberChoose() + val bonusNumber = lottoNumberBonus() + + val result = lottoNumberCheck(lottoList, lottoNumber, bonusNumber) + lottoResultPrint(result, count) + } catch (e: IllegalArgumentException) { + println("[ERROR] ${e.message}") + } + } + + fun lottoMoneyInput(): Int { + println("๊ตฌ์ž…๊ธˆ์•ก์„ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.") + val inputMoney = Console.readLine().toInt() + if (inputMoney % 1000 != 0) { + throw IllegalArgumentException("1000์› ๋‹จ์œ„๋กœ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.") + } + return inputMoney + } + + fun lottoCnt(inputMoney: Int): Int { + return inputMoney / 1000 + } + + fun lottoNumberLimit(count: Int): List> { + val comLottoList = mutableListOf>() + repeat(count) { + val numbers = Randoms.pickUniqueNumbersInRange(1, 45, 6) + comLottoList.add(numbers) + } + return comLottoList + } + + fun lottoNumberPrint(lottoCount: Int, lottoList: List>) { + println("$lottoCount ๊ฐœ๋ฅผ ๊ตฌ๋งคํ–ˆ์Šต๋‹ˆ๋‹ค.") + lottoList.forEach { lotto -> + println(lotto.sorted()) + } + } + + fun lottoNumberChoose(): List { + println("๋‹น์ฒจ ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.") + val lottoNumber = Console.readLine().split(",").map { it.toInt() } + if (lottoNumber.toSet().size != 6) { + throw IllegalArgumentException("์ค‘๋ณต๋˜๋Š” ๋ฒˆํ˜ธ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.") + } + if (lottoNumber.size != 6) { + throw IllegalArgumentException("6๊ฐœ์˜ ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”.") + } + return lottoNumber + } + + fun lottoNumberBonus(): Int { + println("๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.") + return Console.readLine().toInt() + } + + fun lottoNumberCheck(lottoList: List>, comNumber: List, bonusNumber: Int): Map { + val lottoMoneyList = mutableMapOf( + LottoPrize.threeSame.prizeName to 0, + LottoPrize.fourSame.prizeName to 0, + LottoPrize.fiveSame.prizeName to 0, + LottoPrize.fiveSamePlusBonus.prizeName to 0, + LottoPrize.sixSame.prizeName to 0 + ) + for (lotto in lottoList) { + val sameNumber = lotto.filter { it in comNumber }.size + when (sameNumber) { + LottoPrize.threeSame.sameCount -> lottoMoneyList[LottoPrize.threeSame.prizeName] = + lottoMoneyList.getValue(LottoPrize.threeSame.prizeName) + 1 + + LottoPrize.fourSame.sameCount -> lottoMoneyList[LottoPrize.fourSame.prizeName] = + lottoMoneyList.getValue(LottoPrize.fourSame.prizeName) + 1 + + LottoPrize.fiveSame.sameCount -> { + if (lotto.contains(bonusNumber)) { + lottoMoneyList[LottoPrize.fiveSamePlusBonus.prizeName] = + lottoMoneyList.getValue(LottoPrize.fiveSamePlusBonus.prizeName) + 1 + } else { + lottoMoneyList[LottoPrize.fiveSame.prizeName] = + lottoMoneyList.getValue(LottoPrize.fiveSame.prizeName) + 1 + } + } + + LottoPrize.sixSame.sameCount -> lottoMoneyList[LottoPrize.sixSame.prizeName] = + lottoMoneyList.getValue(LottoPrize.sixSame.prizeName) + 1 + } + } + return lottoMoneyList + } + + fun lottoResultPrint(result: Map, lottoCount: Int) { + println("๋‹น์ฒจ ํ†ต๊ณ„") + println("---") + var totalPrize = 0 + val prizeMoney = mapOf( + "3๊ฐœ ์ผ์น˜" to 5000, + "4๊ฐœ ์ผ์น˜" to 50000, + "5๊ฐœ ์ผ์น˜" to 1500000, + "5๊ฐœ ์ผ์น˜, ๋ณด๋„ˆ์Šค ๋ณผ ์ผ์น˜" to 30000000, + "6๊ฐœ ์ผ์น˜" to 2000000000 + ) + for ((key, value) in result) { + val prize = when (key) { + "5๊ฐœ ์ผ์น˜" -> if (result[LottoPrize.fiveSamePlusBonus.prizeName] == 0) "1,500,000์›" else "30,000,000์›" + else -> "${prizeMoney[key]}์›" + } + println("$key ($prize) - ${value}๊ฐœ") + totalPrize += if (key == "5๊ฐœ ์ผ์น˜") { + if (result[LottoPrize.fiveSamePlusBonus.prizeName] == 0) prizeMoney["5๊ฐœ ์ผ์น˜"]!! * value else prizeMoney["5๊ฐœ ์ผ์น˜, ๋ณด๋„ˆ์Šค ๋ณผ ์ผ์น˜"]!! * value + } else { + prizeMoney[key]!! * value + } + } + val inputMoney = lottoCount * 1000 + val rateOfReturn = ((totalPrize - inputMoney) / inputMoney.toDouble() * 100).coerceAtLeast(0.0) + println("์ด ์ˆ˜์ต๋ฅ ์€ ${"%.1f".format(rateOfReturn)}%์ž…๋‹ˆ๋‹ค.") + } +} \ No newline at end of file From b69d9187e21843a774041145257cb9e10d3813d8 Mon Sep 17 00:00:00 2001 From: sooohi <68193613+sooohi@users.noreply.github.com> Date: Wed, 8 Nov 2023 02:11:08 +0900 Subject: [PATCH 16/19] =?UTF-8?q?refactor=20:=20View=20=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lotto/Controller/LottoController.kt | 58 +++++-------------- src/main/kotlin/lotto/View/LottoView.kt | 45 ++++++++++++++ 2 files changed, 59 insertions(+), 44 deletions(-) create mode 100644 src/main/kotlin/lotto/View/LottoView.kt diff --git a/src/main/kotlin/lotto/Controller/LottoController.kt b/src/main/kotlin/lotto/Controller/LottoController.kt index 124725cbe..eb0737741 100644 --- a/src/main/kotlin/lotto/Controller/LottoController.kt +++ b/src/main/kotlin/lotto/Controller/LottoController.kt @@ -1,31 +1,34 @@ package lotto.Controller +import lotto.View.LottoView import camp.nextstep.edu.missionutils.Randoms import camp.nextstep.edu.missionutils.Console -enum class LottoPrize(val sameCount: Int, val prizeMoney: Int, val prizeName: String) { - threeSame(3, 5000, "3๊ฐœ ์ผ์น˜"), - fourSame(4, 50000, "4๊ฐœ ์ผ์น˜"), - fiveSame(5, 1500000, "5๊ฐœ ์ผ์น˜"), - fiveSamePlusBonus(5, 30000000, "5๊ฐœ ์ผ์น˜, ๋ณด๋„ˆ์Šค ๋ณผ ์ผ์น˜"), - sixSame(6, 2000000000, "6๊ฐœ ์ผ์น˜") -} - class LottoController { + private val lottoView = LottoView() + + enum class LottoPrize(val sameCount: Int, val prizeMoney: Int, val prizeName: String) { + threeSame(3, 5000, "3๊ฐœ ์ผ์น˜"), + fourSame(4, 50000, "4๊ฐœ ์ผ์น˜"), + fiveSame(5, 1500000, "5๊ฐœ ์ผ์น˜"), + fiveSamePlusBonus(5, 30000000, "5๊ฐœ ์ผ์น˜, ๋ณด๋„ˆ์Šค ๋ณผ ์ผ์น˜"), + sixSame(6, 2000000000, "6๊ฐœ ์ผ์น˜") + } + fun startGame() { try { val inputMoney = lottoMoneyInput() val count = lottoCnt(inputMoney) val lottoList = lottoNumberLimit(count) - lottoNumberPrint(count, lottoList) + lottoView.printLottoNumbers(count, lottoList) val lottoNumber = lottoNumberChoose() val bonusNumber = lottoNumberBonus() val result = lottoNumberCheck(lottoList, lottoNumber, bonusNumber) - lottoResultPrint(result, count) + lottoView.printResult(result, count) } catch (e: IllegalArgumentException) { - println("[ERROR] ${e.message}") + lottoView.printMessage("[ERROR] ${e.message}") } } @@ -51,12 +54,6 @@ class LottoController { return comLottoList } - fun lottoNumberPrint(lottoCount: Int, lottoList: List>) { - println("$lottoCount ๊ฐœ๋ฅผ ๊ตฌ๋งคํ–ˆ์Šต๋‹ˆ๋‹ค.") - lottoList.forEach { lotto -> - println(lotto.sorted()) - } - } fun lottoNumberChoose(): List { println("๋‹น์ฒจ ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.") @@ -109,31 +106,4 @@ class LottoController { return lottoMoneyList } - fun lottoResultPrint(result: Map, lottoCount: Int) { - println("๋‹น์ฒจ ํ†ต๊ณ„") - println("---") - var totalPrize = 0 - val prizeMoney = mapOf( - "3๊ฐœ ์ผ์น˜" to 5000, - "4๊ฐœ ์ผ์น˜" to 50000, - "5๊ฐœ ์ผ์น˜" to 1500000, - "5๊ฐœ ์ผ์น˜, ๋ณด๋„ˆ์Šค ๋ณผ ์ผ์น˜" to 30000000, - "6๊ฐœ ์ผ์น˜" to 2000000000 - ) - for ((key, value) in result) { - val prize = when (key) { - "5๊ฐœ ์ผ์น˜" -> if (result[LottoPrize.fiveSamePlusBonus.prizeName] == 0) "1,500,000์›" else "30,000,000์›" - else -> "${prizeMoney[key]}์›" - } - println("$key ($prize) - ${value}๊ฐœ") - totalPrize += if (key == "5๊ฐœ ์ผ์น˜") { - if (result[LottoPrize.fiveSamePlusBonus.prizeName] == 0) prizeMoney["5๊ฐœ ์ผ์น˜"]!! * value else prizeMoney["5๊ฐœ ์ผ์น˜, ๋ณด๋„ˆ์Šค ๋ณผ ์ผ์น˜"]!! * value - } else { - prizeMoney[key]!! * value - } - } - val inputMoney = lottoCount * 1000 - val rateOfReturn = ((totalPrize - inputMoney) / inputMoney.toDouble() * 100).coerceAtLeast(0.0) - println("์ด ์ˆ˜์ต๋ฅ ์€ ${"%.1f".format(rateOfReturn)}%์ž…๋‹ˆ๋‹ค.") - } } \ No newline at end of file diff --git a/src/main/kotlin/lotto/View/LottoView.kt b/src/main/kotlin/lotto/View/LottoView.kt new file mode 100644 index 000000000..eda849b6a --- /dev/null +++ b/src/main/kotlin/lotto/View/LottoView.kt @@ -0,0 +1,45 @@ +package lotto.View + +import camp.nextstep.edu.missionutils.Console +import lotto.Controller.LottoController + +class LottoView { + fun printMessage(message: String) { + println(message) + } + + fun printLottoNumbers(lottoCount: Int, lottoList: List>) { + printMessage("$lottoCount ๊ฐœ๋ฅผ ๊ตฌ๋งคํ–ˆ์Šต๋‹ˆ๋‹ค.") + lottoList.forEach { lotto -> + printMessage(lotto.sorted().toString()) + } + } + + fun printResult(result: Map, lottoCount: Int) { + printMessage("๋‹น์ฒจ ํ†ต๊ณ„") + printMessage("---") + var totalPrize = 0 + val prizeMoney = mapOf( + "3๊ฐœ ์ผ์น˜" to 5000, + "4๊ฐœ ์ผ์น˜" to 50000, + "5๊ฐœ ์ผ์น˜" to 1500000, + "5๊ฐœ ์ผ์น˜, ๋ณด๋„ˆ์Šค ๋ณผ ์ผ์น˜" to 30000000, + "6๊ฐœ ์ผ์น˜" to 2000000000 + ) + for ((key, value) in result) { + val prize = when (key) { + "5๊ฐœ ์ผ์น˜" -> if (result[LottoController.LottoPrize.fiveSamePlusBonus.prizeName] == 0) "1,500,000์›" else "30,000,000์›" + else -> "${prizeMoney[key]}์›" + } + printMessage("$key ($prize) - ${value}๊ฐœ") + totalPrize += if (key == "5๊ฐœ ์ผ์น˜") { + if (result[LottoController.LottoPrize.fiveSamePlusBonus.prizeName] == 0) prizeMoney["5๊ฐœ ์ผ์น˜"]!! * value else prizeMoney["5๊ฐœ ์ผ์น˜, ๋ณด๋„ˆ์Šค ๋ณผ ์ผ์น˜"]!! * value + } else { + prizeMoney[key]!! * value + } + } + val inputMoney = lottoCount * 1000 + val rateOfReturn = ((totalPrize - inputMoney) / inputMoney.toDouble() * 100).coerceAtLeast(0.0) + printMessage("์ด ์ˆ˜์ต๋ฅ ์€ ${"%.1f".format(rateOfReturn)}%์ž…๋‹ˆ๋‹ค.") + } +} From 172413329157d2da422d486d54ff9bf6809ee3a2 Mon Sep 17 00:00:00 2001 From: sooohi <68193613+sooohi@users.noreply.github.com> Date: Wed, 8 Nov 2023 02:22:37 +0900 Subject: [PATCH 17/19] =?UTF-8?q?refactor=20:=20Model=20=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 8 +-- .../lotto/Controller/LottoController.kt | 58 ++++++++----------- src/main/kotlin/lotto/Lotto.kt | 9 --- src/main/kotlin/lotto/Model/Lotto.kt | 26 +++++++++ src/main/kotlin/lotto/View/LottoView.kt | 1 - src/test/kotlin/lotto/LottoTest.kt | 1 + 6 files changed, 55 insertions(+), 48 deletions(-) delete mode 100644 src/main/kotlin/lotto/Lotto.kt create mode 100644 src/main/kotlin/lotto/Model/Lotto.kt diff --git a/docs/README.md b/docs/README.md index 00891955b..443c76ca5 100644 --- a/docs/README.md +++ b/docs/README.md @@ -25,21 +25,21 @@ - ํžŒํŠธ: if ์กฐ๊ฑด์ ˆ์—์„œ ๊ฐ’์„ returnํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ๊ตฌํ˜„ํ•˜๋ฉด else๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค. - ๋•Œ๋กœ๋Š” if/else, when๋ฌธ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๋” ๊น”๋”ํ•ด ๋ณด์ผ ์ˆ˜ ์žˆ๋‹ค. ์–ด๋Š ๊ฒฝ์šฐ์— ์“ฐ๋Š” ๊ฒƒ์ด ์ ์ ˆํ• ์ง€ ์Šค์Šค๋กœ ๊ณ ๋ฏผํ•ด ๋ณธ๋‹ค. -[x] Enum ํด๋ž˜์Šค๋ฅผ ์ ์šฉํ•ด ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ๊ตฌํ˜„ํ•œ๋‹ค. --[ ] ๋„๋ฉ”์ธ ๋กœ์ง์— ๋‹จ์œ„ ํ…Œ์ŠคํŠธ๋ฅผ ๊ตฌํ˜„ํ•ด์•ผ ํ•œ๋‹ค. ๋‹จ, UI(System.out, System.in, Scanner) ๋กœ์ง์€ ์ œ์™ธํ•œ๋‹ค. +-[x] ๋„๋ฉ”์ธ ๋กœ์ง์— ๋‹จ์œ„ ํ…Œ์ŠคํŠธ๋ฅผ ๊ตฌํ˜„ํ•ด์•ผ ํ•œ๋‹ค. ๋‹จ, UI(System.out, System.in, Scanner) ๋กœ์ง์€ ์ œ์™ธํ•œ๋‹ค. - ํ•ต์‹ฌ ๋กœ์ง์„ ๊ตฌํ˜„ํ•˜๋Š” ์ฝ”๋“œ์™€ UI๋ฅผ ๋‹ด๋‹นํ•˜๋Š” ๋กœ์ง์„ ๋ถ„๋ฆฌํ•ด ๊ตฌํ˜„ํ•œ๋‹ค. - ๋‹จ์œ„ ํ…Œ์ŠคํŠธ ์ž‘์„ฑ์ด ์ต์ˆ™ํ•˜์ง€ ์•Š๋‹ค๋ฉด `test/kotlin/lotto/LottoTest`๋ฅผ ์ฐธ๊ณ ํ•˜์—ฌ ํ•™์Šตํ•œ ํ›„ ํ…Œ์ŠคํŠธ๋ฅผ ๊ตฌํ˜„ํ•œ๋‹ค. -[x] `camp.nextstep.edu.missionutils`์—์„œ ์ œ๊ณตํ•˜๋Š” `Randoms` ๋ฐ `Console` API๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ตฌํ˜„ํ•ด์•ผ ํ•œ๋‹ค. - Random ๊ฐ’ ์ถ”์ถœ์€ `camp.nextstep.edu.missionutils.Randoms`์˜ `pickUniqueNumbersInRange()`๋ฅผ ํ™œ์šฉํ•œ๋‹ค. - ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•˜๋Š” ๊ฐ’์€ `camp.nextstep.edu.missionutils.Console`์˜ `readLine()`์„ ํ™œ์šฉํ•œ๋‹ค. --[ ] ์ œ๊ณต๋œ `Lotto` ํด๋ž˜์Šค๋ฅผ ํ™œ์šฉํ•ด ๊ตฌํ˜„ํ•ด์•ผ ํ•œ๋‹ค. +-[x] ์ œ๊ณต๋œ `Lotto` ํด๋ž˜์Šค๋ฅผ ํ™œ์šฉํ•ด ๊ตฌํ˜„ํ•ด์•ผ ํ•œ๋‹ค. ## MVC๋ชจ๋ธ ํ™œ์šฉ ### Model -- [ ] Lotto ํด๋ž˜์Šค์— ๋ฒˆํ˜ธ ์ €์žฅ +- [x] Lotto ํด๋ž˜์Šค์— ๋ฒˆํ˜ธ ์ €์žฅ ### View -- [ ] ๋ฉ”์‹œ์ง€ ์ถœ๋ ฅ +- [x] ๋ฉ”์‹œ์ง€ ์ถœ๋ ฅ ### Controller - [x] LottoController : ์ˆซ์ž ์ž…๋ ฅ ๋ฐ›์€ ๊ฒƒ ์ฒ˜๋ฆฌ ๋ฐ ๊ฒŒ์ž„ ์‹คํ–‰ diff --git a/src/main/kotlin/lotto/Controller/LottoController.kt b/src/main/kotlin/lotto/Controller/LottoController.kt index eb0737741..37b0f7b8f 100644 --- a/src/main/kotlin/lotto/Controller/LottoController.kt +++ b/src/main/kotlin/lotto/Controller/LottoController.kt @@ -6,7 +6,6 @@ import camp.nextstep.edu.missionutils.Console class LottoController { private val lottoView = LottoView() - enum class LottoPrize(val sameCount: Int, val prizeMoney: Int, val prizeName: String) { threeSame(3, 5000, "3๊ฐœ ์ผ์น˜"), fourSame(4, 50000, "4๊ฐœ ์ผ์น˜"), @@ -14,7 +13,6 @@ class LottoController { fiveSamePlusBonus(5, 30000000, "5๊ฐœ ์ผ์น˜, ๋ณด๋„ˆ์Šค ๋ณผ ์ผ์น˜"), sixSame(6, 2000000000, "6๊ฐœ ์ผ์น˜") } - fun startGame() { try { val inputMoney = lottoMoneyInput() @@ -32,8 +30,8 @@ class LottoController { } } - fun lottoMoneyInput(): Int { - println("๊ตฌ์ž…๊ธˆ์•ก์„ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.") + private fun lottoMoneyInput(): Int { + lottoView.printMessage("๊ตฌ์ž…๊ธˆ์•ก์„ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.") val inputMoney = Console.readLine().toInt() if (inputMoney % 1000 != 0) { throw IllegalArgumentException("1000์› ๋‹จ์œ„๋กœ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.") @@ -41,11 +39,11 @@ class LottoController { return inputMoney } - fun lottoCnt(inputMoney: Int): Int { + private fun lottoCnt(inputMoney: Int): Int { return inputMoney / 1000 } - fun lottoNumberLimit(count: Int): List> { + private fun lottoNumberLimit(count: Int): List> { val comLottoList = mutableListOf>() repeat(count) { val numbers = Randoms.pickUniqueNumbersInRange(1, 45, 6) @@ -54,9 +52,8 @@ class LottoController { return comLottoList } - - fun lottoNumberChoose(): List { - println("๋‹น์ฒจ ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.") + private fun lottoNumberChoose(): List { + lottoView.printMessage("๋‹น์ฒจ ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.") val lottoNumber = Console.readLine().split(",").map { it.toInt() } if (lottoNumber.toSet().size != 6) { throw IllegalArgumentException("์ค‘๋ณต๋˜๋Š” ๋ฒˆํ˜ธ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.") @@ -67,43 +64,36 @@ class LottoController { return lottoNumber } - fun lottoNumberBonus(): Int { - println("๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.") + private fun lottoNumberBonus(): Int { + lottoView.printMessage("๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.") return Console.readLine().toInt() } - fun lottoNumberCheck(lottoList: List>, comNumber: List, bonusNumber: Int): Map { - val lottoMoneyList = mutableMapOf( - LottoPrize.threeSame.prizeName to 0, - LottoPrize.fourSame.prizeName to 0, - LottoPrize.fiveSame.prizeName to 0, - LottoPrize.fiveSamePlusBonus.prizeName to 0, - LottoPrize.sixSame.prizeName to 0 - ) + private fun lottoNumberCheck(lottoList: List>, comNumber: List, bonusNumber: Int): Map { + val lottoMoneyList = mutableMapOf() + lottoMoneyList["3๊ฐœ ์ผ์น˜"] = 0 + lottoMoneyList["4๊ฐœ ์ผ์น˜"] = 0 + lottoMoneyList["5๊ฐœ ์ผ์น˜"] = 0 + lottoMoneyList["5๊ฐœ ์ผ์น˜, ๋ณด๋„ˆ์Šค ๋ณผ ์ผ์น˜"] = 0 + lottoMoneyList["6๊ฐœ ์ผ์น˜"] = 0 + for (lotto in lottoList) { val sameNumber = lotto.filter { it in comNumber }.size when (sameNumber) { - LottoPrize.threeSame.sameCount -> lottoMoneyList[LottoPrize.threeSame.prizeName] = - lottoMoneyList.getValue(LottoPrize.threeSame.prizeName) + 1 - - LottoPrize.fourSame.sameCount -> lottoMoneyList[LottoPrize.fourSame.prizeName] = - lottoMoneyList.getValue(LottoPrize.fourSame.prizeName) + 1 - - LottoPrize.fiveSame.sameCount -> { + 3 -> lottoMoneyList["3๊ฐœ ์ผ์น˜"] = lottoMoneyList.getOrDefault("3๊ฐœ ์ผ์น˜", 0) + 1 + 4 -> lottoMoneyList["4๊ฐœ ์ผ์น˜"] = lottoMoneyList.getOrDefault("4๊ฐœ ์ผ์น˜", 0) + 1 + 5 -> { if (lotto.contains(bonusNumber)) { - lottoMoneyList[LottoPrize.fiveSamePlusBonus.prizeName] = - lottoMoneyList.getValue(LottoPrize.fiveSamePlusBonus.prizeName) + 1 + lottoMoneyList["5๊ฐœ ์ผ์น˜, ๋ณด๋„ˆ์Šค ๋ณผ ์ผ์น˜"] = + lottoMoneyList.getOrDefault("5๊ฐœ ์ผ์น˜, ๋ณด๋„ˆ์Šค ๋ณผ ์ผ์น˜", 0) + 1 } else { - lottoMoneyList[LottoPrize.fiveSame.prizeName] = - lottoMoneyList.getValue(LottoPrize.fiveSame.prizeName) + 1 + lottoMoneyList["5๊ฐœ ์ผ์น˜"] = lottoMoneyList.getOrDefault("5๊ฐœ ์ผ์น˜", 0) + 1 } } - - LottoPrize.sixSame.sameCount -> lottoMoneyList[LottoPrize.sixSame.prizeName] = - lottoMoneyList.getValue(LottoPrize.sixSame.prizeName) + 1 + 6 -> lottoMoneyList["6๊ฐœ ์ผ์น˜"] = lottoMoneyList.getOrDefault("6๊ฐœ ์ผ์น˜", 0) + 1 } } return lottoMoneyList } -} \ No newline at end of file +} diff --git a/src/main/kotlin/lotto/Lotto.kt b/src/main/kotlin/lotto/Lotto.kt deleted file mode 100644 index 5ca00b4e4..000000000 --- a/src/main/kotlin/lotto/Lotto.kt +++ /dev/null @@ -1,9 +0,0 @@ -package lotto - -class Lotto(private val numbers: List) { - init { - require(numbers.size == 6) - } - - // TODO: ์ถ”๊ฐ€ ๊ธฐ๋Šฅ ๊ตฌํ˜„ -} diff --git a/src/main/kotlin/lotto/Model/Lotto.kt b/src/main/kotlin/lotto/Model/Lotto.kt new file mode 100644 index 000000000..359ca3859 --- /dev/null +++ b/src/main/kotlin/lotto/Model/Lotto.kt @@ -0,0 +1,26 @@ +package lotto.Model + +class Lotto(private val numbers: List) { + init { + require(numbers.size == 6) { "๋กœ๋˜ ๋ฒˆํ˜ธ๋Š” 6๊ฐœ์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค." } + require(numbers.all { it in 1..45 }) { "๋กœ๋˜ ๋ฒˆํ˜ธ๋Š” 1๋ถ€ํ„ฐ 45 ์‚ฌ์ด์˜ ๊ฐ’์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค." } + } + + fun getNumbers(): List { + return numbers + } + + fun checkWinningNumbers(winningNumbers: List, bonusNumber: Int): String { + val matchedNumbers = numbers.filter { it in winningNumbers } + val matchingCount = matchedNumbers.size + return when (matchingCount) { + 3 -> "3๊ฐœ ์ผ์น˜" + 4 -> "4๊ฐœ ์ผ์น˜" + 5 -> { + if (numbers.contains(bonusNumber)) "5๊ฐœ ์ผ์น˜, ๋ณด๋„ˆ์Šค ๋ณผ ์ผ์น˜" else "5๊ฐœ ์ผ์น˜" + } + 6 -> "6๊ฐœ ์ผ์น˜" + else -> "๊ฝ" + } + } +} diff --git a/src/main/kotlin/lotto/View/LottoView.kt b/src/main/kotlin/lotto/View/LottoView.kt index eda849b6a..afd2ec104 100644 --- a/src/main/kotlin/lotto/View/LottoView.kt +++ b/src/main/kotlin/lotto/View/LottoView.kt @@ -1,6 +1,5 @@ package lotto.View -import camp.nextstep.edu.missionutils.Console import lotto.Controller.LottoController class LottoView { diff --git a/src/test/kotlin/lotto/LottoTest.kt b/src/test/kotlin/lotto/LottoTest.kt index 11d85ac2c..730ad49c3 100644 --- a/src/test/kotlin/lotto/LottoTest.kt +++ b/src/test/kotlin/lotto/LottoTest.kt @@ -1,5 +1,6 @@ package lotto +import lotto.Model.Lotto import org.junit.jupiter.api.Test import org.junit.jupiter.api.assertThrows From 43e7db86e22c682151ee647012c270c5a3010701 Mon Sep 17 00:00:00 2001 From: sooohi <68193613+sooohi@users.noreply.github.com> Date: Wed, 8 Nov 2023 02:34:38 +0900 Subject: [PATCH 18/19] =?UTF-8?q?fix=20:=20LottoTest=EC=97=90=EB=9F=AC=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/lotto/Application.kt | 1 + src/main/kotlin/lotto/Model/Lotto.kt | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/lotto/Application.kt b/src/main/kotlin/lotto/Application.kt index dd0eee40f..9a26b5a07 100644 --- a/src/main/kotlin/lotto/Application.kt +++ b/src/main/kotlin/lotto/Application.kt @@ -5,4 +5,5 @@ import lotto.Controller.LottoController fun main() { val lottoController = LottoController() lottoController.startGame() + } diff --git a/src/main/kotlin/lotto/Model/Lotto.kt b/src/main/kotlin/lotto/Model/Lotto.kt index 359ca3859..952ab8ce7 100644 --- a/src/main/kotlin/lotto/Model/Lotto.kt +++ b/src/main/kotlin/lotto/Model/Lotto.kt @@ -3,6 +3,7 @@ package lotto.Model class Lotto(private val numbers: List) { init { require(numbers.size == 6) { "๋กœ๋˜ ๋ฒˆํ˜ธ๋Š” 6๊ฐœ์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค." } + require(numbers.toSet().size == 6) { "๋กœ๋˜ ๋ฒˆํ˜ธ์— ์ค‘๋ณต๋œ ์ˆซ์ž๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค." } require(numbers.all { it in 1..45 }) { "๋กœ๋˜ ๋ฒˆํ˜ธ๋Š” 1๋ถ€ํ„ฐ 45 ์‚ฌ์ด์˜ ๊ฐ’์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค." } } @@ -10,7 +11,7 @@ class Lotto(private val numbers: List) { return numbers } - fun checkWinningNumbers(winningNumbers: List, bonusNumber: Int): String { + fun lottoNumberCheck(winningNumbers: List, bonusNumber: Int): String { val matchedNumbers = numbers.filter { it in winningNumbers } val matchingCount = matchedNumbers.size return when (matchingCount) { From ef80eab25a6d507f5b75c084ed61473dce8ae748 Mon Sep 17 00:00:00 2001 From: sooohi <68193613+sooohi@users.noreply.github.com> Date: Wed, 8 Nov 2023 22:14:10 +0900 Subject: [PATCH 19/19] =?UTF-8?q?style=20:=20=EC=B6=9C=EB=A0=A5=ED=98=95?= =?UTF-8?q?=ED=83=9C=20=EB=A7=9E=EC=B6=94=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lotto/Controller/LottoController.kt | 66 ++++++++++++++----- src/main/kotlin/lotto/View/LottoView.kt | 13 ++-- 2 files changed, 56 insertions(+), 23 deletions(-) diff --git a/src/main/kotlin/lotto/Controller/LottoController.kt b/src/main/kotlin/lotto/Controller/LottoController.kt index 37b0f7b8f..32bb0b9b8 100644 --- a/src/main/kotlin/lotto/Controller/LottoController.kt +++ b/src/main/kotlin/lotto/Controller/LottoController.kt @@ -6,6 +6,7 @@ import camp.nextstep.edu.missionutils.Console class LottoController { private val lottoView = LottoView() + enum class LottoPrize(val sameCount: Int, val prizeMoney: Int, val prizeName: String) { threeSame(3, 5000, "3๊ฐœ ์ผ์น˜"), fourSame(4, 50000, "4๊ฐœ ์ผ์น˜"), @@ -13,6 +14,7 @@ class LottoController { fiveSamePlusBonus(5, 30000000, "5๊ฐœ ์ผ์น˜, ๋ณด๋„ˆ์Šค ๋ณผ ์ผ์น˜"), sixSame(6, 2000000000, "6๊ฐœ ์ผ์น˜") } + fun startGame() { try { val inputMoney = lottoMoneyInput() @@ -27,16 +29,30 @@ class LottoController { lottoView.printResult(result, count) } catch (e: IllegalArgumentException) { lottoView.printMessage("[ERROR] ${e.message}") + startGame() } } private fun lottoMoneyInput(): Int { - lottoView.printMessage("๊ตฌ์ž…๊ธˆ์•ก์„ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.") - val inputMoney = Console.readLine().toInt() - if (inputMoney % 1000 != 0) { - throw IllegalArgumentException("1000์› ๋‹จ์œ„๋กœ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.") + val message = "๊ตฌ์ž…๊ธˆ์•ก์„ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”." + return readIntFromConsole(message) + } + + private fun readIntFromConsole(message: String): Int { + while (true) { + try { + lottoView.printMessage(message) + val input = Console.readLine().toInt() + if (input % 1000 != 0) { + throw IllegalArgumentException("1000์› ๋‹จ์œ„๋กœ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.") + } + return input + } catch (e: NumberFormatException) { + lottoView.printMessage("[ERROR] ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.") + } catch (e: IllegalArgumentException) { + lottoView.printMessage("[ERROR] ${e.message}") + } } - return inputMoney } private fun lottoCnt(inputMoney: Int): Int { @@ -53,20 +69,37 @@ class LottoController { } private fun lottoNumberChoose(): List { - lottoView.printMessage("๋‹น์ฒจ ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.") - val lottoNumber = Console.readLine().split(",").map { it.toInt() } - if (lottoNumber.toSet().size != 6) { - throw IllegalArgumentException("์ค‘๋ณต๋˜๋Š” ๋ฒˆํ˜ธ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.") - } - if (lottoNumber.size != 6) { - throw IllegalArgumentException("6๊ฐœ์˜ ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”.") - } - return lottoNumber + val message = "๋‹น์ฒจ ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”." + return readNumbersFromConsole(message, 6) } private fun lottoNumberBonus(): Int { - lottoView.printMessage("๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.") - return Console.readLine().toInt() + val message = "\n๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”." + return readNumbersFromConsole(message, 1)[0] + } + + private fun readNumbersFromConsole(message: String, expectedSize: Int): List { + while (true) { + try { + lottoView.printMessage(message) + val input = Console.readLine() + val numbers = input.split(",").map { it.trim().toInt() } + if (numbers.size != expectedSize) { + throw IllegalArgumentException("์ž…๋ ฅํ•œ ์ˆซ์ž์˜ ๊ฐœ์ˆ˜๊ฐ€ ์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์Šต๋‹ˆ๋‹ค.") + } + if (numbers.toSet().size != expectedSize) { + throw IllegalArgumentException("์ค‘๋ณต๋˜๋Š” ๋ฒˆํ˜ธ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.") + } + if (numbers.any { it < 1 || it > 45 }) { + throw IllegalArgumentException("[ERROR] ๋กœ๋˜ ๋ฒˆํ˜ธ๋Š” 1๋ถ€ํ„ฐ 45 ์‚ฌ์ด์˜ ๊ฐ’์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.") + } + return numbers + } catch (e: NumberFormatException) { + lottoView.printMessage("[ERROR] ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.") + } catch (e: IllegalArgumentException) { + lottoView.printMessage("[ERROR] ${e.message}") + } + } } private fun lottoNumberCheck(lottoList: List>, comNumber: List, bonusNumber: Int): Map { @@ -95,5 +128,4 @@ class LottoController { } return lottoMoneyList } - } diff --git a/src/main/kotlin/lotto/View/LottoView.kt b/src/main/kotlin/lotto/View/LottoView.kt index afd2ec104..899281bfe 100644 --- a/src/main/kotlin/lotto/View/LottoView.kt +++ b/src/main/kotlin/lotto/View/LottoView.kt @@ -8,14 +8,15 @@ class LottoView { } fun printLottoNumbers(lottoCount: Int, lottoList: List>) { - printMessage("$lottoCount ๊ฐœ๋ฅผ ๊ตฌ๋งคํ–ˆ์Šต๋‹ˆ๋‹ค.") + printMessage("\n${lottoCount}๊ฐœ๋ฅผ ๊ตฌ๋งคํ–ˆ์Šต๋‹ˆ๋‹ค.") lottoList.forEach { lotto -> - printMessage(lotto.sorted().toString()) + printMessage(lotto.sorted().joinToString(", ", "[", "]")) } + println() } fun printResult(result: Map, lottoCount: Int) { - printMessage("๋‹น์ฒจ ํ†ต๊ณ„") + printMessage("\n๋‹น์ฒจ ํ†ต๊ณ„") printMessage("---") var totalPrize = 0 val prizeMoney = mapOf( @@ -28,9 +29,9 @@ class LottoView { for ((key, value) in result) { val prize = when (key) { "5๊ฐœ ์ผ์น˜" -> if (result[LottoController.LottoPrize.fiveSamePlusBonus.prizeName] == 0) "1,500,000์›" else "30,000,000์›" - else -> "${prizeMoney[key]}์›" + else -> "${prizeMoney[key]?.let { "%,d".format(it) }}์›" } - printMessage("$key ($prize) - ${value}๊ฐœ") + printMessage("$key ($prize) - $value ๊ฐœ") totalPrize += if (key == "5๊ฐœ ์ผ์น˜") { if (result[LottoController.LottoPrize.fiveSamePlusBonus.prizeName] == 0) prizeMoney["5๊ฐœ ์ผ์น˜"]!! * value else prizeMoney["5๊ฐœ ์ผ์น˜, ๋ณด๋„ˆ์Šค ๋ณผ ์ผ์น˜"]!! * value } else { @@ -39,6 +40,6 @@ class LottoView { } val inputMoney = lottoCount * 1000 val rateOfReturn = ((totalPrize - inputMoney) / inputMoney.toDouble() * 100).coerceAtLeast(0.0) - printMessage("์ด ์ˆ˜์ต๋ฅ ์€ ${"%.1f".format(rateOfReturn)}%์ž…๋‹ˆ๋‹ค.") + printMessage("์ด ์ˆ˜์ต๋ฅ ์€ ${"%,.1f".format(rateOfReturn)}%์ž…๋‹ˆ๋‹ค.") } }