Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 120 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,121 @@
# javascript-lotto-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,000원 단위로 입력 받으며 1,000원으로 나누어 떨어지지 않는 경우 예외 처리한다.

```
14000

```

- 보너스 번호를 입력 받는다.

```
7

```

### 2) 출력

- 발행한 로또 수량 및 번호를 출력한다. 로또 번호는 오름차순으로 정렬하여 보여준다.

```
8개를 구매했습니다.
[8, 21, 23, 41, 42, 43]
[3, 5, 11, 16, 32, 38]
[7, 11, 16, 35, 36, 44]
[1, 8, 11, 31, 41, 42]
[13, 14, 16, 38, 42, 45]
[7, 11, 30, 40, 42, 43]
[2, 13, 22, 32, 38, 45]
[1, 3, 5, 14, 22, 45]

```

- 당첨 내역을 출력한다.

```
3개 일치 (5,000원) - 1개
4개 일치 (50,000원) - 0개
5개 일치 (1,500,000원) - 0개
5개 일치, 보너스 볼 일치 (30,000,000원) - 0개
6개 일치 (2,000,000,000원) - 0개

```

- 수익률은 소수점 둘째 자리에서 반올림한다. (ex. 100.0%, 51.5%, 1,000,000.0%)

```
총 수익률은 62.5%입니다.

```

### 3) 에러 처리

- 사용자가 잘못된 값을 입력할 경우 "[ERROR]"로 시작하는 메시지 출력

```
[ERROR] 로또 번호는 1부터 45 사이의 숫자여야 합니다.

```

## ⭕실행 결과 예시

```
구입금액을 입력해 주세요.
8000

8개를 구매했습니다.
[8, 21, 23, 41, 42, 43]
[3, 5, 11, 16, 32, 38]
[7, 11, 16, 35, 36, 44]
[1, 8, 11, 31, 41, 42]
[13, 14, 16, 38, 42, 45]
[7, 11, 30, 40, 42, 43]
[2, 13, 22, 32, 38, 45]
[1, 3, 5, 14, 22, 45]

당첨 번호를 입력해 주세요.
1,2,3,4,5,6

보너스 번호를 입력해 주세요.
7

당첨 통계
---
3개 일치 (5,000원) - 1개
4개 일치 (50,000원) - 0개
5개 일치 (1,500,000원) - 0개
5개 일치, 보너스 볼 일치 (30,000,000원) - 0개
6개 일치 (2,000,000,000원) - 0개
총 수익률은 62.5%입니다.

```

## 🔎ETC

- @woowacourse/mission-utils에서 제공하는 Random 및 Console API를 사용하여 구현해야 한다.
- Random 값 추출은 Random. pickUniqueNumbersInRange()를 활용한다.
- 사용자의 값을 입력 및 출력하려면 Console.readLineAsync()와 Console.print()를 활용한다.
14 changes: 4 additions & 10 deletions __tests__/ApplicationTest.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import App from "../src/App.js";
import { MissionUtils } from "@woowacourse/mission-utils";

// Mock functions for console input and output
const mockQuestions = (inputs) => {
MissionUtils.Console.readLineAsync = jest.fn();

MissionUtils.Console.readLineAsync.mockImplementation(() => {
const input = inputs.shift();

return Promise.resolve(input);
});
};
Expand All @@ -24,21 +23,17 @@ const getLogSpy = () => {
return logSpy;
};

// Error test helper
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]"));
};

Expand All @@ -48,7 +43,6 @@ describe("로또 테스트", () => {
});

test("기능 테스트", async () => {
// given
const logSpy = getLogSpy();

mockRandoms([
Expand All @@ -63,11 +57,11 @@ describe("로또 테스트", () => {
]);
mockQuestions(["8000", "1,2,3,4,5,6", "7"]);

// when
const app = new App();
await app.run();

// then
console.log(logSpy.mock.calls.map((call) => call[0]));

const logs = [
"8개를 구매했습니다.",
"[8, 21, 23, 41, 42, 43]",
Expand Down
7 changes: 5 additions & 2 deletions __tests__/LottoTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@ 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, 6]);
}).not.toThrow();
});
});
165 changes: 164 additions & 1 deletion src/App.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,168 @@
import { Console, Random } from "@woowacourse/mission-utils";
import Lotto from "./Lotto.js";
import {
LOTTO_PRICE,
LOTTO_NUMBER_COUNT,
LOTTO_NUMBER_RANGE,
WINNING_PRIZES,
MATCH_COUNTS,
ERROR_MESSAGES,
} from "./constants.js";

class App {
async run() {}
run() {
this.inputPurchaseAmount();
}

async inputPurchaseAmount() {
const amount = await Console.readLineAsync("구입금액을 입력해 주세요.\n");
this.validatePurchaseAmount(amount);
}

validatePurchaseAmount(amount) {
const purchaseAmount = Number(amount);

if (Number.isNaN(purchaseAmount) || purchaseAmount % LOTTO_PRICE !== 0) {
this.handleError(ERROR_MESSAGES.INVALID_AMOUNT);
this.inputPurchaseAmount();
return;
}
this.purchaseAmount = purchaseAmount;
this.printPurchasedLotto();
}

printPurchasedLotto() {
const lottoCount = this.purchaseAmount / LOTTO_PRICE;
Console.print(`${lottoCount}개를 구매했습니다.`);
this.lottoTickets = this.generateLottoTickets(lottoCount);
this.displayLottoTickets();
this.inputWinningNumbers();
}

generateLottoTickets(count) {
return Array.from({ length: count }, () => {
const numbers = Random.pickUniqueNumbersInRange(1, 45, 6).sort((a, b) => a - b);
return new Lotto(numbers);
});
}

displayLottoTickets() {
this.lottoTickets.forEach((ticket) => {
Console.print(`[${ticket.getNumbers().join(", ")}]`);
});
}

async inputWinningNumbers() {
const winningNumbersInput = await Console.readLineAsync("\n당첨 번호를 입력해 주세요.\n");
const winningNumbers = winningNumbersInput.split(",").map(Number);
this.validateWinningNumbers(winningNumbers);
}

validateWinningNumbers(numbers) {
if (numbers.length !== LOTTO_NUMBER_COUNT || new Set(numbers).size !== LOTTO_NUMBER_COUNT) {
this.handleError(ERROR_MESSAGES.INVALID_LOTTO_COUNT);
this.inputWinningNumbers();
return;
}
if (
numbers.some((number) => number < LOTTO_NUMBER_RANGE.MIN || number > LOTTO_NUMBER_RANGE.MAX)
) {
this.handleError(ERROR_MESSAGES.INVALID_NUMBER_RANGE);
this.inputWinningNumbers();
return;
}
this.winningNumbers = numbers;
this.inputBonusNumber();
}

async inputBonusNumber() {
const bonusNumberInput = await Console.readLineAsync("\n보너스 번호를 입력해 주세요.\n");
const bonusNumber = Number(bonusNumberInput);
this.validateBonusNumber(bonusNumber);
}

validateBonusNumber(number) {
if (
Number.isNaN(number) ||
number < LOTTO_NUMBER_RANGE.MIN ||
number > LOTTO_NUMBER_RANGE.MAX
) {
this.handleError(ERROR_MESSAGES.INVALID_NUMBER_RANGE);
this.inputBonusNumber();
return;
}
if (this.winningNumbers.includes(number)) {
this.handleError(ERROR_MESSAGES.BONUS_NUMBER_DUPLICATE);
this.inputBonusNumber();
return;
}
this.bonusNumber = number;
this.calculateResults();
}

calculateResults() {
this.results = { 1: 0, 2: 0, 3: 0, 4: 0, 5: 0 };

this.lottoTickets.forEach((ticket) => {
const matchedCount = ticket
.getNumbers()
.filter((num) => this.winningNumbers.includes(num)).length;
const isBonusMatched = ticket.getNumbers().includes(this.bonusNumber);

if (matchedCount === MATCH_COUNTS.FIRST) {
this.results[1] += 1;
return;
}

if (matchedCount === MATCH_COUNTS.SECOND && isBonusMatched) {
this.results[2] += 1;
return;
}

if (matchedCount === MATCH_COUNTS.THIRD) {
this.results[3] += 1;
return;
}

if (matchedCount === MATCH_COUNTS.FOURTH) {
this.results[4] += 1;
return;
}

if (matchedCount === MATCH_COUNTS.FIFTH) {
this.results[5] += 1;
}
});

this.printResults();
}

printResults() {
Console.print("당첨 통계\n---");
Console.print(`3개 일치 (5,000원) - ${this.results[5]}개`);
Console.print(`4개 일치 (50,000원) - ${this.results[4]}개`);
Console.print(`5개 일치 (1,500,000원) - ${this.results[3]}개`);
Console.print(`5개 일치, 보너스 볼 일치 (30,000,000원) - ${this.results[2]}개`);
Console.print(`6개 일치 (2,000,000,000원) - ${this.results[1]}개`);

this.calculateProfit();
}

calculateProfit() {
const totalPrize =
this.results[1] * WINNING_PRIZES.FIRST +
this.results[2] * WINNING_PRIZES.SECOND +
this.results[3] * WINNING_PRIZES.THIRD +
this.results[4] * WINNING_PRIZES.FOURTH +
this.results[5] * WINNING_PRIZES.FIFTH;

const profitRate = ((totalPrize / this.purchaseAmount) * 100).toFixed(1);
Console.print(`총 수익률은 ${profitRate}%입니다.`);
}

handleError(message) {
Console.print(message);
}
}

export default App;
7 changes: 6 additions & 1 deletion src/Lotto.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@ class Lotto {
if (numbers.length !== 6) {
throw new Error("[ERROR] 로또 번호는 6개여야 합니다.");
}
if (new Set(numbers).size !== numbers.length) {
throw new Error("[ERROR] 로또 번호에는 중복된 숫자가 있을 수 없습니다.");
}
}

// TODO: 추가 기능 구현
getNumbers() {
return this.#numbers;
}
}

export default Lotto;
Loading