diff --git a/README.md b/README.md index 6b715089ef..9d9b421310 100644 --- a/README.md +++ b/README.md @@ -41,7 +41,7 @@ 당첨 로또 - [O] 당첨 번호 일치 개수 반환 - [O] 보너스 번호 일치 검증 -- [] 당첨번호 & 보너스 번호 중복 +- [O] 당첨번호 & 보너스 번호 중복 로또 계산기 - [O] 수익률 반환 @@ -62,6 +62,6 @@ inputView (object) - [O] 입력된 금액 포맷 검증 - [O] 숫자 포맷 검증 - [O] 금액 범위는 1,000원 이상 100,000원 이하의 정수 -- [] 지난 당첨 번호 포맷 검증 +- [O] 지난 당첨 번호 포맷 검증 - [O] 콤마(,)를 기준으로 나눈다 - [O] 숫자 포맷 검증 diff --git a/src/main/kotlin/lotto/LottoMain.kt b/src/main/kotlin/lotto/LottoMain.kt index 116cc60abb..7d00e090f6 100644 --- a/src/main/kotlin/lotto/LottoMain.kt +++ b/src/main/kotlin/lotto/LottoMain.kt @@ -10,16 +10,21 @@ fun main() { val cash = InputView.inputCash() val lottoGame = LottoGame(RandomLogic()) - val purchaseLottoList = lottoGame.buyLotto(cash.toInt()) + val gameTimes = lottoGame.getGameTimes(cash) + val manualGameTimes = InputView.inputManualCnt(gameTimes) - OutputView.showLottoList(purchaseLottoList) + val numberCombinationList = InputView.inputNumberCombination(manualGameTimes) - val winningNumberList = InputView.inputWinningNumber() + val purchaseLottoList = lottoGame.buyLotto(gameTimes, numberCombinationList) + + OutputView.showLottoList(purchaseLottoList, manualGameTimes) + + val winningNumberCombination = InputView.inputWinningNumber() val bonusNumber = InputView.inputBonusNumber() - val winningLotto = LottoMachine.createWinningLotto(winningNumberList, bonusNumber) + val winningLotto = LottoMachine.createWinningLotto(winningNumberCombination, bonusNumber) val winningStatus = lottoGame.getWinningStats(winningLotto, purchaseLottoList) - val winningRate = LottoMachine.createWinningRate(cash.toInt(), winningStatus) + val winningRate = LottoMachine.createWinningRate(cash, winningStatus) OutputView.showWinningStatus(winningStatus, winningRate) } diff --git a/src/main/kotlin/lotto/data/LottoNumber.kt b/src/main/kotlin/lotto/data/LottoNumber.kt index a8f743b6f0..7f323f5751 100644 --- a/src/main/kotlin/lotto/data/LottoNumber.kt +++ b/src/main/kotlin/lotto/data/LottoNumber.kt @@ -19,8 +19,8 @@ class LottoNumber private constructor(private val number: Int) : Comparable = (MIN_NUMBER..MAX_NUMBER).associateWith(::LottoNumber) - fun createLottoNumbers(numbers: List): Set { - return LinkedHashSet(numbers.map(::from)) + fun createLottoNumbers(numberCombination: NumberCombination): Set { + return LinkedHashSet(numberCombination.numberCombination.map(::from)) } fun createRandomLottoNumber(lottoCreation: RandomLogicInterface): Set { diff --git a/src/main/kotlin/lotto/data/LottoRanking.kt b/src/main/kotlin/lotto/data/LottoRanking.kt index 33f7351c12..d8c4831594 100644 --- a/src/main/kotlin/lotto/data/LottoRanking.kt +++ b/src/main/kotlin/lotto/data/LottoRanking.kt @@ -14,18 +14,10 @@ enum class LottoRanking(val matchingNumberCnt: Int, val price: Int) : Prize { companion object { fun findLottoRanking(matchingNumberCnt: Int, hasBonusNumber: Boolean): LottoRanking { - return if (matchingNumberCnt == SecondPlace.matchingNumberCnt) { - determineRankingWithBonusNumber(hasBonusNumber) - } else { - LottoRanking.values().find { it.matchingNumberCnt == matchingNumberCnt } ?: None - } - } - - private fun determineRankingWithBonusNumber(isContainBonusNumber: Boolean): LottoRanking { - return if (isContainBonusNumber) { + return if (matchingNumberCnt == SecondPlace.matchingNumberCnt && hasBonusNumber) { SecondPlace } else { - ThirdPlace + LottoRanking.values().find { it.matchingNumberCnt == matchingNumberCnt } ?: None } } } diff --git a/src/main/kotlin/lotto/data/NumberCombination.kt b/src/main/kotlin/lotto/data/NumberCombination.kt new file mode 100644 index 0000000000..1f6e783c81 --- /dev/null +++ b/src/main/kotlin/lotto/data/NumberCombination.kt @@ -0,0 +1,3 @@ +package lotto.data + +class NumberCombination(val numberCombination: List) diff --git a/src/main/kotlin/lotto/data/WinningLotto.kt b/src/main/kotlin/lotto/data/WinningLotto.kt index 4faded4b37..97f8b140ef 100644 --- a/src/main/kotlin/lotto/data/WinningLotto.kt +++ b/src/main/kotlin/lotto/data/WinningLotto.kt @@ -7,7 +7,7 @@ data class WinningLotto(val lotto: Lotto, val bonusNumber: LottoNumber) { } fun countMatchingNumbers(lotto: Lotto): Int { - return this.lotto.selectNumbers.intersect(lotto.selectNumbers).size + return this.lotto.matching(lotto) } fun hasBonusNumber(lotto: Lotto): Boolean { @@ -19,6 +19,14 @@ data class WinningLotto(val lotto: Lotto, val bonusNumber: LottoNumber) { } private fun validateDuplicationBonusNumber() { - require(!lotto.selectNumbers.contains(bonusNumber)) { "당첨 번호 구성과 보너스 번호가 중복됩니다." } + require(!lotto.selectNumbers.contains(bonusNumber)) { ERR_MSG_DUPLICATION_BONUS_NUMBER } + } + + private fun Lotto.matching(lotto: Lotto): Int { + return this.selectNumbers.intersect(lotto.selectNumbers).size + } + + companion object { + private const val ERR_MSG_DUPLICATION_BONUS_NUMBER = "당첨 번호 구성과 보너스 번호가 중복됩니다." } } diff --git a/src/main/kotlin/lotto/domain/LottoCalculator.kt b/src/main/kotlin/lotto/domain/LottoCalculator.kt index 2215a099fd..171812c593 100644 --- a/src/main/kotlin/lotto/domain/LottoCalculator.kt +++ b/src/main/kotlin/lotto/domain/LottoCalculator.kt @@ -1,18 +1,24 @@ package lotto.domain import lotto.data.LottoRanking +import java.util.EnumMap object LottoCalculator { - private const val GAME_COST = 1000 + private const val ERR_MSG_OVER_MANUAL_GAME_TIMES = "수동 게임 횟수가 총 게임 횟수를 초과하였습니다." - fun calculateWinningRate(cash: Int, winningStatus: Map): Float { + fun calculateWinningRate(cash: Int, winningStatus: EnumMap): Float { val totalPrice = winningStatus.toList().sumOf { it.first.findPrize(it) } return totalPrice / cash.toFloat() } - fun getTimes(cash: Int): Int { - return cash / GAME_COST + fun getTimes(cash: Int, gameCost: Int): Int { + return cash / gameCost + } + + fun getAutoTimes(totalTimes: Int, manualGameTimes: Int): Int { + require(totalTimes >= manualGameTimes) { ERR_MSG_OVER_MANUAL_GAME_TIMES } + return totalTimes - manualGameTimes } } diff --git a/src/main/kotlin/lotto/domain/LottoMachine.kt b/src/main/kotlin/lotto/domain/LottoMachine.kt index 7fd4f83370..e59731974c 100644 --- a/src/main/kotlin/lotto/domain/LottoMachine.kt +++ b/src/main/kotlin/lotto/domain/LottoMachine.kt @@ -3,7 +3,9 @@ package lotto.domain import lotto.data.Lotto import lotto.data.LottoNumber import lotto.data.LottoRanking +import lotto.data.NumberCombination import lotto.data.WinningLotto +import java.util.EnumMap object LottoMachine { @@ -11,8 +13,8 @@ object LottoMachine { return Lotto(lottoNumbers) } - fun createWinningLotto(winningNumbers: List, bonusNumber: Int): WinningLotto { - val winningLotto = LottoNumber.createLottoNumbers(winningNumbers) + fun createWinningLotto(winningNumberCombination: NumberCombination, bonusNumber: Int): WinningLotto { + val winningLotto = LottoNumber.createLottoNumbers(winningNumberCombination) val bonusLottoNumber = LottoNumber.from(bonusNumber) return WinningLotto(Lotto(winningLotto), bonusLottoNumber) } @@ -24,7 +26,7 @@ object LottoMachine { return LottoRanking.findLottoRanking(matchingNumberCnt, hasBonusNumber) } - fun createWinningRate(cash: Int, winningStatus: Map): Float { + fun createWinningRate(cash: Int, winningStatus: EnumMap): Float { return LottoCalculator.calculateWinningRate(cash, winningStatus) } } diff --git a/src/main/kotlin/lotto/domain/WinningDomain.kt b/src/main/kotlin/lotto/domain/WinningDomain.kt index 61d2270c27..08040befde 100644 --- a/src/main/kotlin/lotto/domain/WinningDomain.kt +++ b/src/main/kotlin/lotto/domain/WinningDomain.kt @@ -3,12 +3,13 @@ package lotto.domain import lotto.data.Lotto import lotto.data.LottoRanking import lotto.data.WinningLotto +import java.util.EnumMap object WinningDomain { // 보너스 번호 추가. - fun checkWinningResult(winningLotto: WinningLotto, purchaseLottoList: List): Map { - val winningStatusMap = mutableMapOf() + fun checkWinningResult(winningLotto: WinningLotto, purchaseLottoList: List): EnumMap { + val winningStatusMap: EnumMap = EnumMap(LottoRanking::class.java) purchaseLottoList.forEach { val lottoRanking = LottoMachine.checkLotto(it, winningLotto) diff --git a/src/main/kotlin/lotto/service/LottoGame.kt b/src/main/kotlin/lotto/service/LottoGame.kt index 547d5d8d3d..0b03bec324 100644 --- a/src/main/kotlin/lotto/service/LottoGame.kt +++ b/src/main/kotlin/lotto/service/LottoGame.kt @@ -3,18 +3,32 @@ package lotto.service import lotto.data.Lotto import lotto.data.LottoNumber import lotto.data.LottoRanking +import lotto.data.NumberCombination import lotto.data.WinningLotto import lotto.domain.LottoCalculator import lotto.domain.LottoMachine import lotto.domain.RandomLogicInterface import lotto.domain.WinningDomain +import java.util.EnumMap -class LottoGame(private val randomLogic: RandomLogicInterface) { +class LottoGame(private val randomLogic: RandomLogicInterface, private val gameCost: Int = DEFAULT_COST) { - fun buyLotto(cash: Int): List { - val times = LottoCalculator.getTimes(cash) + fun getGameTimes(cash: Int): Int { + return LottoCalculator.getTimes(cash, gameCost) + } + + fun buyLotto(gameTimes: Int, numberCombinationList: List = emptyList()): List { + val autoTimes = LottoCalculator.getAutoTimes(gameTimes, numberCombinationList.size) + val lottoList = mutableListOf() + + lottoList.addAll(numberCombinationList.map { createManualLotto(it) }) + lottoList.addAll(createLotto(autoTimes)) + + return lottoList + } - return createLotto(times) + fun getWinningStats(winningLotto: WinningLotto, purchaseLottoList: List): EnumMap { + return WinningDomain.checkWinningResult(winningLotto, purchaseLottoList) } private fun createLotto(times: Int): List { @@ -26,8 +40,12 @@ class LottoGame(private val randomLogic: RandomLogicInterface) { return lottoList } - fun getWinningStats(winningLotto: WinningLotto, purchaseLottoList: List): Map { + private fun createManualLotto(numberCombination: NumberCombination): Lotto { + val lottoNumberCombination = LottoNumber.createLottoNumbers(numberCombination) + return LottoMachine.createSelectLotto(lottoNumberCombination) + } - return WinningDomain.checkWinningResult(winningLotto, purchaseLottoList) + companion object { + private const val DEFAULT_COST = 1000 } } diff --git a/src/main/kotlin/lotto/view/InputView.kt b/src/main/kotlin/lotto/view/InputView.kt index b55efb10ad..7d866bc035 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.data.NumberCombination + object InputView { private const val ERR_MSG_INVALID_NUMERIC_FORMAT = "입력된 값의 포맷이 숫자가 압니다." @@ -7,18 +9,37 @@ object InputView { private const val MIN_GAME_COST = 1000 private const val MAX_GAME_COST = 100000 - fun inputCash(): String { + fun inputCash(): Int { println("구입금액을 입력해 주세요.") val inputData = readln() validateCash(inputData) - return inputData + return inputData.toInt() + } + + fun inputManualCnt(gameTimes: Int): Int { + println("수동으로 구매할 로또 수를 입력해 주세요.") + val inputData = readln() + validateBuyManual(inputData, gameTimes) + return inputData.toInt() } - fun inputWinningNumber(): List { + fun inputNumberCombination(manualGameTimes: Int): List { + val numberCombinationList = mutableListOf() + + println("수동으로 구매할 번호를 입력해 주세요.") + repeat(manualGameTimes) { + val inputData = readln() + numberCombinationList.add(NumberCombination(splitInputData(inputData))) + } + println() + return numberCombinationList.toList() + } + + fun inputWinningNumber(): NumberCombination { println("지난 주 당첨 번호를 입력해 주세요.") val inputData = readln() println() - return splitInputData(inputData) + return NumberCombination(splitInputData(inputData)) } fun inputBonusNumber(): Int { @@ -33,6 +54,15 @@ object InputView { validateNumberRange(inputData.toInt()) } + fun validateBuyManual(inputData: String, gameTimes: Int) { + validateNumericFormat(inputData) + validateManualTimes(inputData.toInt(), gameTimes) + } + + private fun validateManualTimes(inputData: Int, gameTimes: Int) { + require(gameTimes >= inputData) { "수동 게임 횟수가 총 게임 횟수를 초과하였습니다." } + } + fun splitInputData(inputData: String): List { val splitData = inputData.split(",").map { it.trim() } validateWinningNumber(splitData) diff --git a/src/main/kotlin/lotto/view/OutputView.kt b/src/main/kotlin/lotto/view/OutputView.kt index 797678e7ad..5d7237f161 100644 --- a/src/main/kotlin/lotto/view/OutputView.kt +++ b/src/main/kotlin/lotto/view/OutputView.kt @@ -2,6 +2,7 @@ package lotto.view import lotto.data.Lotto import lotto.data.LottoRanking +import lotto.domain.LottoCalculator object OutputView { @@ -10,7 +11,10 @@ object OutputView { private const val TXT_LOSS_COMMENT = "기준이 1이기 때문에 결과적으로 손해라는 의미임" private const val DIVIDING_LINE = "---------" - fun showLottoList(lottoList: List) { + fun showLottoList(lottoList: List, manualGameTimes: Int) { + val autoGameTimes = LottoCalculator.getAutoTimes(lottoList.size, manualGameTimes) + + println("수동으로 ${manualGameTimes}장, 자동으로 ${autoGameTimes}장 구매했습니다.") lottoList.forEach { println(it.selectNumbers) } diff --git a/src/test/kotlin/lotto/LottoGameTest.kt b/src/test/kotlin/lotto/LottoGameTest.kt index 3bf369b7f6..5535aec4fd 100644 --- a/src/test/kotlin/lotto/LottoGameTest.kt +++ b/src/test/kotlin/lotto/LottoGameTest.kt @@ -3,6 +3,7 @@ package lotto import lotto.data.Lotto import lotto.data.LottoNumber import lotto.data.LottoRanking +import lotto.data.NumberCombination import lotto.domain.LottoMachine import lotto.domain.RandomLogic import lotto.service.LottoGame @@ -15,9 +16,10 @@ class LottoGameTest { // given : 금액을 입력 받는다. val cash = 4500 val lottoGame = LottoGame(RandomLogic()) + val gameTimes = lottoGame.getGameTimes(cash) // when : 로또를 구매한다. - val lottoList = lottoGame.buyLotto(cash) + val lottoList = lottoGame.buyLotto(gameTimes) // then : 입력 받은 금액의 최대 수량의 로또를 구매한다. assertThat(lottoList.size).isEqualTo(4) @@ -27,16 +29,16 @@ class LottoGameTest { fun `로또 구매와 당첨 번호를 입력 했다면, 당첨을 확인을 요청할 때, 당첨 통계를 반환한다`() { // given : 로또 구매와 당첨 번호를 입력한다. // 2등 - 2개, 3등 - 1개, 4등 - 1개 - val winningNumberList = listOf(1, 2, 3, 4, 5, 6) + val winningNumberCombination = NumberCombination(listOf(1, 2, 3, 4, 5, 6)) val bonusNumber = 7 - val winningLotto = LottoMachine.createWinningLotto(winningNumberList, bonusNumber) + val winningLotto = LottoMachine.createWinningLotto(winningNumberCombination, bonusNumber) - val purchaseLottoNumbers1 = LottoNumber.createLottoNumbers(listOf(1, 2, 3, 4, 5, 7)) - val purchaseLottoNumbers2 = LottoNumber.createLottoNumbers(listOf(1, 2, 3, 4, 5, 7)) - val purchaseLottoNumbers3 = LottoNumber.createLottoNumbers(listOf(1, 2, 3, 4, 7, 8)) - val purchaseLottoNumbers4 = LottoNumber.createLottoNumbers(listOf(1, 2, 3, 7, 8, 9)) - val purchaseLottoNumbers5 = LottoNumber.createLottoNumbers(listOf(1, 2, 6, 7, 8, 9)) - val purchaseLottoNumbers6 = LottoNumber.createLottoNumbers(listOf(11, 12, 13, 14, 15, 16)) + val purchaseLottoNumbers1 = LottoNumber.createLottoNumbers(NumberCombination(listOf(1, 2, 3, 4, 5, 7))) + val purchaseLottoNumbers2 = LottoNumber.createLottoNumbers(NumberCombination(listOf(1, 2, 3, 4, 5, 7))) + val purchaseLottoNumbers3 = LottoNumber.createLottoNumbers(NumberCombination(listOf(1, 2, 3, 4, 7, 8))) + val purchaseLottoNumbers4 = LottoNumber.createLottoNumbers(NumberCombination(listOf(1, 2, 3, 7, 8, 9))) + val purchaseLottoNumbers5 = LottoNumber.createLottoNumbers(NumberCombination(listOf(1, 2, 6, 7, 8, 9))) + val purchaseLottoNumbers6 = LottoNumber.createLottoNumbers(NumberCombination(listOf(11, 12, 13, 14, 15, 16))) val purchaseLotto1 = Lotto((purchaseLottoNumbers1)) val purchaseLotto2 = Lotto((purchaseLottoNumbers2)) diff --git a/src/test/kotlin/lotto/data/LottoNumberTest.kt b/src/test/kotlin/lotto/data/LottoNumberTest.kt index 0bbdc7595e..9448fe65f9 100644 --- a/src/test/kotlin/lotto/data/LottoNumberTest.kt +++ b/src/test/kotlin/lotto/data/LottoNumberTest.kt @@ -9,11 +9,13 @@ class LottoNumberTest { // given : // when : 번호 조합을 입력 받았을 때 - val selectNumberList = listOf(1, 2, 3, 4, 5, 6) + val numberCombination = NumberCombination(listOf(1, 2, 3, 4, 5, 6)) // then : 로또 번호 조합을 생성할 수 있다. - val lottoNumbers = LottoNumber.createLottoNumbers(selectNumberList) - val expect = LottoNumber.createLottoNumbers(listOf(1, 2, 3, 4, 5, 6)) + val lottoNumbers = LottoNumber.createLottoNumbers(numberCombination) + + val expectNumberCombination = NumberCombination(listOf(1, 2, 3, 4, 5, 6)) + val expect = LottoNumber.createLottoNumbers(expectNumberCombination) assertThat(lottoNumbers).isEqualTo(expect) } @@ -21,10 +23,10 @@ class LottoNumberTest { @Test fun `1 ~ 45 범위를 넘어가는 값을 받았다면, 로또를 생성했을 때, 예외를 던진다`() { // given : 범위를 벗어나는 값을 포함하여 번호를 구성한다. - val selectNumberList = listOf(1, 2, 3, 4, 5, 450) + val numberCombination = NumberCombination(listOf(1, 2, 3, 4, 5, 450)) // when : 로또번호를 구성한다. - val actual = runCatching { LottoNumber.createLottoNumbers(selectNumberList) }.exceptionOrNull() + val actual = runCatching { LottoNumber.createLottoNumbers(numberCombination) }.exceptionOrNull() // then : 예외를 던진다. assertThat(actual).isInstanceOf(IllegalArgumentException()::class.java) diff --git a/src/test/kotlin/lotto/data/LottoTest.kt b/src/test/kotlin/lotto/data/LottoTest.kt index a1ab83dfd3..27831e7cdb 100644 --- a/src/test/kotlin/lotto/data/LottoTest.kt +++ b/src/test/kotlin/lotto/data/LottoTest.kt @@ -15,10 +15,10 @@ class LottoTest { @Test fun `6개로 구성되지 않는 번호 구성을 받았다면, 로또를 생성했을 때, 예외를 던진다`() { // given : 6개로 구성되지 않은 번호 구성을 받는다. - val selectNumberList = listOf(1, 2, 3, 4, 5, 6, 7) + val selectNumberCombination = NumberCombination(listOf(1, 2, 3, 4, 5, 6, 7)) // when : 로또번호를 구성한다. - val actual = runCatching { Lotto(LottoNumber.createLottoNumbers(selectNumberList)) }.exceptionOrNull() + val actual = runCatching { Lotto(LottoNumber.createLottoNumbers(selectNumberCombination)) }.exceptionOrNull() // then : 예외를 던진다. Assertions.assertThat(actual).isInstanceOf(IllegalArgumentException()::class.java) @@ -27,10 +27,10 @@ class LottoTest { @Test fun `중복값을 포함한 6개의 번호 구성을 받았다면, 로또를 생성했을 때, 예외를 던진다`() { // given : 중복값을 포함한 6개의 번호 구성을 받는다. - val selectNumberList = listOf(1, 2, 3, 4, 6, 6) + val selectNumberCombination = NumberCombination(listOf(1, 2, 3, 4, 6, 6)) // when : 로또번호를 구성한다. - val actual = runCatching { Lotto(LottoNumber.createLottoNumbers(selectNumberList)) }.exceptionOrNull() + val actual = runCatching { Lotto(LottoNumber.createLottoNumbers(selectNumberCombination)) }.exceptionOrNull() // then : 예외를 던진다. Assertions.assertThat(actual).isInstanceOf(IllegalArgumentException()::class.java) diff --git a/src/test/kotlin/lotto/data/NumberCombinationTest.kt b/src/test/kotlin/lotto/data/NumberCombinationTest.kt new file mode 100644 index 0000000000..637af10c4f --- /dev/null +++ b/src/test/kotlin/lotto/data/NumberCombinationTest.kt @@ -0,0 +1,3 @@ +package lotto.data + +class NumberCombinationTest diff --git a/src/test/kotlin/lotto/data/WinningLottoTest.kt b/src/test/kotlin/lotto/data/WinningLottoTest.kt index 8f9d632ec6..a3a231ca57 100644 --- a/src/test/kotlin/lotto/data/WinningLottoTest.kt +++ b/src/test/kotlin/lotto/data/WinningLottoTest.kt @@ -8,11 +8,11 @@ class WinningLottoTest { @Test fun `당첨 번호와 일치하는 번호 수 반환`() { // given : 당첨로또와 로또를 받는다. - val lotto1 = Lotto(LottoNumber.createLottoNumbers(listOf(1, 2, 3, 4, 5, 6))) - val lotto2 = Lotto(LottoNumber.createLottoNumbers(listOf(1, 2, 3, 4, 5, 7))) + val lotto1 = Lotto(LottoNumber.createLottoNumbers(NumberCombination(listOf(1, 2, 3, 4, 5, 6)))) + val lotto2 = Lotto(LottoNumber.createLottoNumbers(NumberCombination(listOf(1, 2, 3, 4, 5, 7)))) // 당첨번호 [1,2,3,4,5,7] + 보너스 번호 [6] - val wLotto1 = Lotto(LottoNumber.createLottoNumbers(listOf(1, 2, 3, 4, 5, 7))) + val wLotto1 = Lotto(LottoNumber.createLottoNumbers(NumberCombination(listOf(1, 2, 3, 4, 5, 7)))) val winningLotto = WinningLotto(wLotto1, LottoNumber.from(6)) // when : 검증 로직을 실행한다. @@ -27,11 +27,11 @@ class WinningLottoTest { @Test fun `보너스 번호 일치 검증`() { // given : 당첨로또와 로또를 받는다. - val lotto1 = Lotto(LottoNumber.createLottoNumbers(listOf(1, 2, 3, 4, 5, 6))) - val lotto2 = Lotto(LottoNumber.createLottoNumbers(listOf(1, 2, 3, 4, 5, 7))) + val lotto1 = Lotto(LottoNumber.createLottoNumbers(NumberCombination(listOf(1, 2, 3, 4, 5, 6)))) + val lotto2 = Lotto(LottoNumber.createLottoNumbers(NumberCombination(listOf(1, 2, 3, 4, 5, 7)))) // 당첨번호 [1,2,3,4,5,7] + 보너스 번호 [6] - val wLotto1 = Lotto(LottoNumber.createLottoNumbers(listOf(1, 2, 3, 4, 5, 7))) + val wLotto1 = Lotto(LottoNumber.createLottoNumbers(NumberCombination(listOf(1, 2, 3, 4, 5, 7)))) val winningLotto = WinningLotto(wLotto1, LottoNumber.from(6)) // when : 보너스 번호 일치 검증 로직을 호출한다. @@ -46,7 +46,7 @@ class WinningLottoTest { @Test fun `보너스 번호 중복 검증`() { // given : 당첨 번호로 구성된 로또와 이와 중복되는 보너스 번호를 받는다. - val lotto = Lotto(LottoNumber.createLottoNumbers(listOf(1, 2, 3, 4, 5, 6))) + val lotto = Lotto(LottoNumber.createLottoNumbers(NumberCombination(listOf(1, 2, 3, 4, 5, 6)))) val bonusLottoNumber = LottoNumber.from(6) // when : diff --git a/src/test/kotlin/lotto/domain/LottoCalculatorTest.kt b/src/test/kotlin/lotto/domain/LottoCalculatorTest.kt index e743b4ffbe..a81f2c2ed6 100644 --- a/src/test/kotlin/lotto/domain/LottoCalculatorTest.kt +++ b/src/test/kotlin/lotto/domain/LottoCalculatorTest.kt @@ -3,6 +3,7 @@ package lotto.domain import lotto.data.LottoRanking import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.Test +import java.util.EnumMap class LottoCalculatorTest { @Test @@ -10,7 +11,7 @@ class LottoCalculatorTest { // given : 구매한 로또의 통계와 구매 금액을 받는다. // 총 당첨금 5천원 val cash = 100000 - val winningStatus = mutableMapOf() + val winningStatus: EnumMap = EnumMap(LottoRanking::class.java) winningStatus[LottoRanking.FifthPlace] = 1 // when : 구매 금액 대비 당첨 금액에 대한 수익률은 요청한다. diff --git a/src/test/kotlin/lotto/domain/LottoMachineTest.kt b/src/test/kotlin/lotto/domain/LottoMachineTest.kt index 9fb0d43dbd..c5f0942a5a 100644 --- a/src/test/kotlin/lotto/domain/LottoMachineTest.kt +++ b/src/test/kotlin/lotto/domain/LottoMachineTest.kt @@ -3,17 +3,19 @@ package lotto.domain import lotto.data.Lotto import lotto.data.LottoNumber import lotto.data.LottoRanking +import lotto.data.NumberCombination import lotto.data.WinningLotto import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.Test +import java.util.EnumMap class LottoMachineTest { @Test fun `번호 리스트를 받았다면, 로또를 생성 요청을 했을 때, 해당 번호로 구성된 로또를 생성한다`() { // given : 번호를 리스트를 받는다. - val numberList = listOf(1, 2, 3, 4, 5, 6) - val lottoNumbers = LottoNumber.createLottoNumbers(numberList) + val numberCombination = NumberCombination(listOf(1, 2, 3, 4, 5, 6)) + val lottoNumbers = LottoNumber.createLottoNumbers(numberCombination) // when : 로또를 생성한다. val actual = LottoMachine.createSelectLotto(lottoNumbers) @@ -26,12 +28,12 @@ class LottoMachineTest { @Test fun `구매 로또와 당첨 로또를 받았다면, 당첨 확인을 요청했을 때, 당첨 등수를 반환한다`() { // given : 구매 로또 리스트와 추첨된 로또를 받는다. - val winningNumberList = listOf(1, 2, 3, 4, 5, 6) + val numberCombination = NumberCombination(listOf(1, 2, 3, 4, 5, 6)) val bonusLottoNumber = LottoNumber.from(7) - val winningLotto = WinningLotto(Lotto(LottoNumber.createLottoNumbers(winningNumberList)), bonusLottoNumber) + val winningLotto = WinningLotto(Lotto(LottoNumber.createLottoNumbers(numberCombination)), bonusLottoNumber) - val purchaseNumberList = listOf(1, 2, 3, 4, 5, 8) - val purchaseLotto = Lotto(LottoNumber.createLottoNumbers(purchaseNumberList)) + val purchaseNumberCombination = NumberCombination(listOf(1, 2, 3, 4, 5, 8)) + val purchaseLotto = Lotto(LottoNumber.createLottoNumbers(purchaseNumberCombination)) // when : 당첨 여부를 확인을 요청한다. val actual = LottoMachine.checkLotto(purchaseLotto, winningLotto) @@ -43,12 +45,12 @@ class LottoMachineTest { @Test fun ` 2등 당첨 시나리오`() { // given : 보너스 번호를 포함해 일치하는 숫자가 6개인 구성의 로또를 받는다. - val winningNumberList = listOf(1, 2, 3, 4, 5, 6) + val numberCombination = NumberCombination(listOf(1, 2, 3, 4, 5, 6)) val bonusLottoNumber = LottoNumber.from(7) - val winningLotto = WinningLotto(Lotto(LottoNumber.createLottoNumbers(winningNumberList)), bonusLottoNumber) + val winningLotto = WinningLotto(Lotto(LottoNumber.createLottoNumbers(numberCombination)), bonusLottoNumber) - val purchaseNumberList = listOf(1, 2, 3, 4, 5, 7) - val purchaseLotto = Lotto(LottoNumber.createLottoNumbers(purchaseNumberList)) + val purchaseNumberCombination = NumberCombination(listOf(1, 2, 3, 4, 5, 7)) + val purchaseLotto = Lotto(LottoNumber.createLottoNumbers(purchaseNumberCombination)) // when : 당첨 확인을 요청한다 val actual = LottoMachine.checkLotto(purchaseLotto, winningLotto) @@ -61,12 +63,11 @@ class LottoMachineTest { fun `구매 금액과 당첨 통계를 받았고, 수익률을 요청한다면, 수익률을 반환한다`() { // given : 구매 금액과 당첨 통계를 받는다. val cash = 6000 - val winningStatus = mutableMapOf( - LottoRanking.ThirdPlace to 2, - LottoRanking.FourthPlace to 1, - LottoRanking.FifthPlace to 2, - LottoRanking.None to 1 - ) + val winningStatus: EnumMap = EnumMap(LottoRanking::class.java) + winningStatus[LottoRanking.ThirdPlace] = 2 + winningStatus[LottoRanking.FourthPlace] = 1 + winningStatus[LottoRanking.FifthPlace] = 2 + winningStatus[LottoRanking.None] = 1 // when : 수익률을 요청한다. val winningRate = LottoMachine.createWinningRate(cash, winningStatus) @@ -78,12 +79,12 @@ class LottoMachineTest { @Test fun `당첨 로또와 보너스 번호를 받고, 당첨 로또 번호 생성을 요청할때, 당첨 로또번호가 반환된다`() { // given : 1등 당첨 번호, 보너스 번호 - val numberList = listOf(1, 2, 3, 4, 5, 6) - val lottoNumber = LottoNumber.createLottoNumbers(numberList) + val numberCombination = NumberCombination(listOf(1, 2, 3, 4, 5, 6)) + val lottoNumber = LottoNumber.createLottoNumbers(numberCombination) val bonusNumber = 8 // when : 당첨 번호 - val actual = LottoMachine.createWinningLotto(numberList, bonusNumber) + val actual = LottoMachine.createWinningLotto(numberCombination, bonusNumber) // then : 당첨 로또 생성. val lotto = LottoMachine.createSelectLotto(lottoNumber)