Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

. #622

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

. #622

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
64 changes: 32 additions & 32 deletions __tests__/ApplicationTest.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import App from "../src/App.js";
import { MissionUtils } from "@woowacourse/mission-utils";
import { MissionUtils } from '@woowacourse/mission-utils';
import App from '../src/App';

const mockQuestions = (inputs) => {
MissionUtils.Console.readLineAsync = jest.fn();
Expand All @@ -13,13 +13,14 @@ const mockQuestions = (inputs) => {

const mockRandoms = (numbers) => {
MissionUtils.Random.pickUniqueNumbersInRange = jest.fn();
numbers.reduce((acc, number) => {
return acc.mockReturnValueOnce(number);
}, MissionUtils.Random.pickUniqueNumbersInRange);
numbers.reduce(
(acc, number) => acc.mockReturnValueOnce(number),
MissionUtils.Random.pickUniqueNumbersInRange,
);
};

const getLogSpy = () => {
const logSpy = jest.spyOn(MissionUtils.Console, "print");
const logSpy = jest.spyOn(MissionUtils.Console, 'print');
logSpy.mockClear();
return logSpy;
};
Expand All @@ -28,8 +29,8 @@ 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"];
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]);
Expand All @@ -39,15 +40,15 @@ const runException = async (input) => {
await app.play();

// then
expect(logSpy).toHaveBeenCalledWith(expect.stringContaining("[ERROR]"));
}
expect(logSpy).toHaveBeenCalledWith(expect.stringContaining('[ERROR]'));
};

describe("로또 테스트", () => {
describe('로또 테스트', () => {
beforeEach(() => {
jest.restoreAllMocks();
})
});

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

Expand All @@ -61,38 +62,37 @@ describe("로또 테스트", () => {
[2, 13, 22, 32, 38, 45],
[1, 3, 5, 14, 22, 45],
]);
mockQuestions(["8000", "1,2,3,4,5,6", "7"]);
mockQuestions(['8000', '1,2,3,4,5,6', '7']);

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

// then
const logs = [
"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개",
"총 수익률은 62.5%입니다.",
'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개',
'총 수익률은 62.5%입니다.',
];

logs.forEach((log) => {
expect(logSpy).toHaveBeenCalledWith(expect.stringContaining(log));
});
});

test("예외 테스트", async () => {
await runException("1000j");
test('예외 테스트', async () => {
await runException('1000j');
});
});

12 changes: 6 additions & 6 deletions __tests__/LottoTest.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import Lotto from "../src/Lotto.js";
import Lotto from '../src/Lotto';

describe("로또 클래스 테스트", () => {
test("로또 번호의 개수가 6개가 넘어가면 예외가 발생한다.", () => {
describe('로또 클래스 테스트', () => {
test('로또 번호의 개수가 6개가 넘어가면 예외가 발생한다.', () => {
expect(() => {
new Lotto([1, 2, 3, 4, 5, 6, 7]);
}).toThrow("[ERROR]");
}).toThrow('[ERROR]');
});

// TODO: 이 테스트가 통과할 수 있게 구현 코드 작성
test("로또 번호에 중복된 숫자가 있으면 예외가 발생한다.", () => {
test('로또 번호에 중복된 숫자가 있으면 예외가 발생한다.', () => {
expect(() => {
new Lotto([1, 2, 3, 4, 5, 5]);
}).toThrow("[ERROR]");
}).toThrow('[ERROR]');
});

// 아래에 추가 테스트 작성 가능
Expand Down
24 changes: 24 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
## 필요 기능

- 구매 금액 입력 받는 함수
- 금액에 따라 복권 갯수 출력
- 갯수 만큼의 로또 번호 생성 및 출력
- 로또 번호는 오름차순으로 출력
- 당첨번호 입력
- 보너스 번호 입력
- 당첨 통계 출력
- 수익률 출력

## 로또 당첨 기준

```
- 로또 번호의 숫자 범위는 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원
```
16 changes: 15 additions & 1 deletion src/App.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
import MyConsole from './Console.js';
import utility from './utility.js';

class App {
async play() {}
constructor() {
this.console = new MyConsole();
}

async play() {
const input = await this.console.readLineAsync('구입금액을 입력해 주세요.');
utility.validateInput(input);
const money = utility.changeStringToNumber(input);
const amount = utility.getLottoAmount(money);
this.console.log(`${amount}개를 구매했습니다.`);
const lottos = utility.generateLottos(amount);
}
}

export default App;
19 changes: 19 additions & 0 deletions src/Console.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Console } from '@woowacourse/mission-utils';

export default class MyConsole {
log(value) {
Console.print(value);
return this;
}

async read(value = '') {
const input = await Console.readLineAsync(value);
return input;
}

async readLineAsync(value = '') {
Console.print(value);
await Console.readLineAsync('');
return this;
}
}
11 changes: 9 additions & 2 deletions src/Lotto.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
import MyConsole from './Console.js';

class Lotto {
#numbers;

constructor(numbers) {
this.console = new MyConsole();
this.#validate(numbers);
this.#numbers = numbers;
this.#numbers = [...numbers].sort((a, b) => a - b);
}

#validate(numbers) {
if (numbers.length !== 6) {
throw new Error("[ERROR] 로또 번호는 6개여야 합니다.");
throw new Error('[ERROR] 로또 번호는 6개여야 합니다.');
}
return this;
}

// TODO: 추가 기능 구현
printLotto() {
this.console.log(this.#numbers);
}
}

export default Lotto;
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import App from "./App.js";
import App from './App.js';

const app = new App();
await app.play();
36 changes: 36 additions & 0 deletions src/utility.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { Random } from '@woowacourse/mission-utils';
import Lotto from './Lotto.js';

const utility = {
changeStringToNumber: (str) => Number(str),
validateInput: (input) => {
const number = Number(input);
if (Number.isNaN(number)) {
throw new Error('[ERROR] 숫자를 입력해주세요.');
}
},
getLottoAmount: (money) => money / 1000,

generateLottos: (amount) => {
const lottos = [];
for (let i = 0; i < amount; i += 1) {
lottos.push(utility.generateLotto());
}
return lottos;
},

generateLotto: () => {
const numbers = [];
while (numbers.length < 6) {
const number = Random.pickUniqueNumbersInRange(1, 45, 6);
if (!numbers.includes(number)) {
numbers.push(number);
}
}
const lotto = new Lotto(numbers);
lotto.printLotto();
return lotto;
},
};

export default utility;
Empty file added src/validate.js
Empty file.