diff --git a/README.md b/README.md index 5fa2560b46..c75ef97ffc 100644 --- a/README.md +++ b/README.md @@ -1 +1,98 @@ # java-lotto-precourse + +### ๐Ÿ”‘ ํ•œ ์ค„ ์†Œ๊ฐœ + +1์žฅ์— 1000์› ์งœ๋ฆฌ ๋กœ๋˜๋ฅผ ๊ตฌ์ž…ํ•œ ๊ธˆ์•ก์— ๋งž์ถฐ ๋žœ๋คํ•˜๊ณ  ์ค‘๋ณต์ด ์—†๋Š” 6์ž๋ฆฌ ์ˆ˜๋ฅผ ๋ฐœํ–‰ํ•ด์ฃผ๋ฉด, ์ด๋ฅผ ๋‹น์ฒจ ๋ฒˆํ˜ธ์™€ ๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ์— ๊ธฐ๋ฐ˜ํ•˜์—ฌ ๋‹น์ฒจ ๊ฒฐ๊ณผ(๋‹น์ฒจ ๋“ฑ์ˆ˜, ์ˆ˜์ต๋ฅ )์„ ์•Œ๋ ค์ค€๋‹ค. + +
+ +### ๐Ÿ“‘ ๊ตฌํ˜„ ์‚ฌํ•ญ + +#### 1. ๊ธฐ๋ณธ ์„ธํŒ… + - jdk 21 ๋ฒ„์ „ ์„ค์ • + - java style guide ์„ค์ • + +#### 2. ๊ตฌ์ž… ๊ธˆ์•ก ์ž…๋ ฅ + - readLine() ์œผ๋กœ ๋ฌธ์ž์—ด ์ž…๋ ฅ ๋ฐ›๊ธฐ + - ์ž…๋ ฅ ๊ฐ’์— ์–‘์˜ ์ •์ˆ˜ ์ด์™ธ์˜ ๊ฐ’์ด ์˜ค๋Š” ๊ฒฝ์šฐ IllegalArgumentException ๋ฐœ์ƒ ๋ฐ ์—๋Ÿฌ ๋ฉ”์„ธ์ง€ ํ˜•์‹์— ๋”ฐ๋ผ ์ถœ๋ ฅ + - ์ž…๋ ฅ ๊ฐ’์ด 1000 ๋‹จ์œ„๊ฐ€ ์•„๋‹Œ ๊ฒฝ์šฐ IllegalArgumentException ๋ฐœ์ƒ ๋ฐ ์—๋Ÿฌ ๋ฉ”์„ธ์ง€ ํ˜•์‹์— ๋”ฐ๋ผ ์ถœ๋ ฅ + - IlleagalArgumentException ๋ฐœ์ƒ ์‹œ, ์žฌ์ž…๋ ฅ ๋ฐ›๊ธฐ + +#### 3. ๋กœ๋˜ ์žฅ์ˆ˜ ๊ณ„์‚ฐ ๋ฐ ์ถœ๋ ฅ + - ๊ธˆ์•ก์— ๋”ฐ๋ผ ๋กœ๋˜ ์žฅ์ˆ˜ ๊ณ„์‚ฐ + - ๊ตฌ๋งค ์žฅ์ˆ˜ ์ถœ๋ ฅ + +#### 4. ๋กœ๋˜ ๋ฐœํ–‰ ๋ฐ ์ถœ๋ ฅ + - ์ค‘๋ณต์ด ์•„๋‹Œ ๋žœ๋คํ•œ 6๊ฐœ์˜ ์ˆ˜ ๋ฐœ๊ธ‰ + - ๊ตฌ๋งค ์žฅ์ˆ˜๋งŒํผ ์ถœ๋ ฅ + +#### 5. ๋กœ๋˜ ๋‹น์ฒจ ๋ฒˆํ˜ธ ๋ฐ ๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ ์ž…๋ ฅ + - ๋‹น์ฒจ ๋ฒˆํ˜ธ ์ž…๋ ฅ ๋ฐ›๊ธฐ + - ๋‹น์ฒจ ๋ฒˆํ˜ธ ์ž…๋ ฅ ๋ฌธ์ž์—ด์— 6๊ฐœ ์ดˆ๊ณผ์˜ ์ˆ˜๋ฅผ ์ž…๋ ฅํ•œ ๊ฒฝ์šฐ IllegalArgumentException ๋ฐœ์ƒ ๋ฐ ์—๋Ÿฌ ๋ฉ”์„ธ์ง€ ํ˜•์‹์— ๋”ฐ๋ผ ์ถœ๋ ฅ + - ๋‹น์ฒจ ๋ฒˆํ˜ธ ์ž…๋ ฅ ๋ฌธ์ž์—ด์— ์ค‘๋ณต๋œ ์ˆ˜๊ฐ€ ๋‚˜์˜ค๋Š” ๊ฒฝ์šฐ IllegalArgumentException ๋ฐœ์ƒ ๋ฐ ์—๋Ÿฌ ๋ฉ”์„ธ์ง€ ํ˜•์‹์— ๋”ฐ๋ผ ์ถœ๋ ฅ + - ๋‹น์ฒจ ๋ฒˆํ˜ธ ์ž…๋ ฅ ๋ฌธ์ž์—ด์— `,`์™€ ์ˆซ์ž ์ด์™ธ์˜ ๊ฐ’์ด ์ž…๋ ฅ๋œ ๊ฒฝ์šฐ, IllegalArgumentException ๋ฐœ์ƒ ๋ฐ ์—๋Ÿฌ ๋ฉ”์„ธ์ง€ ํ˜•์‹์— ๋”ฐ๋ผ ์ถœ๋ ฅ + - ๋‹น์ฒจ ๋ฒˆํ˜ธ ์ž…๋ ฅ ๋ฌธ์ž์—ด์— 1 ~ 45 ์‚ฌ์ด์˜ ์ˆ˜๊ฐ€ ์•„๋‹Œ ์ˆ˜๊ฐ€ ์ž…๋ ฅ๋œ ๊ฒฝ์šฐ, `IllegalArgumentException` ๋ฐœ์ƒ ๋ฐ ์—๋Ÿฌ ๋ฉ”์„ธ์ง€ ํ˜•์‹์— ๋”ฐ๋ผ ์ถœ๋ ฅ + + - ๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ ์ž…๋ ฅ ๋ฐ›๊ธฐ + - ๋‹น์ฒจ ๋ฒˆํ˜ธ ์ž…๋ ฅ ๊ฐ’์ด ๋‹น์ฒจ ๋ฒˆํ˜ธ ๋ฆฌ์ŠคํŠธ ์•ˆ์— ์žˆ๋Š” ๊ฐ’์ธ ๊ฒฝ์šฐ(์ค‘๋ณต๋œ ๊ฐ’์ด ์žˆ๋Š” ๊ฒฝ์šฐ) IllegalArgumentException ๋ฐœ์ƒ ๋ฐ ์—๋Ÿฌ ๋ฉ”์„ธ์ง€ ํ˜•์‹์— ๋”ฐ๋ผ ์ถœ๋ ฅ + - ๋‹น์ฒจ ๋ฒˆํ˜ธ ์ž…๋ ฅ ๋ฌธ์ž์—ด์— ์ˆซ์ž ์ด์™ธ์˜ ๊ฐ’์ด ์ž…๋ ฅ๋œ ๊ฒฝ์šฐ, IllegalArgumentException ๋ฐœ์ƒ ๋ฐ ์—๋Ÿฌ ๋ฉ”์„ธ์ง€ ํ˜•์‹์— ๋”ฐ๋ผ ์ถœ๋ ฅ + - ๋‹น์ฒจ ๋ฒˆํ˜ธ ์ž…๋ ฅ ๋ฌธ์ž์—ด์— 1 ~ 45 ์‚ฌ์ด์˜ ์ˆ˜๊ฐ€ ์•„๋‹Œ ์ˆ˜๊ฐ€ ์ž…๋ ฅ๋œ ๊ฒฝ์šฐ, `IllegalArgumentException` ๋ฐœ์ƒ ๋ฐ ์—๋Ÿฌ ๋ฉ”์„ธ์ง€ ํ˜•์‹์— ๋”ฐ๋ผ ์ถœ๋ ฅ + - IlleagalArgumentException ๋ฐœ์ƒ ์‹œ, ์žฌ์ž…๋ ฅ ๋ฐ›๊ธฐ + +#### 6. ๋‹น์ฒจ ํ†ต๊ณ„ ์ถœ๋ ฅ + - ๋ฐœํ–‰ํ•œ ๋กœ๋˜ ๋ฒˆํ˜ธ์™€ ๋‹น์ฒจ ๋ฒˆํ˜ธ ์ผ์น˜ ๊ฐœ์ˆ˜ ํ™•์ธ + - ๋ฐœํ–‰ํ•œ ๋กœ๋˜ ๋ฒˆํ˜ธ ์ผ์น˜ ๊ฐœ์ˆ˜ ๋ณ„ ํ†ต๊ณ„ ์ถœ๋ ฅ + +#### 7. ์ˆ˜์ต๋ฅ  ๊ณ„์‚ฐ ๋ฐ ์ถœ๋ ฅ + - (์ˆ˜์ต๊ธˆ / ๊ตฌ๋งค ๊ธˆ์•ก) * 100 ๊ฒฐ๊ณผ์—์„œ ์†Œ์ˆ˜์  ๋‘˜์งธ์ž๋ฆฌ ๋ฐ˜์˜ฌ๋ฆผํ•˜์—ฌ ์ˆ˜์ต๋ฅ  ์ถœ๋ ฅ + +#### 8. ํ…Œ์ŠคํŠธ ์ฝ”๋“œ ์ž‘์„ฑ + [์„ฑ๊ณต] + - 8000 / 1,2,3,4,5,6 / 7 ์ž…๋ ฅ ์‹œ ์˜ˆ์‹œ ๋ฐ์ดํ„ฐ์— ๋”ฐ๋ฅธ ์ถœ๋ ฅ ๊ฒฐ๊ณผ ๋ฐ ์ˆ˜์ต๋ฅ  ํ™•์ธ + - 7000 / 1,2,3,4,5,6 / 7 ์ž…๋ ฅ ์‹œ, 4๊ฐœ ์ผ์น˜ ๊ฒฝ์šฐ 1๊ฐœ ๋ฐœ์ƒ ์‹œ ์ˆ˜์ต๋ฅ  ์ถœ๋ ฅ ๊ฒฐ๊ณผ 2์ž๋ฆฌ ์ˆ˜ ๋ฐ˜์˜ฌ๋ฆผ ํ™•์ธ + + [์‹คํŒจ-๋กœ๋˜ ๊ฐ€๊ฒฉ] + - ๋กœ๋˜ ๊ฐ€๊ฒฉ์ด 1000 ์› ๋‹จ์œ„๊ฐ€ ์•„๋‹Œ ๊ฒฝ์šฐ, ERROR ๋ฉ”์„ธ์ง€ ํฌํ•จ ์—ฌ๋ถ€ ๊ฒ€์‚ฌ + - ๋กœ๋˜ ๊ฐ€๊ฒฉ์— ์–‘์˜ ์ •์ˆ˜ ์ด์™ธ์˜ ๊ฐ’์ด ์˜ค๋Š” ๊ฒฝ์šฐ, ERROR ๋ฉ”์„ธ์ง€ ํฌํ•จ ์—ฌ๋ถ€ ๊ฒ€์‚ฌ + + [์‹คํŒจ-๋‹น์ฒจ ๋ฒˆํ˜ธ] + - ๋‹น์ฒจ ๋ฒ”ํ˜ธ ์ž…๋ ฅ ์‹œ `,`์™€ ์ˆซ์ž ์ด์™ธ์˜ ๊ฐ’์ด ์ž…๋ ฅ๋œ ๊ฒฝ์šฐ, ERROR ๋ฉ”์„ธ์ง€ ํฌํ•จ ์—ฌ๋ถ€ ๊ฒ€์‚ฌ + + [์‹คํŒจ-๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ] + - ๋‹น์ฒจ ๋ฒ”ํ˜ธ ์ž…๋ ฅ ์‹œ ์ˆซ์ž ์ด์™ธ์˜ ๊ฐ’์ด ์ž…๋ ฅ๋œ ๊ฒฝ์šฐ, ERROR ๋ฉ”์„ธ์ง€ ํฌํ•จ ์—ฌ๋ถ€ ๊ฒ€์‚ฌ + - ๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ ์ž…๋ ฅ ์‹œ ๋‹น์ฒจ ๋ฒˆํ˜ธ์™€ ์ค‘๋ณต๋œ ์ˆ˜๊ฐ€ ๋‚˜์˜ค๋Š” ๊ฒฝ์šฐ, ERROR ๋ฉ”์„ธ์ง€ ํฌํ•จ ์—ฌ๋ถ€ ๊ฒ€์‚ฌ + + [์‹คํŒจ-๋กœ๋˜] + - 6๊ฐœ ์ดˆ๊ณผ์˜ ์ˆ˜๋ฅผ ์ž…๋ ฅํ•œ ๊ฒฝ์šฐ, ERROR ๋ฉ”์„ธ์ง€ ํฌํ•จ ์—ฌ๋ถ€ ๊ฒ€์‚ฌ + - ์ค‘๋ณต๋œ ์ˆ˜๊ฐ€ ๋‚˜์˜ค๋Š” ๊ฒฝ์šฐ, ERROR ๋ฉ”์„ธ์ง€ ํฌํ•จ ์—ฌ๋ถ€ ๊ฒ€์‚ฌ + - 1 ~ 45 ์‚ฌ์ด์˜ ์ˆ˜๊ฐ€ ์•„๋‹Œ ์ˆ˜๊ฐ€ ์ž…๋ ฅ๋œ ๊ฒฝ์šฐ, ERROR ๋ฉ”์„ธ์ง€ ํฌํ•จ ์—ฌ๋ถ€ ๊ฒ€์‚ฌ + +
+ +### โœ… ์กฐ๊ฑด +- ๋กœ๋˜ ๋ฒˆํ˜ธ +- [ ] ๋กœ๋˜ ๋ฒˆํ˜ธ์˜ ์ˆซ์ž ๋ฒ”์œ„๋Š” 1 ~ 45 ์‚ฌ์ด์—๋งŒ ์กด์žฌํ•จ +- [ ] ๋กœ๋˜ ๋ฒˆํ˜ธ๋Š” ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜์—ฌ ๋ณด์—ฌ์คŒ +- [ ] 1๊ฐœ์˜ ๋กœ๋˜ ๋ฐœํ–‰(6๊ฐœ์˜ ๋ฒˆํ˜ธ ์ถ”์ถœ) ์‹œ, ๋‹น์ฒจ๋ฒˆํ˜ธ 6๊ฐœ ์ถ”์ฒจ ์‹œ ์ค‘๋ณต๋œ ์ˆซ์ž๊ฐ€ ๋‚˜์˜ค๋ฉด ์•ˆ๋จ + - ๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ์˜ ๊ฒฝ์šฐ๋„ ๋‹น์ฒจ๋ฒˆํ˜ธ์™€ ์ค‘๋ณต๋œ ์ˆซ์ž์ด๋ฉด ์•ˆ๋จ + +- ๋กœ๋˜ ๊ฐ€๊ฒฉ +- [ ] ๋กœ๋˜ ๊ฐ€๊ฒฉ์— ์–‘์˜ ์ •์ˆ˜ ์ด์™ธ์˜ ๊ฐ’์ด ์˜ค๋Š” ๊ฒฝ์šฐ, `IllegalArgumentException` ๋ฐœ์ƒ ๋ฐ `[ERROR]` ๋กœ ์‹œ์ž‘ํ•˜๋Š” ๋ฉ”์„ธ์ง€ ์ถœ๋ ฅ +- [ ] ๋กœ๋˜ ๊ฐ€๊ฒฉ์ด 1000 ๋‹จ์œ„๊ฐ€ ์•„๋‹ ๊ฒฝ์šฐ, `IllegalArgumentException` ๋ฐœ์ƒ ๋ฐ `[ERROR]` ๋กœ ์‹œ์ž‘ํ•˜๋Š” ๋ฉ”์„ธ์ง€ ์ถœ๋ ฅ + +- ๋‹น์ฒจ ๋ฒˆํ˜ธ +- [ ] ๋‹น์ฒจ ๋ฒˆํ˜ธ๋Š” `,`๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๊ตฌ๋ถ„๋จ +- [ ] ๋‹น์ฒจ ๋ฒˆํ˜ธ ์ž…๋ ฅ ์‹œ 6๊ฐœ ์ดˆ๊ณผ์˜ ์ˆ˜๋ฅผ ์ž…๋ ฅํ•œ ๊ฒฝ์šฐ, `IllegalArgumentException` ๋ฐœ์ƒ ๋ฐ `[ERROR]` ๋กœ ์‹œ์ž‘ํ•˜๋Š” ๋ฉ”์„ธ์ง€ ์ถœ๋ ฅ +- [ ] ๋‹น์ฒจ ๋ฒˆํ˜ธ ์ž…๋ ฅ ์‹œ ์ค‘๋ณต๋œ ์ˆ˜๊ฐ€ ๋‚˜์˜ค๋Š” ๊ฒฝ์šฐ, `IllegalArgumentException` ๋ฐœ์ƒ ๋ฐ `[ERROR]` ๋กœ ์‹œ์ž‘ํ•˜๋Š” ๋ฉ”์„ธ์ง€ ์ถœ๋ ฅ +- [ ] ๋‹น์ฒจ ๋ฒˆํ˜ธ ์ž…๋ ฅ ์‹œ `,`์™€ ์ˆซ์ž ์ด์™ธ์˜ ๊ฐ’์ด ์ž…๋ ฅ๋œ ๊ฒฝ์šฐ, `IllegalArgumentException` ๋ฐœ์ƒ ๋ฐ `[ERROR]` ๋กœ ์‹œ์ž‘ํ•˜๋Š” ๋ฉ”์„ธ์ง€ ์ถœ๋ ฅ +- [ ] ๋‹น์ฒจ ๋ฒˆํ˜ธ ์ž…๋ ฅ ์‹œ 1 ~ 45 ์‚ฌ์ด์˜ ์ˆ˜๊ฐ€ ์•„๋‹Œ ์ˆ˜๊ฐ€ ์ž…๋ ฅ๋œ ๊ฒฝ์šฐ, `IllegalArgumentException` ๋ฐœ์ƒ ๋ฐ `[ERROR]` ๋กœ ์‹œ์ž‘ํ•˜๋Š” ๋ฉ”์„ธ์ง€ ์ถœ๋ ฅ + +- ๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ +- [ ] ๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ ์ž…๋ ฅ ์‹œ ๋‹น์ฒจ ๋ฒˆํ˜ธ์™€ ์ค‘๋ณต๋œ ์ˆ˜๊ฐ€ ๋‚˜์˜ค๋Š” ๊ฒฝ์šฐ, `IllegalArgumentException` ๋ฐœ์ƒ ๋ฐ `[ERROR]` ๋กœ ์‹œ์ž‘ํ•˜๋Š” ๋ฉ”์„ธ์ง€ ์ถœ๋ ฅ +- [ ] ๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ ์ž…๋ ฅ ์‹œ ์ˆซ์ž ์ด์™ธ์˜ ๊ฐ’์ด ์ž…๋ ฅ๋œ ๊ฒฝ์šฐ, `IllegalArgumentException` ๋ฐœ์ƒ ๋ฐ `[ERROR]` ๋กœ ์‹œ์ž‘ํ•˜๋Š” ๋ฉ”์„ธ์ง€ ์ถœ๋ ฅ +- [ ] ๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ ์ž…๋ ฅ ์‹œ ์ž…๋ ฅ ์‹œ 1 ~ 45 ์‚ฌ์ด์˜ ์ˆ˜๊ฐ€ ์•„๋‹Œ ์ˆ˜๊ฐ€ ์ž…๋ ฅ๋œ ๊ฒฝ์šฐ, `IllegalArgumentException` ๋ฐœ์ƒ ๋ฐ `[ERROR]` ๋กœ ์‹œ์ž‘ํ•˜๋Š” ๋ฉ”์„ธ์ง€ ์ถœ๋ ฅ + +- ์ˆ˜์ต๋ฅ  +- [ ] ์†Œ์ˆ˜์  ๋‘˜์งธ ์ž๋ฆฌ์—์„œ ๋ฐ˜์˜ฌ๋ฆผํ•œ ๊ฐ’์œผ๋กœ ์ถœ๋ ฅ + + diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java index d190922ba4..dbc07a09d3 100644 --- a/src/main/java/lotto/Application.java +++ b/src/main/java/lotto/Application.java @@ -1,7 +1,115 @@ package lotto; +import static lotto.InputUtil.checkInputIsNotInNumbers; +import static lotto.InputUtil.checkOverThanMinAndLessThanMax; +import static lotto.InputUtil.isClearedByNum; +import static lotto.InputUtil.isNumeric; +import static lotto.InputUtil.isNumericWithCommas; +import static lotto.InputUtil.splitByComma; +import static lotto.Lotto.LOTTO_BASIC_PRICE; +import static lotto.Lotto.LOTTO_MAX_NUM; +import static lotto.Lotto.LOTTO_MIN_NUM; +import static lotto.Lotto.checkLotto; +import static lotto.Lotto.makeRandomLotto; + +import camp.nextstep.edu.missionutils.Console; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + public class Application { + + public static final String WINNING_NUMBERS_NAME = "๋‹น์ฒจ ๋ฒˆํ˜ธ"; + public static void main(String[] args) { - // TODO: ํ”„๋กœ๊ทธ๋žจ ๊ตฌํ˜„ + + // ๊ตฌ์ž… ๊ธˆ์•ก ์ž…๋ ฅ ๋ฐ›๊ณ  ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ + int purchasesCount; + while (true) { + try { + System.out.println("๊ตฌ์ž…๊ธˆ์•ก์„ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.\n"); + String price = Console.readLine(); + + isNumeric(price); + isClearedByNum(price, LOTTO_BASIC_PRICE); // ๊ตฌ์ž…๊ธˆ์•ก์ด 1000์› ๋‹จ์œ„์ธ์ง€ ํ™•์ธ + + purchasesCount = Integer.parseInt(price) / LOTTO_BASIC_PRICE; + break; + } catch (IllegalArgumentException e) { + System.out.println("์ž˜๋ชป๋œ ์ž…๋ ฅ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”\n ์˜ˆ์™ธ ๋ฉ”์„ธ์ง€: " + e.getMessage()); + } + } + + // ๊ตฌ๋งค ๊ฐœ์ˆ˜ ์ถœ๋ ฅ + System.out.println(purchasesCount + "๊ฐœ๋ฅผ ๊ตฌ๋งคํ–ˆ์Šต๋‹ˆ๋‹ค.\n"); + + // ๊ตฌ๋งคํ•œ ๋กœ๋˜ ๋ฐœํ–‰ + List purchases = new ArrayList<>(); + for (int i = 0; i < purchasesCount; i++) { + purchases.add(makeRandomLotto()); + System.out.println(purchases.get(i).getNumbers()); + } + + // ๋‹น์ฒจ ๋กœ๋˜ ๋ฒˆํ˜ธ ์ž…๋ ฅ ๋ฐ›๊ธฐ + Lotto winningLotto; + while (true) { + try { + System.out.println("๋‹น์ฒจ ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.\n"); + String winningNumbers = Console.readLine(); + + isNumericWithCommas(winningNumbers); + + // ๋‹น์ฒจ ๋ฒˆํ˜ธ๋กœ ๋กœ๋˜ ๋งŒ๋“ค๊ธฐ + winningLotto = new Lotto(splitByComma(winningNumbers)); + break; + } catch (IllegalArgumentException e) { + System.out.println("์ž˜๋ชป๋œ ์ž…๋ ฅ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”\n ์˜ˆ์™ธ ๋ฉ”์„ธ์ง€: " + e.getMessage()); + } + } + + int bonusNumber; + // ๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ ์ž…๋ ฅ ๋ฐ›๊ธฐ + while (true) { + try { + System.out.println("๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.\n"); + String enteredBonusNumber = Console.readLine(); + + isNumeric(enteredBonusNumber); + checkOverThanMinAndLessThanMax(Integer.parseInt(enteredBonusNumber), LOTTO_MIN_NUM, + LOTTO_MAX_NUM); + checkInputIsNotInNumbers(winningLotto.getNumbers(), enteredBonusNumber, + WINNING_NUMBERS_NAME); + + bonusNumber = Integer.parseInt(enteredBonusNumber); + break; + } catch (IllegalArgumentException e) { + System.out.println("์ž˜๋ชป๋œ ์ž…๋ ฅ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”\n ์˜ˆ์™ธ ๋ฉ”์„ธ์ง€: " + e.getMessage()); + } + } + + ArrayList statics = new ArrayList(Arrays.asList(0, 0, 0, 0, 0)); + + // ๋‹น์ฒจ ๋‚ด์—ญ ์ถœ๋ ฅ + for (Lotto lotto : purchases) { + int result = checkLotto(lotto.getNumbers(), winningLotto.getNumbers(), bonusNumber); + if (result >= 0 && result < 5) { + statics.set(result, statics.get(result) + 1); + } + } + + System.out.println("3๊ฐœ ์ผ์น˜ (5,000์›) - " + statics.get(0) + "๊ฐœ"); + System.out.println("4๊ฐœ ์ผ์น˜ (50,000์›) - " + statics.get(1) + "๊ฐœ"); + System.out.println("5๊ฐœ ์ผ์น˜ (1,500,000์›) - " + statics.get(2) + "๊ฐœ"); + System.out.println("5๊ฐœ ์ผ์น˜, ๋ณด๋„ˆ์Šค ๋ณผ ์ผ์น˜ (30,000,000์›) - " + statics.get(3) + "๊ฐœ"); + System.out.println("6๊ฐœ ์ผ์น˜ (2,000,000,000์›) - " + statics.get(4) + "๊ฐœ"); + + // ์ˆ˜์ต๋ฅ  ๊ณ„์‚ฐ ๋ฐ ์ถœ๋ ฅ + int profits = statics.get(0)*5000 + statics.get(1)*50000 + statics.get(2)*1500000 + statics.get(3)*30000000 + statics.get(4)*2000000000; + double rateOfReturn = (profits / (double)(purchasesCount * LOTTO_BASIC_PRICE)) * 100; + double roundedRateOfReturn = Math.round(rateOfReturn * 100) / 100.0; + System.out.println("์ด ์ˆ˜์ต๋ฅ ์€ " + roundedRateOfReturn + "%์ž…๋‹ˆ๋‹ค."); + + } + } diff --git a/src/main/java/lotto/InputUtil.java b/src/main/java/lotto/InputUtil.java new file mode 100644 index 0000000000..75b85dd5b8 --- /dev/null +++ b/src/main/java/lotto/InputUtil.java @@ -0,0 +1,57 @@ +package lotto; + +import java.util.ArrayList; +import java.util.List; + +public class InputUtil { + // ์ •์ˆ˜์ธ์ง€ ํ™•์ธ + public static void isNumeric(String input) { + if (input == null || !input.matches("\\d+")) { + throw new IllegalArgumentException("[ERROR] ์ž…๋ ฅ ๊ฐ’ ๋‚ด์— ์ •์ˆ˜ ์™ธ์˜ ๊ฐ’์ด ์žˆ์Šต๋‹ˆ๋‹ค."); + } + } + + // ์ •์ˆ˜ ๋ฐ , ์ธ์ง€ ํ™•์ธ + public static void isNumericWithCommas(String input) { + if (input == null || !input.matches("[\\d,]+")) { + throw new IllegalArgumentException("[ERROR] ์ž…๋ ฅ ๊ฐ’ ๋‚ด์— ์ •์ˆ˜๋‚˜ , ์™ธ์˜ ๊ฐ’์ด ์žˆ์Šต๋‹ˆ๋‹ค."); + } + } + + // ๋‹จ์œ„ ํ™•์ธ + public static void isClearedByNum(String input, int num) { + if ((Integer.parseInt(input) % num) != 0) { + throw new IllegalArgumentException("[ERROR] " + num + "๋‹จ์œ„์˜ ๊ฐ’๋งŒ ์ž…๋ ฅ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค."); + } + } + + // ์ž…๋ ฅ๊ฐ’์ด min ์ด์ƒ, max ๋ฏธ๋งŒ์ธ์ง€ ํ™•์ธํ•˜๋Š” + public static void checkOverThanMinAndLessThanMax(int i, int min, int max) { + if (!(i >= min && i <= max)) { + throw new IllegalArgumentException("[ERROR] ์ž…๋ ฅ ๊ฐ’์€ " + min + "๊ณผ " + max + " ์‚ฌ์ด์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค."); + } + } + + public static void checkInputIsNotInNumbers(List numbers, String input, String numbersName) { + for (int i : numbers) { + if (i == Integer.parseInt(input)) { + throw new IllegalArgumentException("[ERROR] ํ•ด๋‹น ๊ฐ’์ด " + numbersName + " ๋‚ด์— ์กด์žฌํ•˜๋ฉด ์•ˆ๋ฉ๋‹ˆ๋‹ค."); + } + } + } + + // ๋ฌธ์ž์—ด , ๊ธฐ์ค€์œผ๋กœ ์ชผ๊ฐœ๊ธฐ + public static List splitByComma(String input) { + List numbersList = new ArrayList<>(); + + if (input != null && !input.isEmpty()) { + String[] parts = input.split(","); + for (String part : parts) { + numbersList.add(Integer.parseInt(part.trim())); + } + } + + return numbersList; + } + +} diff --git a/src/main/java/lotto/Lotto.java b/src/main/java/lotto/Lotto.java index 88fc5cf12b..53811512a2 100644 --- a/src/main/java/lotto/Lotto.java +++ b/src/main/java/lotto/Lotto.java @@ -1,20 +1,113 @@ package lotto; +import static lotto.InputUtil.checkOverThanMinAndLessThanMax; + +import java.util.ArrayList; +import java.util.HashSet; import java.util.List; +import java.util.Random; +import java.util.Set; +import java.util.function.Supplier; public class Lotto { + public static final int LOTTO_SIZE = 6; + public static final int LOTTO_MAX_NUM = 45; + public static final int LOTTO_MIN_NUM = 1; + public static final int LOTTO_BASIC_PRICE = 1000; + private final List numbers; public Lotto(List numbers) { - validate(numbers); + validateSixCount(numbers); + validateNoDuplicates(numbers); + validateCoverage(numbers); this.numbers = numbers; } - private void validate(List numbers) { - if (numbers.size() != 6) { + // ๋žœ๋ค ๋กœ๋˜ ์ƒ์„ฑ + public static Lotto makeRandomLotto(){ + return new Lotto(generateUniqueRandomNumbers()); + } + + public List getNumbers() { + return numbers; + } + + // 6์ž๋ฆฌ ์ˆซ์ž์ธ์ง€ ํ™•์ธ + private void validateSixCount(List numbers) { + if (numbers.size() != LOTTO_SIZE) { throw new IllegalArgumentException("[ERROR] ๋กœ๋˜ ๋ฒˆํ˜ธ๋Š” 6๊ฐœ์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค."); } } - // TODO: ์ถ”๊ฐ€ ๊ธฐ๋Šฅ ๊ตฌํ˜„ + // ์ค‘๋ณต๋œ ๊ฐ’์ด ์žˆ๋Š”์ง€ ํ™•์ธ + private void validateNoDuplicates(List numbers) { + Set uniqueNumbers = new HashSet<>(numbers); + if (uniqueNumbers.size() != numbers.size()) { + throw new IllegalArgumentException("[ERROR] ์ค‘๋ณต๋œ ์ˆซ์ž๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค."); + } + } + + // ๋ฆฌ์ŠคํŠธ ๋‚ด์˜ ๋ชจ๋“  ์ˆซ์ž๊ฐ€ ๋ฒ”์œ„ ๋‚ด์ธ์ง€ ํ™•์ธ + private void validateCoverage(List numbers) { + for (int i : numbers) { + checkOverThanMinAndLessThanMax(i, LOTTO_MIN_NUM, LOTTO_MAX_NUM); + } + } + + // ๋žœ๋ค ๋ฒˆํ˜ธ ์ƒ์„ฑ + private static List generateUniqueRandomNumbers() { + Set uniqueNumbers = new HashSet<>(); + Random random = new Random(); + + while (uniqueNumbers.size() < LOTTO_SIZE) { + int randomNumber = random.nextInt(LOTTO_MAX_NUM - LOTTO_MIN_NUM + 1) + LOTTO_MIN_NUM; + uniqueNumbers.add(randomNumber); + } + + return new ArrayList<>(uniqueNumbers); + } + + // ๋‹น์ฒจ ๋ฒˆํ˜ธ ๋น„๊ต + private static int countMatchingNumbers(List uniqueNumbers, List winningNumbers) { + Set unique = new HashSet<>(uniqueNumbers); + Set winning = new HashSet<>(winningNumbers); + + unique.retainAll(winning); + return unique.size(); + } + + // ๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ์™€ ์ผ์น˜ ๋น„๊ต + private static boolean isBonusMatched(List uniqueNumbers, int bonusNumber) { + return uniqueNumbers.contains(bonusNumber); + } + + // ์ผ์น˜ ๊ฐœ์ˆ˜์™€ ๋ณด๋„ˆ์Šค ์ผ์น˜ ์—ฌ๋ถ€์— ๋”ฐ๋ฅธ ๊ฒฐ๊ณผ ๋ฐ˜ํ™˜ + public static int getPrizeForMatchCount(int matchCount, boolean bonusMatch) { + if (matchCount == 6) { + return 4; + } + if (matchCount == 5 && bonusMatch) { + return 3; + } + if (matchCount == 5) { + return 2; + } + if (matchCount == 4) { + return 1; + } + if (matchCount == 3) { + return 0; + } + return -1; + } + + // ์ตœ์ข… ๊ฒฐ๊ณผ ๊ณ„์‚ฐ ํ•จ์ˆ˜ + public static int checkLotto(List uniqueNumbers, List winningNumbers, int bonusNumber) { + int matchCount = countMatchingNumbers(uniqueNumbers, winningNumbers); + boolean bonusMatch = isBonusMatched(uniqueNumbers, bonusNumber); + return getPrizeForMatchCount(matchCount, bonusMatch); + } + + } diff --git a/src/test/java/lotto/ApplicationTest.java b/src/test/java/lotto/ApplicationTest.java index a15c7d1f52..2f2b97b4ea 100644 --- a/src/test/java/lotto/ApplicationTest.java +++ b/src/test/java/lotto/ApplicationTest.java @@ -47,13 +47,44 @@ class ApplicationTest extends NsTest { } @Test - void ์˜ˆ์™ธ_ํ…Œ์ŠคํŠธ() { + void ๋กœ๋˜๊ฐ€๊ฒฉ_์ˆซ์ž_์ด์™ธ์˜_๊ฐ’_์ž…๋ ฅ_์˜ˆ์™ธ_ํ…Œ์ŠคํŠธ() { assertSimpleTest(() -> { runException("1000j"); assertThat(output()).contains(ERROR_MESSAGE); }); } + @Test + void ๋กœ๋˜๊ฐ€๊ฒฉ_์ฒœ์›_๋‹จ์œ„_์•„๋‹Œ_๊ฒฝ์šฐ_์˜ˆ์™ธ_ํ…Œ์ŠคํŠธ() { + assertSimpleTest(() -> { + runException("1003"); + assertThat(output()).contains(ERROR_MESSAGE); + }); + } + + @Test + void ๋‹น์ฒจ๋ฒˆํ˜ธ_์‰ผํ‘œ_์ˆซ์ž_์ด์™ธ์˜_๊ฐ’_์ž…๋ ฅ์ธ_๊ฒฝ์šฐ_์˜ˆ์™ธ_ํ…Œ์ŠคํŠธ() { + assertSimpleTest(() -> { + runException("1000", "1,2,3&4,5,6"); + assertThat(output()).contains(ERROR_MESSAGE); + }); + } + @Test + void ๋ณด๋„ˆ์Šค๋ฒˆํ˜ธ_์‰ฝํ‘œ_์ˆซ์ž_์ด์™ธ์˜_๊ฐ’_์ž…๋ ฅ์ธ_๊ฒฝ์šฐ_์˜ˆ์™ธ_ํ…Œ์ŠคํŠธ() { + assertSimpleTest(() -> { + runException("1000", "1,2,3,4,5,6", "12A"); + assertThat(output()).contains(ERROR_MESSAGE); + }); + } + + @Test + void ๋ณด๋„ˆ์Šค๋ฒˆํ˜ธ_๋‹น์ฒจ๋ฒˆํ˜ธ์™€_์ค‘๋ณต์ธ_๊ฒฝ์šฐ_์˜ˆ์™ธ_ํ…Œ์ŠคํŠธ() { + assertSimpleTest(() -> { + runException("1000", "1,2,3,4,5,6", "1"); + assertThat(output()).contains(ERROR_MESSAGE); + }); + } + @Override public void runMain() { Application.main(new String[]{}); diff --git a/src/test/java/lotto/LottoTest.java b/src/test/java/lotto/LottoTest.java index 309f4e50ae..5e657018d9 100644 --- a/src/test/java/lotto/LottoTest.java +++ b/src/test/java/lotto/LottoTest.java @@ -1,25 +1,84 @@ package lotto; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import java.util.List; import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; class LottoTest { + + private List validNumbers; + private List winningNumbers; + private int bonusNumber; + + @BeforeEach + void setUp() { + validNumbers = Arrays.asList(3, 7, 12, 19, 25, 32); + winningNumbers = Arrays.asList(3, 7, 12, 25, 30, 32); + bonusNumber = 19; + } + + @DisplayName("๋กœ๋˜ ๋ฒˆํ˜ธ์˜ ๊ฐœ์ˆ˜๊ฐ€ 6๊ฐœ๊ฐ€ ๋„˜์–ด๊ฐ€๋ฉด ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค") @Test void ๋กœ๋˜_๋ฒˆํ˜ธ์˜_๊ฐœ์ˆ˜๊ฐ€_6๊ฐœ๊ฐ€_๋„˜์–ด๊ฐ€๋ฉด_์˜ˆ์™ธ๊ฐ€_๋ฐœ์ƒํ•œ๋‹ค() { assertThatThrownBy(() -> new Lotto(List.of(1, 2, 3, 4, 5, 6, 7))) .isInstanceOf(IllegalArgumentException.class); } - @DisplayName("๋กœ๋˜ ๋ฒˆํ˜ธ์— ์ค‘๋ณต๋œ ์ˆซ์ž๊ฐ€ ์žˆ์œผ๋ฉด ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.") @Test void ๋กœ๋˜_๋ฒˆํ˜ธ์—_์ค‘๋ณต๋œ_์ˆซ์ž๊ฐ€_์žˆ์œผ๋ฉด_์˜ˆ์™ธ๊ฐ€_๋ฐœ์ƒํ•œ๋‹ค() { assertThatThrownBy(() -> new Lotto(List.of(1, 2, 3, 4, 5, 5))) .isInstanceOf(IllegalArgumentException.class); } + @Test + void ๋กœ๋˜_๋ฒˆํ˜ธ์—_1๊ณผ_45์‚ฌ์ด๊ฐ€_์•„๋‹Œ_๊ฐ’์ด_์žˆ์œผ๋ฉด_์˜ˆ์™ธ๊ฐ€_๋ฐœ์ƒํ•œ๋‹ค(){ + assertThatThrownBy(() -> new Lotto(List.of(46, 1, 2, 3, 4, 5))) + .isInstanceOf(IllegalArgumentException.class); + } + + @Test + void ๋กœ๋˜_๋ฒˆํ˜ธ_๋žœ๋ค_๋ฐœํ–‰_์‹œ_1๊ณผ_45์‚ฌ์ด์˜_6๊ฐœ์˜_์ˆ˜๊ฐ€_๋ฐœํ–‰๋œ๋‹ค(){ + Lotto randomLotto = Lotto.makeRandomLotto(); + List numbers = randomLotto.getNumbers(); + assertEquals(Lotto.LOTTO_SIZE, numbers.size(), "6๊ฐœ์˜ ์ˆ˜๋ฅผ ๊ฐ–๊ณ  ์žˆ๋‹ค."); + assertTrue(numbers.stream().allMatch(num -> num >= Lotto.LOTTO_MIN_NUM && num <= Lotto.LOTTO_MAX_NUM), + "1๊ณผ 45 ์‚ฌ์ด์˜ ๊ฐ’์œผ๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๋‹ค."); + } + + @Test + void ๋‹น์ฒจ_๋ฒˆํ˜ธ์™€_๋ฐœํ–‰_๋กœ๋˜_๋ฒˆํ˜ธ_์‚ฌ์ด์˜_์ผ์น˜๋œ_๊ฐœ์ˆ˜๋ฅผ_ํ™•์ธ_๊ฐ€๋Šฅํ•˜๋‹ค(){ + int matchCount = Lotto.checkLotto(validNumbers, winningNumbers, bonusNumber); + assertEquals(3, matchCount, "๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ ์ œ์™ธ 3๊ฐœ์˜ ์ˆ˜๊ฐ€ ์ผ์น˜ํ•œ๋‹ค."); + } + + @Test + void ์ด๋“ฑ์ธ_๊ฒฝ์šฐ_๋ณด๋„ˆ์Šค_๋ฒˆํ˜ธ์™€_๊ด€๋ จ์ด_์žˆ๋‹ค() { + List userNumbers = Arrays.asList(3, 7, 12, 19, 25, 32); + int prize = Lotto.checkLotto(userNumbers, winningNumbers, bonusNumber); + assertEquals(3, prize, "5๊ฐœ ์ˆ˜ ์ผ์น˜, ๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ ์ผ์น˜"); + } + + @Test + void ์‚ผ๋“ฑ์ธ_๊ฒฝ์šฐ_๋ณด๋„ˆ์Šค_๋ฒˆํ˜ธ์™€_๊ด€๋ จ์ด_์žˆ๋‹ค() { + List userNumbers = Arrays.asList(3, 7, 12, 25, 32, 40); + int prize = Lotto.checkLotto(userNumbers, winningNumbers, bonusNumber); + assertEquals(2, prize, "5๊ฐœ ์ˆ˜ ์ผ์น˜, ๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ ๋ถˆ์ผ์น˜"); + } + + @Test + void ๋ณด๋„ˆ์Šค_๋ฒˆํ˜ธ_ํฌํ•จ_๋“ฑ์ˆ˜_๊ทœ์น™_๋งค์นญ_๋ฒˆํ˜ธ์—_๋งž๊ฒŒ_์ถœ๋ ฅ๋œ๋‹ค() { + assertEquals(4, Lotto.getPrizeForMatchCount(6, false), "6๊ฐœ ์ผ์น˜"); + assertEquals(3, Lotto.getPrizeForMatchCount(5, true), "5๊ฐœ ์ผ์น˜, ๋ณด๋„ˆ์Šค ๋ณผ ์ผ์น˜"); + assertEquals(2, Lotto.getPrizeForMatchCount(5, false), "5๊ฐœ ์ผ์น˜"); + assertEquals(1, Lotto.getPrizeForMatchCount(4, false), "4๊ฐœ ์ผ์น˜"); + assertEquals(0, Lotto.getPrizeForMatchCount(3, false), "3๊ฐœ ์ผ์น˜"); + assertEquals(-1, Lotto.getPrizeForMatchCount(2, false), "3๊ฐœ ๋ฏธ๋งŒ ์ผ์น˜"); + } - // TODO: ์ถ”๊ฐ€ ๊ธฐ๋Šฅ ๊ตฌํ˜„์— ๋”ฐ๋ฅธ ํ…Œ์ŠคํŠธ ์ฝ”๋“œ ์ž‘์„ฑ -} +} \ No newline at end of file