From 94bfcfbe6401bb76e39c9d2c40adf81ff1e1db2e Mon Sep 17 00:00:00 2001 From: kyudong3 Date: Fri, 16 Jun 2023 15:21:05 +0900 Subject: [PATCH 1/6] =?UTF-8?q?chore:=20=EB=A1=9C=EB=98=90(=EC=9E=90?= =?UTF-8?q?=EB=8F=99)=20Readme=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 80d326b9c8..8aa0d417ca 100644 --- a/README.md +++ b/README.md @@ -15,4 +15,19 @@ - [x] indent는 depth가 2를 넘지 않도록 구현 - ex) while 문 안에 if 문이 있으면 들여쓰기는 2이다 - [x] 함수의 길이가 10개 Line을 넘어가지 않도록 구현 - - 함수가 한 가지 일만 잘 하도록 구현 \ No newline at end of file + - 함수가 한 가지 일만 잘 하도록 구현 + + + +## 로또(자동) +### 기능 요구 사항 +- [ ] 로또 구입 금액을 입력하면 구입 금액에 해당하는 로또를 발급해야 한다 +- [ ] 로또 1장의 가격은 1000원이다 +- [ ] 천원 단위가 아닐 경우 내림 + +### 프로그래밍 요구 사항 +- [ ] InputView 생성 +- [ ] indent는 depth가 2를 넘지 않도록 구현 +- [ ] 함수의 길이가 15 라인을 넘어가지 않도록 구현 +- [ ] ResultView 생성 + From db4b96d16609904249c1cb67ec4c073ae9db3819 Mon Sep 17 00:00:00 2001 From: kyudong3 Date: Fri, 16 Jun 2023 16:38:05 +0900 Subject: [PATCH 2/6] =?UTF-8?q?feat:=20=ED=94=84=EB=A1=9C=EA=B7=B8?= =?UTF-8?q?=EB=9E=98=EB=B0=8D=20=EC=9A=94=EA=B5=AC=20=EC=82=AC=ED=95=AD1?= =?UTF-8?q?=20=EA=B5=AC=ED=98=84=20*=20InputView=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- src/main/kotlin/lotto/InputView.kt | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 src/main/kotlin/lotto/InputView.kt diff --git a/README.md b/README.md index 8aa0d417ca..949388075e 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ - [ ] 천원 단위가 아닐 경우 내림 ### 프로그래밍 요구 사항 -- [ ] InputView 생성 +- [x] InputView 생성 - [ ] indent는 depth가 2를 넘지 않도록 구현 - [ ] 함수의 길이가 15 라인을 넘어가지 않도록 구현 - [ ] ResultView 생성 diff --git a/src/main/kotlin/lotto/InputView.kt b/src/main/kotlin/lotto/InputView.kt new file mode 100644 index 0000000000..37e1e7279a --- /dev/null +++ b/src/main/kotlin/lotto/InputView.kt @@ -0,0 +1,20 @@ +package lotto + +object InputView { + + fun requestPurchaseAmount(): Int { + printMessage("구입금액을 입력해 주세요.") + return readln().toInt() + } + + fun requestLastWinnerNumbers(): List { + printMessage("지난 주 당첨 번호를 입력해 주세요.") + return readln().split(",").map { + it.trim().toInt() + } + } + + private fun printMessage(message: String) { + println(message) + } +} From 6a39167358bff283a3f50cee34326d0a0777436a Mon Sep 17 00:00:00 2001 From: kyudong3 Date: Fri, 16 Jun 2023 17:02:01 +0900 Subject: [PATCH 3/6] =?UTF-8?q?feat:=20=EA=B8=B0=EB=8A=A5=20=EC=9A=94?= =?UTF-8?q?=EA=B5=AC=20=EC=82=AC=ED=95=AD1=20=EA=B5=AC=ED=98=84=20*=20?= =?UTF-8?q?=EB=A1=9C=EB=98=90=201=EC=9E=A5=EC=9D=98=20=EA=B0=80=EA=B2=A9?= =?UTF-8?q?=EC=9D=80=201000=EC=9B=90=EC=9D=B4=EB=8B=A4=20*=20Lotto=20->=20?= =?UTF-8?q?Lottery=EB=A1=9C=20=EC=9D=B4=EB=A6=84=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 5 +++-- src/main/kotlin/lotto/LotteryShop.kt | 9 +++++++++ src/test/kotlin/lotto/LotteryShopKoTest.kt | 13 +++++++++++++ src/test/kotlin/lotto/LotteryShopTest.kt | 14 ++++++++++++++ 4 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 src/main/kotlin/lotto/LotteryShop.kt create mode 100644 src/test/kotlin/lotto/LotteryShopKoTest.kt create mode 100644 src/test/kotlin/lotto/LotteryShopTest.kt diff --git a/README.md b/README.md index 949388075e..13cf8ce90b 100644 --- a/README.md +++ b/README.md @@ -21,9 +21,10 @@ ## 로또(자동) ### 기능 요구 사항 -- [ ] 로또 구입 금액을 입력하면 구입 금액에 해당하는 로또를 발급해야 한다 -- [ ] 로또 1장의 가격은 1000원이다 +- [x] 로또 1장의 가격은 1000원이다 +- [ ] 구입한 금액에 해당하는 구매 갯수 반환 - [ ] 천원 단위가 아닐 경우 내림 +- [ ] 로또 구입 금액을 입력하면 구입 금액에 해당하는 로또를 발급해야 한다 ### 프로그래밍 요구 사항 - [x] InputView 생성 diff --git a/src/main/kotlin/lotto/LotteryShop.kt b/src/main/kotlin/lotto/LotteryShop.kt new file mode 100644 index 0000000000..f81aedfd0d --- /dev/null +++ b/src/main/kotlin/lotto/LotteryShop.kt @@ -0,0 +1,9 @@ +package lotto + +class LotteryShop { + + fun buy(money: Int): Int { + if (money == 1000) return money + else throw IllegalArgumentException() + } +} diff --git a/src/test/kotlin/lotto/LotteryShopKoTest.kt b/src/test/kotlin/lotto/LotteryShopKoTest.kt new file mode 100644 index 0000000000..a721ac39ff --- /dev/null +++ b/src/test/kotlin/lotto/LotteryShopKoTest.kt @@ -0,0 +1,13 @@ +package lotto + +import io.kotest.core.spec.style.StringSpec +import io.kotest.matchers.shouldBe + +class LotteryShopKoTest : StringSpec({ + + "로또 1장의 가격은 1000이다" { + val lotteryShop = LotteryShop() + val actualInput = 1000 + lotteryShop.buy(actualInput) shouldBe 1000 + } +}) diff --git a/src/test/kotlin/lotto/LotteryShopTest.kt b/src/test/kotlin/lotto/LotteryShopTest.kt new file mode 100644 index 0000000000..3e10427d10 --- /dev/null +++ b/src/test/kotlin/lotto/LotteryShopTest.kt @@ -0,0 +1,14 @@ +package lotto + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test + +class LotteryShopTest { + + @Test + fun `로또 1장의 가격은 1000이다`() { + val lotteryShop = LotteryShop() + val actualInput = 1000 + assertThat(lotteryShop.buy(actualInput)).isEqualTo(1000) + } +} From 76a713066b2a96f76085a18058784a3d906a4985 Mon Sep 17 00:00:00 2001 From: kyudong3 Date: Fri, 16 Jun 2023 17:11:21 +0900 Subject: [PATCH 4/6] =?UTF-8?q?feat:=20=EA=B8=B0=EB=8A=A5=20=EC=9A=94?= =?UTF-8?q?=EA=B5=AC=20=EC=82=AC=ED=95=AD1=20=EC=9E=AC=EA=B5=AC=ED=98=84?= =?UTF-8?q?=20(=EA=B8=B0=EC=A1=B4=20=EC=9A=94=EA=B5=AC=EC=82=AC=ED=95=AD1?= =?UTF-8?q?=20=EC=82=AD=EC=A0=9C)=20*=20=EA=B5=AC=EB=A7=A4=ED=95=9C=20?= =?UTF-8?q?=EA=B8=88=EC=95=A1=EC=97=90=20=ED=95=B4=EB=8B=B9=ED=95=98?= =?UTF-8?q?=EB=8A=94=20=EB=A1=9C=EB=98=90=20=EA=B0=AF=EC=88=98=20=EB=B0=98?= =?UTF-8?q?=ED=99=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 3 +-- src/main/kotlin/lotto/LotteryShop.kt | 7 +++++-- src/test/kotlin/lotto/LotteryShopKoTest.kt | 6 +++--- src/test/kotlin/lotto/LotteryShopTest.kt | 6 +++--- 4 files changed, 12 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 13cf8ce90b..0dbad1ac7f 100644 --- a/README.md +++ b/README.md @@ -21,8 +21,7 @@ ## 로또(자동) ### 기능 요구 사항 -- [x] 로또 1장의 가격은 1000원이다 -- [ ] 구입한 금액에 해당하는 구매 갯수 반환 +- [x] 구입한 금액에 해당하는 구매 갯수 반환 - [ ] 천원 단위가 아닐 경우 내림 - [ ] 로또 구입 금액을 입력하면 구입 금액에 해당하는 로또를 발급해야 한다 diff --git a/src/main/kotlin/lotto/LotteryShop.kt b/src/main/kotlin/lotto/LotteryShop.kt index f81aedfd0d..4a1f0ae5f9 100644 --- a/src/main/kotlin/lotto/LotteryShop.kt +++ b/src/main/kotlin/lotto/LotteryShop.kt @@ -3,7 +3,10 @@ package lotto class LotteryShop { fun buy(money: Int): Int { - if (money == 1000) return money - else throw IllegalArgumentException() + return money / LOTTERY_PRICE + } + + companion object { + private const val LOTTERY_PRICE = 1000 } } diff --git a/src/test/kotlin/lotto/LotteryShopKoTest.kt b/src/test/kotlin/lotto/LotteryShopKoTest.kt index a721ac39ff..d949674901 100644 --- a/src/test/kotlin/lotto/LotteryShopKoTest.kt +++ b/src/test/kotlin/lotto/LotteryShopKoTest.kt @@ -5,9 +5,9 @@ import io.kotest.matchers.shouldBe class LotteryShopKoTest : StringSpec({ - "로또 1장의 가격은 1000이다" { + "구매한 금액에 해당하는 로또 갯수 반환" { val lotteryShop = LotteryShop() - val actualInput = 1000 - lotteryShop.buy(actualInput) shouldBe 1000 + val actualInput = 14000 + lotteryShop.buy(actualInput) shouldBe 14 } }) diff --git a/src/test/kotlin/lotto/LotteryShopTest.kt b/src/test/kotlin/lotto/LotteryShopTest.kt index 3e10427d10..615392a89d 100644 --- a/src/test/kotlin/lotto/LotteryShopTest.kt +++ b/src/test/kotlin/lotto/LotteryShopTest.kt @@ -6,9 +6,9 @@ import org.junit.jupiter.api.Test class LotteryShopTest { @Test - fun `로또 1장의 가격은 1000이다`() { + fun `구매한 금액에 해당하는 로또 갯수 반환`() { val lotteryShop = LotteryShop() - val actualInput = 1000 - assertThat(lotteryShop.buy(actualInput)).isEqualTo(1000) + val actualInput = 14000 + assertThat(lotteryShop.buy(actualInput)).isEqualTo(14) } } From 03d2c30daeb55507a5a8ae2f13cd84819fb1b33c Mon Sep 17 00:00:00 2001 From: kyudong3 Date: Fri, 16 Jun 2023 23:21:35 +0900 Subject: [PATCH 5/6] =?UTF-8?q?feat:=20=EA=B8=B0=EB=8A=A5=20=EC=9A=94?= =?UTF-8?q?=EA=B5=AC=20=EC=82=AC=ED=95=AD2=20=EA=B5=AC=ED=98=84=20*=20?= =?UTF-8?q?=EA=B5=AC=EB=A7=A4=20=EA=B8=88=EC=95=A1=EC=9D=84=20=EC=9E=85?= =?UTF-8?q?=EB=A0=A5=ED=95=98=EB=A9=B4=20=EA=B5=AC=EC=9E=85=20=EA=B8=88?= =?UTF-8?q?=EC=95=A1=EC=97=90=20=ED=95=B4=EB=8B=B9=ED=95=98=EB=8A=94=20?= =?UTF-8?q?=EB=A1=9C=EB=98=90=EB=A5=BC=20=EB=B0=9C=EA=B8=89=ED=95=B4?= =?UTF-8?q?=EC=95=BC=20=ED=95=9C=EB=8B=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- src/main/kotlin/lotto/Lottery.kt | 5 +++++ src/main/kotlin/lotto/LotteryShop.kt | 12 ++++++++++-- src/main/kotlin/lotto/ResultView.kt | 14 ++++++++++++++ src/main/kotlin/lotto/UserLottery.kt | 6 ++++++ src/test/kotlin/lotto/LotteryShopKoTest.kt | 3 ++- src/test/kotlin/lotto/LotteryShopTest.kt | 3 ++- 7 files changed, 40 insertions(+), 5 deletions(-) create mode 100644 src/main/kotlin/lotto/Lottery.kt create mode 100644 src/main/kotlin/lotto/ResultView.kt create mode 100644 src/main/kotlin/lotto/UserLottery.kt diff --git a/README.md b/README.md index 0dbad1ac7f..8cfa588786 100644 --- a/README.md +++ b/README.md @@ -22,8 +22,8 @@ ## 로또(자동) ### 기능 요구 사항 - [x] 구입한 금액에 해당하는 구매 갯수 반환 +- [x] 로또 구입 금액을 입력하면 구입 금액에 해당하는 로또를 발급해야 한다 - [ ] 천원 단위가 아닐 경우 내림 -- [ ] 로또 구입 금액을 입력하면 구입 금액에 해당하는 로또를 발급해야 한다 ### 프로그래밍 요구 사항 - [x] InputView 생성 diff --git a/src/main/kotlin/lotto/Lottery.kt b/src/main/kotlin/lotto/Lottery.kt new file mode 100644 index 0000000000..2fc5b67fe4 --- /dev/null +++ b/src/main/kotlin/lotto/Lottery.kt @@ -0,0 +1,5 @@ +package lotto + +data class Lottery( + val numbers: List +) diff --git a/src/main/kotlin/lotto/LotteryShop.kt b/src/main/kotlin/lotto/LotteryShop.kt index 4a1f0ae5f9..d390677379 100644 --- a/src/main/kotlin/lotto/LotteryShop.kt +++ b/src/main/kotlin/lotto/LotteryShop.kt @@ -2,8 +2,16 @@ package lotto class LotteryShop { - fun buy(money: Int): Int { - return money / LOTTERY_PRICE + fun buy(money: Int): UserLottery { + val count = money / LOTTERY_PRICE + val lotteryTickets = mutableListOf() + repeat(count) { + lotteryTickets.add( + Lottery((1..45).shuffled().take(6).sorted()) + ) + } + + return UserLottery(count, lotteryTickets) } companion object { diff --git a/src/main/kotlin/lotto/ResultView.kt b/src/main/kotlin/lotto/ResultView.kt new file mode 100644 index 0000000000..7f06bbcfbc --- /dev/null +++ b/src/main/kotlin/lotto/ResultView.kt @@ -0,0 +1,14 @@ +package lotto + +object ResultView { + + fun printLotteryCount(size: Int) { + println("${size}개를 구매했습니다.") + } + + fun printAllLotteries(lotteryTickets: List) { + lotteryTickets.forEach { lottery -> + println(lottery.numbers) + } + } +} diff --git a/src/main/kotlin/lotto/UserLottery.kt b/src/main/kotlin/lotto/UserLottery.kt new file mode 100644 index 0000000000..72a097b546 --- /dev/null +++ b/src/main/kotlin/lotto/UserLottery.kt @@ -0,0 +1,6 @@ +package lotto + +data class UserLottery( + val lotteryCount: Int, + val lotteryTickets: List +) diff --git a/src/test/kotlin/lotto/LotteryShopKoTest.kt b/src/test/kotlin/lotto/LotteryShopKoTest.kt index d949674901..8884279122 100644 --- a/src/test/kotlin/lotto/LotteryShopKoTest.kt +++ b/src/test/kotlin/lotto/LotteryShopKoTest.kt @@ -8,6 +8,7 @@ class LotteryShopKoTest : StringSpec({ "구매한 금액에 해당하는 로또 갯수 반환" { val lotteryShop = LotteryShop() val actualInput = 14000 - lotteryShop.buy(actualInput) shouldBe 14 + val userLottery = lotteryShop.buy(actualInput) + lotteryShop.buy(actualInput) shouldBe userLottery.lotteryCount } }) diff --git a/src/test/kotlin/lotto/LotteryShopTest.kt b/src/test/kotlin/lotto/LotteryShopTest.kt index 615392a89d..d66b1fd569 100644 --- a/src/test/kotlin/lotto/LotteryShopTest.kt +++ b/src/test/kotlin/lotto/LotteryShopTest.kt @@ -9,6 +9,7 @@ class LotteryShopTest { fun `구매한 금액에 해당하는 로또 갯수 반환`() { val lotteryShop = LotteryShop() val actualInput = 14000 - assertThat(lotteryShop.buy(actualInput)).isEqualTo(14) + val userLottery = lotteryShop.buy(actualInput) + assertThat(userLottery.lotteryCount).isEqualTo(14) } } From 23fd1fceca53722a7714c01868436de3e90397e8 Mon Sep 17 00:00:00 2001 From: kyudong3 Date: Tue, 20 Jun 2023 03:20:09 +0900 Subject: [PATCH 6/6] =?UTF-8?q?feat:=20=EB=A1=9C=EB=98=90(=EC=9E=90?= =?UTF-8?q?=EB=8F=99)=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 7 ++-- src/main/kotlin/lotto/InputView.kt | 2 +- src/main/kotlin/lotto/LotteryResult.kt | 7 ++++ src/main/kotlin/lotto/LotteryShop.kt | 5 ++- src/main/kotlin/lotto/LotteryStatistic.kt | 36 +++++++++++++++++ src/main/kotlin/lotto/Main.kt | 15 ++++++++ src/main/kotlin/lotto/ResultView.kt | 11 ++++++ src/main/kotlin/lotto/WinnerPrize.kt | 18 +++++++++ src/test/kotlin/lotto/LotteryShopKoTest.kt | 45 ++++++++++++++++++++-- src/test/kotlin/lotto/LotteryShopTest.kt | 43 ++++++++++++++++++++- 10 files changed, 178 insertions(+), 11 deletions(-) create mode 100644 src/main/kotlin/lotto/LotteryResult.kt create mode 100644 src/main/kotlin/lotto/LotteryStatistic.kt create mode 100644 src/main/kotlin/lotto/Main.kt create mode 100644 src/main/kotlin/lotto/WinnerPrize.kt diff --git a/README.md b/README.md index 8cfa588786..8fd44fa149 100644 --- a/README.md +++ b/README.md @@ -23,11 +23,10 @@ ### 기능 요구 사항 - [x] 구입한 금액에 해당하는 구매 갯수 반환 - [x] 로또 구입 금액을 입력하면 구입 금액에 해당하는 로또를 발급해야 한다 -- [ ] 천원 단위가 아닐 경우 내림 ### 프로그래밍 요구 사항 - [x] InputView 생성 -- [ ] indent는 depth가 2를 넘지 않도록 구현 -- [ ] 함수의 길이가 15 라인을 넘어가지 않도록 구현 -- [ ] ResultView 생성 +- [x] indent는 depth가 2를 넘지 않도록 구현 +- [x] 함수의 길이가 15 라인을 넘어가지 않도록 구현 +- [x] ResultView 생성 diff --git a/src/main/kotlin/lotto/InputView.kt b/src/main/kotlin/lotto/InputView.kt index 37e1e7279a..96b433a731 100644 --- a/src/main/kotlin/lotto/InputView.kt +++ b/src/main/kotlin/lotto/InputView.kt @@ -8,7 +8,7 @@ object InputView { } fun requestLastWinnerNumbers(): List { - printMessage("지난 주 당첨 번호를 입력해 주세요.") + printMessage("\n지난 주 당첨 번호를 입력해 주세요.") return readln().split(",").map { it.trim().toInt() } diff --git a/src/main/kotlin/lotto/LotteryResult.kt b/src/main/kotlin/lotto/LotteryResult.kt new file mode 100644 index 0000000000..e76ac95cc5 --- /dev/null +++ b/src/main/kotlin/lotto/LotteryResult.kt @@ -0,0 +1,7 @@ +package lotto + +data class LotteryResult( + val prize: Int, + val matchCount: Int, + val message: String +) diff --git a/src/main/kotlin/lotto/LotteryShop.kt b/src/main/kotlin/lotto/LotteryShop.kt index d390677379..62ce87afbe 100644 --- a/src/main/kotlin/lotto/LotteryShop.kt +++ b/src/main/kotlin/lotto/LotteryShop.kt @@ -7,14 +7,15 @@ class LotteryShop { val lotteryTickets = mutableListOf() repeat(count) { lotteryTickets.add( - Lottery((1..45).shuffled().take(6).sorted()) + Lottery((MIN_LOTTERY_NUMBER..MAX_LOTTERY_NUMBER).shuffled().take(6).sorted()) ) } - return UserLottery(count, lotteryTickets) } companion object { private const val LOTTERY_PRICE = 1000 + private const val MIN_LOTTERY_NUMBER = 1 + private const val MAX_LOTTERY_NUMBER = 45 } } diff --git a/src/main/kotlin/lotto/LotteryStatistic.kt b/src/main/kotlin/lotto/LotteryStatistic.kt new file mode 100644 index 0000000000..6d94541cde --- /dev/null +++ b/src/main/kotlin/lotto/LotteryStatistic.kt @@ -0,0 +1,36 @@ +package lotto + +object LotteryStatistic { + + fun getWinStatistic( + lotteryTickets: List, + lastWinnerNumbers: List + ): Pair, Double> { + val winResult = checkLotteryTickets(lotteryTickets, lastWinnerNumbers) + + val lotteryResults = mutableListOf() + var sum = 0 + winResult.forEach { (count, matchCount) -> + val prize = WinnerPrize.getPrize(count).money + val message = "${count}개 일치 (${prize}원) - ${matchCount}개" + lotteryResults.add(LotteryResult(prize, matchCount, message)) + sum += (prize * matchCount) + } + + return lotteryResults.toList() to sum.toDouble() + } + + private fun checkLotteryTickets( + lotteryTickets: List, + lastWinnerNumbers: List + ): List> = + lotteryTickets.map { lottery -> + lottery.numbers.intersect( + lastWinnerNumbers.sorted().toSet() + ).count() + }.filter { it >= 3 } + .groupingBy { it } + .eachCount() + .toList() + .sortedBy { it.first } +} diff --git a/src/main/kotlin/lotto/Main.kt b/src/main/kotlin/lotto/Main.kt new file mode 100644 index 0000000000..87c064bbd0 --- /dev/null +++ b/src/main/kotlin/lotto/Main.kt @@ -0,0 +1,15 @@ +package lotto + +fun main() { + val lotteryShop = LotteryShop() + val money = InputView.requestPurchaseAmount() + val userLotteryInfo = lotteryShop.buy(money) + + ResultView.printLotteryCount(userLotteryInfo.lotteryCount) + ResultView.printAllLotteries(userLotteryInfo.lotteryTickets) + + val lastWinnerNumbers = InputView.requestLastWinnerNumbers() + val lotteryResults = LotteryStatistic.getWinStatistic(userLotteryInfo.lotteryTickets, lastWinnerNumbers) + + ResultView.printWinStatistic(lotteryResults, money) +} \ No newline at end of file diff --git a/src/main/kotlin/lotto/ResultView.kt b/src/main/kotlin/lotto/ResultView.kt index 7f06bbcfbc..9cd946aad6 100644 --- a/src/main/kotlin/lotto/ResultView.kt +++ b/src/main/kotlin/lotto/ResultView.kt @@ -11,4 +11,15 @@ object ResultView { println(lottery.numbers) } } + + fun printWinStatistic( + lotteryResults: Pair, Double>, + money: Int + ) { + println("\n당첨 통계\n-------------------") + lotteryResults.first.forEach { lotteryResult -> + println(lotteryResult.message) + } + println(String.format("총 수익률은 %.2f 입니다", lotteryResults.second / money)) + } } diff --git a/src/main/kotlin/lotto/WinnerPrize.kt b/src/main/kotlin/lotto/WinnerPrize.kt new file mode 100644 index 0000000000..1a729acacb --- /dev/null +++ b/src/main/kotlin/lotto/WinnerPrize.kt @@ -0,0 +1,18 @@ +package lotto + +enum class WinnerPrize(val money: Int) { + + FIRST(2_000_000_000), + SECOND(1_500_000), + THIRD(50_000), + LAST(5_000); + + companion object { + fun getPrize(sameCount: Int): WinnerPrize = when (sameCount) { + 3 -> LAST + 4 -> THIRD + 5 -> SECOND + else -> FIRST + } + } +} diff --git a/src/test/kotlin/lotto/LotteryShopKoTest.kt b/src/test/kotlin/lotto/LotteryShopKoTest.kt index 8884279122..4efc83e17f 100644 --- a/src/test/kotlin/lotto/LotteryShopKoTest.kt +++ b/src/test/kotlin/lotto/LotteryShopKoTest.kt @@ -1,14 +1,53 @@ package lotto import io.kotest.core.spec.style.StringSpec +import io.kotest.inspectors.forAll import io.kotest.matchers.shouldBe class LotteryShopKoTest : StringSpec({ "구매한 금액에 해당하는 로또 갯수 반환" { val lotteryShop = LotteryShop() - val actualInput = 14000 - val userLottery = lotteryShop.buy(actualInput) - lotteryShop.buy(actualInput) shouldBe userLottery.lotteryCount + val actualInput = 14_000 + lotteryShop.buy(actualInput).lotteryCount shouldBe 14 + } + + "일치하는 로또 갯수에 의한 당첨 금액" { + val first = WinnerPrize.getPrize(6) + val second = WinnerPrize.getPrize(5) + val third = WinnerPrize.getPrize(4) + val last = WinnerPrize.getPrize(3) + + first.money shouldBe 2_000_000_000 + second.money shouldBe 1_500_000 + third.money shouldBe 50_000 + last.money shouldBe 5_000 + } + + "구매한 로또들과 지난 주 당첨 번호가 일치하는지 확인" { + val lastWinnerNumbers = listOf(1, 2, 3, 4, 5, 6) + val lotteryTickets = listOf( + Lottery(listOf(1, 2, 3, 4, 39, 43)), + Lottery(listOf(9, 11, 21, 27, 28, 32)), + Lottery(listOf(4, 5, 14, 17, 31, 35)), + Lottery(listOf(11, 13, 17, 22, 25, 34)), + Lottery(listOf(1, 3, 5, 21, 41, 42)), + Lottery(listOf(6, 7, 17, 19, 32, 35)), + Lottery(listOf(2, 12, 19, 34, 35, 37)), + Lottery(listOf(1, 3, 5, 6, 39, 44)), + Lottery(listOf(2, 5, 8, 10, 13, 23)), + Lottery(listOf(7, 29, 30, 31, 34, 36)) + ) + + val lotteryResults = LotteryStatistic.getWinStatistic(lotteryTickets, lastWinnerNumbers).first + + lotteryResults.forEach { + when (it.prize) { + 3 -> it.matchCount shouldBe 1 + 4 -> it.matchCount shouldBe 2 + 5 -> it.matchCount shouldBe 0 + 6 -> it.matchCount shouldBe 0 + } + } } }) diff --git a/src/test/kotlin/lotto/LotteryShopTest.kt b/src/test/kotlin/lotto/LotteryShopTest.kt index d66b1fd569..f03174ffed 100644 --- a/src/test/kotlin/lotto/LotteryShopTest.kt +++ b/src/test/kotlin/lotto/LotteryShopTest.kt @@ -8,8 +8,49 @@ class LotteryShopTest { @Test fun `구매한 금액에 해당하는 로또 갯수 반환`() { val lotteryShop = LotteryShop() - val actualInput = 14000 + val actualInput = 14_000 val userLottery = lotteryShop.buy(actualInput) assertThat(userLottery.lotteryCount).isEqualTo(14) } + + @Test + fun `일치하는 로또 갯수에 의한 당첨 금액`() { + val first = WinnerPrize.getPrize(6) + val second = WinnerPrize.getPrize(5) + val third = WinnerPrize.getPrize(4) + val last = WinnerPrize.getPrize(3) + + assertThat(first.money).isEqualTo(2_000_000_000) + assertThat(second.money).isEqualTo(1_500_000) + assertThat(third.money).isEqualTo(50_000) + assertThat(last.money).isEqualTo(5_000) + } + + @Test + fun `구매한 로또들과 지난 주 당첨 번호가 일치하는지 확인`() { + val lastWinnerNumbers = listOf(1, 2, 3, 4, 5, 6) + val lotteryTickets = listOf( + Lottery(listOf(1, 2, 3, 4, 39, 43)), + Lottery(listOf(9, 11, 21, 27, 28, 32)), + Lottery(listOf(4, 5, 14, 17, 31, 35)), + Lottery(listOf(11, 13, 17, 22, 25, 34)), + Lottery(listOf(1, 3, 5, 21, 41, 42)), + Lottery(listOf(6, 7, 17, 19, 32, 35)), + Lottery(listOf(2, 12, 19, 34, 35, 37)), + Lottery(listOf(1, 3, 5, 6, 39, 44)), + Lottery(listOf(2, 5, 8, 10, 13, 23)), + Lottery(listOf(7, 29, 30, 31, 34, 36)) + ) + + val lotteryResults = LotteryStatistic.getWinStatistic(lotteryTickets, lastWinnerNumbers).first + + lotteryResults.forEach { + when (it.prize) { + 3 -> assertThat(it.matchCount).isEqualTo(1) + 4 -> assertThat(it.matchCount).isEqualTo(2) + 5 -> assertThat(it.matchCount).isEqualTo(0) + 6 -> assertThat(it.matchCount).isEqualTo(0) + } + } + } }