-
Notifications
You must be signed in to change notification settings - Fork 355
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
Step3 로또(2등) #1089
Step3 로또(2등) #1089
Changes from 16 commits
ab95179
615a38a
6c5edcb
7bdb298
04232d4
db421e6
ec0f6a3
116fd4c
91b0460
717b1db
53b8766
0dea87b
e14add0
647ca42
0c9b168
ebea44e
2c25c5b
21ee37c
1a96019
9cf802f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package lotto.controller | ||
|
||
import lotto.domain.DrawResult | ||
import lotto.domain.LottoMachine | ||
import lotto.domain.Money | ||
import lotto.domain.TicketMachine | ||
import lotto.domain.WinningLotto | ||
import lotto.view.InputView | ||
import lotto.view.ResultView | ||
|
||
fun main() { | ||
val purchaseAmount = Money(InputView.inputPurchaseAmount()) | ||
val tickets = TicketMachine.exchange(purchaseAmount) | ||
ResultView.printTicketCount(tickets) | ||
|
||
val purchaseLotteries = LottoMachine.purchase(tickets) | ||
ResultView.printLotteries(purchaseLotteries) | ||
|
||
val winningLotto = | ||
WinningLotto.create( | ||
InputView.inputWinningLottoNumbers(), | ||
InputView.inputWinningBonusNumber(), | ||
) | ||
val drawResult = DrawResult.from(winningLotto, purchaseLotteries) | ||
ResultView.printStatistic(purchaseAmount, drawResult) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package lotto.domain | ||
|
||
data class DrawResult(private val rankRewardLottoCountMap: Map<RankReward, LottoCount>) { | ||
fun getProfitRate(purchaseAmount: Money): Double { | ||
return getTotalReward() / purchaseAmount | ||
} | ||
|
||
fun getLottoCount(rankReward: RankReward): LottoCount { | ||
return rankRewardLottoCountMap[rankReward] ?: LottoCount(0) | ||
} | ||
|
||
private fun getTotalReward(): Money { | ||
return rankRewardLottoCountMap.entries.fold(Money.ZERO) { total: Money, (rankReward: RankReward, lottoCount: LottoCount) -> | ||
val totalRewardPerRank: Money = rankReward.money * lottoCount.count | ||
total + totalRewardPerRank | ||
} | ||
} | ||
|
||
companion object { | ||
fun from( | ||
winningLotto: WinningLotto, | ||
purchaseLotteries: List<Lotto>, | ||
): DrawResult { | ||
val rankRewardLottoCountMap = | ||
purchaseLotteries | ||
.map { lotto -> | ||
val matchedCount = winningLotto.countMatchedNumber(lotto) | ||
val matchBonus = winningLotto.matchBonus(lotto) | ||
RankReward.valueOf(matchedCount, matchBonus) | ||
} | ||
.groupingBy { it } | ||
.eachCount() | ||
.mapValues { LottoCount(it.value) } | ||
return DrawResult(rankRewardLottoCountMap) | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package lotto.domain | ||
|
||
data class Lotto(val lottoNumbers: LottoNumbers) { | ||
override fun toString(): String { | ||
return "$lottoNumbers" | ||
} | ||
|
||
companion object { | ||
fun create(): Lotto { | ||
return Lotto(LottoNumbers.create()) | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package lotto.domain | ||
|
||
@JvmInline | ||
value class LottoCount(val count: Int) { | ||
override fun toString(): String { | ||
return "$count" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package lotto.domain | ||
|
||
object LottoMachine { | ||
fun purchase(tickets: List<Ticket>): List<Lotto> { | ||
return List(tickets.size) { Lotto.create() } | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package lotto.domain | ||
|
||
@JvmInline | ||
value class LottoNumber(val number: Int) { | ||
init { | ||
require(number in LOTTO_NUMBER_RANGE) { "로또 번호는 $LOTTO_MIN_NUMBER 부터 $LOTTO_MAX_NUMBER 사이어야 합니다" } | ||
} | ||
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. 요것도 값 객체로 감싸주셨군요 👍 |
||
|
||
override fun toString(): String { | ||
return "$number" | ||
} | ||
|
||
companion object { | ||
private const val LOTTO_MIN_NUMBER = 1 | ||
private const val LOTTO_MAX_NUMBER = 45 | ||
private val LOTTO_NUMBER_RANGE = LOTTO_MIN_NUMBER..LOTTO_MAX_NUMBER | ||
|
||
val ALL = LOTTO_NUMBER_RANGE.map { LottoNumber(it) } | ||
|
||
fun create(): LottoNumber { | ||
return LottoNumber(LOTTO_NUMBER_RANGE.random()) | ||
} | ||
Comment on lines
+24
to
+26
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. 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. 네! 좋은 의견감사해요😊 |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package lotto.domain | ||
|
||
class LottoNumbers(numbers: Set<LottoNumber>) { | ||
val numbers: Set<LottoNumber> | ||
|
||
init { | ||
require(numbers.size == LOTTO_NUMBER_COUNT) { "로또 번호는 $LOTTO_NUMBER_COUNT 개이어야 합니다" } | ||
this.numbers = numbers.sortedBy { it.number }.toSet() | ||
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. LottoNumber가 Comparable을 구현해도 좋을 것 같습니다! 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. 네! 말씀하신대로 구현해볼게요😄 |
||
} | ||
|
||
fun countMatchedNumber(other: LottoNumbers): Int { | ||
return (numbers intersect other.numbers).size | ||
} | ||
|
||
fun containsNumber(other: LottoNumber): Boolean { | ||
return numbers.contains(other) | ||
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.
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. 네! 좋은 키워드 알려주셔서 감사해요😊 반영하도록 하겠습니다!👍 |
||
} | ||
|
||
override fun toString(): String { | ||
return "$numbers" | ||
} | ||
|
||
companion object { | ||
private const val LOTTO_NUMBER_COUNT = 6 | ||
|
||
fun create(): LottoNumbers { | ||
return LottoNumbers(LottoNumber.ALL.shuffled().take(LOTTO_NUMBER_COUNT).toSet()) | ||
} | ||
|
||
fun from(numbers: Set<Int>): LottoNumbers { | ||
return LottoNumbers(numbers.map { LottoNumber(it) }.toSet()) | ||
} | ||
} | ||
} |
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.
Winninglotto로 감싸둔 덕분에 깔끔하네요 👍
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.
이전 단계에서 피드백 주신대로 해보았는데, 이번 단계 구현할 때 조금 더 깔끔해짐을 느꼈어요!
좋은 피드백 감사합니다🙇♂️🙂