From 01355a994cc1cfa7394871563ba597424d64f4fb Mon Sep 17 00:00:00 2001 From: sendkite Date: Mon, 27 Nov 2023 01:56:39 +0900 Subject: [PATCH] feat: match lotto --- README.md | 5 ++++- src/main/kotlin/lotto/Lotto.kt | 10 +++++++-- src/main/kotlin/lotto/LottoBuyer.kt | 1 + src/main/kotlin/lotto/Main.kt | 29 +++++++++++++++++++++++++ src/test/kotlin/lotto/LottoStoreTest.kt | 24 ++++++++++++-------- src/test/kotlin/lotto/LottoTest.kt | 26 ++++++++++++++++++++++ 6 files changed, 83 insertions(+), 12 deletions(-) create mode 100644 src/main/kotlin/lotto/Main.kt diff --git a/README.md b/README.md index 68b2cb26c0..4bf744d21b 100644 --- a/README.md +++ b/README.md @@ -21,4 +21,7 @@ [x] 로또는 1부터 45까지의 숫자 중 6개를 랜덤으로 뽑는다. [x] 로또는 6개의 숫자가 모두 달라야 한다. [x] 로또 한장은 6개의 숫자를 가진다. - [x] 로또 한장은 숫자를 오름차순으로 정렬한다. \ No newline at end of file + [x] 로또 한장은 숫자를 오름차순으로 정렬한다. + [] 당첨 로또를 입력 받는다. + [] 당첨 로또와 구매한 로또를 비교하여 등수를 출력한다. + [] 당첨 로또와 구매한 로또를 비교하여 수익률을 출력한다. diff --git a/src/main/kotlin/lotto/Lotto.kt b/src/main/kotlin/lotto/Lotto.kt index b467351fa1..0b57bdb06a 100644 --- a/src/main/kotlin/lotto/Lotto.kt +++ b/src/main/kotlin/lotto/Lotto.kt @@ -1,9 +1,15 @@ package lotto -class Lotto { +class Lotto( + val numbers: List = generateLottoNumbers(), + var matchCount: Int = 0 +) { - val numbers: List = generateLottoNumbers() companion object { const val PRICE: Int = 1000 } + + fun match(target: Lotto) { + matchCount = numbers.intersect(target.numbers).count() + } } diff --git a/src/main/kotlin/lotto/LottoBuyer.kt b/src/main/kotlin/lotto/LottoBuyer.kt index 10ef204719..b13a5dbf9c 100644 --- a/src/main/kotlin/lotto/LottoBuyer.kt +++ b/src/main/kotlin/lotto/LottoBuyer.kt @@ -5,6 +5,7 @@ class LottoBuyer( ) { fun buyLottoFrom(lottoStore: LottoStore): List { + require(money >= Lotto.PRICE) { "돈이 부족합니다." } return lottoStore.sell(money) } } \ No newline at end of file diff --git a/src/main/kotlin/lotto/Main.kt b/src/main/kotlin/lotto/Main.kt new file mode 100644 index 0000000000..c3e6c4fdac --- /dev/null +++ b/src/main/kotlin/lotto/Main.kt @@ -0,0 +1,29 @@ +package lotto + +fun main(args: Array) { + 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}입니다.") +} \ No newline at end of file diff --git a/src/test/kotlin/lotto/LottoStoreTest.kt b/src/test/kotlin/lotto/LottoStoreTest.kt index e2605536eb..d2a74a8207 100644 --- a/src/test/kotlin/lotto/LottoStoreTest.kt +++ b/src/test/kotlin/lotto/LottoStoreTest.kt @@ -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() } } diff --git a/src/test/kotlin/lotto/LottoTest.kt b/src/test/kotlin/lotto/LottoTest.kt index f5a3b64364..ad0ecc4042 100644 --- a/src/test/kotlin/lotto/LottoTest.kt +++ b/src/test/kotlin/lotto/LottoTest.kt @@ -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 + } + } }