-
Notifications
You must be signed in to change notification settings - Fork 3
[로또] 박현규 미션 제출합니다. #1
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
ParkHyeonkyu
wants to merge
7
commits into
Java-JavaScript-Language-Stuty:main
Choose a base branch
from
ParkHyeonkyu:ParkHyeonkyu
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
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c9a9008
feat:Lotto 클래스 구현
ParkHyeonkyu 8876f40
feat: 로또 구매 구현
ParkHyeonkyu 05d8d6a
feat: 로또 생성 구현
ParkHyeonkyu 9a5c6f3
feat: 번호입력기능구현
ParkHyeonkyu e4c74fe
feat:수익률 계산 구현
ParkHyeonkyu d23eacb
feat: PrizeCheck 클래스 분리
ParkHyeonkyu 262a490
refactor: 함수 중복 제거
ParkHyeonkyu 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,16 @@ | ||
# javascript-lotto-precourse | ||
|
||
## 기능 구현 | ||
1. 사용자로부터 로또 구입 금액을 입력한다. | ||
금액은 1000원 단위로 입력받으며 1000원으로 나누어 떨어지지 않으면 예외처리 | ||
2. 구입 금액에 따른 로또 수량에 맞춰 랜덤으로 로또 번호가 6개 생성된다. | ||
로또 번호의 범위는 1~45이며, 중복되지 않는다. | ||
3. 당첨 번호를 입력받는다. 번호는 쉼표(,)를 기준으로 구분한다. | ||
당첨 번호 추첨 시 중복되지 않는 숫자 6개와 보너스 번호 1개를 뽑는다. | ||
4. 사용자가 구매한 로또 번호와 당첨 번호를 비교하여 당첨 내역 및 수익률을 출력하고 종료한다. | ||
수익률을 계산하는 PrizeCheck 클래스를 만들어 코드 분리를 시도해본다. | ||
|
||
## 예외 처리 | ||
1. 당첨 번호가 6개가 아닌 경우 | ||
2. 로또 번호의 범위가 벗어난 경우 | ||
3. 로또 번호가 중복된 경우 |
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
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
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,80 @@ | ||
import { Console } from "@woowacourse/mission-utils"; | ||
import Lotto from "./Lotto.js"; | ||
import PrizeChecker from "./PrizeChecker.js"; | ||
|
||
class App { | ||
async run() {} | ||
async run() { | ||
try { | ||
const purchaseAmount = await this.getPurchaseAmount(); | ||
const lottoCount = purchaseAmount / 1000; | ||
const lottos = this.generateLottos(lottoCount); | ||
|
||
this.printLottoPurchaseDetails(lottoCount, lottos); | ||
|
||
const winningNumbers = await this.getWinningNumbers(); | ||
const bonusNumber = await this.getBonusNumber(); | ||
|
||
this.printResultsAndRevenueRate(purchaseAmount, lottos, winningNumbers, bonusNumber); | ||
} catch (error) { | ||
Console.print(error.message); | ||
} | ||
} | ||
|
||
async getUserInput(message) { | ||
const input = await Console.readLineAsync(message); | ||
return input.trim(); | ||
} | ||
|
||
async getPurchaseAmount() { | ||
const input = await this.getUserInput(`구입 금액을 입력해주세요.\n`); | ||
const amount = parseInt(input); | ||
this.validatePurchaseAmount(amount); | ||
return amount; | ||
} | ||
|
||
validatePurchaseAmount(amount) { | ||
if (isNaN(amount) || amount % 1000 !== 0) { | ||
throw new Error("[ERROR] 천 원 단위로 입력해야 합니다."); | ||
} | ||
} | ||
|
||
generateLottos(lottoCount) { | ||
return Array.from({ length: lottoCount }, () => Lotto.generateRandomLotto()); | ||
} | ||
|
||
async getWinningNumbers() { | ||
const input = await this.getUserInput("당첨 번호를 입력해주세요. 번호는 쉼표(,)로 구분됩니다.\n"); | ||
const numbers = input.split(",").map(num => parseInt(num.trim())); | ||
const winningLotto = Lotto.validateWinningNumbers(numbers); | ||
return winningLotto.getNumbers(); | ||
} | ||
|
||
async getBonusNumber() { | ||
const input = await this.getUserInput("보너스 번호를 입력해주세요.\n"); | ||
const bonusNumber = parseInt(input); | ||
Lotto.validateBonusNumber(bonusNumber); | ||
return bonusNumber; | ||
} | ||
|
||
printLottoPurchaseDetails(lottoCount, lottos) { | ||
Console.print(`${lottoCount}개를 구매했습니다.`); | ||
lottos.forEach(lotto => Console.print(`[${lotto.getNumbers().join(", ")}]`)); | ||
} | ||
|
||
printResultsAndRevenueRate(purchaseAmount, lottos, winningNumbers, bonusNumber) { | ||
const result = PrizeChecker.checkResults(lottos, winningNumbers, bonusNumber); | ||
this.printResultDetails(result); | ||
const revenueRate = PrizeChecker.calculateRevenueRate(purchaseAmount, result); | ||
Console.print(`총 수익률은 ${revenueRate}%입니다.`); | ||
} | ||
|
||
printResultDetails(result) { | ||
Console.print(`3개 일치 (5,000원) - ${result['3개 일치']}개`); | ||
Console.print(`4개 일치 (50,000원) - ${result['4개 일치']}개`); | ||
Console.print(`5개 일치 (1,500,000원) - ${result['5개 일치']}개`); | ||
Console.print(`5개 일치, 보너스 볼 일치 (30,000,000원) - ${result['5개 일치 (보너스 볼 일치)']}개`); | ||
Console.print(`6개 일치 (2,000,000,000원) - ${result['6개 일치']}개`); | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. App.js 부분에서 사용자 UI 부분만 깔끔하게 정리해서 작성하면 좀 더 코드 분리가 잘 될 것 같습니다! 예외처리문 까지 App에 넣을 필요는 없을 것 같아요! |
||
|
||
export default App; | ||
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
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,52 @@ | ||
//수익률 계산 | ||
class PrizeChecker { | ||
static checkResults(lottos, winningNumbers, bonusNumber) { | ||
const result = { | ||
'6개 일치': 0, | ||
'5개 일치 (보너스 볼 일치)': 0, | ||
'5개 일치': 0, | ||
'4개 일치': 0, | ||
'3개 일치': 0 | ||
}; | ||
|
||
lottos.forEach(lotto => { | ||
const matchingNumbers = this.getMatchingCount(lotto.getNumbers(), winningNumbers); | ||
this.updateResult(result, matchingNumbers, lotto.getNumbers(), bonusNumber); | ||
}); | ||
|
||
return result; | ||
} | ||
|
||
static getMatchingCount(lottoNumbers, winningNumbers) { | ||
return lottoNumbers.filter(number => winningNumbers.includes(number)).length; | ||
} | ||
|
||
static updateResult(result, matchingNumbers, lottoNumbers, bonusNumber) { | ||
if (matchingNumbers === 6) result['6개 일치']++; | ||
if (matchingNumbers === 5 && lottoNumbers.includes(bonusNumber)) { | ||
result['5개 일치 (보너스 볼 일치)']++; | ||
} | ||
if (matchingNumbers === 5) result['5개 일치']++; | ||
if (matchingNumbers === 4) result['4개 일치']++; | ||
if (matchingNumbers === 3) result['3개 일치']++; | ||
} | ||
|
||
static calculateRevenueRate(purchaseAmount, result) { | ||
const totalPrizeMoney = this.calculateTotalPrizeMoney(result); | ||
return ((totalPrizeMoney / purchaseAmount) * 100).toFixed(1); | ||
} | ||
|
||
static calculateTotalPrizeMoney(result) { | ||
const prizeMoney = { | ||
'3개 일치': 5000, | ||
'4개 일치': 50000, | ||
'5개 일치': 1500000, | ||
'5개 일치 (보너스 볼 일치)': 30000000, | ||
'6개 일치': 2000000000 | ||
}; | ||
|
||
return Object.entries(result).reduce((total, [key, value]) => total + value * prizeMoney[key], 0); | ||
} | ||
} | ||
|
||
export default PrizeChecker; |
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,4 +1,4 @@ | ||
import App from "./App.js"; | ||
|
||
const app = new App(); | ||
await app.run(); | ||
await app.run(); |
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.
여기 아래 부분부터는 PrizeChecker 클래스가 아니라 왜 App 클래스에 구현하신 건지 궁금합니다! 개인적으로 수익률 계산하는 곳에 묶어 놓았으면 더 보기 좋지 않을까 싶어서