From 437ff8e250838549dcec806efa655165bbdaff31 Mon Sep 17 00:00:00 2001 From: stopmin Date: Wed, 21 Feb 2024 23:26:43 +0900 Subject: [PATCH 1/7] =?UTF-8?q?1=EC=B0=A8=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 + .../src/main/kotlin/lotto/Application.kt | 3 +- kotlin-lotto/src/main/kotlin/lotto/Lotto.kt | 6 +- .../src/main/kotlin/lotto/LottoGame.kt | 95 +++++++++++++++++++ .../src/main/kotlin/lotto/LottoStore.kt | 76 +++++++++++++++ .../src/main/kotlin/lotto/common/ErrorCode.kt | 4 + .../kotlin/lotto/common/ValidationUtils.kt | 8 ++ 7 files changed, 190 insertions(+), 4 deletions(-) create mode 100644 .gitignore create mode 100644 kotlin-lotto/src/main/kotlin/lotto/LottoGame.kt create mode 100644 kotlin-lotto/src/main/kotlin/lotto/LottoStore.kt create mode 100644 kotlin-lotto/src/main/kotlin/lotto/common/ErrorCode.kt create mode 100644 kotlin-lotto/src/main/kotlin/lotto/common/ValidationUtils.kt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3f096ef --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ + +.idea/workspace.xml diff --git a/kotlin-lotto/src/main/kotlin/lotto/Application.kt b/kotlin-lotto/src/main/kotlin/lotto/Application.kt index d716876..1b483a3 100644 --- a/kotlin-lotto/src/main/kotlin/lotto/Application.kt +++ b/kotlin-lotto/src/main/kotlin/lotto/Application.kt @@ -1,5 +1,6 @@ package lotto fun main() { - TODO("프로그램 구현") + val lottoGame = LottoGame() + lottoGame.run() } diff --git a/kotlin-lotto/src/main/kotlin/lotto/Lotto.kt b/kotlin-lotto/src/main/kotlin/lotto/Lotto.kt index 5ca00b4..f83cdc7 100644 --- a/kotlin-lotto/src/main/kotlin/lotto/Lotto.kt +++ b/kotlin-lotto/src/main/kotlin/lotto/Lotto.kt @@ -1,9 +1,9 @@ package lotto -class Lotto(private val numbers: List) { +class Lotto(val numbers: List) { init { require(numbers.size == 6) } - - // TODO: 추가 기능 구현 } + + diff --git a/kotlin-lotto/src/main/kotlin/lotto/LottoGame.kt b/kotlin-lotto/src/main/kotlin/lotto/LottoGame.kt new file mode 100644 index 0000000..7e8f0f3 --- /dev/null +++ b/kotlin-lotto/src/main/kotlin/lotto/LottoGame.kt @@ -0,0 +1,95 @@ +package lotto + +import lotto.LottoPrize.Companion.findPrize + + +class LottoGame { + companion object { + val lottoStore = LottoStore() + } + + fun run() { + val (purchasePrice, lottoCount) = lottoStore.receivePaymentForLotto() + println("${lottoCount}개를 구매했습니다.") + val generateLottoTickets = lottoStore.generateLottoTickets(lottoCount) + + for (generateLottoTicket in generateLottoTickets) { + println(generateLottoTicket.numbers) + } + + println() + + val winningNumbers = lottoStore.askWinningNumbers() + val bonusNumber = lottoStore.askBonusNumber() + + val (prizeList, prizeMoney) = checkPrizes(generateLottoTickets, winningNumbers, bonusNumber) + val returnRate = getReturnRate(purchasePrice, prizeMoney) + + printGameResult(prizeList, returnRate) + } + + fun getReturnRate(purchasePrice: Int, prizeMoney: Long): Double = + (prizeMoney.toDouble() / purchasePrice.toDouble() * 100.0) + + fun printGameResult(prizeList: List, returnRate: Double) { + println("당첨 통계") + println("---") + + + val result = prizeList.map { it.name }.groupingBy { it }.eachCount() + + listOf( + LottoPrize.THREE_MATCH, + LottoPrize.FOUR_MATCH, + LottoPrize.FIVE_MATCH, + LottoPrize.FIVE_PLUS_BONUS_MATCH, + LottoPrize.SIX_MATCH, + ).map { o -> + println("${o.description} - ${result.get(o.name) ?: 0}개") + } + + println("총 수익률은 ${returnRate}%입니다.") + } +} + +fun checkPrizes( + generatedLottoTickets: List, + winningNumbers: List, + bonusNumber: Int +): Pair, Long> { + var prizeMoney: Long = 0 + val prizeList = generatedLottoTickets.map { lotto -> + val matchCount = lotto.numbers.count { it in winningNumbers } + val isBonusMatched = bonusNumber in lotto.numbers + findPrize(matchCount, isBonusMatched) + } + + prizeList.forEach { o -> + prizeMoney += o.prize + } + + return Pair(prizeList, prizeMoney) +} + + +enum class LottoPrize( + val matchCount: Int, + val prize: Int, + val description: String, + val isBonusMatched: Boolean = false +) { + THREE_MATCH(3, 5_000, "3개 일치 (5,000원)"), + FOUR_MATCH(4, 50_000, "4개 일치 (50,000원)"), + FIVE_MATCH(5, 1_500_000, "5개 일치 (1,500,000원)"), + FIVE_PLUS_BONUS_MATCH(5, 30_000_000, "5개 일치, 보너스 볼 일치 (30,000,000원)", true), + SIX_MATCH(6, 2_000_000_000, "6개 일치 (2,000,000,000원)"), + LOSE(0, 0, "낙첨"); + + companion object { + fun findPrize(matchCount: Int, isBonusMatched: Boolean = false): LottoPrize { + return entries.find { it.matchCount == matchCount && it.isBonusMatched == isBonusMatched } ?: LOSE + } + } +} + + diff --git a/kotlin-lotto/src/main/kotlin/lotto/LottoStore.kt b/kotlin-lotto/src/main/kotlin/lotto/LottoStore.kt new file mode 100644 index 0000000..b5fd3cf --- /dev/null +++ b/kotlin-lotto/src/main/kotlin/lotto/LottoStore.kt @@ -0,0 +1,76 @@ +package lotto + +import camp.nextstep.edu.missionutils.Console +import camp.nextstep.edu.missionutils.Randoms +import lotto.common.require + +class LottoStore { + companion object { + const val LOTTO_NUMBER_MIN = 1 + const val LOTTO_NUMBER_MAX = 45 + const val LOTTO_NUMBERS_PER_TICKET = 6 + const val LOTTO_PRICE = 1000 + } + + fun askWinningNumbers(): List { + println("당첨 번호를 입력해주세요. 번호는 쉼표(,)로 구분하여 입력하세요:") + var numbers: List + do { + numbers = readLine()?.split(",")?.mapNotNull { it.trim().toIntOrNull() } ?: emptyList() + + require(numbers.size == LOTTO_NUMBERS_PER_TICKET) { IllegalArgumentException("[ERROR] 정확히 ${LottoStore.LOTTO_NUMBERS_PER_TICKET}개의 숫자를 입력해야 합니다.") } + numbers.forEach { number -> + require(number in LOTTO_NUMBER_MIN..LOTTO_NUMBER_MAX) { IllegalArgumentException("[ERROR] 숫자는 ${LottoStore.LOTTO_NUMBER_MIN}과 ${LottoStore.LOTTO_NUMBER_MAX} 사이여야 합니다.") } + } + } while (numbers.size != LOTTO_NUMBERS_PER_TICKET + || !numbers.all { it in LOTTO_NUMBER_MIN..LOTTO_NUMBER_MAX } + ) + + println() + + return numbers + } + + fun askBonusNumber(): Int { + println("보너스 번호를 입력해 주세요.") + var bonusNumber: Int + do { + bonusNumber = Console.readLine().toInt() + require(bonusNumber in LOTTO_NUMBER_MIN..LOTTO_NUMBER_MAX) { IllegalArgumentException("[ERROR] 숫자는 ${LOTTO_NUMBER_MIN}과 ${LOTTO_NUMBER_MAX} 사이여야 합니다.") } + } while (bonusNumber !in LOTTO_NUMBER_MIN..LOTTO_NUMBER_MAX) + + println() + + return bonusNumber + } + + fun receivePaymentForLotto(): Pair { + var purchasePrice: Int? = null + while (purchasePrice == null) { + println("구입금액을 입력해 주세요.") + val input = Console.readLine() + try { + purchasePrice = input?.toInt() + } catch (e: NumberFormatException) { + println("[ERROR] 유효한 숫자가 아닙니다. 다시 시도해주세요.") + } + } + println() + return Pair(purchasePrice, purchasePrice / LOTTO_PRICE) + } + + + fun generateLottoTickets(count: Int): List { + return List(count) { + generateLotto() + } + } + + fun generateLotto() = Lotto(generateRandomNumbers(LOTTO_NUMBER_MIN, LOTTO_NUMBER_MAX, LOTTO_NUMBERS_PER_TICKET)) + +} + + +fun generateRandomNumbers(startInclusive: Int, endInclusive: Int, count: Int): List = + Randoms.pickUniqueNumbersInRange(startInclusive, endInclusive, count) + diff --git a/kotlin-lotto/src/main/kotlin/lotto/common/ErrorCode.kt b/kotlin-lotto/src/main/kotlin/lotto/common/ErrorCode.kt new file mode 100644 index 0000000..8084c4f --- /dev/null +++ b/kotlin-lotto/src/main/kotlin/lotto/common/ErrorCode.kt @@ -0,0 +1,4 @@ +package lotto.common + +class ErrorCode { +} diff --git a/kotlin-lotto/src/main/kotlin/lotto/common/ValidationUtils.kt b/kotlin-lotto/src/main/kotlin/lotto/common/ValidationUtils.kt new file mode 100644 index 0000000..1a54066 --- /dev/null +++ b/kotlin-lotto/src/main/kotlin/lotto/common/ValidationUtils.kt @@ -0,0 +1,8 @@ +package lotto.common + +fun require(condition: Boolean, createException: () -> Exception) { + if (!condition) { + val exception = createException() + println(exception.message) + } +} From eca1cb802705fdc615bbcdb9dbbc27e77b9faa20 Mon Sep 17 00:00:00 2001 From: stopmin Date: Wed, 21 Feb 2024 23:34:45 +0900 Subject: [PATCH 2/7] =?UTF-8?q?chore:=20=ED=81=B4=EB=9E=98=EC=8A=A4=20?= =?UTF-8?q?=EC=9C=84=EC=B9=98=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kotlin-lotto/src/main/kotlin/lotto/Lotto.kt | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/kotlin-lotto/src/main/kotlin/lotto/Lotto.kt b/kotlin-lotto/src/main/kotlin/lotto/Lotto.kt index f83cdc7..65f6fc6 100644 --- a/kotlin-lotto/src/main/kotlin/lotto/Lotto.kt +++ b/kotlin-lotto/src/main/kotlin/lotto/Lotto.kt @@ -6,4 +6,23 @@ class Lotto(val numbers: List) { } } +enum class LottoPrize( + val matchCount: Int, + val prize: Int, + val description: String, + val isBonusMatched: Boolean = false +) { + THREE_MATCH(3, 5_000, "3개 일치 (5,000원)"), + FOUR_MATCH(4, 50_000, "4개 일치 (50,000원)"), + FIVE_MATCH(5, 1_500_000, "5개 일치 (1,500,000원)"), + FIVE_PLUS_BONUS_MATCH(5, 30_000_000, "5개 일치, 보너스 볼 일치 (30,000,000원)", true), + SIX_MATCH(6, 2_000_000_000, "6개 일치 (2,000,000,000원)"), + LOSE(0, 0, "낙첨"); + + companion object { + fun findPrize(matchCount: Int, isBonusMatched: Boolean = false): LottoPrize { + return entries.find { it.matchCount == matchCount && it.isBonusMatched == isBonusMatched } ?: LOSE + } + } +} From 72d50b1e07bb95f9dec88a69229a3957b45f290a Mon Sep 17 00:00:00 2001 From: stopmin Date: Wed, 21 Feb 2024 23:34:54 +0900 Subject: [PATCH 3/7] =?UTF-8?q?chore:=20=ED=81=B4=EB=9E=98=EC=8A=A4=20?= =?UTF-8?q?=EC=9C=84=EC=B9=98=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/kotlin/lotto/LottoGame.kt | 21 ------------------- 1 file changed, 21 deletions(-) diff --git a/kotlin-lotto/src/main/kotlin/lotto/LottoGame.kt b/kotlin-lotto/src/main/kotlin/lotto/LottoGame.kt index 7e8f0f3..2e0deae 100644 --- a/kotlin-lotto/src/main/kotlin/lotto/LottoGame.kt +++ b/kotlin-lotto/src/main/kotlin/lotto/LottoGame.kt @@ -72,24 +72,3 @@ fun checkPrizes( } -enum class LottoPrize( - val matchCount: Int, - val prize: Int, - val description: String, - val isBonusMatched: Boolean = false -) { - THREE_MATCH(3, 5_000, "3개 일치 (5,000원)"), - FOUR_MATCH(4, 50_000, "4개 일치 (50,000원)"), - FIVE_MATCH(5, 1_500_000, "5개 일치 (1,500,000원)"), - FIVE_PLUS_BONUS_MATCH(5, 30_000_000, "5개 일치, 보너스 볼 일치 (30,000,000원)", true), - SIX_MATCH(6, 2_000_000_000, "6개 일치 (2,000,000,000원)"), - LOSE(0, 0, "낙첨"); - - companion object { - fun findPrize(matchCount: Int, isBonusMatched: Boolean = false): LottoPrize { - return entries.find { it.matchCount == matchCount && it.isBonusMatched == isBonusMatched } ?: LOSE - } - } -} - - From 75473affd1e7ac6f0fd45287b24f48796390bc22 Mon Sep 17 00:00:00 2001 From: stopmin Date: Wed, 21 Feb 2024 23:35:02 +0900 Subject: [PATCH 4/7] =?UTF-8?q?chore:=20=EB=93=A4=EC=97=AC=EC=93=B0?= =?UTF-8?q?=EA=B8=B0=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kotlin-lotto/src/main/kotlin/lotto/LottoStore.kt | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/kotlin-lotto/src/main/kotlin/lotto/LottoStore.kt b/kotlin-lotto/src/main/kotlin/lotto/LottoStore.kt index b5fd3cf..ff5843e 100644 --- a/kotlin-lotto/src/main/kotlin/lotto/LottoStore.kt +++ b/kotlin-lotto/src/main/kotlin/lotto/LottoStore.kt @@ -18,10 +18,15 @@ class LottoStore { do { numbers = readLine()?.split(",")?.mapNotNull { it.trim().toIntOrNull() } ?: emptyList() - require(numbers.size == LOTTO_NUMBERS_PER_TICKET) { IllegalArgumentException("[ERROR] 정확히 ${LottoStore.LOTTO_NUMBERS_PER_TICKET}개의 숫자를 입력해야 합니다.") } + require(numbers.size == LOTTO_NUMBERS_PER_TICKET) { + IllegalArgumentException("[ERROR] 정확히 ${LOTTO_NUMBERS_PER_TICKET}개의 숫자를 입력해야 합니다.") + } numbers.forEach { number -> - require(number in LOTTO_NUMBER_MIN..LOTTO_NUMBER_MAX) { IllegalArgumentException("[ERROR] 숫자는 ${LottoStore.LOTTO_NUMBER_MIN}과 ${LottoStore.LOTTO_NUMBER_MAX} 사이여야 합니다.") } + require(number in LOTTO_NUMBER_MIN..LOTTO_NUMBER_MAX) { + IllegalArgumentException("[ERROR] 숫자는 ${LOTTO_NUMBER_MIN}과 ${LOTTO_NUMBER_MAX} 사이여야 합니다.") + } } + } while (numbers.size != LOTTO_NUMBERS_PER_TICKET || !numbers.all { it in LOTTO_NUMBER_MIN..LOTTO_NUMBER_MAX } ) From 264e78aabfaaa7fce16facd75a1638c5166bfb66 Mon Sep 17 00:00:00 2001 From: stopmin Date: Wed, 21 Feb 2024 23:37:28 +0900 Subject: [PATCH 5/7] =?UTF-8?q?refactor:=20private=20=EA=B4=80=EB=A0=A8=20?= =?UTF-8?q?=EC=88=98=EC=A0=95/=EB=A9=94=EC=84=9C=EB=93=9C=20=EC=B6=94?= =?UTF-8?q?=EC=B6=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kotlin-lotto/src/main/kotlin/lotto/LottoGame.kt | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/kotlin-lotto/src/main/kotlin/lotto/LottoGame.kt b/kotlin-lotto/src/main/kotlin/lotto/LottoGame.kt index 2e0deae..a446dff 100644 --- a/kotlin-lotto/src/main/kotlin/lotto/LottoGame.kt +++ b/kotlin-lotto/src/main/kotlin/lotto/LottoGame.kt @@ -13,11 +13,7 @@ class LottoGame { println("${lottoCount}개를 구매했습니다.") val generateLottoTickets = lottoStore.generateLottoTickets(lottoCount) - for (generateLottoTicket in generateLottoTickets) { - println(generateLottoTicket.numbers) - } - - println() + printGenerateTickets(generateLottoTickets) val winningNumbers = lottoStore.askWinningNumbers() val bonusNumber = lottoStore.askBonusNumber() @@ -28,10 +24,10 @@ class LottoGame { printGameResult(prizeList, returnRate) } - fun getReturnRate(purchasePrice: Int, prizeMoney: Long): Double = + private fun getReturnRate(purchasePrice: Int, prizeMoney: Long): Double = (prizeMoney.toDouble() / purchasePrice.toDouble() * 100.0) - fun printGameResult(prizeList: List, returnRate: Double) { + private fun printGameResult(prizeList: List, returnRate: Double) { println("당첨 통계") println("---") @@ -50,6 +46,13 @@ class LottoGame { println("총 수익률은 ${returnRate}%입니다.") } + + private fun printGenerateTickets(generateLottoTickets: List) { + for (generateLottoTicket in generateLottoTickets) { + println(generateLottoTicket.numbers) + } + println() + } } fun checkPrizes( From db45d35cf77cea28ba97766d1926b72daef53474 Mon Sep 17 00:00:00 2001 From: stopmin Date: Thu, 22 Feb 2024 09:15:02 +0900 Subject: [PATCH 6/7] =?UTF-8?q?refactor:=20=EC=9E=85=EB=A0=A5=EA=B0=92=20?= =?UTF-8?q?=EB=B0=9B=EB=8A=94=20=EB=B6=80=EB=B6=84=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kotlin-lotto/src/main/kotlin/lotto/LottoStore.kt | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/kotlin-lotto/src/main/kotlin/lotto/LottoStore.kt b/kotlin-lotto/src/main/kotlin/lotto/LottoStore.kt index ff5843e..bd0c78a 100644 --- a/kotlin-lotto/src/main/kotlin/lotto/LottoStore.kt +++ b/kotlin-lotto/src/main/kotlin/lotto/LottoStore.kt @@ -43,23 +43,25 @@ class LottoStore { bonusNumber = Console.readLine().toInt() require(bonusNumber in LOTTO_NUMBER_MIN..LOTTO_NUMBER_MAX) { IllegalArgumentException("[ERROR] 숫자는 ${LOTTO_NUMBER_MIN}과 ${LOTTO_NUMBER_MAX} 사이여야 합니다.") } } while (bonusNumber !in LOTTO_NUMBER_MIN..LOTTO_NUMBER_MAX) - println() return bonusNumber } fun receivePaymentForLotto(): Pair { - var purchasePrice: Int? = null - while (purchasePrice == null) { - println("구입금액을 입력해 주세요.") - val input = Console.readLine() + var purchasePrice: Int? + println("구입급액을 입력해주세요.") + do { try { + val input = Console.readLine() purchasePrice = input?.toInt() } catch (e: NumberFormatException) { - println("[ERROR] 유효한 숫자가 아닙니다. 다시 시도해주세요.") + println("[ERROR] 올바른 숫자를 입력해주세요.") + purchasePrice = null } - } + + } while (purchasePrice == null) + println() return Pair(purchasePrice, purchasePrice / LOTTO_PRICE) } From 88644b62309d32e0d27f6a3b320bbe65060b40f5 Mon Sep 17 00:00:00 2001 From: stopmin Date: Fri, 23 Feb 2024 00:04:59 +0900 Subject: [PATCH 7/7] =?UTF-8?q?refactor:=20=EC=9E=85=EB=A0=A5=EB=B0=9B?= =?UTF-8?q?=EB=8A=94=20=EB=A1=9C=EC=A7=81=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/kotlin/lotto/LottoStore.kt | 63 +++++++++++-------- .../kotlin/lotto/common/ValidationUtils.kt | 8 --- 2 files changed, 38 insertions(+), 33 deletions(-) delete mode 100644 kotlin-lotto/src/main/kotlin/lotto/common/ValidationUtils.kt diff --git a/kotlin-lotto/src/main/kotlin/lotto/LottoStore.kt b/kotlin-lotto/src/main/kotlin/lotto/LottoStore.kt index bd0c78a..519814a 100644 --- a/kotlin-lotto/src/main/kotlin/lotto/LottoStore.kt +++ b/kotlin-lotto/src/main/kotlin/lotto/LottoStore.kt @@ -2,7 +2,10 @@ package lotto import camp.nextstep.edu.missionutils.Console import camp.nextstep.edu.missionutils.Randoms -import lotto.common.require +import java.lang.IllegalStateException +import kotlin.jvm.Throws + +//import lotto.common.require class LottoStore { companion object { @@ -12,6 +15,26 @@ class LottoStore { const val LOTTO_PRICE = 1000 } + fun receivePaymentForLotto(): Pair { + var purchasePrice: Int? + println("구입급액을 입력해주세요.") + do { + val input = readLine() + purchasePrice = input?.toIntOrNull() + if (purchasePrice == null) { + println("[ERROR] 숫자로 입력해야합니다.") + throw NumberFormatException("[ERROR] 숫자로 입력해야합니다.") + } else if (purchasePrice % LOTTO_PRICE != 0) { + println("[ERROR] 숫자는 $LOTTO_PRICE(으)로 나누어 떨어져야 합니다.") + purchasePrice = null // 입력이 유효하지 않으므로 null로 설정하여 다시 입력 받음 + } + // 유효한 입력을 받으면 자동으로 반복문에서 탈출 + } while (purchasePrice == null) + + println() + return Pair(purchasePrice!!, purchasePrice / LOTTO_PRICE) + } + fun askWinningNumbers(): List { println("당첨 번호를 입력해주세요. 번호는 쉼표(,)로 구분하여 입력하세요:") var numbers: List @@ -19,14 +42,14 @@ class LottoStore { numbers = readLine()?.split(",")?.mapNotNull { it.trim().toIntOrNull() } ?: emptyList() require(numbers.size == LOTTO_NUMBERS_PER_TICKET) { - IllegalArgumentException("[ERROR] 정확히 ${LOTTO_NUMBERS_PER_TICKET}개의 숫자를 입력해야 합니다.") + IllegalArgumentException("[ERROR] 정확히 $LOTTO_NUMBERS_PER_TICKET}개의 숫자를 입력해야 합니다.") } + numbers.forEach { number -> require(number in LOTTO_NUMBER_MIN..LOTTO_NUMBER_MAX) { IllegalArgumentException("[ERROR] 숫자는 ${LOTTO_NUMBER_MIN}과 ${LOTTO_NUMBER_MAX} 사이여야 합니다.") } } - } while (numbers.size != LOTTO_NUMBERS_PER_TICKET || !numbers.all { it in LOTTO_NUMBER_MIN..LOTTO_NUMBER_MAX } ) @@ -38,32 +61,22 @@ class LottoStore { fun askBonusNumber(): Int { println("보너스 번호를 입력해 주세요.") - var bonusNumber: Int - do { - bonusNumber = Console.readLine().toInt() - require(bonusNumber in LOTTO_NUMBER_MIN..LOTTO_NUMBER_MAX) { IllegalArgumentException("[ERROR] 숫자는 ${LOTTO_NUMBER_MIN}과 ${LOTTO_NUMBER_MAX} 사이여야 합니다.") } - } while (bonusNumber !in LOTTO_NUMBER_MIN..LOTTO_NUMBER_MAX) - println() - - return bonusNumber - } - - fun receivePaymentForLotto(): Pair { - var purchasePrice: Int? - println("구입급액을 입력해주세요.") + var bonusNumber: Int? do { try { - val input = Console.readLine() - purchasePrice = input?.toInt() - } catch (e: NumberFormatException) { - println("[ERROR] 올바른 숫자를 입력해주세요.") - purchasePrice = null + bonusNumber = Console.readLine().toInt() + check(bonusNumber in LOTTO_NUMBER_MIN..LOTTO_NUMBER_MAX) { NumberFormatException() } + } catch (e: IllegalArgumentException) { + bonusNumber = null + println("[ERROR] 정확한 숫자 형식으로 입력해주세요.") + } catch (e: IllegalStateException) { + bonusNumber = null + println("[ERROR] 숫자는 ${LOTTO_NUMBER_MIN}과 ${LOTTO_NUMBER_MAX} 사이여야 합니다.") } - - } while (purchasePrice == null) - + } while (bonusNumber !is Int) println() - return Pair(purchasePrice, purchasePrice / LOTTO_PRICE) + + return bonusNumber!! } diff --git a/kotlin-lotto/src/main/kotlin/lotto/common/ValidationUtils.kt b/kotlin-lotto/src/main/kotlin/lotto/common/ValidationUtils.kt deleted file mode 100644 index 1a54066..0000000 --- a/kotlin-lotto/src/main/kotlin/lotto/common/ValidationUtils.kt +++ /dev/null @@ -1,8 +0,0 @@ -package lotto.common - -fun require(condition: Boolean, createException: () -> Exception) { - if (!condition) { - val exception = createException() - println(exception.message) - } -}