From 7db83e68da4e94fe6fdcce3646c7fe46e4b644ac Mon Sep 17 00:00:00 2001 From: "pavlo.v" Date: Mon, 19 Jun 2023 17:26:17 +0900 Subject: [PATCH 01/19] =?UTF-8?q?=EA=B8=B0=EC=A1=B4=EC=9D=98=20step1=20REA?= =?UTF-8?q?DME=EC=99=80=20=EB=B3=91=ED=95=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 7 ++++ src/test/kotlin/lottery/LotteryTest.kt | 48 ++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 src/test/kotlin/lottery/LotteryTest.kt diff --git a/README.md b/README.md index 31ebdafabb..9495a6f5db 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,12 @@ # kotlin-lotto +[]로또 구입 금액을 입력하면 구입 금액에 해당하는 로또를 발급해야 한다. +[]로또 1장의 가격은 1000원이다. +[X]로또의 숫자는 6개이다. +[]로또는 당첨 번호는 중복돼서는 안된다. +[]로또의 수익률을 마지막에 구해주어야한다. (구매금액) / (당첨금액) +[]로또의 숫자는 1~45까지이다. + 요구사항 [X]쉼표(,) 또는 콜론(:)을 구분자로 가지는 문자열을 전달하는 경우 구분자를 기준으로 분리한 각 숫자의 합을 반환 (예: “” => 0, "1,2" => 3, " 1,2,3" => 6, “1,2:3” => 6) diff --git a/src/test/kotlin/lottery/LotteryTest.kt b/src/test/kotlin/lottery/LotteryTest.kt new file mode 100644 index 0000000000..478a6c2389 --- /dev/null +++ b/src/test/kotlin/lottery/LotteryTest.kt @@ -0,0 +1,48 @@ +package lottery + +import io.kotest.core.spec.style.StringSpec + +class Lottery(numbers: List) { + private var lotteryNumbers: List + + init { + require(hasDuplicatedLotteryNumbers(numbers)) { "로또 번호에 중복되는 숫자가 있습니다." } + lotteryNumbers = numbers + } + + fun makeManualLottery(numbers: List): Lottery { + return Lottery(numbers.map { LotteryNumber(it) }) + } + + fun hasDuplicatedLotteryNumbers(numbers: List): Boolean { + val numbersSet = hashSetOf(numbers) + return numbersSet.size != numbers.size + } + + companion object { + private val BASE_NUMBERS = (LotteryNumber.MIN_LOTTERY_NUMBER..LotteryNumber.MAX_LOTTERY_NUMBER).toList() + private const val LOTTERY_NUMBER_SIZE = 6 + fun makeAutoLottery(): Lottery { + return Lottery(BASE_NUMBERS.shuffled().take(LOTTERY_NUMBER_SIZE).sorted().map { LotteryNumber(it) }) + } + } +} + +data class LotteryNumber(private val number: Int) { + init { + require(number < MIN_LOTTERY_NUMBER) { "로또 번호는 1보다 커야합니다." } + require(number > MAX_LOTTERY_NUMBER) { "로또 번호는 45보다 작아야합니다." } + } + + companion object { + const val MAX_LOTTERY_NUMBER = 45 + const val MIN_LOTTERY_NUMBER = 1 + } +} + +class LotteryTest : StringSpec({ + "로또의 숫자는 6개이다." { + val lottery = Lottery.makeAutoLottery() + // lottery.size shouldBe 6 + } +}) From bcfc4f5108b683c6ac88c8ee30709bba72bfa6f8 Mon Sep 17 00:00:00 2001 From: "pavlo.v" Date: Thu, 22 Jun 2023 02:46:00 +0900 Subject: [PATCH 02/19] =?UTF-8?q?=EC=88=98=EC=A0=95=20:=20=EB=A1=9C?= =?UTF-8?q?=EB=98=90=EC=9D=98=20=EC=88=AB=EC=9E=90=EB=8A=94=206=EA=B0=9C?= =?UTF-8?q?=EC=9D=B4=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/kotlin/lottery/LotteryTest.kt | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/test/kotlin/lottery/LotteryTest.kt b/src/test/kotlin/lottery/LotteryTest.kt index 478a6c2389..c9d36a1392 100644 --- a/src/test/kotlin/lottery/LotteryTest.kt +++ b/src/test/kotlin/lottery/LotteryTest.kt @@ -1,12 +1,13 @@ package lottery import io.kotest.core.spec.style.StringSpec +import io.kotest.matchers.shouldBe class Lottery(numbers: List) { - private var lotteryNumbers: List + val lotteryNumbers: List init { - require(hasDuplicatedLotteryNumbers(numbers)) { "로또 번호에 중복되는 숫자가 있습니다." } + require(!hasDuplicatedLotteryNumbers(numbers)) { "로또 번호에 중복되는 숫자가 있습니다." } lotteryNumbers = numbers } @@ -14,9 +15,8 @@ class Lottery(numbers: List) { return Lottery(numbers.map { LotteryNumber(it) }) } - fun hasDuplicatedLotteryNumbers(numbers: List): Boolean { - val numbersSet = hashSetOf(numbers) - return numbersSet.size != numbers.size + private fun hasDuplicatedLotteryNumbers(numbers: List): Boolean { + return numbers.size != LOTTERY_NUMBER_SIZE } companion object { @@ -28,10 +28,11 @@ class Lottery(numbers: List) { } } -data class LotteryNumber(private val number: Int) { +@JvmInline +value class LotteryNumber(private val number: Int) { init { - require(number < MIN_LOTTERY_NUMBER) { "로또 번호는 1보다 커야합니다." } - require(number > MAX_LOTTERY_NUMBER) { "로또 번호는 45보다 작아야합니다." } + require(number >= MIN_LOTTERY_NUMBER) { "로또 번호는 1이상여야 합니다." } + require(number <= MAX_LOTTERY_NUMBER) { "로또 번호는 45이하이여야 합니다." } } companion object { @@ -41,8 +42,8 @@ data class LotteryNumber(private val number: Int) { } class LotteryTest : StringSpec({ - "로또의 숫자는 6개이다." { + "자동 로또 생성을 할 경우의 숫자의 개수는 6개이다." { val lottery = Lottery.makeAutoLottery() - // lottery.size shouldBe 6 + lottery.lotteryNumbers.size shouldBe 6 } }) From 070e274d377fe31f327152d988cbcc254d64f941 Mon Sep 17 00:00:00 2001 From: "pavlo.v" Date: Thu, 22 Jun 2023 03:11:11 +0900 Subject: [PATCH 03/19] =?UTF-8?q?=EC=88=98=EC=A0=95=20:=20=EB=A1=9C?= =?UTF-8?q?=EB=98=90=EC=9D=98=20=EB=B2=88=ED=98=B8=EB=8A=94=20=EC=A4=91?= =?UTF-8?q?=EB=B3=B5=EB=8F=BC=EC=84=9C=EB=8A=94=20=EC=95=88=EB=90=9C?= =?UTF-8?q?=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/kotlin/lottery/LotteryTest.kt | 31 +++++++++++++++++--------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/src/test/kotlin/lottery/LotteryTest.kt b/src/test/kotlin/lottery/LotteryTest.kt index c9d36a1392..6ef0e73b74 100644 --- a/src/test/kotlin/lottery/LotteryTest.kt +++ b/src/test/kotlin/lottery/LotteryTest.kt @@ -1,41 +1,45 @@ package lottery +import io.kotest.assertions.throwables.shouldThrow import io.kotest.core.spec.style.StringSpec import io.kotest.matchers.shouldBe -class Lottery(numbers: List) { - val lotteryNumbers: List +class Lottery(numbers: Set) { + val lotteryNumbers: Set init { require(!hasDuplicatedLotteryNumbers(numbers)) { "로또 번호에 중복되는 숫자가 있습니다." } lotteryNumbers = numbers } - fun makeManualLottery(numbers: List): Lottery { - return Lottery(numbers.map { LotteryNumber(it) }) - } - - private fun hasDuplicatedLotteryNumbers(numbers: List): Boolean { + private fun hasDuplicatedLotteryNumbers(numbers: Set): Boolean { return numbers.size != LOTTERY_NUMBER_SIZE } companion object { - private val BASE_NUMBERS = (LotteryNumber.MIN_LOTTERY_NUMBER..LotteryNumber.MAX_LOTTERY_NUMBER).toList() + private val BASE_NUMBERS = (LotteryNumber.MIN_LOTTERY_NUMBER..LotteryNumber.MAX_LOTTERY_NUMBER).toSet() private const val LOTTERY_NUMBER_SIZE = 6 fun makeAutoLottery(): Lottery { - return Lottery(BASE_NUMBERS.shuffled().take(LOTTERY_NUMBER_SIZE).sorted().map { LotteryNumber(it) }) + return Lottery( + BASE_NUMBERS.shuffled().take(LOTTERY_NUMBER_SIZE).sorted().map { LotteryNumber.get(it) } + .toSet() + ) } } } @JvmInline -value class LotteryNumber(private val number: Int) { +value class LotteryNumber private constructor( + private val number: Int, +) { init { require(number >= MIN_LOTTERY_NUMBER) { "로또 번호는 1이상여야 합니다." } require(number <= MAX_LOTTERY_NUMBER) { "로또 번호는 45이하이여야 합니다." } } companion object { + fun get(number: Int) = LotteryNumber(number) + const val MAX_LOTTERY_NUMBER = 45 const val MIN_LOTTERY_NUMBER = 1 } @@ -46,4 +50,11 @@ class LotteryTest : StringSpec({ val lottery = Lottery.makeAutoLottery() lottery.lotteryNumbers.size shouldBe 6 } + + "로또는 번호는 중복돼서는 안된다." { + val duplicatedNumbers = listOf(1, 2, 3, 1, 4, 5) + shouldThrow { + Lottery(duplicatedNumbers.map { LotteryNumber.get(it) }.toSet()) + } + } }) From 3695ccce377761adb44157d3259e33980ae65a06 Mon Sep 17 00:00:00 2001 From: "pavlo.v" Date: Thu, 22 Jun 2023 03:15:26 +0900 Subject: [PATCH 04/19] =?UTF-8?q?=EC=B6=94=EA=B0=80=20:=20=EB=A1=9C?= =?UTF-8?q?=EB=98=90=EC=9D=98=20=EA=B8=88=EC=95=A1=EC=9D=80=201000?= =?UTF-8?q?=EC=9B=90=EC=9D=B4=EB=8B=A4,=20=EB=A1=9C=EB=98=90=20=EC=88=AB?= =?UTF-8?q?=EC=9E=90=EB=8A=94=201=EC=9D=B4=EC=83=81=2045=EC=9D=B4=ED=95=98?= =?UTF-8?q?=EC=9D=B4=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 6 +++--- src/test/kotlin/lottery/LotteryTest.kt | 17 +++++++++++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 9495a6f5db..ca6a2428f8 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,11 @@ # kotlin-lotto []로또 구입 금액을 입력하면 구입 금액에 해당하는 로또를 발급해야 한다. -[]로또 1장의 가격은 1000원이다. +[X]로또 1장의 가격은 1000원이다. [X]로또의 숫자는 6개이다. -[]로또는 당첨 번호는 중복돼서는 안된다. +[X]로또는 당첨 번호는 중복돼서는 안된다. []로또의 수익률을 마지막에 구해주어야한다. (구매금액) / (당첨금액) -[]로또의 숫자는 1~45까지이다. +[X]로또의 숫자는 1~45까지이다. 요구사항 [X]쉼표(,) 또는 콜론(:)을 구분자로 가지는 문자열을 전달하는 경우 구분자를 기준으로 분리한 각 숫자의 합을 반환 (예: “” => 0, "1,2" => 3, " diff --git a/src/test/kotlin/lottery/LotteryTest.kt b/src/test/kotlin/lottery/LotteryTest.kt index 6ef0e73b74..428c466f95 100644 --- a/src/test/kotlin/lottery/LotteryTest.kt +++ b/src/test/kotlin/lottery/LotteryTest.kt @@ -19,6 +19,7 @@ class Lottery(numbers: Set) { companion object { private val BASE_NUMBERS = (LotteryNumber.MIN_LOTTERY_NUMBER..LotteryNumber.MAX_LOTTERY_NUMBER).toSet() private const val LOTTERY_NUMBER_SIZE = 6 + const val LOTTERY_PRICE = 1000 fun makeAutoLottery(): Lottery { return Lottery( BASE_NUMBERS.shuffled().take(LOTTERY_NUMBER_SIZE).sorted().map { LotteryNumber.get(it) } @@ -57,4 +58,20 @@ class LotteryTest : StringSpec({ Lottery(duplicatedNumbers.map { LotteryNumber.get(it) }.toSet()) } } + + "로또 한장의 금액은 1000원 이다." { + Lottery.LOTTERY_PRICE shouldBe 1000 + } + + "로또 숫자의 크기는 1이상 이여야한다." { + shouldThrow { + LotteryNumber.get(0) + } + } + + "로또 숫자의 크기는 45이하여야 한다." { + shouldThrow { + LotteryNumber.get(46) + } + } }) From 1a854bf974a8ea86bd1c0b8bb942e1596fec28d9 Mon Sep 17 00:00:00 2001 From: "pavlo.v" Date: Thu, 22 Jun 2023 03:18:47 +0900 Subject: [PATCH 05/19] =?UTF-8?q?=ED=8C=8C=EC=9D=BC=20=EB=B6=84=EB=A6=AC?= =?UTF-8?q?=ED=99=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/lottery/Lottery.kt | 26 ++++++++++ src/main/kotlin/lottery/LotteryNumber.kt | 18 +++++++ src/test/kotlin/lottery/LotteryGameTest.kt | 6 +++ src/test/kotlin/lottery/LotteryNumberTest.kt | 18 +++++++ src/test/kotlin/lottery/LotteryTest.kt | 53 +------------------- 5 files changed, 69 insertions(+), 52 deletions(-) create mode 100644 src/main/kotlin/lottery/Lottery.kt create mode 100644 src/main/kotlin/lottery/LotteryNumber.kt create mode 100644 src/test/kotlin/lottery/LotteryGameTest.kt create mode 100644 src/test/kotlin/lottery/LotteryNumberTest.kt diff --git a/src/main/kotlin/lottery/Lottery.kt b/src/main/kotlin/lottery/Lottery.kt new file mode 100644 index 0000000000..ba2dc555f0 --- /dev/null +++ b/src/main/kotlin/lottery/Lottery.kt @@ -0,0 +1,26 @@ +package lottery + +class Lottery(numbers: Set) { + val lotteryNumbers: Set + + init { + require(!hasDuplicatedLotteryNumbers(numbers)) { "로또 번호에 중복되는 숫자가 있습니다." } + lotteryNumbers = numbers + } + + private fun hasDuplicatedLotteryNumbers(numbers: Set): Boolean { + return numbers.size != LOTTERY_NUMBER_SIZE + } + + companion object { + private val BASE_NUMBERS = (LotteryNumber.MIN_LOTTERY_NUMBER..LotteryNumber.MAX_LOTTERY_NUMBER).toSet() + private const val LOTTERY_NUMBER_SIZE = 6 + const val LOTTERY_PRICE = 1000 + fun makeAutoLottery(): Lottery { + return Lottery( + BASE_NUMBERS.shuffled().take(LOTTERY_NUMBER_SIZE).sorted().map { LotteryNumber.get(it) } + .toSet() + ) + } + } +} diff --git a/src/main/kotlin/lottery/LotteryNumber.kt b/src/main/kotlin/lottery/LotteryNumber.kt new file mode 100644 index 0000000000..9e5e0406de --- /dev/null +++ b/src/main/kotlin/lottery/LotteryNumber.kt @@ -0,0 +1,18 @@ +package lottery + +@JvmInline +value class LotteryNumber private constructor( + private val number: Int, +) { + init { + require(number >= MIN_LOTTERY_NUMBER) { "로또 번호는 1이상여야 합니다." } + require(number <= MAX_LOTTERY_NUMBER) { "로또 번호는 45이하이여야 합니다." } + } + + companion object { + fun get(number: Int) = LotteryNumber(number) + + const val MAX_LOTTERY_NUMBER = 45 + const val MIN_LOTTERY_NUMBER = 1 + } +} diff --git a/src/test/kotlin/lottery/LotteryGameTest.kt b/src/test/kotlin/lottery/LotteryGameTest.kt new file mode 100644 index 0000000000..e27189a3fd --- /dev/null +++ b/src/test/kotlin/lottery/LotteryGameTest.kt @@ -0,0 +1,6 @@ +package lottery + +import io.kotest.core.spec.style.StringSpec + +class LotteryGameTest : StringSpec({ +}) diff --git a/src/test/kotlin/lottery/LotteryNumberTest.kt b/src/test/kotlin/lottery/LotteryNumberTest.kt new file mode 100644 index 0000000000..6c1092a013 --- /dev/null +++ b/src/test/kotlin/lottery/LotteryNumberTest.kt @@ -0,0 +1,18 @@ +package lottery + +import io.kotest.assertions.throwables.shouldThrow +import io.kotest.core.spec.style.StringSpec + +class LotteryNumberTest : StringSpec({ + "로또 숫자의 크기는 1이상 이여야한다." { + shouldThrow { + LotteryNumber.get(0) + } + } + + "로또 숫자의 크기는 45이하여야 한다." { + shouldThrow { + LotteryNumber.get(46) + } + } +}) diff --git a/src/test/kotlin/lottery/LotteryTest.kt b/src/test/kotlin/lottery/LotteryTest.kt index 428c466f95..e25a4021b0 100644 --- a/src/test/kotlin/lottery/LotteryTest.kt +++ b/src/test/kotlin/lottery/LotteryTest.kt @@ -4,48 +4,6 @@ import io.kotest.assertions.throwables.shouldThrow import io.kotest.core.spec.style.StringSpec import io.kotest.matchers.shouldBe -class Lottery(numbers: Set) { - val lotteryNumbers: Set - - init { - require(!hasDuplicatedLotteryNumbers(numbers)) { "로또 번호에 중복되는 숫자가 있습니다." } - lotteryNumbers = numbers - } - - private fun hasDuplicatedLotteryNumbers(numbers: Set): Boolean { - return numbers.size != LOTTERY_NUMBER_SIZE - } - - companion object { - private val BASE_NUMBERS = (LotteryNumber.MIN_LOTTERY_NUMBER..LotteryNumber.MAX_LOTTERY_NUMBER).toSet() - private const val LOTTERY_NUMBER_SIZE = 6 - const val LOTTERY_PRICE = 1000 - fun makeAutoLottery(): Lottery { - return Lottery( - BASE_NUMBERS.shuffled().take(LOTTERY_NUMBER_SIZE).sorted().map { LotteryNumber.get(it) } - .toSet() - ) - } - } -} - -@JvmInline -value class LotteryNumber private constructor( - private val number: Int, -) { - init { - require(number >= MIN_LOTTERY_NUMBER) { "로또 번호는 1이상여야 합니다." } - require(number <= MAX_LOTTERY_NUMBER) { "로또 번호는 45이하이여야 합니다." } - } - - companion object { - fun get(number: Int) = LotteryNumber(number) - - const val MAX_LOTTERY_NUMBER = 45 - const val MIN_LOTTERY_NUMBER = 1 - } -} - class LotteryTest : StringSpec({ "자동 로또 생성을 할 경우의 숫자의 개수는 6개이다." { val lottery = Lottery.makeAutoLottery() @@ -63,15 +21,6 @@ class LotteryTest : StringSpec({ Lottery.LOTTERY_PRICE shouldBe 1000 } - "로또 숫자의 크기는 1이상 이여야한다." { - shouldThrow { - LotteryNumber.get(0) - } - } - - "로또 숫자의 크기는 45이하여야 한다." { - shouldThrow { - LotteryNumber.get(46) - } + "로또 구입 금액을 입력하면 구입 금액에 해당하는 로또를 발급해야 한다." { } }) From 5c88c78669f7aa744b38bf8a9fb8e07a942c0fc9 Mon Sep 17 00:00:00 2001 From: "pavlo.v" Date: Thu, 22 Jun 2023 03:42:31 +0900 Subject: [PATCH 06/19] =?UTF-8?q?=EC=B6=94=EA=B0=80=20:=20=EB=A1=9C?= =?UTF-8?q?=EB=98=90=20=EA=B5=AC=EC=9E=85=20=EA=B8=88=EC=95=A1=EC=9D=84=20?= =?UTF-8?q?=EC=9E=85=EB=A0=A5=ED=95=98=EB=A9=B4=20=EA=B5=AC=EC=9E=85=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=EB=A5=BC=20=EB=B0=9C=EA=B8=89?= =?UTF-8?q?=ED=95=B4=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/lottery/Lotteries.kt | 15 +++++++++++++++ src/test/kotlin/lottery/LotteryGameTest.kt | 16 ++++++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 src/main/kotlin/lottery/Lotteries.kt diff --git a/README.md b/README.md index ca6a2428f8..2c449e44c6 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # kotlin-lotto -[]로또 구입 금액을 입력하면 구입 금액에 해당하는 로또를 발급해야 한다. +[X]로또 구입 금액을 입력하면 구입 금액에 해당하는 로또를 발급해야 한다. [X]로또 1장의 가격은 1000원이다. [X]로또의 숫자는 6개이다. [X]로또는 당첨 번호는 중복돼서는 안된다. diff --git a/src/main/kotlin/lottery/Lotteries.kt b/src/main/kotlin/lottery/Lotteries.kt new file mode 100644 index 0000000000..e6bb61ff23 --- /dev/null +++ b/src/main/kotlin/lottery/Lotteries.kt @@ -0,0 +1,15 @@ +package lottery + +class Lotteries { + val lotteries = mutableListOf() + + companion object { + fun makeAutoLotteries(number: Int): Lotteries { + val lotteries = Lotteries() + for (i in 0 until number) { + lotteries.lotteries.add(Lottery.makeAutoLottery()) + } + return lotteries + } + } +} diff --git a/src/test/kotlin/lottery/LotteryGameTest.kt b/src/test/kotlin/lottery/LotteryGameTest.kt index e27189a3fd..264de6f4db 100644 --- a/src/test/kotlin/lottery/LotteryGameTest.kt +++ b/src/test/kotlin/lottery/LotteryGameTest.kt @@ -1,6 +1,22 @@ package lottery import io.kotest.core.spec.style.StringSpec +import io.kotest.matchers.shouldBe + +class LotteryGame { + lateinit var winningLottery: Lottery + fun purchaseAutoLotteries(purchasePrice: Int): Lotteries { + return Lotteries.makeAutoLotteries(purchasePrice / Lottery.LOTTERY_PRICE) + } + + companion object { + } +} class LotteryGameTest : StringSpec({ + "로또 구입 금액을 입력하면 구입 금액에 해당하는 로또를 발급해야 한다." { + val lotteryGame = LotteryGame() + val purchaseLotteries = lotteryGame.purchaseAutoLotteries(14000) + purchaseLotteries.lotteries.size shouldBe 14 + } }) From 786c4e62a70661f25e78f447117d9baae8cb39a9 Mon Sep 17 00:00:00 2001 From: "pavlo.v" Date: Thu, 22 Jun 2023 16:39:18 +0900 Subject: [PATCH 07/19] =?UTF-8?q?=EC=B6=94=EA=B0=80=20:=20=EB=A1=9C?= =?UTF-8?q?=EB=98=90=20=ED=99=94=EB=A9=B4=20=EC=B6=9C=EB=A0=A5=EC=9D=84=20?= =?UTF-8?q?=EC=9C=84=ED=95=9C=20View=20=ED=81=B4=EB=9E=98=EC=8A=A4=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/lottery/LotteryGameView.kt | 33 ++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 src/main/kotlin/lottery/LotteryGameView.kt diff --git a/src/main/kotlin/lottery/LotteryGameView.kt b/src/main/kotlin/lottery/LotteryGameView.kt new file mode 100644 index 0000000000..1dac218dd7 --- /dev/null +++ b/src/main/kotlin/lottery/LotteryGameView.kt @@ -0,0 +1,33 @@ +package lottery + +class LotteryGameView { + fun printPurchaseMoneyView() { + println("구입 금액을 입력해 주세요.") + } + + fun printPurchaseLotteryView(number: Int) { + println("{$number}개를 구매했습니다.") + } + + fun printLotteriesNumber(lotteries: Lotteries) { + lotteries.lotteries.forEach { + println(it.lotteryNumbers) + } + } + + fun printWinnerLotteryNumber() { + println("지난 주 당첨 번호를 입력해 주세요.") + } + + fun printLotteryRankView(lotteryRank: LotteryRank) { + println("당첨 통계") + println("---------") + lotteryRank.lotteriesRank.forEach { (prize, number) -> + println("${prize.correctCount}개 일치 (${prize.rewardMoney}원)- ${number}개") + } + } + + fun printProfitView(profit: Float) { + println("총 수익률은 ${profit}입니다.") + } +} From 10a286d2c97397c7d9897f24f36d8bcbab55a9f5 Mon Sep 17 00:00:00 2001 From: "pavlo.v" Date: Mon, 3 Jul 2023 02:56:29 +0900 Subject: [PATCH 08/19] =?UTF-8?q?=EC=B6=94=EA=B0=80=20:=20=EC=9D=B4?= =?UTF-8?q?=EC=A0=84=20=EC=BD=94=EB=93=9C=EB=A6=AC=EB=B7=B0=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/stringcalculator/StringAddCalculator.kt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/kotlin/stringcalculator/StringAddCalculator.kt b/src/main/kotlin/stringcalculator/StringAddCalculator.kt index 9cb9042129..1fe722ed56 100644 --- a/src/main/kotlin/stringcalculator/StringAddCalculator.kt +++ b/src/main/kotlin/stringcalculator/StringAddCalculator.kt @@ -2,9 +2,11 @@ package stringcalculator class StringAddCalculator { private val delimiters = Delimiters() + fun calculate(text: String?): Int { if (text.isNullOrEmpty()) return 0 val parseText = StringParser.deleteCustomDelimiters(text, delimiters) + return parseText.split(delimiters.getDelimitersRegex()).sumOf { if (it.toInt() < 0) throw RuntimeException() it.toInt() From 85049917b2f3e74ca2ea8fd97af0a424ccadda47 Mon Sep 17 00:00:00 2001 From: "pavlo.v" Date: Mon, 3 Jul 2023 03:37:11 +0900 Subject: [PATCH 09/19] =?UTF-8?q?=EC=B6=94=EA=B0=80=20:=20mutable=20?= =?UTF-8?q?=EC=88=98=EC=A0=95=20=EB=B0=8F=20object=20=ED=81=B4=EB=9E=98?= =?UTF-8?q?=EC=8A=A4=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kotlin/stringcalculator/Delimiters.kt | 35 ++++++++++++++++--- .../stringcalculator/StringAddCalculator.kt | 5 +-- .../kotlin/stringcalculator/StringParser.kt | 16 --------- .../kotlin/stringcalculator/DelimitersTest.kt | 14 -------- .../stringcalculator/StringParserTest.kt | 11 ++++++ 5 files changed, 44 insertions(+), 37 deletions(-) delete mode 100644 src/main/kotlin/stringcalculator/StringParser.kt create mode 100644 src/test/kotlin/stringcalculator/StringParserTest.kt diff --git a/src/main/kotlin/stringcalculator/Delimiters.kt b/src/main/kotlin/stringcalculator/Delimiters.kt index 3fc31b9eb5..9e4bc60b01 100644 --- a/src/main/kotlin/stringcalculator/Delimiters.kt +++ b/src/main/kotlin/stringcalculator/Delimiters.kt @@ -1,11 +1,36 @@ package stringcalculator -class Delimiters { - val delimiters = mutableListOf(DEFAULT_DELIMITER_COMMA, DEFAULT_DELIMITER_COLON) +import stringcalculator.Delimiters.Companion.DEFAULT_DELIMITER_COLON +import stringcalculator.Delimiters.Companion.DEFAULT_DELIMITER_COMMA - fun addDelimiter(delimiter: String) { - delimiters.add(delimiter) +object StringParser { + private const val CUSTOM_DELIMITER_INDEX = 1 + private const val WITHOUT_CUSTOM_DELIMITER_EXPRESSION_INDEX = 2 + + fun getDelimitersFromString(text: String): Delimiters { + val matchResult = Regex(Delimiters.CUSTOM_DELIMITER_FIND_REGEX).find(text) + matchResult?.let { + return Delimiters( + listOf( + DEFAULT_DELIMITER_COMMA, + DEFAULT_DELIMITER_COLON, + it.groupValues[CUSTOM_DELIMITER_INDEX] + ) + ) + } + return Delimiters() + } + + fun deleteCustomDelimiters(text: String): String { + val matchResult = Regex(Delimiters.CUSTOM_DELIMITER_FIND_REGEX).find(text) + matchResult?.let { + return it.groupValues[WITHOUT_CUSTOM_DELIMITER_EXPRESSION_INDEX] + } + return text } +} + +class Delimiters(val delimiters: List = listOf(DEFAULT_DELIMITER_COMMA, DEFAULT_DELIMITER_COLON)) { fun getDelimitersRegex(): Regex { return delimiters.joinToString("", "[", "]").toRegex() @@ -14,6 +39,6 @@ class Delimiters { companion object { const val DEFAULT_DELIMITER_COMMA = "," const val DEFAULT_DELIMITER_COLON = ":" - const val CUSTOM_DELIMITER_FIND_REGEX = "//(.)\n(.*)" + const val CUSTOM_DELIMITER_FIND_REGEX = "//(.*)\n(.*)" } } diff --git a/src/main/kotlin/stringcalculator/StringAddCalculator.kt b/src/main/kotlin/stringcalculator/StringAddCalculator.kt index 1fe722ed56..277e1a0658 100644 --- a/src/main/kotlin/stringcalculator/StringAddCalculator.kt +++ b/src/main/kotlin/stringcalculator/StringAddCalculator.kt @@ -1,11 +1,12 @@ package stringcalculator class StringAddCalculator { - private val delimiters = Delimiters() + private lateinit var delimiters: Delimiters fun calculate(text: String?): Int { if (text.isNullOrEmpty()) return 0 - val parseText = StringParser.deleteCustomDelimiters(text, delimiters) + delimiters = StringParser.getDelimitersFromString(text) + val parseText = StringParser.deleteCustomDelimiters(text) return parseText.split(delimiters.getDelimitersRegex()).sumOf { if (it.toInt() < 0) throw RuntimeException() diff --git a/src/main/kotlin/stringcalculator/StringParser.kt b/src/main/kotlin/stringcalculator/StringParser.kt deleted file mode 100644 index 7e399f9173..0000000000 --- a/src/main/kotlin/stringcalculator/StringParser.kt +++ /dev/null @@ -1,16 +0,0 @@ -package stringcalculator - -class StringParser { - companion object { - private const val CUSTOM_DELIMITER_INDEX = 1 - private const val WITHOUT_CUSTOM_DELIMITER_EXPRESSION_INDEX = 2 - fun deleteCustomDelimiters(text: String, delimiters: Delimiters): String { - val matchResult = Regex(Delimiters.CUSTOM_DELIMITER_FIND_REGEX).find(text) - matchResult?.let { - delimiters.addDelimiter(it.groupValues[CUSTOM_DELIMITER_INDEX]) - return it.groupValues[WITHOUT_CUSTOM_DELIMITER_EXPRESSION_INDEX] - } - return text - } - } -} diff --git a/src/test/kotlin/stringcalculator/DelimitersTest.kt b/src/test/kotlin/stringcalculator/DelimitersTest.kt index 2b2ff192b5..c408b90b56 100644 --- a/src/test/kotlin/stringcalculator/DelimitersTest.kt +++ b/src/test/kotlin/stringcalculator/DelimitersTest.kt @@ -1,8 +1,6 @@ package stringcalculator import io.kotest.core.spec.style.StringSpec -import io.kotest.inspectors.forAll -import io.kotest.matchers.collections.shouldContain import io.kotest.matchers.collections.shouldContainAll import io.kotest.matchers.shouldBe @@ -11,20 +9,8 @@ class DelimitersTest : StringSpec({ val delimiters = Delimiters() delimiters.delimiters shouldContainAll listOf(",", ":") } - "새로운 커스텀한 구분자를 추가할 수 있다." { - val delimiters = Delimiters() - delimiters.addDelimiter("&&") - delimiters.delimiters shouldContain "&&" - } "구분자들을 통해 정규식을 만들어준다." { val delimiters = Delimiters() delimiters.getDelimitersRegex() shouldBe "[,:]".toRegex() } - "커스텀한 구분자를 추가할 경우 정규식에 포함된다." { - listOf("#", "?", "^^").forAll { - val delimiters = Delimiters() - delimiters.addDelimiter(it) - delimiters.getDelimitersRegex() shouldBe "[,:$it]".toRegex() - } - } }) diff --git a/src/test/kotlin/stringcalculator/StringParserTest.kt b/src/test/kotlin/stringcalculator/StringParserTest.kt new file mode 100644 index 0000000000..1f2f5ac7ec --- /dev/null +++ b/src/test/kotlin/stringcalculator/StringParserTest.kt @@ -0,0 +1,11 @@ +package stringcalculator + +import io.kotest.core.spec.style.StringSpec +import io.kotest.matchers.collections.shouldContain + +class StringParserTest : StringSpec({ + "//{구분자}\n 사이에 들어온 문자열은 커스텀한 구분자에 추가된다.." { + val delimiters = StringParser.getDelimitersFromString("//^^\n1^^2") + delimiters.delimiters shouldContain "^^" + } +}) From a3167302b93eaa6ddfbf436a4206bdf2ba8cb497 Mon Sep 17 00:00:00 2001 From: "pavlo.v" Date: Mon, 3 Jul 2023 16:25:43 +0900 Subject: [PATCH 10/19] =?UTF-8?q?=EC=B6=94=EA=B0=80=20:=20LotteryGameView?= =?UTF-8?q?=20object=20=EB=A1=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/lottery/LotteryGameView.kt | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/main/kotlin/lottery/LotteryGameView.kt b/src/main/kotlin/lottery/LotteryGameView.kt index 1dac218dd7..2ef46424c9 100644 --- a/src/main/kotlin/lottery/LotteryGameView.kt +++ b/src/main/kotlin/lottery/LotteryGameView.kt @@ -1,18 +1,21 @@ package lottery -class LotteryGameView { +object LotteryGameView { + + private const val NONE_PRIZE_INDEX = 1 fun printPurchaseMoneyView() { println("구입 금액을 입력해 주세요.") } fun printPurchaseLotteryView(number: Int) { - println("{$number}개를 구매했습니다.") + println("${number}개를 구매했습니다.") } fun printLotteriesNumber(lotteries: Lotteries) { lotteries.lotteries.forEach { println(it.lotteryNumbers) } + println() } fun printWinnerLotteryNumber() { @@ -22,12 +25,12 @@ class LotteryGameView { fun printLotteryRankView(lotteryRank: LotteryRank) { println("당첨 통계") println("---------") - lotteryRank.lotteriesRank.forEach { (prize, number) -> - println("${prize.correctCount}개 일치 (${prize.rewardMoney}원)- ${number}개") + lotteryRank.lotteriesRank.drop(NONE_PRIZE_INDEX).forEach { + println("${it.correctCount}개 일치 (${it.rewardMoney}원)- ${it.count}개") } } - fun printProfitView(profit: Float) { - println("총 수익률은 ${profit}입니다.") + fun printProfitView(profit: Double) { + println("총 수익률은" + String.format("%.2f", profit) + " 입니다.") } } From 092ac1352bb62d6ee982fda682b1ec30c177f560 Mon Sep 17 00:00:00 2001 From: "pavlo.v" Date: Mon, 3 Jul 2023 16:26:29 +0900 Subject: [PATCH 11/19] =?UTF-8?q?=EC=B6=94=EA=B0=80=20:=20LotteryGame,=20L?= =?UTF-8?q?otteryGameTest=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/kotlin/lottery/LotteryGameTest.kt | 10 ---------- src/test/kotlin/lottery/LotteryTest.kt | 3 --- 2 files changed, 13 deletions(-) diff --git a/src/test/kotlin/lottery/LotteryGameTest.kt b/src/test/kotlin/lottery/LotteryGameTest.kt index 264de6f4db..5b70712730 100644 --- a/src/test/kotlin/lottery/LotteryGameTest.kt +++ b/src/test/kotlin/lottery/LotteryGameTest.kt @@ -3,16 +3,6 @@ package lottery import io.kotest.core.spec.style.StringSpec import io.kotest.matchers.shouldBe -class LotteryGame { - lateinit var winningLottery: Lottery - fun purchaseAutoLotteries(purchasePrice: Int): Lotteries { - return Lotteries.makeAutoLotteries(purchasePrice / Lottery.LOTTERY_PRICE) - } - - companion object { - } -} - class LotteryGameTest : StringSpec({ "로또 구입 금액을 입력하면 구입 금액에 해당하는 로또를 발급해야 한다." { val lotteryGame = LotteryGame() diff --git a/src/test/kotlin/lottery/LotteryTest.kt b/src/test/kotlin/lottery/LotteryTest.kt index e25a4021b0..586361b50a 100644 --- a/src/test/kotlin/lottery/LotteryTest.kt +++ b/src/test/kotlin/lottery/LotteryTest.kt @@ -20,7 +20,4 @@ class LotteryTest : StringSpec({ "로또 한장의 금액은 1000원 이다." { Lottery.LOTTERY_PRICE shouldBe 1000 } - - "로또 구입 금액을 입력하면 구입 금액에 해당하는 로또를 발급해야 한다." { - } }) From 9b2f46807509569f754711ac255979427299764c Mon Sep 17 00:00:00 2001 From: "pavlo.v" Date: Mon, 3 Jul 2023 16:27:15 +0900 Subject: [PATCH 12/19] =?UTF-8?q?=EC=B6=94=EA=B0=80=20:=20=EB=A1=9C?= =?UTF-8?q?=EB=98=90=20=EB=8B=B9=EC=B2=A8=20=ED=9A=9F=EC=88=98=20=EA=B0=81?= =?UTF-8?q?=EA=B0=81=20=EA=B5=AC=ED=95=98=EA=B8=B0,=20=EC=88=98=EC=9D=B5?= =?UTF-8?q?=EB=A5=A0=20=EA=B5=AC=ED=95=98=EA=B8=B0=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 3 ++- src/main/kotlin/lottery/Lottery.kt | 10 +++++++++- src/main/kotlin/lottery/LotteryNumber.kt | 4 ++++ src/main/kotlin/lottery/LotteryPrize.kt | 15 +++++++++++++++ src/main/kotlin/lottery/LotteryRank.kt | 14 ++++++++++++++ src/main/kotlin/lottery/WinningLottery.kt | 14 ++++++++++++++ 6 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 src/main/kotlin/lottery/LotteryPrize.kt create mode 100644 src/main/kotlin/lottery/LotteryRank.kt create mode 100644 src/main/kotlin/lottery/WinningLottery.kt diff --git a/README.md b/README.md index 2c449e44c6..63888deb49 100644 --- a/README.md +++ b/README.md @@ -4,8 +4,9 @@ [X]로또 1장의 가격은 1000원이다. [X]로또의 숫자는 6개이다. [X]로또는 당첨 번호는 중복돼서는 안된다. -[]로또의 수익률을 마지막에 구해주어야한다. (구매금액) / (당첨금액) +[X]로또의 수익률을 마지막에 구해주어야한다. (구매금액) / (당첨금액) [X]로또의 숫자는 1~45까지이다. +[X]로또의 당첨 횟수를 각각 구해야한다. 요구사항 [X]쉼표(,) 또는 콜론(:)을 구분자로 가지는 문자열을 전달하는 경우 구분자를 기준으로 분리한 각 숫자의 합을 반환 (예: “” => 0, "1,2" => 3, " diff --git a/src/main/kotlin/lottery/Lottery.kt b/src/main/kotlin/lottery/Lottery.kt index ba2dc555f0..955ff6145a 100644 --- a/src/main/kotlin/lottery/Lottery.kt +++ b/src/main/kotlin/lottery/Lottery.kt @@ -12,9 +12,17 @@ class Lottery(numbers: Set) { return numbers.size != LOTTERY_NUMBER_SIZE } + fun checkRank(numbers: Set): Int { + var correctCount = 0 + numbers.forEach { + if (lotteryNumbers.contains(it)) correctCount++ + } + return correctCount + } + companion object { private val BASE_NUMBERS = (LotteryNumber.MIN_LOTTERY_NUMBER..LotteryNumber.MAX_LOTTERY_NUMBER).toSet() - private const val LOTTERY_NUMBER_SIZE = 6 + const val LOTTERY_NUMBER_SIZE = 6 const val LOTTERY_PRICE = 1000 fun makeAutoLottery(): Lottery { return Lottery( diff --git a/src/main/kotlin/lottery/LotteryNumber.kt b/src/main/kotlin/lottery/LotteryNumber.kt index 9e5e0406de..d98715daf4 100644 --- a/src/main/kotlin/lottery/LotteryNumber.kt +++ b/src/main/kotlin/lottery/LotteryNumber.kt @@ -15,4 +15,8 @@ value class LotteryNumber private constructor( const val MAX_LOTTERY_NUMBER = 45 const val MIN_LOTTERY_NUMBER = 1 } + + override fun toString(): String { + return "$number" + } } diff --git a/src/main/kotlin/lottery/LotteryPrize.kt b/src/main/kotlin/lottery/LotteryPrize.kt new file mode 100644 index 0000000000..c4d27a5d56 --- /dev/null +++ b/src/main/kotlin/lottery/LotteryPrize.kt @@ -0,0 +1,15 @@ +package lottery + +enum class LotteryPrize(val correctCount: Int, val rewardMoney: Int, var count: Int = 0) { + NONE(0, 0), + FORTH(3, 5_000), + THIRD(4, 50_000), + SECOND(5, 1_500_000), + FIRST(6, 2_000_000_000), ; + + companion object { + fun get(correctCount: Int): LotteryPrize? { + return LotteryPrize.values().find { it.correctCount == correctCount } + } + } +} diff --git a/src/main/kotlin/lottery/LotteryRank.kt b/src/main/kotlin/lottery/LotteryRank.kt new file mode 100644 index 0000000000..54e3c2c3a6 --- /dev/null +++ b/src/main/kotlin/lottery/LotteryRank.kt @@ -0,0 +1,14 @@ +package lottery + +class LotteryRank { + var lotteriesRank: List = LotteryPrize.values().toList() + + fun plusRank(rank: LotteryPrize) = ++lotteriesRank.find { it == rank }!!.count + fun calculateProfit(money: Int): Double { + var total = 0.0 + lotteriesRank.forEach { + total += it.rewardMoney * it.count + } + return total / money + } +} diff --git a/src/main/kotlin/lottery/WinningLottery.kt b/src/main/kotlin/lottery/WinningLottery.kt new file mode 100644 index 0000000000..b88258881d --- /dev/null +++ b/src/main/kotlin/lottery/WinningLottery.kt @@ -0,0 +1,14 @@ +package lottery + +class WinningLottery(numbers: Set) { + val winningNumbers: Set + + init { + require(!hasDuplicatedLotteryNumbers(numbers)) { "로또 번호에 중복되는 숫자가 있습니다." } + winningNumbers = numbers + } + + private fun hasDuplicatedLotteryNumbers(numbers: Set): Boolean { + return numbers.size != Lottery.LOTTERY_NUMBER_SIZE + } +} From e4875eb04c0cd1c8e0b239031db5b9ee94db675d Mon Sep 17 00:00:00 2001 From: "pavlo.v" Date: Mon, 3 Jul 2023 16:27:35 +0900 Subject: [PATCH 13/19] =?UTF-8?q?=EC=B6=94=EA=B0=80=20:=20=EC=8B=A4?= =?UTF-8?q?=ED=96=89=ED=95=A0=EC=88=98=20=EC=9E=88=EB=8A=94=20main=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/lottery/LotteryGame.kt | 32 ++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 src/main/kotlin/lottery/LotteryGame.kt diff --git a/src/main/kotlin/lottery/LotteryGame.kt b/src/main/kotlin/lottery/LotteryGame.kt new file mode 100644 index 0000000000..ef677c88a9 --- /dev/null +++ b/src/main/kotlin/lottery/LotteryGame.kt @@ -0,0 +1,32 @@ +package lottery + +class LotteryGame { + private lateinit var winningLottery: WinningLottery + private val lotteryRank = LotteryRank() + fun purchaseAutoLotteries(purchasePrice: Int): Lotteries { + return Lotteries.makeAutoLotteries(purchasePrice / Lottery.LOTTERY_PRICE) + } + + fun start() { + LotteryGameView.printPurchaseMoneyView() + val money = readln().toInt() + val purchaseAutoLotteries = purchaseAutoLotteries(money) + LotteryGameView.printPurchaseLotteryView(purchaseAutoLotteries.lotteries.size) + LotteryGameView.printLotteriesNumber(purchaseAutoLotteries) + LotteryGameView.printWinnerLotteryNumber() + val numbers = readln().replace("\\s".toRegex(), "").split(",") + winningLottery = WinningLottery(numbers.map { LotteryNumber.get(it.toInt()) }.toSet()) + + purchaseAutoLotteries.lotteries.forEach { + val checkRank = it.checkRank(winningLottery.winningNumbers) + lotteryRank.plusRank(LotteryPrize.get(checkRank) ?: LotteryPrize.NONE) + } + LotteryGameView.printLotteryRankView(lotteryRank) + LotteryGameView.printProfitView(lotteryRank.calculateProfit(money)) + } +} + +fun main() { + val game = LotteryGame() + game.start() +} From 656bbb108c1a11ea9585c442dd13ea019ec45ea6 Mon Sep 17 00:00:00 2001 From: "pavlo.v" Date: Mon, 3 Jul 2023 16:29:41 +0900 Subject: [PATCH 14/19] =?UTF-8?q?=EC=B6=94=EA=B0=80=20:=20WinningLotteryTe?= =?UTF-8?q?st=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/kotlin/lottery/WinningLotteryTest.kt | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 src/test/kotlin/lottery/WinningLotteryTest.kt diff --git a/src/test/kotlin/lottery/WinningLotteryTest.kt b/src/test/kotlin/lottery/WinningLotteryTest.kt new file mode 100644 index 0000000000..c5074548d0 --- /dev/null +++ b/src/test/kotlin/lottery/WinningLotteryTest.kt @@ -0,0 +1,13 @@ +package lottery + +import io.kotest.assertions.throwables.shouldThrow +import io.kotest.core.spec.style.StringSpec + +class WinningLotteryTest : StringSpec({ + "당첨 번호는 중복되어서는 안된다." { + val duplicatedNumbers = listOf(1, 2, 3, 1, 4, 5) + shouldThrow { + WinningLottery(duplicatedNumbers.map { LotteryNumber.get(it) }.toSet()) + } + } +}) From ddfa00c46795f065f274c91669713f32c9b2aed4 Mon Sep 17 00:00:00 2001 From: "pavlo.v" Date: Mon, 3 Jul 2023 16:52:57 +0900 Subject: [PATCH 15/19] =?UTF-8?q?=EC=B6=94=EA=B0=80=20:=20LotteryPrizeTest?= =?UTF-8?q?=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/lottery/LotteryRank.kt | 3 +-- src/test/kotlin/lottery/LotteryPrizeTest.kt | 13 +++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) create mode 100644 src/test/kotlin/lottery/LotteryPrizeTest.kt diff --git a/src/main/kotlin/lottery/LotteryRank.kt b/src/main/kotlin/lottery/LotteryRank.kt index 54e3c2c3a6..079cf08ba2 100644 --- a/src/main/kotlin/lottery/LotteryRank.kt +++ b/src/main/kotlin/lottery/LotteryRank.kt @@ -1,8 +1,7 @@ package lottery class LotteryRank { - var lotteriesRank: List = LotteryPrize.values().toList() - + val lotteriesRank: List = LotteryPrize.values().toList() fun plusRank(rank: LotteryPrize) = ++lotteriesRank.find { it == rank }!!.count fun calculateProfit(money: Int): Double { var total = 0.0 diff --git a/src/test/kotlin/lottery/LotteryPrizeTest.kt b/src/test/kotlin/lottery/LotteryPrizeTest.kt new file mode 100644 index 0000000000..583a206c09 --- /dev/null +++ b/src/test/kotlin/lottery/LotteryPrizeTest.kt @@ -0,0 +1,13 @@ +package lottery + +import io.kotest.core.spec.style.StringSpec +import io.kotest.matchers.shouldBe + +class LotteryPrizeTest : StringSpec({ + "correctCount 갯수에 해당하는 LotteryPrize 를 가져와야한다." { + LotteryPrize.get(3) shouldBe LotteryPrize.FORTH + LotteryPrize.get(4) shouldBe LotteryPrize.THIRD + LotteryPrize.get(5) shouldBe LotteryPrize.SECOND + LotteryPrize.get(6) shouldBe LotteryPrize.FIRST + } +}) From 7dc228b10009925c6e86ab50834a8b5e0503ccc7 Mon Sep 17 00:00:00 2001 From: "pavlo.v" Date: Tue, 4 Jul 2023 20:49:58 +0900 Subject: [PATCH 16/19] =?UTF-8?q?=EB=B3=80=EA=B2=BD=20:=20package=20?= =?UTF-8?q?=EA=B5=AC=EC=A1=B0=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kotlin/lottery/{ => controller}/LotteryGame.kt | 10 +++++++++- src/main/kotlin/lottery/{ => domain}/Lotteries.kt | 2 +- src/main/kotlin/lottery/{ => domain}/Lottery.kt | 2 +- .../kotlin/lottery/{ => domain}/LotteryNumber.kt | 2 +- .../kotlin/lottery/{ => domain}/LotteryPrize.kt | 2 +- src/main/kotlin/lottery/{ => domain}/LotteryRank.kt | 2 +- .../kotlin/lottery/{ => domain}/WinningLottery.kt | 2 +- .../kotlin/lottery/{ => view}/LotteryGameView.kt | 5 ++++- src/test/kotlin/lottery/LotteryGameTest.kt | 1 + src/test/kotlin/lottery/LotteryNumberTest.kt | 1 + src/test/kotlin/lottery/LotteryPrizeTest.kt | 1 + src/test/kotlin/lottery/LotteryRankTest.kt | 13 +++++++++++++ src/test/kotlin/lottery/LotteryTest.kt | 2 ++ src/test/kotlin/lottery/WinningLotteryTest.kt | 2 ++ 14 files changed, 39 insertions(+), 8 deletions(-) rename src/main/kotlin/lottery/{ => controller}/LotteryGame.kt (82%) rename src/main/kotlin/lottery/{ => domain}/Lotteries.kt (93%) rename src/main/kotlin/lottery/{ => domain}/Lottery.kt (97%) rename src/main/kotlin/lottery/{ => domain}/LotteryNumber.kt (95%) rename src/main/kotlin/lottery/{ => domain}/LotteryPrize.kt (94%) rename src/main/kotlin/lottery/{ => domain}/LotteryRank.kt (94%) rename src/main/kotlin/lottery/{ => domain}/WinningLottery.kt (94%) rename src/main/kotlin/lottery/{ => view}/LotteryGameView.kt (91%) create mode 100644 src/test/kotlin/lottery/LotteryRankTest.kt diff --git a/src/main/kotlin/lottery/LotteryGame.kt b/src/main/kotlin/lottery/controller/LotteryGame.kt similarity index 82% rename from src/main/kotlin/lottery/LotteryGame.kt rename to src/main/kotlin/lottery/controller/LotteryGame.kt index ef677c88a9..540e3c9f11 100644 --- a/src/main/kotlin/lottery/LotteryGame.kt +++ b/src/main/kotlin/lottery/controller/LotteryGame.kt @@ -1,4 +1,12 @@ -package lottery +package lottery.controller + +import lottery.domain.Lotteries +import lottery.domain.Lottery +import lottery.domain.LotteryNumber +import lottery.domain.LotteryPrize +import lottery.domain.LotteryRank +import lottery.domain.WinningLottery +import lottery.view.LotteryGameView class LotteryGame { private lateinit var winningLottery: WinningLottery diff --git a/src/main/kotlin/lottery/Lotteries.kt b/src/main/kotlin/lottery/domain/Lotteries.kt similarity index 93% rename from src/main/kotlin/lottery/Lotteries.kt rename to src/main/kotlin/lottery/domain/Lotteries.kt index e6bb61ff23..7274da2a75 100644 --- a/src/main/kotlin/lottery/Lotteries.kt +++ b/src/main/kotlin/lottery/domain/Lotteries.kt @@ -1,4 +1,4 @@ -package lottery +package lottery.domain class Lotteries { val lotteries = mutableListOf() diff --git a/src/main/kotlin/lottery/Lottery.kt b/src/main/kotlin/lottery/domain/Lottery.kt similarity index 97% rename from src/main/kotlin/lottery/Lottery.kt rename to src/main/kotlin/lottery/domain/Lottery.kt index 955ff6145a..9b85812903 100644 --- a/src/main/kotlin/lottery/Lottery.kt +++ b/src/main/kotlin/lottery/domain/Lottery.kt @@ -1,4 +1,4 @@ -package lottery +package lottery.domain class Lottery(numbers: Set) { val lotteryNumbers: Set diff --git a/src/main/kotlin/lottery/LotteryNumber.kt b/src/main/kotlin/lottery/domain/LotteryNumber.kt similarity index 95% rename from src/main/kotlin/lottery/LotteryNumber.kt rename to src/main/kotlin/lottery/domain/LotteryNumber.kt index d98715daf4..e276766975 100644 --- a/src/main/kotlin/lottery/LotteryNumber.kt +++ b/src/main/kotlin/lottery/domain/LotteryNumber.kt @@ -1,4 +1,4 @@ -package lottery +package lottery.domain @JvmInline value class LotteryNumber private constructor( diff --git a/src/main/kotlin/lottery/LotteryPrize.kt b/src/main/kotlin/lottery/domain/LotteryPrize.kt similarity index 94% rename from src/main/kotlin/lottery/LotteryPrize.kt rename to src/main/kotlin/lottery/domain/LotteryPrize.kt index c4d27a5d56..66e0667070 100644 --- a/src/main/kotlin/lottery/LotteryPrize.kt +++ b/src/main/kotlin/lottery/domain/LotteryPrize.kt @@ -1,4 +1,4 @@ -package lottery +package lottery.domain enum class LotteryPrize(val correctCount: Int, val rewardMoney: Int, var count: Int = 0) { NONE(0, 0), diff --git a/src/main/kotlin/lottery/LotteryRank.kt b/src/main/kotlin/lottery/domain/LotteryRank.kt similarity index 94% rename from src/main/kotlin/lottery/LotteryRank.kt rename to src/main/kotlin/lottery/domain/LotteryRank.kt index 079cf08ba2..d747897d74 100644 --- a/src/main/kotlin/lottery/LotteryRank.kt +++ b/src/main/kotlin/lottery/domain/LotteryRank.kt @@ -1,4 +1,4 @@ -package lottery +package lottery.domain class LotteryRank { val lotteriesRank: List = LotteryPrize.values().toList() diff --git a/src/main/kotlin/lottery/WinningLottery.kt b/src/main/kotlin/lottery/domain/WinningLottery.kt similarity index 94% rename from src/main/kotlin/lottery/WinningLottery.kt rename to src/main/kotlin/lottery/domain/WinningLottery.kt index b88258881d..4ecadcb35c 100644 --- a/src/main/kotlin/lottery/WinningLottery.kt +++ b/src/main/kotlin/lottery/domain/WinningLottery.kt @@ -1,4 +1,4 @@ -package lottery +package lottery.domain class WinningLottery(numbers: Set) { val winningNumbers: Set diff --git a/src/main/kotlin/lottery/LotteryGameView.kt b/src/main/kotlin/lottery/view/LotteryGameView.kt similarity index 91% rename from src/main/kotlin/lottery/LotteryGameView.kt rename to src/main/kotlin/lottery/view/LotteryGameView.kt index 2ef46424c9..89a131b53e 100644 --- a/src/main/kotlin/lottery/LotteryGameView.kt +++ b/src/main/kotlin/lottery/view/LotteryGameView.kt @@ -1,4 +1,7 @@ -package lottery +package lottery.view + +import lottery.domain.Lotteries +import lottery.domain.LotteryRank object LotteryGameView { diff --git a/src/test/kotlin/lottery/LotteryGameTest.kt b/src/test/kotlin/lottery/LotteryGameTest.kt index 5b70712730..eacecd19d0 100644 --- a/src/test/kotlin/lottery/LotteryGameTest.kt +++ b/src/test/kotlin/lottery/LotteryGameTest.kt @@ -2,6 +2,7 @@ package lottery import io.kotest.core.spec.style.StringSpec import io.kotest.matchers.shouldBe +import lottery.controller.LotteryGame class LotteryGameTest : StringSpec({ "로또 구입 금액을 입력하면 구입 금액에 해당하는 로또를 발급해야 한다." { diff --git a/src/test/kotlin/lottery/LotteryNumberTest.kt b/src/test/kotlin/lottery/LotteryNumberTest.kt index 6c1092a013..a323b620b0 100644 --- a/src/test/kotlin/lottery/LotteryNumberTest.kt +++ b/src/test/kotlin/lottery/LotteryNumberTest.kt @@ -2,6 +2,7 @@ package lottery import io.kotest.assertions.throwables.shouldThrow import io.kotest.core.spec.style.StringSpec +import lottery.domain.LotteryNumber class LotteryNumberTest : StringSpec({ "로또 숫자의 크기는 1이상 이여야한다." { diff --git a/src/test/kotlin/lottery/LotteryPrizeTest.kt b/src/test/kotlin/lottery/LotteryPrizeTest.kt index 583a206c09..43b24e71bf 100644 --- a/src/test/kotlin/lottery/LotteryPrizeTest.kt +++ b/src/test/kotlin/lottery/LotteryPrizeTest.kt @@ -2,6 +2,7 @@ package lottery import io.kotest.core.spec.style.StringSpec import io.kotest.matchers.shouldBe +import lottery.domain.LotteryPrize class LotteryPrizeTest : StringSpec({ "correctCount 갯수에 해당하는 LotteryPrize 를 가져와야한다." { diff --git a/src/test/kotlin/lottery/LotteryRankTest.kt b/src/test/kotlin/lottery/LotteryRankTest.kt new file mode 100644 index 0000000000..0ca253101d --- /dev/null +++ b/src/test/kotlin/lottery/LotteryRankTest.kt @@ -0,0 +1,13 @@ +package lottery + +import io.kotest.core.spec.style.StringSpec +import io.kotest.matchers.shouldBe +import lottery.domain.LotteryPrize +import lottery.domain.LotteryRank + +class LotteryRankTest : StringSpec({ + "plus호출시 해당하는 로또 등수를 올려주어야한다." { + val lotteryRank = LotteryRank() + lotteryRank.plusRank(LotteryPrize.THIRD) shouldBe 1 + } +}) diff --git a/src/test/kotlin/lottery/LotteryTest.kt b/src/test/kotlin/lottery/LotteryTest.kt index 586361b50a..7617c22420 100644 --- a/src/test/kotlin/lottery/LotteryTest.kt +++ b/src/test/kotlin/lottery/LotteryTest.kt @@ -3,6 +3,8 @@ package lottery import io.kotest.assertions.throwables.shouldThrow import io.kotest.core.spec.style.StringSpec import io.kotest.matchers.shouldBe +import lottery.domain.Lottery +import lottery.domain.LotteryNumber class LotteryTest : StringSpec({ "자동 로또 생성을 할 경우의 숫자의 개수는 6개이다." { diff --git a/src/test/kotlin/lottery/WinningLotteryTest.kt b/src/test/kotlin/lottery/WinningLotteryTest.kt index c5074548d0..eead0a5033 100644 --- a/src/test/kotlin/lottery/WinningLotteryTest.kt +++ b/src/test/kotlin/lottery/WinningLotteryTest.kt @@ -2,6 +2,8 @@ package lottery import io.kotest.assertions.throwables.shouldThrow import io.kotest.core.spec.style.StringSpec +import lottery.domain.LotteryNumber +import lottery.domain.WinningLottery class WinningLotteryTest : StringSpec({ "당첨 번호는 중복되어서는 안된다." { From a32e80030060b266bbfd4706b09be00cd13939e1 Mon Sep 17 00:00:00 2001 From: "pavlo.v" Date: Tue, 4 Jul 2023 22:28:51 +0900 Subject: [PATCH 17/19] =?UTF-8?q?=EB=B3=80=EA=B2=BD=20:=20LotteryPrize=20?= =?UTF-8?q?=EA=B0=9C=EC=88=98=EB=A5=BC=20enum=20=EC=9D=B4=20=EA=B0=96?= =?UTF-8?q?=EC=A7=80=20=EC=95=8A=EA=B2=8C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/lottery/domain/Lottery.kt | 6 +----- .../kotlin/lottery/domain/LotteryPrize.kt | 2 +- src/main/kotlin/lottery/domain/LotteryRank.kt | 15 ++++++++------ .../kotlin/lottery/view/LotteryGameView.kt | 4 ++-- src/test/kotlin/lottery/LotteryPrizeTest.kt | 14 ------------- src/test/kotlin/lottery/LotteryRankTest.kt | 13 ------------ .../{ => controller}/LotteryGameTest.kt | 3 +-- .../lottery/{ => domain}/LotteryNumberTest.kt | 3 +-- .../kotlin/lottery/domain/LotteryPrizeTest.kt | 13 ++++++++++++ .../kotlin/lottery/domain/LotteryRankTest.kt | 20 +++++++++++++++++++ .../lottery/{ => domain}/LotteryTest.kt | 4 +--- .../{ => domain}/WinningLotteryTest.kt | 4 +--- 12 files changed, 50 insertions(+), 51 deletions(-) delete mode 100644 src/test/kotlin/lottery/LotteryPrizeTest.kt delete mode 100644 src/test/kotlin/lottery/LotteryRankTest.kt rename src/test/kotlin/lottery/{ => controller}/LotteryGameTest.kt (88%) rename src/test/kotlin/lottery/{ => domain}/LotteryNumberTest.kt (89%) create mode 100644 src/test/kotlin/lottery/domain/LotteryPrizeTest.kt create mode 100644 src/test/kotlin/lottery/domain/LotteryRankTest.kt rename src/test/kotlin/lottery/{ => domain}/LotteryTest.kt (89%) rename src/test/kotlin/lottery/{ => domain}/WinningLotteryTest.kt (81%) diff --git a/src/main/kotlin/lottery/domain/Lottery.kt b/src/main/kotlin/lottery/domain/Lottery.kt index 9b85812903..5dbabcf3d1 100644 --- a/src/main/kotlin/lottery/domain/Lottery.kt +++ b/src/main/kotlin/lottery/domain/Lottery.kt @@ -13,11 +13,7 @@ class Lottery(numbers: Set) { } fun checkRank(numbers: Set): Int { - var correctCount = 0 - numbers.forEach { - if (lotteryNumbers.contains(it)) correctCount++ - } - return correctCount + return numbers.count { it in lotteryNumbers } } companion object { diff --git a/src/main/kotlin/lottery/domain/LotteryPrize.kt b/src/main/kotlin/lottery/domain/LotteryPrize.kt index 66e0667070..475dfff2e7 100644 --- a/src/main/kotlin/lottery/domain/LotteryPrize.kt +++ b/src/main/kotlin/lottery/domain/LotteryPrize.kt @@ -1,6 +1,6 @@ package lottery.domain -enum class LotteryPrize(val correctCount: Int, val rewardMoney: Int, var count: Int = 0) { +enum class LotteryPrize(val correctCount: Int, val rewardMoney: Int) { NONE(0, 0), FORTH(3, 5_000), THIRD(4, 50_000), diff --git a/src/main/kotlin/lottery/domain/LotteryRank.kt b/src/main/kotlin/lottery/domain/LotteryRank.kt index d747897d74..f25f3721d9 100644 --- a/src/main/kotlin/lottery/domain/LotteryRank.kt +++ b/src/main/kotlin/lottery/domain/LotteryRank.kt @@ -1,13 +1,16 @@ package lottery.domain class LotteryRank { - val lotteriesRank: List = LotteryPrize.values().toList() - fun plusRank(rank: LotteryPrize) = ++lotteriesRank.find { it == rank }!!.count + val lotteriesRank = LotteryPrize.values().associateWith { 0 }.toMutableMap() + + fun plusRank(rank: LotteryPrize) { + lotteriesRank[rank] = lotteriesRank.getOrDefault(rank, 0) + 1 + } + fun calculateProfit(money: Int): Double { - var total = 0.0 - lotteriesRank.forEach { - total += it.rewardMoney * it.count - } + val total = lotteriesRank.map { (prize, count) -> + prize.rewardMoney * count + }.sumOf { it }.toDouble() return total / money } } diff --git a/src/main/kotlin/lottery/view/LotteryGameView.kt b/src/main/kotlin/lottery/view/LotteryGameView.kt index 89a131b53e..c30ed36c98 100644 --- a/src/main/kotlin/lottery/view/LotteryGameView.kt +++ b/src/main/kotlin/lottery/view/LotteryGameView.kt @@ -28,8 +28,8 @@ object LotteryGameView { fun printLotteryRankView(lotteryRank: LotteryRank) { println("당첨 통계") println("---------") - lotteryRank.lotteriesRank.drop(NONE_PRIZE_INDEX).forEach { - println("${it.correctCount}개 일치 (${it.rewardMoney}원)- ${it.count}개") + lotteryRank.lotteriesRank.forEach { (prize, count) -> + println("${prize.correctCount}개 일치 (${prize.rewardMoney}원)- ${count}개") } } diff --git a/src/test/kotlin/lottery/LotteryPrizeTest.kt b/src/test/kotlin/lottery/LotteryPrizeTest.kt deleted file mode 100644 index 43b24e71bf..0000000000 --- a/src/test/kotlin/lottery/LotteryPrizeTest.kt +++ /dev/null @@ -1,14 +0,0 @@ -package lottery - -import io.kotest.core.spec.style.StringSpec -import io.kotest.matchers.shouldBe -import lottery.domain.LotteryPrize - -class LotteryPrizeTest : StringSpec({ - "correctCount 갯수에 해당하는 LotteryPrize 를 가져와야한다." { - LotteryPrize.get(3) shouldBe LotteryPrize.FORTH - LotteryPrize.get(4) shouldBe LotteryPrize.THIRD - LotteryPrize.get(5) shouldBe LotteryPrize.SECOND - LotteryPrize.get(6) shouldBe LotteryPrize.FIRST - } -}) diff --git a/src/test/kotlin/lottery/LotteryRankTest.kt b/src/test/kotlin/lottery/LotteryRankTest.kt deleted file mode 100644 index 0ca253101d..0000000000 --- a/src/test/kotlin/lottery/LotteryRankTest.kt +++ /dev/null @@ -1,13 +0,0 @@ -package lottery - -import io.kotest.core.spec.style.StringSpec -import io.kotest.matchers.shouldBe -import lottery.domain.LotteryPrize -import lottery.domain.LotteryRank - -class LotteryRankTest : StringSpec({ - "plus호출시 해당하는 로또 등수를 올려주어야한다." { - val lotteryRank = LotteryRank() - lotteryRank.plusRank(LotteryPrize.THIRD) shouldBe 1 - } -}) diff --git a/src/test/kotlin/lottery/LotteryGameTest.kt b/src/test/kotlin/lottery/controller/LotteryGameTest.kt similarity index 88% rename from src/test/kotlin/lottery/LotteryGameTest.kt rename to src/test/kotlin/lottery/controller/LotteryGameTest.kt index eacecd19d0..50163b4f3a 100644 --- a/src/test/kotlin/lottery/LotteryGameTest.kt +++ b/src/test/kotlin/lottery/controller/LotteryGameTest.kt @@ -1,8 +1,7 @@ -package lottery +package lottery.controller import io.kotest.core.spec.style.StringSpec import io.kotest.matchers.shouldBe -import lottery.controller.LotteryGame class LotteryGameTest : StringSpec({ "로또 구입 금액을 입력하면 구입 금액에 해당하는 로또를 발급해야 한다." { diff --git a/src/test/kotlin/lottery/LotteryNumberTest.kt b/src/test/kotlin/lottery/domain/LotteryNumberTest.kt similarity index 89% rename from src/test/kotlin/lottery/LotteryNumberTest.kt rename to src/test/kotlin/lottery/domain/LotteryNumberTest.kt index a323b620b0..dbdd2f3fde 100644 --- a/src/test/kotlin/lottery/LotteryNumberTest.kt +++ b/src/test/kotlin/lottery/domain/LotteryNumberTest.kt @@ -1,8 +1,7 @@ -package lottery +package lottery.domain import io.kotest.assertions.throwables.shouldThrow import io.kotest.core.spec.style.StringSpec -import lottery.domain.LotteryNumber class LotteryNumberTest : StringSpec({ "로또 숫자의 크기는 1이상 이여야한다." { diff --git a/src/test/kotlin/lottery/domain/LotteryPrizeTest.kt b/src/test/kotlin/lottery/domain/LotteryPrizeTest.kt new file mode 100644 index 0000000000..b9a4f57f89 --- /dev/null +++ b/src/test/kotlin/lottery/domain/LotteryPrizeTest.kt @@ -0,0 +1,13 @@ +package lottery.domain + +import io.kotest.core.spec.style.StringSpec +import io.kotest.matchers.shouldBe + +class LotteryPrizeTest : StringSpec({ + "correctCount 갯수에 해당하는 LotteryPrize 를 가져와야한다." { + LotteryPrize.get(correctCount = 3) shouldBe LotteryPrize.FORTH + LotteryPrize.get(correctCount = 4) shouldBe LotteryPrize.THIRD + LotteryPrize.get(correctCount = 5) shouldBe LotteryPrize.SECOND + LotteryPrize.get(correctCount = 6) shouldBe LotteryPrize.FIRST + } +}) diff --git a/src/test/kotlin/lottery/domain/LotteryRankTest.kt b/src/test/kotlin/lottery/domain/LotteryRankTest.kt new file mode 100644 index 0000000000..6cce1cdd0c --- /dev/null +++ b/src/test/kotlin/lottery/domain/LotteryRankTest.kt @@ -0,0 +1,20 @@ +package lottery.domain + +import io.kotest.core.spec.style.StringSpec +import io.kotest.matchers.shouldBe + +class LotteryRankTest : StringSpec({ + "plusRank 호출시 해당하는 로또 등수를 올려주어야한다." { + val lotteryRank = LotteryRank() + lotteryRank.lotteriesRank[LotteryPrize.THIRD] shouldBe 0 + lotteryRank.plusRank(LotteryPrize.THIRD) + lotteryRank.lotteriesRank[LotteryPrize.THIRD] shouldBe 1 + } + + "로또 구매 금액과 수익금으로 수익률(금액 / 구매금액)을 계산해 주어야한다." { + val lotteryRank = LotteryRank() + lotteryRank.calculateProfit(1000) shouldBe 0 + lotteryRank.plusRank(LotteryPrize.FORTH) + lotteryRank.calculateProfit(1000) shouldBe 5 + } +}) diff --git a/src/test/kotlin/lottery/LotteryTest.kt b/src/test/kotlin/lottery/domain/LotteryTest.kt similarity index 89% rename from src/test/kotlin/lottery/LotteryTest.kt rename to src/test/kotlin/lottery/domain/LotteryTest.kt index 7617c22420..8726ccebc2 100644 --- a/src/test/kotlin/lottery/LotteryTest.kt +++ b/src/test/kotlin/lottery/domain/LotteryTest.kt @@ -1,10 +1,8 @@ -package lottery +package lottery.domain import io.kotest.assertions.throwables.shouldThrow import io.kotest.core.spec.style.StringSpec import io.kotest.matchers.shouldBe -import lottery.domain.Lottery -import lottery.domain.LotteryNumber class LotteryTest : StringSpec({ "자동 로또 생성을 할 경우의 숫자의 개수는 6개이다." { diff --git a/src/test/kotlin/lottery/WinningLotteryTest.kt b/src/test/kotlin/lottery/domain/WinningLotteryTest.kt similarity index 81% rename from src/test/kotlin/lottery/WinningLotteryTest.kt rename to src/test/kotlin/lottery/domain/WinningLotteryTest.kt index eead0a5033..7d1137df29 100644 --- a/src/test/kotlin/lottery/WinningLotteryTest.kt +++ b/src/test/kotlin/lottery/domain/WinningLotteryTest.kt @@ -1,9 +1,7 @@ -package lottery +package lottery.domain import io.kotest.assertions.throwables.shouldThrow import io.kotest.core.spec.style.StringSpec -import lottery.domain.LotteryNumber -import lottery.domain.WinningLottery class WinningLotteryTest : StringSpec({ "당첨 번호는 중복되어서는 안된다." { From 5d7e88ab0d7f76a0c0e3d76d6d5473064a47d643 Mon Sep 17 00:00:00 2001 From: "pavlo.v" Date: Tue, 4 Jul 2023 22:44:32 +0900 Subject: [PATCH 18/19] =?UTF-8?q?=EB=B3=80=EA=B2=BD=20:=20=EB=A1=9C?= =?UTF-8?q?=EB=98=90=20=EB=B9=84=EA=B5=90=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/lottery/controller/LotteryGame.kt | 4 ++-- src/main/kotlin/lottery/domain/Lottery.kt | 2 +- src/main/kotlin/lottery/view/LotteryGameView.kt | 4 ++-- src/test/kotlin/lottery/domain/LotteryTest.kt | 8 ++++++++ 4 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/main/kotlin/lottery/controller/LotteryGame.kt b/src/main/kotlin/lottery/controller/LotteryGame.kt index 540e3c9f11..c9a4974136 100644 --- a/src/main/kotlin/lottery/controller/LotteryGame.kt +++ b/src/main/kotlin/lottery/controller/LotteryGame.kt @@ -26,8 +26,8 @@ class LotteryGame { winningLottery = WinningLottery(numbers.map { LotteryNumber.get(it.toInt()) }.toSet()) purchaseAutoLotteries.lotteries.forEach { - val checkRank = it.checkRank(winningLottery.winningNumbers) - lotteryRank.plusRank(LotteryPrize.get(checkRank) ?: LotteryPrize.NONE) + val correctCount = it.checkCorrectCount(winningLottery.winningNumbers) + lotteryRank.plusRank(LotteryPrize.get(correctCount) ?: LotteryPrize.NONE) } LotteryGameView.printLotteryRankView(lotteryRank) LotteryGameView.printProfitView(lotteryRank.calculateProfit(money)) diff --git a/src/main/kotlin/lottery/domain/Lottery.kt b/src/main/kotlin/lottery/domain/Lottery.kt index 5dbabcf3d1..1bb5246ea5 100644 --- a/src/main/kotlin/lottery/domain/Lottery.kt +++ b/src/main/kotlin/lottery/domain/Lottery.kt @@ -12,7 +12,7 @@ class Lottery(numbers: Set) { return numbers.size != LOTTERY_NUMBER_SIZE } - fun checkRank(numbers: Set): Int { + fun checkCorrectCount(numbers: Set): Int { return numbers.count { it in lotteryNumbers } } diff --git a/src/main/kotlin/lottery/view/LotteryGameView.kt b/src/main/kotlin/lottery/view/LotteryGameView.kt index c30ed36c98..dd15dff146 100644 --- a/src/main/kotlin/lottery/view/LotteryGameView.kt +++ b/src/main/kotlin/lottery/view/LotteryGameView.kt @@ -1,11 +1,11 @@ package lottery.view import lottery.domain.Lotteries +import lottery.domain.LotteryPrize import lottery.domain.LotteryRank object LotteryGameView { - private const val NONE_PRIZE_INDEX = 1 fun printPurchaseMoneyView() { println("구입 금액을 입력해 주세요.") } @@ -28,7 +28,7 @@ object LotteryGameView { fun printLotteryRankView(lotteryRank: LotteryRank) { println("당첨 통계") println("---------") - lotteryRank.lotteriesRank.forEach { (prize, count) -> + lotteryRank.lotteriesRank.filter { it.key != LotteryPrize.NONE }.forEach { (prize, count) -> println("${prize.correctCount}개 일치 (${prize.rewardMoney}원)- ${count}개") } } diff --git a/src/test/kotlin/lottery/domain/LotteryTest.kt b/src/test/kotlin/lottery/domain/LotteryTest.kt index 8726ccebc2..8c62d4a394 100644 --- a/src/test/kotlin/lottery/domain/LotteryTest.kt +++ b/src/test/kotlin/lottery/domain/LotteryTest.kt @@ -20,4 +20,12 @@ class LotteryTest : StringSpec({ "로또 한장의 금액은 1000원 이다." { Lottery.LOTTERY_PRICE shouldBe 1000 } + + "로또 두개를 비교했을때 맞는 개수를 정확하게 가져와야한다." { + val numbers = listOf(1, 2, 3, 4, 5, 6) + val winningNumbers = listOf(1, 2, 3, 8, 9, 10) + val lottery = Lottery(numbers.map { LotteryNumber.get(it) }.toSet()) + val winningLottery = WinningLottery(winningNumbers.map { LotteryNumber.get(it) }.toSet()) + lottery.checkCorrectCount(winningLottery.winningNumbers) shouldBe 3 + } }) From cec863ac793edf074a76f94d8848a4424848d2f8 Mon Sep 17 00:00:00 2001 From: choihwan2 Date: Wed, 5 Jul 2023 16:03:07 +0900 Subject: [PATCH 19/19] =?UTF-8?q?=EB=B3=80=EA=B2=BD=20:=20const=20val=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/lottery/domain/LotteryRank.kt | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/lottery/domain/LotteryRank.kt b/src/main/kotlin/lottery/domain/LotteryRank.kt index f25f3721d9..16bef685f9 100644 --- a/src/main/kotlin/lottery/domain/LotteryRank.kt +++ b/src/main/kotlin/lottery/domain/LotteryRank.kt @@ -1,7 +1,7 @@ package lottery.domain class LotteryRank { - val lotteriesRank = LotteryPrize.values().associateWith { 0 }.toMutableMap() + val lotteriesRank = LotteryPrize.values().associateWith { RANK_DEFAULT_VALUE }.toMutableMap() fun plusRank(rank: LotteryPrize) { lotteriesRank[rank] = lotteriesRank.getOrDefault(rank, 0) + 1 @@ -13,4 +13,8 @@ class LotteryRank { }.sumOf { it }.toDouble() return total / money } + + companion object { + const val RANK_DEFAULT_VALUE = 0 + } }