Skip to content

Commit

Permalink
feat: match lotto
Browse files Browse the repository at this point in the history
  • Loading branch information
sendkite committed Nov 26, 2023
1 parent bb5a273 commit 01355a9
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 12 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,7 @@
[x] 로또는 1부터 45까지의 숫자 중 6개를 랜덤으로 뽑는다.
[x] 로또는 6개의 숫자가 모두 달라야 한다.
[x] 로또 한장은 6개의 숫자를 가진다.
[x] 로또 한장은 숫자를 오름차순으로 정렬한다.
[x] 로또 한장은 숫자를 오름차순으로 정렬한다.
[] 당첨 로또를 입력 받는다.
[] 당첨 로또와 구매한 로또를 비교하여 등수를 출력한다.
[] 당첨 로또와 구매한 로또를 비교하여 수익률을 출력한다.
10 changes: 8 additions & 2 deletions src/main/kotlin/lotto/Lotto.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
package lotto

class Lotto {
class Lotto(
val numbers: List<Int> = generateLottoNumbers(),
var matchCount: Int = 0
) {

val numbers: List<Int> = generateLottoNumbers()
companion object {
const val PRICE: Int = 1000
}

fun match(target: Lotto) {
matchCount = numbers.intersect(target.numbers).count()
}
}
1 change: 1 addition & 0 deletions src/main/kotlin/lotto/LottoBuyer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class LottoBuyer(
) {

fun buyLottoFrom(lottoStore: LottoStore): List<Lotto> {
require(money >= Lotto.PRICE) { "돈이 부족합니다." }
return lottoStore.sell(money)
}
}
29 changes: 29 additions & 0 deletions src/main/kotlin/lotto/Main.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package lotto

fun main(args: Array<String>) {
println("구매금액을 입력해주세요.")
var inputMoney = readLine()!!.toInt()

val lottoBuyer = LottoBuyer(inputMoney)
val lottos = lottoBuyer.buyLottoFrom(LottoStore())

println(String.format("%s개를 구매했습니다.", lottos.size))
lottos.forEach { lotto ->
println(lotto.numbers)
}

println("지난 주 당첨 번호를 입력해주세요.")
val lastWeekWinningNumbers = readLine()!!.split(", ").map { it.toInt() }

lottos.forEach { lotto ->
lotto.match(Lotto(lastWeekWinningNumbers))
}

println("당첨 통계")
println("---------")
println("3개 일치 (5000원) - ${lottos.count { it.matchCount == 3 }}")
println("4개 일치 (50000원) - ${lottos.count { it.matchCount == 4 }}")
println("5개 일치 (1500000원) - ${lottos.count { it.matchCount == 5 }}")
println("6개 일치 (2000000000원) - ${lottos.count { it.matchCount == 6 }}")
println("총 수익률은 ${lottos.size * Lotto.PRICE / inputMoney}입니다.")
}
24 changes: 15 additions & 9 deletions src/test/kotlin/lotto/LottoStoreTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,25 @@ package lotto

import io.kotest.matchers.collections.haveSize
import io.kotest.matchers.should
import io.kotest.matchers.shouldBe
import org.junit.jupiter.api.Test
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.CsvSource

class LottoStoreTest {

@Test
fun `1000원으로 로또를 구매한다`() {
val lotto1 = LottoStore().sell(1000)
lotto1 should haveSize(1)

val lotto2 = LottoStore().sell(500)
lotto2 should haveSize(0)
@ParameterizedTest
@CsvSource(value = ["1000,1", "2000,2", "3000,3"])
fun `로또를 여라장 구매한다`(money: Int, expected: Int) {
val lotto = LottoStore().sell(money)
lotto should haveSize(expected)
}

val lotto3 = LottoStore().sell(18000)
lotto3 should haveSize(18)
@Test
fun `로또를 구매할 돈이 부족하면 예외가 발생한다`() {
val lottoStore = LottoStore()
val money = 900
val lotto = lottoStore.sell(money)
lotto shouldBe emptyList()
}
}
26 changes: 26 additions & 0 deletions src/test/kotlin/lotto/LottoTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,30 @@ class LottoTest {
it shouldBeOneOf lotto.numbers
}
}

@Test
fun `test lotto matching`() {

val targetLottoNumbers = listOf(1, 2, 3, 4, 5, 6)
val targetLotto = Lotto(targetLottoNumbers)

val countAndLotto = listOf(
Pair(1, listOf(1, 7, 8, 9, 10, 11)),
Pair(2, listOf(1, 2, 8, 9, 10, 11)),
Pair(3, listOf(1, 2, 3, 9, 10, 11)),
Pair(4, listOf(1, 2, 3, 4, 10, 11)),
Pair(5, listOf(1, 2, 3, 4, 5, 11)),
Pair(6, listOf(1, 2, 3, 4, 5, 6))
)


for ((expectedMatchCount, lottoNumbers) in countAndLotto) {

val lotto = Lotto(lottoNumbers)
targetLotto.match(lotto)

val actualMatchCount = targetLotto.matchCount
actualMatchCount shouldBe expectedMatchCount
}
}
}

0 comments on commit 01355a9

Please sign in to comment.