-
Notifications
You must be signed in to change notification settings - Fork 363
[로또] 김지환 미션 제출합니다. #386
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
Open
Turtle-Hwan
wants to merge
17
commits into
woowacourse-precourse:main
Choose a base branch
from
Turtle-Hwan:Turtle-Hwan
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
[로또] 김지환 미션 제출합니다. #386
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
d18b4cc
chore: setting eslint config
Turtle-Hwan c4efb34
feat: App 기본 구조 정리
Turtle-Hwan c8a1350
feat: MESSAGES 상수 정리
Turtle-Hwan de299f3
feat: MESSAGES 입출력 메세지 추가
Turtle-Hwan 8dd01c5
docs: add readme
Turtle-Hwan 967e55f
로또 구입 금액 입력 기능
Turtle-Hwan 3a90053
feat: message const 분리
Turtle-Hwan 42f3204
test: LottoVendingMachine 단위 테스트 추가
Turtle-Hwan 0f17e79
feat: LottoVendingMachine class 분리
Turtle-Hwan a2bb8d5
feat: lotto 번호 발행 기능
Turtle-Hwan e842ce4
feat: 발행한 로또 수량 및 번호
Turtle-Hwan 4d1dab0
feat: 당첨 번호 입력 기능
Turtle-Hwan edb884e
feat: Lotto 당첨 번호 판별 기능 추가
Turtle-Hwan 1d2a34b
feat: 보너스 번호 입력 기능
Turtle-Hwan a77ef29
feat: 수익률 및 맞춘 개수 기능
Turtle-Hwan d089476
feat: 수익률 및 맞춘 개수 기능
Turtle-Hwan 7a221c3
fix: add await
Turtle-Hwan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,37 @@ | ||
| # javascript-lotto-precourse | ||
|
|
||
| ## 기능 목록 - turtlehwan | ||
|
|
||
| - [x] 로또 구입 금액 입력 기능 | ||
| - [x] 예외 : 정수가 아닌 숫자입니다. | ||
| - [x] 예외 : 정수가 1,000 미만입니다. | ||
| - [x] 예외 : 입력 받은 수가 1,000 단위가 아닙니다. | ||
| - [x] 로또 번호 발행 기능 | ||
| - [x] 1~45 정수 숫자 범위 -> MissionUtils | ||
| - [x] 중복되지 않는 6개 숫자 발행 -> MissionUtils | ||
| - [x] 발행한 로또 수량 및 번호 출력 (오름차순) | ||
| - [x] 당첨 번호 입력 기능 | ||
| - [x] 양의 정수 판별 | ||
| - [x] 1~45 숫자 범위 | ||
| - [x] 중복되지 않는 6개의 숫자 입력 | ||
| - [x] 보너스 번호 입력 기능 | ||
| - [x] 양의 정수 판별 | ||
| - [x] 1~45 숫자 범위 | ||
| - [x] 중복되지 않는 6개의 숫자 입력 | ||
| - [x] 당첨 확인(등수 판별) 기능 | ||
| - [x] 당첨 내역 확인 및 출력 | ||
| - [x] 수익률 계산 및 출력 | ||
| - [x] 소수점 둘째 자리에서 반올림 | ||
|
|
||
| ## 중점 사항 | ||
|
|
||
| - 값을 하드코딩하지 않고, 상수로 빼내서 정리하기 | ||
| - 메서드가 한 가지 기능을 하는지 확인하는 기준 세우기 | ||
| - 여러 메서드에서 중복되는 코드가 있으면 별도의 메서드로 분리 | ||
| - 해당 메서드를 변경할 때 서로 다른 이유로 변경하고 commit하는지 살펴보기 | ||
| - 안내 문구 출력, 사용자 입력 처리, 유효값 검증 등의 작업을 각기 다른 함수로 분리 | ||
| - class의 private 필드 잘 이용하기 (# 문법) | ||
| - JavaScript에서 객체를 만드는 다양한 방법을 이해하고 사용하기 | ||
| - 기능별 단위 테스트 만들기 | ||
| - 테스트를 작성하는 이유에 대해 본인의 경험을 토대로 정리 | ||
| - 처음부터 큰 단위의 테스트를 만들지 않기 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| import { MissionUtils } from "@woowacourse/mission-utils"; | ||
| import LottoVendingMachine from "../src/LottoVendingMachine.js"; | ||
|
|
||
| const mockQuestions = (inputs) => { | ||
| MissionUtils.Console.readLineAsync = jest.fn(); | ||
|
|
||
| MissionUtils.Console.readLineAsync.mockImplementation(() => { | ||
| const input = inputs.shift(); | ||
|
|
||
| return Promise.resolve(input); | ||
| }); | ||
| }; | ||
|
|
||
| const getLogSpy = () => { | ||
| const logSpy = jest.spyOn(MissionUtils.Console, "print"); | ||
| logSpy.mockClear(); | ||
| return logSpy; | ||
| }; | ||
|
|
||
| const runException = async (input) => { | ||
| // given | ||
| const logSpy = getLogSpy(); | ||
|
|
||
| const INPUT_NUMBERS_TO_END = ["1000", "1,2,3,4,5,6", "7"]; | ||
| mockQuestions([input, ...INPUT_NUMBERS_TO_END]); | ||
|
|
||
| // when | ||
| const LottoVM = new LottoVendingMachine(); | ||
| await LottoVM.purchaseLottoAmountInput(); | ||
|
|
||
| // then | ||
| expect(logSpy).toHaveBeenCalledWith(expect.stringContaining("[ERROR]")); | ||
| }; | ||
|
|
||
| describe("LottoVendingMachine 클래스 테스트", () => { | ||
| beforeEach(() => { | ||
| jest.restoreAllMocks(); | ||
| }); | ||
|
|
||
| test("금액이 정수가 아니면 예외가 발생한다.", async () => { | ||
| await runException("1000.5"); | ||
| }); | ||
|
|
||
| test("금액이 1000원 미만이면 예외가 발생한다.", async () => { | ||
| await runException("500"); | ||
| }); | ||
|
|
||
| test("금액이 1000원 단위가 아니면 예외가 발생한다.", async () => { | ||
| await runException("1521"); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| import babelParser from '@babel/eslint-parser'; | ||
| import { FlatCompat } from '@eslint/eslintrc'; | ||
| import pluginJs from '@eslint/js'; | ||
| import prettier from 'eslint-config-prettier'; | ||
| import globals from 'globals'; | ||
| import path from 'path'; | ||
| import { fileURLToPath } from 'url'; | ||
|
|
||
| const filename = fileURLToPath(import.meta.url); | ||
| const dirname = path.dirname(filename); | ||
|
|
||
| const compat = new FlatCompat({ | ||
| baseDirectory: dirname, | ||
| }); | ||
|
|
||
| export default [ | ||
| { | ||
| languageOptions: { | ||
| ecmaVersion: 'latest', | ||
| sourceType: 'module', | ||
| parser: babelParser, | ||
| parserOptions: { | ||
| requireConfigFile: false, | ||
| }, | ||
| globals: { | ||
| ...globals.node, | ||
| ...globals.jest, | ||
| }, | ||
| }, | ||
| }, | ||
| ...compat.extends('eslint-config-airbnb-base'), | ||
| prettier, | ||
| { | ||
| files: ['/src//.js', '/tests//.js', '/tests//*.js'], | ||
| rules: { | ||
| 'max-depth': ['error', 2], | ||
| 'max-params': ['error', 3], | ||
| 'max-lines-per-function': ['error', { max: 15 }], | ||
| 'import/extensions': ['error', 'ignorePackages'], | ||
| 'import/prefer-default-export': 'off', | ||
| 'class-methods-use-this': 'off', | ||
| 'no-plusplus': ['error', { allowForLoopAfterthoughts: true }], | ||
| 'no-console': 'off', | ||
| 'no-unused-vars': 'error', | ||
| 'prefer-const': 'error', | ||
| 'no-var': 'error', | ||
| eqeqeq: ['error', 'always'], | ||
| }, | ||
| }, | ||
| { | ||
| files: ['eslint.config.js'], | ||
| rules: { | ||
| 'no-underscore-dangle': 'off', | ||
| 'import/no-extraneous-dependencies': ['error', { packageDir: __dirname }], | ||
| }, | ||
| }, | ||
| ]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,10 @@ | ||
| import LottoVendingMachine from "./LottoVendingMachine.js"; | ||
|
|
||
| class App { | ||
| async run() {} | ||
| async run() { | ||
| const LottoVM = new LottoVendingMachine(); | ||
| await LottoVM.run(); | ||
| } | ||
| } | ||
|
|
||
| export default App; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,18 +1,53 @@ | ||
| import { MAGIC_NUMBER } from "./constants/magicNumber.js"; | ||
| import { MESSAGES } from "./constants/messages.js"; | ||
|
|
||
| class Lotto { | ||
| #numbers; | ||
|
|
||
| constructor(numbers) { | ||
| this.#validate(numbers); | ||
| this.#validateDuplicate(numbers); | ||
| this.#numbers = numbers; | ||
| } | ||
|
|
||
| #validate(numbers) { | ||
| if (numbers.length !== 6) { | ||
| throw new Error("[ERROR] 로또 번호는 6개여야 합니다."); | ||
| throw new Error( | ||
| MESSAGES.ERROR.PREFIX + | ||
| MESSAGES.ERROR.LOTTO_PICK_NUM(MAGIC_NUMBER.LOTTO_PICK_NUM) | ||
| ); | ||
| } | ||
|
|
||
| numbers.forEach((number) => { | ||
| if (!Number.isInteger(Number(number))) { | ||
| throw new Error(MESSAGES.ERROR.PREFIX + MESSAGES.ERROR.NOT_INT); | ||
| } | ||
| this.#validateIntsRange( | ||
| number, | ||
| MAGIC_NUMBER.LOTTO_MIN_NUM, | ||
| MAGIC_NUMBER.LOTTO_MAX_NUM | ||
| ); | ||
| }); | ||
| } | ||
|
|
||
| #validateIntsRange(number, min, max) { | ||
| if (Number(number) < min || Number(number) > max) { | ||
| throw new Error( | ||
| MESSAGES.ERROR.PREFIX + MESSAGES.ERROR.RANGE_INT(min, max) | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| // TODO: 추가 기능 구현 | ||
| #validateDuplicate(numbers) { | ||
| const set = new Set(numbers); | ||
| if (numbers.length !== set.size) { | ||
| throw new Error(MESSAGES.ERROR.PREFIX + MESSAGES.ERROR.DUPLICATE_INT); | ||
| } | ||
| } | ||
|
|
||
| getLottoNumbers() { | ||
| return this.#numbers; | ||
| } | ||
| } | ||
|
|
||
| export default Lotto; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
validate안에 validateIntsRange 는 validate안에 넣었는데 validateDuplicate를 따로 하신 이유가 있을까요?!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.