From c0f6f680a64ab7ccaebb7ada86743b395fae807c Mon Sep 17 00:00:00 2001 From: inaemin Date: Mon, 3 Nov 2025 21:05:56 +0900 Subject: [PATCH 01/11] =?UTF-8?q?docs:=20README=EC=97=90=20=EA=B8=B0?= =?UTF-8?q?=EB=8A=A5=20=EB=AA=A9=EB=A1=9D=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/README.md b/README.md index 15bb106b5..583a0a414 100644 --- a/README.md +++ b/README.md @@ -1 +1,32 @@ # javascript-lotto-precourse + +## 기능 목록 + +### 입력받기 + +- 1000원으로 나누어 떨어지는 금액을 입력받는다. + - [ERROR] 나누어 떨어지지 않으면 오류. + - [ERROR] 1000원 이상이 아니라면 오류. +- 쉼표 기준으로 구분되는 6개의 당첨 번호를 입력 받는다. + - [ERROR] 6개가 아니면 오류. + - [ERROR] 1부터 45까지의 숫자가 아니면 오류. +- 보너스 번호를 입력받는다. + - [ERROR] 1부터 45까지의 숫자가 아니면 오류. + +### 출력하기 + +- 생성한 로또를 출력한다. +- 당첨 통계를 출력한다. + +### 로또 관리자 + +- 입력받은 구입금액에 맞춰 로또를 생성한다. +- 당첨 번호와 생성한 로또를 비교하여 당첨액을 계산한다. +- 당첨액에서 구입금액을 나눠서 수익률을 계산한다. + +### 로또 + +- 로또 번호의 숫자 범위는 1부터 45까지. +- 로또 번호는 중복되지 않아야 한다. +- 로또 번호는 6개이다. +- 로또 번호는 오름차순으로 정렬한다. From 932626ccda5e257ca0adc3ce329d4293257a34ea Mon Sep 17 00:00:00 2001 From: inaemin Date: Mon, 3 Nov 2025 23:38:37 +0900 Subject: [PATCH 02/11] =?UTF-8?q?docs:=20=EA=B8=B0=EB=8A=A5=20=EB=AA=A9?= =?UTF-8?q?=EB=A1=9D=EC=9D=84=20=EB=8D=94=20=EC=83=81=EC=84=B8=ED=95=98?= =?UTF-8?q?=EA=B2=8C=20=EC=9E=91=EC=84=B1=20(=EA=B2=80=EC=A6=9D=20?= =?UTF-8?q?=EB=A1=9C=EC=A7=81,=20=EC=B6=9C=EB=A0=A5=20=ED=98=95=EC=8B=9D?= =?UTF-8?q?=20=EB=AA=85=EC=8B=9C)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/README.md b/README.md index 583a0a414..de5133532 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,38 @@ # javascript-lotto-precourse +간단한 로또 발매기를 구현한 프로젝트입니다. +사용자가 구입금액을 입력하면 로또를 생성하고, 당첨번호와 비교하여 당첨 내역과 수익률을 보여줍니다. + +## 주요 기능 + +- 로또 자동 생성 및 출력 +- 당첨번호 비교 및 상금 계산 +- 수익률 계산 (소수점 둘째 자리 반올림) +- 예외 처리 및 재입력 + +## 프로젝트 구조 + +- **App**: 프로그램의 전체 흐름 관리 +- **LottoManager**: 로또 생성 및 당첨금/수익률 계산 +- **Lotto**: 개별 로또 번호 관리 및 검증 +- **InputManager**: 사용자 입력받기 (숫자, 쉼표 구분 숫자) +- **Validator**: 입력값 유효성 검증 +- **OutputManager**: 결과 출력 +- **constants**: 상수 및 당첨금 정보 (Map 사용) + +## 실행 방법 + +```bash +npm install +npm run start +``` + +## 테스트 + +```bash +npm run test +``` + ## 기능 목록 ### 입력받기 @@ -10,13 +43,20 @@ - 쉼표 기준으로 구분되는 6개의 당첨 번호를 입력 받는다. - [ERROR] 6개가 아니면 오류. - [ERROR] 1부터 45까지의 숫자가 아니면 오류. + - [ERROR] 당첨 번호에 중복이 있으면 오류. - 보너스 번호를 입력받는다. - [ERROR] 1부터 45까지의 숫자가 아니면 오류. + - [ERROR] 당첨 번호와 보너스 번호가 중복되면 오류. ### 출력하기 +- 구입한 로또 개수를 출력한다. + - "X개를 구매했습니다." 형식 - 생성한 로또를 출력한다. + - "[번호1, 번호2, ...]" 형식으로 오름차순 정렬 - 당첨 통계를 출력한다. + - 3개 일치부터 6개 일치까지 당첨 내역 출력 + - 수익률을 소수점 둘째 자리에서 반올림하여 출력 ### 로또 관리자 From 75092d3b36179bb47c5b3e1418688bb111910a0a Mon Sep 17 00:00:00 2001 From: inaemin Date: Mon, 3 Nov 2025 23:39:42 +0900 Subject: [PATCH 03/11] =?UTF-8?q?feat:=20=EC=83=81=EC=88=98=20=EC=A0=95?= =?UTF-8?q?=EC=9D=98=20(=EB=A1=9C=EB=98=90=20=EB=B2=88=ED=98=B8=20?= =?UTF-8?q?=EB=B2=94=EC=9C=84,=20=EA=B0=80=EA=B2=A9,=20=EB=8B=B9=EC=B2=A8?= =?UTF-8?q?=EA=B8=88)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/constants.js | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 src/constants.js diff --git a/src/constants.js b/src/constants.js new file mode 100644 index 000000000..b5dd4c934 --- /dev/null +++ b/src/constants.js @@ -0,0 +1,11 @@ +export const MIN_LOTTO_NUM = 1; +export const MAX_LOTTO_NUM = 45; +export const COST_PER_LOTTO = 1000; +export const PRIZE_BY_MATCH_COUNT = new Map([ + ["3", { prize: 5000, label: "3개 일치" }], + ["4", { prize: 50000, label: "4개 일치" }], + ["5", { prize: 1500000, label: "5개 일치" }], + ["5+bonus", { prize: 30000000, label: "5개 일치, 보너스 볼 일치" }], + ["6", { prize: 2000000000, label: "6개 일치" }], +]); +export const MIN_WINNING_MATCH = 3; From 665c1b9433f0b58f62d4d7be56db956df53cf03d Mon Sep 17 00:00:00 2001 From: inaemin Date: Mon, 3 Nov 2025 23:39:45 +0900 Subject: [PATCH 04/11] =?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=20(=EB=B2=88=ED=98=B8=20?= =?UTF-8?q?=EA=B2=80=EC=A6=9D,=20=EB=A7=A4=EC=B9=AD=20=ED=99=95=EC=9D=B8)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Lotto.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/Lotto.js b/src/Lotto.js index cb0b1527e..62826f377 100644 --- a/src/Lotto.js +++ b/src/Lotto.js @@ -10,9 +10,26 @@ class Lotto { if (numbers.length !== 6) { throw new Error("[ERROR] 로또 번호는 6개여야 합니다."); } + if (new Set([...numbers]).size !== 6) { + throw new Error("[ERROR] 로또 번호에 중복이 있습니다."); + } } // TODO: 추가 기능 구현 + getNumbers() { + return [...this.#numbers]; + } + + getMatchCount(winningNumbers, bonusNumber) { + let matchCount = 0; + for (let lottoNum of this.#numbers) { + if (winningNumbers.includes(lottoNum)) matchCount++; + } + if (matchCount === 5 && this.#numbers.includes(bonusNumber)) { + return `${matchCount}+bonus`; + } + return matchCount; + } } export default Lotto; From d2c0bbd869feb9aae223e847737fc1e073f864e6 Mon Sep 17 00:00:00 2001 From: inaemin Date: Mon, 3 Nov 2025 23:39:48 +0900 Subject: [PATCH 05/11] =?UTF-8?q?feat:=20Validator=20=ED=81=B4=EB=9E=98?= =?UTF-8?q?=EC=8A=A4=20=EA=B5=AC=ED=98=84=20(=EC=9E=85=EB=A0=A5=EA=B0=92?= =?UTF-8?q?=20=EA=B2=80=EC=A6=9D)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Validator.js | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 src/Validator.js diff --git a/src/Validator.js b/src/Validator.js new file mode 100644 index 000000000..4afa5c46a --- /dev/null +++ b/src/Validator.js @@ -0,0 +1,41 @@ +import { COST_PER_LOTTO, MAX_LOTTO_NUM, MIN_LOTTO_NUM } from "./constants.js"; + +export class Validator { + static validateNumber(numStr) { + if (numStr.trim() === "" || isNaN(Number(numStr.trim()))) { + throw new Error(`[ERROR] 유효한 숫자가 아닙니다.`); + } + } + + static validatePurchaseAmount(amount) { + if (amount < COST_PER_LOTTO) { + throw new Error( + `[ERROR] 로또 최소 구입 금액은 ${COST_PER_LOTTO}원 입니다.` + ); + } + if (amount % COST_PER_LOTTO !== 0) { + throw new Error( + `[ERROR] 구입 금액은 ${COST_PER_LOTTO}원으로 나누어 떨어져야 합니다.` + ); + } + } + + static validateWinningAndBonusNumbers(winningNumbers, bonusNumber) { + this.#isValidWinningNumbers(winningNumbers); + if (winningNumbers.includes(bonusNumber)) { + throw new Error(`[ERROR] 당첨숫자와 보너스 숫자는 중복될 수 없습니다.`); + } + } + + static #isValidWinningNumbers(winningNumbers) { + for (let num of winningNumbers) { + if (num < MIN_LOTTO_NUM || num > MAX_LOTTO_NUM) { + throw new Error(`[ERROR] 유효한 당첨 숫자가 아닙니다.`); + } + } + const winningNumbersSet = new Set([...winningNumbers]); + if (winningNumbers.length !== winningNumbersSet.size) { + throw new Error(`[ERROR] 당첨 숫자는 중복될 수 없습니다.`); + } + } +} From 162e8ab088931e4cece1c406feb5a9b6f2a599c3 Mon Sep 17 00:00:00 2001 From: inaemin Date: Mon, 3 Nov 2025 23:39:50 +0900 Subject: [PATCH 06/11] =?UTF-8?q?feat:=20InputManager=20=ED=81=B4=EB=9E=98?= =?UTF-8?q?=EC=8A=A4=20=EA=B5=AC=ED=98=84=20(=EC=9E=85=EB=A0=A5=20?= =?UTF-8?q?=EB=B0=9B=EA=B8=B0)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/InputManager.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 src/InputManager.js diff --git a/src/InputManager.js b/src/InputManager.js new file mode 100644 index 000000000..07fde10d0 --- /dev/null +++ b/src/InputManager.js @@ -0,0 +1,16 @@ +import { Console } from "@woowacourse/mission-utils"; +import { Validator } from "./Validator.js"; + +export class InputManager { + static async readInputNumber(msg) { + const inputStr = await Console.readLineAsync(`${msg}`); + Validator.validateNumber(inputStr); + return Number(inputStr.trim()); + } + + static async readInputNumberWithComma(msg) { + const input = await Console.readLineAsync(`${msg}`); + input.split(",").forEach((numStr) => Validator.validateNumber(numStr)); + return input.split(",").map((numStr) => Number(numStr.trim())); + } +} From c994e0ae5ed50113594a3abbbccc0a4e67204651 Mon Sep 17 00:00:00 2001 From: inaemin Date: Mon, 3 Nov 2025 23:39:54 +0900 Subject: [PATCH 07/11] =?UTF-8?q?feat:=20OutputManager=20=ED=81=B4?= =?UTF-8?q?=EB=9E=98=EC=8A=A4=20=EA=B5=AC=ED=98=84=20(=EA=B2=B0=EA=B3=BC?= =?UTF-8?q?=20=EC=B6=9C=EB=A0=A5)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/OutputManager.js | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 src/OutputManager.js diff --git a/src/OutputManager.js b/src/OutputManager.js new file mode 100644 index 000000000..034e780cc --- /dev/null +++ b/src/OutputManager.js @@ -0,0 +1,7 @@ +import { Console } from "@woowacourse/mission-utils"; + +export class OutputManager { + static print(msg) { + Console.print(`${msg}`); + } +} From 55b1d0ca2bdbcd72d505fb3c81720b41bddadb61 Mon Sep 17 00:00:00 2001 From: inaemin Date: Mon, 3 Nov 2025 23:39:57 +0900 Subject: [PATCH 08/11] =?UTF-8?q?feat:=20=EC=9C=A0=ED=8B=B8=EB=A6=AC?= =?UTF-8?q?=ED=8B=B0=20=ED=95=A8=EC=88=98=20=EA=B5=AC=ED=98=84=20(?= =?UTF-8?q?=EC=84=B8=EB=8B=A8=EC=9C=84=20=EC=BD=A4=EB=A7=88=20=ED=8F=AC?= =?UTF-8?q?=EB=A7=B7)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils.js | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 src/utils.js diff --git a/src/utils.js b/src/utils.js new file mode 100644 index 000000000..21802cd09 --- /dev/null +++ b/src/utils.js @@ -0,0 +1,3 @@ +export function formatCurrency(amount) { + return amount.toLocaleString("ko-KR"); +} From 405ad4af09f2db4121791c0f54a461f971b04c32 Mon Sep 17 00:00:00 2001 From: inaemin Date: Mon, 3 Nov 2025 23:40:00 +0900 Subject: [PATCH 09/11] =?UTF-8?q?feat:=20LottoManager=20=ED=81=B4=EB=9E=98?= =?UTF-8?q?=EC=8A=A4=20=EA=B5=AC=ED=98=84=20(=EB=A1=9C=EB=98=90=20?= =?UTF-8?q?=EC=83=9D=EC=84=B1,=20=EB=8B=B9=EC=B2=A8=EA=B8=88/=EC=88=98?= =?UTF-8?q?=EC=9D=B5=EB=A5=A0=20=EA=B3=84=EC=82=B0)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/LottoManager.js | 71 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 src/LottoManager.js diff --git a/src/LottoManager.js b/src/LottoManager.js new file mode 100644 index 000000000..493b3b62a --- /dev/null +++ b/src/LottoManager.js @@ -0,0 +1,71 @@ +import { Random } from "@woowacourse/mission-utils"; +import { + COST_PER_LOTTO, + MIN_WINNING_MATCH, + PRIZE_BY_MATCH_COUNT, +} from "./constants.js"; +import { OutputManager } from "./OutputManager.js"; +import { Validator } from "./Validator.js"; +import Lotto from "./Lotto.js"; +import { formatCurrency } from "./utils.js"; + +export class LottoManager { + #lottoAmount; + #lottoCount; + #lottos = []; + #prizeCount = {}; + #prizeAmount = 0; + + constructor(purchaseAmount) { + Validator.validatePurchaseAmount(purchaseAmount); + this.#lottoAmount = purchaseAmount; + this.#lottoCount = purchaseAmount / COST_PER_LOTTO; + OutputManager.print(`\n${this.#lottoCount}개를 구매했습니다.`); + this.#makeLotto(); + } + + #makeLotto() { + for (let i = 0; i < this.#lottoCount; i++) { + const lottoNumbers = [...Random.pickUniqueNumbersInRange(1, 45, 6)].sort( + (a, b) => a - b + ); + const newLotto = new Lotto(lottoNumbers); + this.#lottos.push(newLotto); + OutputManager.print(`[${newLotto.getNumbers().join(", ")}]`); + } + } + + calculateWinningStatistics(winningNumbers, bonusNumber) { + this.#calculatePrizeAmount(winningNumbers, bonusNumber); + this.#printStatistics(); + } + + #calculatePrizeAmount(winningNumbers, bonusNumber) { + for (let lotto of this.#lottos) { + const result = lotto.getMatchCount(winningNumbers, bonusNumber); + if (result < MIN_WINNING_MATCH) continue; + this.#prizeCount[result] = (this.#prizeCount[result] || 0) + 1; + this.#prizeAmount += PRIZE_BY_MATCH_COUNT.get(String(result)).prize; + } + } + + #printStatistics() { + const ROI = parseFloat( + ((this.#prizeAmount / this.#lottoAmount) * 100).toFixed(2) + ); + OutputManager.print(`\n당첨 통계`); + OutputManager.print(`---`); + this.#printPrizeDetails(); + OutputManager.print(`총 수익률은 ${ROI}%입니다.`); + } + + #printPrizeDetails() { + for (let [matchCount, { prize, label }] of PRIZE_BY_MATCH_COUNT) { + OutputManager.print( + `${label} (${formatCurrency(prize)}원) - ${ + this.#prizeCount[matchCount] || 0 + }개` + ); + } + } +} From 0243552e8d0079e3559e586cb54fc4c17bccdef1 Mon Sep 17 00:00:00 2001 From: inaemin Date: Mon, 3 Nov 2025 23:40:02 +0900 Subject: [PATCH 10/11] =?UTF-8?q?feat:=20App=20=ED=81=B4=EB=9E=98=EC=8A=A4?= =?UTF-8?q?=20=EA=B5=AC=ED=98=84=20(=ED=94=84=EB=A1=9C=EA=B7=B8=EB=9E=A8?= =?UTF-8?q?=20=ED=9D=90=EB=A6=84=20=EA=B4=80=EB=A6=AC)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.js | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/App.js b/src/App.js index 091aa0a5d..9a66f99ce 100644 --- a/src/App.js +++ b/src/App.js @@ -1,5 +1,27 @@ +import { InputManager } from "./InputManager.js"; +import { LottoManager } from "./LottoManager.js"; +import { OutputManager } from "./OutputManager.js"; +import { Validator } from "./Validator.js"; + class App { - async run() {} + async run() { + try { + const purchaseAmount = await InputManager.readInputNumber( + `구입금액을 입력해 주세요.\n` + ); + const lottoManager = new LottoManager(purchaseAmount); + const winningNumbers = await InputManager.readInputNumberWithComma( + `\n당첨 번호를 입력해 주세요.\n` + ); + const bonusNumber = await InputManager.readInputNumber( + `\n보너스 번호를 입력해 주세요.\n` + ); + Validator.validateWinningAndBonusNumbers(winningNumbers, bonusNumber); + lottoManager.calculateWinningStatistics(winningNumbers, bonusNumber); + } catch (error) { + OutputManager.print(`${error.message}`); + } + } } export default App; From f2ca2406281bb9948c09777aa5d161fdd72422a3 Mon Sep 17 00:00:00 2001 From: inaemin Date: Mon, 3 Nov 2025 23:44:08 +0900 Subject: [PATCH 11/11] =?UTF-8?q?test:=20=EB=8B=A8=EC=9C=84=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8=20=EC=B6=94=EA=B0=80=20(Lotto,=20LottoManage?= =?UTF-8?q?r)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- __tests__/LottoManagerTest.js | 91 +++++++++++++++++++++++++++++++++++ __tests__/LottoTest.js | 42 +++++++++++++++- src/LottoManager.js | 14 ++++++ 3 files changed, 145 insertions(+), 2 deletions(-) create mode 100644 __tests__/LottoManagerTest.js diff --git a/__tests__/LottoManagerTest.js b/__tests__/LottoManagerTest.js new file mode 100644 index 000000000..659c3ce86 --- /dev/null +++ b/__tests__/LottoManagerTest.js @@ -0,0 +1,91 @@ +import { LottoManager } from "../src/LottoManager.js"; +import { MissionUtils } from "@woowacourse/mission-utils"; + +describe("LottoManager 클래스 테스트", () => { + beforeEach(() => { + jest.spyOn(MissionUtils.Console, "print").mockImplementation(); + }); + + afterEach(() => { + jest.restoreAllMocks(); + }); + + test("구입금액이 1000원 미만이면 예외가 발생한다.", () => { + expect(() => { + new LottoManager(500); + }).toThrow("[ERROR]"); + }); + + test("구입금액이 1000원으로 나누어떨어지지 않으면 예외가 발생한다.", () => { + expect(() => { + new LottoManager(1500); + }).toThrow("[ERROR]"); + }); + + test("정상적인 구입금액으로 로또를 생성할 수 있다.", () => { + const lottoManager = new LottoManager(3000); + expect(lottoManager.getLottos().length).toBe(3); + }); + + test("구입한 로또 개수는 구입금액을 1000으로 나눈 값이다.", () => { + const lottoManager = new LottoManager(5000); + expect(lottoManager.getLottos().length).toBe(5); + }); +}); + +describe("당첨금 계산 테스트", () => { + beforeEach(() => { + jest.spyOn(MissionUtils.Console, "print").mockImplementation(); + jest.spyOn(MissionUtils.Random, "pickUniqueNumbersInRange").mockReturnValue([ + 1, 2, 3, 4, 5, 6, + ]); + }); + + afterEach(() => { + jest.restoreAllMocks(); + }); + + test("당첨이 없으면 상금이 0이다.", () => { + const lottoManager = new LottoManager(1000); + lottoManager.calculateWinningStatistics( + [40, 41, 42, 43, 44, 45], + 39 + ); + expect(lottoManager.getPrizeAmount()).toBe(0); + }); + + test("6개 일치시 최고 상금을 받는다.", () => { + const lottoManager = new LottoManager(1000); + lottoManager.calculateWinningStatistics([1, 2, 3, 4, 5, 6], 7); + expect(lottoManager.getPrizeAmount()).toBe(2000000000); + }); +}); + +describe("수익률 계산 테스트", () => { + beforeEach(() => { + jest.spyOn(MissionUtils.Console, "print").mockImplementation(); + jest.spyOn(MissionUtils.Random, "pickUniqueNumbersInRange").mockReturnValue([ + 1, 2, 3, 4, 5, 6, + ]); + }); + + afterEach(() => { + jest.restoreAllMocks(); + }); + + test("당첨이 없으면 수익률은 0이다.", () => { + const lottoManager = new LottoManager(1000); + lottoManager.calculateWinningStatistics( + [40, 41, 42, 43, 44, 45], + 39 + ); + expect(lottoManager.getROI()).toBe(0); + }); + + test("6개 일치시 수익률을 계산한다.", () => { + const lottoManager = new LottoManager(1000); + lottoManager.calculateWinningStatistics([1, 2, 3, 4, 5, 6], 7); + const roi = lottoManager.getROI(); + expect(roi).toBe(200000000); + }); +}); diff --git a/__tests__/LottoTest.js b/__tests__/LottoTest.js index 409aaf69b..f08079f45 100644 --- a/__tests__/LottoTest.js +++ b/__tests__/LottoTest.js @@ -7,12 +7,50 @@ describe("로또 클래스 테스트", () => { }).toThrow("[ERROR]"); }); - // TODO: 테스트가 통과하도록 프로덕션 코드 구현 test("로또 번호에 중복된 숫자가 있으면 예외가 발생한다.", () => { expect(() => { new Lotto([1, 2, 3, 4, 5, 5]); }).toThrow("[ERROR]"); }); - // TODO: 추가 기능 구현에 따른 테스트 코드 작성 + test("로또 번호의 개수가 6개보다 적으면 예외가 발생한다.", () => { + expect(() => { + new Lotto([1, 2, 3, 4, 5]); + }).toThrow("[ERROR]"); + }); + + test("정상적인 로또 번호를 생성할 수 있다.", () => { + const lotto = new Lotto([1, 2, 3, 4, 5, 6]); + expect(lotto.getNumbers()).toEqual([1, 2, 3, 4, 5, 6]); + }); + + test("6개 모두 일치하면 6을 반환한다.", () => { + const lotto = new Lotto([1, 2, 3, 4, 5, 6]); + const matchCount = lotto.getMatchCount([1, 2, 3, 4, 5, 6], 7); + expect(matchCount).toBe(6); + }); + + test("5개가 일치하고 보너스가 일치하면 '5+bonus'를 반환한다.", () => { + const lotto = new Lotto([1, 2, 3, 4, 5, 6]); + const matchCount = lotto.getMatchCount([1, 2, 3, 4, 5, 7], 6); + expect(matchCount).toBe("5+bonus"); + }); + + test("5개가 일치하고 보너스가 일치하지 않으면 5를 반환한다.", () => { + const lotto = new Lotto([1, 2, 3, 4, 5, 6]); + const matchCount = lotto.getMatchCount([1, 2, 3, 4, 5, 7], 8); + expect(matchCount).toBe(5); + }); + + test("3개가 일치하면 3을 반환한다.", () => { + const lotto = new Lotto([1, 2, 3, 4, 5, 6]); + const matchCount = lotto.getMatchCount([1, 2, 3, 7, 8, 9], 10); + expect(matchCount).toBe(3); + }); + + test("일치하는 번호가 없으면 0을 반환한다.", () => { + const lotto = new Lotto([1, 2, 3, 4, 5, 6]); + const matchCount = lotto.getMatchCount([7, 8, 9, 10, 11, 12], 13); + expect(matchCount).toBe(0); + }); }); diff --git a/src/LottoManager.js b/src/LottoManager.js index 493b3b62a..352e0f9cf 100644 --- a/src/LottoManager.js +++ b/src/LottoManager.js @@ -68,4 +68,18 @@ export class LottoManager { ); } } + + getLottos() { + return this.#lottos; + } + + getPrizeAmount() { + return this.#prizeAmount; + } + + getROI() { + return parseFloat( + ((this.#prizeAmount / this.#lottoAmount) * 100).toFixed(2) + ); + } }