From f75466be9f4d7527d496b2ee405f0e44a9617212 Mon Sep 17 00:00:00 2001 From: Euiyeong <105384780+snsdl0905@users.noreply.github.com> Date: Mon, 4 Nov 2024 14:37:39 +0900 Subject: [PATCH 1/6] =?UTF-8?q?docs:=20README.md=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/README.md b/README.md index 15bb106b5..d2dc95376 100644 --- a/README.md +++ b/README.md @@ -1 +1,18 @@ # javascript-lotto-precourse +### ๐Ÿš€ ๊ธฐ๋Šฅ ๊ตฌํ˜„ ๋ชฉ๋ก +- [ ] ๋กœ๋˜ ๊ตฌ์ž… ๊ธˆ์•ก์„ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค. + - ๋กœ๋˜ 1์žฅ์˜ ๊ฐ€๊ฒฉ์€ 1000์›์ด๋‹ค. +- [ ] ๊ตฌ์ž… ๊ธˆ์•ก ๋งŒํผ์˜ ๋กœ๋˜ ๋ฒˆํ˜ธ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. + - ์ค‘๋ณต๋˜์ง€ ์•Š๋Š” 1~45 ๋ฒ”์œ„์˜ ์ˆซ์ž๋ฅผ 6๊ฐœ์™€ ๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ๋ฅผ ๋ฝ‘๋Š”๋‹ค. +- [ ] ๋‹น์ฒจ ๋ฒˆํ˜ธ์™€ ๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค. +- [ ] ๋‹น์ฒจ ๋‚ด์—ญ์„ ์ถœ๋ ฅํ•œ๋‹ค. + - 1๋“ฑ: 6๊ฐœ ๋ฒˆํ˜ธ ์ผ์น˜ / 2,000,000,000์› + - 2๋“ฑ: 5๊ฐœ ๋ฒˆํ˜ธ + ๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ ์ผ์น˜ / 30,000,000์› + - 3๋“ฑ: 5๊ฐœ ๋ฒˆํ˜ธ ์ผ์น˜ / 1,500,000์› + - 4๋“ฑ: 4๊ฐœ ๋ฒˆํ˜ธ ์ผ์น˜ / 50,000์› + - 5๋“ฑ: 3๊ฐœ ๋ฒˆํ˜ธ ์ผ์น˜ / 5,000์› +- [ ] ์ˆ˜์ต๋ฅ ์„ ์ถœ๋ ฅํ•œ๋‹ค. +- ์˜ˆ์™ธ์ฒ˜๋ฆฌ + - [ ] ๋กœ๋˜ ๊ตฌ์ž… ๊ธˆ์•ก์€ 1000์œผ๋กœ ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง€๋Š” ์–‘์˜ ์ •์ˆ˜์ด๋‹ค. + - [ ] ์ž…๋ ฅ๋ฐ›์€ ๋‹น์ฒจ๋ฒˆํ˜ธ์™€ ๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ์˜ ๋ฒ”์œ„๋Š” 1~45์ด๋‹ค. + - [ ] ์ž…๋ ฅ๋ฐ›์€ ๋‹น์ฒจ๋ฒˆํ˜ธ์™€ ๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ๋Š” ๊ฒน์น˜์ง€ ์•Š๋Š”๋‹ค. \ No newline at end of file From 15f01d21bfe811ad098b2415ab39bba4bf8f793e Mon Sep 17 00:00:00 2001 From: Euiyeong <105384780+snsdl0905@users.noreply.github.com> Date: Mon, 4 Nov 2024 15:47:34 +0900 Subject: [PATCH 2/6] =?UTF-8?q?feat:=20=EB=A1=9C=EB=98=90=20=EA=B5=AC?= =?UTF-8?q?=EC=9E=85=20=EA=B8=88=EC=95=A1=EC=9D=84=20=EC=9E=85=EB=A0=A5?= =?UTF-8?q?=EB=B0=9B=EB=8A=94=EB=8B=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 ++-- src/App.js | 7 ++++++- src/Lotto.js | 11 ++++++++++- src/View.js | 18 ++++++++++++++++++ 4 files changed, 36 insertions(+), 4 deletions(-) create mode 100644 src/View.js diff --git a/README.md b/README.md index d2dc95376..2e7f6597e 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # javascript-lotto-precourse ### ๐Ÿš€ ๊ธฐ๋Šฅ ๊ตฌํ˜„ ๋ชฉ๋ก -- [ ] ๋กœ๋˜ ๊ตฌ์ž… ๊ธˆ์•ก์„ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค. +- [x] ๋กœ๋˜ ๊ตฌ์ž… ๊ธˆ์•ก์„ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค. - ๋กœ๋˜ 1์žฅ์˜ ๊ฐ€๊ฒฉ์€ 1000์›์ด๋‹ค. - [ ] ๊ตฌ์ž… ๊ธˆ์•ก ๋งŒํผ์˜ ๋กœ๋˜ ๋ฒˆํ˜ธ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. - ์ค‘๋ณต๋˜์ง€ ์•Š๋Š” 1~45 ๋ฒ”์œ„์˜ ์ˆซ์ž๋ฅผ 6๊ฐœ์™€ ๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ๋ฅผ ๋ฝ‘๋Š”๋‹ค. @@ -13,6 +13,6 @@ - 5๋“ฑ: 3๊ฐœ ๋ฒˆํ˜ธ ์ผ์น˜ / 5,000์› - [ ] ์ˆ˜์ต๋ฅ ์„ ์ถœ๋ ฅํ•œ๋‹ค. - ์˜ˆ์™ธ์ฒ˜๋ฆฌ - - [ ] ๋กœ๋˜ ๊ตฌ์ž… ๊ธˆ์•ก์€ 1000์œผ๋กœ ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง€๋Š” ์–‘์˜ ์ •์ˆ˜์ด๋‹ค. + - [x] ๋กœ๋˜ ๊ตฌ์ž… ๊ธˆ์•ก์€ 1000์œผ๋กœ ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง€๋Š” ์–‘์˜ ์ •์ˆ˜์ด๋‹ค. - [ ] ์ž…๋ ฅ๋ฐ›์€ ๋‹น์ฒจ๋ฒˆํ˜ธ์™€ ๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ์˜ ๋ฒ”์œ„๋Š” 1~45์ด๋‹ค. - [ ] ์ž…๋ ฅ๋ฐ›์€ ๋‹น์ฒจ๋ฒˆํ˜ธ์™€ ๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ๋Š” ๊ฒน์น˜์ง€ ์•Š๋Š”๋‹ค. \ No newline at end of file diff --git a/src/App.js b/src/App.js index 091aa0a5d..d8833fe65 100644 --- a/src/App.js +++ b/src/App.js @@ -1,5 +1,10 @@ +import { getMoney } from "./View.js"; + class App { - async run() {} + async run() { + const lottoQuantity = await getMoney(); + console.log(lottoQuantity); + } } export default App; diff --git a/src/Lotto.js b/src/Lotto.js index cb0b1527e..2ed9f622b 100644 --- a/src/Lotto.js +++ b/src/Lotto.js @@ -10,9 +10,18 @@ class Lotto { if (numbers.length !== 6) { throw new Error("[ERROR] ๋กœ๋˜ ๋ฒˆํ˜ธ๋Š” 6๊ฐœ์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค."); } + numbers.forEach((number) => { + if(number < 1 || number > 45){ + throw new Error("[ERROR] ๋กœ๋˜ ๋ฒˆํ˜ธ๋Š” 1๋ถ€ํ„ฐ 45 ์‚ฌ์ด์˜ ์ˆซ์ž์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค."); + } + }) + + const numberSet = new Set(numbers); + if(numberSet.size !== 6){ + throw new Error("[ERROR] ๋กœ๋˜ ๋ฒˆํ˜ธ๋Š” ์ค‘๋ณต๋˜์ง€ ์•Š๋Š” 6๊ฐœ์˜ ์ˆซ์ž์ž…๋‹ˆ๋‹ค."); + } } - // TODO: ์ถ”๊ฐ€ ๊ธฐ๋Šฅ ๊ตฌํ˜„ } export default Lotto; diff --git a/src/View.js b/src/View.js new file mode 100644 index 000000000..8db355c79 --- /dev/null +++ b/src/View.js @@ -0,0 +1,18 @@ +import { MissionUtils } from "@woowacourse/mission-utils"; + +export const getMoney = async () => { + const money = await MissionUtils.Console.readLineAsync("๊ตฌ์ž…๊ธˆ์•ก์„ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”\n"); + if(isNaN(money.trim())){ + throw new Error("[ERROR] ๊ตฌ์ž…๊ธˆ์•ก์„ ์ˆซ์ž๋กœ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."); + } + + if(+money < 1000){ + throw new Error("[ERROR] 1000์›๋ถ€ํ„ฐ ๊ตฌ๋งค๊ฐ€ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค."); + } + + if(money%1000 !== 0){ + throw new Error("[ERROR] 1000์› ๋‹จ์œ„๋กœ ๊ตฌ๋งค๊ฐ€ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค."); + } + + return money/1000; +} \ No newline at end of file From 5b679555614f0eb4ae6436c55f8d5c67734302ae Mon Sep 17 00:00:00 2001 From: Euiyeong <105384780+snsdl0905@users.noreply.github.com> Date: Mon, 4 Nov 2024 19:24:38 +0900 Subject: [PATCH 3/6] =?UTF-8?q?feat:=20=EA=B5=AC=EC=9E=85=20=EA=B8=88?= =?UTF-8?q?=EC=95=A1=EB=A7=8C=ED=81=BC=20=EB=A1=9C=EB=98=90=EB=B2=88?= =?UTF-8?q?=ED=98=B8=EB=A5=BC=20=EC=83=9D=EC=84=B1=ED=95=9C=EB=8B=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- src/App.js | 3 ++- src/LottoMachine.js | 30 ++++++++++++++++++++++++++++++ 3 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 src/LottoMachine.js diff --git a/README.md b/README.md index 2e7f6597e..754f66c48 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ ### ๐Ÿš€ ๊ธฐ๋Šฅ ๊ตฌํ˜„ ๋ชฉ๋ก - [x] ๋กœ๋˜ ๊ตฌ์ž… ๊ธˆ์•ก์„ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค. - ๋กœ๋˜ 1์žฅ์˜ ๊ฐ€๊ฒฉ์€ 1000์›์ด๋‹ค. -- [ ] ๊ตฌ์ž… ๊ธˆ์•ก ๋งŒํผ์˜ ๋กœ๋˜ ๋ฒˆํ˜ธ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. +- [x] ๊ตฌ์ž… ๊ธˆ์•ก ๋งŒํผ์˜ ๋กœ๋˜ ๋ฒˆํ˜ธ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. - ์ค‘๋ณต๋˜์ง€ ์•Š๋Š” 1~45 ๋ฒ”์œ„์˜ ์ˆซ์ž๋ฅผ 6๊ฐœ์™€ ๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ๋ฅผ ๋ฝ‘๋Š”๋‹ค. - [ ] ๋‹น์ฒจ ๋ฒˆํ˜ธ์™€ ๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค. - [ ] ๋‹น์ฒจ ๋‚ด์—ญ์„ ์ถœ๋ ฅํ•œ๋‹ค. diff --git a/src/App.js b/src/App.js index d8833fe65..0df05af51 100644 --- a/src/App.js +++ b/src/App.js @@ -1,9 +1,10 @@ +import { getLottoNumbers, offerLottoSheet } from "./LottoMachine.js"; import { getMoney } from "./View.js"; class App { async run() { const lottoQuantity = await getMoney(); - console.log(lottoQuantity); + offerLottoSheet(lottoQuantity); } } diff --git a/src/LottoMachine.js b/src/LottoMachine.js new file mode 100644 index 000000000..4c7996df8 --- /dev/null +++ b/src/LottoMachine.js @@ -0,0 +1,30 @@ +export const getLottoNumbers = () => { + let numberSet = new Set(); + while(numberSet.size < 6){ + let number = Math.floor(Math.random() * 45) + 1 + numberSet.add(number); + } + return [...numberSet].sort((a, b) => a - b); +} + +export const getBonusNumber = (numbers) => { + let number = Math.floor(Math.random() * 45) + 1; + console.log(numbers); + console.log(number); + + while(numbers.has(number)){ + number = Math.floor(Math.random() * 45) + 1; + } + return number; +} + +export const offerLottoSheet = (quantity) => { + let sheet = []; + + for(let i=0; i Date: Mon, 4 Nov 2024 20:31:54 +0900 Subject: [PATCH 4/6] =?UTF-8?q?feat:=20=EB=8B=B9=EC=B2=A8=20=EB=B2=88?= =?UTF-8?q?=ED=98=B8=EC=99=80=20=EB=B3=B4=EB=84=88=EC=8A=A4=20=EB=B2=88?= =?UTF-8?q?=ED=98=B8=EB=A5=BC=20=EC=9E=85=EB=A0=A5=EB=B0=9B=EB=8A=94?= =?UTF-8?q?=EB=8B=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 6 +++--- src/App.js | 9 +++++---- src/LottoMachine.js | 4 ++-- src/View.js | 19 ++++++++++++++++++- src/utils.js | 8 ++++++++ 5 files changed, 36 insertions(+), 10 deletions(-) create mode 100644 src/utils.js diff --git a/README.md b/README.md index 754f66c48..e402ed54b 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ - ๋กœ๋˜ 1์žฅ์˜ ๊ฐ€๊ฒฉ์€ 1000์›์ด๋‹ค. - [x] ๊ตฌ์ž… ๊ธˆ์•ก ๋งŒํผ์˜ ๋กœ๋˜ ๋ฒˆํ˜ธ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. - ์ค‘๋ณต๋˜์ง€ ์•Š๋Š” 1~45 ๋ฒ”์œ„์˜ ์ˆซ์ž๋ฅผ 6๊ฐœ์™€ ๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ๋ฅผ ๋ฝ‘๋Š”๋‹ค. -- [ ] ๋‹น์ฒจ ๋ฒˆํ˜ธ์™€ ๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค. +- [x] ๋‹น์ฒจ ๋ฒˆํ˜ธ์™€ ๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค. - [ ] ๋‹น์ฒจ ๋‚ด์—ญ์„ ์ถœ๋ ฅํ•œ๋‹ค. - 1๋“ฑ: 6๊ฐœ ๋ฒˆํ˜ธ ์ผ์น˜ / 2,000,000,000์› - 2๋“ฑ: 5๊ฐœ ๋ฒˆํ˜ธ + ๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ ์ผ์น˜ / 30,000,000์› @@ -14,5 +14,5 @@ - [ ] ์ˆ˜์ต๋ฅ ์„ ์ถœ๋ ฅํ•œ๋‹ค. - ์˜ˆ์™ธ์ฒ˜๋ฆฌ - [x] ๋กœ๋˜ ๊ตฌ์ž… ๊ธˆ์•ก์€ 1000์œผ๋กœ ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง€๋Š” ์–‘์˜ ์ •์ˆ˜์ด๋‹ค. - - [ ] ์ž…๋ ฅ๋ฐ›์€ ๋‹น์ฒจ๋ฒˆํ˜ธ์™€ ๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ์˜ ๋ฒ”์œ„๋Š” 1~45์ด๋‹ค. - - [ ] ์ž…๋ ฅ๋ฐ›์€ ๋‹น์ฒจ๋ฒˆํ˜ธ์™€ ๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ๋Š” ๊ฒน์น˜์ง€ ์•Š๋Š”๋‹ค. \ No newline at end of file + - [x] ์ž…๋ ฅ๋ฐ›์€ ๋‹น์ฒจ๋ฒˆํ˜ธ์™€ ๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ์˜ ๋ฒ”์œ„๋Š” 1~45์ด๋‹ค. + - [x] ์ž…๋ ฅ๋ฐ›์€ ๋‹น์ฒจ๋ฒˆํ˜ธ์™€ ๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ๋Š” ๊ฒน์น˜์ง€ ์•Š๋Š”๋‹ค. \ No newline at end of file diff --git a/src/App.js b/src/App.js index 0df05af51..43aec30f9 100644 --- a/src/App.js +++ b/src/App.js @@ -1,10 +1,11 @@ -import { getLottoNumbers, offerLottoSheet } from "./LottoMachine.js"; -import { getMoney } from "./View.js"; +// import { createLottoSheet } from "./LottoMachine.js"; +import { getLottoNumber, getMoney } from "./View.js"; class App { async run() { - const lottoQuantity = await getMoney(); - offerLottoSheet(lottoQuantity); + const lottoQuantity = await getMoney(); + const userLotto = createLottoSheet(lottoQuantity); + const winningNumbers = getLottoNumber(); } } diff --git a/src/LottoMachine.js b/src/LottoMachine.js index 4c7996df8..fc5c5edc6 100644 --- a/src/LottoMachine.js +++ b/src/LottoMachine.js @@ -1,4 +1,4 @@ -export const getLottoNumbers = () => { +export const createLottoNumbers = () => { let numberSet = new Set(); while(numberSet.size < 6){ let number = Math.floor(Math.random() * 45) + 1 @@ -7,7 +7,7 @@ export const getLottoNumbers = () => { return [...numberSet].sort((a, b) => a - b); } -export const getBonusNumber = (numbers) => { +export const createBonusNumber = (numbers) => { let number = Math.floor(Math.random() * 45) + 1; console.log(numbers); console.log(number); diff --git a/src/View.js b/src/View.js index 8db355c79..9a28aec0b 100644 --- a/src/View.js +++ b/src/View.js @@ -1,4 +1,6 @@ import { MissionUtils } from "@woowacourse/mission-utils"; +import { checkValidation } from "./utils"; +import Lotto from "./Lotto"; export const getMoney = async () => { const money = await MissionUtils.Console.readLineAsync("๊ตฌ์ž…๊ธˆ์•ก์„ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”\n"); @@ -13,6 +15,21 @@ export const getMoney = async () => { if(money%1000 !== 0){ throw new Error("[ERROR] 1000์› ๋‹จ์œ„๋กœ ๊ตฌ๋งค๊ฐ€ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค."); } - + return money/1000; +} + +export const getLottoNumber = async () => { + const inputNumber = await MissionUtils.Console.readLineAsync("๋‹น์ฒจ ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.\n"); + const winningNumber = inputNumber.split(',').map((number) => +(number.trim())); + try { + const lotto = new Lotto(winningNumber); + + let bonusNumber = await MissionUtils.Console.readLineAsync("๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.\n"); + bonusNumber = +bonusNumber.trim(); + checkValidation(bonusNumber, lotto); + return [lotto, bonusNumber]; + } catch (error) { + console.error(error.message); + } } \ No newline at end of file diff --git a/src/utils.js b/src/utils.js new file mode 100644 index 000000000..87dc1c851 --- /dev/null +++ b/src/utils.js @@ -0,0 +1,8 @@ +export const checkValidation = (number, numberSet) => { + if(number < 1 || number > 45){ + throw new Error("[ERROR] ๋กœ๋˜ ๋ฒˆํ˜ธ๋Š” 1๋ถ€ํ„ฐ 45 ์‚ฌ์ด์˜ ์ˆซ์ž์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค."); + } + if(numberSet.has(number)){ + throw new Error("[ERROR] ๋ณด๋„ˆ์Šค ์ˆซ์ž๋Š” ๋กœ๋˜ ๋ฒˆํ˜ธ์™€ ์ค‘๋ณต๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค."); + } +} \ No newline at end of file From 71db57c7c029d981f9324783b20250c2d9505af9 Mon Sep 17 00:00:00 2001 From: Euiyeong <105384780+snsdl0905@users.noreply.github.com> Date: Mon, 4 Nov 2024 22:55:01 +0900 Subject: [PATCH 5/6] =?UTF-8?q?feat:=20=EB=8B=B9=EC=B2=A8=20=EB=82=B4?= =?UTF-8?q?=EC=97=AD=EC=9D=84=20=EC=B6=9C=EB=A0=A5=ED=95=9C=EB=8B=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- src/App.js | 14 +++++++--- src/Lotto.js | 1 - src/LottoCalculator.js | 17 ++++++++++++ src/LottoMachine.js | 18 +++---------- src/Validator.js | 8 ++++++ src/View.js | 61 ++++++++++++++++++++++++++++++++++-------- src/utils.js | 8 ------ 8 files changed, 89 insertions(+), 40 deletions(-) create mode 100644 src/LottoCalculator.js create mode 100644 src/Validator.js delete mode 100644 src/utils.js diff --git a/README.md b/README.md index e402ed54b..ff578372d 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ - [x] ๊ตฌ์ž… ๊ธˆ์•ก ๋งŒํผ์˜ ๋กœ๋˜ ๋ฒˆํ˜ธ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. - ์ค‘๋ณต๋˜์ง€ ์•Š๋Š” 1~45 ๋ฒ”์œ„์˜ ์ˆซ์ž๋ฅผ 6๊ฐœ์™€ ๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ๋ฅผ ๋ฝ‘๋Š”๋‹ค. - [x] ๋‹น์ฒจ ๋ฒˆํ˜ธ์™€ ๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค. -- [ ] ๋‹น์ฒจ ๋‚ด์—ญ์„ ์ถœ๋ ฅํ•œ๋‹ค. +- [x] ๋‹น์ฒจ ๋‚ด์—ญ์„ ์ถœ๋ ฅํ•œ๋‹ค. - 1๋“ฑ: 6๊ฐœ ๋ฒˆํ˜ธ ์ผ์น˜ / 2,000,000,000์› - 2๋“ฑ: 5๊ฐœ ๋ฒˆํ˜ธ + ๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ ์ผ์น˜ / 30,000,000์› - 3๋“ฑ: 5๊ฐœ ๋ฒˆํ˜ธ ์ผ์น˜ / 1,500,000์› diff --git a/src/App.js b/src/App.js index 43aec30f9..157a883cc 100644 --- a/src/App.js +++ b/src/App.js @@ -1,11 +1,17 @@ -// import { createLottoSheet } from "./LottoMachine.js"; -import { getLottoNumber, getMoney } from "./View.js"; +import { offerLottoSheet } from "./LottoMachine.js"; +import { calculateWinning } from "./LottoCalculator.js"; +import { getBonusNumber, getLottoNumber, getMoney, printLottoNumbers, printResult } from "./View.js"; class App { async run() { const lottoQuantity = await getMoney(); - const userLotto = createLottoSheet(lottoQuantity); - const winningNumbers = getLottoNumber(); + const userLotto = await offerLottoSheet(lottoQuantity); + await printLottoNumbers(lottoQuantity, userLotto); + const winningNumbers = await getLottoNumber(); + const bonusNumber = await getBonusNumber(winningNumbers); + let winningArray = [winningNumbers, bonusNumber]; + const winningResult = await calculateWinning(winningArray, userLotto); + await printResult(winningResult); } } diff --git a/src/Lotto.js b/src/Lotto.js index 2ed9f622b..0bb78b931 100644 --- a/src/Lotto.js +++ b/src/Lotto.js @@ -21,7 +21,6 @@ class Lotto { throw new Error("[ERROR] ๋กœ๋˜ ๋ฒˆํ˜ธ๋Š” ์ค‘๋ณต๋˜์ง€ ์•Š๋Š” 6๊ฐœ์˜ ์ˆซ์ž์ž…๋‹ˆ๋‹ค."); } } - } export default Lotto; diff --git a/src/LottoCalculator.js b/src/LottoCalculator.js new file mode 100644 index 000000000..4005dc649 --- /dev/null +++ b/src/LottoCalculator.js @@ -0,0 +1,17 @@ +export const calculateWinning = (winningNumber, userNumbers) => { + const [winningArray, bonus] = winningNumber; + + const prizeCounts = { 3: 0, 4: 0, 5: 0, '5+bonus': 0, 6: 0 }; + + userNumbers.forEach(numbers => { + const matchingCount = numbers.filter(number => winningArray.includes(number)).length; + + if (matchingCount === 5 && numbers.includes(bonus)) { + prizeCounts['5+bonus'] += 1; + } else if (matchingCount >= 3) { + prizeCounts[matchingCount] += 1; + } + }); + + return prizeCounts; +} diff --git a/src/LottoMachine.js b/src/LottoMachine.js index fc5c5edc6..80d92f44a 100644 --- a/src/LottoMachine.js +++ b/src/LottoMachine.js @@ -7,24 +7,12 @@ export const createLottoNumbers = () => { return [...numberSet].sort((a, b) => a - b); } -export const createBonusNumber = (numbers) => { - let number = Math.floor(Math.random() * 45) + 1; - console.log(numbers); - console.log(number); - - while(numbers.has(number)){ - number = Math.floor(Math.random() * 45) + 1; - } - return number; -} - export const offerLottoSheet = (quantity) => { let sheet = []; for(let i=0; i { + if(number < 1 || number > 45){ + throw new Error("[ERROR] ๋กœ๋˜ ๋ฒˆํ˜ธ๋Š” 1๋ถ€ํ„ฐ 45 ์‚ฌ์ด์˜ ์ˆซ์ž์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค."); + } + if(numberSet.has(number)){ + throw new Error("[ERROR] ๋ณด๋„ˆ์Šค ์ˆซ์ž๋Š” ๋กœ๋˜ ๋ฒˆํ˜ธ์™€ ์ค‘๋ณต๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค."); + } +} \ No newline at end of file diff --git a/src/View.js b/src/View.js index 9a28aec0b..7a4a0f1ab 100644 --- a/src/View.js +++ b/src/View.js @@ -1,6 +1,6 @@ import { MissionUtils } from "@woowacourse/mission-utils"; -import { checkValidation } from "./utils"; -import Lotto from "./Lotto"; +import { checkValidation } from "./Validator.js"; +import Lotto from "./Lotto.js"; export const getMoney = async () => { const money = await MissionUtils.Console.readLineAsync("๊ตฌ์ž…๊ธˆ์•ก์„ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”\n"); @@ -20,16 +20,55 @@ export const getMoney = async () => { } export const getLottoNumber = async () => { - const inputNumber = await MissionUtils.Console.readLineAsync("๋‹น์ฒจ ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.\n"); - const winningNumber = inputNumber.split(',').map((number) => +(number.trim())); try { - const lotto = new Lotto(winningNumber); + const inputNumber = await MissionUtils.Console.readLineAsync("๋‹น์ฒจ ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.\n"); + const winningNumber = inputNumber.split(',').map((number) => +(number.trim())); - let bonusNumber = await MissionUtils.Console.readLineAsync("๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.\n"); - bonusNumber = +bonusNumber.trim(); - checkValidation(bonusNumber, lotto); - return [lotto, bonusNumber]; + new Lotto(winningNumber); + + let lottoArray = winningNumber.sort((a, b) => a - b); + + return lottoArray; } catch (error) { - console.error(error.message); + throw new Error("[ERROR] ์ •ํ™•ํ•œ ๋กœ๋˜ ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”."); } -} \ No newline at end of file +} + +export const getBonusNumber = async(numbers) => { + let bonusNumber = await MissionUtils.Console.readLineAsync("๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.\n"); + bonusNumber = +bonusNumber.trim(); + + checkValidation(bonusNumber, new Set(numbers)); + return bonusNumber; +} + +export const printLottoNumbers = (number, lottoNumbers) => { + MissionUtils.Console.print(`${number}๊ฐœ๋ฅผ ๊ตฌ๋งคํ–ˆ์Šต๋‹ˆ๋‹ค.`); + MissionUtils.Console.print(lottoNumbers); +} + +export const printResult = async (result) => { + const prizeTable = { + 3: "5,000", + 4: "50,000", + 5: "1,500,000", + "5+bonus": "30,000,000", + 6: "2,000,000,000" + }; + + MissionUtils.Console.print(` + ๋‹น์ฒจ ํ†ต๊ณ„ + --- + `); + Object.entries(result).forEach(([key, count]) => { + const prize = prizeTable[key]; + let bonus = ''; + if(key === '5+bonus'){ + key = 5; + bonus = '๋ณด๋„ˆ์Šค ๋ณผ ์ผ์น˜' + } + MissionUtils.Console.print(`${key}๊ฐœ ์ผ์น˜, ${bonus} (${prize}์›) - ${count}๊ฐœ`); + }); + +} + diff --git a/src/utils.js b/src/utils.js deleted file mode 100644 index 87dc1c851..000000000 --- a/src/utils.js +++ /dev/null @@ -1,8 +0,0 @@ -export const checkValidation = (number, numberSet) => { - if(number < 1 || number > 45){ - throw new Error("[ERROR] ๋กœ๋˜ ๋ฒˆํ˜ธ๋Š” 1๋ถ€ํ„ฐ 45 ์‚ฌ์ด์˜ ์ˆซ์ž์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค."); - } - if(numberSet.has(number)){ - throw new Error("[ERROR] ๋ณด๋„ˆ์Šค ์ˆซ์ž๋Š” ๋กœ๋˜ ๋ฒˆํ˜ธ์™€ ์ค‘๋ณต๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค."); - } -} \ No newline at end of file From b9ed6060e247306afa4daf0a22607a8e8473ad24 Mon Sep 17 00:00:00 2001 From: Euiyeong <105384780+snsdl0905@users.noreply.github.com> Date: Mon, 4 Nov 2024 23:45:32 +0900 Subject: [PATCH 6/6] =?UTF-8?q?feat:=20=EC=88=98=EC=9D=B5=EB=A5=A0?= =?UTF-8?q?=EC=9D=84=20=EC=B6=9C=EB=A0=A5=ED=95=9C=EB=8B=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- src/App.js | 7 +++++-- src/Constant.js | 7 +++++++ src/LottoCalculator.js | 20 ++++++++++++++++++++ src/LottoMachine.js | 8 +++----- src/View.js | 25 ++++++++++++------------- 6 files changed, 48 insertions(+), 21 deletions(-) create mode 100644 src/Constant.js diff --git a/README.md b/README.md index ff578372d..975fb4f27 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ - 3๋“ฑ: 5๊ฐœ ๋ฒˆํ˜ธ ์ผ์น˜ / 1,500,000์› - 4๋“ฑ: 4๊ฐœ ๋ฒˆํ˜ธ ์ผ์น˜ / 50,000์› - 5๋“ฑ: 3๊ฐœ ๋ฒˆํ˜ธ ์ผ์น˜ / 5,000์› -- [ ] ์ˆ˜์ต๋ฅ ์„ ์ถœ๋ ฅํ•œ๋‹ค. +- [x] ์ˆ˜์ต๋ฅ ์„ ์ถœ๋ ฅํ•œ๋‹ค. - ์˜ˆ์™ธ์ฒ˜๋ฆฌ - [x] ๋กœ๋˜ ๊ตฌ์ž… ๊ธˆ์•ก์€ 1000์œผ๋กœ ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง€๋Š” ์–‘์˜ ์ •์ˆ˜์ด๋‹ค. - [x] ์ž…๋ ฅ๋ฐ›์€ ๋‹น์ฒจ๋ฒˆํ˜ธ์™€ ๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ์˜ ๋ฒ”์œ„๋Š” 1~45์ด๋‹ค. diff --git a/src/App.js b/src/App.js index 157a883cc..bb04063a3 100644 --- a/src/App.js +++ b/src/App.js @@ -1,6 +1,6 @@ import { offerLottoSheet } from "./LottoMachine.js"; -import { calculateWinning } from "./LottoCalculator.js"; -import { getBonusNumber, getLottoNumber, getMoney, printLottoNumbers, printResult } from "./View.js"; +import { calculateProfit, calculateWinning, getWinningPrice } from "./LottoCalculator.js"; +import { getBonusNumber, getLottoNumber, getMoney, printLottoNumbers, printProfit, printResult } from "./View.js"; class App { async run() { @@ -12,6 +12,9 @@ class App { let winningArray = [winningNumbers, bonusNumber]; const winningResult = await calculateWinning(winningArray, userLotto); await printResult(winningResult); + const winningPrice = await getWinningPrice(winningResult); + const profit = await calculateProfit(lottoQuantity*1000, winningPrice); + await printProfit(profit); } } diff --git a/src/Constant.js b/src/Constant.js new file mode 100644 index 000000000..d80a2c89d --- /dev/null +++ b/src/Constant.js @@ -0,0 +1,7 @@ +export const PRIZE_TABLE = { + 3: "5,000", + 4: "50,000", + 5: "1,500,000", + "5+bonus": "30,000,000", + 6: "2,000,000,000" +}; \ No newline at end of file diff --git a/src/LottoCalculator.js b/src/LottoCalculator.js index 4005dc649..14ab73355 100644 --- a/src/LottoCalculator.js +++ b/src/LottoCalculator.js @@ -1,3 +1,5 @@ +import { PRIZE_TABLE } from "./Constant.js"; + export const calculateWinning = (winningNumber, userNumbers) => { const [winningArray, bonus] = winningNumber; @@ -15,3 +17,21 @@ export const calculateWinning = (winningNumber, userNumbers) => { return prizeCounts; } + +export const getWinningPrice = (result) => { + let totalPrize = 0; + for (const [key, count] of Object.entries(result)) { + if (count > 0) { + const prize = parseInt(PRIZE_TABLE[key].replace(/,/g, ''), 10); + totalPrize += prize * count; + } + } + + return totalPrize; +} + +export const calculateProfit = (price, winningPrice) => { + const profit = winningPrice/price * 100; + + return profit.toFixed(1); +} \ No newline at end of file diff --git a/src/LottoMachine.js b/src/LottoMachine.js index 80d92f44a..d0b6533a7 100644 --- a/src/LottoMachine.js +++ b/src/LottoMachine.js @@ -1,9 +1,7 @@ +import { MissionUtils } from "@woowacourse/mission-utils"; + export const createLottoNumbers = () => { - let numberSet = new Set(); - while(numberSet.size < 6){ - let number = Math.floor(Math.random() * 45) + 1 - numberSet.add(number); - } + let numberSet = MissionUtils.Random.pickUniqueNumbersInRange(1, 45, 6); return [...numberSet].sort((a, b) => a - b); } diff --git a/src/View.js b/src/View.js index 7a4a0f1ab..125fbe8ff 100644 --- a/src/View.js +++ b/src/View.js @@ -1,6 +1,7 @@ import { MissionUtils } from "@woowacourse/mission-utils"; import { checkValidation } from "./Validator.js"; import Lotto from "./Lotto.js"; +import { PRIZE_TABLE } from "./Constant.js"; export const getMoney = async () => { const money = await MissionUtils.Console.readLineAsync("๊ตฌ์ž…๊ธˆ์•ก์„ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”\n"); @@ -44,31 +45,29 @@ export const getBonusNumber = async(numbers) => { export const printLottoNumbers = (number, lottoNumbers) => { MissionUtils.Console.print(`${number}๊ฐœ๋ฅผ ๊ตฌ๋งคํ–ˆ์Šต๋‹ˆ๋‹ค.`); - MissionUtils.Console.print(lottoNumbers); + lottoNumbers.forEach((lottoNumber) => { + const formattedLottoNumbers = `[${lottoNumber.join(', ')}]`; + MissionUtils.Console.print(formattedLottoNumbers); + }) } export const printResult = async (result) => { - const prizeTable = { - 3: "5,000", - 4: "50,000", - 5: "1,500,000", - "5+bonus": "30,000,000", - 6: "2,000,000,000" - }; - MissionUtils.Console.print(` ๋‹น์ฒจ ํ†ต๊ณ„ --- `); Object.entries(result).forEach(([key, count]) => { - const prize = prizeTable[key]; + const prize = PRIZE_TABLE[key]; let bonus = ''; if(key === '5+bonus'){ - key = 5; - bonus = '๋ณด๋„ˆ์Šค ๋ณผ ์ผ์น˜' + MissionUtils.Console.print(`5๊ฐœ ์ผ์น˜, ๋ณด๋„ˆ์Šค ๋ณผ ์ผ์น˜ (${prize}์›) - ${count}๊ฐœ`); } - MissionUtils.Console.print(`${key}๊ฐœ ์ผ์น˜, ${bonus} (${prize}์›) - ${count}๊ฐœ`); + else { MissionUtils.Console.print(`${key}๊ฐœ ์ผ์น˜ (${prize}์›) - ${count}๊ฐœ`); } }); } +export const printProfit = (profit) => { + MissionUtils.Console.print(`์ด ์ˆ˜์ต๋ฅ ์€ ${profit}%์ž…๋‹ˆ๋‹ค.`) + +} \ No newline at end of file