From 93c45ffa1a6d072c277d5d305c2b5e3169f3f38e Mon Sep 17 00:00:00 2001 From: Jin-Chanyong Date: Mon, 4 Nov 2024 19:32:06 +0900 Subject: [PATCH 01/30] =?UTF-8?q?docs:=20README=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit README.md 파일에 이하 내용 추가: - 기능 요구 사항 - 기능 목록 단위 --- README.md | 48 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 15bb106b5..f6f92e855 100644 --- a/README.md +++ b/README.md @@ -1 +1,47 @@ -# javascript-lotto-precourse +# javascript-racingcar-precourse + +## 기능 요구 사항 + +- 로또 번호의 숫자 범위는 1~45까지이다. +- 1개의 로또를 발행할 때 중복되지 않는 6개의 숫자를 뽑는다. +- 당첨 번호 추첨 시 중복되지 않는 숫자 6개와 보너스 번호 1개를 뽑는다. +- 당첨은 1등부터 5등까지 있다. 당첨 기준과 금액은 아래와 같다. +- 1등: 6개 번호 일치 / 2,000,000,000원 +- 2등: 5개 번호 + 보너스 번호 일치 / 30,000,000원 +- 3등: 5개 번호 일치 / 1,500,000원 +- 4등: 4개 번호 일치 / 50,000원 +- 5등: 3개 번호 일치 / 5,000원 +- 로또 구입 금액을 입력하면 구입 금액에 해당하는 만큼 로또를 발행해야 한다. +- 로또 1장의 가격은 1,000원이다. +- 당첨 번호와 보너스 번호를 입력받는다. +- 사용자가 구매한 로또 번호와 당첨 번호를 비교하여 당첨 내역 및 수익률을 출력하고 로또 게임을 종료한다. +- 사용자가 잘못된 값을 입력할 경우 "[ERROR]"로 시작하는 메시지와 함께 Error를 발생시키고 해당 메시지를 출력한 다음 해당 지점부터 다시 입력을 받는다. + +## 기능 목록 단위 + +- [ ] 1. 로또 구매 로직 구현 + + - [ ] 1-1. 사용자로부터 로또 구입 금액 입력받기 + - [ ] 입력된 금액이 1000원 단위로 나누어 떨어지지 않거나, 잘못된 입력 시 다시 입력. + - [ ] 1-2. 구입 금액에 따라 구매할 로또 수량을 계산하기 + - [ ] 1-3. 구매한 로또 수량 만큼 로또 번호 생성하고 출력하기 + - [ ] 로또 번호는 오름차순으로 정렬. + +- [ ] 2. 당첨 번호 및 보너스 번호 입력 로직 구현 + + - [ ] 2-1. 사용자로부터 당첨 번호 6개와 보너스 번호 1개를 입력받기 + - [ ] 입력된 번호가 1부터 45사이의 정수가 아니거나, 중복이 있을 시 다시 입력. + +- [ ] 3. 당첨 내역 비교 및 결과 계산 + + - [ ] 3-1. 구매한 로또 번호와 당첨 번호를 비교하여 당첨 등수 계산하기 + - [ ] 3-2. 1등부터 5등까지의 당첨 횟수 출력하기 + +- [ ] 4. 수익률 계산 및 출력 + + - [ ] 4-1. 전체 당첨 금액 계산하기. + - [ ] 4-2. 수익률을 계산해 소수점 둘째 자리에서 반올림하여 출력하기. + +- [ ] 5. 테스트 코드 작성 + + - [ ] Jest 를 이용하여, 위 1, 2, 3, 4 가 제대로 기능하는지, 출력값은 정확한지를 테스트한다. From 713620a4e5ac5f15ba5ae2565387756e5b88ae45 Mon Sep 17 00:00:00 2001 From: Jin-Chanyong Date: Mon, 4 Nov 2024 19:39:04 +0900 Subject: [PATCH 02/30] =?UTF-8?q?feat:=20=EC=97=90=EB=9F=AC=20=EB=B0=9C?= =?UTF-8?q?=EC=83=9D=20=EB=B0=8F=20=EC=B6=9C=EB=A0=A5=20=ED=95=A8=EC=88=98?= =?UTF-8?q?=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit throwError.js 파일에 이하 내용 추가: - 에러 발생 및 출력 함수 throwError 을 구현하여, 에러 메시지 포맷 일관화. --- utils/throwError.js | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 utils/throwError.js diff --git a/utils/throwError.js b/utils/throwError.js new file mode 100644 index 000000000..ca9834e5f --- /dev/null +++ b/utils/throwError.js @@ -0,0 +1,6 @@ +import { Console } from "@woowacourse/mission-utils"; + +export default function throwError(message) { + Console.print(`[ERROR] ${message}`); + throw new Error(`[ERROR] ${message}`); +} From c55ce6b6da18216555232deab31f121ec91a6949 Mon Sep 17 00:00:00 2001 From: Jin-Chanyong Date: Mon, 4 Nov 2024 20:28:01 +0900 Subject: [PATCH 03/30] =?UTF-8?q?docs:=20README=20=ED=8C=8C=EC=9D=BC=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 README.md 파일에 이하 내용 수정: - 2-1 하위 내용에 예외 처리 경우 추가 --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f6f92e855..b82dd9c92 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,7 @@ - [ ] 2. 당첨 번호 및 보너스 번호 입력 로직 구현 - [ ] 2-1. 사용자로부터 당첨 번호 6개와 보너스 번호 1개를 입력받기 - - [ ] 입력된 번호가 1부터 45사이의 정수가 아니거나, 중복이 있을 시 다시 입력. + - [ ] 입력된 번호가 1부터 45사이의 정수가 아니거나, 중복이 있거나, 쉼표로 구분되지 않을 시 다시 입력. - [ ] 3. 당첨 내역 비교 및 결과 계산 From a732530dcacc0a84fa0584fa398812c148798ee1 Mon Sep 17 00:00:00 2001 From: Jin-Chanyong Date: Mon, 4 Nov 2024 20:36:42 +0900 Subject: [PATCH 04/30] =?UTF-8?q?feat:=20=EA=B5=AC=EB=A7=A4=20=EA=B8=88?= =?UTF-8?q?=EC=95=A1=20=EC=9E=85=EB=A0=A5=EA=B0=92=20=EC=9C=A0=ED=9A=A8?= =?UTF-8?q?=EC=84=B1=20=EA=B2=80=EC=82=AC=20=ED=95=A8=EC=88=98=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 inputValidator.js 파일에 이하 내용 추가: - 구매 금액 입력값의 예외 처리를 위한 purchaseAmountValidate 함수 추가 --- modules/inputValidator.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 modules/inputValidator.js diff --git a/modules/inputValidator.js b/modules/inputValidator.js new file mode 100644 index 000000000..cf4e76d9e --- /dev/null +++ b/modules/inputValidator.js @@ -0,0 +1,17 @@ +import throwError from "../utils/throwError"; + +export function purchaseAmountValidate(amount) { + if (!amount.trim()) { + throwError("구입 금액을 입력해 주세요."); + } + + if (isNaN(amount)) { + throwError("구입 금액은 숫자로만 입력해 주세요."); + } + + const amountNumber = Number(amount); + + if (amountNumber <= 0 || amountNumber % 1000 !== 0) { + throwError("구입 금액은 1,000원 단위의 양의 정수로 입력해 주세요."); + } +} From ea95ba01feace7025690ee66120713432625483c Mon Sep 17 00:00:00 2001 From: Jin-Chanyong Date: Mon, 4 Nov 2024 20:41:49 +0900 Subject: [PATCH 05/30] =?UTF-8?q?refactor:=20=EC=97=90=EB=9F=AC=20?= =?UTF-8?q?=EB=B0=9C=EC=83=9D=20=ED=95=A8=EC=88=98=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit throwError.js 파일의 이하 내용 수정: - throwError 함수가 하나의 기능만을 하기 위해, 에러 출력 기능 제거 --- utils/throwError.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/utils/throwError.js b/utils/throwError.js index ca9834e5f..98ca3a12c 100644 --- a/utils/throwError.js +++ b/utils/throwError.js @@ -1,6 +1,3 @@ -import { Console } from "@woowacourse/mission-utils"; - export default function throwError(message) { - Console.print(`[ERROR] ${message}`); throw new Error(`[ERROR] ${message}`); } From c23310fff4ef507b7c3a1da43044f6f52f2fc602 Mon Sep 17 00:00:00 2001 From: Jin-Chanyong Date: Mon, 4 Nov 2024 20:51:22 +0900 Subject: [PATCH 06/30] =?UTF-8?q?feat:=20=EA=B5=AC=EC=9E=85=20=EA=B8=88?= =?UTF-8?q?=EC=95=A1=20=EC=9E=85=EB=A0=A5=20=ED=95=A8=EC=88=98=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 userInput.js 파일에 이하 내용 추가: - 구입 금액 입력 함수 getValidatedPurchaseAmount 함수 추가 - 입력값 유효성 검사 중 오류 발생 시, 오류 메시지 출력 후 재입력 --- modules/userInput.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 modules/userInput.js diff --git a/modules/userInput.js b/modules/userInput.js new file mode 100644 index 000000000..8432476cb --- /dev/null +++ b/modules/userInput.js @@ -0,0 +1,17 @@ +import { Console } from "@woowacourse/mission-utils"; +import { purchaseAmountValidate } from "./inputValidator"; + +export async function getValidatedPurchaseAmount() { + while (true) { + const purchaseAmount = await Console.readLineAsync( + "구입 금액을 입력해 주세요.\n" + ); + + try { + purchaseAmountValidate(purchaseAmount); + return purchaseAmount; + } catch (error) { + Console.print(error.message); + } + } +} From da014afdac6b9a690ab0c03c45c57880de0710c5 Mon Sep 17 00:00:00 2001 From: Jin-Chanyong Date: Mon, 4 Nov 2024 21:05:25 +0900 Subject: [PATCH 07/30] =?UTF-8?q?fix:=20import=20=EA=B2=BD=EB=A1=9C?= =?UTF-8?q?=EC=97=90=20=ED=99=95=EC=9E=A5=EC=9E=90=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit inputValidator.js, userInput.js 파일에 이하 내용 수정: - 모듈 import 에서 파일 명에 확장자 명 추가 --- modules/inputValidator.js | 2 +- modules/userInput.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/inputValidator.js b/modules/inputValidator.js index cf4e76d9e..52fd6b121 100644 --- a/modules/inputValidator.js +++ b/modules/inputValidator.js @@ -1,4 +1,4 @@ -import throwError from "../utils/throwError"; +import throwError from "../utils/throwError.js"; export function purchaseAmountValidate(amount) { if (!amount.trim()) { diff --git a/modules/userInput.js b/modules/userInput.js index 8432476cb..497969d1b 100644 --- a/modules/userInput.js +++ b/modules/userInput.js @@ -1,5 +1,5 @@ import { Console } from "@woowacourse/mission-utils"; -import { purchaseAmountValidate } from "./inputValidator"; +import { purchaseAmountValidate } from "./inputValidator.js"; export async function getValidatedPurchaseAmount() { while (true) { From cfcd9b5a1a5a71f4c58555a57385b7bbf4a3bb30 Mon Sep 17 00:00:00 2001 From: Jin-Chanyong Date: Mon, 4 Nov 2024 21:50:50 +0900 Subject: [PATCH 08/30] =?UTF-8?q?feat:=20=EB=8B=B9=EC=B2=A8=20=EB=B2=88?= =?UTF-8?q?=ED=98=B8=20=EC=9C=A0=ED=9A=A8=EC=84=B1=20=EA=B2=80=EC=82=AC=20?= =?UTF-8?q?=ED=95=A8=EC=88=98=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit inputValidator.js 파일에 이하 내용 추가: - 당첨 번호 입력값의 유효성을 검사하는 winningNumbersValidate 함수 추가 --- modules/inputValidator.js | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/modules/inputValidator.js b/modules/inputValidator.js index 52fd6b121..f37504e47 100644 --- a/modules/inputValidator.js +++ b/modules/inputValidator.js @@ -15,3 +15,36 @@ export function purchaseAmountValidate(amount) { throwError("구입 금액은 1,000원 단위의 양의 정수로 입력해 주세요."); } } + +export function winningNumbersValidate(numbers) { + const numbersArray = numbers.split(","); + const numbersSet = new Set(numbersArray); + + if (!numbers.trim()) { + throwError("번호를 입력해 주세요."); + } + + if (numbersArray.length !== 6) { + throwError("로또 번호는 6개여야 합니다."); + } + + if (numbersSet.size !== numbersArray.length) { + throwError("중복된 번호가 있습니다."); + } + + numbersArray.forEach((number) => { + const numericValue = Number(number); + + if (isNaN(number)) { + throwError("잘못된 입력이거나, 쉼표로 구분된 숫자열이 아닙니다."); + } + + if (!Number.isInteger(numericValue)) { + throwError("번호는 정수로 입력해주세요."); + } + + if (numericValue < 1 || numericValue > 45) { + throwError("숫자는 1 과 45 사이로 입력해주세요."); + } + }); +} From d2e052cdb1890ae5a60c4f3317e2d22e72bd6cd8 Mon Sep 17 00:00:00 2001 From: Jin-Chanyong Date: Mon, 4 Nov 2024 21:59:14 +0900 Subject: [PATCH 09/30] =?UTF-8?q?feat:=20=EB=B3=B4=EB=84=88=EC=8A=A4=20?= =?UTF-8?q?=EC=A0=90=EC=88=98=20=EC=9E=85=EB=A0=A5=EA=B0=92=20=EC=9C=A0?= =?UTF-8?q?=ED=9A=A8=EC=84=B1=20=EA=B2=80=EC=82=AC=20=ED=95=A8=EC=88=98=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 inputValidator.js 파일에 이하 내용 추가: - 보너스 점수 입력값의 유효성 검사 함수 bonusNumberValidate 함수 추가 --- modules/inputValidator.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/modules/inputValidator.js b/modules/inputValidator.js index f37504e47..ed6f246d5 100644 --- a/modules/inputValidator.js +++ b/modules/inputValidator.js @@ -48,3 +48,27 @@ export function winningNumbersValidate(numbers) { } }); } + +export function bonusNumberValidate(number, pickedNumbers) { + const numericValue = Number(number); + + if (!number.trim()) { + throwError("번호를 입력해 주세요."); + } + + if (isNaN(number)) { + throwError("번호는 숫자로 입력해 주세요."); + } + + if (!Number.isInteger(numericValue)) { + throwError("번호는 정수로 입력해주세요."); + } + + if (numericValue < 1 || numericValue > 45) { + throwError("숫자는 1 과 45 사이로 입력해주세요."); + } + + if (pickedNumbers.some((number) => number === numericValue)) { + throwError("이전에 선택한 숫자와 중복된 숫자입니다."); + } +} From 451c3a1b8d8846d89000ed85fc0f87976285d647 Mon Sep 17 00:00:00 2001 From: Jin-Chanyong Date: Mon, 4 Nov 2024 22:01:54 +0900 Subject: [PATCH 10/30] =?UTF-8?q?feat:=20=EB=8B=B9=EC=B2=A8=20=EB=B2=88?= =?UTF-8?q?=ED=98=B8=20=EC=9E=85=EB=A0=A5=20=ED=95=A8=EC=88=98=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 userInput.js 파일에 이하 내용 추가: - 당첨 번호를 입력받고, 유효성 검사 후 당첨 번호 배열을 반환하는 getValidatedWinningNumbers 함수 추가 --- modules/userInput.js | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/modules/userInput.js b/modules/userInput.js index 497969d1b..41b8818fa 100644 --- a/modules/userInput.js +++ b/modules/userInput.js @@ -1,5 +1,8 @@ import { Console } from "@woowacourse/mission-utils"; -import { purchaseAmountValidate } from "./inputValidator.js"; +import { + purchaseAmountValidate, + winningNumbersValidate, +} from "./inputValidator.js"; export async function getValidatedPurchaseAmount() { while (true) { @@ -15,3 +18,18 @@ export async function getValidatedPurchaseAmount() { } } } + +export async function getValidatedWinningNumbers() { + while (true) { + const winningNumbers = await Console.readLineAsync( + "당첨 번호를 입력해 주세요.\n" + ); + + try { + winningNumbersValidate(winningNumbers); + return winningNumbers.split(",").map(Number); + } catch (error) { + Console.print(error.message); + } + } +} From 760082f1c887e6bdf089c9cf1cdc6eb2a8145b16 Mon Sep 17 00:00:00 2001 From: Jin-Chanyong Date: Mon, 4 Nov 2024 22:04:49 +0900 Subject: [PATCH 11/30] =?UTF-8?q?feat:=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=20=ED=95=A8=EC=88=98=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 userInput.js 파일에 이하 내용 추가: - 보너스 번호를 입력받고, 유효성 검사 후 보너스 번호를 반환하는 getValidatedBonusNumber 함수 추가 --- modules/userInput.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/modules/userInput.js b/modules/userInput.js index 41b8818fa..883f3ba04 100644 --- a/modules/userInput.js +++ b/modules/userInput.js @@ -1,5 +1,6 @@ import { Console } from "@woowacourse/mission-utils"; import { + bonusNumberValidate, purchaseAmountValidate, winningNumbersValidate, } from "./inputValidator.js"; @@ -33,3 +34,18 @@ export async function getValidatedWinningNumbers() { } } } + +export async function getValidatedBonusNumber() { + while (true) { + const bonusNumber = await Console.readLineAsync( + "\n보너스 번호를 입력해 주세요.\n" + ); + + try { + bonusNumberValidate(bonusNumber); + return Number(bonusNumber); + } catch (error) { + Console.print(error.message); + } + } +} From a01506448bdfcb45aacdaa90288e18e456bf8973 Mon Sep 17 00:00:00 2001 From: Jin-Chanyong Date: Mon, 4 Nov 2024 22:06:18 +0900 Subject: [PATCH 12/30] =?UTF-8?q?fix:=20getValidatedWinningNumbers=20?= =?UTF-8?q?=ED=95=A8=EC=88=98=20=EC=9E=85=EB=A0=A5=20=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 userInput.js 파일에서 이하 내용 수정: - getValidatedWinningNumbers 함수에서 입력 안내 메시지를 한줄 띄고 나오도록 수정 --- modules/userInput.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/userInput.js b/modules/userInput.js index 883f3ba04..b107994ca 100644 --- a/modules/userInput.js +++ b/modules/userInput.js @@ -23,7 +23,7 @@ export async function getValidatedPurchaseAmount() { export async function getValidatedWinningNumbers() { while (true) { const winningNumbers = await Console.readLineAsync( - "당첨 번호를 입력해 주세요.\n" + "\n당첨 번호를 입력해 주세요.\n" ); try { From 1fc221be14b3a23d266725987323a2f0558e0e1a Mon Sep 17 00:00:00 2001 From: Jin-Chanyong Date: Mon, 4 Nov 2024 22:20:07 +0900 Subject: [PATCH 13/30] =?UTF-8?q?feat:=20Lotto=20=ED=81=B4=EB=9E=98?= =?UTF-8?q?=EC=8A=A4=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Lotto.js 파일에 이하 내용 추가: - 로또 번호를 생성하는 Lotto 클래스 추가 - 생성된 로또 번호의 유효성 검사 메소드와 번호를 가져오는 메소스 포함 --- models/Lotto.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 models/Lotto.js diff --git a/models/Lotto.js b/models/Lotto.js new file mode 100644 index 000000000..bdbb54110 --- /dev/null +++ b/models/Lotto.js @@ -0,0 +1,23 @@ +import { Console } from "@woowacourse/mission-utils"; +import { winningNumbersValidate } from "../modules/inputValidator.js"; + +export default class Lotto { + #numbers; + + constructor(numbers) { + this.#validate(numbers); + this.#numbers = numbers; + } + + #validate(numbers) { + try { + winningNumbersValidate(numbers.join(",")); + } catch { + Console.print("로또 번호 생성 중 문제가 발생했습니다."); + } + } + + getNumbers() { + return this.#numbers; + } +} From aa9b5715896873cb6a5cbdeceda6e2453d5c03cc Mon Sep 17 00:00:00 2001 From: Jin-Chanyong Date: Mon, 4 Nov 2024 22:23:09 +0900 Subject: [PATCH 14/30] =?UTF-8?q?feat:=20=EB=A1=9C=EB=98=90=20=EA=B4=80?= =?UTF-8?q?=EB=A0=A8=20=EC=83=81=EC=88=98=20=ED=8C=8C=EC=9D=BC=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 lottoConstants 파일에 이하 내용 추가: - 로또의 가격, 숫자 갯수, 숫자 최소 최대값 을 포함하는 상수값 - 로또의 등수별 가격을 포함하는 상수값 --- constants/lottoConstants.js | 14 ++++++++++++++ src/Lotto.js | 18 ------------------ 2 files changed, 14 insertions(+), 18 deletions(-) create mode 100644 constants/lottoConstants.js delete mode 100644 src/Lotto.js diff --git a/constants/lottoConstants.js b/constants/lottoConstants.js new file mode 100644 index 000000000..354d6ae7f --- /dev/null +++ b/constants/lottoConstants.js @@ -0,0 +1,14 @@ +export const lotto = { + PRICE: 1000, + NUMBER_COUNT: 6, + NUMBER_MIN: 1, + NUMBER_MAX: 45, +}; + +export const prizeMoney = { + FIRST: 2000000000, + SECOND: 30000000, + THIRD: 1500000, + FOURTH: 50000, + FIFTH: 5000, +}; diff --git a/src/Lotto.js b/src/Lotto.js deleted file mode 100644 index cb0b1527e..000000000 --- a/src/Lotto.js +++ /dev/null @@ -1,18 +0,0 @@ -class Lotto { - #numbers; - - constructor(numbers) { - this.#validate(numbers); - this.#numbers = numbers; - } - - #validate(numbers) { - if (numbers.length !== 6) { - throw new Error("[ERROR] 로또 번호는 6개여야 합니다."); - } - } - - // TODO: 추가 기능 구현 -} - -export default Lotto; From e8f7497759f11baf9ba8f1b17f5ea8dc9dd4973d Mon Sep 17 00:00:00 2001 From: Jin-Chanyong Date: Mon, 4 Nov 2024 22:27:57 +0900 Subject: [PATCH 15/30] =?UTF-8?q?feat:=20=EB=A1=9C=EB=98=90=20=EC=83=9D?= =?UTF-8?q?=EC=84=B1=20=ED=95=A8=EC=88=98=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit lottoService.js 파일에 이하 내용 추가: - 로또들을 생성하고 그 배열을 반환하는 generateLottos 함수 추가 --- modules/lottoService.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 modules/lottoService.js diff --git a/modules/lottoService.js b/modules/lottoService.js new file mode 100644 index 000000000..0abe5ae79 --- /dev/null +++ b/modules/lottoService.js @@ -0,0 +1,20 @@ +import { Random } from "@woowacourse/mission-utils"; +import { lotto } from "../constants/lottoConstants.js"; + +export function generateLottos(count) { + const lottos = []; + + for (let lottoCount = 0; lottoCount < count; lottoCount++) { + const numbers = Random.pickUniqueNumbersInRange( + lotto.NUMBER_MIN, + lotto.NUMBER_MAX, + lotto.NUMBER_COUNT + ); + + numbers.sort((a, b) => a - b); + + lottos.push(new Lotto(numbers)); + } + + return lottos; +} From 9c0d75c83eb081be438e1983f4d923a49b73ceb6 Mon Sep 17 00:00:00 2001 From: Jin-Chanyong Date: Mon, 4 Nov 2024 22:30:53 +0900 Subject: [PATCH 16/30] =?UTF-8?q?feat:=20=EB=A1=9C=EB=98=90=20=EC=B6=9C?= =?UTF-8?q?=EB=A0=A5=20=ED=95=A8=EC=88=98=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit lottoService.js 파일에 이하 내용 추가: - 생성된 로또들을 하나씩 출력하는 printLottos 함수 추가 --- modules/lottoService.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/modules/lottoService.js b/modules/lottoService.js index 0abe5ae79..e38038dd3 100644 --- a/modules/lottoService.js +++ b/modules/lottoService.js @@ -1,4 +1,4 @@ -import { Random } from "@woowacourse/mission-utils"; +import { Console, Random } from "@woowacourse/mission-utils"; import { lotto } from "../constants/lottoConstants.js"; export function generateLottos(count) { @@ -18,3 +18,9 @@ export function generateLottos(count) { return lottos; } + +export function printLottos(lottos) { + lottos.forEach((lotto) => { + Console.print(lotto.getNumbers()); + }); +} From 8334275b9c34c354c51c393a7f93e5251c45403b Mon Sep 17 00:00:00 2001 From: Jin-Chanyong Date: Mon, 4 Nov 2024 22:47:34 +0900 Subject: [PATCH 17/30] =?UTF-8?q?feat:=20=EB=A1=9C=EB=98=90=20=EB=B2=88?= =?UTF-8?q?=ED=98=B8=EC=9D=98=20=EC=9D=BC=EC=B9=98=20=EA=B0=9C=EC=88=98?= =?UTF-8?q?=EB=A5=BC=20=EB=B0=98=ED=99=98=ED=95=98=EB=8A=94=20=ED=95=A8?= =?UTF-8?q?=EC=88=98=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit lottoUtils.js 파일에서 이하 내용 추가: - 로또 번호와 당첨 번호 간 일치하는 개수를 반환하는 getMatchedCount 함수 추가 --- utils/lottoUtils.js | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 utils/lottoUtils.js diff --git a/utils/lottoUtils.js b/utils/lottoUtils.js new file mode 100644 index 000000000..438d4dc78 --- /dev/null +++ b/utils/lottoUtils.js @@ -0,0 +1,4 @@ +export default function getMatchedCount(lottoNumbers, winningNumbers) { + return lottoNumbers.filter((number) => winningNumbers.includes(number)) + .length; +} From f5218655eb3ef0ad2527436261400729fb5ae22b Mon Sep 17 00:00:00 2001 From: Jin-Chanyong Date: Mon, 4 Nov 2024 22:48:42 +0900 Subject: [PATCH 18/30] =?UTF-8?q?feat:=20=EC=88=98=EC=9D=B5=EB=A5=A0?= =?UTF-8?q?=EC=9D=84=20=EA=B3=84=EC=82=B0=ED=95=98=EB=8A=94=20=ED=95=A8?= =?UTF-8?q?=EC=88=98=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit lottoUtils.js 파일에서 이하 내용 추가: - 수익률을 계산하는 calculateYield 함수 추가 --- utils/lottoUtils.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/utils/lottoUtils.js b/utils/lottoUtils.js index 438d4dc78..1330540dd 100644 --- a/utils/lottoUtils.js +++ b/utils/lottoUtils.js @@ -1,4 +1,8 @@ -export default function getMatchedCount(lottoNumbers, winningNumbers) { +export function getMatchedCount(lottoNumbers, winningNumbers) { return lottoNumbers.filter((number) => winningNumbers.includes(number)) .length; } + +export function calculateYield(totalPrize, purchaseAmount) { + return ((totalPrize / purchaseAmount) * 100).toFixed(1); +} From 1f1adc9d12fa6766cbbc2eea6cdd62198c145d31 Mon Sep 17 00:00:00 2001 From: Jin-Chanyong Date: Mon, 4 Nov 2024 22:51:53 +0900 Subject: [PATCH 19/30] =?UTF-8?q?feat:=20=EB=A1=9C=EB=98=90=20=EB=93=B1?= =?UTF-8?q?=EC=88=98=EB=B3=84=20=EB=8B=B9=EC=B2=A8=20=ED=9A=9F=EC=88=98=20?= =?UTF-8?q?=EB=B0=98=ED=99=98=20=ED=95=A8=EC=88=98=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit lottoService.js 파일에 이하 내용 추가: - 로또 등수 별 당첨 횟수를 반환하는 calculateWinsCount 함수 추가 --- modules/lottoService.js | 43 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/modules/lottoService.js b/modules/lottoService.js index e38038dd3..89591f2ae 100644 --- a/modules/lottoService.js +++ b/modules/lottoService.js @@ -1,5 +1,6 @@ import { Console, Random } from "@woowacourse/mission-utils"; import { lotto } from "../constants/lottoConstants.js"; +import { getMatchedCount } from "../utils/lottoUtils.js"; export function generateLottos(count) { const lottos = []; @@ -24,3 +25,45 @@ export function printLottos(lottos) { Console.print(lotto.getNumbers()); }); } + +export function calculateWinsCount(lottos, winningNumbers, bonusNumber) { + const resultCount = { + FIRST: 0, + SECOND: 0, + THIRD: 0, + FOURTH: 0, + FIFTH: 0, + }; + + lottos.forEach((lotto) => { + const matchedCount = getMatchedCount(lotto.getNumbers(), winningNumbers); + const hasBonus = lotto.getNumbers().includes(bonusNumber); + + if (matchedCount === 6) { + resultCount.FIRST += 1; + return; + } + + if (matchedCount === 5 && hasBonus) { + resultCount.SECOND += 1; + return; + } + + if (matchedCount === 5) { + resultCount.THIRD += 1; + return; + } + + if (matchedCount === 4) { + resultCount.FOURTH += 1; + return; + } + + if (matchedCount === 3) { + resultCount.FIFTH += 1; + return; + } + }); + + return resultCount; +} From 4597a642ee8ef183eff6326e4fdf3a307808d791 Mon Sep 17 00:00:00 2001 From: Jin-Chanyong Date: Mon, 4 Nov 2024 23:05:04 +0900 Subject: [PATCH 20/30] =?UTF-8?q?feat:=20=EB=A1=9C=EB=98=90=20=EB=8B=B9?= =?UTF-8?q?=EC=B2=A8=EA=B8=88=EC=9D=98=20=EC=88=98=EC=9D=B5=EB=A5=A0?= =?UTF-8?q?=EC=9D=84=20=EA=B3=84=EC=82=B0=ED=95=98=EB=8A=94=20=ED=95=A8?= =?UTF-8?q?=EC=88=98=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit lottoService.js 파일에 이하 내용 추가: - 로또의 수익률을 계산하는 calculateLottoYield 함수 추가 --- modules/lottoService.js | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/modules/lottoService.js b/modules/lottoService.js index 89591f2ae..6cdb32bbc 100644 --- a/modules/lottoService.js +++ b/modules/lottoService.js @@ -1,6 +1,6 @@ import { Console, Random } from "@woowacourse/mission-utils"; -import { lotto } from "../constants/lottoConstants.js"; -import { getMatchedCount } from "../utils/lottoUtils.js"; +import { lotto, prizeMoney } from "../constants/lottoConstants.js"; +import { calculateYield, getMatchedCount } from "../utils/lottoUtils.js"; export function generateLottos(count) { const lottos = []; @@ -67,3 +67,14 @@ export function calculateWinsCount(lottos, winningNumbers, bonusNumber) { return resultCount; } + +export function calculateLottoYield(resultCount, purchaseAmount) { + const totalPrize = + resultCount.FIRST * prizeMoney.FIRST + + resultCount.SECOND * prizeMoney.SECOND + + resultCount.THIRD * prizeMoney.THIRD + + resultCount.FOURTH * prizeMoney.FOURTH + + resultCount.FIFTH * prizeMoney.FIFTH; + + return calculateYield(totalPrize, purchaseAmount); +} From cab8c1daa8ad9c2c7b6d92c7e3f5d975917eab14 Mon Sep 17 00:00:00 2001 From: Jin-Chanyong Date: Mon, 4 Nov 2024 23:12:37 +0900 Subject: [PATCH 21/30] =?UTF-8?q?feat:=20=EB=A1=9C=EB=98=90=20=EA=B2=B0?= =?UTF-8?q?=EA=B3=BC=20=EC=B6=9C=EB=A0=A5=20=ED=95=A8=EC=88=98=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 lottoService.js 파일에 이하 내용 추가: - 로또 당첨 횟수와 수익률에 대한 결과를 출력하는 printLottoResult 함수 추가 --- modules/lottoService.js | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/modules/lottoService.js b/modules/lottoService.js index 6cdb32bbc..022c375bf 100644 --- a/modules/lottoService.js +++ b/modules/lottoService.js @@ -26,7 +26,7 @@ export function printLottos(lottos) { }); } -export function calculateWinsCount(lottos, winningNumbers, bonusNumber) { +function calculateWinsCount(lottos, winningNumbers, bonusNumber) { const resultCount = { FIRST: 0, SECOND: 0, @@ -68,7 +68,7 @@ export function calculateWinsCount(lottos, winningNumbers, bonusNumber) { return resultCount; } -export function calculateLottoYield(resultCount, purchaseAmount) { +function calculateLottoYield(resultCount, purchaseAmount) { const totalPrize = resultCount.FIRST * prizeMoney.FIRST + resultCount.SECOND * prizeMoney.SECOND + @@ -78,3 +78,35 @@ export function calculateLottoYield(resultCount, purchaseAmount) { return calculateYield(totalPrize, purchaseAmount); } + +export function printLottoResult( + lottos, + winningNumbers, + bonusNumber, + purchaseAmount +) { + const resultCount = calculateWinsCount(lottos, winningNumbers, bonusNumber); + const lottoYield = calculateLottoYield(resultCount, purchaseAmount); + + Console.print("\n당첨 통계\n---"); + Console.print( + `3개 일치 (${prizeMoney.FIFTH.toLocaleString()}원) - ${resultCount.FIFTH}개` + ); + Console.print( + `4개 일치 (${prizeMoney.FOURTH.toLocaleString()}원) - ${ + resultCount.FOURTH + }개` + ); + Console.print( + `5개 일치 (${prizeMoney.THIRD.toLocaleString()}원) - ${resultCount.THIRD}개` + ); + Console.print( + `5개 일치, 보너스 볼 일치 (${prizeMoney.SECOND.toLocaleString()}원) - ${ + resultCount.SECOND + }개` + ); + Console.print( + `6개 일치 (${prizeMoney.FIRST.toLocaleString()}원) - ${resultCount.FIRST}개` + ); + Console.print(`총 수익률은 ${lottoYield}%입니다.`); +} From ea620ca0b33813cab0b31d61bb1c1a760178134e Mon Sep 17 00:00:00 2001 From: Jin-Chanyong Date: Mon, 4 Nov 2024 23:16:23 +0900 Subject: [PATCH 22/30] =?UTF-8?q?feat:=20=EB=A1=9C=EB=98=90=20=EB=B0=9C?= =?UTF-8?q?=EB=A7=A4=EA=B8=B0=20=EB=A1=9C=EC=A7=81=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit App.js 파일에 이하 내용 추가: - 로또 발매기 로직 구현 --- src/App.js | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/App.js b/src/App.js index 091aa0a5d..85c4baeb8 100644 --- a/src/App.js +++ b/src/App.js @@ -1,5 +1,30 @@ +import { lotto } from "../constants/lottoConstants.js"; +import { + generateLottos, + printLottoResult, + printLottos, +} from "../modules/lottoService.js"; +import { + getValidatedPurchaseAmount, + getValidatedWinningNumbers, +} from "../modules/userInput.js"; + class App { - async run() {} + async run() { + const purchaseAmount = await getValidatedPurchaseAmount(); + + const lottoCount = Math.floor(purchaseAmount / lotto.PRICE); + + const lottos = generateLottos(lottoCount); + + printLottos(lottos); + + const winningNumbers = await getValidatedWinningNumbers(); + + const bonusNumber = await getValidatedBonusNumber(); + + printLottoResult(lottos, winningNumbers, bonusNumber, purchaseAmount); + } } export default App; From 5a43376b2a614934906cae88b42d5d338c56b919 Mon Sep 17 00:00:00 2001 From: Jin-Chanyong Date: Mon, 4 Nov 2024 23:18:39 +0900 Subject: [PATCH 23/30] =?UTF-8?q?fix:=20Lotto=20=ED=81=B4=EB=9E=98?= =?UTF-8?q?=EC=8A=A4=20import=20=EB=AC=B8=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit lottoService.js 파일에 이하 내용 수정: - Lotto 클래스를 import 하지 않아 발생한 오류 수정 --- modules/lottoService.js | 1 + 1 file changed, 1 insertion(+) diff --git a/modules/lottoService.js b/modules/lottoService.js index 022c375bf..81079f10e 100644 --- a/modules/lottoService.js +++ b/modules/lottoService.js @@ -1,6 +1,7 @@ import { Console, Random } from "@woowacourse/mission-utils"; import { lotto, prizeMoney } from "../constants/lottoConstants.js"; import { calculateYield, getMatchedCount } from "../utils/lottoUtils.js"; +import Lotto from "../models/lotto.js"; export function generateLottos(count) { const lottos = []; From 9e446b24c6fddb52f5d66501bcf435f28d821dd2 Mon Sep 17 00:00:00 2001 From: Jin-Chanyong Date: Mon, 4 Nov 2024 23:20:32 +0900 Subject: [PATCH 24/30] =?UTF-8?q?fix:=20getValidatedBonusNumber=20import?= =?UTF-8?q?=20=EB=AC=B8=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit App.js 파일에 이하 내용 수정: - getValidatedBonusNumber 함수를 import 하지 않아 발생한 오류 수정 --- src/App.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/App.js b/src/App.js index 85c4baeb8..04ce506f9 100644 --- a/src/App.js +++ b/src/App.js @@ -5,6 +5,7 @@ import { printLottos, } from "../modules/lottoService.js"; import { + getValidatedBonusNumber, getValidatedPurchaseAmount, getValidatedWinningNumbers, } from "../modules/userInput.js"; From 0535facc68a096c5f9465fab076314d6201e3657 Mon Sep 17 00:00:00 2001 From: Jin-Chanyong Date: Mon, 4 Nov 2024 23:23:14 +0900 Subject: [PATCH 25/30] =?UTF-8?q?fix:=20printLottos=20=ED=95=A8=EC=88=98?= =?UTF-8?q?=20=EC=B6=9C=EB=A0=A5=EA=B0=92=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit lottoService.js 파일에서 이하 내용 수정: - printLottos 함수에서 구매 갯수를 출력하지 않던 것을 수정 --- modules/lottoService.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/modules/lottoService.js b/modules/lottoService.js index 81079f10e..9e3545089 100644 --- a/modules/lottoService.js +++ b/modules/lottoService.js @@ -22,6 +22,8 @@ export function generateLottos(count) { } export function printLottos(lottos) { + Console.print(`\n${lottos.length}개를 구매했습니다.`); + lottos.forEach((lotto) => { Console.print(lotto.getNumbers()); }); From e749ecd4d8f2c5680c4924e63149e0177b84c6b0 Mon Sep 17 00:00:00 2001 From: Jin-Chanyong Date: Mon, 4 Nov 2024 23:27:20 +0900 Subject: [PATCH 26/30] =?UTF-8?q?fix:=20bonusNumberValidate=20=EC=98=A4?= =?UTF-8?q?=EB=A5=98=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit App.js, userInput.js 파일에서 이하 내용 수정: - bonusNumberValidate 에 파라미터가 전해지지 않던 오류 수정 --- modules/userInput.js | 4 ++-- src/App.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/modules/userInput.js b/modules/userInput.js index b107994ca..a3ecfcc51 100644 --- a/modules/userInput.js +++ b/modules/userInput.js @@ -35,14 +35,14 @@ export async function getValidatedWinningNumbers() { } } -export async function getValidatedBonusNumber() { +export async function getValidatedBonusNumber(winningNumbers) { while (true) { const bonusNumber = await Console.readLineAsync( "\n보너스 번호를 입력해 주세요.\n" ); try { - bonusNumberValidate(bonusNumber); + bonusNumberValidate(bonusNumber, winningNumbers); return Number(bonusNumber); } catch (error) { Console.print(error.message); diff --git a/src/App.js b/src/App.js index 04ce506f9..63a151e61 100644 --- a/src/App.js +++ b/src/App.js @@ -22,7 +22,7 @@ class App { const winningNumbers = await getValidatedWinningNumbers(); - const bonusNumber = await getValidatedBonusNumber(); + const bonusNumber = await getValidatedBonusNumber(winningNumbers); printLottoResult(lottos, winningNumbers, bonusNumber, purchaseAmount); } From 549d47390807d5555c8b3bb28d22386da5a4f17c Mon Sep 17 00:00:00 2001 From: Jin-Chanyong Date: Mon, 4 Nov 2024 23:42:33 +0900 Subject: [PATCH 27/30] =?UTF-8?q?fix:=20=ED=85=8C=EC=8A=A4=ED=8A=B8?= =?UTF-8?q?=EC=99=80=20=EC=B6=9C=EB=A0=A5=EA=B0=92=20=EB=B6=88=EC=9D=BC?= =?UTF-8?q?=EC=B9=98=20=EC=98=A4=EB=A5=98=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit lottoService.js 파일에서 이하 내용 수정: - 배열을 그대로 출력하여 출력값이 테스트 케이스와 일치하지 않는 내용 수정 --- modules/lottoService.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/lottoService.js b/modules/lottoService.js index 9e3545089..1dcb152b8 100644 --- a/modules/lottoService.js +++ b/modules/lottoService.js @@ -25,7 +25,7 @@ export function printLottos(lottos) { Console.print(`\n${lottos.length}개를 구매했습니다.`); lottos.forEach((lotto) => { - Console.print(lotto.getNumbers()); + Console.print(`[${lotto.getNumbers().join(", ")}]`); }); } From a185696247d2c31f7622981458008376f1be4ebd Mon Sep 17 00:00:00 2001 From: Jin-Chanyong Date: Mon, 4 Nov 2024 23:43:25 +0900 Subject: [PATCH 28/30] =?UTF-8?q?fix:=20Lotto=20=ED=81=B4=EB=9E=98?= =?UTF-8?q?=EC=8A=A4=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=98=A4=EB=A5=98=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 Lotto.js, LottoTest.js 파일에서 이하 내용 수정: - 로또 클래스의 테스트에서 예외 오류가 발생하지 않던 오류를 수정 --- __tests__/LottoTest.js | 2 +- models/Lotto.js | 7 +------ 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/__tests__/LottoTest.js b/__tests__/LottoTest.js index 409aaf69b..bcdb1ab05 100644 --- a/__tests__/LottoTest.js +++ b/__tests__/LottoTest.js @@ -1,4 +1,4 @@ -import Lotto from "../src/Lotto"; +import Lotto from "../models/lotto.js"; describe("로또 클래스 테스트", () => { test("로또 번호의 개수가 6개가 넘어가면 예외가 발생한다.", () => { diff --git a/models/Lotto.js b/models/Lotto.js index bdbb54110..cd8951bd7 100644 --- a/models/Lotto.js +++ b/models/Lotto.js @@ -1,4 +1,3 @@ -import { Console } from "@woowacourse/mission-utils"; import { winningNumbersValidate } from "../modules/inputValidator.js"; export default class Lotto { @@ -10,11 +9,7 @@ export default class Lotto { } #validate(numbers) { - try { - winningNumbersValidate(numbers.join(",")); - } catch { - Console.print("로또 번호 생성 중 문제가 발생했습니다."); - } + winningNumbersValidate(numbers.join(",")); } getNumbers() { From d60d2836360686dcdeca0f9038a30ea88df3305c Mon Sep 17 00:00:00 2001 From: Jin-Chanyong Date: Mon, 4 Nov 2024 23:48:20 +0900 Subject: [PATCH 29/30] =?UTF-8?q?feat:=20=EB=A1=9C=EB=98=90=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8=20=EC=BC=80=EC=9D=B4=EC=8A=A4=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 ApplicationTest.js 파일에 이하 내용 추가: - 유효하지 않은 값을 입력한 경우의 테스트 케이스 추가 --- __tests__/ApplicationTest.js | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/__tests__/ApplicationTest.js b/__tests__/ApplicationTest.js index 872380c9c..aaa87b4df 100644 --- a/__tests__/ApplicationTest.js +++ b/__tests__/ApplicationTest.js @@ -3,10 +3,8 @@ import { MissionUtils } from "@woowacourse/mission-utils"; const mockQuestions = (inputs) => { MissionUtils.Console.readLineAsync = jest.fn(); - MissionUtils.Console.readLineAsync.mockImplementation(() => { const input = inputs.shift(); - return Promise.resolve(input); }); }; @@ -25,30 +23,25 @@ const getLogSpy = () => { }; const runException = async (input) => { - // given const logSpy = getLogSpy(); - const RANDOM_NUMBERS_TO_END = [1, 2, 3, 4, 5, 6]; const INPUT_NUMBERS_TO_END = ["1000", "1,2,3,4,5,6", "7"]; mockRandoms([RANDOM_NUMBERS_TO_END]); mockQuestions([input, ...INPUT_NUMBERS_TO_END]); - // when const app = new App(); await app.run(); - // then expect(logSpy).toHaveBeenCalledWith(expect.stringContaining("[ERROR]")); }; -describe("로또 테스트", () => { +describe("로또 애플리케이션 테스트", () => { beforeEach(() => { jest.restoreAllMocks(); }); test("기능 테스트", async () => { - // given const logSpy = getLogSpy(); mockRandoms([ @@ -63,11 +56,9 @@ describe("로또 테스트", () => { ]); mockQuestions(["8000", "1,2,3,4,5,6", "7"]); - // when const app = new App(); await app.run(); - // then const logs = [ "8개를 구매했습니다.", "[8, 21, 23, 41, 42, 43]", @@ -91,7 +82,11 @@ describe("로또 테스트", () => { }); }); - test("예외 테스트", async () => { + test("예외 테스트 - 유효하지 않은 금액 입력", async () => { await runException("1000j"); }); + + test("예외 테스트 - 유효하지 않은 당첨 번호 입력", async () => { + await runException("1,2,3,4,5,abc"); + }); }); From 8848994bf3485ac39b21d463788ee83ee6c74ce3 Mon Sep 17 00:00:00 2001 From: Jin-Chanyong Date: Mon, 4 Nov 2024 23:49:30 +0900 Subject: [PATCH 30/30] =?UTF-8?q?docs:=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20?= =?UTF-8?q?=EA=B2=B0=EA=B3=BC=EC=97=90=20=EB=94=B0=EB=A5=B8=20=EA=B8=B0?= =?UTF-8?q?=EB=8A=A5=20=EB=AA=A9=EB=A1=9D=20=EB=8B=A8=EC=9C=84=20=EC=B2=B4?= =?UTF-8?q?=ED=81=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit README.md 파일에서 이하 내용 수정: - 테스트 결과에 따른 완료된 기능 목록 단위 체크 --- README.md | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index b82dd9c92..1cd763428 100644 --- a/README.md +++ b/README.md @@ -19,29 +19,29 @@ ## 기능 목록 단위 -- [ ] 1. 로또 구매 로직 구현 +- [x] 1. 로또 구매 로직 구현 - - [ ] 1-1. 사용자로부터 로또 구입 금액 입력받기 - - [ ] 입력된 금액이 1000원 단위로 나누어 떨어지지 않거나, 잘못된 입력 시 다시 입력. - - [ ] 1-2. 구입 금액에 따라 구매할 로또 수량을 계산하기 - - [ ] 1-3. 구매한 로또 수량 만큼 로또 번호 생성하고 출력하기 - - [ ] 로또 번호는 오름차순으로 정렬. + - [x] 1-1. 사용자로부터 로또 구입 금액 입력받기 + - [x] 입력된 금액이 1000원 단위로 나누어 떨어지지 않거나, 잘못된 입력 시 다시 입력. + - [x] 1-2. 구입 금액에 따라 구매할 로또 수량을 계산하기 + - [x] 1-3. 구매한 로또 수량 만큼 로또 번호 생성하고 출력하기 + - [x] 로또 번호는 오름차순으로 정렬. -- [ ] 2. 당첨 번호 및 보너스 번호 입력 로직 구현 +- [x] 2. 당첨 번호 및 보너스 번호 입력 로직 구현 - - [ ] 2-1. 사용자로부터 당첨 번호 6개와 보너스 번호 1개를 입력받기 - - [ ] 입력된 번호가 1부터 45사이의 정수가 아니거나, 중복이 있거나, 쉼표로 구분되지 않을 시 다시 입력. + - [x] 2-1. 사용자로부터 당첨 번호 6개와 보너스 번호 1개를 입력받기 + - [x] 입력된 번호가 1부터 45사이의 정수가 아니거나, 중복이 있거나, 쉼표로 구분되지 않을 시 다시 입력. -- [ ] 3. 당첨 내역 비교 및 결과 계산 +- [x] 3. 당첨 내역 비교 및 결과 계산 - - [ ] 3-1. 구매한 로또 번호와 당첨 번호를 비교하여 당첨 등수 계산하기 - - [ ] 3-2. 1등부터 5등까지의 당첨 횟수 출력하기 + - [x] 3-1. 구매한 로또 번호와 당첨 번호를 비교하여 당첨 등수 계산하기 + - [x] 3-2. 1등부터 5등까지의 당첨 횟수 출력하기 -- [ ] 4. 수익률 계산 및 출력 +- [x] 4. 수익률 계산 및 출력 - - [ ] 4-1. 전체 당첨 금액 계산하기. - - [ ] 4-2. 수익률을 계산해 소수점 둘째 자리에서 반올림하여 출력하기. + - [x] 4-1. 전체 당첨 금액 계산하기. + - [x] 4-2. 수익률을 계산해 소수점 둘째 자리에서 반올림하여 출력하기. -- [ ] 5. 테스트 코드 작성 +- [x] 5. 테스트 코드 작성 - - [ ] Jest 를 이용하여, 위 1, 2, 3, 4 가 제대로 기능하는지, 출력값은 정확한지를 테스트한다. + - [x] Jest 를 이용하여, 위 1, 2, 3, 4 가 제대로 기능하는지, 출력값은 정확한지를 테스트한다.