diff --git a/README.md b/README.md index cbae739405..d3e4720a10 100644 --- a/README.md +++ b/README.md @@ -1 +1,24 @@ -# kotlin-lotto \ No newline at end of file +# kotlin-lotto + +## 자동 만들기 TODO +### 1. Input View + - [ ] 구입금액 입력받기 + - [ ] 당첨번호 입력받기 + +### 2. Output View + - [ ] 생성된 로또번호 출력하기 + - [ ] 당첨통계 출력하기 + +### 3. Service + - [X] 자동번호 등록하기 + - [X] 랜덤번호 생성하기 + - [X] 당첨번호 등록하기 + - [x] 통계내기 - 당첨 개수 확인하기 + - [ ] 통계내기 - 등수계산하기 + - [ ] 통계내기 - 수익률계산하기 + +### 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..85584c4c3c --- /dev/null +++ b/src/main/kotlin/LottoController.kt @@ -0,0 +1,44 @@ +import UI.InputView +import UI.OutputView +import domain.LottoInput +import domain.WinningLotto +import service.AutoNumberGenerator +import service.CalculateWinningStatistic +import service.RandomNumberGenerator +import service.WinningNumberRegister + +class LottoController { + 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(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.purchaseAmount!!) + outputView.statisticRateView(rate) + + + } +} + +fun main() { + LottoController().start() +} \ No newline at end of file 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/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 new file mode 100644 index 0000000000..7910b08e00 --- /dev/null +++ b/src/main/kotlin/domain/LottoInput.kt @@ -0,0 +1,15 @@ +package domain + +class LottoInput( + val purchaseCount: Int? = 0, + val purchaseAmount: Int? = 0, + val winningLotto: List? = listOf() +) { + fun of(lottoInput: LottoInput): LottoInput { + return LottoInput( + purchaseCount = lottoInput.purchaseCount ?: this.purchaseCount, + purchaseAmount = lottoInput.purchaseAmount ?: this.purchaseAmount, + 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 new file mode 100644 index 0000000000..e069f9f9fa --- /dev/null +++ b/src/main/kotlin/service/AutoNumberGenerator.kt @@ -0,0 +1,15 @@ +package service + +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 new file mode 100644 index 0000000000..1610cf1119 --- /dev/null +++ b/src/main/kotlin/service/RandomNumberGenerator.kt @@ -0,0 +1,13 @@ +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/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 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