-
Notifications
You must be signed in to change notification settings - Fork 323
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: 보너스 볼 추가 #996
base: baek0318
Are you sure you want to change the base?
Step3: 보너스 볼 추가 #996
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,32 @@ | ||
package lotto.domain | ||
|
||
@JvmInline | ||
value class LottoAnswer( | ||
private val answer: List<Int> | ||
data class LottoAnswer( | ||
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. LottoAnswer라고 하니 어떤 역할인지 떠오르지 않았습니다. |
||
private val answer: List<Int>, | ||
private val bonusNumber: Int | ||
Comment on lines
+4
to
+5
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 match(inputLottos: List<Lotto>): Map<Int, Int> { | ||
fun match(inputLottos: List<Lotto>): Map<MatchCount, Int> { | ||
return inputLottos | ||
.map { innerMatch(it) } | ||
.groupingBy { it } | ||
.eachCount() | ||
} | ||
|
||
private fun innerMatch(inputLotto: Lotto): Int { | ||
return inputLotto.numbers.count { outer -> isAnswerNumberMatch(outer) } | ||
private fun innerMatch(inputLotto: Lotto): MatchCount { | ||
val count = inputLotto.numbers.count { outer -> isAnswerNumberMatch(outer) } | ||
val isBonusMatch = isBonusMatch(inputLotto) | ||
return MatchCount.of(count, isBonusMatch) | ||
} | ||
|
||
private fun isBonusMatch(inputLotto: Lotto): Boolean { | ||
return inputLotto.numbers.find { bonusNumber == it } != null | ||
} | ||
|
||
private fun isAnswerNumberMatch(outer: Int): Boolean { | ||
return answer.find { outer == it } != null | ||
} | ||
|
||
companion object { | ||
fun create(answer: List<Int>) = LottoAnswer(answer) | ||
fun create(answer: List<Int>, bonusNumber: Int) = LottoAnswer(answer, bonusNumber) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package lotto.domain | ||
|
||
enum class MatchCount( | ||
val matchCount: Int, | ||
val isMatchBonus: Boolean | ||
) { | ||
ZERO(0, false), | ||
ONE(1, false), | ||
TWO(2, false), | ||
THREE(3, false), | ||
FOUR(4, false), | ||
FIVE(5, false), | ||
FIVE_WITH_BONUS(5, true), | ||
SIX(6, false); | ||
|
||
companion object { | ||
fun of(count: Int, isMatchBonus: Boolean): MatchCount { | ||
val matchCount = values().find { it.matchCount == count } ?: throw IllegalArgumentException("해당하는 매치 카운트가 없습니다.") | ||
if (matchCount == FIVE && isMatchBonus) { | ||
return FIVE_WITH_BONUS | ||
} | ||
return matchCount | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,25 +2,33 @@ package lotto.view | |
|
||
import lotto.domain.Lotto | ||
import lotto.domain.LottoResult | ||
import lotto.domain.MatchCount | ||
|
||
object OutputView { | ||
|
||
fun outputBuyResult(lottoCount: Int, lotteries: List<Lotto>) { | ||
println("${lottoCount}개를 구매했습니다.") | ||
lotteries.forEach { | ||
println(it.numbers) | ||
println(it.numbers.sorted()) | ||
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. 로또 번호에 대한 중복 검사도 없는 것 같습니다. |
||
} | ||
println() | ||
} | ||
|
||
fun outputLottoResult(result: LottoResult, strategy: Map<Int, Int>) { | ||
fun outputLottoResult(result: LottoResult, strategy: Map<MatchCount, Int>) { | ||
println() | ||
println("당첨 통계") | ||
println("---------") | ||
strategy.entries | ||
.sortedBy { it.key } | ||
.map { "${it.key}개 일치 (${it.value}원)- ${result.earnResult[it.key] ?: 0}개" } | ||
.sortedBy { it.key.matchCount } | ||
.map { outputLottoResultSeperate(it.key, it.value, result.earnResult) } | ||
.forEach { println(it) } | ||
println("총 수익률은 ${result.earningRate}입니다.") | ||
} | ||
|
||
private fun outputLottoResultSeperate(matchCount: MatchCount, amount: Int, earnResult: Map<MatchCount, Int>): String { | ||
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. 함수명은 보통 동사를 앞에 두죠. |
||
if (matchCount.isMatchBonus) { | ||
return "${matchCount.matchCount}개 일치, 보너스 볼 일치(${amount}원)- ${earnResult[matchCount] ?: 0}개" | ||
} | ||
return "${matchCount.matchCount}개 일치 (${amount}원)- ${earnResult[matchCount] ?: 0}개" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package lotto | ||
|
||
import lotto.domain.strategy.NumberGenerator | ||
|
||
object TestNumberGeneratorFive : NumberGenerator { | ||
override fun generate(size: Int): List<Int> = listOf(1, 2, 3, 4, 5, 7) | ||
} |
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.
이렇게 매핑을 하기보다 힌트에도 있듯이 Enum으로 관리해 보면 어떨까요?
그리고 지금처럼 컨트롤러에 이 로직이 있는 것이 적합한지도 고민해 보시면 좋을 것 같아요.