From 10d29c7e319fa184d4933d27ae06e43f32109ac3 Mon Sep 17 00:00:00 2001 From: kimgwon <92065911+kimgwon@users.noreply.github.com> Date: Mon, 4 Nov 2024 15:41:45 +0900 Subject: [PATCH 01/24] docs(README): Create README for feature documentation --- README.md | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/README.md b/README.md index 4fc0ae874..8b62fa7cc 100644 --- a/README.md +++ b/README.md @@ -1 +1,37 @@ # kotlin-lotto-precourse +## 입력 +`camp.nextstep.edu.missionutils.Console`의 `readLine()을 활용 +- [ ] `구입금액을 입력해 주세요.` 로또 구입 금액을 입력받는다. +- [ ] `당첨 번호를 입력해 주세요.` 당첨 번호를 입력 받는다. 쉼표를 기준으로 구분한다. +- [ ] `보너스 번호를 입력해 주세요.` 보너스 번호를 입력받는다. + +## Lotto +- 변수1 `번호 리스트` +- 변수2 `당첨 금액` +- 함수1 번호 정렬해서 번호 리스트에 저장 +- 함수2 번호가 몇 개 맞는지 체크해서 저장 + +## LottoController +- [ ] 로또 발매 (1,000원당 1장). `camp.nextstep.edu.missionutils.Randoms`의 `pickUniqueNumbersInRange()`를 활용. +- [ ] 수익률 계산 + +## LottoConstant +- 1등: 6개 번호 일치 / 2,000,000,000원 +- 2등: 5개 번호 + 보너스 번호 일치 / 30,000,000원 +- 3등: 5개 번호 일치 / 1,500,000원 +- 4등: 4개 번호 일치 / 50,000원 +- 5등: 3개 번호 일치 / 5,000원 + +## 출력 +- [ ] `$num개를 구매했습니다.` 발행한 로또 수량 및 번호를 출력한다. 로또 번호는 오름차순으로 정렬하여 보여준다. +- [ ] `당첨 통계` 당첨 내역을 출력한다. +- [ ] `총 수익률 $profitRate` 수익률을 출력한다. + +## 예외 +- [ ] 구입 금액은 숫자만 가능하다. +- [ ] 로또 구입 금액이 1,000원 단위로 나누어 떨어지지 않는 경우 예외 처리한다. +- [ ] 당첨 번호는 숫자여야 한다. +- [ ] 당첨 번호는 6개여야 한다. +- [ ] 보너스 번호는 숫자여야 한다. +- [ ] 보너스 번호는 1개여야 한다. +- [ ] 로또 번호는 1~45 사이의 숫자여야 한다. \ No newline at end of file From fb2f2cca9f8d8c1f5a5afa4fa26d7b30c6ae3e16 Mon Sep 17 00:00:00 2001 From: kimgwon <92065911+kimgwon@users.noreply.github.com> Date: Mon, 4 Nov 2024 15:48:02 +0900 Subject: [PATCH 02/24] feat(Input): Create getPurchaseAmount() function for user input - Implemented getPurchaseAmount() to receive purchase amount of lotto --- .../kotlin/lotto/validation/InputValidation.kt | 13 +++++++++++++ src/main/kotlin/lotto/view/Input.kt | 18 ++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 src/main/kotlin/lotto/validation/InputValidation.kt create mode 100644 src/main/kotlin/lotto/view/Input.kt diff --git a/src/main/kotlin/lotto/validation/InputValidation.kt b/src/main/kotlin/lotto/validation/InputValidation.kt new file mode 100644 index 000000000..0b7023801 --- /dev/null +++ b/src/main/kotlin/lotto/validation/InputValidation.kt @@ -0,0 +1,13 @@ +package lotto.validation + +class InputValidation { + fun typeInt(input: String): Int { + return input.toIntOrNull() ?: throw IllegalArgumentException("숫자를 입력해주세요.") + } + + fun unitOfNumber (number:Int, unit: Int): Int { + if (number % unit == 0) + return number + throw IllegalArgumentException("${unit}원 단위로 입력해주세요.") + } +} \ No newline at end of file diff --git a/src/main/kotlin/lotto/view/Input.kt b/src/main/kotlin/lotto/view/Input.kt new file mode 100644 index 000000000..3a114506e --- /dev/null +++ b/src/main/kotlin/lotto/view/Input.kt @@ -0,0 +1,18 @@ +package lotto.view + +import camp.nextstep.edu.missionutils.Console +import lotto.validation.InputValidation + +class Input { + private val inputValidation = InputValidation() + + fun getPurchaseAmount(): Int { + println("구입금액을 입력해 주세요.") + return readLine().let { + inputValidation.typeInt(it). also { amount -> + inputValidation.unitOfNumber(amount, 1000) } + } + } + + private fun readLine(): String = Console.readLine() +} \ No newline at end of file From bd0dc2e3d2c9c2a0f7dbd4330324048b22c7060b Mon Sep 17 00:00:00 2001 From: kimgwon <92065911+kimgwon@users.noreply.github.com> Date: Mon, 4 Nov 2024 16:58:35 +0900 Subject: [PATCH 03/24] feat(Input): Create getLotteryNumbers() function for user input - Implemented getLotteryNumbers() to receive result of lotto numbers --- src/main/kotlin/lotto/validation/InputValidation.kt | 10 ++++++++-- src/main/kotlin/lotto/view/Input.kt | 12 ++++++++++-- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/main/kotlin/lotto/validation/InputValidation.kt b/src/main/kotlin/lotto/validation/InputValidation.kt index 0b7023801..1698802b6 100644 --- a/src/main/kotlin/lotto/validation/InputValidation.kt +++ b/src/main/kotlin/lotto/validation/InputValidation.kt @@ -2,12 +2,18 @@ package lotto.validation class InputValidation { fun typeInt(input: String): Int { - return input.toIntOrNull() ?: throw IllegalArgumentException("숫자를 입력해주세요.") + return input.toIntOrNull() ?: throw IllegalArgumentException("숫자를 입력해주세요. ${input}은 숫자가 아닙니다.") } - fun unitOfNumber (number:Int, unit: Int): Int { + fun unitOfNumber(number: Int, unit: Int): Int { if (number % unit == 0) return number throw IllegalArgumentException("${unit}원 단위로 입력해주세요.") } + + fun lotteryNumbersDelimiter(input: String): List { + return input.split(",").map { + typeInt(it) + } + } } \ No newline at end of file diff --git a/src/main/kotlin/lotto/view/Input.kt b/src/main/kotlin/lotto/view/Input.kt index 3a114506e..39d9880fb 100644 --- a/src/main/kotlin/lotto/view/Input.kt +++ b/src/main/kotlin/lotto/view/Input.kt @@ -9,9 +9,17 @@ class Input { fun getPurchaseAmount(): Int { println("구입금액을 입력해 주세요.") return readLine().let { - inputValidation.typeInt(it). also { amount -> - inputValidation.unitOfNumber(amount, 1000) } + inputValidation.typeInt(it).also { amount -> + inputValidation.unitOfNumber(amount, 1000) } + } + } + + fun getLotteryNumbers(): List { + println("당첨 번호를 입력해 주세요.") + return readLine().let { + inputValidation.lotteryNumbersDelimiter(it) + } } private fun readLine(): String = Console.readLine() From 99724224fbe760aac58db2116daf3cd1bd3fdfe0 Mon Sep 17 00:00:00 2001 From: kimgwon <92065911+kimgwon@users.noreply.github.com> Date: Mon, 4 Nov 2024 18:20:29 +0900 Subject: [PATCH 04/24] feat(Input): Create getLotteryBonusNumber() function for user input - Implemented getLotteryBonusNumber() to receive result of lotto bonus number --- src/main/kotlin/lotto/validation/InputValidation.kt | 5 +++++ src/main/kotlin/lotto/view/Input.kt | 9 +++++++++ 2 files changed, 14 insertions(+) diff --git a/src/main/kotlin/lotto/validation/InputValidation.kt b/src/main/kotlin/lotto/validation/InputValidation.kt index 1698802b6..9d34acb92 100644 --- a/src/main/kotlin/lotto/validation/InputValidation.kt +++ b/src/main/kotlin/lotto/validation/InputValidation.kt @@ -16,4 +16,9 @@ class InputValidation { typeInt(it) } } + + fun lotteryNumberRange(number: Int): Int { + require(number in 1..45) { "로또 번호는 1부터 45 사이의 숫자여야 합니다." } + return number + } } \ No newline at end of file diff --git a/src/main/kotlin/lotto/view/Input.kt b/src/main/kotlin/lotto/view/Input.kt index 39d9880fb..a08dc4f35 100644 --- a/src/main/kotlin/lotto/view/Input.kt +++ b/src/main/kotlin/lotto/view/Input.kt @@ -22,5 +22,14 @@ class Input { } } + fun getLotteryBonusNumber(): Int { + println("보너스 번호를 입력해주세요.") + return readLine().let { + inputValidation.typeInt(it).also { number -> + inputValidation.lotteryNumberRange(number) + } + } + } + private fun readLine(): String = Console.readLine() } \ No newline at end of file From c2b2a930216df32ceb8b318c2698f627556b7b6e Mon Sep 17 00:00:00 2001 From: kimgwon <92065911+kimgwon@users.noreply.github.com> Date: Mon, 4 Nov 2024 18:45:31 +0900 Subject: [PATCH 05/24] feat(InputValidation): Create lottoNumbers() function for validation lotto numbers - Implemented lottoNumbers() to validate lotto numbers - Validated lotto numbers count is 6 and numbers don't duplicate --- src/main/kotlin/lotto/Lotto.kt | 6 ++++- .../lotto/validation/InputValidation.kt | 23 +++++++++++++++++-- src/main/kotlin/lotto/view/Input.kt | 8 +++---- 3 files changed, 30 insertions(+), 7 deletions(-) diff --git a/src/main/kotlin/lotto/Lotto.kt b/src/main/kotlin/lotto/Lotto.kt index b97abc385..b962ad24a 100644 --- a/src/main/kotlin/lotto/Lotto.kt +++ b/src/main/kotlin/lotto/Lotto.kt @@ -1,8 +1,12 @@ package lotto +import lotto.validation.InputValidation + class Lotto(private val numbers: List) { + private val validator = InputValidation() + init { - require(numbers.size == 6) { "[ERROR] 로또 번호는 6개여야 합니다." } + validator.lottoNumbers(numbers) } // TODO: 추가 기능 구현 diff --git a/src/main/kotlin/lotto/validation/InputValidation.kt b/src/main/kotlin/lotto/validation/InputValidation.kt index 9d34acb92..9b250e1f4 100644 --- a/src/main/kotlin/lotto/validation/InputValidation.kt +++ b/src/main/kotlin/lotto/validation/InputValidation.kt @@ -11,14 +11,33 @@ class InputValidation { throw IllegalArgumentException("${unit}원 단위로 입력해주세요.") } - fun lotteryNumbersDelimiter(input: String): List { + fun lottoNumbersDelimiter(input: String): List { return input.split(",").map { typeInt(it) } } - fun lotteryNumberRange(number: Int): Int { + fun lottoNumberRange(number: Int): Int { require(number in 1..45) { "로또 번호는 1부터 45 사이의 숫자여야 합니다." } return number } + + private fun lottoNumbersSize(numbers: List): List { + require(numbers.size == 6) { "당첨 번호는 6개의 숫자로 입력해주세요." } + return numbers + } + + private fun lottoNumbersNotDuplicate(numbers: List): List { + require(numbers.toSet().size == 6) { "당첨 번호는 중복되면 안됩니다." } + return numbers + } + + fun lottoNumbers(numbers: List): List { + return numbers.map { + lottoNumberRange(it) + }.also { + lottoNumbersSize(it) + lottoNumbersNotDuplicate(it) + } + } } \ No newline at end of file diff --git a/src/main/kotlin/lotto/view/Input.kt b/src/main/kotlin/lotto/view/Input.kt index a08dc4f35..beaabee07 100644 --- a/src/main/kotlin/lotto/view/Input.kt +++ b/src/main/kotlin/lotto/view/Input.kt @@ -15,18 +15,18 @@ class Input { } } - fun getLotteryNumbers(): List { + fun getLottoNumbers(): List { println("당첨 번호를 입력해 주세요.") return readLine().let { - inputValidation.lotteryNumbersDelimiter(it) + inputValidation.lottoNumbersDelimiter(it) } } - fun getLotteryBonusNumber(): Int { + fun getLottoBonusNumber(): Int { println("보너스 번호를 입력해주세요.") return readLine().let { inputValidation.typeInt(it).also { number -> - inputValidation.lotteryNumberRange(number) + inputValidation.lottoNumberRange(number) } } } From ca8c5183cb3bf9b5981c992dc46d3224aaa60f4e Mon Sep 17 00:00:00 2001 From: kimgwon <92065911+kimgwon@users.noreply.github.com> Date: Mon, 4 Nov 2024 19:30:01 +0900 Subject: [PATCH 06/24] feat(LottoController): Create getLottoTicketCount() function for calculation lotto ticket - Implemented getLottoTicketCount() to calculation from amount --- src/main/kotlin/lotto/controller/LottoController.kt | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 src/main/kotlin/lotto/controller/LottoController.kt diff --git a/src/main/kotlin/lotto/controller/LottoController.kt b/src/main/kotlin/lotto/controller/LottoController.kt new file mode 100644 index 000000000..dbe81b325 --- /dev/null +++ b/src/main/kotlin/lotto/controller/LottoController.kt @@ -0,0 +1,9 @@ +package lotto.controller + +import lotto.view.Input + +class LottoController(val input: Input) { + private fun getLottoTicketCount(): Int { + return input.getPurchaseAmount() / 1000 + } +} \ No newline at end of file From 4641e64954d4228f708a5340c98028ff2e2f91fb Mon Sep 17 00:00:00 2001 From: kimgwon <92065911+kimgwon@users.noreply.github.com> Date: Mon, 4 Nov 2024 19:31:57 +0900 Subject: [PATCH 07/24] feat(LottoController): Add createRandomLotto() function for creating Lotto - Implemented createRandomLotto() to create random numbers lotto --- src/main/kotlin/lotto/controller/LottoController.kt | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/main/kotlin/lotto/controller/LottoController.kt b/src/main/kotlin/lotto/controller/LottoController.kt index dbe81b325..ecebbe7bd 100644 --- a/src/main/kotlin/lotto/controller/LottoController.kt +++ b/src/main/kotlin/lotto/controller/LottoController.kt @@ -1,9 +1,15 @@ package lotto.controller +import camp.nextstep.edu.missionutils.Randoms.pickUniqueNumbersInRange +import lotto.Lotto import lotto.view.Input class LottoController(val input: Input) { private fun getLottoTicketCount(): Int { return input.getPurchaseAmount() / 1000 } + + private fun createRandomLotto(): Lotto { + return Lotto(pickUniqueNumbersInRange(1, 45, 6).sorted()) + } } \ No newline at end of file From b14642221f6aa73ce67db025f87bb2f9fa1d7edb Mon Sep 17 00:00:00 2001 From: kimgwon <92065911+kimgwon@users.noreply.github.com> Date: Mon, 4 Nov 2024 19:33:50 +0900 Subject: [PATCH 08/24] feat(LottoController): Add createResult() function for creating lotto number result - Implemented createResult() to create lotto result --- .../kotlin/lotto/controller/LottoController.kt | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/main/kotlin/lotto/controller/LottoController.kt b/src/main/kotlin/lotto/controller/LottoController.kt index ecebbe7bd..1bdf25917 100644 --- a/src/main/kotlin/lotto/controller/LottoController.kt +++ b/src/main/kotlin/lotto/controller/LottoController.kt @@ -2,9 +2,12 @@ package lotto.controller import camp.nextstep.edu.missionutils.Randoms.pickUniqueNumbersInRange import lotto.Lotto +import lotto.validation.InputValidation import lotto.view.Input class LottoController(val input: Input) { + private val validator = InputValidation() + private fun getLottoTicketCount(): Int { return input.getPurchaseAmount() / 1000 } @@ -12,4 +15,18 @@ class LottoController(val input: Input) { private fun createRandomLotto(): Lotto { return Lotto(pickUniqueNumbersInRange(1, 45, 6).sorted()) } + + private fun createResult(): List { + val numbers = input.getLottoNumbers() + val bonusNumbers = input.getLottoBonusNumber() + val resultNumbers: MutableList = mutableListOf() + + repeat(numbers.size) { + resultNumbers.add(numbers[it]) + } + resultNumbers.add(bonusNumbers) + + return validator.lottoNumbersNotDuplicate(resultNumbers.toList()) + } + } \ No newline at end of file From 747c80dc33571884897fc3d6ce4b4fa945717491 Mon Sep 17 00:00:00 2001 From: kimgwon <92065911+kimgwon@users.noreply.github.com> Date: Mon, 4 Nov 2024 19:40:43 +0900 Subject: [PATCH 09/24] feat(Output): Add printLottoNumbers() function for printing lotto numbers - Implemented printLottoNumbers() to print lotto numbers --- src/main/kotlin/lotto/view/Output.kt | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 src/main/kotlin/lotto/view/Output.kt diff --git a/src/main/kotlin/lotto/view/Output.kt b/src/main/kotlin/lotto/view/Output.kt new file mode 100644 index 000000000..2ce880b7c --- /dev/null +++ b/src/main/kotlin/lotto/view/Output.kt @@ -0,0 +1,5 @@ +package lotto.view + +class Output { + fun printLottoNumbers(numbers: List) = println(numbers) +} \ No newline at end of file From a31aa4f4a4e5016d72700aa4dbb7ec917f0f86cb Mon Sep 17 00:00:00 2001 From: kimgwon <92065911+kimgwon@users.noreply.github.com> Date: Mon, 4 Nov 2024 21:33:21 +0900 Subject: [PATCH 10/24] feat(Lotto): Add getLottoNumbers() function for get lotto numbers - Implemented getLottoNumbers() to get lotto numbers --- src/main/kotlin/lotto/{ => model}/Lotto.kt | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) rename src/main/kotlin/lotto/{ => model}/Lotto.kt (68%) diff --git a/src/main/kotlin/lotto/Lotto.kt b/src/main/kotlin/lotto/model/Lotto.kt similarity index 68% rename from src/main/kotlin/lotto/Lotto.kt rename to src/main/kotlin/lotto/model/Lotto.kt index b962ad24a..7d04a6649 100644 --- a/src/main/kotlin/lotto/Lotto.kt +++ b/src/main/kotlin/lotto/model/Lotto.kt @@ -1,4 +1,4 @@ -package lotto +package lotto.model import lotto.validation.InputValidation @@ -9,5 +9,7 @@ class Lotto(private val numbers: List) { validator.lottoNumbers(numbers) } - // TODO: 추가 기능 구현 + fun getLottoNumbers(): List { + return numbers + } } From 3184f1442aeb98f61eeb75dd93168ce9138ccdf5 Mon Sep 17 00:00:00 2001 From: kimgwon <92065911+kimgwon@users.noreply.github.com> Date: Mon, 4 Nov 2024 21:36:31 +0900 Subject: [PATCH 11/24] feat(LottoController): Add calculateTotalMatchResult() function for calculating lotto result - Implemented calculateTotalMatchResult() to calculate result of total lotto --- .../lotto/controller/LottoController.kt | 20 +++++++++++++++++-- src/main/kotlin/lotto/model/Lotto.kt | 12 +++++++++++ .../kotlin/lotto/model/LottoMatchResult.kt | 6 ++++++ .../lotto/model/LottoTotalMatchResult.kt | 9 +++++++++ 4 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 src/main/kotlin/lotto/model/LottoMatchResult.kt create mode 100644 src/main/kotlin/lotto/model/LottoTotalMatchResult.kt diff --git a/src/main/kotlin/lotto/controller/LottoController.kt b/src/main/kotlin/lotto/controller/LottoController.kt index 1bdf25917..df90f76bd 100644 --- a/src/main/kotlin/lotto/controller/LottoController.kt +++ b/src/main/kotlin/lotto/controller/LottoController.kt @@ -1,12 +1,16 @@ package lotto.controller import camp.nextstep.edu.missionutils.Randoms.pickUniqueNumbersInRange -import lotto.Lotto +import lotto.model.Lotto +import lotto.model.LottoTotalMatchResult import lotto.validation.InputValidation import lotto.view.Input +import lotto.view.Output -class LottoController(val input: Input) { +class LottoController(val input: Input, val output: Output) { + private val lottos: MutableList = mutableListOf() private val validator = InputValidation() + private val totalMatchResult = LottoTotalMatchResult() private fun getLottoTicketCount(): Int { return input.getPurchaseAmount() / 1000 @@ -29,4 +33,16 @@ class LottoController(val input: Input) { return validator.lottoNumbersNotDuplicate(resultNumbers.toList()) } + private fun calculateTotalMatchResult(result: List) { + repeat(lottos.size) { idx -> + val matchResult = lottos[idx].calculateMatchResult(result) + when (matchResult.matchNumbersCount) { + 1 -> totalMatchResult.prize1 += 1 + 2 -> if (matchResult.isMatchBonus) totalMatchResult.prize2 += 1 + 3 -> totalMatchResult.prize3 += 1 + 4 -> totalMatchResult.prize4 += 1 + 5 -> totalMatchResult.prize5 += 1 + } + } + } } \ No newline at end of file diff --git a/src/main/kotlin/lotto/model/Lotto.kt b/src/main/kotlin/lotto/model/Lotto.kt index 7d04a6649..00e2333f1 100644 --- a/src/main/kotlin/lotto/model/Lotto.kt +++ b/src/main/kotlin/lotto/model/Lotto.kt @@ -12,4 +12,16 @@ class Lotto(private val numbers: List) { fun getLottoNumbers(): List { return numbers } + + fun calculateMatchResult(resultNumbers: List): LottoMatchResult { + val numbersForCheck = numbers.toSet() + val lottoResult = LottoMatchResult() + + repeat(resultNumbers.size - 1) { + if (it in numbersForCheck) lottoResult.matchNumbersCount += 1 + } + if (resultNumbers.last() in numbersForCheck) lottoResult.isMatchBonus = true + + return lottoResult + } } diff --git a/src/main/kotlin/lotto/model/LottoMatchResult.kt b/src/main/kotlin/lotto/model/LottoMatchResult.kt new file mode 100644 index 000000000..bb7e74401 --- /dev/null +++ b/src/main/kotlin/lotto/model/LottoMatchResult.kt @@ -0,0 +1,6 @@ +package lotto.model + +data class LottoMatchResult( + var matchNumbersCount: Int = 0, + var isMatchBonus: Boolean = false +) \ No newline at end of file diff --git a/src/main/kotlin/lotto/model/LottoTotalMatchResult.kt b/src/main/kotlin/lotto/model/LottoTotalMatchResult.kt new file mode 100644 index 000000000..2a23cf3dd --- /dev/null +++ b/src/main/kotlin/lotto/model/LottoTotalMatchResult.kt @@ -0,0 +1,9 @@ +package lotto.model + +data class LottoTotalMatchResult( + var prize1: Int = 0, + var prize2: Int = 0, + var prize3: Int = 0, + var prize4: Int = 0, + var prize5: Int = 0 +) \ No newline at end of file From ecc058a47f7ac8387f4d45ee132123bc5c602ae0 Mon Sep 17 00:00:00 2001 From: kimgwon <92065911+kimgwon@users.noreply.github.com> Date: Mon, 4 Nov 2024 21:38:01 +0900 Subject: [PATCH 12/24] refactor(InputValidation): Modify lottoNumberNotDuplicate() function to public - Modified lottoNumberNotDuplicate() to public, to use create lotto result in LottoController --- src/main/kotlin/lotto/validation/InputValidation.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/lotto/validation/InputValidation.kt b/src/main/kotlin/lotto/validation/InputValidation.kt index 9b250e1f4..ced23bc1e 100644 --- a/src/main/kotlin/lotto/validation/InputValidation.kt +++ b/src/main/kotlin/lotto/validation/InputValidation.kt @@ -27,8 +27,8 @@ class InputValidation { return numbers } - private fun lottoNumbersNotDuplicate(numbers: List): List { - require(numbers.toSet().size == 6) { "당첨 번호는 중복되면 안됩니다." } + fun lottoNumbersNotDuplicate(numbers: List): List { + require(numbers.toSet().size == numbers.size) { "번호는 중복되면 안됩니다." } return numbers } From b92ca75cfe53a61e456c68f7646aa2929902fe62 Mon Sep 17 00:00:00 2001 From: kimgwon <92065911+kimgwon@users.noreply.github.com> Date: Mon, 4 Nov 2024 22:19:45 +0900 Subject: [PATCH 13/24] fix(LottoController): Modify logic of prize count Corrected the order of match conditions. This ensures accurate counting of prize matches. --- .../kotlin/lotto/controller/LottoController.kt | 14 ++++++++------ .../kotlin/lotto/model/LottoTotalMatchResult.kt | 10 +++++----- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/main/kotlin/lotto/controller/LottoController.kt b/src/main/kotlin/lotto/controller/LottoController.kt index df90f76bd..ad7fac91b 100644 --- a/src/main/kotlin/lotto/controller/LottoController.kt +++ b/src/main/kotlin/lotto/controller/LottoController.kt @@ -33,16 +33,18 @@ class LottoController(val input: Input, val output: Output) { return validator.lottoNumbersNotDuplicate(resultNumbers.toList()) } - private fun calculateTotalMatchResult(result: List) { + private fun calculateTotalMatchResult(result: List): LottoTotalMatchResult { + val totalMatchResult = LottoTotalMatchResult() repeat(lottos.size) { idx -> val matchResult = lottos[idx].calculateMatchResult(result) when (matchResult.matchNumbersCount) { - 1 -> totalMatchResult.prize1 += 1 - 2 -> if (matchResult.isMatchBonus) totalMatchResult.prize2 += 1 - 3 -> totalMatchResult.prize3 += 1 - 4 -> totalMatchResult.prize4 += 1 - 5 -> totalMatchResult.prize5 += 1 + 5 -> totalMatchResult.prizeCount1 += 1 + 4 -> if (matchResult.isMatchBonus) totalMatchResult.prizeCount2 += 1 + 3 -> totalMatchResult.prizeCount3 += 1 + 2 -> totalMatchResult.prizeCount4 += 1 + 1 -> totalMatchResult.prizeCount5 += 1 } } + return totalMatchResult } } \ No newline at end of file diff --git a/src/main/kotlin/lotto/model/LottoTotalMatchResult.kt b/src/main/kotlin/lotto/model/LottoTotalMatchResult.kt index 2a23cf3dd..ab1c75c6e 100644 --- a/src/main/kotlin/lotto/model/LottoTotalMatchResult.kt +++ b/src/main/kotlin/lotto/model/LottoTotalMatchResult.kt @@ -1,9 +1,9 @@ package lotto.model data class LottoTotalMatchResult( - var prize1: Int = 0, - var prize2: Int = 0, - var prize3: Int = 0, - var prize4: Int = 0, - var prize5: Int = 0 + var prizeCount1: Int = 0, + var prizeCount2: Int = 0, + var prizeCount3: Int = 0, + var prizeCount4: Int = 0, + var prizeCount5: Int = 0 ) \ No newline at end of file From 43e8a7e51183c0004873897837c3cf3a4f5435cb Mon Sep 17 00:00:00 2001 From: kimgwon <92065911+kimgwon@users.noreply.github.com> Date: Mon, 4 Nov 2024 22:21:08 +0900 Subject: [PATCH 14/24] feat(LottoController): Add calculateRateOfReturn() function to lotto's calculate rate of return --- src/main/kotlin/lotto/controller/LottoController.kt | 5 ++++- src/main/kotlin/lotto/model/LottoTotalMatchResult.kt | 9 ++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/lotto/controller/LottoController.kt b/src/main/kotlin/lotto/controller/LottoController.kt index ad7fac91b..a0555607e 100644 --- a/src/main/kotlin/lotto/controller/LottoController.kt +++ b/src/main/kotlin/lotto/controller/LottoController.kt @@ -10,7 +10,6 @@ import lotto.view.Output class LottoController(val input: Input, val output: Output) { private val lottos: MutableList = mutableListOf() private val validator = InputValidation() - private val totalMatchResult = LottoTotalMatchResult() private fun getLottoTicketCount(): Int { return input.getPurchaseAmount() / 1000 @@ -47,4 +46,8 @@ class LottoController(val input: Input, val output: Output) { } return totalMatchResult } + + private fun calculateRateOfReturn(totalMatchResult: LottoTotalMatchResult): Float { + return String.format("%.1f", totalMatchResult.getTotalPrize() / lottos.size).toFloat() + } } \ No newline at end of file diff --git a/src/main/kotlin/lotto/model/LottoTotalMatchResult.kt b/src/main/kotlin/lotto/model/LottoTotalMatchResult.kt index ab1c75c6e..47b1cefe5 100644 --- a/src/main/kotlin/lotto/model/LottoTotalMatchResult.kt +++ b/src/main/kotlin/lotto/model/LottoTotalMatchResult.kt @@ -6,4 +6,11 @@ data class LottoTotalMatchResult( var prizeCount3: Int = 0, var prizeCount4: Int = 0, var prizeCount5: Int = 0 -) \ No newline at end of file +) { + private fun getPrize1(): Int = prizeCount1 * 2000000000 + private fun getPrize2(): Int = prizeCount2 * 30000000 + private fun getPrize3(): Int = prizeCount3 * 1500000 + private fun getPrize4(): Int = prizeCount4 * 50000 + private fun getPrize5(): Int = prizeCount5 * 5000 + fun getTotalPrize(): Float = (getPrize1() + getPrize2() + getPrize3() + getPrize4() + getPrize5()).toFloat() +} \ No newline at end of file From 4e2a2f80cab9c28dfddb8d9e1e6028256ea5872d Mon Sep 17 00:00:00 2001 From: kimgwon <92065911+kimgwon@users.noreply.github.com> Date: Mon, 4 Nov 2024 22:39:13 +0900 Subject: [PATCH 15/24] fix(Input): Add validation for unique lotto numbers in 1..45 range Ensured lotto numbers are unique and fall in 1 to 45 range. --- src/main/kotlin/lotto/view/Input.kt | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/lotto/view/Input.kt b/src/main/kotlin/lotto/view/Input.kt index beaabee07..3e4e01210 100644 --- a/src/main/kotlin/lotto/view/Input.kt +++ b/src/main/kotlin/lotto/view/Input.kt @@ -17,9 +17,11 @@ class Input { fun getLottoNumbers(): List { println("당첨 번호를 입력해 주세요.") - return readLine().let { - inputValidation.lottoNumbersDelimiter(it) + val value = readLine().let { + inputValidation.lottoNumbers(inputValidation.lottoNumbersDelimiter(it)) } + println() + return value } fun getLottoBonusNumber(): Int { From c9c8711c9e2ca29fb206061bb5037a2e2318e1f0 Mon Sep 17 00:00:00 2001 From: kimgwon <92065911+kimgwon@users.noreply.github.com> Date: Mon, 4 Nov 2024 22:40:21 +0900 Subject: [PATCH 16/24] chore(Input): Add println() to change line --- src/main/kotlin/lotto/view/Input.kt | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/lotto/view/Input.kt b/src/main/kotlin/lotto/view/Input.kt index 3e4e01210..e03cc0a9c 100644 --- a/src/main/kotlin/lotto/view/Input.kt +++ b/src/main/kotlin/lotto/view/Input.kt @@ -8,11 +8,13 @@ class Input { fun getPurchaseAmount(): Int { println("구입금액을 입력해 주세요.") - return readLine().let { + val value = readLine().let { inputValidation.typeInt(it).also { amount -> inputValidation.unitOfNumber(amount, 1000) } } + println() + return value } fun getLottoNumbers(): List { @@ -26,11 +28,13 @@ class Input { fun getLottoBonusNumber(): Int { println("보너스 번호를 입력해주세요.") - return readLine().let { + val value = readLine().let { inputValidation.typeInt(it).also { number -> inputValidation.lottoNumberRange(number) } } + println() + return value } private fun readLine(): String = Console.readLine() From 1e3e31690c18c058c4d096353951a345bd029737 Mon Sep 17 00:00:00 2001 From: kimgwon <92065911+kimgwon@users.noreply.github.com> Date: Mon, 4 Nov 2024 23:00:53 +0900 Subject: [PATCH 17/24] chore(LottoController): Add value amount to calculate rate of return --- .../kotlin/lotto/controller/LottoController.kt | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/main/kotlin/lotto/controller/LottoController.kt b/src/main/kotlin/lotto/controller/LottoController.kt index a0555607e..df2fbb8fa 100644 --- a/src/main/kotlin/lotto/controller/LottoController.kt +++ b/src/main/kotlin/lotto/controller/LottoController.kt @@ -11,8 +11,8 @@ class LottoController(val input: Input, val output: Output) { private val lottos: MutableList = mutableListOf() private val validator = InputValidation() - private fun getLottoTicketCount(): Int { - return input.getPurchaseAmount() / 1000 + private fun getLottoTicketCount(amount: Int): Int { + return amount / 1000 } private fun createRandomLotto(): Lotto { @@ -37,17 +37,16 @@ class LottoController(val input: Input, val output: Output) { repeat(lottos.size) { idx -> val matchResult = lottos[idx].calculateMatchResult(result) when (matchResult.matchNumbersCount) { - 5 -> totalMatchResult.prizeCount1 += 1 - 4 -> if (matchResult.isMatchBonus) totalMatchResult.prizeCount2 += 1 - 3 -> totalMatchResult.prizeCount3 += 1 - 2 -> totalMatchResult.prizeCount4 += 1 - 1 -> totalMatchResult.prizeCount5 += 1 + 6 -> totalMatchResult.prizeCount1 += 1 + 5 -> if (matchResult.isMatchBonus) totalMatchResult.prizeCount2 += 1 else totalMatchResult.prizeCount3 += 1 + 4 -> totalMatchResult.prizeCount4 += 1 + 3 -> totalMatchResult.prizeCount5 += 1 } } return totalMatchResult } - private fun calculateRateOfReturn(totalMatchResult: LottoTotalMatchResult): Float { - return String.format("%.1f", totalMatchResult.getTotalPrize() / lottos.size).toFloat() + private fun calculateRateOfReturn(totalMatchResult: LottoTotalMatchResult, amount: Int): Float { + return String.format("%.1f", totalMatchResult.getTotalPrize() / amount).toFloat() } } \ No newline at end of file From d5348f07b8417a59f17eb3d3a58eddd3f8b9c823 Mon Sep 17 00:00:00 2001 From: kimgwon <92065911+kimgwon@users.noreply.github.com> Date: Mon, 4 Nov 2024 23:05:09 +0900 Subject: [PATCH 18/24] feat(LottoController): Add runMachine() function to run lotto machine - Got lotto result numbers, bonus number - Calculated how many match numbers - Calculated the rate of return --- src/main/kotlin/lotto/Application.kt | 10 +++++++++- .../kotlin/lotto/controller/LottoController.kt | 16 ++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/lotto/Application.kt b/src/main/kotlin/lotto/Application.kt index 151821c9c..523b8fab9 100644 --- a/src/main/kotlin/lotto/Application.kt +++ b/src/main/kotlin/lotto/Application.kt @@ -1,5 +1,13 @@ package lotto +import lotto.controller.LottoController +import lotto.view.Input +import lotto.view.Output + fun main() { - // TODO: 프로그램 구현 + val input: Input = Input() + val output: Output = Output() + val lottoController: LottoController = LottoController(input, output) + + lottoController.runMachine() } diff --git a/src/main/kotlin/lotto/controller/LottoController.kt b/src/main/kotlin/lotto/controller/LottoController.kt index df2fbb8fa..cb76f3eb2 100644 --- a/src/main/kotlin/lotto/controller/LottoController.kt +++ b/src/main/kotlin/lotto/controller/LottoController.kt @@ -49,4 +49,20 @@ class LottoController(val input: Input, val output: Output) { private fun calculateRateOfReturn(totalMatchResult: LottoTotalMatchResult, amount: Int): Float { return String.format("%.1f", totalMatchResult.getTotalPrize() / amount).toFloat() } + + fun runMachine() { + val amount = input.getPurchaseAmount() + val count = getLottoTicketCount(amount) + repeat(count) { + val lotto: Lotto = createRandomLotto() + lottos.add(lotto) + } + + output.printLottoNumbers(lottos) + + val result = createResult() + val totalMatchResult = calculateTotalMatchResult(result) + val rateOfReturn = calculateRateOfReturn(totalMatchResult, amount) + output.printTotalLottoPrize(totalMatchResult, rateOfReturn) + } } \ No newline at end of file From ab20982df7c26f8f20f112b96fede3ea2247b5ee Mon Sep 17 00:00:00 2001 From: kimgwon <92065911+kimgwon@users.noreply.github.com> Date: Mon, 4 Nov 2024 23:08:52 +0900 Subject: [PATCH 19/24] fix(Lotto): Change condition in calculateMatchResult() function - Changed condition from (it in numbers) to (resultNumbers[idx] in numbers) as 'it' represents the index, not the value. --- src/main/kotlin/lotto/model/Lotto.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/lotto/model/Lotto.kt b/src/main/kotlin/lotto/model/Lotto.kt index 00e2333f1..d039b25fc 100644 --- a/src/main/kotlin/lotto/model/Lotto.kt +++ b/src/main/kotlin/lotto/model/Lotto.kt @@ -17,8 +17,8 @@ class Lotto(private val numbers: List) { val numbersForCheck = numbers.toSet() val lottoResult = LottoMatchResult() - repeat(resultNumbers.size - 1) { - if (it in numbersForCheck) lottoResult.matchNumbersCount += 1 + repeat(resultNumbers.size - 1) { idx -> + if (resultNumbers[idx] in numbersForCheck) lottoResult.matchNumbersCount += 1 } if (resultNumbers.last() in numbersForCheck) lottoResult.isMatchBonus = true From 5f81f9317d97fa037042a4d4e897129886812814 Mon Sep 17 00:00:00 2001 From: kimgwon <92065911+kimgwon@users.noreply.github.com> Date: Mon, 4 Nov 2024 23:09:41 +0900 Subject: [PATCH 20/24] feat(Output): Add printLottoNumbers() to show lotto numbers --- src/main/kotlin/lotto/view/Output.kt | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/lotto/view/Output.kt b/src/main/kotlin/lotto/view/Output.kt index 2ce880b7c..d3b5d9f5a 100644 --- a/src/main/kotlin/lotto/view/Output.kt +++ b/src/main/kotlin/lotto/view/Output.kt @@ -1,5 +1,13 @@ package lotto.view +import lotto.model.Lotto + class Output { - fun printLottoNumbers(numbers: List) = println(numbers) + fun printLottoNumbers(lottos: List) { + println("${lottos.size}개를 구매했습니다.") + repeat(lottos.size) { idx -> + println(lottos[idx].getLottoNumbers()) + } + println() + } } \ No newline at end of file From 61442b0f73a4806f1b71029e158def005f3e3cd4 Mon Sep 17 00:00:00 2001 From: kimgwon <92065911+kimgwon@users.noreply.github.com> Date: Mon, 4 Nov 2024 23:10:17 +0900 Subject: [PATCH 21/24] feat(Output): Add printTotalLottoPrize() to show total lotto prize --- src/main/kotlin/lotto/view/Output.kt | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/main/kotlin/lotto/view/Output.kt b/src/main/kotlin/lotto/view/Output.kt index d3b5d9f5a..03e1c7c1a 100644 --- a/src/main/kotlin/lotto/view/Output.kt +++ b/src/main/kotlin/lotto/view/Output.kt @@ -1,6 +1,7 @@ package lotto.view import lotto.model.Lotto +import lotto.model.LottoTotalMatchResult class Output { fun printLottoNumbers(lottos: List) { @@ -10,4 +11,15 @@ class Output { } println() } + + fun printTotalLottoPrize(totalMatchResult: LottoTotalMatchResult, rateOfReturn: Float) { + println("당첨 통계") + println("---") + println("3개 일치 (5,000원) - ${totalMatchResult.prizeCount5}개") + println("4개 일치 (50,000원) - ${totalMatchResult.prizeCount4}개") + println("5개 일치 (1,500,000원) - ${totalMatchResult.prizeCount3}개") + println("5개 일치, 보너스 볼 일치 (30,000,000원) - ${totalMatchResult.prizeCount2}개") + println("6개 일치 (2,000,000,000원) - ${totalMatchResult.prizeCount1}개") + println("총 수익률은 ${rateOfReturn}%입니다.") + } } \ No newline at end of file From 4afc986f4a0f360b10467ece729d83c30468c062 Mon Sep 17 00:00:00 2001 From: kimgwon <92065911+kimgwon@users.noreply.github.com> Date: Mon, 4 Nov 2024 23:45:09 +0900 Subject: [PATCH 22/24] refactor: Extract common constant --- src/main/kotlin/lotto/constant/Message.kt | 13 +++++++++++++ src/main/kotlin/lotto/controller/LottoController.kt | 3 ++- src/main/kotlin/lotto/view/Input.kt | 12 ++++++++---- src/main/kotlin/lotto/view/Output.kt | 7 ++++--- 4 files changed, 27 insertions(+), 8 deletions(-) create mode 100644 src/main/kotlin/lotto/constant/Message.kt diff --git a/src/main/kotlin/lotto/constant/Message.kt b/src/main/kotlin/lotto/constant/Message.kt new file mode 100644 index 000000000..a1895a702 --- /dev/null +++ b/src/main/kotlin/lotto/constant/Message.kt @@ -0,0 +1,13 @@ +package lotto.constant + +object Message { + const val INFO_GET_PURCHASE_AMOUNT = "구입금액을 입력해 주세요." + const val INFO_GET_LOTTO_NUMBERS = "당첨 번호를 입력해 주세요." + const val INFO_GET_LOTTO_BONUS_NUMBERS = "보너스 번호를 입력해주세요." + + const val TICKET_PRICE = 1000 + + + const val LOTTO_RESULT_MESSAGE = "당첨 통계\n---" + const val LOTTO_RATE_OF_RETURN = "총 수익률은 %f%입니다." +} \ No newline at end of file diff --git a/src/main/kotlin/lotto/controller/LottoController.kt b/src/main/kotlin/lotto/controller/LottoController.kt index cb76f3eb2..e110f183d 100644 --- a/src/main/kotlin/lotto/controller/LottoController.kt +++ b/src/main/kotlin/lotto/controller/LottoController.kt @@ -1,6 +1,7 @@ package lotto.controller import camp.nextstep.edu.missionutils.Randoms.pickUniqueNumbersInRange +import lotto.constant.Message.TICKET_PRICE import lotto.model.Lotto import lotto.model.LottoTotalMatchResult import lotto.validation.InputValidation @@ -12,7 +13,7 @@ class LottoController(val input: Input, val output: Output) { private val validator = InputValidation() private fun getLottoTicketCount(amount: Int): Int { - return amount / 1000 + return amount / TICKET_PRICE } private fun createRandomLotto(): Lotto { diff --git a/src/main/kotlin/lotto/view/Input.kt b/src/main/kotlin/lotto/view/Input.kt index e03cc0a9c..75f85ee32 100644 --- a/src/main/kotlin/lotto/view/Input.kt +++ b/src/main/kotlin/lotto/view/Input.kt @@ -1,16 +1,20 @@ package lotto.view import camp.nextstep.edu.missionutils.Console +import lotto.constant.Message.INFO_GET_LOTTO_BONUS_NUMBERS +import lotto.constant.Message.INFO_GET_LOTTO_NUMBERS +import lotto.constant.Message.INFO_GET_PURCHASE_AMOUNT +import lotto.constant.Message.TICKET_PRICE import lotto.validation.InputValidation class Input { private val inputValidation = InputValidation() fun getPurchaseAmount(): Int { - println("구입금액을 입력해 주세요.") + println(INFO_GET_PURCHASE_AMOUNT) val value = readLine().let { inputValidation.typeInt(it).also { amount -> - inputValidation.unitOfNumber(amount, 1000) + inputValidation.unitOfNumber(amount, TICKET_PRICE) } } println() @@ -18,7 +22,7 @@ class Input { } fun getLottoNumbers(): List { - println("당첨 번호를 입력해 주세요.") + println(INFO_GET_LOTTO_NUMBERS) val value = readLine().let { inputValidation.lottoNumbers(inputValidation.lottoNumbersDelimiter(it)) } @@ -27,7 +31,7 @@ class Input { } fun getLottoBonusNumber(): Int { - println("보너스 번호를 입력해주세요.") + println(INFO_GET_LOTTO_BONUS_NUMBERS) val value = readLine().let { inputValidation.typeInt(it).also { number -> inputValidation.lottoNumberRange(number) diff --git a/src/main/kotlin/lotto/view/Output.kt b/src/main/kotlin/lotto/view/Output.kt index 03e1c7c1a..1497da6a2 100644 --- a/src/main/kotlin/lotto/view/Output.kt +++ b/src/main/kotlin/lotto/view/Output.kt @@ -1,5 +1,7 @@ package lotto.view +import lotto.constant.Message.LOTTO_RATE_OF_RETURN +import lotto.constant.Message.LOTTO_RESULT_MESSAGE import lotto.model.Lotto import lotto.model.LottoTotalMatchResult @@ -13,13 +15,12 @@ class Output { } fun printTotalLottoPrize(totalMatchResult: LottoTotalMatchResult, rateOfReturn: Float) { - println("당첨 통계") - println("---") + println(LOTTO_RESULT_MESSAGE) println("3개 일치 (5,000원) - ${totalMatchResult.prizeCount5}개") println("4개 일치 (50,000원) - ${totalMatchResult.prizeCount4}개") println("5개 일치 (1,500,000원) - ${totalMatchResult.prizeCount3}개") println("5개 일치, 보너스 볼 일치 (30,000,000원) - ${totalMatchResult.prizeCount2}개") println("6개 일치 (2,000,000,000원) - ${totalMatchResult.prizeCount1}개") - println("총 수익률은 ${rateOfReturn}%입니다.") + println(LOTTO_RATE_OF_RETURN.format(rateOfReturn)) } } \ No newline at end of file From d0171b62ee271d6a9c7959b1d3b4b57ded8b56f4 Mon Sep 17 00:00:00 2001 From: kimgwon <92065911+kimgwon@users.noreply.github.com> Date: Mon, 4 Nov 2024 23:45:28 +0900 Subject: [PATCH 23/24] test(LottoTest): Create test --- src/test/kotlin/lotto/LottoTest.kt | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/test/kotlin/lotto/LottoTest.kt b/src/test/kotlin/lotto/LottoTest.kt index 122fae572..3703b1d62 100644 --- a/src/test/kotlin/lotto/LottoTest.kt +++ b/src/test/kotlin/lotto/LottoTest.kt @@ -1,5 +1,6 @@ package lotto +import lotto.model.Lotto import org.junit.jupiter.api.Test import org.junit.jupiter.api.assertThrows @@ -20,4 +21,17 @@ class LottoTest { } // TODO: 추가 기능 구현에 따른 테스트 코드 작성 + @Test + fun `로또 번호가 1보다 작은 숫자면 예외가 발생한다`() { + assertThrows { + Lotto(listOf(0, 2, 3, 4, 5, 6)) + } + } + + @Test + fun `로또 번호가 45보다 큰 숫자면 예외가 발생한다`() { + assertThrows { + Lotto(listOf(1, 2, 3, 4, 5, 46)) + } + } } From ae72b415d140280d3d2a80a24573ffe7802b07dc Mon Sep 17 00:00:00 2001 From: kimgwon <92065911+kimgwon@users.noreply.github.com> Date: Mon, 4 Nov 2024 23:59:24 +0900 Subject: [PATCH 24/24] refactor(Error): Extract common constant --- src/main/kotlin/lotto/constant/Error.kt | 9 +++++++ src/main/kotlin/lotto/constant/Message.kt | 4 ++- .../lotto/validation/InputValidation.kt | 25 ++++++++++++------- src/main/kotlin/lotto/view/Input.kt | 2 +- 4 files changed, 29 insertions(+), 11 deletions(-) create mode 100644 src/main/kotlin/lotto/constant/Error.kt diff --git a/src/main/kotlin/lotto/constant/Error.kt b/src/main/kotlin/lotto/constant/Error.kt new file mode 100644 index 000000000..aa80ce682 --- /dev/null +++ b/src/main/kotlin/lotto/constant/Error.kt @@ -0,0 +1,9 @@ +package lotto.constant + +object Error { + const val NOT_NUMBER = "[ERROR] 숫자를 입력해주세요. %s은 숫자가 아닙니다." + const val UNIT_OF_PRICE = "[ERROR] %d원 단위로 입력해주세요." + const val LOTTO_RANGE = "[ERROR] 로또 번호는 1부터 45 사이의 숫자여야 합니다." + const val LOTTO_SIZE = "[ERROR] 당첨 번호는 6개의 숫자로 입력해주세요." + const val LOTTO_DUPLICATE = "[ERROR] 번호는 중복되면 안됩니다." +} \ No newline at end of file diff --git a/src/main/kotlin/lotto/constant/Message.kt b/src/main/kotlin/lotto/constant/Message.kt index a1895a702..fc09f72a9 100644 --- a/src/main/kotlin/lotto/constant/Message.kt +++ b/src/main/kotlin/lotto/constant/Message.kt @@ -5,8 +5,10 @@ object Message { const val INFO_GET_LOTTO_NUMBERS = "당첨 번호를 입력해 주세요." const val INFO_GET_LOTTO_BONUS_NUMBERS = "보너스 번호를 입력해주세요." + const val COMMA = "," + const val TICKET_PRICE = 1000 - + const val LOTTO_RESULT_MESSAGE = "당첨 통계\n---" const val LOTTO_RATE_OF_RETURN = "총 수익률은 %f%입니다." diff --git a/src/main/kotlin/lotto/validation/InputValidation.kt b/src/main/kotlin/lotto/validation/InputValidation.kt index ced23bc1e..12e92296d 100644 --- a/src/main/kotlin/lotto/validation/InputValidation.kt +++ b/src/main/kotlin/lotto/validation/InputValidation.kt @@ -1,34 +1,41 @@ package lotto.validation +import lotto.constant.Error.LOTTO_DUPLICATE +import lotto.constant.Error.LOTTO_RANGE +import lotto.constant.Error.LOTTO_SIZE +import lotto.constant.Error.NOT_NUMBER +import lotto.constant.Error.UNIT_OF_PRICE +import lotto.constant.Message.COMMA + class InputValidation { fun typeInt(input: String): Int { - return input.toIntOrNull() ?: throw IllegalArgumentException("숫자를 입력해주세요. ${input}은 숫자가 아닙니다.") + return input.toIntOrNull() ?: throw IllegalArgumentException(NOT_NUMBER.format(input)) } - fun unitOfNumber(number: Int, unit: Int): Int { - if (number % unit == 0) - return number - throw IllegalArgumentException("${unit}원 단위로 입력해주세요.") + fun unitOfPrice(price: Int, unit: Int): Int { + if (price % unit == 0) + return price + throw IllegalArgumentException(UNIT_OF_PRICE.format(unit)) } fun lottoNumbersDelimiter(input: String): List { - return input.split(",").map { + return input.split(COMMA).map { typeInt(it) } } fun lottoNumberRange(number: Int): Int { - require(number in 1..45) { "로또 번호는 1부터 45 사이의 숫자여야 합니다." } + require(number in 1..45) { LOTTO_RANGE } return number } private fun lottoNumbersSize(numbers: List): List { - require(numbers.size == 6) { "당첨 번호는 6개의 숫자로 입력해주세요." } + require(numbers.size == 6) { LOTTO_SIZE } return numbers } fun lottoNumbersNotDuplicate(numbers: List): List { - require(numbers.toSet().size == numbers.size) { "번호는 중복되면 안됩니다." } + require(numbers.toSet().size == numbers.size) { LOTTO_DUPLICATE } return numbers } diff --git a/src/main/kotlin/lotto/view/Input.kt b/src/main/kotlin/lotto/view/Input.kt index 75f85ee32..22573af54 100644 --- a/src/main/kotlin/lotto/view/Input.kt +++ b/src/main/kotlin/lotto/view/Input.kt @@ -14,7 +14,7 @@ class Input { println(INFO_GET_PURCHASE_AMOUNT) val value = readLine().let { inputValidation.typeInt(it).also { amount -> - inputValidation.unitOfNumber(amount, TICKET_PRICE) + inputValidation.unitOfPrice(amount, TICKET_PRICE) } } println()