Skip to content

Commit aca6066

Browse files
committed
로또 생성 로직 리팩토링
1 parent b7ac43e commit aca6066

File tree

9 files changed

+50
-42
lines changed

9 files changed

+50
-42
lines changed

lotto/src/main/kotlin/io/github/gunkim/lotto/application/LottoSystem.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import io.github.gunkim.lotto.application.io.Input
44
import io.github.gunkim.lotto.application.io.Output
55
import io.github.gunkim.lotto.application.io.console.ConsoleInput
66
import io.github.gunkim.lotto.application.io.console.ConsoleOutput
7-
import io.github.gunkim.lotto.domain.CreateRandomNumberPolicy
7+
import io.github.gunkim.lotto.domain.CreateRandomLottoPolicy
88
import io.github.gunkim.lotto.domain.Lotto
99
import io.github.gunkim.lotto.domain.LottoMachine
1010
import io.github.gunkim.lotto.domain.LottoNumber
@@ -43,7 +43,7 @@ class LottoSystem(
4343
fun default(): LottoSystem = LottoSystem(
4444
ConsoleInput(),
4545
ConsoleOutput(),
46-
LottoMachine(CreateRandomNumberPolicy())
46+
LottoMachine(CreateRandomLottoPolicy())
4747
)
4848
}
4949
}

lotto/src/main/kotlin/io/github/gunkim/lotto/application/io/console/ConsoleOutput.kt

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,19 @@ class ConsoleOutput : Output {
3131
override fun showStatistics(results: Map<Rank, Int>) {
3232
println("당첨 통계")
3333
println("---------")
34-
Rank.entries.reversed().forEach { showRank(it, results) }
34+
Rank.entries.forEach { showRank(it, results) }
3535
}
3636

3737
private fun showRank(rank: Rank, results: Map<Rank, Int>) {
3838
val resultCnt = results[rank] ?: return
39-
val (reward, matchCnt) = rank
40-
println("${matchCnt}개 일치 ($reward)-${resultCnt}")
39+
val reward = rank.reward
40+
41+
println("${rank.number}등 (${formatMoney(reward)}) - ${resultCnt}")
42+
}
43+
44+
private fun formatMoney(money: Int) = money.toString().replace(MONEY_PATTERN, "$1,")
45+
46+
companion object {
47+
private val MONEY_PATTERN = Regex("(\\d)(?=(?:\\d{3})+(?!\\d))")
4148
}
4249
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package io.github.gunkim.lotto.domain
2+
3+
fun interface CreateLottoPolicy {
4+
fun createLotto(): Lotto
5+
}

lotto/src/main/kotlin/io/github/gunkim/lotto/domain/CreateNumberPolicy.kt

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package io.github.gunkim.lotto.domain
2+
3+
import java.util.random.RandomGenerator
4+
5+
class CreateRandomLottoPolicy : CreateLottoPolicy {
6+
override fun createLotto() = Lotto(
7+
generateSequence(::createLottoNumber)
8+
.distinct()
9+
.take(Lotto.NUMBER_SIZE)
10+
.toList()
11+
)
12+
13+
private fun createLottoNumber() = LottoNumber(
14+
randomGenerator.nextInt(LottoNumber.MAX_NUMBER) + 1
15+
)
16+
17+
companion object {
18+
private val randomGenerator = RandomGenerator.getDefault()
19+
}
20+
}

lotto/src/main/kotlin/io/github/gunkim/lotto/domain/CreateRandomNumberPolicy.kt

Lines changed: 0 additions & 11 deletions
This file was deleted.

lotto/src/main/kotlin/io/github/gunkim/lotto/domain/LottoMachine.kt

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,16 @@
11
package io.github.gunkim.lotto.domain
22

3-
import io.github.gunkim.lotto.domain.Lotto.Companion.NUMBER_SIZE
4-
53
data class LottoMachine(
6-
private val factory: CreateNumberPolicy
4+
private val factory: CreateLottoPolicy,
75
) {
86
fun buy(money: Int): List<Lotto> {
97
require(money >= PRICE) { "한장도 살 수 없는 금액입니다." }
108

11-
return generateSequence { createLottoNumbers() }
9+
return generateSequence(factory::createLotto)
1210
.take(money / PRICE)
13-
.map(::Lotto)
1411
.toList()
1512
}
1613

17-
private fun createLottoNumbers() = generateSequence(factory::create)
18-
.distinct()
19-
.take(NUMBER_SIZE)
20-
.map(::LottoNumber)
21-
.toList()
22-
2314
companion object {
2415
private const val PRICE = 1_000
2516
}

lotto/src/main/kotlin/io/github/gunkim/lotto/domain/Rank.kt

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ package io.github.gunkim.lotto.domain
22

33
enum class Rank(
44
val reward: Int,
5-
val matchCnt: Int
5+
val matchCnt: Int,
6+
val number: Int,
67
) {
7-
FIRST(2_000_000_000, 6),
8-
SECOND(30_000_000, 5),
9-
THIRD(1_500_000, 5),
10-
FOURTH(50_000, 4),
11-
FIFTH(5_000, 3),
12-
MISS(0, 0)
8+
FIRST(2_000_000_000, 6, 1),
9+
SECOND(30_000_000, 5, 2),
10+
THIRD(1_500_000, 5, 3),
11+
FOURTH(50_000, 4, 4),
12+
FIFTH(5_000, 3, 5),
13+
MISS(0, 0, 6)
1314
;
1415

1516
operator fun component1(): Int = reward

lotto/src/test/kotlin/io/github/gunkim/lotto/domain/LottoMachineTests.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ import io.kotest.matchers.collections.shouldHaveSize
88
@DisplayName("로또 발권기는")
99
class LottoMachineTests : StringSpec({
1010
"받은 돈 만큼 로또 티켓을 발행한다" {
11-
var i = 1
12-
val lottos = LottoMachine { i++ }.buy(2_500)
11+
val buyMoney = 2_500
12+
val lottos = LottoMachine { Lotto.from("1,2,3,4,5,6") }.buy(buyMoney)
1313

1414
lottos shouldHaveSize 2
1515
}
1616
"티켓 한장 가격보다 적은 돈을 받을 경우 예외가 발생한다" {
17-
shouldThrow<IllegalArgumentException> { LottoMachine { 1 }.buy(500) }
17+
shouldThrow<IllegalArgumentException> { LottoMachine { Lotto.from("1,2,3,4,5,6") }.buy(500) }
1818
}
1919
})

0 commit comments

Comments
 (0)