From e0e8d35be27089a82d7aafd657752b1e10e3db36 Mon Sep 17 00:00:00 2001 From: yuni Date: Fri, 21 Feb 2025 17:14:48 +0900 Subject: [PATCH 01/11] =?UTF-8?q?=F0=9F=93=9D=20Updeat:README.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Update README.md --- README.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/README.md b/README.md index 15bb106..f8c19cc 100644 --- a/README.md +++ b/README.md @@ -1 +1,28 @@ # javascript-lotto-precourse +✅ 입출력 +- 로또 구입 금액 입력(1000단위) +- 당첨 번호 입력 +- 보너스 번호 입력 +- 발행한 로또 개수 출력 +- 결과 출력 + +✅ 전처리 +- 당첨 번호 쉼표 기준 구분(앞 뒤로 공백제거, 숫자로 변환) +- 발행한 로또 개수 구하기 + +✅ 1등 ~ 5등 검사 +- (로또 개수만큼) 중복되지 않는 숫자 6개 뽑기, 오름차순 정렬 +- 1등 조건 검사, 개수 카운트 +- 2등 조건 검사, 개수 카운트 +- 3등 조건 검사, 개수 카운트 +- 4등 조건 검사, 개수 카운트 +- 5등 조건 검사, 개수 카운트 + +✅ 수익률 계산 +- 수익률 계산 (벌어들인 금액/구매 금액), 소수점 둘째 자리 반올림 + +✅ 예외처리 +- 입력 오류 : 입력 금액이 1000으로 나누어 떨어지지 않는 경우 +- 입력 오류 : 숫자가 아니거나, 쉼표가 아닌 숫자 +- 입력 오류 : 숫자가 중복되는 경우 +- 입력 오류 : 로또 번호가 범위를 벗어나는 경우 \ No newline at end of file From 630ba05e8193b444619963124d0257778d7d4356 Mon Sep 17 00:00:00 2001 From: yuni Date: Fri, 21 Feb 2025 17:17:02 +0900 Subject: [PATCH 02/11] =?UTF-8?q?=E2=9C=A8=20Feat:=EC=9E=85=EC=B6=9C?= =?UTF-8?q?=EB=A0=A5=20=EA=B8=B0=EB=8A=A5=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 로또 구입 금액, 당첨 번호, 보너스 번호를 입력할 수 있는 기능을 추가하였다. --- src/App.js | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/App.js b/src/App.js index 091aa0a..d550cb0 100644 --- a/src/App.js +++ b/src/App.js @@ -1,5 +1,18 @@ +import { Console } from "@woowacourse/mission-utils"; + class App { - async run() {} + async run() { + //구입금액 입력 + const inputMoney = await Console.readLineAsync('구입금액을 입력해 주세요.'); + const inputNumbers = await Console.readLineAsync('당첨 번호를 입력해 주세요.'); + const inputBonus = await Console.readLineAsync('보너스 번호를 입력해 주세요.'); + + //출력 + Console.print(inputMoney); + Console.print(inputNumbers); + Console.print(inputBonus); + + } } export default App; From 64333ddc4ed57b7f1effb9aff058064900097ee4 Mon Sep 17 00:00:00 2001 From: yuni Date: Fri, 21 Feb 2025 18:38:31 +0900 Subject: [PATCH 03/11] =?UTF-8?q?=F0=9F=93=9D=20Update:=20README.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 클래스 설계, 코드 흐름을 추가하였다. --- README.md | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index f8c19cc..c473c39 100644 --- a/README.md +++ b/README.md @@ -25,4 +25,25 @@ - 입력 오류 : 입력 금액이 1000으로 나누어 떨어지지 않는 경우 - 입력 오류 : 숫자가 아니거나, 쉼표가 아닌 숫자 - 입력 오류 : 숫자가 중복되는 경우 -- 입력 오류 : 로또 번호가 범위를 벗어나는 경우 \ No newline at end of file +- 입력 오류 : 로또 번호가 범위를 벗어나는 경우 + +# 클래스 설계 +- Lotto 클래스 : 입력된 로또 번호 검증 및 관리 +- LottoGame 클래스 : 게임진행, 로또발행, 당첨내역, 예외처리 +- App 클래스 : 입출력 담당 + +# 코드 흐름 +- App 로또 금액 입력받기 + -> LottoGame 로또 금액만큼 로또 발행 요청 + -> Lotto 로또 발행 (외부에서 수정 불가) + -> LottoGame 발행된 로또 저장 + -> App 로또출력 + +- App 로또 번호, 보너스 입력받기 + -> LottoGame 로또 번호 전처리(,제거) + -> Lotto 유저 로또 검증, 발행 + -> LottoGame 유저 로또 저장 + -> LottoGame 로또 번호 당첨 여부 카운트 + -> App 당첨여부출력 + +# MVC 구조 From 9dfe302d632bdcec4efaf1658ec3b402aebdd2e7 Mon Sep 17 00:00:00 2001 From: yuni Date: Fri, 21 Feb 2025 19:04:43 +0900 Subject: [PATCH 04/11] =?UTF-8?q?=E2=9C=A8=20Feat:=20=EB=A1=9C=EB=98=90=20?= =?UTF-8?q?=EB=B0=9C=ED=96=89=20=EA=B8=B0=EB=8A=A5=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 랜덤으로 로또를 발행해 저장 및 출력하는 기능을 추가하였다. --- src/App.js | 5 +++++ src/Lotto.js | 9 +++++++-- src/LottoGame.js | 19 +++++++++++++++++++ 3 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 src/LottoGame.js diff --git a/src/App.js b/src/App.js index d550cb0..d9e3fd5 100644 --- a/src/App.js +++ b/src/App.js @@ -1,9 +1,14 @@ import { Console } from "@woowacourse/mission-utils"; +import LottoGame from "../src/LottoGame.js"; class App { async run() { + const lottoGame = new LottoGame(); + //구입금액 입력 const inputMoney = await Console.readLineAsync('구입금액을 입력해 주세요.'); + const moneyNum = inputMoney/1000; + Console.print(lottoGame.creatLotto(moneyNum)); const inputNumbers = await Console.readLineAsync('당첨 번호를 입력해 주세요.'); const inputBonus = await Console.readLineAsync('보너스 번호를 입력해 주세요.'); diff --git a/src/Lotto.js b/src/Lotto.js index cb0b152..6471e32 100644 --- a/src/Lotto.js +++ b/src/Lotto.js @@ -1,5 +1,8 @@ +import { Random } from "@woowacourse/mission-utils"; + +//로또 번호 저장, 검증, 반환 class Lotto { - #numbers; + #numbers; //받은 로또 번호리스트 constructor(numbers) { this.#validate(numbers); @@ -12,7 +15,9 @@ class Lotto { } } - // TODO: 추가 기능 구현 + getNumbers(){ + return this.#numbers + } } export default Lotto; diff --git a/src/LottoGame.js b/src/LottoGame.js new file mode 100644 index 0000000..a5ff4ca --- /dev/null +++ b/src/LottoGame.js @@ -0,0 +1,19 @@ +import { Console, Random } from "@woowacourse/mission-utils"; +import Lotto from "../src/Lotto.js"; + +class LottoGame{ + constructor(){ + this.lottoList = [] + } + + //로또 발행 + creatLotto(num){ + for(let i = 0; i < num; i++){ + let numbers = Random.pickUniqueNumbersInRange(1, 45, 6); + this.lottoList.push(new Lotto(numbers).getNumbers()); + } + return this.lottoList + } +} + +export default LottoGame; \ No newline at end of file From 1111dc59f39809c4fe75e7c49444362fa01326da Mon Sep 17 00:00:00 2001 From: yuni Date: Fri, 21 Feb 2025 19:44:25 +0900 Subject: [PATCH 05/11] =?UTF-8?q?=E2=9C=A8=20Feat:=EC=A0=84=EC=B2=98?= =?UTF-8?q?=EB=A6=AC=20=EA=B8=B0=EB=8A=A5=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 당첨 번호를 쉼표 기준으로 구분하고, 발행한 로또 개수를 구하는 기능을 추가하였다. --- src/App.js | 16 +++++++++------- src/LottoGame.js | 22 ++++++++++++++++++---- 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/src/App.js b/src/App.js index d9e3fd5..ce2abfc 100644 --- a/src/App.js +++ b/src/App.js @@ -8,15 +8,17 @@ class App { //구입금액 입력 const inputMoney = await Console.readLineAsync('구입금액을 입력해 주세요.'); const moneyNum = inputMoney/1000; - Console.print(lottoGame.creatLotto(moneyNum)); - const inputNumbers = await Console.readLineAsync('당첨 번호를 입력해 주세요.'); - const inputBonus = await Console.readLineAsync('보너스 번호를 입력해 주세요.'); + Console.print(`${moneyNum}개를 구매했습니다.`); + for (const lotto in lottoGame.creatLotto(moneyNum)) { + Console.print(lottoGame.creatLotto(moneyNum)[lotto]); + } - //출력 - Console.print(inputMoney); - Console.print(inputNumbers); - Console.print(inputBonus); + //당첨번호 입력 + const inputNumbers = await Console.readLineAsync('당첨 번호를 입력해 주세요.'); + const userLotto = lottoGame.preprocessUserLotto(inputNumbers); + Console.print(userLotto); + const inputBonus = await Console.readLineAsync('보너스 번호를 입력해 주세요.'); } } diff --git a/src/LottoGame.js b/src/LottoGame.js index a5ff4ca..613beca 100644 --- a/src/LottoGame.js +++ b/src/LottoGame.js @@ -3,16 +3,30 @@ import Lotto from "../src/Lotto.js"; class LottoGame{ constructor(){ - this.lottoList = [] + this.winningLottos = [] } //로또 발행 creatLotto(num){ for(let i = 0; i < num; i++){ - let numbers = Random.pickUniqueNumbersInRange(1, 45, 6); - this.lottoList.push(new Lotto(numbers).getNumbers()); + let numbers = Random.pickUniqueNumbersInRange(1, 45, 6); //중복되지 않는 숫자 6개 + this.winningLottos.push(new Lotto(numbers).getNumbers()); } - return this.lottoList + this.winningLottos.sort((a,b) => a - b); + return this.winningLottos + } + + // userLotto 전처리 + preprocessUserLotto(userNumbers) { + return userNumbers.split(",").map(num => num.trim()).map(num => Number(num)); + } + + //로또 당첨조건 검사 + checkLottoWinning(userLotto, winningLottos, bonusNumber) { + + // 일치하는 번호 개수 구하기 + + // 보너스 번호와 일치 여부 확인 } } From 4a0e378f642e9439655021e75e4bd631f446e11e Mon Sep 17 00:00:00 2001 From: yuni Date: Fri, 21 Feb 2025 21:57:12 +0900 Subject: [PATCH 06/11] =?UTF-8?q?=E2=9C=A8=20Feat:=20=EB=8B=B9=EC=B2=A8=20?= =?UTF-8?q?=ED=9A=9F=EC=88=98=20=EA=B2=80=EC=82=AC=20=EA=B8=B0=EB=8A=A5=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 일치하는 숫자를 검사하고 숫자를 카운트하는 기능을 추가하였다. --- src/App.js | 16 ++++++++++++++-- src/LottoGame.js | 42 ++++++++++++++++++++++++++++++++++++------ 2 files changed, 50 insertions(+), 8 deletions(-) diff --git a/src/App.js b/src/App.js index ce2abfc..50a29ec 100644 --- a/src/App.js +++ b/src/App.js @@ -9,8 +9,9 @@ class App { const inputMoney = await Console.readLineAsync('구입금액을 입력해 주세요.'); const moneyNum = inputMoney/1000; Console.print(`${moneyNum}개를 구매했습니다.`); - for (const lotto in lottoGame.creatLotto(moneyNum)) { - Console.print(lottoGame.creatLotto(moneyNum)[lotto]); + const winningLotto = lottoGame.creatLotto(moneyNum) + for (const lotto in winningLotto) { + Console.print(winningLotto[lotto]); } //당첨번호 입력 @@ -18,7 +19,18 @@ class App { const userLotto = lottoGame.preprocessUserLotto(inputNumbers); Console.print(userLotto); + //보너스번호 입력 const inputBonus = await Console.readLineAsync('보너스 번호를 입력해 주세요.'); + + //결과 출력 + const rank = lottoGame.returnRank(userLotto,winningLotto,inputBonus); + Console.print(`당첨 통계`); + Console.print(`---`); + Console.print(`3개 일치 (5,000원) - ${rank.fifth}개`); + Console.print(`4개 일치 (50,000원) - ${rank.fourth}개`); + Console.print(`5개 일치 (1,500,000원) - ${rank.second + rank.third}개`); + Console.print(`5개 일치, 보너스 볼 일치 (30,000,000원) - ${rank.third}개`); + Console.print(`6개 일치 (2,000,000,000원) - ${rank.first}개`); } } diff --git a/src/LottoGame.js b/src/LottoGame.js index 613beca..b00ee2e 100644 --- a/src/LottoGame.js +++ b/src/LottoGame.js @@ -4,6 +4,7 @@ import Lotto from "../src/Lotto.js"; class LottoGame{ constructor(){ this.winningLottos = [] + this.counts = {first : 0, second : 0, third: 0, fourth: 0, fifth: 0}; } //로또 발행 @@ -18,15 +19,44 @@ class LottoGame{ // userLotto 전처리 preprocessUserLotto(userNumbers) { - return userNumbers.split(",").map(num => num.trim()).map(num => Number(num)); + userNumbers = userNumbers.split(",").map(num => num.trim()).map(num => Number(num)); + const userLotto = new Lotto(userNumbers); + return userLotto.getNumbers().sort((a,b) => a - b); } - //로또 당첨조건 검사 - checkLottoWinning(userLotto, winningLottos, bonusNumber) { - - // 일치하는 번호 개수 구하기 + // 일치하는 숫자 개수 계산 + calculateMatchingCount(userLotto, winningLotto) { + return userLotto.filter(num => winningLotto.includes(num)).length; + } + + // 등수 판별 + getRank(matchingCount, bonusMatch) { + switch (matchingCount) { + case 6: return 'first'; + case 5: return bonusMatch ? 'third' : 'second'; + case 4: return 'fourth'; + case 3: return 'fifth'; + default: return null; + } + } + + // 등수 반환 + returnRank(userLotto, winningLottos, bonusNumber){ + let matchingCount = 0; + let bonusMatch = userLotto.includes(bonusNumber); + + for (let i = 0; i < winningLottos.length; i++) { + // 일치하는 번호 개수 계산 + matchingCount = this.calculateMatchingCount(userLotto, winningLottos[i]); + + // 등수 판별 + let rank = this.getRank(matchingCount, bonusMatch); + if (rank) { + this.counts[rank] += 1; + } + } - // 보너스 번호와 일치 여부 확인 + return this.counts; } } From 5f491f5ee96e07504cc8901d48db9e486c19caa7 Mon Sep 17 00:00:00 2001 From: yuni Date: Fri, 21 Feb 2025 22:04:41 +0900 Subject: [PATCH 07/11] =?UTF-8?q?=E2=9C=A8=20Feat:=20=EC=88=98=EC=9D=B5?= =?UTF-8?q?=EB=A5=A0=20=EA=B3=84=EC=82=B0=20=EA=B8=B0=EB=8A=A5=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 총 수익을 계산하고 투자 금액으로 나눠, 소수점 둘째 자리까지 반올림하는 기능을 추가하였다. --- src/App.js | 2 ++ src/LottoGame.js | 13 +++++++++++++ 2 files changed, 15 insertions(+) diff --git a/src/App.js b/src/App.js index 50a29ec..a13c889 100644 --- a/src/App.js +++ b/src/App.js @@ -31,6 +31,8 @@ class App { Console.print(`5개 일치 (1,500,000원) - ${rank.second + rank.third}개`); Console.print(`5개 일치, 보너스 볼 일치 (30,000,000원) - ${rank.third}개`); Console.print(`6개 일치 (2,000,000,000원) - ${rank.first}개`); + const earningRate = lottoGame.earningRate(inputMoney); + Console.print(`총 수익률은 ${earningRate}%입니다.`); } } diff --git a/src/LottoGame.js b/src/LottoGame.js index b00ee2e..d04e2b5 100644 --- a/src/LottoGame.js +++ b/src/LottoGame.js @@ -58,6 +58,19 @@ class LottoGame{ return this.counts; } + + //수익률 계산 (벌어들인 금액/구매 금액) + earningRate(money){ + const earnings = (this.counts.fifth * 5000) + + (this.counts.fourth * 50000) + + (this.counts.second * 1500000) + + (this.counts.third * 30000000) + + (this.counts.first * 2000000000); + + const earningsRate = earnings / money; + const roundedearningsRate = parseFloat(earningsRate.toFixed(2)); + return roundedearningsRate; + } } export default LottoGame; \ No newline at end of file From 49f64aa3a4848f2e08582861896ec43c4ed6984c Mon Sep 17 00:00:00 2001 From: yuni Date: Fri, 21 Feb 2025 22:06:41 +0900 Subject: [PATCH 08/11] =?UTF-8?q?=F0=9F=90=9B=20Fix:=EC=88=98=EC=9D=B5?= =?UTF-8?q?=EB=A5=A0=20=EA=B3=84=EC=82=B0=20=EA=B8=B0=EB=8A=A5=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/LottoGame.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/LottoGame.js b/src/LottoGame.js index d04e2b5..bbef49c 100644 --- a/src/LottoGame.js +++ b/src/LottoGame.js @@ -68,7 +68,7 @@ class LottoGame{ (this.counts.first * 2000000000); const earningsRate = earnings / money; - const roundedearningsRate = parseFloat(earningsRate.toFixed(2)); + const roundedearningsRate = parseFloat(earningsRate.toFixed(1)); return roundedearningsRate; } } From f76663f84dab6056f2fdaaf35aaeed2e3d0590f5 Mon Sep 17 00:00:00 2001 From: yuni Date: Fri, 21 Feb 2025 23:28:04 +0900 Subject: [PATCH 09/11] =?UTF-8?q?=E2=9C=A8=20=EC=98=88=EC=99=B8=EC=B2=98?= =?UTF-8?q?=EB=A6=AC=20=EA=B8=B0=EB=8A=A5=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 예외를 처리하는 기능을 추가하였다. --- src/App.js | 68 +++++++++++++++++++++++++++++------------------- src/LottoGame.js | 2 +- 2 files changed, 42 insertions(+), 28 deletions(-) diff --git a/src/App.js b/src/App.js index a13c889..d3560ba 100644 --- a/src/App.js +++ b/src/App.js @@ -3,36 +3,50 @@ import LottoGame from "../src/LottoGame.js"; class App { async run() { - const lottoGame = new LottoGame(); + try { + const lottoGame = new LottoGame(); - //구입금액 입력 - const inputMoney = await Console.readLineAsync('구입금액을 입력해 주세요.'); - const moneyNum = inputMoney/1000; - Console.print(`${moneyNum}개를 구매했습니다.`); - const winningLotto = lottoGame.creatLotto(moneyNum) - for (const lotto in winningLotto) { - Console.print(winningLotto[lotto]); - } - - //당첨번호 입력 - const inputNumbers = await Console.readLineAsync('당첨 번호를 입력해 주세요.'); - const userLotto = lottoGame.preprocessUserLotto(inputNumbers); - Console.print(userLotto); + //구입금액 입력 + const inputMoney = await Console.readLineAsync('구입금액을 입력해 주세요.'); + if (isNaN(inputMoney) || inputMoney % 1000 !== 0) { + throw new Error("[ERROR] IllegalArgumentException") + } + const moneyNum = inputMoney/1000; - //보너스번호 입력 - const inputBonus = await Console.readLineAsync('보너스 번호를 입력해 주세요.'); + //구입한 로또 출력 + Console.print(`${moneyNum}개를 구매했습니다.`); + const winningLotto = lottoGame.creatLotto(moneyNum) + for (let lotto in winningLotto){ + Console.print(`[${winningLotto[lotto].join(", ")}]`); + } + + //당첨번호 입력 + const inputNumbers = await Console.readLineAsync('당첨 번호를 입력해 주세요.'); + const userLotto = lottoGame.preprocessUserLotto(inputNumbers); + Console.print(userLotto); + + //보너스번호 입력 + const inputBonus = await Console.readLineAsync('보너스 번호를 입력해 주세요.'); + if (userLotto.includes(Number(inputBonus))){ + throw new Error("[Error] 보너스 번호는 입력하지 않은 값이어야 합니다."); + } + + //결과 출력 + const rank = lottoGame.returnRank(userLotto,winningLotto,inputBonus); + Console.print(`당첨 통계`); + Console.print(`---`); + Console.print(`3개 일치 (5,000원) - ${rank.fifth}개`); + Console.print(`4개 일치 (50,000원) - ${rank.fourth}개`); + Console.print(`5개 일치 (1,500,000원) - ${rank.third}개`); + Console.print(`5개 일치, 보너스 볼 일치 (30,000,000원) - ${rank.second}개`); + Console.print(`6개 일치 (2,000,000,000원) - ${rank.first}개`); + const earningRate = lottoGame.earningRate(inputMoney); + Console.print(`총 수익률은 ${earningRate}%입니다.`); - //결과 출력 - const rank = lottoGame.returnRank(userLotto,winningLotto,inputBonus); - Console.print(`당첨 통계`); - Console.print(`---`); - Console.print(`3개 일치 (5,000원) - ${rank.fifth}개`); - Console.print(`4개 일치 (50,000원) - ${rank.fourth}개`); - Console.print(`5개 일치 (1,500,000원) - ${rank.second + rank.third}개`); - Console.print(`5개 일치, 보너스 볼 일치 (30,000,000원) - ${rank.third}개`); - Console.print(`6개 일치 (2,000,000,000원) - ${rank.first}개`); - const earningRate = lottoGame.earningRate(inputMoney); - Console.print(`총 수익률은 ${earningRate}%입니다.`); + } catch (error){ + Console.print(error.message); + return Promise.reject(error); + } } } diff --git a/src/LottoGame.js b/src/LottoGame.js index bbef49c..e59efbd 100644 --- a/src/LottoGame.js +++ b/src/LottoGame.js @@ -67,7 +67,7 @@ class LottoGame{ (this.counts.third * 30000000) + (this.counts.first * 2000000000); - const earningsRate = earnings / money; + const earningsRate = (earnings / money) * 100 ; const roundedearningsRate = parseFloat(earningsRate.toFixed(1)); return roundedearningsRate; } From 197f7ef423e83b5f3c076b69fc992f863615e131 Mon Sep 17 00:00:00 2001 From: yuni Date: Sat, 22 Feb 2025 00:25:52 +0900 Subject: [PATCH 10/11] =?UTF-8?q?=E2=9C=A8=20Feat:=20=EC=9E=85=EB=A0=A5=20?= =?UTF-8?q?=EB=8B=A4=EC=8B=9C=20=EB=B0=9B=EB=8A=94=20=EA=B8=B0=EB=8A=A5=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 오류가 난 뒤 다시 입력받는 기능을 추가했습니다. --- src/App.js | 77 ++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 55 insertions(+), 22 deletions(-) diff --git a/src/App.js b/src/App.js index d3560ba..211c6eb 100644 --- a/src/App.js +++ b/src/App.js @@ -7,34 +7,26 @@ class App { const lottoGame = new LottoGame(); //구입금액 입력 - const inputMoney = await Console.readLineAsync('구입금액을 입력해 주세요.'); - if (isNaN(inputMoney) || inputMoney % 1000 !== 0) { - throw new Error("[ERROR] IllegalArgumentException") - } - const moneyNum = inputMoney/1000; + const inputMoney = await this.getInputMoney(); + const ticketCount = inputMoney / 1000; //구입한 로또 출력 - Console.print(`${moneyNum}개를 구매했습니다.`); - const winningLotto = lottoGame.creatLotto(moneyNum) - for (let lotto in winningLotto){ + Console.print(`${ticketCount}개를 구매했습니다.`); + const winningLotto = lottoGame.creatLotto(ticketCount); + for (let lotto in winningLotto) { Console.print(`[${winningLotto[lotto].join(", ")}]`); } - + //당첨번호 입력 - const inputNumbers = await Console.readLineAsync('당첨 번호를 입력해 주세요.'); - const userLotto = lottoGame.preprocessUserLotto(inputNumbers); - Console.print(userLotto); - + const userLotto = await this.getUserLotto(lottoGame); + //보너스번호 입력 - const inputBonus = await Console.readLineAsync('보너스 번호를 입력해 주세요.'); - if (userLotto.includes(Number(inputBonus))){ - throw new Error("[Error] 보너스 번호는 입력하지 않은 값이어야 합니다."); - } - + const inputBonus = await this.getBonusNumber(userLotto); + //결과 출력 - const rank = lottoGame.returnRank(userLotto,winningLotto,inputBonus); - Console.print(`당첨 통계`); - Console.print(`---`); + const rank = lottoGame.returnRank(userLotto, winningLotto, inputBonus); + Console.print("당첨 통계"); + Console.print("---"); Console.print(`3개 일치 (5,000원) - ${rank.fifth}개`); Console.print(`4개 일치 (50,000원) - ${rank.fourth}개`); Console.print(`5개 일치 (1,500,000원) - ${rank.third}개`); @@ -43,11 +35,52 @@ class App { const earningRate = lottoGame.earningRate(inputMoney); Console.print(`총 수익률은 ${earningRate}%입니다.`); - } catch (error){ + } catch (error) { Console.print(error.message); return Promise.reject(error); } } + + // 재귀적으로 구입금액 입력 받기 + async getInputMoney() { + try { + const inputMoney = await Console.readLineAsync('구입금액을 입력해 주세요.'); + const moneyNum = Number(inputMoney); + if (isNaN(moneyNum) || inputMoney % 1000 !== 0 || !Number.isInteger(moneyNum)) { + throw new Error("[ERROR] IllegalArgumentException"); + } + return inputMoney; + } catch (error) { + Console.print(error.message); + return this.getInputMoney(); // 재귀적으로 다시 호출 + } + } + + // 재귀적으로 당첨번호 입력 받기 + async getUserLotto(lottoGame) { + try { + const inputNumbers = await Console.readLineAsync('당첨 번호를 입력해 주세요.'); + const userLotto = lottoGame.preprocessUserLotto(inputNumbers); + return userLotto; + } catch (error) { + Console.print(error.message); + return this.getUserLotto(lottoGame); // 재귀적으로 다시 호출 + } + } + + // 재귀적으로 보너스 번호 입력 받기 + async getBonusNumber(userLotto) { + try { + const inputBonus = await Console.readLineAsync('보너스 번호를 입력해 주세요.'); + if (userLotto.includes(Number(inputBonus))||isNaN(inputBonus)) { + throw new Error("[ERROR] 보너스 번호는 입력하지 않은 숫자이어야 합니다."); + } + return inputBonus; + } catch (error) { + Console.print(error.message); + return this.getBonusNumber(userLotto); // 재귀적으로 다시 호출 + } + } } export default App; From 2bd7aeb7af54edfece04608be37d1dbdcfd5352b Mon Sep 17 00:00:00 2001 From: yuni Date: Sat, 22 Feb 2025 00:36:18 +0900 Subject: [PATCH 11/11] =?UTF-8?q?=F0=9F=93=9D=20Update:=20README.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit README.md 업데이트 --- README.md | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index c473c39..07ed1f2 100644 --- a/README.md +++ b/README.md @@ -1,31 +1,35 @@ # javascript-lotto-precourse ✅ 입출력 -- 로또 구입 금액 입력(1000단위) -- 당첨 번호 입력 -- 보너스 번호 입력 -- 발행한 로또 개수 출력 +- 로또 구입 금액 입력(1000단위) ✅ +- 당첨 번호 입력 ✅ +- 보너스 번호 입력 ✅ +- 발행한 로또 개수 출력 ✅ - 결과 출력 ✅ 전처리 -- 당첨 번호 쉼표 기준 구분(앞 뒤로 공백제거, 숫자로 변환) -- 발행한 로또 개수 구하기 +- 당첨 번호 쉼표 기준 구분(앞 뒤로 공백제거, 숫자로 변환) ✅ +- 발행한 로또 개수 구하기 ✅ ✅ 1등 ~ 5등 검사 -- (로또 개수만큼) 중복되지 않는 숫자 6개 뽑기, 오름차순 정렬 -- 1등 조건 검사, 개수 카운트 -- 2등 조건 검사, 개수 카운트 -- 3등 조건 검사, 개수 카운트 -- 4등 조건 검사, 개수 카운트 -- 5등 조건 검사, 개수 카운트 +- (로또 개수만큼) 중복되지 않는 숫자 6개 뽑기, 오름차순 정렬 ✅ +- 1등 조건 검사, 개수 카운트 ✅ +- 2등 조건 검사, 개수 카운트 ✅ +- 3등 조건 검사, 개수 카운트 ✅ +- 4등 조건 검사, 개수 카운트 ✅ +- 5등 조건 검사, 개수 카운트 ✅ ✅ 수익률 계산 -- 수익률 계산 (벌어들인 금액/구매 금액), 소수점 둘째 자리 반올림 +- 수익률 계산 (벌어들인 금액/구매 금액), 소수점 둘째 자리 반올림 ✅ ✅ 예외처리 -- 입력 오류 : 입력 금액이 1000으로 나누어 떨어지지 않는 경우 -- 입력 오류 : 숫자가 아니거나, 쉼표가 아닌 숫자 -- 입력 오류 : 숫자가 중복되는 경우 -- 입력 오류 : 로또 번호가 범위를 벗어나는 경우 +- 입력 오류 : 입력 금액이 1000으로 나누어 떨어지지 않는 경우 ✅ +- 입력 오류 : 로또 구매 금액이 숫자가 경우 ✅ +- 입력 오류 : 숫자가 아니거나, 쉼표가 아닌 것이 입력된 경우 ✅ +- 입력 오류 : 숫자가 중복되는 경우 ✅ +- 입력 오류 : 로또 번호가 범위를 벗어나는 경우 ✅ +- 입력 오류 : 로또 번호가 6개가 아닌 경우 ✅ +- 입력 오류 : 보너스 번호가 이미 입력한 번호와 중복되는 경우 ✅ +- 입력 오류 : 보너스 번호가 숫자가 아닌 경우 ✅ # 클래스 설계 - Lotto 클래스 : 입력된 로또 번호 검증 및 관리