From 81f7f600008f5ffa46ba4676b39820e9beb58477 Mon Sep 17 00:00:00 2001 From: "haein.lee" Date: Sun, 1 Jan 2023 23:02:05 +0900 Subject: [PATCH 1/3] =?UTF-8?q?Readme=20=EA=B5=AC=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 24 ++++++++++++++++++- src/main/kotlin/LottoController.kt | 9 +++++++ src/main/kotlin/domain/LottoInput.kt | 6 +++++ .../kotlin/service/AutoNumberGenerator.kt | 5 ++++ .../kotlin/service/RandomNumberGenerator.kt | 14 +++++++++++ src/test/kotlin/RandomNumberGeneratorTest.kt | 11 +++++++++ 6 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 src/main/kotlin/LottoController.kt create mode 100644 src/main/kotlin/domain/LottoInput.kt create mode 100644 src/main/kotlin/service/AutoNumberGenerator.kt create mode 100644 src/main/kotlin/service/RandomNumberGenerator.kt create mode 100644 src/test/kotlin/RandomNumberGeneratorTest.kt diff --git a/README.md b/README.md index cbae739405..83939e9d0d 100644 --- a/README.md +++ b/README.md @@ -1 +1,23 @@ -# kotlin-lotto \ No newline at end of file +# kotlin-lotto + +## 자동 만들기 TODO +### 1. Input View + - [ ] 구입금액 입력받기 + - [ ] 당첨번호 입력받기 + +### 2. Output View + - [ ] 생성된 로또번호 출력하기 + - [ ] 당첨통계 출력하기 + +### 3. Service + - [ ] 자동번호 등록하기 + - [ ] 랜덤번호 생성하기 + - [ ] 당첨번호 등록하기 + - [ ] 통계내기 - 등수계산하기 + - [ ] 통계내기 - 수익률계산하기 + +### 4. Domain + - [ ] 로또번호 - 구매번호 + - [ ] 로또번호 - 당첨번호 + - [ ] input - 구입금액 + - [ ] 통계 \ No newline at end of file diff --git a/src/main/kotlin/LottoController.kt b/src/main/kotlin/LottoController.kt new file mode 100644 index 0000000000..ebb616a473 --- /dev/null +++ b/src/main/kotlin/LottoController.kt @@ -0,0 +1,9 @@ +class LottoController { + fun start(){ + println("start game") + } +} + +fun main() { + LottoController().start() +} \ No newline at end of file diff --git a/src/main/kotlin/domain/LottoInput.kt b/src/main/kotlin/domain/LottoInput.kt new file mode 100644 index 0000000000..8b22c7f53e --- /dev/null +++ b/src/main/kotlin/domain/LottoInput.kt @@ -0,0 +1,6 @@ +package domain + +interface LottoInput { + val totalPrice: Int + val autoAmount: Int +} \ No newline at end of file diff --git a/src/main/kotlin/service/AutoNumberGenerator.kt b/src/main/kotlin/service/AutoNumberGenerator.kt new file mode 100644 index 0000000000..7565cc623e --- /dev/null +++ b/src/main/kotlin/service/AutoNumberGenerator.kt @@ -0,0 +1,5 @@ +package service + +class AutoNumberGenerator { + +} \ No newline at end of file diff --git a/src/main/kotlin/service/RandomNumberGenerator.kt b/src/main/kotlin/service/RandomNumberGenerator.kt new file mode 100644 index 0000000000..991fe7ac8d --- /dev/null +++ b/src/main/kotlin/service/RandomNumberGenerator.kt @@ -0,0 +1,14 @@ +package service + +class RandomNumberGenerator(override val startNumber: Int, override val endNumber: Int) : + NumberGenerator { + override fun getGeneratedNumber(): List = + (startNumber..endNumber).map { it }.shuffled().slice(0..5) + +} + +interface NumberGenerator { + fun getGeneratedNumber(): List + val startNumber: Int + val endNumber: Int +} \ No newline at end of file diff --git a/src/test/kotlin/RandomNumberGeneratorTest.kt b/src/test/kotlin/RandomNumberGeneratorTest.kt new file mode 100644 index 0000000000..d401fb3af9 --- /dev/null +++ b/src/test/kotlin/RandomNumberGeneratorTest.kt @@ -0,0 +1,11 @@ +import org.junit.jupiter.api.Test +import service.RandomNumberGenerator + +class RandomNumberGeneratorTest { + + @Test + fun `랜덤하게`(){ + val randomNumberGenerator = RandomNumberGenerator(1,45) + println(randomNumberGenerator.getGeneratedNumber()) + } +} \ No newline at end of file From 1fdf9c7d90c107e4fe2c4146535bbee5f9bb131e Mon Sep 17 00:00:00 2001 From: "haein.lee" Date: Mon, 2 Jan 2023 20:57:53 +0900 Subject: [PATCH 2/3] =?UTF-8?q?service=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 7 +++--- src/main/kotlin/LottoController.kt | 22 ++++++++++++++++++- src/main/kotlin/domain/Lotto.kt | 16 ++++++++++++++ src/main/kotlin/domain/LottoInput.kt | 13 ++++++++--- src/main/kotlin/domain/Rank.kt | 17 ++++++++++++++ src/main/kotlin/domain/Statistic.kt | 3 +++ .../kotlin/service/AutoNumberGenerator.kt | 12 +++++++++- .../service/CalculateWinningStatistic.kt | 16 ++++++++++++++ .../kotlin/service/RandomNumberGenerator.kt | 1 - .../kotlin/service/WinningNumberRegister.kt | 12 ++++++++++ src/test/kotlin/AutoNumberGeneratorTest.kt | 19 ++++++++++++++++ src/test/kotlin/LottoTest.kt | 16 ++++++++++++++ 12 files changed, 145 insertions(+), 9 deletions(-) create mode 100644 src/main/kotlin/domain/Lotto.kt create mode 100644 src/main/kotlin/domain/Rank.kt create mode 100644 src/main/kotlin/domain/Statistic.kt create mode 100644 src/main/kotlin/service/CalculateWinningStatistic.kt create mode 100644 src/main/kotlin/service/WinningNumberRegister.kt create mode 100644 src/test/kotlin/AutoNumberGeneratorTest.kt create mode 100644 src/test/kotlin/LottoTest.kt diff --git a/README.md b/README.md index 83939e9d0d..d3e4720a10 100644 --- a/README.md +++ b/README.md @@ -10,9 +10,10 @@ - [ ] 당첨통계 출력하기 ### 3. Service - - [ ] 자동번호 등록하기 - - [ ] 랜덤번호 생성하기 - - [ ] 당첨번호 등록하기 + - [X] 자동번호 등록하기 + - [X] 랜덤번호 생성하기 + - [X] 당첨번호 등록하기 + - [x] 통계내기 - 당첨 개수 확인하기 - [ ] 통계내기 - 등수계산하기 - [ ] 통계내기 - 수익률계산하기 diff --git a/src/main/kotlin/LottoController.kt b/src/main/kotlin/LottoController.kt index ebb616a473..2a6074b6cf 100644 --- a/src/main/kotlin/LottoController.kt +++ b/src/main/kotlin/LottoController.kt @@ -1,6 +1,26 @@ +import domain.LottoInput +import domain.WinningLotto +import service.AutoNumberGenerator +import service.CalculateWinningStatistic +import service.RandomNumberGenerator +import service.WinningNumberRegister + class LottoController { fun start(){ - println("start game") + val amount = 14000 + val lottoInput = LottoInput(totalPrice = amount) + val randomNumberGenerator = RandomNumberGenerator(1,45) + val autoNumberGenerator = AutoNumberGenerator(randomNumberGenerator) + val generatedLottos = autoNumberGenerator.saveAfterGenerate(amount/1000) + val winning = listOf(1,6,7,8,9,10) + lottoInput.of(LottoInput(winningLotto = winning)) + val winningNumberRegister = WinningNumberRegister(randomNumberGenerator) + val winningLotto = winningNumberRegister.register(lottoInput) + val calculateWinningStatistic = CalculateWinningStatistic() + val ranks = calculateWinningStatistic.calculateRank(generatedLottos, winningLotto) + val prize = calculateWinningStatistic.calculatePrize(ranks) + val rate = calculateWinningStatistic.calculateRate(prize, lottoInput.totalPrice!!) + } } diff --git a/src/main/kotlin/domain/Lotto.kt b/src/main/kotlin/domain/Lotto.kt new file mode 100644 index 0000000000..b7545459b5 --- /dev/null +++ b/src/main/kotlin/domain/Lotto.kt @@ -0,0 +1,16 @@ +package domain + +interface Lotto { + val numbers: List + val winningCount: Int? +} + +class PurchasedLotto(override val numbers: List, override val winningCount: Int = 0) : Lotto { + fun calculateWinningCount(winningLotto: WinningLotto): Int{ + return winningLotto.numbers.filter { + this.numbers.contains(it) + }.size + } +} + +class WinningLotto(override val numbers: List, override val winningCount: Int? = null) : Lotto \ No newline at end of file diff --git a/src/main/kotlin/domain/LottoInput.kt b/src/main/kotlin/domain/LottoInput.kt index 8b22c7f53e..b68af72894 100644 --- a/src/main/kotlin/domain/LottoInput.kt +++ b/src/main/kotlin/domain/LottoInput.kt @@ -1,6 +1,13 @@ package domain -interface LottoInput { - val totalPrice: Int - val autoAmount: Int +class LottoInput( + val totalPrice: Int? = 0, + val winningLotto: List? = listOf() +) { + fun of(lottoInput: LottoInput): LottoInput { + return LottoInput( + totalPrice = lottoInput.totalPrice ?: this.totalPrice, + winningLotto = lottoInput.winningLotto ?: this.winningLotto + ) + } } \ No newline at end of file diff --git a/src/main/kotlin/domain/Rank.kt b/src/main/kotlin/domain/Rank.kt new file mode 100644 index 0000000000..637006f56f --- /dev/null +++ b/src/main/kotlin/domain/Rank.kt @@ -0,0 +1,17 @@ +package domain + +enum class Rank(val matchedCount: Int? = 0, val prize: Int = 0) { + FAIL(null, 0), + FOURTH(3, 5000), + THIRD(4, 50000), + SECOND(5, 1500000), + FIRST(6, 2000000000); + + fun isNotBoom(): Boolean = this != FAIL + + companion object { + fun find(matchedCount: Int): Rank { + return values().find { it.matchedCount == matchedCount } ?: FAIL + } + } +} diff --git a/src/main/kotlin/domain/Statistic.kt b/src/main/kotlin/domain/Statistic.kt new file mode 100644 index 0000000000..4eaf752f45 --- /dev/null +++ b/src/main/kotlin/domain/Statistic.kt @@ -0,0 +1,3 @@ +package domain + +class Statistic(val prize : Int, val rate: Double) \ No newline at end of file diff --git a/src/main/kotlin/service/AutoNumberGenerator.kt b/src/main/kotlin/service/AutoNumberGenerator.kt index 7565cc623e..e069f9f9fa 100644 --- a/src/main/kotlin/service/AutoNumberGenerator.kt +++ b/src/main/kotlin/service/AutoNumberGenerator.kt @@ -1,5 +1,15 @@ package service -class AutoNumberGenerator { +import domain.PurchasedLotto +class AutoNumberGenerator( + private val randomNumberGenerator: RandomNumberGenerator +) { + fun saveAfterGenerate(count: Int): List{ + val result = mutableListOf() + for(i:Int in 0 until count){ + result.add(PurchasedLotto(randomNumberGenerator.getGeneratedNumber())) + } + return result + } } \ No newline at end of file diff --git a/src/main/kotlin/service/CalculateWinningStatistic.kt b/src/main/kotlin/service/CalculateWinningStatistic.kt new file mode 100644 index 0000000000..1aea07bf82 --- /dev/null +++ b/src/main/kotlin/service/CalculateWinningStatistic.kt @@ -0,0 +1,16 @@ +package service + +import domain.PurchasedLotto +import domain.Rank +import domain.WinningLotto + +class CalculateWinningStatistic { + fun calculateRank(candidate: List, winningLotto: WinningLotto): List = + candidate.map { Rank.find(it.calculateWinningCount(winningLotto)) } + + fun calculatePrize(ranks: List): Int = + Rank.values().fold(0) { totalPrize, nowRank -> totalPrize + nowRank.prize } + + fun calculateRate(prize: Int, purchase: Int): Double = + prize.toDouble() / prize +} \ No newline at end of file diff --git a/src/main/kotlin/service/RandomNumberGenerator.kt b/src/main/kotlin/service/RandomNumberGenerator.kt index 991fe7ac8d..1610cf1119 100644 --- a/src/main/kotlin/service/RandomNumberGenerator.kt +++ b/src/main/kotlin/service/RandomNumberGenerator.kt @@ -4,7 +4,6 @@ class RandomNumberGenerator(override val startNumber: Int, override val endNumbe NumberGenerator { override fun getGeneratedNumber(): List = (startNumber..endNumber).map { it }.shuffled().slice(0..5) - } interface NumberGenerator { diff --git a/src/main/kotlin/service/WinningNumberRegister.kt b/src/main/kotlin/service/WinningNumberRegister.kt new file mode 100644 index 0000000000..9a70bf775e --- /dev/null +++ b/src/main/kotlin/service/WinningNumberRegister.kt @@ -0,0 +1,12 @@ +package service + +import domain.LottoInput +import domain.WinningLotto + +class WinningNumberRegister( + private val randomNumberGenerator: RandomNumberGenerator +) { + fun register(input : LottoInput): WinningLotto{ + return WinningLotto(numbers = input.winningLotto!!) + } +} \ No newline at end of file diff --git a/src/test/kotlin/AutoNumberGeneratorTest.kt b/src/test/kotlin/AutoNumberGeneratorTest.kt new file mode 100644 index 0000000000..6110eb26a5 --- /dev/null +++ b/src/test/kotlin/AutoNumberGeneratorTest.kt @@ -0,0 +1,19 @@ +import org.junit.jupiter.api.Test +import service.AutoNumberGenerator +import service.RandomNumberGenerator + +class AutoNumberGeneratorTest { + + var autoNumberGenerator: AutoNumberGenerator + var randomNumberGenerator = RandomNumberGenerator(1,45) + init { + autoNumberGenerator = AutoNumberGenerator(randomNumberGenerator) + } + @Test + fun `자동번호생성테스트`() { + val result = autoNumberGenerator.saveAfterGenerate(3) + for (i: Int in 0 until result.size) { + println(result.get(i).numbers) + } + } +} \ No newline at end of file diff --git a/src/test/kotlin/LottoTest.kt b/src/test/kotlin/LottoTest.kt new file mode 100644 index 0000000000..f2faba4410 --- /dev/null +++ b/src/test/kotlin/LottoTest.kt @@ -0,0 +1,16 @@ +import domain.PurchasedLotto +import domain.WinningLotto +import org.junit.jupiter.api.Test + +class LottoTest { + + @Test + fun `calculateWinningLottoTest`(){ + val target = listOf(1,2,3,4,5,6) + val winning = listOf(1,6,7,8,9,10) + val winningLotto = WinningLotto(numbers = winning) + val purchasedLotto = PurchasedLotto(numbers = target) + val winningCount = purchasedLotto.calculateWinningCount(winningLotto) + println(winningCount) + } +} \ No newline at end of file From 3345a9adedb996ca244f8ea61accc539640ab389 Mon Sep 17 00:00:00 2001 From: "haein.lee" Date: Mon, 2 Jan 2023 21:19:26 +0900 Subject: [PATCH 3/3] =?UTF-8?q?UI=20=EC=A0=81=EC=9A=A9=20:=20rank=20?= =?UTF-8?q?=EA=B4=80=EB=A0=A8=20output=20=EC=A0=81=EC=9A=A9=20=ED=95=84?= =?UTF-8?q?=EC=9A=94=20,=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80=20=ED=95=84=EC=9A=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/LottoController.kt | 29 +++++++++++++++++++------ src/main/kotlin/UI/InputView.kt | 21 ++++++++++++++++++ src/main/kotlin/UI/OutputView.kt | 32 ++++++++++++++++++++++++++++ src/main/kotlin/domain/LottoInput.kt | 6 ++++-- 4 files changed, 79 insertions(+), 9 deletions(-) create mode 100644 src/main/kotlin/UI/InputView.kt create mode 100644 src/main/kotlin/UI/OutputView.kt diff --git a/src/main/kotlin/LottoController.kt b/src/main/kotlin/LottoController.kt index 2a6074b6cf..85584c4c3c 100644 --- a/src/main/kotlin/LottoController.kt +++ b/src/main/kotlin/LottoController.kt @@ -1,3 +1,5 @@ +import UI.InputView +import UI.OutputView import domain.LottoInput import domain.WinningLotto import service.AutoNumberGenerator @@ -6,20 +8,33 @@ import service.RandomNumberGenerator import service.WinningNumberRegister class LottoController { - fun start(){ - val amount = 14000 - val lottoInput = LottoInput(totalPrice = amount) - val randomNumberGenerator = RandomNumberGenerator(1,45) + fun start() { + val inputView = InputView + val outputView = OutputView + + val lottoInput = inputView.purchaseInputView() + outputView.ticketCountView(lottoInput.purchaseCount!!) + + val randomNumberGenerator = RandomNumberGenerator(1, 45) val autoNumberGenerator = AutoNumberGenerator(randomNumberGenerator) - val generatedLottos = autoNumberGenerator.saveAfterGenerate(amount/1000) - val winning = listOf(1,6,7,8,9,10) + val generatedLottos = autoNumberGenerator.saveAfterGenerate(lottoInput.purchaseCount) + outputView.purchaseLottoView(generatedLottos) + + val winning = inputView.winningLottoInputView() lottoInput.of(LottoInput(winningLotto = winning)) + val winningNumberRegister = WinningNumberRegister(randomNumberGenerator) val winningLotto = winningNumberRegister.register(lottoInput) + + outputView.statisticDividerView() + val calculateWinningStatistic = CalculateWinningStatistic() val ranks = calculateWinningStatistic.calculateRank(generatedLottos, winningLotto) + //output ranks statisticPrizeView val prize = calculateWinningStatistic.calculatePrize(ranks) - val rate = calculateWinningStatistic.calculateRate(prize, lottoInput.totalPrice!!) + val rate = calculateWinningStatistic.calculateRate(prize, lottoInput.purchaseAmount!!) + outputView.statisticRateView(rate) + } } diff --git a/src/main/kotlin/UI/InputView.kt b/src/main/kotlin/UI/InputView.kt new file mode 100644 index 0000000000..1be9f2ea33 --- /dev/null +++ b/src/main/kotlin/UI/InputView.kt @@ -0,0 +1,21 @@ +package UI + +import domain.LottoInput + +object InputView { + + fun purchaseInputView(): LottoInput { + println("구입금액을 입력해 주세요.") + val inputPrice = readln().trim() + return LottoInput( + purchaseAmount = inputPrice.toInt(), + purchaseCount = inputPrice.toInt() / 1000 + ) + } + + fun winningLottoInputView(): List { + println("지난 주 당첨 번호를 입력해 주세요.") + val input = readln().trim() + return input.split(",").map { it.toInt() } + } +} \ No newline at end of file diff --git a/src/main/kotlin/UI/OutputView.kt b/src/main/kotlin/UI/OutputView.kt new file mode 100644 index 0000000000..7898efdc02 --- /dev/null +++ b/src/main/kotlin/UI/OutputView.kt @@ -0,0 +1,32 @@ +package UI + +import domain.LottoInput +import domain.PurchasedLotto +import domain.Rank + +object OutputView { + + fun ticketCountView(ticketCount: Int) { + println("${ticketCount}개를 구매했습니다.") + } + + fun purchaseLottoView(purchasedLottos: List) { + purchasedLottos.forEach{ + println(it.numbers.joinToString { "," }) + } + } + + fun statisticDividerView() { + println("당첨 통계") + println("---------") + } + + fun statisticRateView(rate: Double) { + print("총 수익률은 ${rate}입니다.") + println("(기준이 1이기 때문에 결과적으로 ${if (rate >= 1) "이익" else "손해"}라는 의미임)") + } + + fun statisticPrizeView(rank: Rank, winCount: Int) { + println("${rank.matchedCount}개 일치 (${rank.prize}원) - ${winCount}개") + } +} \ No newline at end of file diff --git a/src/main/kotlin/domain/LottoInput.kt b/src/main/kotlin/domain/LottoInput.kt index b68af72894..7910b08e00 100644 --- a/src/main/kotlin/domain/LottoInput.kt +++ b/src/main/kotlin/domain/LottoInput.kt @@ -1,12 +1,14 @@ package domain class LottoInput( - val totalPrice: Int? = 0, + val purchaseCount: Int? = 0, + val purchaseAmount: Int? = 0, val winningLotto: List? = listOf() ) { fun of(lottoInput: LottoInput): LottoInput { return LottoInput( - totalPrice = lottoInput.totalPrice ?: this.totalPrice, + purchaseCount = lottoInput.purchaseCount ?: this.purchaseCount, + purchaseAmount = lottoInput.purchaseAmount ?: this.purchaseAmount, winningLotto = lottoInput.winningLotto ?: this.winningLotto ) }