From 20d2f4b9183d456ac6d9d7f720843ea7bfe9a06d Mon Sep 17 00:00:00 2001 From: SeokJun Jeong Date: Tue, 7 Jan 2025 15:44:54 +0900 Subject: [PATCH 01/12] =?UTF-8?q?refactor:=20LottoPreset=20=EB=A7=8C?= =?UTF-8?q?=EB=93=A4=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1~45의 숫자를 매번 만들어서 사용하는 것보다 미리 만들어두고 사용하는 것이 더 효율적으로 보임 --- src/main/kotlin/lotto/Lotto.kt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/lotto/Lotto.kt b/src/main/kotlin/lotto/Lotto.kt index 160d35a65..94c2aa062 100644 --- a/src/main/kotlin/lotto/Lotto.kt +++ b/src/main/kotlin/lotto/Lotto.kt @@ -8,7 +8,7 @@ class Lotto private constructor(val lottoNumbers: List) { require(lottoNumbersSize == lottoNumbers.toSet().size) } - constructor() : this((1..45).shuffled().take(6).sorted().map { LottoNumber(it) }) + constructor() : this(LottoPreset.shuffled().take(6).sortedBy { it.number }) constructor(vararg number: Int) : this(number.map { LottoNumber(it) }) @@ -21,6 +21,7 @@ class Lotto private constructor(val lottoNumbers: List) { } companion object { + private val LottoPreset = List(45) { LottoNumber(it + 1) } private const val LOTTO_SIZE = 6 } } From d7f1b78f802110142d8e9fcd42d53d7e4b60aa3d Mon Sep 17 00:00:00 2001 From: SeokJun Jeong Date: Tue, 7 Jan 2025 16:04:26 +0900 Subject: [PATCH 02/12] =?UTF-8?q?refactor:=20Lotto=EC=9D=98=20lottoNumbers?= =?UTF-8?q?=EB=A5=BC=20Set=EC=9C=BC=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/lotto/Lotto.kt | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/main/kotlin/lotto/Lotto.kt b/src/main/kotlin/lotto/Lotto.kt index 94c2aa062..5e76c577d 100644 --- a/src/main/kotlin/lotto/Lotto.kt +++ b/src/main/kotlin/lotto/Lotto.kt @@ -1,19 +1,18 @@ package lotto -class Lotto private constructor(val lottoNumbers: List) { +class Lotto private constructor(val lottoNumbers: Set) { init { val lottoNumbersSize = lottoNumbers.size require(lottoNumbersSize == LOTTO_SIZE) - require(lottoNumbersSize == lottoNumbers.toSet().size) } - constructor() : this(LottoPreset.shuffled().take(6).sortedBy { it.number }) + constructor() : this(LottoPreset.shuffled().take(6).sortedBy { it.number }.toSet()) - constructor(vararg number: Int) : this(number.map { LottoNumber(it) }) + constructor(vararg number: Int) : this(number.map { LottoNumber(it) }.toSet()) fun countMatch(winningLotto: Lotto): Int { - return lottoNumbers.intersect(winningLotto.lottoNumbers.toSet()).count() + return lottoNumbers.intersect(winningLotto.lottoNumbers).count() } override fun toString(): String { From 4d3958475718be8247955a010150171388f9f85c Mon Sep 17 00:00:00 2001 From: SeokJun Jeong Date: Tue, 7 Jan 2025 16:34:52 +0900 Subject: [PATCH 03/12] =?UTF-8?q?refactor:=2045=EB=9D=BC=EB=8A=94=20?= =?UTF-8?q?=EB=A7=A4=EC=A7=81=EB=84=98=EB=B2=84=EB=A5=BC=20=EC=83=81?= =?UTF-8?q?=EC=88=98=EB=A1=9C=20=EC=B6=94=EC=B6=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/lotto/Lotto.kt | 2 +- src/main/kotlin/lotto/LottoNumber.kt | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/lotto/Lotto.kt b/src/main/kotlin/lotto/Lotto.kt index 5e76c577d..fb2fa7657 100644 --- a/src/main/kotlin/lotto/Lotto.kt +++ b/src/main/kotlin/lotto/Lotto.kt @@ -20,7 +20,7 @@ class Lotto private constructor(val lottoNumbers: Set) { } companion object { - private val LottoPreset = List(45) { LottoNumber(it + 1) } + private val LottoPreset = List(LottoNumber.END_LOTTO_NUMBER) { LottoNumber(it + 1) } private const val LOTTO_SIZE = 6 } } diff --git a/src/main/kotlin/lotto/LottoNumber.kt b/src/main/kotlin/lotto/LottoNumber.kt index 430374156..11176b3ce 100644 --- a/src/main/kotlin/lotto/LottoNumber.kt +++ b/src/main/kotlin/lotto/LottoNumber.kt @@ -3,10 +3,15 @@ package lotto @JvmInline value class LottoNumber(val number: Int) { init { - require(number in 1..45) + require(number in START_LOTTO_NUMBER..END_LOTTO_NUMBER) } override fun toString(): String { return number.toString() } + + companion object { + const val START_LOTTO_NUMBER = 1 + const val END_LOTTO_NUMBER = 45 + } } From 1dbdff183d361671c05b51860f518093561a6df7 Mon Sep 17 00:00:00 2001 From: SeokJun Jeong Date: Tue, 7 Jan 2025 16:46:38 +0900 Subject: [PATCH 04/12] =?UTF-8?q?refactor:=20=EB=8F=84=EB=A9=94=EC=9D=B8?= =?UTF-8?q?=20=EB=A1=9C=EC=A7=81=EC=97=90=EC=84=9C=20=EB=B7=B0=20=EC=9D=98?= =?UTF-8?q?=EC=A1=B4=EC=84=B1=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/lotto/Lotto.kt | 4 ---- src/main/kotlin/lotto/LottoNumber.kt | 4 ---- src/main/kotlin/lotto/view/OutputView.kt | 2 +- 3 files changed, 1 insertion(+), 9 deletions(-) diff --git a/src/main/kotlin/lotto/Lotto.kt b/src/main/kotlin/lotto/Lotto.kt index fb2fa7657..f0f50479a 100644 --- a/src/main/kotlin/lotto/Lotto.kt +++ b/src/main/kotlin/lotto/Lotto.kt @@ -15,10 +15,6 @@ class Lotto private constructor(val lottoNumbers: Set) { return lottoNumbers.intersect(winningLotto.lottoNumbers).count() } - override fun toString(): String { - return "[${lottoNumbers.joinToString(", ")}]" - } - companion object { private val LottoPreset = List(LottoNumber.END_LOTTO_NUMBER) { LottoNumber(it + 1) } private const val LOTTO_SIZE = 6 diff --git a/src/main/kotlin/lotto/LottoNumber.kt b/src/main/kotlin/lotto/LottoNumber.kt index 11176b3ce..db0a98842 100644 --- a/src/main/kotlin/lotto/LottoNumber.kt +++ b/src/main/kotlin/lotto/LottoNumber.kt @@ -6,10 +6,6 @@ value class LottoNumber(val number: Int) { require(number in START_LOTTO_NUMBER..END_LOTTO_NUMBER) } - override fun toString(): String { - return number.toString() - } - companion object { const val START_LOTTO_NUMBER = 1 const val END_LOTTO_NUMBER = 45 diff --git a/src/main/kotlin/lotto/view/OutputView.kt b/src/main/kotlin/lotto/view/OutputView.kt index a4b61fe20..5d9d4da08 100644 --- a/src/main/kotlin/lotto/view/OutputView.kt +++ b/src/main/kotlin/lotto/view/OutputView.kt @@ -15,7 +15,7 @@ class OutputView { fun showLotto(lotto: List) { lotto.forEach { - println(it) + println("[${it.lottoNumbers.map { lottoNumber -> lottoNumber.number }.joinToString(", ")}]") } } From 17ffe030a19b5fe20d081218488ee9787f21b1b3 Mon Sep 17 00:00:00 2001 From: SeokJun Jeong Date: Tue, 7 Jan 2025 17:47:40 +0900 Subject: [PATCH 05/12] =?UTF-8?q?refactor:=20domain=20=EC=84=B1=EA=B2=A9?= =?UTF-8?q?=EC=9D=98=20=ED=81=B4=EB=9E=98=EC=8A=A4=EB=93=A4=20=ED=8C=A8?= =?UTF-8?q?=ED=82=A4=EC=A7=80=20=EC=9D=B4=EB=8F=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/lotto/LottoVendingMachine.kt | 2 ++ src/main/kotlin/lotto/application/LottoApplication.kt | 4 ++-- src/main/kotlin/lotto/{ => domain}/Lotto.kt | 2 +- src/main/kotlin/lotto/{ => domain}/LottoNumber.kt | 2 +- src/main/kotlin/lotto/{ => domain}/LottoResult.kt | 2 +- src/main/kotlin/lotto/{ => domain}/Prize.kt | 2 +- src/main/kotlin/lotto/view/OutputView.kt | 6 +++--- src/test/kotlin/lotto/LottoNumberTest.kt | 1 + src/test/kotlin/lotto/LottoResultTest.kt | 3 +++ src/test/kotlin/lotto/LottoTest.kt | 1 + src/test/kotlin/lotto/PrizeTest.kt | 1 + 11 files changed, 17 insertions(+), 9 deletions(-) rename src/main/kotlin/lotto/{ => domain}/Lotto.kt (96%) rename src/main/kotlin/lotto/{ => domain}/LottoNumber.kt (92%) rename src/main/kotlin/lotto/{ => domain}/LottoResult.kt (97%) rename src/main/kotlin/lotto/{ => domain}/Prize.kt (93%) diff --git a/src/main/kotlin/lotto/LottoVendingMachine.kt b/src/main/kotlin/lotto/LottoVendingMachine.kt index 8ff596d95..d3aa9a686 100644 --- a/src/main/kotlin/lotto/LottoVendingMachine.kt +++ b/src/main/kotlin/lotto/LottoVendingMachine.kt @@ -1,5 +1,7 @@ package lotto +import lotto.domain.Lotto + object LottoVendingMachine { private const val LOTTO_PRICE = 1000 diff --git a/src/main/kotlin/lotto/application/LottoApplication.kt b/src/main/kotlin/lotto/application/LottoApplication.kt index 5335339a5..16e1802eb 100644 --- a/src/main/kotlin/lotto/application/LottoApplication.kt +++ b/src/main/kotlin/lotto/application/LottoApplication.kt @@ -1,7 +1,7 @@ package lotto.application -import lotto.Lotto -import lotto.LottoResult +import lotto.domain.Lotto +import lotto.domain.LottoResult import lotto.LottoVendingMachine import lotto.view.InputView import lotto.view.OutputView diff --git a/src/main/kotlin/lotto/Lotto.kt b/src/main/kotlin/lotto/domain/Lotto.kt similarity index 96% rename from src/main/kotlin/lotto/Lotto.kt rename to src/main/kotlin/lotto/domain/Lotto.kt index f0f50479a..11a698cd7 100644 --- a/src/main/kotlin/lotto/Lotto.kt +++ b/src/main/kotlin/lotto/domain/Lotto.kt @@ -1,4 +1,4 @@ -package lotto +package lotto.domain class Lotto private constructor(val lottoNumbers: Set) { diff --git a/src/main/kotlin/lotto/LottoNumber.kt b/src/main/kotlin/lotto/domain/LottoNumber.kt similarity index 92% rename from src/main/kotlin/lotto/LottoNumber.kt rename to src/main/kotlin/lotto/domain/LottoNumber.kt index db0a98842..b1eaf1ac4 100644 --- a/src/main/kotlin/lotto/LottoNumber.kt +++ b/src/main/kotlin/lotto/domain/LottoNumber.kt @@ -1,4 +1,4 @@ -package lotto +package lotto.domain @JvmInline value class LottoNumber(val number: Int) { diff --git a/src/main/kotlin/lotto/LottoResult.kt b/src/main/kotlin/lotto/domain/LottoResult.kt similarity index 97% rename from src/main/kotlin/lotto/LottoResult.kt rename to src/main/kotlin/lotto/domain/LottoResult.kt index cafe0b78d..59138f96c 100644 --- a/src/main/kotlin/lotto/LottoResult.kt +++ b/src/main/kotlin/lotto/domain/LottoResult.kt @@ -1,4 +1,4 @@ -package lotto +package lotto.domain class LottoResult private constructor(private val result: Map) : Map by result { private val totalPrizeMoney: Int diff --git a/src/main/kotlin/lotto/Prize.kt b/src/main/kotlin/lotto/domain/Prize.kt similarity index 93% rename from src/main/kotlin/lotto/Prize.kt rename to src/main/kotlin/lotto/domain/Prize.kt index c2bfe763a..4c10b4ec4 100644 --- a/src/main/kotlin/lotto/Prize.kt +++ b/src/main/kotlin/lotto/domain/Prize.kt @@ -1,4 +1,4 @@ -package lotto +package lotto.domain enum class Prize(val money: Int, val count: Int) { FIRST(2_000_000_000, 6), diff --git a/src/main/kotlin/lotto/view/OutputView.kt b/src/main/kotlin/lotto/view/OutputView.kt index 5d9d4da08..546071725 100644 --- a/src/main/kotlin/lotto/view/OutputView.kt +++ b/src/main/kotlin/lotto/view/OutputView.kt @@ -1,8 +1,8 @@ package lotto.view -import lotto.Lotto -import lotto.LottoResult -import lotto.Prize +import lotto.domain.Lotto +import lotto.domain.LottoResult +import lotto.domain.Prize class OutputView { fun showInputMoneyMessage() { diff --git a/src/test/kotlin/lotto/LottoNumberTest.kt b/src/test/kotlin/lotto/LottoNumberTest.kt index 310781c52..14775d969 100644 --- a/src/test/kotlin/lotto/LottoNumberTest.kt +++ b/src/test/kotlin/lotto/LottoNumberTest.kt @@ -2,6 +2,7 @@ package lotto import io.kotest.assertions.throwables.shouldNotThrow import io.kotest.assertions.throwables.shouldThrow +import lotto.domain.LottoNumber import org.junit.jupiter.api.Test class LottoNumberTest { diff --git a/src/test/kotlin/lotto/LottoResultTest.kt b/src/test/kotlin/lotto/LottoResultTest.kt index da591f4a4..3de5af84b 100644 --- a/src/test/kotlin/lotto/LottoResultTest.kt +++ b/src/test/kotlin/lotto/LottoResultTest.kt @@ -1,6 +1,9 @@ package lotto import io.kotest.matchers.shouldBe +import lotto.domain.Lotto +import lotto.domain.LottoResult +import lotto.domain.Prize import org.junit.jupiter.api.Test class LottoResultTest { diff --git a/src/test/kotlin/lotto/LottoTest.kt b/src/test/kotlin/lotto/LottoTest.kt index 62243aa48..741c5d584 100644 --- a/src/test/kotlin/lotto/LottoTest.kt +++ b/src/test/kotlin/lotto/LottoTest.kt @@ -1,6 +1,7 @@ package lotto import io.kotest.matchers.shouldBe +import lotto.domain.Lotto import org.junit.jupiter.api.Test class LottoTest { diff --git a/src/test/kotlin/lotto/PrizeTest.kt b/src/test/kotlin/lotto/PrizeTest.kt index f29b9e8e8..4b4fdfe2d 100644 --- a/src/test/kotlin/lotto/PrizeTest.kt +++ b/src/test/kotlin/lotto/PrizeTest.kt @@ -1,6 +1,7 @@ package lotto import io.kotest.matchers.shouldBe +import lotto.domain.Prize import org.junit.jupiter.api.Test class PrizeTest { From cc4ec52b0154037736ae5612ed0b7d6d7ce9be0d Mon Sep 17 00:00:00 2001 From: SeokJun Jeong Date: Tue, 7 Jan 2025 18:31:50 +0900 Subject: [PATCH 06/12] =?UTF-8?q?refactor:=20Lottos=EB=9D=BC=EB=8A=94=20?= =?UTF-8?q?=EC=9D=BC=EA=B8=89=20=EC=BB=AC=EB=A0=89=EC=85=98=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/lotto/LottoVendingMachine.kt | 5 +-- src/main/kotlin/lotto/domain/LottoResult.kt | 4 +-- src/main/kotlin/lotto/domain/Lottos.kt | 5 +++ src/main/kotlin/lotto/view/OutputView.kt | 10 +++--- src/test/kotlin/lotto/LottoResultTest.kt | 36 ++++++++++--------- .../kotlin/lotto/LottoVendingMachineTest.kt | 4 +-- 6 files changed, 37 insertions(+), 27 deletions(-) create mode 100644 src/main/kotlin/lotto/domain/Lottos.kt diff --git a/src/main/kotlin/lotto/LottoVendingMachine.kt b/src/main/kotlin/lotto/LottoVendingMachine.kt index d3aa9a686..2fcef65e4 100644 --- a/src/main/kotlin/lotto/LottoVendingMachine.kt +++ b/src/main/kotlin/lotto/LottoVendingMachine.kt @@ -1,11 +1,12 @@ package lotto import lotto.domain.Lotto +import lotto.domain.Lottos object LottoVendingMachine { private const val LOTTO_PRICE = 1000 - fun buyLotto(money: Int?): List { + fun buyLotto(money: Int?): Lottos { requireNotNull(money) { "구매 금액은 null이 아니어야 함" } @@ -13,6 +14,6 @@ object LottoVendingMachine { "구매 금액은 1000 보다 커야 함" } - return List(money / LOTTO_PRICE) { Lotto() } + return Lottos(List(money / LOTTO_PRICE) { Lotto() }) } } diff --git a/src/main/kotlin/lotto/domain/LottoResult.kt b/src/main/kotlin/lotto/domain/LottoResult.kt index 59138f96c..5ba7f560c 100644 --- a/src/main/kotlin/lotto/domain/LottoResult.kt +++ b/src/main/kotlin/lotto/domain/LottoResult.kt @@ -19,9 +19,9 @@ class LottoResult private constructor(private val result: Map) : Map companion object { fun makeLottoResult( winningLotto: Lotto, - lottos: List, + lottos: Lottos, ): LottoResult { - val result = lottos.groupingBy { lotto -> + val result = lottos.lottos.groupingBy { lotto -> val count = lotto.countMatch(winningLotto) Prize.from(count) }.eachCount() diff --git a/src/main/kotlin/lotto/domain/Lottos.kt b/src/main/kotlin/lotto/domain/Lottos.kt new file mode 100644 index 000000000..83bc2e460 --- /dev/null +++ b/src/main/kotlin/lotto/domain/Lottos.kt @@ -0,0 +1,5 @@ +package lotto.domain + +class Lottos(lottos: List) { + val lottos: List = lottos.toList() +} diff --git a/src/main/kotlin/lotto/view/OutputView.kt b/src/main/kotlin/lotto/view/OutputView.kt index 546071725..88d85ff0a 100644 --- a/src/main/kotlin/lotto/view/OutputView.kt +++ b/src/main/kotlin/lotto/view/OutputView.kt @@ -1,7 +1,7 @@ package lotto.view -import lotto.domain.Lotto import lotto.domain.LottoResult +import lotto.domain.Lottos import lotto.domain.Prize class OutputView { @@ -9,12 +9,12 @@ class OutputView { println("구입금액을 입력해 주세요.") } - fun showLottoCount(lotto: List) { - println("${lotto.size}개를 구매했습니다.") + fun showLottoCount(lottos: Lottos) { + println("${lottos.lottos.size}개를 구매했습니다.") } - fun showLotto(lotto: List) { - lotto.forEach { + fun showLotto(lottos: Lottos) { + lottos.lottos.forEach { println("[${it.lottoNumbers.map { lottoNumber -> lottoNumber.number }.joinToString(", ")}]") } } diff --git a/src/test/kotlin/lotto/LottoResultTest.kt b/src/test/kotlin/lotto/LottoResultTest.kt index 3de5af84b..20bb83863 100644 --- a/src/test/kotlin/lotto/LottoResultTest.kt +++ b/src/test/kotlin/lotto/LottoResultTest.kt @@ -3,6 +3,7 @@ package lotto import io.kotest.matchers.shouldBe import lotto.domain.Lotto import lotto.domain.LottoResult +import lotto.domain.Lottos import lotto.domain.Prize import org.junit.jupiter.api.Test @@ -12,22 +13,25 @@ class LottoResultTest { internal fun `구매한 로또와 결과가 일치`() { val winningLotto = Lotto(1, 2, 3, 4, 5, 6) - val lottos = mutableListOf().apply { - add(Lotto(8, 21, 23, 41, 42, 43)) - add(Lotto(3, 5, 11, 16, 32, 38)) - add(Lotto(7, 11, 16, 35, 36, 44)) - add(Lotto(1, 8, 11, 31, 41, 42)) - add(Lotto(13, 14, 16, 38, 42, 45)) - add(Lotto(7, 11, 30, 40, 42, 43)) - add(Lotto(2, 13, 22, 32, 38, 45)) - add(Lotto(23, 25, 33, 36, 39, 41)) - add(Lotto(1, 3, 5, 14, 22, 45)) - add(Lotto(5, 9, 38, 41, 43, 44)) - add(Lotto(2, 8, 9, 18, 19, 21)) - add(Lotto(13, 14, 18, 21, 23, 35)) - add(Lotto(17, 21, 29, 37, 42, 45)) - add(Lotto(3, 8, 27, 30, 35, 44)) - } + val lottos = Lottos( + listOf( + Lotto(8, 21, 23, 41, 42, 43), + Lotto(3, 5, 11, 16, 32, 38), + Lotto(7, 11, 16, 35, 36, 44), + Lotto(1, 8, 11, 31, 41, 42), + Lotto(13, 14, 16, 38, 42, 45), + Lotto(7, 11, 30, 40, 42, 43), + Lotto(2, 13, 22, 32, 38, 45), + Lotto(23, 25, 33, 36, 39, 41), + Lotto(1, 3, 5, 14, 22, 45), + Lotto(5, 9, 38, 41, 43, 44), + Lotto(2, 8, 9, 18, 19, 21), + Lotto(13, 14, 18, 21, 23, 35), + Lotto(17, 21, 29, 37, 42, 45), + Lotto(3, 8, 27, 30, 35, 44), + ) + ) + val lottoResult = LottoResult.makeLottoResult( winningLotto = winningLotto, diff --git a/src/test/kotlin/lotto/LottoVendingMachineTest.kt b/src/test/kotlin/lotto/LottoVendingMachineTest.kt index 180f2cc17..f65bd1a37 100644 --- a/src/test/kotlin/lotto/LottoVendingMachineTest.kt +++ b/src/test/kotlin/lotto/LottoVendingMachineTest.kt @@ -29,7 +29,7 @@ class LottoVendingMachineTest { @Test internal fun `로또 한 장의 금액은 1000원 이다`() { - LottoVendingMachine.buyLotto(1000).size shouldBe 1 - LottoVendingMachine.buyLotto(14000).size shouldBe 14 + LottoVendingMachine.buyLotto(1000).lottos.size shouldBe 1 + LottoVendingMachine.buyLotto(14000).lottos.size shouldBe 14 } } From 703f515382d0f48557467ffc4807c6c3ea1acd5a Mon Sep 17 00:00:00 2001 From: SeokJun Jeong Date: Tue, 7 Jan 2025 18:52:09 +0900 Subject: [PATCH 07/12] =?UTF-8?q?refactor:=20Input=EC=9D=84=20=EC=9C=84?= =?UTF-8?q?=ED=95=B4=20Output=EC=9C=BC=EB=A1=9C=20=EC=B6=9C=EB=A0=A5?= =?UTF-8?q?=ED=95=98=EB=8A=94=20=EB=A1=9C=EC=A7=81=EC=9D=84=20=EC=9D=B4?= =?UTF-8?q?=EB=8F=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/lotto/application/LottoApplication.kt | 2 -- src/main/kotlin/lotto/view/InputView.kt | 2 ++ src/main/kotlin/lotto/view/OutputView.kt | 8 -------- 3 files changed, 2 insertions(+), 10 deletions(-) diff --git a/src/main/kotlin/lotto/application/LottoApplication.kt b/src/main/kotlin/lotto/application/LottoApplication.kt index 16e1802eb..22c05ea24 100644 --- a/src/main/kotlin/lotto/application/LottoApplication.kt +++ b/src/main/kotlin/lotto/application/LottoApplication.kt @@ -12,13 +12,11 @@ class LottoApplication( ) { fun run() { - outputView.showInputMoneyMessage() val money = inputView.inputMoney() val lotto = LottoVendingMachine.buyLotto(money) outputView.showLottoCount(lotto) outputView.showLotto(lotto) - outputView.showInputWinningNumbersMessage() val winningNumbers: List = inputView.inputWinningNumbers() val lottoResult= LottoResult.makeLottoResult( diff --git a/src/main/kotlin/lotto/view/InputView.kt b/src/main/kotlin/lotto/view/InputView.kt index 0caa6776e..eb74f7c0b 100644 --- a/src/main/kotlin/lotto/view/InputView.kt +++ b/src/main/kotlin/lotto/view/InputView.kt @@ -2,12 +2,14 @@ package lotto.view class InputView { fun inputMoney(): Int { + println("구입금액을 입력해 주세요.") val input = readlnOrNull()?.toIntOrNull() requireNotNull(input) return input } fun inputWinningNumbers(): List { + println("지난 주 당첨 번호를 입력해 주세요.") return readlnOrNull()?.split(",")?.map { it.trim().toInt() } ?: emptyList() } } diff --git a/src/main/kotlin/lotto/view/OutputView.kt b/src/main/kotlin/lotto/view/OutputView.kt index 88d85ff0a..6ef13f3c3 100644 --- a/src/main/kotlin/lotto/view/OutputView.kt +++ b/src/main/kotlin/lotto/view/OutputView.kt @@ -5,10 +5,6 @@ import lotto.domain.Lottos import lotto.domain.Prize class OutputView { - fun showInputMoneyMessage() { - println("구입금액을 입력해 주세요.") - } - fun showLottoCount(lottos: Lottos) { println("${lottos.lottos.size}개를 구매했습니다.") } @@ -19,10 +15,6 @@ class OutputView { } } - fun showInputWinningNumbersMessage() { - println("지난 주 당첨 번호를 입력해 주세요.") - } - fun showResult(lottoResult: LottoResult, money: Int) { println("당첨 통계") println("---------") From 764631ac3c476f9a8ec9f2ddc88483bdd30d7155 Mon Sep 17 00:00:00 2001 From: SeokJun Jeong Date: Tue, 7 Jan 2025 18:55:17 +0900 Subject: [PATCH 08/12] =?UTF-8?q?refactor:=20=EB=8B=B9=EC=B2=A8=EB=B2=88?= =?UTF-8?q?=ED=98=B8=20=EC=9E=85=EB=A0=A5=EC=9D=98=20=EB=B0=98=ED=99=98?= =?UTF-8?q?=ED=83=80=EC=9E=85=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/lotto/application/LottoApplication.kt | 4 ++-- src/main/kotlin/lotto/view/InputView.kt | 7 +++++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/main/kotlin/lotto/application/LottoApplication.kt b/src/main/kotlin/lotto/application/LottoApplication.kt index 22c05ea24..b76e6ebac 100644 --- a/src/main/kotlin/lotto/application/LottoApplication.kt +++ b/src/main/kotlin/lotto/application/LottoApplication.kt @@ -17,10 +17,10 @@ class LottoApplication( outputView.showLottoCount(lotto) outputView.showLotto(lotto) - val winningNumbers: List = inputView.inputWinningNumbers() + val winningLotto: Lotto = inputView.inputWinningNumbers() val lottoResult= LottoResult.makeLottoResult( - winningLotto = Lotto(*winningNumbers.toIntArray()), + winningLotto = winningLotto, lottos = lotto, ) outputView.showResult(lottoResult, money) diff --git a/src/main/kotlin/lotto/view/InputView.kt b/src/main/kotlin/lotto/view/InputView.kt index eb74f7c0b..d76c3a227 100644 --- a/src/main/kotlin/lotto/view/InputView.kt +++ b/src/main/kotlin/lotto/view/InputView.kt @@ -1,5 +1,7 @@ package lotto.view +import lotto.domain.Lotto + class InputView { fun inputMoney(): Int { println("구입금액을 입력해 주세요.") @@ -8,8 +10,9 @@ class InputView { return input } - fun inputWinningNumbers(): List { + fun inputWinningNumbers(): Lotto { println("지난 주 당첨 번호를 입력해 주세요.") - return readlnOrNull()?.split(",")?.map { it.trim().toInt() } ?: emptyList() + val winningNumbers: List = readlnOrNull()?.split(",")?.map { it.trim().toInt() } ?: emptyList() + return Lotto(*winningNumbers.toIntArray()) } } From c6c52378daf1553cf9e0efa64c7836772e570ebd Mon Sep 17 00:00:00 2001 From: SeokJun Jeong Date: Wed, 8 Jan 2025 08:41:28 +0900 Subject: [PATCH 09/12] =?UTF-8?q?feat:=20=EB=B3=B4=EB=84=88=EC=8A=A4?= =?UTF-8?q?=EB=B3=BC=202=EB=93=B1=20=EA=B8=B0=EB=8A=A5=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lotto/application/LottoApplication.kt | 3 + src/main/kotlin/lotto/domain/LottoResult.kt | 16 ++- src/main/kotlin/lotto/domain/Prize.kt | 14 +-- src/main/kotlin/lotto/view/InputView.kt | 6 + src/main/kotlin/lotto/view/OutputView.kt | 7 +- src/test/kotlin/lotto/LottoResultTest.kt | 118 ++++++++++++++---- src/test/kotlin/lotto/PrizeTest.kt | 17 +-- 7 files changed, 133 insertions(+), 48 deletions(-) diff --git a/src/main/kotlin/lotto/application/LottoApplication.kt b/src/main/kotlin/lotto/application/LottoApplication.kt index b76e6ebac..bea29ec35 100644 --- a/src/main/kotlin/lotto/application/LottoApplication.kt +++ b/src/main/kotlin/lotto/application/LottoApplication.kt @@ -3,6 +3,7 @@ package lotto.application import lotto.domain.Lotto import lotto.domain.LottoResult import lotto.LottoVendingMachine +import lotto.domain.LottoNumber import lotto.view.InputView import lotto.view.OutputView @@ -18,9 +19,11 @@ class LottoApplication( outputView.showLotto(lotto) val winningLotto: Lotto = inputView.inputWinningNumbers() + val bonusNumber: LottoNumber = inputView.inputBonusNumber() val lottoResult= LottoResult.makeLottoResult( winningLotto = winningLotto, + bonusLottoNumber = bonusNumber, lottos = lotto, ) outputView.showResult(lottoResult, money) diff --git a/src/main/kotlin/lotto/domain/LottoResult.kt b/src/main/kotlin/lotto/domain/LottoResult.kt index 5ba7f560c..0ad2078c6 100644 --- a/src/main/kotlin/lotto/domain/LottoResult.kt +++ b/src/main/kotlin/lotto/domain/LottoResult.kt @@ -19,11 +19,23 @@ class LottoResult private constructor(private val result: Map) : Map companion object { fun makeLottoResult( winningLotto: Lotto, + bonusLottoNumber: LottoNumber, lottos: Lottos, ): LottoResult { val result = lottos.lottos.groupingBy { lotto -> - val count = lotto.countMatch(winningLotto) - Prize.from(count) + val matchCount = lotto.countMatch(winningLotto) + when (matchCount) { + 6 -> Prize.FIRST + 5 -> if (bonusLottoNumber in lotto.lottoNumbers) { + Prize.SECOND + } else { + Prize.THIRD + } + + 4 -> Prize.FOURTH + 3 -> Prize.FIFTH + else -> Prize.NONE + } }.eachCount() return LottoResult(result) } diff --git a/src/main/kotlin/lotto/domain/Prize.kt b/src/main/kotlin/lotto/domain/Prize.kt index 4c10b4ec4..2b94098ad 100644 --- a/src/main/kotlin/lotto/domain/Prize.kt +++ b/src/main/kotlin/lotto/domain/Prize.kt @@ -2,16 +2,10 @@ package lotto.domain enum class Prize(val money: Int, val count: Int) { FIRST(2_000_000_000, 6), - SECOND(1_500_000, 5), - THIRD(50_000, 4), - FOURTH(5_000, 3), + SECOND(30_000_000, 5), + THIRD(1_500_000, 5), + FOURTH(50_000, 4), + FIFTH(5_000, 3), NONE(0, 0), ; - - companion object { - fun from(count: Int): Prize { - return entries.find { it.count == count } ?: NONE - } - } - } diff --git a/src/main/kotlin/lotto/view/InputView.kt b/src/main/kotlin/lotto/view/InputView.kt index d76c3a227..cbb0b1be3 100644 --- a/src/main/kotlin/lotto/view/InputView.kt +++ b/src/main/kotlin/lotto/view/InputView.kt @@ -1,6 +1,7 @@ package lotto.view import lotto.domain.Lotto +import lotto.domain.LottoNumber class InputView { fun inputMoney(): Int { @@ -15,4 +16,9 @@ class InputView { val winningNumbers: List = readlnOrNull()?.split(",")?.map { it.trim().toInt() } ?: emptyList() return Lotto(*winningNumbers.toIntArray()) } + + fun inputBonusNumber(): LottoNumber { + println("보너스 볼을 입력해 주세요.") + return LottoNumber(readlnOrNull()?.toIntOrNull() ?: 0) + } } diff --git a/src/main/kotlin/lotto/view/OutputView.kt b/src/main/kotlin/lotto/view/OutputView.kt index 6ef13f3c3..52fc2d2c5 100644 --- a/src/main/kotlin/lotto/view/OutputView.kt +++ b/src/main/kotlin/lotto/view/OutputView.kt @@ -22,7 +22,12 @@ class OutputView { .filter { it != Prize.NONE } .sortedByDescending { it.count } .forEach { prize -> - println("${prize.count}개 일치 (${prize.money}원) - ${lottoResult[prize]}개") + val title = if (prize == Prize.SECOND) { + "${prize.count}개 일치, 보너스 볼 일치 (${prize.money}원)" + } else { + "${prize.count}개 일치 (${prize.money}원)" + } + println("$title - ${lottoResult[prize]}개") } println(String.format("총 수익률은 %.2f입니다.", lottoResult.getRateOfReturn(money))) diff --git a/src/test/kotlin/lotto/LottoResultTest.kt b/src/test/kotlin/lotto/LottoResultTest.kt index 20bb83863..10a0f4068 100644 --- a/src/test/kotlin/lotto/LottoResultTest.kt +++ b/src/test/kotlin/lotto/LottoResultTest.kt @@ -2,6 +2,7 @@ package lotto import io.kotest.matchers.shouldBe import lotto.domain.Lotto +import lotto.domain.LottoNumber import lotto.domain.LottoResult import lotto.domain.Lottos import lotto.domain.Prize @@ -10,38 +11,111 @@ import org.junit.jupiter.api.Test class LottoResultTest { @Test - internal fun `구매한 로또와 결과가 일치`() { + internal fun `6개가 일치하면 1등`() { val winningLotto = Lotto(1, 2, 3, 4, 5, 6) + val bonusLottoNumber = LottoNumber(7) val lottos = Lottos( - listOf( - Lotto(8, 21, 23, 41, 42, 43), - Lotto(3, 5, 11, 16, 32, 38), - Lotto(7, 11, 16, 35, 36, 44), - Lotto(1, 8, 11, 31, 41, 42), - Lotto(13, 14, 16, 38, 42, 45), - Lotto(7, 11, 30, 40, 42, 43), - Lotto(2, 13, 22, 32, 38, 45), - Lotto(23, 25, 33, 36, 39, 41), - Lotto(1, 3, 5, 14, 22, 45), - Lotto(5, 9, 38, 41, 43, 44), - Lotto(2, 8, 9, 18, 19, 21), - Lotto(13, 14, 18, 21, 23, 35), - Lotto(17, 21, 29, 37, 42, 45), - Lotto(3, 8, 27, 30, 35, 44), - ) + listOf(Lotto(1, 2, 3, 4, 5, 6)) ) + val lottoResult = LottoResult.makeLottoResult( + winningLotto = winningLotto, + bonusLottoNumber = bonusLottoNumber, + lottos = lottos, + ) + + lottoResult[Prize.FIRST] shouldBe 1 + } + + @Test + internal fun `5개가 일치하고 보너스번호가 일치하면 2등`() { + val winningLotto = Lotto(1, 2, 3, 4, 5, 6) + val bonusLottoNumber = LottoNumber(7) + + val lottos = Lottos( + listOf(Lotto(1, 2, 3, 4, 5, 7)) + ) + + val lottoResult = LottoResult.makeLottoResult( + winningLotto = winningLotto, + bonusLottoNumber = bonusLottoNumber, + lottos = lottos, + ) + + lottoResult[Prize.SECOND] shouldBe 1 + } + + @Test + internal fun `5개가 일치하면 3등`() { + val winningLotto = Lotto(1, 2, 3, 4, 5, 6) + val bonusLottoNumber = LottoNumber(7) + + val lottos = Lottos( + listOf(Lotto(1, 2, 3, 4, 5, 8)) + ) + + val lottoResult = LottoResult.makeLottoResult( + winningLotto = winningLotto, + bonusLottoNumber = bonusLottoNumber, + lottos = lottos, + ) + + lottoResult[Prize.THIRD] shouldBe 1 + } + + @Test + internal fun `4개가 일치하면 4등`() { + val winningLotto = Lotto(1, 2, 3, 4, 5, 6) + val bonusLottoNumber = LottoNumber(7) + + val lottos = Lottos( + listOf(Lotto(1, 2, 3, 4, 8, 9)) + ) val lottoResult = LottoResult.makeLottoResult( winningLotto = winningLotto, - lottos = lottos + bonusLottoNumber = bonusLottoNumber, + lottos = lottos, ) - lottoResult[Prize.FIRST] shouldBe 0 - lottoResult[Prize.SECOND] shouldBe 0 - lottoResult[Prize.THIRD] shouldBe 0 lottoResult[Prize.FOURTH] shouldBe 1 - lottoResult[Prize.NONE] shouldBe 13 } + + @Test + internal fun `3개가 일치하면 5등`() { + val winningLotto = Lotto(1, 2, 3, 4, 5, 6) + val bonusLottoNumber = LottoNumber(7) + + val lottos = Lottos( + listOf(Lotto(1, 2, 3, 8, 9, 10)) + ) + + val lottoResult = LottoResult.makeLottoResult( + winningLotto = winningLotto, + bonusLottoNumber = bonusLottoNumber, + lottos = lottos, + ) + + lottoResult[Prize.FIFTH] shouldBe 1 + } + + @Test + internal fun `2개 이하로 일치하면 당첨 없음`() { + val winningLotto = Lotto(1, 2, 3, 4, 5, 6) + val bonusLottoNumber = LottoNumber(7) + + val lottos = Lottos( + listOf(Lotto(1, 2,8, 9, 10, 11)) + ) + + val lottoResult = LottoResult.makeLottoResult( + winningLotto = winningLotto, + bonusLottoNumber = bonusLottoNumber, + lottos = lottos, + ) + + lottoResult[Prize.NONE] shouldBe 1 + } + } diff --git a/src/test/kotlin/lotto/PrizeTest.kt b/src/test/kotlin/lotto/PrizeTest.kt index 4b4fdfe2d..6743e7c08 100644 --- a/src/test/kotlin/lotto/PrizeTest.kt +++ b/src/test/kotlin/lotto/PrizeTest.kt @@ -8,20 +8,11 @@ class PrizeTest { @Test internal fun `로또 당첨 금액`() { Prize.FIRST.money shouldBe 2_000_000_000 - Prize.SECOND.money shouldBe 1_500_000 - Prize.THIRD.money shouldBe 50_000 - Prize.FOURTH.money shouldBe 5_000 + Prize.SECOND.money shouldBe 30_000_000 + Prize.THIRD.money shouldBe 1_500_000 + Prize.FOURTH.money shouldBe 50_000 + Prize.FIFTH.money shouldBe 5_000 Prize.NONE.money shouldBe 0 } - @Test - internal fun `일치하는 개수에 맞는 등수`() { - Prize.from(6) shouldBe Prize.FIRST - Prize.from(5) shouldBe Prize.SECOND - Prize.from(4) shouldBe Prize.THIRD - Prize.from(3) shouldBe Prize.FOURTH - Prize.from(2) shouldBe Prize.NONE - Prize.from(1) shouldBe Prize.NONE - Prize.from(0) shouldBe Prize.NONE - } } From 7176b6853e8b37f9d468b82c8c21cdfdac6fbf97 Mon Sep 17 00:00:00 2001 From: SeokJun Jeong Date: Wed, 8 Jan 2025 08:44:13 +0900 Subject: [PATCH 10/12] =?UTF-8?q?feat:=20=EC=B6=9C=EC=84=9D=ED=95=98?= =?UTF-8?q?=EB=8A=94=20=EC=88=9C=EC=84=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/lotto/view/OutputView.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/kotlin/lotto/view/OutputView.kt b/src/main/kotlin/lotto/view/OutputView.kt index 52fc2d2c5..a9966005d 100644 --- a/src/main/kotlin/lotto/view/OutputView.kt +++ b/src/main/kotlin/lotto/view/OutputView.kt @@ -20,7 +20,7 @@ class OutputView { println("---------") Prize.entries .filter { it != Prize.NONE } - .sortedByDescending { it.count } + .reversed() .forEach { prize -> val title = if (prize == Prize.SECOND) { "${prize.count}개 일치, 보너스 볼 일치 (${prize.money}원)" From 414e25ea30380e60bdae0500f431d953080c67b8 Mon Sep 17 00:00:00 2001 From: SeokJun Jeong Date: Wed, 8 Jan 2025 08:46:31 +0900 Subject: [PATCH 11/12] =?UTF-8?q?fix:=20LottoResult.totalPrizeMoney=20?= =?UTF-8?q?=ED=83=80=EC=9E=85=EC=9D=84=20Long=EC=9C=BC=EB=A1=9C=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Int.MAX_VALUE 를 넘어서 overflow가 되면 수익률이 음수로 되는 버그를 수정 --- src/main/kotlin/lotto/domain/LottoResult.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/lotto/domain/LottoResult.kt b/src/main/kotlin/lotto/domain/LottoResult.kt index 0ad2078c6..1a5025fb8 100644 --- a/src/main/kotlin/lotto/domain/LottoResult.kt +++ b/src/main/kotlin/lotto/domain/LottoResult.kt @@ -1,10 +1,10 @@ package lotto.domain class LottoResult private constructor(private val result: Map) : Map by result { - private val totalPrizeMoney: Int + private val totalPrizeMoney: Long get() { return result.map { - it.key.money * it.value + it.key.money.toLong() * it.value }.sum() } From aabe9dc9f981e8a2e9996627c86ca7054ffc1337 Mon Sep 17 00:00:00 2001 From: SeokJun Jeong Date: Wed, 8 Jan 2025 09:06:53 +0900 Subject: [PATCH 12/12] =?UTF-8?q?test:=20PrizeTest=EC=97=90=20=EB=8B=B9?= =?UTF-8?q?=EC=B2=A8=20=EA=B0=9C=EC=88=98=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/test/kotlin/lotto/PrizeTest.kt | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/test/kotlin/lotto/PrizeTest.kt b/src/test/kotlin/lotto/PrizeTest.kt index 6743e7c08..66b02ce02 100644 --- a/src/test/kotlin/lotto/PrizeTest.kt +++ b/src/test/kotlin/lotto/PrizeTest.kt @@ -15,4 +15,13 @@ class PrizeTest { Prize.NONE.money shouldBe 0 } + @Test + fun `당첨 개수`() { + Prize.FIRST.count shouldBe 6 + Prize.SECOND.count shouldBe 5 + Prize.THIRD.count shouldBe 5 + Prize.FOURTH.count shouldBe 4 + Prize.FIFTH.count shouldBe 3 + Prize.NONE.count shouldBe 0 + } }