From 954e16bc2d8e8776c45c424f76b485744bd7c393 Mon Sep 17 00:00:00 2001 From: Arkadii Shyn Date: Thu, 27 Mar 2025 20:08:35 +0100 Subject: [PATCH 01/40] docs: design --- README.md | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index cbae739405..4e6196a506 100644 --- a/README.md +++ b/README.md @@ -1 +1,22 @@ -# kotlin-lotto \ No newline at end of file +# kotlin-lotto + +## Process + +1. Input purchase +2. Calculate purchased amount of tickets +3. Generate purchased tickets +4. Input winning numbers +5. Calculate matches +6. Calculate return rate +7. Display result + +## MVC +### Model +1. Ticket +2. Purchased Tickets +3. Lotto Result +### View +1. Input View +2. Result View +### Controller +1. Lotto Controller From 580df846239a4c1c09e16aee0a6e279c75af4190 Mon Sep 17 00:00:00 2001 From: Arkadii Shyn Date: Thu, 27 Mar 2025 20:36:15 +0100 Subject: [PATCH 02/40] docs: test cases for TicketModel --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index 4e6196a506..18b9f5bc78 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,7 @@ 7. Display result ## MVC + ### Model 1. Ticket 2. Purchased Tickets @@ -20,3 +21,9 @@ 2. Result View ### Controller 1. Lotto Controller + +## TDD + +### Ticket +- [ ] ticket should contain 6 numbers +- [ ] should fail if numbers less or more then 6 From ae0801415b1d229d602d29bb31bd1000f11b4b39 Mon Sep 17 00:00:00 2001 From: Arkadii Shyn Date: Thu, 27 Mar 2025 20:40:42 +0100 Subject: [PATCH 03/40] test: case 1.1 - failing test --- README.md | 6 +++--- src/main/kotlin/lotto/model/TicketModel.kt | 5 +++++ src/test/kotlin/lotto/model/TicketModelModelTest.kt | 13 +++++++++++++ 3 files changed, 21 insertions(+), 3 deletions(-) create mode 100644 src/main/kotlin/lotto/model/TicketModel.kt create mode 100644 src/test/kotlin/lotto/model/TicketModelModelTest.kt diff --git a/README.md b/README.md index 18b9f5bc78..1bed757bef 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,6 @@ ## TDD -### Ticket -- [ ] ticket should contain 6 numbers -- [ ] should fail if numbers less or more then 6 +### 1. Ticket +- [ ] 1.1. ticket should contain 6 numbers +- [ ] 1.2. should fail if numbers less or more then 6 diff --git a/src/main/kotlin/lotto/model/TicketModel.kt b/src/main/kotlin/lotto/model/TicketModel.kt new file mode 100644 index 0000000000..00bdc1d8de --- /dev/null +++ b/src/main/kotlin/lotto/model/TicketModel.kt @@ -0,0 +1,5 @@ +package lotto.model + +class TicketModel() { + +} \ No newline at end of file diff --git a/src/test/kotlin/lotto/model/TicketModelModelTest.kt b/src/test/kotlin/lotto/model/TicketModelModelTest.kt new file mode 100644 index 0000000000..f53b2cb710 --- /dev/null +++ b/src/test/kotlin/lotto/model/TicketModelModelTest.kt @@ -0,0 +1,13 @@ +package lotto.model + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test + +class TicketModelModelTest { + + @Test + fun `ticket should contain 6 numbers`(){ + val ticket = TicketModel() + assertThat(ticket.numbers).isEqualTo(listOf(1,2,3,4,5,6)) + } +} \ No newline at end of file From d4b1f6554b5fd81e90910d56cb8d8a5eacfa140a Mon Sep 17 00:00:00 2001 From: Arkadii Shyn Date: Thu, 27 Mar 2025 20:41:19 +0100 Subject: [PATCH 04/40] test: case 1.1 - passing test --- src/main/kotlin/lotto/model/TicketModel.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/kotlin/lotto/model/TicketModel.kt b/src/main/kotlin/lotto/model/TicketModel.kt index 00bdc1d8de..8aa8fcae6a 100644 --- a/src/main/kotlin/lotto/model/TicketModel.kt +++ b/src/main/kotlin/lotto/model/TicketModel.kt @@ -1,5 +1,5 @@ package lotto.model class TicketModel() { - + val numbers: List = listOf(1,2,3,4,5,6) } \ No newline at end of file From 5ddd2f72e7dde989996e5dabbe93d225cf6d077f Mon Sep 17 00:00:00 2001 From: Arkadii Shyn Date: Thu, 27 Mar 2025 20:43:28 +0100 Subject: [PATCH 05/40] test: case 1.1 - refactor --- src/main/kotlin/lotto/model/TicketModel.kt | 3 +-- src/test/kotlin/lotto/model/TicketModelModelTest.kt | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/main/kotlin/lotto/model/TicketModel.kt b/src/main/kotlin/lotto/model/TicketModel.kt index 8aa8fcae6a..295ff0840c 100644 --- a/src/main/kotlin/lotto/model/TicketModel.kt +++ b/src/main/kotlin/lotto/model/TicketModel.kt @@ -1,5 +1,4 @@ package lotto.model -class TicketModel() { - val numbers: List = listOf(1,2,3,4,5,6) +class TicketModel(val numbers : List) { } \ No newline at end of file diff --git a/src/test/kotlin/lotto/model/TicketModelModelTest.kt b/src/test/kotlin/lotto/model/TicketModelModelTest.kt index f53b2cb710..c54cdadc4a 100644 --- a/src/test/kotlin/lotto/model/TicketModelModelTest.kt +++ b/src/test/kotlin/lotto/model/TicketModelModelTest.kt @@ -7,7 +7,7 @@ class TicketModelModelTest { @Test fun `ticket should contain 6 numbers`(){ - val ticket = TicketModel() + val ticket = TicketModel(listOf(1,2,3,4,5,6)) assertThat(ticket.numbers).isEqualTo(listOf(1,2,3,4,5,6)) } } \ No newline at end of file From e66a204ee45d14f6f401c9ec1d4f1630fb11a083 Mon Sep 17 00:00:00 2001 From: Arkadii Shyn Date: Thu, 27 Mar 2025 20:44:54 +0100 Subject: [PATCH 06/40] test: case 1.1 - new test --- src/test/kotlin/lotto/model/TicketModelModelTest.kt | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/test/kotlin/lotto/model/TicketModelModelTest.kt b/src/test/kotlin/lotto/model/TicketModelModelTest.kt index c54cdadc4a..74d8137644 100644 --- a/src/test/kotlin/lotto/model/TicketModelModelTest.kt +++ b/src/test/kotlin/lotto/model/TicketModelModelTest.kt @@ -10,4 +10,10 @@ class TicketModelModelTest { val ticket = TicketModel(listOf(1,2,3,4,5,6)) assertThat(ticket.numbers).isEqualTo(listOf(1,2,3,4,5,6)) } + + @Test + fun `ticket should contain another 6 numbers`(){ + val ticket = TicketModel(listOf(6,5,4,3,2,1)) + assertThat(ticket.numbers).isEqualTo(listOf(6,5,4,3,2,1)) + } } \ No newline at end of file From a2a7b8ecee8b7eb39c45b92eee19247b6c258c4e Mon Sep 17 00:00:00 2001 From: Arkadii Shyn Date: Thu, 27 Mar 2025 20:51:46 +0100 Subject: [PATCH 07/40] test: case 1.1 - refactor test --- .../lotto/model/TicketModelModelTest.kt | 22 ++++++++++++------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/test/kotlin/lotto/model/TicketModelModelTest.kt b/src/test/kotlin/lotto/model/TicketModelModelTest.kt index 74d8137644..edbaf745fe 100644 --- a/src/test/kotlin/lotto/model/TicketModelModelTest.kt +++ b/src/test/kotlin/lotto/model/TicketModelModelTest.kt @@ -2,18 +2,24 @@ package lotto.model import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.Test +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.MethodSource class TicketModelModelTest { - @Test - fun `ticket should contain 6 numbers`(){ - val ticket = TicketModel(listOf(1,2,3,4,5,6)) - assertThat(ticket.numbers).isEqualTo(listOf(1,2,3,4,5,6)) + @ParameterizedTest + @MethodSource("provideNumbers") + fun `ticket should contain 6 numbers`(numbers: List) { + val ticket = TicketModel(numbers) + assertThat(ticket.numbers).isEqualTo(numbers) } - @Test - fun `ticket should contain another 6 numbers`(){ - val ticket = TicketModel(listOf(6,5,4,3,2,1)) - assertThat(ticket.numbers).isEqualTo(listOf(6,5,4,3,2,1)) + companion object { + @JvmStatic + fun provideNumbers(): List> = listOf( + listOf(1, 2, 3, 4, 5, 6), + listOf(6, 5, 4, 3, 2, 1), + listOf(8, 9, 10, 11, 12, 13) + ) } } \ No newline at end of file From 9dcb672c61e06d725a100c444436f012c110f1e2 Mon Sep 17 00:00:00 2001 From: Arkadii Shyn Date: Thu, 27 Mar 2025 20:52:09 +0100 Subject: [PATCH 08/40] test: case 1.1 - refactor test --- src/test/kotlin/lotto/model/TicketModelModelTest.kt | 1 - 1 file changed, 1 deletion(-) diff --git a/src/test/kotlin/lotto/model/TicketModelModelTest.kt b/src/test/kotlin/lotto/model/TicketModelModelTest.kt index edbaf745fe..80fdfa9c4c 100644 --- a/src/test/kotlin/lotto/model/TicketModelModelTest.kt +++ b/src/test/kotlin/lotto/model/TicketModelModelTest.kt @@ -1,7 +1,6 @@ package lotto.model import org.assertj.core.api.Assertions.assertThat -import org.junit.jupiter.api.Test import org.junit.jupiter.params.ParameterizedTest import org.junit.jupiter.params.provider.MethodSource From e107701c7fdaef5cb55cafadaf59882c88d0a182 Mon Sep 17 00:00:00 2001 From: Arkadii Shyn Date: Thu, 27 Mar 2025 20:54:29 +0100 Subject: [PATCH 09/40] test: case 1.2 - failing test --- src/test/kotlin/lotto/model/TicketModelModelTest.kt | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/test/kotlin/lotto/model/TicketModelModelTest.kt b/src/test/kotlin/lotto/model/TicketModelModelTest.kt index 80fdfa9c4c..fbf237ec4c 100644 --- a/src/test/kotlin/lotto/model/TicketModelModelTest.kt +++ b/src/test/kotlin/lotto/model/TicketModelModelTest.kt @@ -1,6 +1,8 @@ package lotto.model import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.assertThrows import org.junit.jupiter.params.ParameterizedTest import org.junit.jupiter.params.provider.MethodSource @@ -13,6 +15,11 @@ class TicketModelModelTest { assertThat(ticket.numbers).isEqualTo(numbers) } + @Test + fun `should fail if numbers less or more then 6`() { + assertThrows { TicketModel(listOf(1, 2, 3)) } + } + companion object { @JvmStatic fun provideNumbers(): List> = listOf( From 6ec260401e05fade7987b994e4b54c485bec881a Mon Sep 17 00:00:00 2001 From: Arkadii Shyn Date: Thu, 27 Mar 2025 21:54:21 +0100 Subject: [PATCH 10/40] test: case 1.2 - passing test --- src/main/kotlin/lotto/model/TicketModel.kt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/main/kotlin/lotto/model/TicketModel.kt b/src/main/kotlin/lotto/model/TicketModel.kt index 295ff0840c..7c026b776e 100644 --- a/src/main/kotlin/lotto/model/TicketModel.kt +++ b/src/main/kotlin/lotto/model/TicketModel.kt @@ -1,4 +1,7 @@ package lotto.model class TicketModel(val numbers : List) { + init { + require(numbers.size == 6) + } } \ No newline at end of file From 7b41b8cd643ca6f695bcc73acd18786f10471d0c Mon Sep 17 00:00:00 2001 From: Arkadii Shyn Date: Thu, 27 Mar 2025 21:56:04 +0100 Subject: [PATCH 11/40] test: case 1.2 - refactor --- src/main/kotlin/lotto/model/TicketModel.kt | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/lotto/model/TicketModel.kt b/src/main/kotlin/lotto/model/TicketModel.kt index 7c026b776e..a102254a0c 100644 --- a/src/main/kotlin/lotto/model/TicketModel.kt +++ b/src/main/kotlin/lotto/model/TicketModel.kt @@ -1,7 +1,11 @@ package lotto.model +const val TICKET_NUMBER_LENGTH = 6 + class TicketModel(val numbers : List) { init { - require(numbers.size == 6) + require(numbers.size == TICKET_NUMBER_LENGTH) { + "Ticket should contain $TICKET_NUMBER_LENGTH numbers" + } } } \ No newline at end of file From bfaff9b2338f556af6c7d481f4d22fd09c5bf3cf Mon Sep 17 00:00:00 2001 From: Arkadii Shyn Date: Thu, 27 Mar 2025 22:01:50 +0100 Subject: [PATCH 12/40] test: case 1.2 - refactor test --- .../kotlin/lotto/model/TicketModelModelTest.kt | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/test/kotlin/lotto/model/TicketModelModelTest.kt b/src/test/kotlin/lotto/model/TicketModelModelTest.kt index fbf237ec4c..9d73d9b589 100644 --- a/src/test/kotlin/lotto/model/TicketModelModelTest.kt +++ b/src/test/kotlin/lotto/model/TicketModelModelTest.kt @@ -9,23 +9,31 @@ import org.junit.jupiter.params.provider.MethodSource class TicketModelModelTest { @ParameterizedTest - @MethodSource("provideNumbers") + @MethodSource("validTickets") fun `ticket should contain 6 numbers`(numbers: List) { val ticket = TicketModel(numbers) assertThat(ticket.numbers).isEqualTo(numbers) } - @Test - fun `should fail if numbers less or more then 6`() { - assertThrows { TicketModel(listOf(1, 2, 3)) } + @ParameterizedTest + @MethodSource("invalidTickets") + fun `should fail if numbers less or more then 6`(numbers: List) { + assertThrows { TicketModel(numbers) } } companion object { @JvmStatic - fun provideNumbers(): List> = listOf( + fun validTickets(): List> = listOf( listOf(1, 2, 3, 4, 5, 6), listOf(6, 5, 4, 3, 2, 1), listOf(8, 9, 10, 11, 12, 13) ) + @JvmStatic + fun invalidTickets(): List> = listOf( + listOf(1, 2, 3), + listOf(3, 2, 1), + listOf(1, 2, 3, 4, 5, 6, 7), + listOf(7, 6, 5, 4, 3, 2, 1), + ) } } \ No newline at end of file From a13697a3e6bd2117fb559d09f570a479cf40439c Mon Sep 17 00:00:00 2001 From: Arkadii Shyn Date: Fri, 28 Mar 2025 21:15:40 +0100 Subject: [PATCH 13/40] dacs: test cases for PurchasedTickets --- README.md | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 1bed757bef..bd67ea3ebd 100644 --- a/README.md +++ b/README.md @@ -25,5 +25,10 @@ ## TDD ### 1. Ticket -- [ ] 1.1. ticket should contain 6 numbers -- [ ] 1.2. should fail if numbers less or more then 6 +- [x] 1.1. ticket should contain 6 numbers +- [x] 1.2. should fail if numbers less or more then 6 + +### 2. Purchased Ticket +- [ ] 2.1. should generate correct number of tickets based on purchased amount +- [ ] 2.2. should return empty list if purchased amount less that ticket price +- [ ] 2.3. should fail if purchased amount is negative From 1c4447cb26e87c95db978bf046b76f784c4d6cfd Mon Sep 17 00:00:00 2001 From: Arkadii Shyn Date: Fri, 28 Mar 2025 22:07:26 +0100 Subject: [PATCH 14/40] test: case 2.1. passing test --- README.md | 7 ++++--- src/main/kotlin/lotto/model/PurchasedTickets.kt | 6 ++++++ src/test/kotlin/lotto/model/PurchasedTicketsTest.kt | 13 +++++++++++++ src/test/kotlin/lotto/model/TicketModelModelTest.kt | 1 - 4 files changed, 23 insertions(+), 4 deletions(-) create mode 100644 src/main/kotlin/lotto/model/PurchasedTickets.kt create mode 100644 src/test/kotlin/lotto/model/PurchasedTicketsTest.kt diff --git a/README.md b/README.md index bd67ea3ebd..d84bb1fb1d 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,7 @@ - [x] 1.2. should fail if numbers less or more then 6 ### 2. Purchased Ticket -- [ ] 2.1. should generate correct number of tickets based on purchased amount -- [ ] 2.2. should return empty list if purchased amount less that ticket price -- [ ] 2.3. should fail if purchased amount is negative +- [ ] 2.1. should calculate correct number of tickets based on purchased amount +- [ ] 2.2. should generate tickets based on purchased amount +- [ ] 2.3. should return empty list if purchased amount less that ticket price +- [ ] 2.4. should fail if purchased amount is negative diff --git a/src/main/kotlin/lotto/model/PurchasedTickets.kt b/src/main/kotlin/lotto/model/PurchasedTickets.kt new file mode 100644 index 0000000000..4116b2959b --- /dev/null +++ b/src/main/kotlin/lotto/model/PurchasedTickets.kt @@ -0,0 +1,6 @@ +package lotto.model + +class PurchasedTickets(val amount: Int) { + + val ticketCount = 5 +} \ No newline at end of file diff --git a/src/test/kotlin/lotto/model/PurchasedTicketsTest.kt b/src/test/kotlin/lotto/model/PurchasedTicketsTest.kt new file mode 100644 index 0000000000..3e34fb96d8 --- /dev/null +++ b/src/test/kotlin/lotto/model/PurchasedTicketsTest.kt @@ -0,0 +1,13 @@ +package lotto.model + +import org.assertj.core.api.Assertions.assertThat; +import org.junit.jupiter.api.Test; + +class PurchasedTicketsTest { + + @Test + fun `should calculate correct number of tickets based on purchased amount`() { + val tickets = PurchasedTickets(5000); + assertThat(tickets.ticketCount).isEqualTo(5) + } +} \ No newline at end of file diff --git a/src/test/kotlin/lotto/model/TicketModelModelTest.kt b/src/test/kotlin/lotto/model/TicketModelModelTest.kt index 9d73d9b589..874dae8da4 100644 --- a/src/test/kotlin/lotto/model/TicketModelModelTest.kt +++ b/src/test/kotlin/lotto/model/TicketModelModelTest.kt @@ -1,7 +1,6 @@ package lotto.model import org.assertj.core.api.Assertions.assertThat -import org.junit.jupiter.api.Test import org.junit.jupiter.api.assertThrows import org.junit.jupiter.params.ParameterizedTest import org.junit.jupiter.params.provider.MethodSource From 4266c9f03579d817939161c43f68d7c8354de48c Mon Sep 17 00:00:00 2001 From: Arkadii Shyn Date: Fri, 28 Mar 2025 22:09:04 +0100 Subject: [PATCH 15/40] test: case 2.1. refactor --- src/main/kotlin/lotto/model/PurchasedTickets.kt | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/lotto/model/PurchasedTickets.kt b/src/main/kotlin/lotto/model/PurchasedTickets.kt index 4116b2959b..5329366583 100644 --- a/src/main/kotlin/lotto/model/PurchasedTickets.kt +++ b/src/main/kotlin/lotto/model/PurchasedTickets.kt @@ -1,6 +1,7 @@ package lotto.model -class PurchasedTickets(val amount: Int) { +const val TICKET_PRICE = 1000; - val ticketCount = 5 +class PurchasedTickets(amount: Int) { + val ticketCount = amount/TICKET_PRICE } \ No newline at end of file From 55401f462b90d4e42aaf81f967bf6c5b85108e3e Mon Sep 17 00:00:00 2001 From: Arkadii Shyn Date: Fri, 28 Mar 2025 22:40:35 +0100 Subject: [PATCH 16/40] refactor: implement ticket factory method --- src/main/kotlin/lotto/model/TicketModel.kt | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/main/kotlin/lotto/model/TicketModel.kt b/src/main/kotlin/lotto/model/TicketModel.kt index a102254a0c..a2ce164a6d 100644 --- a/src/main/kotlin/lotto/model/TicketModel.kt +++ b/src/main/kotlin/lotto/model/TicketModel.kt @@ -8,4 +8,10 @@ class TicketModel(val numbers : List) { "Ticket should contain $TICKET_NUMBER_LENGTH numbers" } } + + companion object { + fun generate(): TicketModel { + return TicketModel((1..45).shuffled().take(6)) + } + } } \ No newline at end of file From 45178dd417311dd3511069c992b0d116b4766808 Mon Sep 17 00:00:00 2001 From: Arkadii Shyn Date: Fri, 28 Mar 2025 22:50:06 +0100 Subject: [PATCH 17/40] refactor: implement buy ticket method --- src/main/kotlin/lotto/model/PurchasedTickets.kt | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/lotto/model/PurchasedTickets.kt b/src/main/kotlin/lotto/model/PurchasedTickets.kt index 5329366583..f65989cf4b 100644 --- a/src/main/kotlin/lotto/model/PurchasedTickets.kt +++ b/src/main/kotlin/lotto/model/PurchasedTickets.kt @@ -2,6 +2,11 @@ package lotto.model const val TICKET_PRICE = 1000; -class PurchasedTickets(amount: Int) { - val ticketCount = amount/TICKET_PRICE +class PurchasedTickets(private val tickets: List) { + companion object { + fun buyTickets(amount: Int): PurchasedTickets { + val ticketCount = amount/TICKET_PRICE + return PurchasedTickets( List(ticketCount) { TicketModel.generate()} ) + } + } } \ No newline at end of file From d5c83a821638db56f8d0b347d2de01dcbcb14a8b Mon Sep 17 00:00:00 2001 From: Arkadii Shyn Date: Mon, 31 Mar 2025 20:59:27 +0200 Subject: [PATCH 18/40] test: case 2.1. refactor --- src/main/kotlin/lotto/model/PurchasedTickets.kt | 2 ++ src/test/kotlin/lotto/model/PurchasedTicketsTest.kt | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/lotto/model/PurchasedTickets.kt b/src/main/kotlin/lotto/model/PurchasedTickets.kt index f65989cf4b..a2d7914af9 100644 --- a/src/main/kotlin/lotto/model/PurchasedTickets.kt +++ b/src/main/kotlin/lotto/model/PurchasedTickets.kt @@ -3,6 +3,8 @@ package lotto.model const val TICKET_PRICE = 1000; class PurchasedTickets(private val tickets: List) { + fun getTickets(): List = tickets + companion object { fun buyTickets(amount: Int): PurchasedTickets { val ticketCount = amount/TICKET_PRICE diff --git a/src/test/kotlin/lotto/model/PurchasedTicketsTest.kt b/src/test/kotlin/lotto/model/PurchasedTicketsTest.kt index 3e34fb96d8..abe9c85e2c 100644 --- a/src/test/kotlin/lotto/model/PurchasedTicketsTest.kt +++ b/src/test/kotlin/lotto/model/PurchasedTicketsTest.kt @@ -7,7 +7,7 @@ class PurchasedTicketsTest { @Test fun `should calculate correct number of tickets based on purchased amount`() { - val tickets = PurchasedTickets(5000); - assertThat(tickets.ticketCount).isEqualTo(5) + val purchasedTickets = PurchasedTickets.buyTickets(5000) + assertThat(purchasedTickets.getTickets().size).isEqualTo(5) } } \ No newline at end of file From 5c8d3a9b9fd5cd53675b0346947701b5f499672f Mon Sep 17 00:00:00 2001 From: Arkadii Shyn Date: Mon, 31 Mar 2025 21:03:23 +0200 Subject: [PATCH 19/40] test: case 2.2. failing test --- README.md | 2 +- src/test/kotlin/lotto/model/PurchasedTicketsTest.kt | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d84bb1fb1d..81f26e2e5a 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ - [x] 1.2. should fail if numbers less or more then 6 ### 2. Purchased Ticket -- [ ] 2.1. should calculate correct number of tickets based on purchased amount +- [X] 2.1. should calculate correct number of tickets based on purchased amount - [ ] 2.2. should generate tickets based on purchased amount - [ ] 2.3. should return empty list if purchased amount less that ticket price - [ ] 2.4. should fail if purchased amount is negative diff --git a/src/test/kotlin/lotto/model/PurchasedTicketsTest.kt b/src/test/kotlin/lotto/model/PurchasedTicketsTest.kt index abe9c85e2c..20e77ee427 100644 --- a/src/test/kotlin/lotto/model/PurchasedTicketsTest.kt +++ b/src/test/kotlin/lotto/model/PurchasedTicketsTest.kt @@ -10,4 +10,10 @@ class PurchasedTicketsTest { val purchasedTickets = PurchasedTickets.buyTickets(5000) assertThat(purchasedTickets.getTickets().size).isEqualTo(5) } + + @Test + fun `should generate tickets based on purchased amount`() { + val purchasedTickets = PurchasedTickets.buyTickets(1000) + assertThat(purchasedTickets.getTickets().first()).isEqualTo(TicketModel(listOf(1,2,3,4,5,6))) + } } \ No newline at end of file From 39859dfff0c478428b8fce00cde7db8f33c32fc4 Mon Sep 17 00:00:00 2001 From: Arkadii Shyn Date: Mon, 31 Mar 2025 21:25:40 +0200 Subject: [PATCH 20/40] test: add ticket number generator --- src/main/kotlin/lotto/model/RandomTicketNumberGenerator.kt | 7 +++++++ src/main/kotlin/lotto/model/TicketNumberGenerator.kt | 5 +++++ 2 files changed, 12 insertions(+) create mode 100644 src/main/kotlin/lotto/model/RandomTicketNumberGenerator.kt create mode 100644 src/main/kotlin/lotto/model/TicketNumberGenerator.kt diff --git a/src/main/kotlin/lotto/model/RandomTicketNumberGenerator.kt b/src/main/kotlin/lotto/model/RandomTicketNumberGenerator.kt new file mode 100644 index 0000000000..059ee56780 --- /dev/null +++ b/src/main/kotlin/lotto/model/RandomTicketNumberGenerator.kt @@ -0,0 +1,7 @@ +package lotto.model + +class RandomTicketNumberGenerator: TicketNumberGenerator { + override fun generateNumbers(): List { + return (1..45).shuffled().take(6).toList() + } +} \ No newline at end of file diff --git a/src/main/kotlin/lotto/model/TicketNumberGenerator.kt b/src/main/kotlin/lotto/model/TicketNumberGenerator.kt new file mode 100644 index 0000000000..820c072227 --- /dev/null +++ b/src/main/kotlin/lotto/model/TicketNumberGenerator.kt @@ -0,0 +1,5 @@ +package lotto.model + +interface TicketNumberGenerator { + fun generateNumbers(): List +} \ No newline at end of file From 4e8e38bdcb9a27885a09e34794253f82be11ee09 Mon Sep 17 00:00:00 2001 From: Arkadii Shyn Date: Mon, 31 Mar 2025 21:35:35 +0200 Subject: [PATCH 21/40] refactor: generate ticket using generator --- src/main/kotlin/lotto/model/PurchasedTickets.kt | 4 ++-- src/main/kotlin/lotto/model/TicketModel.kt | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/kotlin/lotto/model/PurchasedTickets.kt b/src/main/kotlin/lotto/model/PurchasedTickets.kt index a2d7914af9..113831e23e 100644 --- a/src/main/kotlin/lotto/model/PurchasedTickets.kt +++ b/src/main/kotlin/lotto/model/PurchasedTickets.kt @@ -6,9 +6,9 @@ class PurchasedTickets(private val tickets: List) { fun getTickets(): List = tickets companion object { - fun buyTickets(amount: Int): PurchasedTickets { + fun buyTickets(amount: Int, generator: TicketNumberGenerator): PurchasedTickets { val ticketCount = amount/TICKET_PRICE - return PurchasedTickets( List(ticketCount) { TicketModel.generate()} ) + return PurchasedTickets( List(ticketCount) { TicketModel.generate(generator)} ) } } } \ No newline at end of file diff --git a/src/main/kotlin/lotto/model/TicketModel.kt b/src/main/kotlin/lotto/model/TicketModel.kt index a2ce164a6d..a5ca280e18 100644 --- a/src/main/kotlin/lotto/model/TicketModel.kt +++ b/src/main/kotlin/lotto/model/TicketModel.kt @@ -10,8 +10,8 @@ class TicketModel(val numbers : List) { } companion object { - fun generate(): TicketModel { - return TicketModel((1..45).shuffled().take(6)) + fun generate(generator: TicketNumberGenerator): TicketModel { + return TicketModel(generator.generateNumbers()) } } } \ No newline at end of file From dbef2504716818a35de7ba8c00ffe3bc2df2ac8c Mon Sep 17 00:00:00 2001 From: Arkadii Shyn Date: Mon, 31 Mar 2025 21:50:11 +0200 Subject: [PATCH 22/40] test: case 2.2. refactoring --- src/main/kotlin/lotto/model/TicketModel.kt | 2 +- src/test/kotlin/lotto/model/PurchasedTicketsTest.kt | 11 ++++++++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/main/kotlin/lotto/model/TicketModel.kt b/src/main/kotlin/lotto/model/TicketModel.kt index a5ca280e18..4f73f315f8 100644 --- a/src/main/kotlin/lotto/model/TicketModel.kt +++ b/src/main/kotlin/lotto/model/TicketModel.kt @@ -2,7 +2,7 @@ package lotto.model const val TICKET_NUMBER_LENGTH = 6 -class TicketModel(val numbers : List) { +data class TicketModel(val numbers : List) { init { require(numbers.size == TICKET_NUMBER_LENGTH) { "Ticket should contain $TICKET_NUMBER_LENGTH numbers" diff --git a/src/test/kotlin/lotto/model/PurchasedTicketsTest.kt b/src/test/kotlin/lotto/model/PurchasedTicketsTest.kt index 20e77ee427..0c24dadb28 100644 --- a/src/test/kotlin/lotto/model/PurchasedTicketsTest.kt +++ b/src/test/kotlin/lotto/model/PurchasedTicketsTest.kt @@ -4,16 +4,21 @@ import org.assertj.core.api.Assertions.assertThat; import org.junit.jupiter.api.Test; class PurchasedTicketsTest { + private val testGenerator = TestTicketNumberGenerator(listOf(1, 2, 3, 4, 5, 6)) @Test fun `should calculate correct number of tickets based on purchased amount`() { - val purchasedTickets = PurchasedTickets.buyTickets(5000) + val purchasedTickets = PurchasedTickets.buyTickets(5000, testGenerator) assertThat(purchasedTickets.getTickets().size).isEqualTo(5) } @Test fun `should generate tickets based on purchased amount`() { - val purchasedTickets = PurchasedTickets.buyTickets(1000) - assertThat(purchasedTickets.getTickets().first()).isEqualTo(TicketModel(listOf(1,2,3,4,5,6))) + val purchasedTickets = PurchasedTickets.buyTickets(1000, testGenerator) + assertThat(purchasedTickets.getTickets().first()).isEqualTo(TicketModel(listOf(1, 2, 3, 4, 5, 6))) + } + + class TestTicketNumberGenerator(private val fixedNumbers: List) : TicketNumberGenerator { + override fun generateNumbers(): List = fixedNumbers } } \ No newline at end of file From 35960000620c9177bcaa8d487e5ff5c52b6c95c4 Mon Sep 17 00:00:00 2001 From: Arkadii Shyn Date: Mon, 31 Mar 2025 21:52:09 +0200 Subject: [PATCH 23/40] test: case 2.2. refactoring --- README.md | 2 +- src/test/kotlin/lotto/model/PurchasedTicketsTest.kt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 81f26e2e5a..20ccf0dcea 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,6 @@ ### 2. Purchased Ticket - [X] 2.1. should calculate correct number of tickets based on purchased amount -- [ ] 2.2. should generate tickets based on purchased amount +- [ ] 2.2. should generate tickets with correct numbers using generator - [ ] 2.3. should return empty list if purchased amount less that ticket price - [ ] 2.4. should fail if purchased amount is negative diff --git a/src/test/kotlin/lotto/model/PurchasedTicketsTest.kt b/src/test/kotlin/lotto/model/PurchasedTicketsTest.kt index 0c24dadb28..c325a11da9 100644 --- a/src/test/kotlin/lotto/model/PurchasedTicketsTest.kt +++ b/src/test/kotlin/lotto/model/PurchasedTicketsTest.kt @@ -13,7 +13,7 @@ class PurchasedTicketsTest { } @Test - fun `should generate tickets based on purchased amount`() { + fun `should generate tickets with correct numbers using generator`() { val purchasedTickets = PurchasedTickets.buyTickets(1000, testGenerator) assertThat(purchasedTickets.getTickets().first()).isEqualTo(TicketModel(listOf(1, 2, 3, 4, 5, 6))) } From 02a7229709d9fe532977ba5da5eb13c0799c6902 Mon Sep 17 00:00:00 2001 From: Arkadii Shyn Date: Mon, 31 Mar 2025 21:53:25 +0200 Subject: [PATCH 24/40] test: case 2.3. passing test --- README.md | 4 ++-- src/test/kotlin/lotto/model/PurchasedTicketsTest.kt | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 20ccf0dcea..5f758275b1 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,6 @@ ### 2. Purchased Ticket - [X] 2.1. should calculate correct number of tickets based on purchased amount -- [ ] 2.2. should generate tickets with correct numbers using generator -- [ ] 2.3. should return empty list if purchased amount less that ticket price +- [x] 2.2. should generate tickets with correct numbers using generator +- [x] 2.3. should return empty list if purchased amount less that ticket price - [ ] 2.4. should fail if purchased amount is negative diff --git a/src/test/kotlin/lotto/model/PurchasedTicketsTest.kt b/src/test/kotlin/lotto/model/PurchasedTicketsTest.kt index c325a11da9..80ef300c69 100644 --- a/src/test/kotlin/lotto/model/PurchasedTicketsTest.kt +++ b/src/test/kotlin/lotto/model/PurchasedTicketsTest.kt @@ -18,6 +18,12 @@ class PurchasedTicketsTest { assertThat(purchasedTickets.getTickets().first()).isEqualTo(TicketModel(listOf(1, 2, 3, 4, 5, 6))) } + @Test + fun `should return empty list if purchased amount less that ticket price`() { + val purchasedTickets = PurchasedTickets.buyTickets(999, testGenerator) + assertThat(purchasedTickets.getTickets().size).isEqualTo(0) + } + class TestTicketNumberGenerator(private val fixedNumbers: List) : TicketNumberGenerator { override fun generateNumbers(): List = fixedNumbers } From 87511259ae35e3525026cb55994f00ac7a90fe10 Mon Sep 17 00:00:00 2001 From: Arkadii Shyn Date: Mon, 31 Mar 2025 21:56:01 +0200 Subject: [PATCH 25/40] test: case 2.4. failing test --- src/test/kotlin/lotto/model/PurchasedTicketsTest.kt | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/test/kotlin/lotto/model/PurchasedTicketsTest.kt b/src/test/kotlin/lotto/model/PurchasedTicketsTest.kt index 80ef300c69..93efa1fa1a 100644 --- a/src/test/kotlin/lotto/model/PurchasedTicketsTest.kt +++ b/src/test/kotlin/lotto/model/PurchasedTicketsTest.kt @@ -2,6 +2,7 @@ package lotto.model import org.assertj.core.api.Assertions.assertThat; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.assertThrows class PurchasedTicketsTest { private val testGenerator = TestTicketNumberGenerator(listOf(1, 2, 3, 4, 5, 6)) @@ -24,6 +25,13 @@ class PurchasedTicketsTest { assertThat(purchasedTickets.getTickets().size).isEqualTo(0) } + @Test + fun `should fail if purchased amount is negative`() { + assertThrows { + PurchasedTickets.buyTickets(-1, testGenerator) + } + } + class TestTicketNumberGenerator(private val fixedNumbers: List) : TicketNumberGenerator { override fun generateNumbers(): List = fixedNumbers } From 855ee4bf848727d7e013ee1ca6e39d10c674dc30 Mon Sep 17 00:00:00 2001 From: Arkadii Shyn Date: Mon, 31 Mar 2025 21:56:40 +0200 Subject: [PATCH 26/40] test: case 2.4. passing test --- src/main/kotlin/lotto/model/PurchasedTickets.kt | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/lotto/model/PurchasedTickets.kt b/src/main/kotlin/lotto/model/PurchasedTickets.kt index 113831e23e..df26055c8c 100644 --- a/src/main/kotlin/lotto/model/PurchasedTickets.kt +++ b/src/main/kotlin/lotto/model/PurchasedTickets.kt @@ -7,8 +7,9 @@ class PurchasedTickets(private val tickets: List) { companion object { fun buyTickets(amount: Int, generator: TicketNumberGenerator): PurchasedTickets { - val ticketCount = amount/TICKET_PRICE - return PurchasedTickets( List(ticketCount) { TicketModel.generate(generator)} ) + require(amount > 0) + val ticketCount = amount / TICKET_PRICE + return PurchasedTickets(List(ticketCount) { TicketModel.generate(generator) }) } } } \ No newline at end of file From 9bb66939ed8b18b207a8d334fbd38e8b981e933f Mon Sep 17 00:00:00 2001 From: Arkadii Shyn Date: Mon, 31 Mar 2025 22:38:25 +0200 Subject: [PATCH 27/40] test: test cases for Lotto Result --- README.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 5f758275b1..95ca56fa33 100644 --- a/README.md +++ b/README.md @@ -32,4 +32,9 @@ - [X] 2.1. should calculate correct number of tickets based on purchased amount - [x] 2.2. should generate tickets with correct numbers using generator - [x] 2.3. should return empty list if purchased amount less that ticket price -- [ ] 2.4. should fail if purchased amount is negative +- [x] 2.4. should fail if purchased amount is negative + +### 3. Lotto Result +- [ ] 3.1. should calculate matches +- [ ] 3.1. should calculate total prize correctly +- [ ] 3.2. should calculate return rate correctly From a019f154d9b0fa1cf6d4dc2ed5bb05fe5dcff39f Mon Sep 17 00:00:00 2001 From: Arkadii Shyn Date: Mon, 31 Mar 2025 22:53:18 +0200 Subject: [PATCH 28/40] test: case 3.1. failing test --- src/test/kotlin/lotto/model/LottoResultTest.kt | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 src/test/kotlin/lotto/model/LottoResultTest.kt diff --git a/src/test/kotlin/lotto/model/LottoResultTest.kt b/src/test/kotlin/lotto/model/LottoResultTest.kt new file mode 100644 index 0000000000..8026c57b82 --- /dev/null +++ b/src/test/kotlin/lotto/model/LottoResultTest.kt @@ -0,0 +1,17 @@ +package lotto.model + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test + +class LottoResultTest { + @Test + fun `should calculate matches`() { + val winningNumbers = listOf(1, 2, 3, 4, 5, 6) + val tickets = listOf( + TicketModel(listOf(1, 2, 3, 4, 5, 6)) + ) + val lottoResult = LottoResult(winningNumbers, tickets) + val result = lottoResult.calculateResult() + assertThat(result[6]).isEqualTo(1) + } +} \ No newline at end of file From aa601be88b141f7e3e4c3ac2d51d1253ec70b96e Mon Sep 17 00:00:00 2001 From: Arkadii Shyn Date: Mon, 31 Mar 2025 22:53:24 +0200 Subject: [PATCH 29/40] test: case 3.1. passing test --- src/main/kotlin/lotto/model/LottoResult.kt | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 src/main/kotlin/lotto/model/LottoResult.kt diff --git a/src/main/kotlin/lotto/model/LottoResult.kt b/src/main/kotlin/lotto/model/LottoResult.kt new file mode 100644 index 0000000000..6ee6cebe73 --- /dev/null +++ b/src/main/kotlin/lotto/model/LottoResult.kt @@ -0,0 +1,8 @@ +package lotto.model + +class LottoResult(private val winningNumbers: List, private val tickets: List) { + fun calculateResult(): Map { + return mapOf(Pair(6,1)) + } + +} \ No newline at end of file From 181397d6b0d24dde8011ae2a9ff69eca6b3fa442 Mon Sep 17 00:00:00 2001 From: Arkadii Shyn Date: Mon, 31 Mar 2025 23:06:56 +0200 Subject: [PATCH 30/40] test: case 3.1. refactor --- src/main/kotlin/lotto/model/LottoResult.kt | 6 ++++-- src/test/kotlin/lotto/model/LottoResultTest.kt | 4 ++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/main/kotlin/lotto/model/LottoResult.kt b/src/main/kotlin/lotto/model/LottoResult.kt index 6ee6cebe73..a5232e72cd 100644 --- a/src/main/kotlin/lotto/model/LottoResult.kt +++ b/src/main/kotlin/lotto/model/LottoResult.kt @@ -2,7 +2,9 @@ package lotto.model class LottoResult(private val winningNumbers: List, private val tickets: List) { fun calculateResult(): Map { - return mapOf(Pair(6,1)) + return tickets.groupingBy { ticket -> + ticket.numbers.count { it in winningNumbers } + } + .eachCount() } - } \ No newline at end of file diff --git a/src/test/kotlin/lotto/model/LottoResultTest.kt b/src/test/kotlin/lotto/model/LottoResultTest.kt index 8026c57b82..d3b037ade9 100644 --- a/src/test/kotlin/lotto/model/LottoResultTest.kt +++ b/src/test/kotlin/lotto/model/LottoResultTest.kt @@ -8,10 +8,10 @@ class LottoResultTest { fun `should calculate matches`() { val winningNumbers = listOf(1, 2, 3, 4, 5, 6) val tickets = listOf( - TicketModel(listOf(1, 2, 3, 4, 5, 6)) + TicketModel(listOf(1, 2, 3, 4, 5, 7)) ) val lottoResult = LottoResult(winningNumbers, tickets) val result = lottoResult.calculateResult() - assertThat(result[6]).isEqualTo(1) + assertThat(result[5]).isEqualTo(1) } } \ No newline at end of file From e575348e598697f3b6542853b0cac4f07e89db22 Mon Sep 17 00:00:00 2001 From: Arkadii Shyn Date: Mon, 31 Mar 2025 23:11:52 +0200 Subject: [PATCH 31/40] test: case 3.2. failing test --- src/main/kotlin/lotto/model/LottoResult.kt | 4 ++++ src/test/kotlin/lotto/model/LottoResultTest.kt | 13 +++++++++++++ 2 files changed, 17 insertions(+) diff --git a/src/main/kotlin/lotto/model/LottoResult.kt b/src/main/kotlin/lotto/model/LottoResult.kt index a5232e72cd..38dae535e2 100644 --- a/src/main/kotlin/lotto/model/LottoResult.kt +++ b/src/main/kotlin/lotto/model/LottoResult.kt @@ -7,4 +7,8 @@ class LottoResult(private val winningNumbers: List, private val tickets: Li } .eachCount() } + + fun totalPrize(): Int{ + return 0 + } } \ No newline at end of file diff --git a/src/test/kotlin/lotto/model/LottoResultTest.kt b/src/test/kotlin/lotto/model/LottoResultTest.kt index d3b037ade9..e07b05a1ea 100644 --- a/src/test/kotlin/lotto/model/LottoResultTest.kt +++ b/src/test/kotlin/lotto/model/LottoResultTest.kt @@ -14,4 +14,17 @@ class LottoResultTest { val result = lottoResult.calculateResult() assertThat(result[5]).isEqualTo(1) } + + @Test + fun `should calculate total prize correctly`() { + val winningNumbers = listOf(1, 2, 3, 4, 5, 6) + val tickets = listOf( + TicketModel(listOf(1, 2, 3, 4, 5, 6)), + TicketModel(listOf(1, 2, 3, 4, 5, 7)), + TicketModel(listOf(1, 2, 3, 4, 7, 8)), + + ) + val lottoResult = LottoResult(winningNumbers, tickets) + assertThat(lottoResult.totalPrize()).isEqualTo(2_001_550_000) + } } \ No newline at end of file From e28b3dafe62aaa0a2f56827d1dac0bb212efcff5 Mon Sep 17 00:00:00 2001 From: Arkadii Shyn Date: Mon, 31 Mar 2025 23:12:15 +0200 Subject: [PATCH 32/40] test: case 3.2. passing test --- src/main/kotlin/lotto/model/LottoResult.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/kotlin/lotto/model/LottoResult.kt b/src/main/kotlin/lotto/model/LottoResult.kt index 38dae535e2..7980dac218 100644 --- a/src/main/kotlin/lotto/model/LottoResult.kt +++ b/src/main/kotlin/lotto/model/LottoResult.kt @@ -9,6 +9,6 @@ class LottoResult(private val winningNumbers: List, private val tickets: Li } fun totalPrize(): Int{ - return 0 + return 2_001_550_000 } } \ No newline at end of file From 87a01f4b1dd87f226cff8b01295af3f752d39792 Mon Sep 17 00:00:00 2001 From: Arkadii Shyn Date: Mon, 31 Mar 2025 23:14:12 +0200 Subject: [PATCH 33/40] test: case 3.2. refactor --- src/main/kotlin/lotto/model/LottoResult.kt | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/lotto/model/LottoResult.kt b/src/main/kotlin/lotto/model/LottoResult.kt index 7980dac218..732623bf17 100644 --- a/src/main/kotlin/lotto/model/LottoResult.kt +++ b/src/main/kotlin/lotto/model/LottoResult.kt @@ -9,6 +9,15 @@ class LottoResult(private val winningNumbers: List, private val tickets: Li } fun totalPrize(): Int{ - return 2_001_550_000 + val prizeMap = mapOf( + 3 to 5_000, + 4 to 50_000, + 5 to 1_500_000, + 6 to 2_000_000_000 + ) + + return calculateResult().entries.sumOf { (matchCount, count) -> + prizeMap[matchCount]?.times(count) ?: 0 + } } } \ No newline at end of file From 553ac317351908d44e57b6f0e24c6bbab75a19bb Mon Sep 17 00:00:00 2001 From: Arkadii Shyn Date: Mon, 31 Mar 2025 23:18:37 +0200 Subject: [PATCH 34/40] test: case 3.3. passing test --- src/main/kotlin/lotto/model/LottoResult.kt | 4 ++++ src/test/kotlin/lotto/model/LottoResultTest.kt | 13 +++++++++++++ 2 files changed, 17 insertions(+) diff --git a/src/main/kotlin/lotto/model/LottoResult.kt b/src/main/kotlin/lotto/model/LottoResult.kt index 732623bf17..3a880b38cf 100644 --- a/src/main/kotlin/lotto/model/LottoResult.kt +++ b/src/main/kotlin/lotto/model/LottoResult.kt @@ -20,4 +20,8 @@ class LottoResult(private val winningNumbers: List, private val tickets: Li prizeMap[matchCount]?.times(count) ?: 0 } } + + fun returnRate(): Double { + return 667_183.33 + } } \ No newline at end of file diff --git a/src/test/kotlin/lotto/model/LottoResultTest.kt b/src/test/kotlin/lotto/model/LottoResultTest.kt index e07b05a1ea..598089d04f 100644 --- a/src/test/kotlin/lotto/model/LottoResultTest.kt +++ b/src/test/kotlin/lotto/model/LottoResultTest.kt @@ -27,4 +27,17 @@ class LottoResultTest { val lottoResult = LottoResult(winningNumbers, tickets) assertThat(lottoResult.totalPrize()).isEqualTo(2_001_550_000) } + + @Test + fun `should calculate return rate correctly`() { + val winningNumbers = listOf(1, 2, 3, 4, 5, 6) + val tickets = listOf( + TicketModel(listOf(1, 2, 3, 4, 5, 6)), + TicketModel(listOf(1, 2, 3, 4, 5, 7)), + TicketModel(listOf(1, 2, 3, 4, 7, 8)), + + ) + val lottoResult = LottoResult(winningNumbers, tickets) + assertThat(lottoResult.returnRate()).isEqualTo(667_183.33) + } } \ No newline at end of file From 278a134499bfca3c8aa77a6bdf608299e3f6c369 Mon Sep 17 00:00:00 2001 From: Arkadii Shyn Date: Mon, 31 Mar 2025 23:22:00 +0200 Subject: [PATCH 35/40] test: case 3.3. refactor --- src/main/kotlin/lotto/model/LottoResult.kt | 4 ++-- src/test/kotlin/lotto/model/LottoResultTest.kt | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/main/kotlin/lotto/model/LottoResult.kt b/src/main/kotlin/lotto/model/LottoResult.kt index 3a880b38cf..ec76f9192e 100644 --- a/src/main/kotlin/lotto/model/LottoResult.kt +++ b/src/main/kotlin/lotto/model/LottoResult.kt @@ -21,7 +21,7 @@ class LottoResult(private val winningNumbers: List, private val tickets: Li } } - fun returnRate(): Double { - return 667_183.33 + fun returnRate(purchaseAmount: Int): Double { + return if (purchaseAmount == 0) 0.0 else totalPrize().toDouble() / purchaseAmount } } \ No newline at end of file diff --git a/src/test/kotlin/lotto/model/LottoResultTest.kt b/src/test/kotlin/lotto/model/LottoResultTest.kt index 598089d04f..4f67491f06 100644 --- a/src/test/kotlin/lotto/model/LottoResultTest.kt +++ b/src/test/kotlin/lotto/model/LottoResultTest.kt @@ -38,6 +38,7 @@ class LottoResultTest { ) val lottoResult = LottoResult(winningNumbers, tickets) - assertThat(lottoResult.returnRate()).isEqualTo(667_183.33) + val purchasedAmount = 3000 + assertThat(lottoResult.returnRate(purchasedAmount)).isEqualTo(667_183.3333333334) } } \ No newline at end of file From 0b81b9f32b87f512a45c6a97269cd0a856f5496b Mon Sep 17 00:00:00 2001 From: Arkadii Shyn Date: Mon, 31 Mar 2025 23:27:03 +0200 Subject: [PATCH 36/40] test: case 3.4. implementation --- README.md | 1 + src/main/kotlin/lotto/model/LottoResult.kt | 3 +++ src/test/kotlin/lotto/model/LottoResultTest.kt | 11 +++++++++++ 3 files changed, 15 insertions(+) diff --git a/README.md b/README.md index 95ca56fa33..d8c41cfd48 100644 --- a/README.md +++ b/README.md @@ -38,3 +38,4 @@ - [ ] 3.1. should calculate matches - [ ] 3.1. should calculate total prize correctly - [ ] 3.2. should calculate return rate correctly +- [ ] 3.4. should return empty map if matches less than 3 diff --git a/src/main/kotlin/lotto/model/LottoResult.kt b/src/main/kotlin/lotto/model/LottoResult.kt index ec76f9192e..4943145c5d 100644 --- a/src/main/kotlin/lotto/model/LottoResult.kt +++ b/src/main/kotlin/lotto/model/LottoResult.kt @@ -1,11 +1,14 @@ package lotto.model +const val MINIMUM_MATCHES = 3 + class LottoResult(private val winningNumbers: List, private val tickets: List) { fun calculateResult(): Map { return tickets.groupingBy { ticket -> ticket.numbers.count { it in winningNumbers } } .eachCount() + .filterKeys { it >= MINIMUM_MATCHES } } fun totalPrize(): Int{ diff --git a/src/test/kotlin/lotto/model/LottoResultTest.kt b/src/test/kotlin/lotto/model/LottoResultTest.kt index 4f67491f06..017f88f0ee 100644 --- a/src/test/kotlin/lotto/model/LottoResultTest.kt +++ b/src/test/kotlin/lotto/model/LottoResultTest.kt @@ -15,6 +15,17 @@ class LottoResultTest { assertThat(result[5]).isEqualTo(1) } + @Test + fun `should return empty map if matches less than 3`() { + val winningNumbers = listOf(1, 2, 3, 4, 5, 6) + val tickets = listOf( + TicketModel(listOf(1, 2, 7, 8, 9, 10)) + ) + val lottoResult = LottoResult(winningNumbers, tickets) + val result = lottoResult.calculateResult() + assertThat(result).isEmpty() + } + @Test fun `should calculate total prize correctly`() { val winningNumbers = listOf(1, 2, 3, 4, 5, 6) From 51a77dc89bde52791a4915542ad405524a5e8f40 Mon Sep 17 00:00:00 2001 From: Arkadii Shyn Date: Mon, 31 Mar 2025 23:27:45 +0200 Subject: [PATCH 37/40] docs: mark implemented tests --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index d8c41cfd48..52235f1783 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,7 @@ - [x] 2.4. should fail if purchased amount is negative ### 3. Lotto Result -- [ ] 3.1. should calculate matches -- [ ] 3.1. should calculate total prize correctly -- [ ] 3.2. should calculate return rate correctly -- [ ] 3.4. should return empty map if matches less than 3 +- [x] 3.1. should calculate matches +- [x] 3.1. should calculate total prize correctly +- [x] 3.2. should calculate return rate correctly +- [x] 3.4. should return empty map if matches less than 3 From 6b21092ca3b741e0340b305d78d19aef9db0aff0 Mon Sep 17 00:00:00 2001 From: Arkadii Shyn Date: Mon, 31 Mar 2025 23:37:16 +0200 Subject: [PATCH 38/40] feat: LottoController --- src/main/kotlin/lotto/Main.kt | 7 +++++++ .../lotto/controller/LottoController.kt | 20 +++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 src/main/kotlin/lotto/Main.kt create mode 100644 src/main/kotlin/lotto/controller/LottoController.kt diff --git a/src/main/kotlin/lotto/Main.kt b/src/main/kotlin/lotto/Main.kt new file mode 100644 index 0000000000..403b6a06e6 --- /dev/null +++ b/src/main/kotlin/lotto/Main.kt @@ -0,0 +1,7 @@ +package lotto + +import lotto.controller.LottoController + +fun main() { + LottoController.run() +} \ No newline at end of file diff --git a/src/main/kotlin/lotto/controller/LottoController.kt b/src/main/kotlin/lotto/controller/LottoController.kt new file mode 100644 index 0000000000..62d08b9e0f --- /dev/null +++ b/src/main/kotlin/lotto/controller/LottoController.kt @@ -0,0 +1,20 @@ +package lotto.controller + +import lotto.model.LottoResult +import lotto.model.PurchasedTickets +import lotto.model.RandomTicketNumberGenerator + +class LottoController { + + companion object { + private val generator = RandomTicketNumberGenerator() + fun run() { + val purchasedAmount = 5000 + val purchasedTickets = PurchasedTickets.buyTickets(purchasedAmount, generator) + println(purchasedTickets.getTickets()) + val winningNumbers = listOf(1, 2, 3, 4, 5, 6) + val result = LottoResult(winningNumbers, purchasedTickets.getTickets()) + println(result) + } + } +} \ No newline at end of file From 2d8f4bc0f61d1c23d8e8354fdbfbf967403ccd51 Mon Sep 17 00:00:00 2001 From: Arkadii Shyn Date: Tue, 1 Apr 2025 00:05:24 +0200 Subject: [PATCH 39/40] feat: InputView --- .../lotto/controller/LottoController.kt | 5 ++-- src/main/kotlin/lotto/view/InputView.kt | 23 +++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 src/main/kotlin/lotto/view/InputView.kt diff --git a/src/main/kotlin/lotto/controller/LottoController.kt b/src/main/kotlin/lotto/controller/LottoController.kt index 62d08b9e0f..cff5ffb039 100644 --- a/src/main/kotlin/lotto/controller/LottoController.kt +++ b/src/main/kotlin/lotto/controller/LottoController.kt @@ -3,16 +3,17 @@ package lotto.controller import lotto.model.LottoResult import lotto.model.PurchasedTickets import lotto.model.RandomTicketNumberGenerator +import lotto.view.InputView class LottoController { companion object { private val generator = RandomTicketNumberGenerator() fun run() { - val purchasedAmount = 5000 + val purchasedAmount = InputView.readPurchaseAmount() val purchasedTickets = PurchasedTickets.buyTickets(purchasedAmount, generator) println(purchasedTickets.getTickets()) - val winningNumbers = listOf(1, 2, 3, 4, 5, 6) + val winningNumbers = InputView.readWinningNumbers() val result = LottoResult(winningNumbers, purchasedTickets.getTickets()) println(result) } diff --git a/src/main/kotlin/lotto/view/InputView.kt b/src/main/kotlin/lotto/view/InputView.kt new file mode 100644 index 0000000000..cd45e16927 --- /dev/null +++ b/src/main/kotlin/lotto/view/InputView.kt @@ -0,0 +1,23 @@ +package lotto.view + +class InputView { + + companion object { + fun readPurchaseAmount(): Int { + println("Please enter the purchase amount.") + return readlnOrNull()?.toIntOrNull() + ?.takeIf { it >= 1000 } + ?: throw IllegalArgumentException("Invalid amount. Must be at least 1,000 KRW.") + } + + fun readWinningNumbers(): List { + println("Please enter last week’s winning numbers.") + return readlnOrNull() + ?.split(",") + ?.map { it.trim().toInt() } + ?.toList() + ?.takeIf { it.size == 6 } + ?: throw IllegalArgumentException("Invalid input. Must enter exactly 6 unique numbers.") + } + } +} \ No newline at end of file From eb29f3f1c24bd407bed0b456936ee54034483d86 Mon Sep 17 00:00:00 2001 From: Arkadii Shyn Date: Tue, 1 Apr 2025 00:11:04 +0200 Subject: [PATCH 40/40] feat: ResultView --- .../lotto/controller/LottoController.kt | 6 ++-- src/main/kotlin/lotto/view/ResultView.kt | 36 +++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 src/main/kotlin/lotto/view/ResultView.kt diff --git a/src/main/kotlin/lotto/controller/LottoController.kt b/src/main/kotlin/lotto/controller/LottoController.kt index cff5ffb039..1f84445e75 100644 --- a/src/main/kotlin/lotto/controller/LottoController.kt +++ b/src/main/kotlin/lotto/controller/LottoController.kt @@ -4,6 +4,7 @@ import lotto.model.LottoResult import lotto.model.PurchasedTickets import lotto.model.RandomTicketNumberGenerator import lotto.view.InputView +import lotto.view.ResultView class LottoController { @@ -12,10 +13,11 @@ class LottoController { fun run() { val purchasedAmount = InputView.readPurchaseAmount() val purchasedTickets = PurchasedTickets.buyTickets(purchasedAmount, generator) - println(purchasedTickets.getTickets()) + ResultView.showTickets(purchasedTickets.getTickets()) + val winningNumbers = InputView.readWinningNumbers() val result = LottoResult(winningNumbers, purchasedTickets.getTickets()) - println(result) + ResultView.showStatistics(result, purchasedAmount) } } } \ No newline at end of file diff --git a/src/main/kotlin/lotto/view/ResultView.kt b/src/main/kotlin/lotto/view/ResultView.kt new file mode 100644 index 0000000000..3b5bc99b46 --- /dev/null +++ b/src/main/kotlin/lotto/view/ResultView.kt @@ -0,0 +1,36 @@ +package lotto.view + +import lotto.model.LottoResult +import lotto.model.TicketModel + +class ResultView { + + companion object { + fun showTickets(tickets: List) { + println("You have purchased ${tickets.size} tickets.") + tickets.forEach { println(it) } + } + + fun showStatistics(result: LottoResult, purchaseAmount: Int) { + println("Winning Statistics") + println("------------------") + + val stats = result.calculateResult() + val prizeMap = mapOf( + 3 to 5_000L, + 4 to 50_000L, + 5 to 1_500_000L, + 6 to 2_000_000_000L + ) + + prizeMap.forEach { (matchCount, prize) -> + val count = stats.getOrDefault(matchCount, 0) + println("$matchCount Matches ($prize KRW) - $count tickets") + } + + val returnRate = result.returnRate(purchaseAmount) + println("Total return rate is %.2f (A rate below 1 means a loss)".format(returnRate)) + } + } + +} \ No newline at end of file