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
9 changes: 9 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"arrowParens": "always",
"singleQuote": true,
"semi": true,
"useTabs": false,
"tabWidth": 2,
"printWidth": 80,
"trailingComma": "all"
}
98 changes: 98 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,99 @@
# javascript-planetlotto-precourse

## 📌 개요

- 우테코 로또 발매기인 행성 로또를 구현한다

## ⌨️ 입력

- 로또 구입 금액을 입력받는다.

```
1000

```

- 당첨 번호를 입력받는다. 번호는 쉼표(,)를 기준으로 구분한다.

```
1,2,3,4,5

```

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

```
6

```

## 🖥 출력

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

```
2개를 구매했습니다.
[8, 11, 13, 21, 22]
[1, 3, 6, 14, 22]
```

- 당첨 내역을 출력한다.

```
당첨 통계
---
5개 일치 (100,000,000원) - 0개
4개 일치, 보너스 번호 일치 (10,000,000원) - 0개
4개 일치 (1,500,000원) - 0개
3개 일치, 보너스 번호 일치 (500,000원) - 0개
2개 일치, 보너스 번호 일치 (5,000원) - 1개
0개 일치 (0원) - 1개
```

- 예외 상황 시 에러 문구를 출력해야 한다. 단, 에러 문구는 "[ERROR]"로 시작해야 한다.

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

## 실행 결과 예시

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

2개를 구매했습니다.
[8, 11, 13, 21, 22]
[1, 3, 6, 14, 22]

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

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

당첨 통계
---
5개 일치 (100,000,000원) - 0개
4개 일치, 보너스 번호 일치 (10,000,000원) - 0개
4개 일치 (1,500,000원) - 0개
3개 일치, 보너스 번호 일치 (500,000원) - 0개
2개 일치, 보너스 번호 일치 (5,000원) - 1개
0개 일치 (0원) - 1개
```

## 🧩 구현 기능 목록

- 로또 구입 금액을 입력 받는다
- 로또 1장의 가격은 500원이다
- 당첨 번호와 보너스 번호를 입력받는다
- 사용자가 구매한 로또 번호와 당첨 번호를 비교한다
- 당첨 내역을 출력한다

## ⚠️ 예외 처리

-

## ➕ 기타

- 추가 조건
8 changes: 6 additions & 2 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import LottoController from './controller/LottoController.js';

class App {
async run() {}
async run() {
const manager = new LottoController();
await manager.runLottoMachine();
}
}

export default App;
20 changes: 20 additions & 0 deletions src/constants/format.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export const LOTTO_CONSTANTS = {
PRICE: 500,
MIN_NUMBER: 1,
MAX_NUMBER: 30,
NUMBER_COUNT: 5,
};
export const RANK = {
FIRST: 'FIRST',
SECOND: 'SECOND',
THIRD: 'THIRD',
FOURTH: 'FOURTH',
FIFTH: 'FIFTH',
};
export const PRICE_INFO = {
[RANK.FIFTH]: { key: 5, prize: 5000, text: '3개 일치' },
[RANK.FOURTH]: { key: 4, prize: 50000, text: '4개 일치' },
[RANK.THIRD]: { key: 3, prize: 1500000, text: '5개 일치' },
[RANK.SECOND]: { key: 2, prize: 30000000, text: '5개 일치, 보너스 볼 일치' },
[RANK.FIRST]: { key: 1, prize: 2000000000, text: '6개 일치' },
};
10 changes: 10 additions & 0 deletions src/constants/message.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export const ERROR_MESSAGE = {
INVALID_PURCHASE_AMOUNT: '1000원으로 나누어 떨어지는 정수를 입력해주세요.',

INVALID_LOTTO_COUNT: '로또 번호는 5개여야 합니다.',
INVALID_LOTTO_RANGE: '로또 번호는 1에서 30사이의 숫자 입니다.',
DUPLICATE_LOTTO_NUMBERS: '로또 번호는 중복될 수 없습니다.',

INVALID_BONUS_NUMBER_RANGE: '보너스 번호는 1에서 30사이의 숫자 입니다.',
DUPLICATE_BONUS_NUMBER: '보너스 번호는 당첨 번호와 중복될 수 없습니다.',
};
91 changes: 91 additions & 0 deletions src/controller/LottoController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { InputView, OutputView } from '../view.js';
import LottoResultCalculator from '../service/LottoResultCalculator.js';
import { LottoCreator } from '../util/LottoCreator.js';
import { Validation } from '../util/Validation.js';

export default class LottoController {
#lottos;
#winnerLotto;
#bonusNumber;
#lottoResultArray;
#lottoResult;
constructor() {
this.#lottos = [];
this.#lottoResultArray = [];
this.#lottoResult = new Map();
}

async runLottoMachine() {
const purchaseAmount = await this.#getValidPurchaseAmount();

const randomLotto = LottoCreator.setLotto(purchaseAmount);
this.#lottos = LottoCreator.LottoGenerator(randomLotto);
this.#printLotto(randomLotto);

this.#winnerLotto = await this.#getValidWinningLotto();
this.#bonusNumber = await this.#getValidBonusNumber();

this.#calculatorLotto();
this.#printResult(this.#lottoResult);
}

async #getValidPurchaseAmount() {
while (true) {
try {
const purchaseAmountNumber = await InputView.askAmount();
Validation.validateLottoCount(purchaseAmountNumber);
return purchaseAmountNumber;
} catch (error) {
OutputView.printErrorMessage(error.message);
}
}
}
async #getValidWinningLotto() {
while (true) {
try {
const winningNumberString = await InputView.askWinningLotto();
Validation.validateLottoNumber(winningNumberString);
return winningNumberString;
} catch (error) {
OutputView.printErrorMessage(error.message);
}
}
}
async #getValidBonusNumber() {
while (true) {
try {
const bonusNumberAsNumber = await InputView.askBonusNumber();
Validation.validateBonusNumber(bonusNumberAsNumber, this.#winnerLotto);
return bonusNumberAsNumber;
} catch (error) {
OutputView.printErrorMessage(error.message);
}
}
}
#calculatorLotto() {
this.#lottos.forEach((lotto) => {
this.#lottoResultArray.push(
LottoResultCalculator.getRank(
lotto,
this.#winnerLotto,
this.#bonusNumber,
),
);
});
this.#statisticsLotto();
}
#statisticsLotto() {
for (let rank = 0; rank <= 6; rank++) {
this.#lottoResult.set(rank, 0);
}
this.#lottoResultArray.forEach((rank) => {
this.#lottoResult.set(rank, this.#lottoResult.get(rank) + 1);
});
}
#printResult(lottoResult) {
OutputView.printResult(lottoResult);
}
#printLotto(randomLottos) {
OutputView.printPurchasedLottos(randomLottos);
}
}
56 changes: 56 additions & 0 deletions src/model/Lotto.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { LOTTO_CONSTANTS } from '../constants/format.js';
import { ERROR_MESSAGE } from '../constants/message.js';

class Lotto {
#numbers;

constructor(numbers) {
this.#validate(numbers);
this.#numbers = numbers;
}

#validate(numbers) {
this.#validateLength(numbers);
this.#validateRange(numbers);
this.#validateNoDuplicates(numbers);
}

#validateLength(numbers) {
if (numbers.length !== LOTTO_CONSTANTS.NUMBER_COUNT) {
throw new Error(ERROR_MESSAGE.INVALID_LOTTO_COUNT);
}
}
#validateRange(numbers) {
const isValid = (number) =>
Number.isInteger(number) &&
number >= LOTTO_CONSTANTS.MIN_NUMBER &&
number <= LOTTO_CONSTANTS.MAX_NUMBER;

if (numbers.some((number) => !isValid(number)))
throw new Error(ERROR_MESSAGE.INVALID_LOTTO_RANGE);
}

#validateNoDuplicates(numbers) {
const uniqueNumbers = new Set(numbers);
if (uniqueNumbers.size !== numbers.length)
throw new Error(ERROR_MESSAGE.DUPLICATE_LOTTO_NUMBERS);
}
// getNumber() {
// /*
// return this.#numbers 로 반환할 경우 원본 메모리주소를 그대로 반환하기 때문에 캡슐화가 깨질 수 있다.
// 따라서 스프레드 연산자[...array] 를 사용하여 복사본을 안전하게 return 할 수 있다.
// */
// return [...this.#numbers];
// }
matchCount(winningNumber) {
const matchNumber = this.#numbers.filter((number) =>
winningNumber.includes(number),
).length;
return matchNumber;
}
containBonusNumber(bonusNumber) {
return this.#numbers.includes(bonusNumber);
}
}

export default Lotto;
16 changes: 16 additions & 0 deletions src/service/LottoResultCalculator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export default class LottoResultCalculator {
static getRank(lottoNumbers, winningNumber, bonusNumber) {
return this.getTotalRank(
lottoNumbers.matchCount(winningNumber),
lottoNumbers.containBonusNumber(bonusNumber),
);
}
static getTotalRank(matchCount, hasBonus) {
if (matchCount === 5) return 1;
if (matchCount === 4 && hasBonus) return 2;
if (matchCount === 4) return 3;
if (matchCount === 3) return 4;
if (matchCount === 2) return 5;
return 0;
}
}
11 changes: 11 additions & 0 deletions src/service/randomNumbers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { LOTTO_CONSTANTS } from '../constants/format.js';
import { MissionUtils } from '@woowacourse/mission-utils';
const randomNumbers = () => {
const lottoNumbers = MissionUtils.Random.pickUniqueNumbersInRange(
LOTTO_CONSTANTS.MIN_NUMBER,
LOTTO_CONSTANTS.MAX_NUMBER,
LOTTO_CONSTANTS.NUMBER_COUNT,
);
return lottoNumbers;
};
export default randomNumbers;
25 changes: 25 additions & 0 deletions src/util/LottoCreator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import randomNumbers from '../service/randomNumbers.js';
import Lotto from '../model/Lotto.js';
import { LOTTO_CONSTANTS } from '../constants/format.js';

export const LottoCreator = {
setLotto(purchaseAmount) {
const lottoCount = purchaseAmount / LOTTO_CONSTANTS.PRICE;
let lottos = [];
for (let i = 0; i < lottoCount; i += 1) {
const numbers = sortNumber(randomNumbers());
lottos.push(numbers);
}
return lottos;
},
LottoGenerator(randomLotto) {
let lottoObjects = [];
randomLotto.forEach((lottos) => {
lottoObjects.push(new Lotto(lottos));
});
return lottoObjects;
},
};
const sortNumber = (numbers) => {
return [...numbers].sort((a, b) => a - b);
};
Loading