Skip to content

Commit

Permalink
refactor: LottoResult -> MatchResult 로 명칭을 바꾸었다.
Browse files Browse the repository at this point in the history
  • Loading branch information
giwankim committed Nov 28, 2024
1 parent 01b6032 commit 9fa0848
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/main/kotlin/lotto/domain/Lotto.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,19 @@ class Lotto(
val numberOfLines: Int
get() = lines.size

fun match(winner: WinningLine): LottoResult {
fun match(winner: WinningLine): MatchResult {
val rankToCount =
lines
.groupBy { it.match(winner) }
.mapValues { (_, value) -> value.size }
return LottoResult.from(rankToCount)
return MatchResult.from(rankToCount)
}

fun merge(lotto: Lotto): Lotto = Lotto(lines + lotto.lines)

companion object {
fun from(vararg lines: LottoLine): Lotto = Lotto(lines.toList())

fun from(lines: List<List<Int>>): Lotto = Lotto(lines.map(LottoLine::from))
}
}
17 changes: 17 additions & 0 deletions src/main/kotlin/lotto/domain/MatchResult.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package lotto.domain

import java.util.EnumMap

data class MatchResult(
private val rankToCount: EnumMap<Rank, Int>,
) {
val totalPrize: Int = rankToCount.entries.sumOf { it.key.prize * it.value }

fun get(rank: Rank): Int = rankToCount.getOrDefault(rank, 0)

companion object {
fun from(rankToCount: Map<Rank, Int>): MatchResult = MatchResult(EnumMap(rankToCount))

fun of(vararg counts: Pair<Rank, Int>): MatchResult = from(counts.toMap())
}
}
2 changes: 1 addition & 1 deletion src/test/kotlin/lotto/domain/LottoTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class LottoTest {
val result = lotto.match(winner)

val expected =
LottoResult.of(
MatchResult.of(
Rank.FIRST to 1,
Rank.SECOND to 1,
Rank.THIRD to 1,
Expand Down
19 changes: 19 additions & 0 deletions src/test/kotlin/lotto/domain/MatchResultTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package lotto.domain

import io.kotest.matchers.shouldBe
import org.junit.jupiter.api.Test

@Suppress("NonAsciiCharacters")
class MatchResultTest {
@Test
fun `총상금을 계산한다`() {
val result =
MatchResult.of(
Rank.FOURTH to 1,
Rank.FIFTH to 1,
Rank.MISS to 12,
)

result.totalPrize shouldBe 55_000
}
}

0 comments on commit 9fa0848

Please sign in to comment.