From bca997a65698c977f143f7ade37219c3c944124c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B3=A0=EC=98=81=EA=B2=BD=28Yeonggyeong=20Go=29/?= =?UTF-8?q?=EC=BB=A4=EB=A8=B8=EC=8A=A4=ED=94=8C=EB=9E=AB=ED=8F=BC=EA=B0=9C?= =?UTF-8?q?=EB=B0=9C=ED=8C=80/SKP?= Date: Sun, 2 Jul 2023 21:49:17 +0900 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=80=203=EB=8B=A8=EA=B3=84=20-=20?= =?UTF-8?q?=EB=A1=9C=EB=98=90(2=EB=93=B1)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/lotto/LottoPlay.kt | 3 +- src/main/kotlin/lotto/model/Lotto.kt | 13 +++++- src/main/kotlin/lotto/model/LottoPrize.kt | 29 +++++++++---- .../service/LottoTicketCountCalculator.kt | 2 - src/main/kotlin/lotto/view/LottoPlayer.kt | 3 +- src/main/kotlin/lotto/view/LottoVendor.kt | 11 +++-- src/test/kotlin/lotto/LottoPlayTest.kt | 42 ++++++++++++++++--- 7 files changed, 79 insertions(+), 24 deletions(-) diff --git a/src/main/kotlin/lotto/LottoPlay.kt b/src/main/kotlin/lotto/LottoPlay.kt index ef712d45a1..0bb780ac11 100644 --- a/src/main/kotlin/lotto/LottoPlay.kt +++ b/src/main/kotlin/lotto/LottoPlay.kt @@ -23,8 +23,9 @@ fun main() { printLottoNumber(purchasedLottos) val lastWeekWinningString = LottoVendor.readLastWeekWinningString() + val lastWeekBonusNumber = LottoVendor.readLastWeekBonusBallNumber() val lastWeekWinningNumbers: List = toIntList(lastWeekWinningString) - purchasedLottos.forEach { it.setLottoPrize(lastWeekWinningNumbers) } + purchasedLottos.forEach { it.setLottoPrize(lastWeekWinningNumbers, lastWeekBonusNumber) } val winningRatio = LottoPlayResultAnalysis.getWinningRatio(purchaseAmount, purchasedLottos) LottoPlayer.printResult(purchasedLottos, winningRatio) } diff --git a/src/main/kotlin/lotto/model/Lotto.kt b/src/main/kotlin/lotto/model/Lotto.kt index 517820f518..23a6ad94ed 100644 --- a/src/main/kotlin/lotto/model/Lotto.kt +++ b/src/main/kotlin/lotto/model/Lotto.kt @@ -4,14 +4,23 @@ class Lotto { var lottoPrize: LottoPrize = LottoPrize.NONE_PRIZE val purchasedLottoNumbers: List = LOTTO_NUMBERS.shuffled().take(LOTTO_NUMBER_COUNT).sorted() - fun setLottoPrize(lastWeekWinningNumbers: List): LottoPrize { + private fun match(lastWeekWinningNumbers: List): Int { val matchingNumbers = purchasedLottoNumbers.intersect(lastWeekWinningNumbers.toSet()).size - lottoPrize = LottoPrize.getLottoPrize(matchingNumbers) + return matchingNumbers + } + + fun setLottoPrize(lastWeekWinningNumbers: List, lastWeekBonusNumber: Int): LottoPrize { + val matchingNumbers = match(lastWeekWinningNumbers) + val bonusBallMatching: Int = + if (purchasedLottoNumbers.contains(lastWeekBonusNumber)) CONTAIN_BALL else NOT_CONTAIN_BALL + lottoPrize = LottoPrize.getLottoPrize(matchingNumbers, bonusBallMatching) return lottoPrize } companion object { private val LOTTO_NUMBERS: List = (1..45).toList() private const val LOTTO_NUMBER_COUNT = 6 + private const val CONTAIN_BALL = 1 + private const val NOT_CONTAIN_BALL = 0 } } diff --git a/src/main/kotlin/lotto/model/LottoPrize.kt b/src/main/kotlin/lotto/model/LottoPrize.kt index 76a0121f05..8c2bc2b97c 100644 --- a/src/main/kotlin/lotto/model/LottoPrize.kt +++ b/src/main/kotlin/lotto/model/LottoPrize.kt @@ -1,17 +1,28 @@ package lotto.model -enum class LottoPrize(val matchingCount: Int, val prizeAmount: Long) { - FIRST(6, 2000000000), - SECOND(5, 1500000), - THIRD(4, 50000), - FOURTH(3, 5000), - NONE_PRIZE(0, 0); +enum class LottoPrize(val matchingCount: Int, val bonusMatching: Int = 0, val prizeAmount: Long) { + FIRST(6, 0, 2000000000), + BONUS(5, 1, 30000000), + SECOND(5, 0, 1500000), + THIRD(4, 0, 50000), + FOURTH(3, 0, 5000), + NONE_PRIZE(0, 0, 0); companion object { private const val PRIZING_CONT = 3 - fun getLottoPrize(matchingCount: Int): LottoPrize { - if(matchingCount >= PRIZING_CONT) { return NONE_PRIZE } - return values().find { it.matchingCount == matchingCount }!! + private const val SECOND_PRIZE_MATCHING_COUNT = 5 + fun getLottoPrize(matchingCount: Int, lastWeekBonusNumber: Int): LottoPrize { + if (matchingCount < PRIZING_CONT) { + return NONE_PRIZE + } + + val prize = values().find { it.matchingCount == matchingCount } ?: return NONE_PRIZE + + if (matchingCount == SECOND_PRIZE_MATCHING_COUNT) { + if ( lastWeekBonusNumber > 0) return BONUS + else return SECOND + } + return prize } } } diff --git a/src/main/kotlin/lotto/service/LottoTicketCountCalculator.kt b/src/main/kotlin/lotto/service/LottoTicketCountCalculator.kt index 19e067488e..88bcb3dff3 100644 --- a/src/main/kotlin/lotto/service/LottoTicketCountCalculator.kt +++ b/src/main/kotlin/lotto/service/LottoTicketCountCalculator.kt @@ -1,9 +1,7 @@ package lotto.service object LottoTicketCountCalculator { - private const val LOTTO_PRICE = 1000 fun getCount(purchaseAmount: Int) : Int = purchaseAmount / LOTTO_PRICE - } diff --git a/src/main/kotlin/lotto/view/LottoPlayer.kt b/src/main/kotlin/lotto/view/LottoPlayer.kt index 4180953106..31c6ca2da0 100644 --- a/src/main/kotlin/lotto/view/LottoPlayer.kt +++ b/src/main/kotlin/lotto/view/LottoPlayer.kt @@ -12,9 +12,10 @@ object LottoPlayer { println("3개 일치 (5000원)- ${LottoPlayResultAnalysis.staticPrize(winningLottos, 5000)}") println("4개 일치 (50000원)- ${LottoPlayResultAnalysis.staticPrize(winningLottos, 50000)}") println("5개 일치 (1500000원)- ${LottoPlayResultAnalysis.staticPrize(winningLottos, 1500000)}") + println("5개 일치, 보너스 볼 일치(30000000원) - ${LottoPlayResultAnalysis.staticPrize(winningLottos, 30000000)}") println("6개 일치 (2000000000원)-${LottoPlayResultAnalysis.staticPrize(winningLottos, 2000000000)}") println() println("총 수익률은 ${formatWinningRatio(winningRatio)} 입니다.(기준이 1이기 때문에 결과적으로 ${if (winningRatio >= 1) "이득" else "손해"}라는 의미임)") } -} \ No newline at end of file +} diff --git a/src/main/kotlin/lotto/view/LottoVendor.kt b/src/main/kotlin/lotto/view/LottoVendor.kt index 9fae0e1e35..a060e0e153 100644 --- a/src/main/kotlin/lotto/view/LottoVendor.kt +++ b/src/main/kotlin/lotto/view/LottoVendor.kt @@ -13,11 +13,16 @@ object LottoVendor { } fun printLottoNumber(purchasedLottos:List) { - purchasedLottos.forEach { print(it) } + purchasedLottos.forEach { println(it.purchasedLottoNumbers.toString()) } } fun readLastWeekWinningString(): String { println("지난 주 당첨 번호를 입력해 주세요.") - return readLine().toString() + return readln() } -} \ No newline at end of file + + fun readLastWeekBonusBallNumber(): Int { + println("보너스 볼을 입력해 주세요.") + return readln().toInt() + } +} diff --git a/src/test/kotlin/lotto/LottoPlayTest.kt b/src/test/kotlin/lotto/LottoPlayTest.kt index 08cb52d8e0..1895a24e2f 100644 --- a/src/test/kotlin/lotto/LottoPlayTest.kt +++ b/src/test/kotlin/lotto/LottoPlayTest.kt @@ -35,7 +35,7 @@ class LottoPlayTest { @ParameterizedTest @ValueSource(ints = [2]) fun testNotPrizeAmount(matchingCount: Int) { - Assertions.assertThat(LottoPrize.getLottoPrize(matchingCount).prizeAmount).isEqualTo(0) + Assertions.assertThat(LottoPrize.getLottoPrize(matchingCount, 0).prizeAmount).isEqualTo(0) } @DisplayName(value = "지난주 당첨 번호와 3개 일치 상금 5000원 ") @@ -45,7 +45,12 @@ class LottoPlayTest { val winningLottoNumber = Lotto() val lastWeekWinningLottoNumber = winningLottoNumber.purchasedLottoNumbers.toMutableList() - Assertions.assertThat(winningLottoNumber.setLottoPrize(lastWeekWinningLottoNumber.subList(0,3)).prizeAmount) + Assertions.assertThat( + winningLottoNumber.setLottoPrize( + lastWeekWinningLottoNumber.subList(0, 3), + lastWeekWinningLottoNumber.last() + 1 + ).prizeAmount + ) .isEqualTo(matchingCount.toLong()) } @@ -56,7 +61,12 @@ class LottoPlayTest { val winningLottoNumber = Lotto() val lastWeekWinningLottoNumber = winningLottoNumber.purchasedLottoNumbers.toMutableList() - Assertions.assertThat(winningLottoNumber.setLottoPrize(lastWeekWinningLottoNumber.subList(0,4)).prizeAmount) + Assertions.assertThat( + winningLottoNumber.setLottoPrize( + lastWeekWinningLottoNumber.subList(0, 4), + lastWeekWinningLottoNumber.last() + 1 + ).prizeAmount + ) .isEqualTo(matchingCount.toLong()) } @@ -67,7 +77,28 @@ class LottoPlayTest { val winningLottoNumber = Lotto() val lastWeekWinningLottoNumber = winningLottoNumber.purchasedLottoNumbers.toMutableList() - Assertions.assertThat(winningLottoNumber.setLottoPrize(lastWeekWinningLottoNumber.subList(0,5)).prizeAmount) + Assertions.assertThat( + winningLottoNumber.setLottoPrize( + lastWeekWinningLottoNumber.subList(0, 5), + lastWeekWinningLottoNumber.last() + 1 + ).prizeAmount + ) + .isEqualTo(matchingCount.toLong()) + } + + @DisplayName(value = "지난주 당첨 번호와 5개 보너스볼 일치 상금 3000000원") + @ParameterizedTest + @ValueSource(strings = ["30000000"]) + fun testBonusPrizeAmount(matchingCount: String) { + val winningLottoNumber = Lotto() + val lastWeekWinningLottoNumber = winningLottoNumber.purchasedLottoNumbers.toMutableList() + + Assertions.assertThat( + winningLottoNumber.setLottoPrize( + lastWeekWinningLottoNumber.subList(0, 5), + lastWeekWinningLottoNumber.last() + ).prizeAmount + ) .isEqualTo(matchingCount.toLong()) } @@ -77,8 +108,7 @@ class LottoPlayTest { fun testFirstPrizeAmount(matchingCount: String) { val winningLottoNumber = Lotto() val lastWeekWinningLottoNumber = winningLottoNumber.purchasedLottoNumbers.toList() - Assertions.assertThat(winningLottoNumber.setLottoPrize(lastWeekWinningLottoNumber).prizeAmount) + Assertions.assertThat(winningLottoNumber.setLottoPrize(lastWeekWinningLottoNumber, 1).prizeAmount) .isEqualTo(matchingCount.toLong()) } - }