From 9edc1beedbe4e73bb697d209c0830acab872ca49 Mon Sep 17 00:00:00 2001 From: dajeong Date: Tue, 6 Dec 2022 22:56:26 +0900 Subject: [PATCH 01/21] =?UTF-8?q?[step4]=203=EB=8B=A8=EA=B3=84=20=EB=A6=AC?= =?UTF-8?q?=EB=B7=B0=20=EB=B0=98=EC=98=81=20-=20=EC=83=9D=EC=84=B1?= =?UTF-8?q?=EC=9E=90=20=EA=B8=B0=EB=B3=B8=EA=B0=92?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - LottoRank의 기본값 설정 --- src/main/kotlin/lotto/domain/LottoRank.kt | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/main/kotlin/lotto/domain/LottoRank.kt b/src/main/kotlin/lotto/domain/LottoRank.kt index f0be7906b7..f5ce16e85b 100644 --- a/src/main/kotlin/lotto/domain/LottoRank.kt +++ b/src/main/kotlin/lotto/domain/LottoRank.kt @@ -2,15 +2,15 @@ package lotto.domain enum class LottoRank( val hitCount: Int, - val hasBonusNumber: Boolean, - val prizeMoney: Int + val prizeMoney: Int, + val hasBonusNumber: Boolean = false ) { - FIRST(6, false, 2000000000), - SECOND(5, true, 30000000), - THIRD(5, false, 1500000), - FOURTH(4, false, 50000), - FIFTH(3, false, 5000), - MISS(0, false, 0); + FIRST(hitCount = 6, prizeMoney = 2000000000), + SECOND(hitCount = 5, prizeMoney = 30000000, hasBonusNumber = true), + THIRD(hitCount = 5, prizeMoney = 1500000), + FOURTH(hitCount = 4, prizeMoney = 50000), + FIFTH(hitCount = 3, prizeMoney = 5000), + MISS(hitCount = 0, prizeMoney = 0); companion object { fun from(hitCount: Int, hasBonusNumber: Boolean): LottoRank { From f528f8bade28c451425e35656198c025c89510c3 Mon Sep 17 00:00:00 2001 From: dajeong Date: Tue, 6 Dec 2022 23:00:59 +0900 Subject: [PATCH 02/21] =?UTF-8?q?[step4]=203=EB=8B=A8=EA=B3=84=20=EB=A6=AC?= =?UTF-8?q?=EB=B7=B0=20=EB=B0=98=EC=98=81=20-=20companion=20object=20?= =?UTF-8?q?=ED=95=A8=EC=88=98=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - companion object 함수에서 일반함수로 --- src/main/kotlin/lotto/domain/LottoRank.kt | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/main/kotlin/lotto/domain/LottoRank.kt b/src/main/kotlin/lotto/domain/LottoRank.kt index f5ce16e85b..5f12dd7915 100644 --- a/src/main/kotlin/lotto/domain/LottoRank.kt +++ b/src/main/kotlin/lotto/domain/LottoRank.kt @@ -12,16 +12,16 @@ enum class LottoRank( FIFTH(hitCount = 3, prizeMoney = 5000), MISS(hitCount = 0, prizeMoney = 0); - companion object { - fun from(hitCount: Int, hasBonusNumber: Boolean): LottoRank { - return values().find { it.hitCount == hitCount && isHitBonusNumber(it, hasBonusNumber) } ?: return MISS + private fun isHitBonusNumber(it: LottoRank, hasBonusNumber: Boolean): Boolean { + if (it.hasBonusNumber) { + return hasBonusNumber } + return true + } - private fun isHitBonusNumber(it: LottoRank, hasBonusNumber: Boolean): Boolean { - if (it.hasBonusNumber) { - return hasBonusNumber - } - return true + companion object { + fun from(hitCount: Int, hasBonusNumber: Boolean): LottoRank { + return values().find { it.hitCount == hitCount && it.isHitBonusNumber(it, hasBonusNumber) } ?: return MISS } fun winRanks(): List { From 401a85d65ad7047836cdca918a55008820d76b66 Mon Sep 17 00:00:00 2001 From: dajeong Date: Tue, 6 Dec 2022 23:04:47 +0900 Subject: [PATCH 03/21] =?UTF-8?q?[step4]=203=EB=8B=A8=EA=B3=84=20=EB=A6=AC?= =?UTF-8?q?=EB=B7=B0=20=EB=B0=98=EC=98=81=20-=20=EB=B9=84=ED=9A=A8?= =?UTF-8?q?=EC=9C=A8=EC=A0=81=EC=9D=B8=20=EC=BD=94=EB=93=9C=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 함수 실행할때마다 생성되는 객체를 생성자로 --- src/main/kotlin/lotto/domain/LottoResultService.kt | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/lotto/domain/LottoResultService.kt b/src/main/kotlin/lotto/domain/LottoResultService.kt index 5e390438ff..14db85d36b 100644 --- a/src/main/kotlin/lotto/domain/LottoResultService.kt +++ b/src/main/kotlin/lotto/domain/LottoResultService.kt @@ -1,10 +1,11 @@ package lotto.domain class LottoResultService( - private val luckyNumbers: LuckyNumbers, + luckyNumbers: LuckyNumbers, ) { + private val lottoWinner = LottoWinner(luckyNumbers) + fun inquireStatistics(payment: Int, lottoList: List): LottoStatisticsTotal { - val lottoWinner = LottoWinner(luckyNumbers) val winLottoList = lottoWinner.findWinLottoList(lottoList) val lottoStatisticsService = LottoStatisticsService(payment, winLottoList) return lottoStatisticsService.statistics() From e093e7d3a2533f229a37711703bfb7d331719ed9 Mon Sep 17 00:00:00 2001 From: dajeong Date: Tue, 6 Dec 2022 23:06:48 +0900 Subject: [PATCH 04/21] =?UTF-8?q?[step4]=203=EB=8B=A8=EA=B3=84=20=EB=A6=AC?= =?UTF-8?q?=EB=B7=B0=20=EB=B0=98=EC=98=81=20-=20=EB=B6=88=ED=95=84?= =?UTF-8?q?=EC=9A=94=ED=95=9C=20=EB=B3=80=EC=88=98=20=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/lotto/domain/LottoStatistics.kt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/kotlin/lotto/domain/LottoStatistics.kt b/src/main/kotlin/lotto/domain/LottoStatistics.kt index f0172ef29c..454c1a9a54 100644 --- a/src/main/kotlin/lotto/domain/LottoStatistics.kt +++ b/src/main/kotlin/lotto/domain/LottoStatistics.kt @@ -5,8 +5,7 @@ import lotto.util.NumberUtil class LottoStatistics( private val winLottoList: List ) { - private val prizeList: List = winLottoList.map { it.prizeMoney } - private val totalPrize: Int = prizeList.sum() + private val totalPrize: Int = winLottoList.sumOf { it.prizeMoney } fun earningRate(inputPayment: Int): Double { val earningRate = totalPrize.toDouble() / inputPayment.toDouble() From f9d9dca46706bb0d6f1fb1781b86bdc3ab3575ed Mon Sep 17 00:00:00 2001 From: dajeong Date: Thu, 8 Dec 2022 22:48:42 +0900 Subject: [PATCH 05/21] =?UTF-8?q?[step4]=204=EB=8B=A8=EA=B3=84=20=EA=B8=B0?= =?UTF-8?q?=EB=8A=A5=EB=AA=A9=EB=A1=9D=20TODO=20=EC=A0=95=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/lotto/README.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/main/kotlin/lotto/README.md b/src/main/kotlin/lotto/README.md index 8fd8e215cd..943bc8ae68 100644 --- a/src/main/kotlin/lotto/README.md +++ b/src/main/kotlin/lotto/README.md @@ -1,5 +1,18 @@ # TODO +## 구현할 '로또(수동)' 기능 목록 +- [ ] 수동으로 구매할 로또 수 입력 기능 +- [ ] 수동으로 구매할 로또 번호 입력 기능 +- [ ] 수동 로또 생성 기능 +- [ ] 지불한 금액 클래스 생성 +- [ ] 로또 번호 클래스 생성 +- [ ] 로또 당첨금 클래스 생성 +- [ ] 로또 당첨 번호 개수와 당첨 로또 개수를 위한 개수 클래스 생성 +- [ ] 수익률 클래스 생성 +- [ ] 숫자를 추상화한 클래스 생성 (로또 번호의 상위 타입) +- [ ] 입력 문자열 클래스 생성 +- [ ] 예외 케이스 검토하여 처리 + ## 구현할 '로또(2등)' 기능 목록 - [x] 보너스 볼 입력 기능 - [x] 당첨 통계에 2등 결과 추가 From d9d7caead00b22e0ff0118c2ef4d37a88e5e2a93 Mon Sep 17 00:00:00 2001 From: dajeong Date: Thu, 8 Dec 2022 22:54:43 +0900 Subject: [PATCH 06/21] =?UTF-8?q?[step4]=20=EC=A7=80=EB=B6=88=20=EA=B8=88?= =?UTF-8?q?=EC=95=A1=20=ED=81=B4=EB=9E=98=EC=8A=A4=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 지불 금액에 대한 검증 코드 추가 --- src/main/kotlin/lotto/README.md | 2 +- src/main/kotlin/lotto/domain/LottoResultService.kt | 2 +- src/main/kotlin/lotto/domain/LottoShop.kt | 6 +++--- src/main/kotlin/lotto/domain/LottoStatistics.kt | 4 ++-- src/main/kotlin/lotto/domain/LottoStatisticsService.kt | 2 +- src/main/kotlin/lotto/domain/Payment.kt | 9 +++++++++ src/main/kotlin/lotto/view/InputView.kt | 5 +++-- src/test/kotlin/lotto/domain/LottoShopTest.kt | 6 +++--- .../kotlin/lotto/domain/LottoStatisticsServiceTest.kt | 2 +- src/test/kotlin/lotto/domain/LottoStatisticsTest.kt | 6 +++--- 10 files changed, 27 insertions(+), 17 deletions(-) create mode 100644 src/main/kotlin/lotto/domain/Payment.kt diff --git a/src/main/kotlin/lotto/README.md b/src/main/kotlin/lotto/README.md index 943bc8ae68..35626fd835 100644 --- a/src/main/kotlin/lotto/README.md +++ b/src/main/kotlin/lotto/README.md @@ -4,7 +4,7 @@ - [ ] 수동으로 구매할 로또 수 입력 기능 - [ ] 수동으로 구매할 로또 번호 입력 기능 - [ ] 수동 로또 생성 기능 -- [ ] 지불한 금액 클래스 생성 +- [x] 지불한 금액 클래스 생성 - [ ] 로또 번호 클래스 생성 - [ ] 로또 당첨금 클래스 생성 - [ ] 로또 당첨 번호 개수와 당첨 로또 개수를 위한 개수 클래스 생성 diff --git a/src/main/kotlin/lotto/domain/LottoResultService.kt b/src/main/kotlin/lotto/domain/LottoResultService.kt index 14db85d36b..aba1eb1c08 100644 --- a/src/main/kotlin/lotto/domain/LottoResultService.kt +++ b/src/main/kotlin/lotto/domain/LottoResultService.kt @@ -5,7 +5,7 @@ class LottoResultService( ) { private val lottoWinner = LottoWinner(luckyNumbers) - fun inquireStatistics(payment: Int, lottoList: List): LottoStatisticsTotal { + fun inquireStatistics(payment: Payment, lottoList: List): LottoStatisticsTotal { val winLottoList = lottoWinner.findWinLottoList(lottoList) val lottoStatisticsService = LottoStatisticsService(payment, winLottoList) return lottoStatisticsService.statistics() diff --git a/src/main/kotlin/lotto/domain/LottoShop.kt b/src/main/kotlin/lotto/domain/LottoShop.kt index 550fb1302d..18ba9273ce 100644 --- a/src/main/kotlin/lotto/domain/LottoShop.kt +++ b/src/main/kotlin/lotto/domain/LottoShop.kt @@ -3,13 +3,13 @@ package lotto.domain class LottoShop( private val lottoGenerator: LottoGenerator ) { - fun buyLotto(inputPayment: Int): List { + fun buyLotto(inputPayment: Payment): List { val lottoCount = calculateLottoCount(inputPayment) return lottoGenerator.generate(lottoCount) } - private fun calculateLottoCount(payment: Int): Int { - return payment / LOTTO_PRICE + private fun calculateLottoCount(payment: Payment): Int { + return payment.payment / LOTTO_PRICE } companion object { diff --git a/src/main/kotlin/lotto/domain/LottoStatistics.kt b/src/main/kotlin/lotto/domain/LottoStatistics.kt index 454c1a9a54..63592a0b92 100644 --- a/src/main/kotlin/lotto/domain/LottoStatistics.kt +++ b/src/main/kotlin/lotto/domain/LottoStatistics.kt @@ -7,8 +7,8 @@ class LottoStatistics( ) { private val totalPrize: Int = winLottoList.sumOf { it.prizeMoney } - fun earningRate(inputPayment: Int): Double { - val earningRate = totalPrize.toDouble() / inputPayment.toDouble() + fun earningRate(inputPayment: Payment): Double { + val earningRate = totalPrize.toDouble() / inputPayment.payment.toDouble() return NumberUtil.floor(earningRate, EARNING_RATE_DECIMAL_PLACE) } diff --git a/src/main/kotlin/lotto/domain/LottoStatisticsService.kt b/src/main/kotlin/lotto/domain/LottoStatisticsService.kt index acc58d1051..f6f5dbb6a1 100644 --- a/src/main/kotlin/lotto/domain/LottoStatisticsService.kt +++ b/src/main/kotlin/lotto/domain/LottoStatisticsService.kt @@ -1,7 +1,7 @@ package lotto.domain class LottoStatisticsService( - private val payment: Int, + private val payment: Payment, private val winLottoList: List ) { fun statistics(): LottoStatisticsTotal { diff --git a/src/main/kotlin/lotto/domain/Payment.kt b/src/main/kotlin/lotto/domain/Payment.kt new file mode 100644 index 0000000000..b2448ce2a8 --- /dev/null +++ b/src/main/kotlin/lotto/domain/Payment.kt @@ -0,0 +1,9 @@ +package lotto.domain + +class Payment( + val payment: Int +) { + init { + require(payment >= 0) { "지불액은 음수가 될 수 없습니다." } + } +} diff --git a/src/main/kotlin/lotto/view/InputView.kt b/src/main/kotlin/lotto/view/InputView.kt index 1053b55a6f..eaa520bab1 100644 --- a/src/main/kotlin/lotto/view/InputView.kt +++ b/src/main/kotlin/lotto/view/InputView.kt @@ -1,14 +1,15 @@ package lotto.view +import lotto.domain.Payment import lotto.util.StringValidator class InputView { - fun inputPayment(): Int { + fun inputPayment(): Payment { println(INPUT_PAYMENT_GUIDE) val payment = readln() validatePaymentInput(payment) - return payment.toInt() + return Payment(payment.toInt()) } private fun validatePaymentInput(payment: String) { diff --git a/src/test/kotlin/lotto/domain/LottoShopTest.kt b/src/test/kotlin/lotto/domain/LottoShopTest.kt index e7547dfbf0..01dcf795dc 100644 --- a/src/test/kotlin/lotto/domain/LottoShopTest.kt +++ b/src/test/kotlin/lotto/domain/LottoShopTest.kt @@ -13,9 +13,9 @@ class LottoShopTest : StringSpec({ "로또 구매 테스트" { forAll( // given - row("0원을 내면 0개를 반환한다.", 0, 0), - row("5000원을 내면 5개를 반환한다.", 5000, 5), - row("5500원을 내면 5개를 반환한다.", 5500, 5) + row("0원을 내면 0개를 반환한다.", Payment(0), 0), + row("5000원을 내면 5개를 반환한다.", Payment(5000), 5), + row("5500원을 내면 5개를 반환한다.", Payment(5500), 5) ) { title, payment, expectedSize -> // when val actual = lottoShop.buyLotto(payment) diff --git a/src/test/kotlin/lotto/domain/LottoStatisticsServiceTest.kt b/src/test/kotlin/lotto/domain/LottoStatisticsServiceTest.kt index 2415d14d82..baea82e0a1 100644 --- a/src/test/kotlin/lotto/domain/LottoStatisticsServiceTest.kt +++ b/src/test/kotlin/lotto/domain/LottoStatisticsServiceTest.kt @@ -10,7 +10,7 @@ class LottoStatisticsServiceTest : StringSpec({ "당첨자 통계 통합 결과 테스트" { // given - val payment = 15000 + val payment = Payment(15000) forAll( row( LottoRank.FOURTH, diff --git a/src/test/kotlin/lotto/domain/LottoStatisticsTest.kt b/src/test/kotlin/lotto/domain/LottoStatisticsTest.kt index caef33336c..7d951a49b4 100644 --- a/src/test/kotlin/lotto/domain/LottoStatisticsTest.kt +++ b/src/test/kotlin/lotto/domain/LottoStatisticsTest.kt @@ -8,9 +8,9 @@ import io.kotest.matchers.shouldBe class LottoStatisticsTest : StringSpec({ "로또 수익률 계산 테스트" { forAll( - row(listOf(LottoRank.FOURTH, LottoRank.FIFTH), 100000, 0.55), - row(listOf(LottoRank.FIFTH), 5000, 1.0), - row(listOf(LottoRank.FOURTH), 5000, 10), + row(listOf(LottoRank.FOURTH, LottoRank.FIFTH), Payment(100000), 0.55), + row(listOf(LottoRank.FIFTH), Payment(5000), 1.0), + row(listOf(LottoRank.FOURTH), Payment(5000), 10), ) { prizeList, payment, expectedEarningRate -> // given val lottoStatistics = LottoStatistics(prizeList) From 56aba68d1b4efa6b2f5695745c66489151b0b3b1 Mon Sep 17 00:00:00 2001 From: dajeong Date: Mon, 12 Dec 2022 09:24:56 +0900 Subject: [PATCH 07/21] =?UTF-8?q?[step4]=204=EB=8B=A8=EA=B3=84=20=EA=B3=BC?= =?UTF-8?q?=EC=A0=9C=20=EB=82=B4=EC=9A=A9=20readme=EC=97=90=20=EC=9E=91?= =?UTF-8?q?=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/README.md b/README.md index 0ef22f5718..cbabd2a9b6 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,79 @@ # kotlin-lotto +# 🚀 4단계 - 로또(수동) + +## 기능 요구사항 +- 현재 로또 생성기는 자동 생성 기능만 제공한다. 사용자가 수동으로 추첨 번호를 입력할 수 있도록 해야 한다. +- 입력한 금액, 자동 생성 숫자, 수동 생성 번호를 입력하도록 해야 한다. + +## 실행 결과 +``` +구입금액을 입력해 주세요. +14000 + +수동으로 구매할 로또 수를 입력해 주세요. +3 + +수동으로 구매할 번호를 입력해 주세요. +8, 21, 23, 41, 42, 43 +3, 5, 11, 16, 32, 38 +7, 11, 16, 35, 36, 44 + +수동으로 3장, 자동으로 11개를 구매했습니다. +[8, 21, 23, 41, 42, 43] +[3, 5, 11, 16, 32, 38] +[7, 11, 16, 35, 36, 44] +[1, 8, 11, 31, 41, 42] +[13, 14, 16, 38, 42, 45] +[7, 11, 30, 40, 42, 43] +[2, 13, 22, 32, 38, 45] +[23, 25, 33, 36, 39, 41] +[1, 3, 5, 14, 22, 45] +[5, 9, 38, 41, 43, 44] +[2, 8, 9, 18, 19, 21] +[13, 14, 18, 21, 23, 35] +[17, 21, 29, 37, 42, 45] +[3, 8, 27, 30, 35, 44] + +지난 주 당첨 번호를 입력해 주세요. +1, 2, 3, 4, 5, 6 +보너스 볼을 입력해 주세요. +7 + +당첨 통계 +--------- +3개 일치 (5000원)- 1개 +4개 일치 (50000원)- 0개 +5개 일치 (1500000원)- 0개 +5개 일치, 보너스 볼 일치(30000000원) - 0개 +6개 일치 (2000000000원)- 0개 +총 수익률은 0.35입니다.(기준이 1이기 때문에 결과적으로 손해라는 의미임) +``` + +## 프로그래밍 요구 사항 +- 모든 원시값과 문자열을 포장한다. +- 예외 처리를 통해 에러가 발생하지 않도록 한다. +- 모든 기능을 TDD로 구현해 단위 테스트가 존재해야 한다. 단, UI(System.out, System.in) 로직은 제외 +- Enum 클래스를 적용해 프로그래밍을 구현한다. +- 일급 컬렉션을 쓴다. +- indent(인덴트, 들여쓰기) depth를 2를 넘지 않도록 구현한다. 1까지만 허용한다. +- 예를 들어 while문 안에 if문이 있으면 들여쓰기는 2이다. +- 함수(또는 메서드)가 한 가지 일만 잘 하도록 구현한다. +- 기능을 구현하기 전에 README.md 파일에 구현할 기능 목록을 정리해 추가한다. +- git의 commit 단위는 앞 단계에서 README.md 파일에 정리한 기능 목록 단위로 추가한다. + +## 힌트 +- 모든 원시값과 문자열을 포장한다. +- 로또 숫자 하나는 Int다. 이 숫자 하나를 추상화한 LottoNumber를 추가해 구현한다. +- 예외 처리를 통해 에러가 발생하지 않도록 한다. +- 참고로 코틀린에서는 아래와 같이 예외 처리를 한다. 장기적으로는 아래와 같이 예외 처리하는 걸 연습해 본다. +- 논리적인 오류일 때만 예외를 던진다. +- 논리적인 오류가 아니면 예외를 던지지 말고 null을 반환한다. +- 실패하는 경우가 복잡해서 null로 처리할 수 없으면 sealed class를 반환한다. +- 일반적인 코틀린 코드에서 try-catch를 사용하지 않는다. + +--- + # 🚀 3단계 - 로또(2등) ## 기능 요구사항 From 77605aaf28a35819345ee0c4a160f3add88ba438 Mon Sep 17 00:00:00 2001 From: dajeong Date: Mon, 12 Dec 2022 09:28:13 +0900 Subject: [PATCH 08/21] =?UTF-8?q?[step4]=20=EB=A1=9C=EB=98=90=20=EB=B2=88?= =?UTF-8?q?=ED=98=B8=20=ED=81=B4=EB=9E=98=EC=8A=A4=20=EC=83=9D=EC=84=B1,?= =?UTF-8?q?=20Int=20=ED=83=80=EC=9E=85=EC=9D=84=20=ED=8F=AC=EC=9E=A5?= =?UTF-8?q?=ED=95=A0=20Number=20=ED=81=B4=EB=9E=98=EC=8A=A4=20=EC=83=9D?= =?UTF-8?q?=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/lotto/README.md | 3 +- .../kotlin/lotto/application/common/Number.kt | 17 ++++ src/main/kotlin/lotto/domain/Lotto.kt | 2 +- .../kotlin/lotto/domain/LottoGenerator.kt | 8 +- src/main/kotlin/lotto/domain/LottoNumber.kt | 17 ++++ src/main/kotlin/lotto/domain/LottoRank.kt | 22 +++-- src/main/kotlin/lotto/domain/LottoShop.kt | 6 +- .../kotlin/lotto/domain/LottoStatistics.kt | 5 +- .../lotto/domain/LottoStatisticsResult.kt | 4 +- src/main/kotlin/lotto/domain/LottoWinner.kt | 2 +- src/main/kotlin/lotto/domain/LuckyNumbers.kt | 23 +++-- src/main/kotlin/lotto/domain/Payment.kt | 6 +- src/main/kotlin/lotto/util/NumberGenerator.kt | 4 +- .../lotto/util/RandomNumberGenerator.kt | 7 +- src/main/kotlin/lotto/view/InputView.kt | 13 +-- .../kotlin/lotto/domain/LottoGeneratorTest.kt | 5 +- src/test/kotlin/lotto/domain/LottoShopTest.kt | 7 +- .../domain/LottoStatisticsServiceTest.kt | 23 ++--- .../lotto/domain/LottoStatisticsTest.kt | 21 ++-- src/test/kotlin/lotto/domain/LottoTest.kt | 11 ++- .../kotlin/lotto/domain/LottoWinnerTest.kt | 52 ++++++++-- .../kotlin/lotto/domain/LuckyNumbersTest.kt | 95 +++++++++++++++++-- ...orTest.kt => RandomNumberGeneratorTest.kt} | 4 +- 23 files changed, 275 insertions(+), 82 deletions(-) create mode 100644 src/main/kotlin/lotto/application/common/Number.kt create mode 100644 src/main/kotlin/lotto/domain/LottoNumber.kt rename src/test/kotlin/lotto/util/{NumberGeneratorTest.kt => RandomNumberGeneratorTest.kt} (85%) diff --git a/src/main/kotlin/lotto/README.md b/src/main/kotlin/lotto/README.md index 35626fd835..2f994e45d3 100644 --- a/src/main/kotlin/lotto/README.md +++ b/src/main/kotlin/lotto/README.md @@ -1,11 +1,12 @@ # TODO ## 구현할 '로또(수동)' 기능 목록 +- [ ] 모든 원시값과 문자열을 포장 - [ ] 수동으로 구매할 로또 수 입력 기능 - [ ] 수동으로 구매할 로또 번호 입력 기능 - [ ] 수동 로또 생성 기능 - [x] 지불한 금액 클래스 생성 -- [ ] 로또 번호 클래스 생성 +- [x] 로또 번호 클래스 생성 - [ ] 로또 당첨금 클래스 생성 - [ ] 로또 당첨 번호 개수와 당첨 로또 개수를 위한 개수 클래스 생성 - [ ] 수익률 클래스 생성 diff --git a/src/main/kotlin/lotto/application/common/Number.kt b/src/main/kotlin/lotto/application/common/Number.kt new file mode 100644 index 0000000000..0be82dcd8c --- /dev/null +++ b/src/main/kotlin/lotto/application/common/Number.kt @@ -0,0 +1,17 @@ +package lotto.application.common + +data class Number( + val number: Int +) { + fun isPositive(): Boolean { + return number > 0 + } + + fun isNegative(): Boolean { + return number < 0 + } + + fun toDouble(): Double { + return number.toDouble() + } +} diff --git a/src/main/kotlin/lotto/domain/Lotto.kt b/src/main/kotlin/lotto/domain/Lotto.kt index 3490cb738d..4426875f09 100644 --- a/src/main/kotlin/lotto/domain/Lotto.kt +++ b/src/main/kotlin/lotto/domain/Lotto.kt @@ -1,7 +1,7 @@ package lotto.domain class Lotto( - val numbers: List + val numbers: List ) { init { require(numbers.size == LOTTO_NUMBERS_SIZE) { "로또 번호는 ${LOTTO_NUMBERS_SIZE}개가 필요합니다." } diff --git a/src/main/kotlin/lotto/domain/LottoGenerator.kt b/src/main/kotlin/lotto/domain/LottoGenerator.kt index 02ca2248b7..19adfc5049 100644 --- a/src/main/kotlin/lotto/domain/LottoGenerator.kt +++ b/src/main/kotlin/lotto/domain/LottoGenerator.kt @@ -1,15 +1,17 @@ package lotto.domain +import lotto.application.common.Number import lotto.util.NumberGenerator class LottoGenerator( private val numberGenerator: NumberGenerator ) { - fun generate(size: Int): List { - return List(size) { Lotto(randomNumber()) } + fun generate(size: Number): List { + return List(size.number) { Lotto(randomNumber()) } } - private fun randomNumber(): List { + private fun randomNumber(): List { return numberGenerator.generate(Lotto.LOTTO_START_NUMBER, Lotto.LOTTO_END_NUMBER, Lotto.LOTTO_NUMBERS_SIZE) + .map { LottoNumber(it) } } } diff --git a/src/main/kotlin/lotto/domain/LottoNumber.kt b/src/main/kotlin/lotto/domain/LottoNumber.kt new file mode 100644 index 0000000000..b328246940 --- /dev/null +++ b/src/main/kotlin/lotto/domain/LottoNumber.kt @@ -0,0 +1,17 @@ +package lotto.domain + +import lotto.application.common.Number + +data class LottoNumber( + val number: Number +) { + + init { + require(number.number in LOTTO_MIN_NUMBER..LOTTO_MAX_NUMBER) { "로또 번호는 ${LOTTO_MIN_NUMBER}와 $LOTTO_MAX_NUMBER 사이 값 이여야 합니다." } + } + + companion object { + const val LOTTO_MIN_NUMBER = 1 + const val LOTTO_MAX_NUMBER = 45 + } +} diff --git a/src/main/kotlin/lotto/domain/LottoRank.kt b/src/main/kotlin/lotto/domain/LottoRank.kt index 5f12dd7915..5d0df3aaeb 100644 --- a/src/main/kotlin/lotto/domain/LottoRank.kt +++ b/src/main/kotlin/lotto/domain/LottoRank.kt @@ -1,16 +1,18 @@ package lotto.domain +import lotto.application.common.Number + enum class LottoRank( - val hitCount: Int, - val prizeMoney: Int, + val hitCount: Number, + val prizeMoney: Number, val hasBonusNumber: Boolean = false ) { - FIRST(hitCount = 6, prizeMoney = 2000000000), - SECOND(hitCount = 5, prizeMoney = 30000000, hasBonusNumber = true), - THIRD(hitCount = 5, prizeMoney = 1500000), - FOURTH(hitCount = 4, prizeMoney = 50000), - FIFTH(hitCount = 3, prizeMoney = 5000), - MISS(hitCount = 0, prizeMoney = 0); + FIRST(hitCount = Number(6), prizeMoney = Number(2000000000)), + SECOND(hitCount = Number(5), prizeMoney = Number(30000000), hasBonusNumber = true), + THIRD(hitCount = Number(5), prizeMoney = Number(1500000)), + FOURTH(hitCount = Number(4), prizeMoney = Number(50000)), + FIFTH(hitCount = Number(3), prizeMoney = Number(5000)), + MISS(hitCount = Number(0), prizeMoney = Number(0)); private fun isHitBonusNumber(it: LottoRank, hasBonusNumber: Boolean): Boolean { if (it.hasBonusNumber) { @@ -20,12 +22,12 @@ enum class LottoRank( } companion object { - fun from(hitCount: Int, hasBonusNumber: Boolean): LottoRank { + fun from(hitCount: Number, hasBonusNumber: Boolean): LottoRank { return values().find { it.hitCount == hitCount && it.isHitBonusNumber(it, hasBonusNumber) } ?: return MISS } fun winRanks(): List { - return values().filter { it.prizeMoney > 0 }.reversed() + return values().filter { it.prizeMoney.isPositive() }.reversed() } } } diff --git a/src/main/kotlin/lotto/domain/LottoShop.kt b/src/main/kotlin/lotto/domain/LottoShop.kt index 18ba9273ce..3acdc6aa37 100644 --- a/src/main/kotlin/lotto/domain/LottoShop.kt +++ b/src/main/kotlin/lotto/domain/LottoShop.kt @@ -1,5 +1,7 @@ package lotto.domain +import lotto.application.common.Number + class LottoShop( private val lottoGenerator: LottoGenerator ) { @@ -8,8 +10,8 @@ class LottoShop( return lottoGenerator.generate(lottoCount) } - private fun calculateLottoCount(payment: Payment): Int { - return payment.payment / LOTTO_PRICE + private fun calculateLottoCount(payment: Payment): Number { + return Number(payment.payment.number / LOTTO_PRICE) } companion object { diff --git a/src/main/kotlin/lotto/domain/LottoStatistics.kt b/src/main/kotlin/lotto/domain/LottoStatistics.kt index 63592a0b92..fa9c53deaf 100644 --- a/src/main/kotlin/lotto/domain/LottoStatistics.kt +++ b/src/main/kotlin/lotto/domain/LottoStatistics.kt @@ -1,11 +1,12 @@ package lotto.domain +import lotto.application.common.Number import lotto.util.NumberUtil class LottoStatistics( private val winLottoList: List ) { - private val totalPrize: Int = winLottoList.sumOf { it.prizeMoney } + private val totalPrize: Number = Number(winLottoList.sumOf { it.prizeMoney.number }) fun earningRate(inputPayment: Payment): Double { val earningRate = totalPrize.toDouble() / inputPayment.payment.toDouble() @@ -18,7 +19,7 @@ class LottoStatistics( return LottoRank.winRanks().map { LottoStatisticsResult( lottoRank = it, - winLottoCount = hitCountMap.getOrDefault(it, emptyList()).size + winLottoCount = Number(hitCountMap.getOrDefault(it, emptyList()).size) ) } } diff --git a/src/main/kotlin/lotto/domain/LottoStatisticsResult.kt b/src/main/kotlin/lotto/domain/LottoStatisticsResult.kt index 955a7464f4..d8aee5df9b 100644 --- a/src/main/kotlin/lotto/domain/LottoStatisticsResult.kt +++ b/src/main/kotlin/lotto/domain/LottoStatisticsResult.kt @@ -1,6 +1,8 @@ package lotto.domain +import lotto.application.common.Number + class LottoStatisticsResult( val lottoRank: LottoRank, - val winLottoCount: Int + val winLottoCount: Number ) diff --git a/src/main/kotlin/lotto/domain/LottoWinner.kt b/src/main/kotlin/lotto/domain/LottoWinner.kt index f8379ff532..3de8422885 100644 --- a/src/main/kotlin/lotto/domain/LottoWinner.kt +++ b/src/main/kotlin/lotto/domain/LottoWinner.kt @@ -9,5 +9,5 @@ class LottoWinner( .filter { hasPrize(it) } } - private fun hasPrize(lottoRank: LottoRank) = lottoRank.prizeMoney > 0 + private fun hasPrize(lottoRank: LottoRank) = lottoRank.prizeMoney.isPositive() } diff --git a/src/main/kotlin/lotto/domain/LuckyNumbers.kt b/src/main/kotlin/lotto/domain/LuckyNumbers.kt index 384d90683d..790048791e 100644 --- a/src/main/kotlin/lotto/domain/LuckyNumbers.kt +++ b/src/main/kotlin/lotto/domain/LuckyNumbers.kt @@ -1,27 +1,36 @@ package lotto.domain +import lotto.application.common.Number + class LuckyNumbers( - private val luckyNumbers: List, - private val bonusNumber: Int + luckyNumbers: List, + bonusNumber: Number ) { + private val _luckyNumbers: List + private val _bonusNumber: LottoNumber + init { require(luckyNumbers.size == LUCKY_NUMBER_SIZE) { "당첨 번호는 ${LUCKY_NUMBER_SIZE}개가 필요합니다." } require(luckyNumbers.toSet().size == LUCKY_NUMBER_SIZE) { "당첨 번호에 중복이 있습니다." } require(!luckyNumbers.contains(bonusNumber)) { "보너스볼은 당첨번호와 중복될 수 없습니다." } + + _luckyNumbers = luckyNumbers.map { LottoNumber(it) } + _bonusNumber = LottoNumber(bonusNumber) } - fun rank(numbers: List): LottoRank { + fun rank(numbers: List): LottoRank { val hitCount = countHitNumbers(numbers) val hasBonusNumber = containsBonusNumber(numbers) return LottoRank.from(hitCount, hasBonusNumber) } - private fun countHitNumbers(numbers: List): Int { - return numbers.count { number -> luckyNumbers.contains(number) } + private fun countHitNumbers(numbers: List): Number { + val count = numbers.count { number -> _luckyNumbers.contains(number) } + return Number(count) } - private fun containsBonusNumber(numbers: List): Boolean { - return numbers.contains(bonusNumber) + private fun containsBonusNumber(numbers: List): Boolean { + return numbers.contains(_bonusNumber) } companion object { diff --git a/src/main/kotlin/lotto/domain/Payment.kt b/src/main/kotlin/lotto/domain/Payment.kt index b2448ce2a8..ff9060844c 100644 --- a/src/main/kotlin/lotto/domain/Payment.kt +++ b/src/main/kotlin/lotto/domain/Payment.kt @@ -1,9 +1,11 @@ package lotto.domain +import lotto.application.common.Number + class Payment( - val payment: Int + val payment: Number ) { init { - require(payment >= 0) { "지불액은 음수가 될 수 없습니다." } + require(payment.isNegative().not()) { "지불액은 음수가 될 수 없습니다." } } } diff --git a/src/main/kotlin/lotto/util/NumberGenerator.kt b/src/main/kotlin/lotto/util/NumberGenerator.kt index 9d2cf01b7c..9b8ec6e960 100644 --- a/src/main/kotlin/lotto/util/NumberGenerator.kt +++ b/src/main/kotlin/lotto/util/NumberGenerator.kt @@ -1,5 +1,7 @@ package lotto.util +import lotto.application.common.Number + interface NumberGenerator { - fun generate(start: Int, end: Int, size: Int): List + fun generate(start: Int, end: Int, size: Int): List } diff --git a/src/main/kotlin/lotto/util/RandomNumberGenerator.kt b/src/main/kotlin/lotto/util/RandomNumberGenerator.kt index fb80a9364e..9c71fdc280 100644 --- a/src/main/kotlin/lotto/util/RandomNumberGenerator.kt +++ b/src/main/kotlin/lotto/util/RandomNumberGenerator.kt @@ -1,9 +1,12 @@ package lotto.util +import lotto.application.common.Number + class RandomNumberGenerator: NumberGenerator { - override fun generate(start: Int, end: Int, size: Int): List { + override fun generate(start: Int, end: Int, size: Int): List { val range = start..end val shuffled = range.shuffled() - return shuffled.subList(0, size) + val subList = shuffled.subList(0, size) + return subList.map { Number(it) } } } diff --git a/src/main/kotlin/lotto/view/InputView.kt b/src/main/kotlin/lotto/view/InputView.kt index eaa520bab1..129d0ee4fe 100644 --- a/src/main/kotlin/lotto/view/InputView.kt +++ b/src/main/kotlin/lotto/view/InputView.kt @@ -1,5 +1,6 @@ package lotto.view +import lotto.application.common.Number import lotto.domain.Payment import lotto.util.StringValidator @@ -9,7 +10,7 @@ class InputView { println(INPUT_PAYMENT_GUIDE) val payment = readln() validatePaymentInput(payment) - return Payment(payment.toInt()) + return Payment(Number(payment.toInt())) } private fun validatePaymentInput(payment: String) { @@ -17,7 +18,7 @@ class InputView { StringValidator.validateNumber(payment) } - fun inputLuckyNumbers(): List { + fun inputLuckyNumbers(): List { println(INPUT_LUCKY_NUMBERS_GUIDE) val luckyNumberString = readln() val luckyNumbers = splitNumbers(luckyNumberString) @@ -34,14 +35,14 @@ class InputView { } } - private fun convert(split: List): List { - return split.map { it.toInt() } + private fun convert(split: List): List { + return split.map { Number(it.toInt()) } } - fun inputBonusNumber(): Int { + fun inputBonusNumber(): Number { println(INPUT_BONUS_BALL) val bonusBallNumberString = readln() - return bonusBallNumberString.toInt() + return Number(bonusBallNumberString.toInt()) } private companion object { diff --git a/src/test/kotlin/lotto/domain/LottoGeneratorTest.kt b/src/test/kotlin/lotto/domain/LottoGeneratorTest.kt index 507e9a6f46..b4bf1b8ddc 100644 --- a/src/test/kotlin/lotto/domain/LottoGeneratorTest.kt +++ b/src/test/kotlin/lotto/domain/LottoGeneratorTest.kt @@ -2,6 +2,7 @@ package lotto.domain import io.kotest.core.spec.style.StringSpec import io.kotest.matchers.collections.shouldHaveSize +import lotto.application.common.Number import lotto.util.RandomNumberGenerator class LottoGeneratorTest : StringSpec({ @@ -10,10 +11,10 @@ class LottoGeneratorTest : StringSpec({ "로또 생성 개수 확인 테스트" { // given - val inputSize = 24 + val inputSize = Number(24) // when val resultLotto = lottoGenerator.generate(inputSize) // then - resultLotto shouldHaveSize inputSize + resultLotto shouldHaveSize inputSize.number } }) diff --git a/src/test/kotlin/lotto/domain/LottoShopTest.kt b/src/test/kotlin/lotto/domain/LottoShopTest.kt index 01dcf795dc..6eca8dbe09 100644 --- a/src/test/kotlin/lotto/domain/LottoShopTest.kt +++ b/src/test/kotlin/lotto/domain/LottoShopTest.kt @@ -4,6 +4,7 @@ import io.kotest.core.spec.style.StringSpec import io.kotest.data.forAll import io.kotest.data.row import io.kotest.matchers.shouldBe +import lotto.application.common.Number import lotto.util.RandomNumberGenerator class LottoShopTest : StringSpec({ @@ -13,9 +14,9 @@ class LottoShopTest : StringSpec({ "로또 구매 테스트" { forAll( // given - row("0원을 내면 0개를 반환한다.", Payment(0), 0), - row("5000원을 내면 5개를 반환한다.", Payment(5000), 5), - row("5500원을 내면 5개를 반환한다.", Payment(5500), 5) + row("0원을 내면 0개를 반환한다.", Payment(Number(0)), 0), + row("5000원을 내면 5개를 반환한다.", Payment(Number(5000)), 5), + row("5500원을 내면 5개를 반환한다.", Payment(Number(5500)), 5) ) { title, payment, expectedSize -> // when val actual = lottoShop.buyLotto(payment) diff --git a/src/test/kotlin/lotto/domain/LottoStatisticsServiceTest.kt b/src/test/kotlin/lotto/domain/LottoStatisticsServiceTest.kt index baea82e0a1..2d7b5a048f 100644 --- a/src/test/kotlin/lotto/domain/LottoStatisticsServiceTest.kt +++ b/src/test/kotlin/lotto/domain/LottoStatisticsServiceTest.kt @@ -5,31 +5,32 @@ import io.kotest.data.forAll import io.kotest.data.row import io.kotest.matchers.equality.shouldBeEqualToComparingFields import io.kotest.matchers.shouldBe +import lotto.application.common.Number class LottoStatisticsServiceTest : StringSpec({ "당첨자 통계 통합 결과 테스트" { // given - val payment = Payment(15000) + val payment = Payment(Number(15000)) forAll( row( LottoRank.FOURTH, listOf( - LottoStatisticsResult(LottoRank.FIFTH, 0), - LottoStatisticsResult(LottoRank.FOURTH, 1), - LottoStatisticsResult(LottoRank.THIRD, 0), - LottoStatisticsResult(LottoRank.SECOND, 0), - LottoStatisticsResult(LottoRank.FIRST, 0), + LottoStatisticsResult(LottoRank.FIFTH, Number(0)), + LottoStatisticsResult(LottoRank.FOURTH, Number(1)), + LottoStatisticsResult(LottoRank.THIRD, Number(0)), + LottoStatisticsResult(LottoRank.SECOND, Number(0)), + LottoStatisticsResult(LottoRank.FIRST, Number(0)), ) ), row( LottoRank.FIRST, listOf( - LottoStatisticsResult(LottoRank.FIFTH, 0), - LottoStatisticsResult(LottoRank.FOURTH, 0), - LottoStatisticsResult(LottoRank.THIRD, 0), - LottoStatisticsResult(LottoRank.SECOND, 0), - LottoStatisticsResult(LottoRank.FIRST, 1), + LottoStatisticsResult(LottoRank.FIFTH, Number(0)), + LottoStatisticsResult(LottoRank.FOURTH, Number(0)), + LottoStatisticsResult(LottoRank.THIRD, Number(0)), + LottoStatisticsResult(LottoRank.SECOND, Number(0)), + LottoStatisticsResult(LottoRank.FIRST, Number(1)), ) ) ) { lottoResult, expected -> diff --git a/src/test/kotlin/lotto/domain/LottoStatisticsTest.kt b/src/test/kotlin/lotto/domain/LottoStatisticsTest.kt index 7d951a49b4..473a487352 100644 --- a/src/test/kotlin/lotto/domain/LottoStatisticsTest.kt +++ b/src/test/kotlin/lotto/domain/LottoStatisticsTest.kt @@ -4,13 +4,18 @@ import io.kotest.core.spec.style.StringSpec import io.kotest.data.forAll import io.kotest.data.row import io.kotest.matchers.shouldBe +import lotto.application.common.Number +import lotto.domain.LottoRank +import lotto.domain.LottoStatistics +import lotto.domain.LottoStatisticsResult +import lotto.domain.Payment class LottoStatisticsTest : StringSpec({ "로또 수익률 계산 테스트" { forAll( - row(listOf(LottoRank.FOURTH, LottoRank.FIFTH), Payment(100000), 0.55), - row(listOf(LottoRank.FIFTH), Payment(5000), 1.0), - row(listOf(LottoRank.FOURTH), Payment(5000), 10), + row(listOf(LottoRank.FOURTH, LottoRank.FIFTH), Payment(Number(100000)), 0.55), + row(listOf(LottoRank.FIFTH), Payment(Number(5000)), 1.0), + row(listOf(LottoRank.FOURTH), Payment(Number(5000)), 10), ) { prizeList, payment, expectedEarningRate -> // given val lottoStatistics = LottoStatistics(prizeList) @@ -34,11 +39,11 @@ class LottoStatisticsTest : StringSpec({ val lottoStatistics = LottoStatistics(winLottoList) val expected = listOf( - LottoStatisticsResult(LottoRank.FIFTH, 3), - LottoStatisticsResult(LottoRank.FOURTH, 0), - LottoStatisticsResult(LottoRank.THIRD, 2), - LottoStatisticsResult(LottoRank.SECOND, 0), - LottoStatisticsResult(LottoRank.FIRST, 1), + LottoStatisticsResult(LottoRank.FIFTH, Number(3)), + LottoStatisticsResult(LottoRank.FOURTH, Number(0)), + LottoStatisticsResult(LottoRank.THIRD, Number(2)), + LottoStatisticsResult(LottoRank.SECOND, Number(0)), + LottoStatisticsResult(LottoRank.FIRST, Number(1)), ) // when diff --git a/src/test/kotlin/lotto/domain/LottoTest.kt b/src/test/kotlin/lotto/domain/LottoTest.kt index 0b14972c5e..cd72ea5fc0 100644 --- a/src/test/kotlin/lotto/domain/LottoTest.kt +++ b/src/test/kotlin/lotto/domain/LottoTest.kt @@ -3,11 +3,20 @@ package lotto.domain import io.kotest.assertions.throwables.shouldThrowExactly import io.kotest.core.spec.style.StringSpec import io.kotest.matchers.shouldBe +import lotto.application.common.Number class LottoTest : StringSpec({ "로또 번호가 6개를 넘으면 에러 발생 테스트" { - val numbers = listOf(1, 2, 3, 4, 5, 6, 7) + val numbers = listOf( + LottoNumber(Number(1)), + LottoNumber(Number(2)), + LottoNumber(Number(3)), + LottoNumber(Number(4)), + LottoNumber(Number(5)), + LottoNumber(Number(6)), + LottoNumber(Number(7)) + ) val exception = shouldThrowExactly { Lotto(numbers) } diff --git a/src/test/kotlin/lotto/domain/LottoWinnerTest.kt b/src/test/kotlin/lotto/domain/LottoWinnerTest.kt index 5d831b3461..419f131e7a 100644 --- a/src/test/kotlin/lotto/domain/LottoWinnerTest.kt +++ b/src/test/kotlin/lotto/domain/LottoWinnerTest.kt @@ -3,19 +3,59 @@ package lotto.domain import io.kotest.core.spec.style.StringSpec import io.kotest.matchers.collections.shouldContainInOrder import io.kotest.matchers.shouldBe +import lotto.application.common.Number +import lotto.domain.Lotto +import lotto.domain.LottoRank +import lotto.domain.LottoWinner +import lotto.domain.LuckyNumbers class LottoWinnerTest : StringSpec({ "당첨 로또 선택 테스트" { // given - val luckyNumbers = listOf(1, 3, 5, 7, 9, 11) - val bonusNumber = 13 + val luckyNumbers = listOf( + Number(1), + Number(3), + Number(5), + Number(7), + Number(9), + Number(11) + ) + val bonusNumber = Number(13) val lottoWinner = LottoWinner(LuckyNumbers(luckyNumbers, bonusNumber)) - val fourthWinLotto = Lotto(listOf(5, 7, 9, 11, 12, 13)) - val secondWinLotto = Lotto(listOf(3, 5, 7, 9, 11, 13)) - val thirdWinLotto = Lotto(listOf(3, 5, 7, 9, 11, 15)) - val notWintLotto = Lotto(listOf(2, 4, 6, 8, 10, 13)) + val fourthWinLotto = Lotto(listOf( + LottoNumber(Number(5)), + LottoNumber(Number(7)), + LottoNumber(Number(9)), + LottoNumber(Number(11)), + LottoNumber(Number(12)), + LottoNumber(Number(13)) + )) + val secondWinLotto = Lotto(listOf( + LottoNumber(Number(3)), + LottoNumber(Number(5)), + LottoNumber(Number(7)), + LottoNumber(Number(9)), + LottoNumber(Number(11)), + LottoNumber(Number(13)) + )) + val thirdWinLotto = Lotto(listOf( + LottoNumber(Number(3)), + LottoNumber(Number(5)), + LottoNumber(Number(7)), + LottoNumber(Number(9)), + LottoNumber(Number(11)), + LottoNumber(Number(15)) + )) + val notWintLotto = Lotto(listOf( + LottoNumber(Number(2)), + LottoNumber(Number(4)), + LottoNumber(Number(6)), + LottoNumber(Number(8)), + LottoNumber(Number(10)), + LottoNumber(Number(13)) + )) // when val result = lottoWinner.findWinLottoList(listOf(fourthWinLotto, notWintLotto, secondWinLotto, thirdWinLotto)) // then diff --git a/src/test/kotlin/lotto/domain/LuckyNumbersTest.kt b/src/test/kotlin/lotto/domain/LuckyNumbersTest.kt index aabdad0046..51497ce7ab 100644 --- a/src/test/kotlin/lotto/domain/LuckyNumbersTest.kt +++ b/src/test/kotlin/lotto/domain/LuckyNumbersTest.kt @@ -5,28 +5,51 @@ import io.kotest.core.spec.style.StringSpec import io.kotest.data.forAll import io.kotest.data.row import io.kotest.matchers.shouldBe +import lotto.application.common.Number class LuckyNumbersTest : StringSpec({ "당첨 번호가 6개를 넘으면 에러 발생 테스트" { - val numbers = listOf(1, 2, 3, 4, 5, 6, 7) + val numbers = listOf( + Number(1), + Number(2), + Number(3), + Number(4), + Number(5), + Number(6), + Number(7) + ) val exception = shouldThrowExactly { - LuckyNumbers(luckyNumbers = numbers, bonusNumber = 10) + LuckyNumbers(luckyNumbers = numbers, bonusNumber = Number(10)) } exception.message shouldBe "당첨 번호는 6개가 필요합니다." } "당첨 번호가 중복이면 에러 발생 테스트" { - val numbers = listOf(1, 2, 3, 4, 6, 6) + val numbers = listOf( + Number(1), + Number(2), + Number(3), + Number(4), + Number(6), + Number(6) + ) val exception = shouldThrowExactly { - LuckyNumbers(luckyNumbers = numbers, bonusNumber = 10) + LuckyNumbers(luckyNumbers = numbers, bonusNumber = Number(10)) } exception.message shouldBe "당첨 번호에 중복이 있습니다." } "보너스볼 중복 에러 테스트" { - val numbers = listOf(1, 2, 3, 4, 5, 6) + val numbers = listOf( + Number(1), + Number(2), + Number(3), + Number(4), + Number(5), + Number(6) + ) val exception = shouldThrowExactly { - LuckyNumbers(luckyNumbers = numbers, bonusNumber = 6) + LuckyNumbers(luckyNumbers = numbers, bonusNumber = Number(6)) } exception.message shouldBe "보너스볼은 당첨번호와 중복될 수 없습니다." } @@ -34,12 +57,64 @@ class LuckyNumbersTest : StringSpec({ "로또 Hit Count 계산 테스트" { forAll( // given - row("0개 일치하는 로또", LuckyNumbers(luckyNumbers = listOf(1, 3, 5, 7, 9, 11), bonusNumber = 13), listOf(2, 4, 6, 8, 10, 12), LottoRank.MISS), - row("3개 일치하는 로또", LuckyNumbers(luckyNumbers = listOf(1, 3, 5, 7, 9, 11), bonusNumber = 13), listOf(7, 8, 9, 10, 11, 12), LottoRank.FIFTH), + row( + "0개 일치하는 로또", + LuckyNumbers(luckyNumbers = listOf( + Number(1), + Number(3), + Number(5), + Number(7), + Number(9), + Number(11) + ), bonusNumber = Number(13)), + listOf( + LottoNumber(Number(2)), + LottoNumber(Number(4)), + LottoNumber(Number(6)), + LottoNumber(Number(8)), + LottoNumber(Number(10)), + LottoNumber(Number(12)) + ), + LottoRank.MISS + ), + row( + "3개 일치하는 로또", + LuckyNumbers(luckyNumbers = listOf( + Number(1), + Number(3), + Number(5), + Number(7), + Number(9), + Number(11) + ), bonusNumber = Number(13)), + listOf( + LottoNumber(Number(7)), + LottoNumber(Number(8)), + LottoNumber(Number(9)), + LottoNumber(Number(10)), + LottoNumber(Number(11)), + LottoNumber(Number(12)) + ), + LottoRank.FIFTH + ), row( "5개 일치+보너스번호 일치하는 로또", - LuckyNumbers(luckyNumbers = listOf(1, 3, 5, 7, 9, 11), bonusNumber = 13), - listOf(1, 3, 5, 7, 9, 13), + LuckyNumbers(luckyNumbers = listOf( + Number(1), + Number(3), + Number(5), + Number(7), + Number(9), + Number(11) + ), bonusNumber = Number(13)), + listOf( + LottoNumber(Number(1)), + LottoNumber(Number(3)), + LottoNumber(Number(5)), + LottoNumber(Number(7)), + LottoNumber(Number(9)), + LottoNumber(Number(13)) + ), LottoRank.SECOND ) ) { title, luckyNumbers, lottoNumbers, expectedRank -> diff --git a/src/test/kotlin/lotto/util/NumberGeneratorTest.kt b/src/test/kotlin/lotto/util/RandomNumberGeneratorTest.kt similarity index 85% rename from src/test/kotlin/lotto/util/NumberGeneratorTest.kt rename to src/test/kotlin/lotto/util/RandomNumberGeneratorTest.kt index 2decea957d..568f30c158 100644 --- a/src/test/kotlin/lotto/util/NumberGeneratorTest.kt +++ b/src/test/kotlin/lotto/util/RandomNumberGeneratorTest.kt @@ -4,7 +4,7 @@ import io.kotest.core.spec.style.StringSpec import io.kotest.matchers.collections.shouldHaveSize import io.kotest.matchers.ints.shouldBeInRange -class NumberGeneratorTest : StringSpec({ +class RandomNumberGeneratorTest : StringSpec({ val numberGenerator = RandomNumberGenerator() @@ -18,7 +18,7 @@ class NumberGeneratorTest : StringSpec({ // then randomNumbers shouldHaveSize inputSize randomNumbers.forEach { - it shouldBeInRange start..end + it.number shouldBeInRange start..end } } }) From 89e133dfb58a0519ef7ff89d03fb58ad3a6418ed Mon Sep 17 00:00:00 2001 From: dajeong Date: Mon, 12 Dec 2022 09:53:14 +0900 Subject: [PATCH 09/21] =?UTF-8?q?[step4]=20=EC=9E=85=EB=A0=A5=20=EB=AC=B8?= =?UTF-8?q?=EC=9E=90=EC=97=B4=20=ED=8F=AC=EC=9E=A5=20=ED=81=B4=EB=9E=98?= =?UTF-8?q?=EC=8A=A4=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/lotto/README.md | 3 +- .../lotto/application/common/NumberString.kt | 18 +++++ .../application/common/NumberStringList.kt | 11 +++ src/main/kotlin/lotto/util/StringValidator.kt | 16 ---- src/main/kotlin/lotto/view/InputView.kt | 31 ++------ .../stringCalculator/StringAddCalculator.kt | 3 +- .../common/NumberStringListTest.kt} | 24 ++++-- .../application/common/NumberStringTest.kt | 56 ++++++++++++++ .../lotto/domain/LottoStatisticsTest.kt | 4 - .../kotlin/lotto/domain/LottoWinnerTest.kt | 76 ++++++++++--------- .../kotlin/lotto/domain/LuckyNumbersTest.kt | 54 +++++++------ 11 files changed, 181 insertions(+), 115 deletions(-) create mode 100644 src/main/kotlin/lotto/application/common/NumberString.kt create mode 100644 src/main/kotlin/lotto/application/common/NumberStringList.kt delete mode 100644 src/main/kotlin/lotto/util/StringValidator.kt rename src/test/kotlin/lotto/{util/StringValidatorTest.kt => application/common/NumberStringListTest.kt} (55%) create mode 100644 src/test/kotlin/lotto/application/common/NumberStringTest.kt diff --git a/src/main/kotlin/lotto/README.md b/src/main/kotlin/lotto/README.md index 2f994e45d3..64cdc5f421 100644 --- a/src/main/kotlin/lotto/README.md +++ b/src/main/kotlin/lotto/README.md @@ -1,7 +1,8 @@ # TODO ## 구현할 '로또(수동)' 기능 목록 -- [ ] 모든 원시값과 문자열을 포장 +- [x] 모든 원시값 포장 클래스 생성 +- [x] 입력 문자열 포장 클래스 생성 - [ ] 수동으로 구매할 로또 수 입력 기능 - [ ] 수동으로 구매할 로또 번호 입력 기능 - [ ] 수동 로또 생성 기능 diff --git a/src/main/kotlin/lotto/application/common/NumberString.kt b/src/main/kotlin/lotto/application/common/NumberString.kt new file mode 100644 index 0000000000..ca87dff5e5 --- /dev/null +++ b/src/main/kotlin/lotto/application/common/NumberString.kt @@ -0,0 +1,18 @@ +package lotto.application.common + +class NumberString( + private val string: String +) { + init { + require(string.isNotBlank()) { "값이 비어있습니다." } + require(isNumber()) { "숫자가 아닙니다. (입력값:$string)" } + } + + private fun isNumber(): Boolean { + return string.toCharArray().all { it in '0'..'9' } + } + + fun toNumber(): Number { + return Number(string.toInt()) + } +} diff --git a/src/main/kotlin/lotto/application/common/NumberStringList.kt b/src/main/kotlin/lotto/application/common/NumberStringList.kt new file mode 100644 index 0000000000..9662ba4c57 --- /dev/null +++ b/src/main/kotlin/lotto/application/common/NumberStringList.kt @@ -0,0 +1,11 @@ +package lotto.application.common + +class NumberStringList( + string: String +) { + val list = string.split(",").map { NumberString(it.trim()) } + + fun toNumberList(): List { + return list.map { it.toNumber() } + } +} diff --git a/src/main/kotlin/lotto/util/StringValidator.kt b/src/main/kotlin/lotto/util/StringValidator.kt deleted file mode 100644 index cd1e8221fe..0000000000 --- a/src/main/kotlin/lotto/util/StringValidator.kt +++ /dev/null @@ -1,16 +0,0 @@ -package lotto.util - -object StringValidator { - fun validateNumber(string: String) { - val isNumeric = string.toCharArray().all { it in '0'..'9' } - if (!isNumeric) { - throw IllegalArgumentException("숫자가 아닙니다. (입력값:$string)") - } - } - - fun validateNotBlank(string: String) { - if (string.isBlank()) { - throw IllegalArgumentException("값이 비어있습니다.") - } - } -} diff --git a/src/main/kotlin/lotto/view/InputView.kt b/src/main/kotlin/lotto/view/InputView.kt index 129d0ee4fe..be752433d3 100644 --- a/src/main/kotlin/lotto/view/InputView.kt +++ b/src/main/kotlin/lotto/view/InputView.kt @@ -1,42 +1,21 @@ package lotto.view import lotto.application.common.Number +import lotto.application.common.NumberString +import lotto.application.common.NumberStringList import lotto.domain.Payment -import lotto.util.StringValidator class InputView { fun inputPayment(): Payment { println(INPUT_PAYMENT_GUIDE) - val payment = readln() - validatePaymentInput(payment) - return Payment(Number(payment.toInt())) - } - - private fun validatePaymentInput(payment: String) { - StringValidator.validateNotBlank(payment) - StringValidator.validateNumber(payment) + val payment = NumberString(readln()) + return Payment(payment.toNumber()) } fun inputLuckyNumbers(): List { println(INPUT_LUCKY_NUMBERS_GUIDE) - val luckyNumberString = readln() - val luckyNumbers = splitNumbers(luckyNumberString) - validateLuckyNumbersInput(luckyNumbers) - return convert(luckyNumbers) - } - - private fun splitNumbers(input: String) = input.split(",").map { it.trim() } - - private fun validateLuckyNumbersInput(split: List) { - split.forEach { - StringValidator.validateNotBlank(it) - StringValidator.validateNumber(it) - } - } - - private fun convert(split: List): List { - return split.map { Number(it.toInt()) } + return NumberStringList(readln()).toNumberList() } fun inputBonusNumber(): Number { diff --git a/src/main/kotlin/stringCalculator/StringAddCalculator.kt b/src/main/kotlin/stringCalculator/StringAddCalculator.kt index 00554ff064..48286c9c95 100644 --- a/src/main/kotlin/stringCalculator/StringAddCalculator.kt +++ b/src/main/kotlin/stringCalculator/StringAddCalculator.kt @@ -23,7 +23,8 @@ class StringAddCalculator { return split(numberString, delimiter) } - private fun split(string: String, delimiter: Regex = DEFAULT_DELIMITER_REGEX): List = string.split(delimiter) + private fun split(string: String, delimiter: Regex = DEFAULT_DELIMITER_REGEX): List = + string.split(delimiter) private fun split(string: String, delimiter: String): List { if (delimiter.isEmpty()) { diff --git a/src/test/kotlin/lotto/util/StringValidatorTest.kt b/src/test/kotlin/lotto/application/common/NumberStringListTest.kt similarity index 55% rename from src/test/kotlin/lotto/util/StringValidatorTest.kt rename to src/test/kotlin/lotto/application/common/NumberStringListTest.kt index 45130c6e26..ceda7ce766 100644 --- a/src/test/kotlin/lotto/util/StringValidatorTest.kt +++ b/src/test/kotlin/lotto/application/common/NumberStringListTest.kt @@ -1,28 +1,38 @@ -package lotto.util +package lotto.application.common import io.kotest.assertions.throwables.shouldThrowExactly import io.kotest.core.spec.style.StringSpec import io.kotest.matchers.shouldBe -class StringValidatorTest : StringSpec({ +class NumberStringListTest : StringSpec({ + "숫자 아닐때 에러 발생 테스트" { val exception = shouldThrowExactly { - StringValidator.validateNumber("1!") + NumberStringList("1,a") } - exception.message shouldBe "숫자가 아닙니다. (입력값:1!)" + exception.message shouldBe "숫자가 아닙니다. (입력값:a)" } "빈 문자열 일 때 에러 테스트" { val exception = shouldThrowExactly { - StringValidator.validateNotBlank("") + NumberStringList("1,") } exception.message shouldBe "값이 비어있습니다." } - "공백 문자열 에러 테스트" { + "공백 문자열 일 때 에러 테스트" { val exception = shouldThrowExactly { - StringValidator.validateNotBlank(" ") + NumberStringList("1, ") } exception.message shouldBe "값이 비어있습니다." } + + "숫자 리스트 변환 테스트" { + // given + val numberStringList = NumberStringList("1,2,3,4,5") + // when + val numberList = numberStringList.toNumberList() + // then + numberList shouldBe listOf(Number(1), Number(2), Number(3), Number(4), Number(5)) + } }) diff --git a/src/test/kotlin/lotto/application/common/NumberStringTest.kt b/src/test/kotlin/lotto/application/common/NumberStringTest.kt new file mode 100644 index 0000000000..6a77a26f13 --- /dev/null +++ b/src/test/kotlin/lotto/application/common/NumberStringTest.kt @@ -0,0 +1,56 @@ +package lotto.application.common + +import io.kotest.assertions.throwables.shouldThrowExactly +import io.kotest.core.spec.style.StringSpec +import io.kotest.matchers.shouldBe + +class NumberStringTest : StringSpec({ + + "숫자 아닐때 에러 발생 테스트" { + val exception = shouldThrowExactly { + NumberString("1!") + } + exception.message shouldBe "숫자가 아닙니다. (입력값:1!)" + } + + "빈 문자열 일 때 에러 테스트" { + val exception = shouldThrowExactly { + NumberString("") + } + exception.message shouldBe "값이 비어있습니다." + } + + "공백 문자열 일 때 에러 테스트" { + val exception = shouldThrowExactly { + NumberString(" ") + } + exception.message shouldBe "값이 비어있습니다." + } + + "양수 판단 테스트" { + // given + val number = Number(1) + // when + val actual = number.isPositive() + // then + actual shouldBe true + } + + "음수 판단 테스트" { + // given + val number = Number(-1) + // when + val actual = number.isNegative() + // then + actual shouldBe true + } + + "double 타입으로 변환 테스트" { + // given + val number = Number(1) + // when + val actual = number.toDouble() + // then + actual shouldBe 1.0 + } +}) \ No newline at end of file diff --git a/src/test/kotlin/lotto/domain/LottoStatisticsTest.kt b/src/test/kotlin/lotto/domain/LottoStatisticsTest.kt index 473a487352..4642429b77 100644 --- a/src/test/kotlin/lotto/domain/LottoStatisticsTest.kt +++ b/src/test/kotlin/lotto/domain/LottoStatisticsTest.kt @@ -5,10 +5,6 @@ import io.kotest.data.forAll import io.kotest.data.row import io.kotest.matchers.shouldBe import lotto.application.common.Number -import lotto.domain.LottoRank -import lotto.domain.LottoStatistics -import lotto.domain.LottoStatisticsResult -import lotto.domain.Payment class LottoStatisticsTest : StringSpec({ "로또 수익률 계산 테스트" { diff --git a/src/test/kotlin/lotto/domain/LottoWinnerTest.kt b/src/test/kotlin/lotto/domain/LottoWinnerTest.kt index 419f131e7a..ebd902aa45 100644 --- a/src/test/kotlin/lotto/domain/LottoWinnerTest.kt +++ b/src/test/kotlin/lotto/domain/LottoWinnerTest.kt @@ -4,10 +4,6 @@ import io.kotest.core.spec.style.StringSpec import io.kotest.matchers.collections.shouldContainInOrder import io.kotest.matchers.shouldBe import lotto.application.common.Number -import lotto.domain.Lotto -import lotto.domain.LottoRank -import lotto.domain.LottoWinner -import lotto.domain.LuckyNumbers class LottoWinnerTest : StringSpec({ @@ -24,38 +20,46 @@ class LottoWinnerTest : StringSpec({ val bonusNumber = Number(13) val lottoWinner = LottoWinner(LuckyNumbers(luckyNumbers, bonusNumber)) - val fourthWinLotto = Lotto(listOf( - LottoNumber(Number(5)), - LottoNumber(Number(7)), - LottoNumber(Number(9)), - LottoNumber(Number(11)), - LottoNumber(Number(12)), - LottoNumber(Number(13)) - )) - val secondWinLotto = Lotto(listOf( - LottoNumber(Number(3)), - LottoNumber(Number(5)), - LottoNumber(Number(7)), - LottoNumber(Number(9)), - LottoNumber(Number(11)), - LottoNumber(Number(13)) - )) - val thirdWinLotto = Lotto(listOf( - LottoNumber(Number(3)), - LottoNumber(Number(5)), - LottoNumber(Number(7)), - LottoNumber(Number(9)), - LottoNumber(Number(11)), - LottoNumber(Number(15)) - )) - val notWintLotto = Lotto(listOf( - LottoNumber(Number(2)), - LottoNumber(Number(4)), - LottoNumber(Number(6)), - LottoNumber(Number(8)), - LottoNumber(Number(10)), - LottoNumber(Number(13)) - )) + val fourthWinLotto = Lotto( + listOf( + LottoNumber(Number(5)), + LottoNumber(Number(7)), + LottoNumber(Number(9)), + LottoNumber(Number(11)), + LottoNumber(Number(12)), + LottoNumber(Number(13)) + ) + ) + val secondWinLotto = Lotto( + listOf( + LottoNumber(Number(3)), + LottoNumber(Number(5)), + LottoNumber(Number(7)), + LottoNumber(Number(9)), + LottoNumber(Number(11)), + LottoNumber(Number(13)) + ) + ) + val thirdWinLotto = Lotto( + listOf( + LottoNumber(Number(3)), + LottoNumber(Number(5)), + LottoNumber(Number(7)), + LottoNumber(Number(9)), + LottoNumber(Number(11)), + LottoNumber(Number(15)) + ) + ) + val notWintLotto = Lotto( + listOf( + LottoNumber(Number(2)), + LottoNumber(Number(4)), + LottoNumber(Number(6)), + LottoNumber(Number(8)), + LottoNumber(Number(10)), + LottoNumber(Number(13)) + ) + ) // when val result = lottoWinner.findWinLottoList(listOf(fourthWinLotto, notWintLotto, secondWinLotto, thirdWinLotto)) // then diff --git a/src/test/kotlin/lotto/domain/LuckyNumbersTest.kt b/src/test/kotlin/lotto/domain/LuckyNumbersTest.kt index 51497ce7ab..3297405cea 100644 --- a/src/test/kotlin/lotto/domain/LuckyNumbersTest.kt +++ b/src/test/kotlin/lotto/domain/LuckyNumbersTest.kt @@ -59,14 +59,16 @@ class LuckyNumbersTest : StringSpec({ // given row( "0개 일치하는 로또", - LuckyNumbers(luckyNumbers = listOf( - Number(1), - Number(3), - Number(5), - Number(7), - Number(9), - Number(11) - ), bonusNumber = Number(13)), + LuckyNumbers( + luckyNumbers = listOf( + Number(1), + Number(3), + Number(5), + Number(7), + Number(9), + Number(11) + ), bonusNumber = Number(13) + ), listOf( LottoNumber(Number(2)), LottoNumber(Number(4)), @@ -79,14 +81,16 @@ class LuckyNumbersTest : StringSpec({ ), row( "3개 일치하는 로또", - LuckyNumbers(luckyNumbers = listOf( - Number(1), - Number(3), - Number(5), - Number(7), - Number(9), - Number(11) - ), bonusNumber = Number(13)), + LuckyNumbers( + luckyNumbers = listOf( + Number(1), + Number(3), + Number(5), + Number(7), + Number(9), + Number(11) + ), bonusNumber = Number(13) + ), listOf( LottoNumber(Number(7)), LottoNumber(Number(8)), @@ -99,14 +103,16 @@ class LuckyNumbersTest : StringSpec({ ), row( "5개 일치+보너스번호 일치하는 로또", - LuckyNumbers(luckyNumbers = listOf( - Number(1), - Number(3), - Number(5), - Number(7), - Number(9), - Number(11) - ), bonusNumber = Number(13)), + LuckyNumbers( + luckyNumbers = listOf( + Number(1), + Number(3), + Number(5), + Number(7), + Number(9), + Number(11) + ), bonusNumber = Number(13) + ), listOf( LottoNumber(Number(1)), LottoNumber(Number(3)), From 3291402d7e0605cfdc5671316025c62b8942f3f6 Mon Sep 17 00:00:00 2001 From: dajeong Date: Mon, 12 Dec 2022 10:41:29 +0900 Subject: [PATCH 10/21] =?UTF-8?q?[step4]=20=ED=8C=A8=ED=82=A4=EC=A7=80=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/{application => }/common/Number.kt | 2 +- .../kotlin/lotto/{application => }/common/NumberString.kt | 2 +- .../lotto/{application => }/common/NumberStringList.kt | 2 +- src/main/kotlin/lotto/domain/LottoGenerator.kt | 2 +- src/main/kotlin/lotto/domain/LottoNumber.kt | 2 +- src/main/kotlin/lotto/domain/LottoRank.kt | 2 +- src/main/kotlin/lotto/domain/LottoShop.kt | 2 +- src/main/kotlin/lotto/domain/LottoStatistics.kt | 2 +- src/main/kotlin/lotto/domain/LottoStatisticsResult.kt | 2 +- src/main/kotlin/lotto/domain/LuckyNumbers.kt | 2 +- src/main/kotlin/lotto/domain/Payment.kt | 2 +- src/main/kotlin/lotto/util/NumberGenerator.kt | 2 +- src/main/kotlin/lotto/util/RandomNumberGenerator.kt | 2 +- src/main/kotlin/lotto/view/InputView.kt | 6 +++--- .../kotlin/lotto/application/common/NumberStringListTest.kt | 2 ++ .../kotlin/lotto/application/common/NumberStringTest.kt | 2 ++ src/test/kotlin/lotto/domain/LottoGeneratorTest.kt | 2 +- src/test/kotlin/lotto/domain/LottoShopTest.kt | 2 +- src/test/kotlin/lotto/domain/LottoStatisticsServiceTest.kt | 2 +- src/test/kotlin/lotto/domain/LottoStatisticsTest.kt | 2 +- src/test/kotlin/lotto/domain/LottoTest.kt | 2 +- src/test/kotlin/lotto/domain/LottoWinnerTest.kt | 2 +- src/test/kotlin/lotto/domain/LuckyNumbersTest.kt | 2 +- 23 files changed, 27 insertions(+), 23 deletions(-) rename src/main/kotlin/lotto/{application => }/common/Number.kt (88%) rename src/main/kotlin/lotto/{application => }/common/NumberString.kt (92%) rename src/main/kotlin/lotto/{application => }/common/NumberStringList.kt (86%) diff --git a/src/main/kotlin/lotto/application/common/Number.kt b/src/main/kotlin/lotto/common/Number.kt similarity index 88% rename from src/main/kotlin/lotto/application/common/Number.kt rename to src/main/kotlin/lotto/common/Number.kt index 0be82dcd8c..cdb8fdec68 100644 --- a/src/main/kotlin/lotto/application/common/Number.kt +++ b/src/main/kotlin/lotto/common/Number.kt @@ -1,4 +1,4 @@ -package lotto.application.common +package lotto.common data class Number( val number: Int diff --git a/src/main/kotlin/lotto/application/common/NumberString.kt b/src/main/kotlin/lotto/common/NumberString.kt similarity index 92% rename from src/main/kotlin/lotto/application/common/NumberString.kt rename to src/main/kotlin/lotto/common/NumberString.kt index ca87dff5e5..ed1c4dc946 100644 --- a/src/main/kotlin/lotto/application/common/NumberString.kt +++ b/src/main/kotlin/lotto/common/NumberString.kt @@ -1,4 +1,4 @@ -package lotto.application.common +package lotto.common class NumberString( private val string: String diff --git a/src/main/kotlin/lotto/application/common/NumberStringList.kt b/src/main/kotlin/lotto/common/NumberStringList.kt similarity index 86% rename from src/main/kotlin/lotto/application/common/NumberStringList.kt rename to src/main/kotlin/lotto/common/NumberStringList.kt index 9662ba4c57..0628d1598b 100644 --- a/src/main/kotlin/lotto/application/common/NumberStringList.kt +++ b/src/main/kotlin/lotto/common/NumberStringList.kt @@ -1,4 +1,4 @@ -package lotto.application.common +package lotto.common class NumberStringList( string: String diff --git a/src/main/kotlin/lotto/domain/LottoGenerator.kt b/src/main/kotlin/lotto/domain/LottoGenerator.kt index 19adfc5049..4c83a00540 100644 --- a/src/main/kotlin/lotto/domain/LottoGenerator.kt +++ b/src/main/kotlin/lotto/domain/LottoGenerator.kt @@ -1,6 +1,6 @@ package lotto.domain -import lotto.application.common.Number +import lotto.common.Number import lotto.util.NumberGenerator class LottoGenerator( diff --git a/src/main/kotlin/lotto/domain/LottoNumber.kt b/src/main/kotlin/lotto/domain/LottoNumber.kt index b328246940..423b267bf2 100644 --- a/src/main/kotlin/lotto/domain/LottoNumber.kt +++ b/src/main/kotlin/lotto/domain/LottoNumber.kt @@ -1,6 +1,6 @@ package lotto.domain -import lotto.application.common.Number +import lotto.common.Number data class LottoNumber( val number: Number diff --git a/src/main/kotlin/lotto/domain/LottoRank.kt b/src/main/kotlin/lotto/domain/LottoRank.kt index 5d0df3aaeb..7e389ca620 100644 --- a/src/main/kotlin/lotto/domain/LottoRank.kt +++ b/src/main/kotlin/lotto/domain/LottoRank.kt @@ -1,6 +1,6 @@ package lotto.domain -import lotto.application.common.Number +import lotto.common.Number enum class LottoRank( val hitCount: Number, diff --git a/src/main/kotlin/lotto/domain/LottoShop.kt b/src/main/kotlin/lotto/domain/LottoShop.kt index 3acdc6aa37..1fc941d00f 100644 --- a/src/main/kotlin/lotto/domain/LottoShop.kt +++ b/src/main/kotlin/lotto/domain/LottoShop.kt @@ -1,6 +1,6 @@ package lotto.domain -import lotto.application.common.Number +import lotto.common.Number class LottoShop( private val lottoGenerator: LottoGenerator diff --git a/src/main/kotlin/lotto/domain/LottoStatistics.kt b/src/main/kotlin/lotto/domain/LottoStatistics.kt index fa9c53deaf..1166eb3932 100644 --- a/src/main/kotlin/lotto/domain/LottoStatistics.kt +++ b/src/main/kotlin/lotto/domain/LottoStatistics.kt @@ -1,6 +1,6 @@ package lotto.domain -import lotto.application.common.Number +import lotto.common.Number import lotto.util.NumberUtil class LottoStatistics( diff --git a/src/main/kotlin/lotto/domain/LottoStatisticsResult.kt b/src/main/kotlin/lotto/domain/LottoStatisticsResult.kt index d8aee5df9b..c74b291d86 100644 --- a/src/main/kotlin/lotto/domain/LottoStatisticsResult.kt +++ b/src/main/kotlin/lotto/domain/LottoStatisticsResult.kt @@ -1,6 +1,6 @@ package lotto.domain -import lotto.application.common.Number +import lotto.common.Number class LottoStatisticsResult( val lottoRank: LottoRank, diff --git a/src/main/kotlin/lotto/domain/LuckyNumbers.kt b/src/main/kotlin/lotto/domain/LuckyNumbers.kt index 790048791e..6cee8fde16 100644 --- a/src/main/kotlin/lotto/domain/LuckyNumbers.kt +++ b/src/main/kotlin/lotto/domain/LuckyNumbers.kt @@ -1,6 +1,6 @@ package lotto.domain -import lotto.application.common.Number +import lotto.common.Number class LuckyNumbers( luckyNumbers: List, diff --git a/src/main/kotlin/lotto/domain/Payment.kt b/src/main/kotlin/lotto/domain/Payment.kt index ff9060844c..3b62e87d1b 100644 --- a/src/main/kotlin/lotto/domain/Payment.kt +++ b/src/main/kotlin/lotto/domain/Payment.kt @@ -1,6 +1,6 @@ package lotto.domain -import lotto.application.common.Number +import lotto.common.Number class Payment( val payment: Number diff --git a/src/main/kotlin/lotto/util/NumberGenerator.kt b/src/main/kotlin/lotto/util/NumberGenerator.kt index 9b8ec6e960..1167656b0b 100644 --- a/src/main/kotlin/lotto/util/NumberGenerator.kt +++ b/src/main/kotlin/lotto/util/NumberGenerator.kt @@ -1,6 +1,6 @@ package lotto.util -import lotto.application.common.Number +import lotto.common.Number interface NumberGenerator { fun generate(start: Int, end: Int, size: Int): List diff --git a/src/main/kotlin/lotto/util/RandomNumberGenerator.kt b/src/main/kotlin/lotto/util/RandomNumberGenerator.kt index 9c71fdc280..160b934f2b 100644 --- a/src/main/kotlin/lotto/util/RandomNumberGenerator.kt +++ b/src/main/kotlin/lotto/util/RandomNumberGenerator.kt @@ -1,6 +1,6 @@ package lotto.util -import lotto.application.common.Number +import lotto.common.Number class RandomNumberGenerator: NumberGenerator { override fun generate(start: Int, end: Int, size: Int): List { diff --git a/src/main/kotlin/lotto/view/InputView.kt b/src/main/kotlin/lotto/view/InputView.kt index be752433d3..348c56b05a 100644 --- a/src/main/kotlin/lotto/view/InputView.kt +++ b/src/main/kotlin/lotto/view/InputView.kt @@ -1,8 +1,8 @@ package lotto.view -import lotto.application.common.Number -import lotto.application.common.NumberString -import lotto.application.common.NumberStringList +import lotto.common.Number +import lotto.common.NumberString +import lotto.common.NumberStringList import lotto.domain.Payment class InputView { diff --git a/src/test/kotlin/lotto/application/common/NumberStringListTest.kt b/src/test/kotlin/lotto/application/common/NumberStringListTest.kt index ceda7ce766..bc292994a5 100644 --- a/src/test/kotlin/lotto/application/common/NumberStringListTest.kt +++ b/src/test/kotlin/lotto/application/common/NumberStringListTest.kt @@ -3,6 +3,8 @@ package lotto.application.common import io.kotest.assertions.throwables.shouldThrowExactly import io.kotest.core.spec.style.StringSpec import io.kotest.matchers.shouldBe +import lotto.common.Number +import lotto.common.NumberStringList class NumberStringListTest : StringSpec({ diff --git a/src/test/kotlin/lotto/application/common/NumberStringTest.kt b/src/test/kotlin/lotto/application/common/NumberStringTest.kt index 6a77a26f13..bfc0cc5e87 100644 --- a/src/test/kotlin/lotto/application/common/NumberStringTest.kt +++ b/src/test/kotlin/lotto/application/common/NumberStringTest.kt @@ -3,6 +3,8 @@ package lotto.application.common import io.kotest.assertions.throwables.shouldThrowExactly import io.kotest.core.spec.style.StringSpec import io.kotest.matchers.shouldBe +import lotto.common.Number +import lotto.common.NumberString class NumberStringTest : StringSpec({ diff --git a/src/test/kotlin/lotto/domain/LottoGeneratorTest.kt b/src/test/kotlin/lotto/domain/LottoGeneratorTest.kt index b4bf1b8ddc..8395b53a50 100644 --- a/src/test/kotlin/lotto/domain/LottoGeneratorTest.kt +++ b/src/test/kotlin/lotto/domain/LottoGeneratorTest.kt @@ -2,7 +2,7 @@ package lotto.domain import io.kotest.core.spec.style.StringSpec import io.kotest.matchers.collections.shouldHaveSize -import lotto.application.common.Number +import lotto.common.Number import lotto.util.RandomNumberGenerator class LottoGeneratorTest : StringSpec({ diff --git a/src/test/kotlin/lotto/domain/LottoShopTest.kt b/src/test/kotlin/lotto/domain/LottoShopTest.kt index 6eca8dbe09..aec3d93699 100644 --- a/src/test/kotlin/lotto/domain/LottoShopTest.kt +++ b/src/test/kotlin/lotto/domain/LottoShopTest.kt @@ -4,7 +4,7 @@ import io.kotest.core.spec.style.StringSpec import io.kotest.data.forAll import io.kotest.data.row import io.kotest.matchers.shouldBe -import lotto.application.common.Number +import lotto.common.Number import lotto.util.RandomNumberGenerator class LottoShopTest : StringSpec({ diff --git a/src/test/kotlin/lotto/domain/LottoStatisticsServiceTest.kt b/src/test/kotlin/lotto/domain/LottoStatisticsServiceTest.kt index 2d7b5a048f..c963d6e35b 100644 --- a/src/test/kotlin/lotto/domain/LottoStatisticsServiceTest.kt +++ b/src/test/kotlin/lotto/domain/LottoStatisticsServiceTest.kt @@ -5,7 +5,7 @@ import io.kotest.data.forAll import io.kotest.data.row import io.kotest.matchers.equality.shouldBeEqualToComparingFields import io.kotest.matchers.shouldBe -import lotto.application.common.Number +import lotto.common.Number class LottoStatisticsServiceTest : StringSpec({ diff --git a/src/test/kotlin/lotto/domain/LottoStatisticsTest.kt b/src/test/kotlin/lotto/domain/LottoStatisticsTest.kt index 4642429b77..243f303ed1 100644 --- a/src/test/kotlin/lotto/domain/LottoStatisticsTest.kt +++ b/src/test/kotlin/lotto/domain/LottoStatisticsTest.kt @@ -4,7 +4,7 @@ import io.kotest.core.spec.style.StringSpec import io.kotest.data.forAll import io.kotest.data.row import io.kotest.matchers.shouldBe -import lotto.application.common.Number +import lotto.common.Number class LottoStatisticsTest : StringSpec({ "로또 수익률 계산 테스트" { diff --git a/src/test/kotlin/lotto/domain/LottoTest.kt b/src/test/kotlin/lotto/domain/LottoTest.kt index cd72ea5fc0..bb096be518 100644 --- a/src/test/kotlin/lotto/domain/LottoTest.kt +++ b/src/test/kotlin/lotto/domain/LottoTest.kt @@ -3,7 +3,7 @@ package lotto.domain import io.kotest.assertions.throwables.shouldThrowExactly import io.kotest.core.spec.style.StringSpec import io.kotest.matchers.shouldBe -import lotto.application.common.Number +import lotto.common.Number class LottoTest : StringSpec({ diff --git a/src/test/kotlin/lotto/domain/LottoWinnerTest.kt b/src/test/kotlin/lotto/domain/LottoWinnerTest.kt index ebd902aa45..7238095c37 100644 --- a/src/test/kotlin/lotto/domain/LottoWinnerTest.kt +++ b/src/test/kotlin/lotto/domain/LottoWinnerTest.kt @@ -3,7 +3,7 @@ package lotto.domain import io.kotest.core.spec.style.StringSpec import io.kotest.matchers.collections.shouldContainInOrder import io.kotest.matchers.shouldBe -import lotto.application.common.Number +import lotto.common.Number class LottoWinnerTest : StringSpec({ diff --git a/src/test/kotlin/lotto/domain/LuckyNumbersTest.kt b/src/test/kotlin/lotto/domain/LuckyNumbersTest.kt index 3297405cea..71828daa4a 100644 --- a/src/test/kotlin/lotto/domain/LuckyNumbersTest.kt +++ b/src/test/kotlin/lotto/domain/LuckyNumbersTest.kt @@ -5,7 +5,7 @@ import io.kotest.core.spec.style.StringSpec import io.kotest.data.forAll import io.kotest.data.row import io.kotest.matchers.shouldBe -import lotto.application.common.Number +import lotto.common.Number class LuckyNumbersTest : StringSpec({ "당첨 번호가 6개를 넘으면 에러 발생 테스트" { From 6a205a9a2f75a4a6c6af51e5db75dd4368158a7e Mon Sep 17 00:00:00 2001 From: dajeong Date: Mon, 12 Dec 2022 10:57:50 +0900 Subject: [PATCH 11/21] =?UTF-8?q?[step4]=20=ED=81=B4=EB=9E=98=EC=8A=A4?= =?UTF-8?q?=EB=AA=85=20=EB=B3=80=EA=B2=BD=20Number=20->=20IntegerNumber?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit integer 타입만 다루고 있어 이름 변경 --- .../common/{Number.kt => IntegerNumber.kt} | 6 +- src/main/kotlin/lotto/common/NumberString.kt | 4 +- .../kotlin/lotto/common/NumberStringList.kt | 2 +- .../kotlin/lotto/domain/LottoGenerator.kt | 4 +- src/main/kotlin/lotto/domain/LottoNumber.kt | 4 +- src/main/kotlin/lotto/domain/LottoRank.kt | 20 +-- src/main/kotlin/lotto/domain/LottoShop.kt | 8 +- .../kotlin/lotto/domain/LottoStatistics.kt | 6 +- .../lotto/domain/LottoStatisticsResult.kt | 4 +- src/main/kotlin/lotto/domain/LuckyNumbers.kt | 10 +- src/main/kotlin/lotto/domain/Payment.kt | 4 +- src/main/kotlin/lotto/util/NumberGenerator.kt | 4 +- .../lotto/util/RandomNumberGenerator.kt | 8 +- src/main/kotlin/lotto/view/InputView.kt | 8 +- .../common/NumberStringListTest.kt | 6 +- .../common/NumberStringTest.kt | 12 +- .../kotlin/lotto/domain/LottoGeneratorTest.kt | 4 +- src/test/kotlin/lotto/domain/LottoShopTest.kt | 8 +- .../domain/LottoStatisticsServiceTest.kt | 24 ++-- .../lotto/domain/LottoStatisticsTest.kt | 18 +-- src/test/kotlin/lotto/domain/LottoTest.kt | 16 +-- .../kotlin/lotto/domain/LottoWinnerTest.kt | 64 ++++----- .../kotlin/lotto/domain/LuckyNumbersTest.kt | 124 +++++++++--------- 23 files changed, 184 insertions(+), 184 deletions(-) rename src/main/kotlin/lotto/common/{Number.kt => IntegerNumber.kt} (63%) rename src/test/kotlin/lotto/{application => }/common/NumberStringListTest.kt (85%) rename src/test/kotlin/lotto/{application => }/common/NumberStringTest.kt (87%) diff --git a/src/main/kotlin/lotto/common/Number.kt b/src/main/kotlin/lotto/common/IntegerNumber.kt similarity index 63% rename from src/main/kotlin/lotto/common/Number.kt rename to src/main/kotlin/lotto/common/IntegerNumber.kt index cdb8fdec68..9c359ebb19 100644 --- a/src/main/kotlin/lotto/common/Number.kt +++ b/src/main/kotlin/lotto/common/IntegerNumber.kt @@ -1,6 +1,6 @@ package lotto.common -data class Number( +data class IntegerNumber( val number: Int ) { fun isPositive(): Boolean { @@ -14,4 +14,8 @@ data class Number( fun toDouble(): Double { return number.toDouble() } + + fun divide(other: IntegerNumber): IntegerNumber { + return IntegerNumber(number.div(other.number)) + } } diff --git a/src/main/kotlin/lotto/common/NumberString.kt b/src/main/kotlin/lotto/common/NumberString.kt index ed1c4dc946..48e8e4ba24 100644 --- a/src/main/kotlin/lotto/common/NumberString.kt +++ b/src/main/kotlin/lotto/common/NumberString.kt @@ -12,7 +12,7 @@ class NumberString( return string.toCharArray().all { it in '0'..'9' } } - fun toNumber(): Number { - return Number(string.toInt()) + fun toNumber(): IntegerNumber { + return IntegerNumber(string.toInt()) } } diff --git a/src/main/kotlin/lotto/common/NumberStringList.kt b/src/main/kotlin/lotto/common/NumberStringList.kt index 0628d1598b..a8ec624ef3 100644 --- a/src/main/kotlin/lotto/common/NumberStringList.kt +++ b/src/main/kotlin/lotto/common/NumberStringList.kt @@ -5,7 +5,7 @@ class NumberStringList( ) { val list = string.split(",").map { NumberString(it.trim()) } - fun toNumberList(): List { + fun toNumberList(): List { return list.map { it.toNumber() } } } diff --git a/src/main/kotlin/lotto/domain/LottoGenerator.kt b/src/main/kotlin/lotto/domain/LottoGenerator.kt index 4c83a00540..cebbd4107a 100644 --- a/src/main/kotlin/lotto/domain/LottoGenerator.kt +++ b/src/main/kotlin/lotto/domain/LottoGenerator.kt @@ -1,12 +1,12 @@ package lotto.domain -import lotto.common.Number +import lotto.common.IntegerNumber import lotto.util.NumberGenerator class LottoGenerator( private val numberGenerator: NumberGenerator ) { - fun generate(size: Number): List { + fun generate(size: IntegerNumber): List { return List(size.number) { Lotto(randomNumber()) } } diff --git a/src/main/kotlin/lotto/domain/LottoNumber.kt b/src/main/kotlin/lotto/domain/LottoNumber.kt index 423b267bf2..1e5d9ac30e 100644 --- a/src/main/kotlin/lotto/domain/LottoNumber.kt +++ b/src/main/kotlin/lotto/domain/LottoNumber.kt @@ -1,9 +1,9 @@ package lotto.domain -import lotto.common.Number +import lotto.common.IntegerNumber data class LottoNumber( - val number: Number + val number: IntegerNumber ) { init { diff --git a/src/main/kotlin/lotto/domain/LottoRank.kt b/src/main/kotlin/lotto/domain/LottoRank.kt index 7e389ca620..86bca13353 100644 --- a/src/main/kotlin/lotto/domain/LottoRank.kt +++ b/src/main/kotlin/lotto/domain/LottoRank.kt @@ -1,18 +1,18 @@ package lotto.domain -import lotto.common.Number +import lotto.common.IntegerNumber enum class LottoRank( - val hitCount: Number, - val prizeMoney: Number, + val hitCount: IntegerNumber, + val prizeMoney: IntegerNumber, val hasBonusNumber: Boolean = false ) { - FIRST(hitCount = Number(6), prizeMoney = Number(2000000000)), - SECOND(hitCount = Number(5), prizeMoney = Number(30000000), hasBonusNumber = true), - THIRD(hitCount = Number(5), prizeMoney = Number(1500000)), - FOURTH(hitCount = Number(4), prizeMoney = Number(50000)), - FIFTH(hitCount = Number(3), prizeMoney = Number(5000)), - MISS(hitCount = Number(0), prizeMoney = Number(0)); + FIRST(hitCount = IntegerNumber(6), prizeMoney = IntegerNumber(2000000000)), + SECOND(hitCount = IntegerNumber(5), prizeMoney = IntegerNumber(30000000), hasBonusNumber = true), + THIRD(hitCount = IntegerNumber(5), prizeMoney = IntegerNumber(1500000)), + FOURTH(hitCount = IntegerNumber(4), prizeMoney = IntegerNumber(50000)), + FIFTH(hitCount = IntegerNumber(3), prizeMoney = IntegerNumber(5000)), + MISS(hitCount = IntegerNumber(0), prizeMoney = IntegerNumber(0)); private fun isHitBonusNumber(it: LottoRank, hasBonusNumber: Boolean): Boolean { if (it.hasBonusNumber) { @@ -22,7 +22,7 @@ enum class LottoRank( } companion object { - fun from(hitCount: Number, hasBonusNumber: Boolean): LottoRank { + fun from(hitCount: IntegerNumber, hasBonusNumber: Boolean): LottoRank { return values().find { it.hitCount == hitCount && it.isHitBonusNumber(it, hasBonusNumber) } ?: return MISS } diff --git a/src/main/kotlin/lotto/domain/LottoShop.kt b/src/main/kotlin/lotto/domain/LottoShop.kt index 1fc941d00f..786a3f3722 100644 --- a/src/main/kotlin/lotto/domain/LottoShop.kt +++ b/src/main/kotlin/lotto/domain/LottoShop.kt @@ -1,6 +1,6 @@ package lotto.domain -import lotto.common.Number +import lotto.common.IntegerNumber class LottoShop( private val lottoGenerator: LottoGenerator @@ -10,11 +10,11 @@ class LottoShop( return lottoGenerator.generate(lottoCount) } - private fun calculateLottoCount(payment: Payment): Number { - return Number(payment.payment.number / LOTTO_PRICE) + private fun calculateLottoCount(payment: Payment): IntegerNumber { + return payment.payment.divide(LOTTO_PRICE) } companion object { - private const val LOTTO_PRICE = 1000 + private val LOTTO_PRICE = IntegerNumber(1000) } } diff --git a/src/main/kotlin/lotto/domain/LottoStatistics.kt b/src/main/kotlin/lotto/domain/LottoStatistics.kt index 1166eb3932..5947cf72f9 100644 --- a/src/main/kotlin/lotto/domain/LottoStatistics.kt +++ b/src/main/kotlin/lotto/domain/LottoStatistics.kt @@ -1,12 +1,12 @@ package lotto.domain -import lotto.common.Number +import lotto.common.IntegerNumber import lotto.util.NumberUtil class LottoStatistics( private val winLottoList: List ) { - private val totalPrize: Number = Number(winLottoList.sumOf { it.prizeMoney.number }) + private val totalPrize: IntegerNumber = IntegerNumber(winLottoList.sumOf { it.prizeMoney.number }) fun earningRate(inputPayment: Payment): Double { val earningRate = totalPrize.toDouble() / inputPayment.payment.toDouble() @@ -19,7 +19,7 @@ class LottoStatistics( return LottoRank.winRanks().map { LottoStatisticsResult( lottoRank = it, - winLottoCount = Number(hitCountMap.getOrDefault(it, emptyList()).size) + winLottoCount = IntegerNumber(hitCountMap.getOrDefault(it, emptyList()).size) ) } } diff --git a/src/main/kotlin/lotto/domain/LottoStatisticsResult.kt b/src/main/kotlin/lotto/domain/LottoStatisticsResult.kt index c74b291d86..c07b2e174a 100644 --- a/src/main/kotlin/lotto/domain/LottoStatisticsResult.kt +++ b/src/main/kotlin/lotto/domain/LottoStatisticsResult.kt @@ -1,8 +1,8 @@ package lotto.domain -import lotto.common.Number +import lotto.common.IntegerNumber class LottoStatisticsResult( val lottoRank: LottoRank, - val winLottoCount: Number + val winLottoCount: IntegerNumber ) diff --git a/src/main/kotlin/lotto/domain/LuckyNumbers.kt b/src/main/kotlin/lotto/domain/LuckyNumbers.kt index 6cee8fde16..a320fb39ee 100644 --- a/src/main/kotlin/lotto/domain/LuckyNumbers.kt +++ b/src/main/kotlin/lotto/domain/LuckyNumbers.kt @@ -1,10 +1,10 @@ package lotto.domain -import lotto.common.Number +import lotto.common.IntegerNumber class LuckyNumbers( - luckyNumbers: List, - bonusNumber: Number + luckyNumbers: List, + bonusNumber: IntegerNumber ) { private val _luckyNumbers: List private val _bonusNumber: LottoNumber @@ -24,9 +24,9 @@ class LuckyNumbers( return LottoRank.from(hitCount, hasBonusNumber) } - private fun countHitNumbers(numbers: List): Number { + private fun countHitNumbers(numbers: List): IntegerNumber { val count = numbers.count { number -> _luckyNumbers.contains(number) } - return Number(count) + return IntegerNumber(count) } private fun containsBonusNumber(numbers: List): Boolean { diff --git a/src/main/kotlin/lotto/domain/Payment.kt b/src/main/kotlin/lotto/domain/Payment.kt index 3b62e87d1b..4903c2e502 100644 --- a/src/main/kotlin/lotto/domain/Payment.kt +++ b/src/main/kotlin/lotto/domain/Payment.kt @@ -1,9 +1,9 @@ package lotto.domain -import lotto.common.Number +import lotto.common.IntegerNumber class Payment( - val payment: Number + val payment: IntegerNumber ) { init { require(payment.isNegative().not()) { "지불액은 음수가 될 수 없습니다." } diff --git a/src/main/kotlin/lotto/util/NumberGenerator.kt b/src/main/kotlin/lotto/util/NumberGenerator.kt index 1167656b0b..1ab05319dc 100644 --- a/src/main/kotlin/lotto/util/NumberGenerator.kt +++ b/src/main/kotlin/lotto/util/NumberGenerator.kt @@ -1,7 +1,7 @@ package lotto.util -import lotto.common.Number +import lotto.common.IntegerNumber interface NumberGenerator { - fun generate(start: Int, end: Int, size: Int): List + fun generate(start: Int, end: Int, size: Int): List } diff --git a/src/main/kotlin/lotto/util/RandomNumberGenerator.kt b/src/main/kotlin/lotto/util/RandomNumberGenerator.kt index 160b934f2b..5c2a6dc931 100644 --- a/src/main/kotlin/lotto/util/RandomNumberGenerator.kt +++ b/src/main/kotlin/lotto/util/RandomNumberGenerator.kt @@ -1,12 +1,12 @@ package lotto.util -import lotto.common.Number +import lotto.common.IntegerNumber -class RandomNumberGenerator: NumberGenerator { - override fun generate(start: Int, end: Int, size: Int): List { +class RandomNumberGenerator : NumberGenerator { + override fun generate(start: Int, end: Int, size: Int): List { val range = start..end val shuffled = range.shuffled() val subList = shuffled.subList(0, size) - return subList.map { Number(it) } + return subList.map { IntegerNumber(it) } } } diff --git a/src/main/kotlin/lotto/view/InputView.kt b/src/main/kotlin/lotto/view/InputView.kt index 348c56b05a..113ff40f92 100644 --- a/src/main/kotlin/lotto/view/InputView.kt +++ b/src/main/kotlin/lotto/view/InputView.kt @@ -1,6 +1,6 @@ package lotto.view -import lotto.common.Number +import lotto.common.IntegerNumber import lotto.common.NumberString import lotto.common.NumberStringList import lotto.domain.Payment @@ -13,15 +13,15 @@ class InputView { return Payment(payment.toNumber()) } - fun inputLuckyNumbers(): List { + fun inputLuckyNumbers(): List { println(INPUT_LUCKY_NUMBERS_GUIDE) return NumberStringList(readln()).toNumberList() } - fun inputBonusNumber(): Number { + fun inputBonusNumber(): IntegerNumber { println(INPUT_BONUS_BALL) val bonusBallNumberString = readln() - return Number(bonusBallNumberString.toInt()) + return IntegerNumber(bonusBallNumberString.toInt()) } private companion object { diff --git a/src/test/kotlin/lotto/application/common/NumberStringListTest.kt b/src/test/kotlin/lotto/common/NumberStringListTest.kt similarity index 85% rename from src/test/kotlin/lotto/application/common/NumberStringListTest.kt rename to src/test/kotlin/lotto/common/NumberStringListTest.kt index bc292994a5..765c9aea56 100644 --- a/src/test/kotlin/lotto/application/common/NumberStringListTest.kt +++ b/src/test/kotlin/lotto/common/NumberStringListTest.kt @@ -1,10 +1,8 @@ -package lotto.application.common +package lotto.common import io.kotest.assertions.throwables.shouldThrowExactly import io.kotest.core.spec.style.StringSpec import io.kotest.matchers.shouldBe -import lotto.common.Number -import lotto.common.NumberStringList class NumberStringListTest : StringSpec({ @@ -35,6 +33,6 @@ class NumberStringListTest : StringSpec({ // when val numberList = numberStringList.toNumberList() // then - numberList shouldBe listOf(Number(1), Number(2), Number(3), Number(4), Number(5)) + numberList shouldBe listOf(IntegerNumber(1), IntegerNumber(2), IntegerNumber(3), IntegerNumber(4), IntegerNumber(5)) } }) diff --git a/src/test/kotlin/lotto/application/common/NumberStringTest.kt b/src/test/kotlin/lotto/common/NumberStringTest.kt similarity index 87% rename from src/test/kotlin/lotto/application/common/NumberStringTest.kt rename to src/test/kotlin/lotto/common/NumberStringTest.kt index bfc0cc5e87..e449d3a7b8 100644 --- a/src/test/kotlin/lotto/application/common/NumberStringTest.kt +++ b/src/test/kotlin/lotto/common/NumberStringTest.kt @@ -1,10 +1,8 @@ -package lotto.application.common +package lotto.common import io.kotest.assertions.throwables.shouldThrowExactly import io.kotest.core.spec.style.StringSpec import io.kotest.matchers.shouldBe -import lotto.common.Number -import lotto.common.NumberString class NumberStringTest : StringSpec({ @@ -31,7 +29,7 @@ class NumberStringTest : StringSpec({ "양수 판단 테스트" { // given - val number = Number(1) + val number = IntegerNumber(1) // when val actual = number.isPositive() // then @@ -40,7 +38,7 @@ class NumberStringTest : StringSpec({ "음수 판단 테스트" { // given - val number = Number(-1) + val number = IntegerNumber(-1) // when val actual = number.isNegative() // then @@ -49,10 +47,10 @@ class NumberStringTest : StringSpec({ "double 타입으로 변환 테스트" { // given - val number = Number(1) + val number = IntegerNumber(1) // when val actual = number.toDouble() // then actual shouldBe 1.0 } -}) \ No newline at end of file +}) diff --git a/src/test/kotlin/lotto/domain/LottoGeneratorTest.kt b/src/test/kotlin/lotto/domain/LottoGeneratorTest.kt index 8395b53a50..c0cc4fd176 100644 --- a/src/test/kotlin/lotto/domain/LottoGeneratorTest.kt +++ b/src/test/kotlin/lotto/domain/LottoGeneratorTest.kt @@ -2,7 +2,7 @@ package lotto.domain import io.kotest.core.spec.style.StringSpec import io.kotest.matchers.collections.shouldHaveSize -import lotto.common.Number +import lotto.common.IntegerNumber import lotto.util.RandomNumberGenerator class LottoGeneratorTest : StringSpec({ @@ -11,7 +11,7 @@ class LottoGeneratorTest : StringSpec({ "로또 생성 개수 확인 테스트" { // given - val inputSize = Number(24) + val inputSize = IntegerNumber(24) // when val resultLotto = lottoGenerator.generate(inputSize) // then diff --git a/src/test/kotlin/lotto/domain/LottoShopTest.kt b/src/test/kotlin/lotto/domain/LottoShopTest.kt index aec3d93699..8496944a09 100644 --- a/src/test/kotlin/lotto/domain/LottoShopTest.kt +++ b/src/test/kotlin/lotto/domain/LottoShopTest.kt @@ -4,7 +4,7 @@ import io.kotest.core.spec.style.StringSpec import io.kotest.data.forAll import io.kotest.data.row import io.kotest.matchers.shouldBe -import lotto.common.Number +import lotto.common.IntegerNumber import lotto.util.RandomNumberGenerator class LottoShopTest : StringSpec({ @@ -14,9 +14,9 @@ class LottoShopTest : StringSpec({ "로또 구매 테스트" { forAll( // given - row("0원을 내면 0개를 반환한다.", Payment(Number(0)), 0), - row("5000원을 내면 5개를 반환한다.", Payment(Number(5000)), 5), - row("5500원을 내면 5개를 반환한다.", Payment(Number(5500)), 5) + row("0원을 내면 0개를 반환한다.", Payment(IntegerNumber(0)), 0), + row("5000원을 내면 5개를 반환한다.", Payment(IntegerNumber(5000)), 5), + row("5500원을 내면 5개를 반환한다.", Payment(IntegerNumber(5500)), 5) ) { title, payment, expectedSize -> // when val actual = lottoShop.buyLotto(payment) diff --git a/src/test/kotlin/lotto/domain/LottoStatisticsServiceTest.kt b/src/test/kotlin/lotto/domain/LottoStatisticsServiceTest.kt index c963d6e35b..a2dee42b5e 100644 --- a/src/test/kotlin/lotto/domain/LottoStatisticsServiceTest.kt +++ b/src/test/kotlin/lotto/domain/LottoStatisticsServiceTest.kt @@ -5,32 +5,32 @@ import io.kotest.data.forAll import io.kotest.data.row import io.kotest.matchers.equality.shouldBeEqualToComparingFields import io.kotest.matchers.shouldBe -import lotto.common.Number +import lotto.common.IntegerNumber class LottoStatisticsServiceTest : StringSpec({ "당첨자 통계 통합 결과 테스트" { // given - val payment = Payment(Number(15000)) + val payment = Payment(IntegerNumber(15000)) forAll( row( LottoRank.FOURTH, listOf( - LottoStatisticsResult(LottoRank.FIFTH, Number(0)), - LottoStatisticsResult(LottoRank.FOURTH, Number(1)), - LottoStatisticsResult(LottoRank.THIRD, Number(0)), - LottoStatisticsResult(LottoRank.SECOND, Number(0)), - LottoStatisticsResult(LottoRank.FIRST, Number(0)), + LottoStatisticsResult(LottoRank.FIFTH, IntegerNumber(0)), + LottoStatisticsResult(LottoRank.FOURTH, IntegerNumber(1)), + LottoStatisticsResult(LottoRank.THIRD, IntegerNumber(0)), + LottoStatisticsResult(LottoRank.SECOND, IntegerNumber(0)), + LottoStatisticsResult(LottoRank.FIRST, IntegerNumber(0)), ) ), row( LottoRank.FIRST, listOf( - LottoStatisticsResult(LottoRank.FIFTH, Number(0)), - LottoStatisticsResult(LottoRank.FOURTH, Number(0)), - LottoStatisticsResult(LottoRank.THIRD, Number(0)), - LottoStatisticsResult(LottoRank.SECOND, Number(0)), - LottoStatisticsResult(LottoRank.FIRST, Number(1)), + LottoStatisticsResult(LottoRank.FIFTH, IntegerNumber(0)), + LottoStatisticsResult(LottoRank.FOURTH, IntegerNumber(0)), + LottoStatisticsResult(LottoRank.THIRD, IntegerNumber(0)), + LottoStatisticsResult(LottoRank.SECOND, IntegerNumber(0)), + LottoStatisticsResult(LottoRank.FIRST, IntegerNumber(1)), ) ) ) { lottoResult, expected -> diff --git a/src/test/kotlin/lotto/domain/LottoStatisticsTest.kt b/src/test/kotlin/lotto/domain/LottoStatisticsTest.kt index 243f303ed1..38e15a4abc 100644 --- a/src/test/kotlin/lotto/domain/LottoStatisticsTest.kt +++ b/src/test/kotlin/lotto/domain/LottoStatisticsTest.kt @@ -4,14 +4,14 @@ import io.kotest.core.spec.style.StringSpec import io.kotest.data.forAll import io.kotest.data.row import io.kotest.matchers.shouldBe -import lotto.common.Number +import lotto.common.IntegerNumber class LottoStatisticsTest : StringSpec({ "로또 수익률 계산 테스트" { forAll( - row(listOf(LottoRank.FOURTH, LottoRank.FIFTH), Payment(Number(100000)), 0.55), - row(listOf(LottoRank.FIFTH), Payment(Number(5000)), 1.0), - row(listOf(LottoRank.FOURTH), Payment(Number(5000)), 10), + row(listOf(LottoRank.FOURTH, LottoRank.FIFTH), Payment(IntegerNumber(100000)), 0.55), + row(listOf(LottoRank.FIFTH), Payment(IntegerNumber(5000)), 1.0), + row(listOf(LottoRank.FOURTH), Payment(IntegerNumber(5000)), 10), ) { prizeList, payment, expectedEarningRate -> // given val lottoStatistics = LottoStatistics(prizeList) @@ -35,11 +35,11 @@ class LottoStatisticsTest : StringSpec({ val lottoStatistics = LottoStatistics(winLottoList) val expected = listOf( - LottoStatisticsResult(LottoRank.FIFTH, Number(3)), - LottoStatisticsResult(LottoRank.FOURTH, Number(0)), - LottoStatisticsResult(LottoRank.THIRD, Number(2)), - LottoStatisticsResult(LottoRank.SECOND, Number(0)), - LottoStatisticsResult(LottoRank.FIRST, Number(1)), + LottoStatisticsResult(LottoRank.FIFTH, IntegerNumber(3)), + LottoStatisticsResult(LottoRank.FOURTH, IntegerNumber(0)), + LottoStatisticsResult(LottoRank.THIRD, IntegerNumber(2)), + LottoStatisticsResult(LottoRank.SECOND, IntegerNumber(0)), + LottoStatisticsResult(LottoRank.FIRST, IntegerNumber(1)), ) // when diff --git a/src/test/kotlin/lotto/domain/LottoTest.kt b/src/test/kotlin/lotto/domain/LottoTest.kt index bb096be518..aab6c9f646 100644 --- a/src/test/kotlin/lotto/domain/LottoTest.kt +++ b/src/test/kotlin/lotto/domain/LottoTest.kt @@ -3,19 +3,19 @@ package lotto.domain import io.kotest.assertions.throwables.shouldThrowExactly import io.kotest.core.spec.style.StringSpec import io.kotest.matchers.shouldBe -import lotto.common.Number +import lotto.common.IntegerNumber class LottoTest : StringSpec({ "로또 번호가 6개를 넘으면 에러 발생 테스트" { val numbers = listOf( - LottoNumber(Number(1)), - LottoNumber(Number(2)), - LottoNumber(Number(3)), - LottoNumber(Number(4)), - LottoNumber(Number(5)), - LottoNumber(Number(6)), - LottoNumber(Number(7)) + LottoNumber(IntegerNumber(1)), + LottoNumber(IntegerNumber(2)), + LottoNumber(IntegerNumber(3)), + LottoNumber(IntegerNumber(4)), + LottoNumber(IntegerNumber(5)), + LottoNumber(IntegerNumber(6)), + LottoNumber(IntegerNumber(7)) ) val exception = shouldThrowExactly { Lotto(numbers) diff --git a/src/test/kotlin/lotto/domain/LottoWinnerTest.kt b/src/test/kotlin/lotto/domain/LottoWinnerTest.kt index 7238095c37..cc4f4379f8 100644 --- a/src/test/kotlin/lotto/domain/LottoWinnerTest.kt +++ b/src/test/kotlin/lotto/domain/LottoWinnerTest.kt @@ -3,61 +3,61 @@ package lotto.domain import io.kotest.core.spec.style.StringSpec import io.kotest.matchers.collections.shouldContainInOrder import io.kotest.matchers.shouldBe -import lotto.common.Number +import lotto.common.IntegerNumber class LottoWinnerTest : StringSpec({ "당첨 로또 선택 테스트" { // given val luckyNumbers = listOf( - Number(1), - Number(3), - Number(5), - Number(7), - Number(9), - Number(11) + IntegerNumber(1), + IntegerNumber(3), + IntegerNumber(5), + IntegerNumber(7), + IntegerNumber(9), + IntegerNumber(11) ) - val bonusNumber = Number(13) + val bonusNumber = IntegerNumber(13) val lottoWinner = LottoWinner(LuckyNumbers(luckyNumbers, bonusNumber)) val fourthWinLotto = Lotto( listOf( - LottoNumber(Number(5)), - LottoNumber(Number(7)), - LottoNumber(Number(9)), - LottoNumber(Number(11)), - LottoNumber(Number(12)), - LottoNumber(Number(13)) + LottoNumber(IntegerNumber(5)), + LottoNumber(IntegerNumber(7)), + LottoNumber(IntegerNumber(9)), + LottoNumber(IntegerNumber(11)), + LottoNumber(IntegerNumber(12)), + LottoNumber(IntegerNumber(13)) ) ) val secondWinLotto = Lotto( listOf( - LottoNumber(Number(3)), - LottoNumber(Number(5)), - LottoNumber(Number(7)), - LottoNumber(Number(9)), - LottoNumber(Number(11)), - LottoNumber(Number(13)) + LottoNumber(IntegerNumber(3)), + LottoNumber(IntegerNumber(5)), + LottoNumber(IntegerNumber(7)), + LottoNumber(IntegerNumber(9)), + LottoNumber(IntegerNumber(11)), + LottoNumber(IntegerNumber(13)) ) ) val thirdWinLotto = Lotto( listOf( - LottoNumber(Number(3)), - LottoNumber(Number(5)), - LottoNumber(Number(7)), - LottoNumber(Number(9)), - LottoNumber(Number(11)), - LottoNumber(Number(15)) + LottoNumber(IntegerNumber(3)), + LottoNumber(IntegerNumber(5)), + LottoNumber(IntegerNumber(7)), + LottoNumber(IntegerNumber(9)), + LottoNumber(IntegerNumber(11)), + LottoNumber(IntegerNumber(15)) ) ) val notWintLotto = Lotto( listOf( - LottoNumber(Number(2)), - LottoNumber(Number(4)), - LottoNumber(Number(6)), - LottoNumber(Number(8)), - LottoNumber(Number(10)), - LottoNumber(Number(13)) + LottoNumber(IntegerNumber(2)), + LottoNumber(IntegerNumber(4)), + LottoNumber(IntegerNumber(6)), + LottoNumber(IntegerNumber(8)), + LottoNumber(IntegerNumber(10)), + LottoNumber(IntegerNumber(13)) ) ) // when diff --git a/src/test/kotlin/lotto/domain/LuckyNumbersTest.kt b/src/test/kotlin/lotto/domain/LuckyNumbersTest.kt index 71828daa4a..3d09cd0d66 100644 --- a/src/test/kotlin/lotto/domain/LuckyNumbersTest.kt +++ b/src/test/kotlin/lotto/domain/LuckyNumbersTest.kt @@ -5,51 +5,51 @@ import io.kotest.core.spec.style.StringSpec import io.kotest.data.forAll import io.kotest.data.row import io.kotest.matchers.shouldBe -import lotto.common.Number +import lotto.common.IntegerNumber class LuckyNumbersTest : StringSpec({ "당첨 번호가 6개를 넘으면 에러 발생 테스트" { val numbers = listOf( - Number(1), - Number(2), - Number(3), - Number(4), - Number(5), - Number(6), - Number(7) + IntegerNumber(1), + IntegerNumber(2), + IntegerNumber(3), + IntegerNumber(4), + IntegerNumber(5), + IntegerNumber(6), + IntegerNumber(7) ) val exception = shouldThrowExactly { - LuckyNumbers(luckyNumbers = numbers, bonusNumber = Number(10)) + LuckyNumbers(luckyNumbers = numbers, bonusNumber = IntegerNumber(10)) } exception.message shouldBe "당첨 번호는 6개가 필요합니다." } "당첨 번호가 중복이면 에러 발생 테스트" { val numbers = listOf( - Number(1), - Number(2), - Number(3), - Number(4), - Number(6), - Number(6) + IntegerNumber(1), + IntegerNumber(2), + IntegerNumber(3), + IntegerNumber(4), + IntegerNumber(6), + IntegerNumber(6) ) val exception = shouldThrowExactly { - LuckyNumbers(luckyNumbers = numbers, bonusNumber = Number(10)) + LuckyNumbers(luckyNumbers = numbers, bonusNumber = IntegerNumber(10)) } exception.message shouldBe "당첨 번호에 중복이 있습니다." } "보너스볼 중복 에러 테스트" { val numbers = listOf( - Number(1), - Number(2), - Number(3), - Number(4), - Number(5), - Number(6) + IntegerNumber(1), + IntegerNumber(2), + IntegerNumber(3), + IntegerNumber(4), + IntegerNumber(5), + IntegerNumber(6) ) val exception = shouldThrowExactly { - LuckyNumbers(luckyNumbers = numbers, bonusNumber = Number(6)) + LuckyNumbers(luckyNumbers = numbers, bonusNumber = IntegerNumber(6)) } exception.message shouldBe "보너스볼은 당첨번호와 중복될 수 없습니다." } @@ -61,21 +61,21 @@ class LuckyNumbersTest : StringSpec({ "0개 일치하는 로또", LuckyNumbers( luckyNumbers = listOf( - Number(1), - Number(3), - Number(5), - Number(7), - Number(9), - Number(11) - ), bonusNumber = Number(13) + IntegerNumber(1), + IntegerNumber(3), + IntegerNumber(5), + IntegerNumber(7), + IntegerNumber(9), + IntegerNumber(11) + ), bonusNumber = IntegerNumber(13) ), listOf( - LottoNumber(Number(2)), - LottoNumber(Number(4)), - LottoNumber(Number(6)), - LottoNumber(Number(8)), - LottoNumber(Number(10)), - LottoNumber(Number(12)) + LottoNumber(IntegerNumber(2)), + LottoNumber(IntegerNumber(4)), + LottoNumber(IntegerNumber(6)), + LottoNumber(IntegerNumber(8)), + LottoNumber(IntegerNumber(10)), + LottoNumber(IntegerNumber(12)) ), LottoRank.MISS ), @@ -83,21 +83,21 @@ class LuckyNumbersTest : StringSpec({ "3개 일치하는 로또", LuckyNumbers( luckyNumbers = listOf( - Number(1), - Number(3), - Number(5), - Number(7), - Number(9), - Number(11) - ), bonusNumber = Number(13) + IntegerNumber(1), + IntegerNumber(3), + IntegerNumber(5), + IntegerNumber(7), + IntegerNumber(9), + IntegerNumber(11) + ), bonusNumber = IntegerNumber(13) ), listOf( - LottoNumber(Number(7)), - LottoNumber(Number(8)), - LottoNumber(Number(9)), - LottoNumber(Number(10)), - LottoNumber(Number(11)), - LottoNumber(Number(12)) + LottoNumber(IntegerNumber(7)), + LottoNumber(IntegerNumber(8)), + LottoNumber(IntegerNumber(9)), + LottoNumber(IntegerNumber(10)), + LottoNumber(IntegerNumber(11)), + LottoNumber(IntegerNumber(12)) ), LottoRank.FIFTH ), @@ -105,21 +105,21 @@ class LuckyNumbersTest : StringSpec({ "5개 일치+보너스번호 일치하는 로또", LuckyNumbers( luckyNumbers = listOf( - Number(1), - Number(3), - Number(5), - Number(7), - Number(9), - Number(11) - ), bonusNumber = Number(13) + IntegerNumber(1), + IntegerNumber(3), + IntegerNumber(5), + IntegerNumber(7), + IntegerNumber(9), + IntegerNumber(11) + ), bonusNumber = IntegerNumber(13) ), listOf( - LottoNumber(Number(1)), - LottoNumber(Number(3)), - LottoNumber(Number(5)), - LottoNumber(Number(7)), - LottoNumber(Number(9)), - LottoNumber(Number(13)) + LottoNumber(IntegerNumber(1)), + LottoNumber(IntegerNumber(3)), + LottoNumber(IntegerNumber(5)), + LottoNumber(IntegerNumber(7)), + LottoNumber(IntegerNumber(9)), + LottoNumber(IntegerNumber(13)) ), LottoRank.SECOND ) From 0db5155e75ab52b34f93281ff41834b077061b48 Mon Sep 17 00:00:00 2001 From: dajeong Date: Mon, 12 Dec 2022 11:40:46 +0900 Subject: [PATCH 12/21] =?UTF-8?q?[step4]=20=EC=8B=A4=EC=88=98=EA=B0=92=20?= =?UTF-8?q?=ED=8F=AC=EC=9E=A5=20=ED=81=B4=EB=9E=98=EC=8A=A4=20DoubleNumber?= =?UTF-8?q?=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/lotto/README.md | 3 +- src/main/kotlin/lotto/common/DoubleNumber.kt | 33 +++++++++++++++++++ src/main/kotlin/lotto/common/IntegerNumber.kt | 4 +-- .../kotlin/lotto/domain/LottoStatistics.kt | 7 ++-- .../lotto/domain/LottoStatisticsTotal.kt | 4 ++- src/main/kotlin/lotto/util/NumberUtil.kt | 13 ++++---- src/main/kotlin/lotto/view/ResultView.kt | 5 +-- .../kotlin/lotto/common/NumberStringTest.kt | 2 +- .../lotto/domain/LottoStatisticsTest.kt | 7 ++-- src/test/kotlin/lotto/util/NumberUtilTest.kt | 7 ++-- 10 files changed, 62 insertions(+), 23 deletions(-) create mode 100644 src/main/kotlin/lotto/common/DoubleNumber.kt diff --git a/src/main/kotlin/lotto/README.md b/src/main/kotlin/lotto/README.md index 64cdc5f421..2b780fecd4 100644 --- a/src/main/kotlin/lotto/README.md +++ b/src/main/kotlin/lotto/README.md @@ -1,7 +1,8 @@ # TODO ## 구현할 '로또(수동)' 기능 목록 -- [x] 모든 원시값 포장 클래스 생성 +- [x] 정수값 포장 클래스 생성 +- [x] 실수값 포장 클래스 생성 - [x] 입력 문자열 포장 클래스 생성 - [ ] 수동으로 구매할 로또 수 입력 기능 - [ ] 수동으로 구매할 로또 번호 입력 기능 diff --git a/src/main/kotlin/lotto/common/DoubleNumber.kt b/src/main/kotlin/lotto/common/DoubleNumber.kt new file mode 100644 index 0000000000..a1a097353a --- /dev/null +++ b/src/main/kotlin/lotto/common/DoubleNumber.kt @@ -0,0 +1,33 @@ +package lotto.common + +import kotlin.math.pow + +data class DoubleNumber( + val number: Double +) { + constructor(number: IntegerNumber) : this(number.number.toDouble()) + + fun divide(other: DoubleNumber): DoubleNumber { + return DoubleNumber(number.div(other.number)) + } + + fun pow(x: DoubleNumber): DoubleNumber { + return DoubleNumber(number.pow(x.number)) + } + + fun multiply(other: DoubleNumber): DoubleNumber { + return DoubleNumber(number.times(other.number)) + } + + fun floor(): DoubleNumber { + return DoubleNumber(kotlin.math.floor(number)) + } + + fun isNegative(): Boolean { + return number < 0 + } + + fun isGreaterThanEquals(other: DoubleNumber): Boolean { + return number >= other.number + } +} diff --git a/src/main/kotlin/lotto/common/IntegerNumber.kt b/src/main/kotlin/lotto/common/IntegerNumber.kt index 9c359ebb19..3206ba1938 100644 --- a/src/main/kotlin/lotto/common/IntegerNumber.kt +++ b/src/main/kotlin/lotto/common/IntegerNumber.kt @@ -11,8 +11,8 @@ data class IntegerNumber( return number < 0 } - fun toDouble(): Double { - return number.toDouble() + fun toDouble(): DoubleNumber { + return DoubleNumber(this) } fun divide(other: IntegerNumber): IntegerNumber { diff --git a/src/main/kotlin/lotto/domain/LottoStatistics.kt b/src/main/kotlin/lotto/domain/LottoStatistics.kt index 5947cf72f9..08404f78cf 100644 --- a/src/main/kotlin/lotto/domain/LottoStatistics.kt +++ b/src/main/kotlin/lotto/domain/LottoStatistics.kt @@ -1,5 +1,6 @@ package lotto.domain +import lotto.common.DoubleNumber import lotto.common.IntegerNumber import lotto.util.NumberUtil @@ -8,8 +9,8 @@ class LottoStatistics( ) { private val totalPrize: IntegerNumber = IntegerNumber(winLottoList.sumOf { it.prizeMoney.number }) - fun earningRate(inputPayment: Payment): Double { - val earningRate = totalPrize.toDouble() / inputPayment.payment.toDouble() + fun earningRate(inputPayment: Payment): DoubleNumber { + val earningRate = totalPrize.toDouble().divide(inputPayment.payment.toDouble()) return NumberUtil.floor(earningRate, EARNING_RATE_DECIMAL_PLACE) } @@ -25,6 +26,6 @@ class LottoStatistics( } companion object { - private const val EARNING_RATE_DECIMAL_PLACE = 2 + private val EARNING_RATE_DECIMAL_PLACE = DoubleNumber(2.0) } } diff --git a/src/main/kotlin/lotto/domain/LottoStatisticsTotal.kt b/src/main/kotlin/lotto/domain/LottoStatisticsTotal.kt index f261a191f1..e006ffb058 100644 --- a/src/main/kotlin/lotto/domain/LottoStatisticsTotal.kt +++ b/src/main/kotlin/lotto/domain/LottoStatisticsTotal.kt @@ -1,6 +1,8 @@ package lotto.domain +import lotto.common.DoubleNumber + class LottoStatisticsTotal( - val earningRate: Double, + val earningRate: DoubleNumber, val winLottoStatisticsResult: List ) diff --git a/src/main/kotlin/lotto/util/NumberUtil.kt b/src/main/kotlin/lotto/util/NumberUtil.kt index 77a445ef60..820a69b123 100644 --- a/src/main/kotlin/lotto/util/NumberUtil.kt +++ b/src/main/kotlin/lotto/util/NumberUtil.kt @@ -1,15 +1,14 @@ package lotto.util -import kotlin.math.floor -import kotlin.math.pow +import lotto.common.DoubleNumber object NumberUtil { - fun floor(number: Double, decimalPlace: Int): Double { - if (decimalPlace == -1) { + fun floor(number: DoubleNumber, decimalPlace: DoubleNumber): DoubleNumber { + if (decimalPlace.isNegative()) { return number } - val pow = 10.0.pow(decimalPlace.toDouble()) - val floor = floor(number * pow) - return floor / pow + val pow = DoubleNumber(10.0).pow(decimalPlace) + val floor = number.multiply(pow).floor() + return floor.divide(pow) } } diff --git a/src/main/kotlin/lotto/view/ResultView.kt b/src/main/kotlin/lotto/view/ResultView.kt index d38089efaa..3875d9cfb1 100644 --- a/src/main/kotlin/lotto/view/ResultView.kt +++ b/src/main/kotlin/lotto/view/ResultView.kt @@ -1,5 +1,6 @@ package lotto.view +import lotto.common.DoubleNumber import lotto.domain.Lotto import lotto.domain.LottoRank import lotto.domain.LottoStatisticsResult @@ -39,9 +40,9 @@ class ResultView { return "" } - private fun printEarningRate(earningRate: Double) { + private fun printEarningRate(earningRate: DoubleNumber) { print("총 수익률은 ${earningRate}입니다. ") - if (earningRate > 1) { + if (earningRate.isGreaterThanEquals(DoubleNumber(1.0))) { println("(기준이 1이기 때문에 이익입니다.)") return } diff --git a/src/test/kotlin/lotto/common/NumberStringTest.kt b/src/test/kotlin/lotto/common/NumberStringTest.kt index e449d3a7b8..9ab9e93a8b 100644 --- a/src/test/kotlin/lotto/common/NumberStringTest.kt +++ b/src/test/kotlin/lotto/common/NumberStringTest.kt @@ -51,6 +51,6 @@ class NumberStringTest : StringSpec({ // when val actual = number.toDouble() // then - actual shouldBe 1.0 + actual shouldBe DoubleNumber(1.0) } }) diff --git a/src/test/kotlin/lotto/domain/LottoStatisticsTest.kt b/src/test/kotlin/lotto/domain/LottoStatisticsTest.kt index 38e15a4abc..5d398a4127 100644 --- a/src/test/kotlin/lotto/domain/LottoStatisticsTest.kt +++ b/src/test/kotlin/lotto/domain/LottoStatisticsTest.kt @@ -4,14 +4,15 @@ import io.kotest.core.spec.style.StringSpec import io.kotest.data.forAll import io.kotest.data.row import io.kotest.matchers.shouldBe +import lotto.common.DoubleNumber import lotto.common.IntegerNumber class LottoStatisticsTest : StringSpec({ "로또 수익률 계산 테스트" { forAll( - row(listOf(LottoRank.FOURTH, LottoRank.FIFTH), Payment(IntegerNumber(100000)), 0.55), - row(listOf(LottoRank.FIFTH), Payment(IntegerNumber(5000)), 1.0), - row(listOf(LottoRank.FOURTH), Payment(IntegerNumber(5000)), 10), + row(listOf(LottoRank.FOURTH, LottoRank.FIFTH), Payment(IntegerNumber(100000)), DoubleNumber(0.55)), + row(listOf(LottoRank.FIFTH), Payment(IntegerNumber(5000)), DoubleNumber(1.0)), + row(listOf(LottoRank.FOURTH), Payment(IntegerNumber(5000)), DoubleNumber(10.0)), ) { prizeList, payment, expectedEarningRate -> // given val lottoStatistics = LottoStatistics(prizeList) diff --git a/src/test/kotlin/lotto/util/NumberUtilTest.kt b/src/test/kotlin/lotto/util/NumberUtilTest.kt index 2eadd4a249..1a7ed55152 100644 --- a/src/test/kotlin/lotto/util/NumberUtilTest.kt +++ b/src/test/kotlin/lotto/util/NumberUtilTest.kt @@ -4,15 +4,16 @@ import io.kotest.core.spec.style.StringSpec import io.kotest.data.forAll import io.kotest.data.row import io.kotest.matchers.shouldBe +import lotto.common.DoubleNumber class NumberUtilTest : StringSpec({ "소숫점 지정 자리수 이하 버림 테스트" { forAll( // given - row(1.2345, 0, 1.0), - row(1.2345, 3, 1.234), - row(1.2345, -1, 1.2345) + row(DoubleNumber(1.2345), DoubleNumber(0.0), DoubleNumber(1.0)), + row(DoubleNumber(1.2345), DoubleNumber(3.0), DoubleNumber(1.234)), + row(DoubleNumber(1.2345), DoubleNumber(-1.0), DoubleNumber(1.2345)) ) { number, decimalPlace, expectedResult -> // when val actualResult = NumberUtil.floor(number, decimalPlace) From 76bd30259f0ed55475eb474de965ae3348c4ccd0 Mon Sep 17 00:00:00 2001 From: dajeong Date: Mon, 12 Dec 2022 19:09:51 +0900 Subject: [PATCH 13/21] =?UTF-8?q?[step4]=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80,=20=EC=BD=94=EB=93=9C=20=EC=A0=95=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/lotto/common/NumberString.kt | 2 +- src/main/kotlin/lotto/common/NumberStringList.kt | 4 ++-- src/main/kotlin/lotto/view/InputView.kt | 7 +++---- .../kotlin/lotto/common/NumberStringListTest.kt | 2 +- src/test/kotlin/lotto/domain/PaymentTest.kt | 15 +++++++++++++++ 5 files changed, 22 insertions(+), 8 deletions(-) create mode 100644 src/test/kotlin/lotto/domain/PaymentTest.kt diff --git a/src/main/kotlin/lotto/common/NumberString.kt b/src/main/kotlin/lotto/common/NumberString.kt index 48e8e4ba24..d895209d6a 100644 --- a/src/main/kotlin/lotto/common/NumberString.kt +++ b/src/main/kotlin/lotto/common/NumberString.kt @@ -12,7 +12,7 @@ class NumberString( return string.toCharArray().all { it in '0'..'9' } } - fun toNumber(): IntegerNumber { + fun toIntegerNumber(): IntegerNumber { return IntegerNumber(string.toInt()) } } diff --git a/src/main/kotlin/lotto/common/NumberStringList.kt b/src/main/kotlin/lotto/common/NumberStringList.kt index a8ec624ef3..87e0a0f01d 100644 --- a/src/main/kotlin/lotto/common/NumberStringList.kt +++ b/src/main/kotlin/lotto/common/NumberStringList.kt @@ -5,7 +5,7 @@ class NumberStringList( ) { val list = string.split(",").map { NumberString(it.trim()) } - fun toNumberList(): List { - return list.map { it.toNumber() } + fun toIntegerNumberList(): List { + return list.map { it.toIntegerNumber() } } } diff --git a/src/main/kotlin/lotto/view/InputView.kt b/src/main/kotlin/lotto/view/InputView.kt index 113ff40f92..79c8087559 100644 --- a/src/main/kotlin/lotto/view/InputView.kt +++ b/src/main/kotlin/lotto/view/InputView.kt @@ -10,18 +10,17 @@ class InputView { fun inputPayment(): Payment { println(INPUT_PAYMENT_GUIDE) val payment = NumberString(readln()) - return Payment(payment.toNumber()) + return Payment(payment.toIntegerNumber()) } fun inputLuckyNumbers(): List { println(INPUT_LUCKY_NUMBERS_GUIDE) - return NumberStringList(readln()).toNumberList() + return NumberStringList(readln()).toIntegerNumberList() } fun inputBonusNumber(): IntegerNumber { println(INPUT_BONUS_BALL) - val bonusBallNumberString = readln() - return IntegerNumber(bonusBallNumberString.toInt()) + return NumberString(readln()).toIntegerNumber() } private companion object { diff --git a/src/test/kotlin/lotto/common/NumberStringListTest.kt b/src/test/kotlin/lotto/common/NumberStringListTest.kt index 765c9aea56..996f127503 100644 --- a/src/test/kotlin/lotto/common/NumberStringListTest.kt +++ b/src/test/kotlin/lotto/common/NumberStringListTest.kt @@ -31,7 +31,7 @@ class NumberStringListTest : StringSpec({ // given val numberStringList = NumberStringList("1,2,3,4,5") // when - val numberList = numberStringList.toNumberList() + val numberList = numberStringList.toIntegerNumberList() // then numberList shouldBe listOf(IntegerNumber(1), IntegerNumber(2), IntegerNumber(3), IntegerNumber(4), IntegerNumber(5)) } diff --git a/src/test/kotlin/lotto/domain/PaymentTest.kt b/src/test/kotlin/lotto/domain/PaymentTest.kt new file mode 100644 index 0000000000..90baf9b22a --- /dev/null +++ b/src/test/kotlin/lotto/domain/PaymentTest.kt @@ -0,0 +1,15 @@ +package lotto.domain + +import io.kotest.assertions.throwables.shouldThrowExactly +import io.kotest.core.spec.style.StringSpec +import io.kotest.matchers.shouldBe +import lotto.common.IntegerNumber + +class PaymentTest : StringSpec({ + "숫자 아닐때 에러 발생 테스트" { + val exception = shouldThrowExactly { + Payment(IntegerNumber(-1)) + } + exception.message shouldBe "지불액은 음수가 될 수 없습니다." + } +}) From 17716c82442823e55ffcc50030ec32a110fa007d Mon Sep 17 00:00:00 2001 From: dajeong Date: Mon, 12 Dec 2022 22:13:10 +0900 Subject: [PATCH 14/21] =?UTF-8?q?[step4]=20=EA=B2=B0=EA=B3=BC=20=EC=B6=9C?= =?UTF-8?q?=EB=A0=A5=20=EC=98=A4=EB=A5=98=20=EC=88=98=EC=A0=95,=20?= =?UTF-8?q?=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EB=B3=B4=EC=99=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kotlin/lotto/application/Application.kt | 2 +- src/main/kotlin/lotto/common/DoubleNumber.kt | 6 +- src/main/kotlin/lotto/common/IntegerNumber.kt | 4 ++ src/main/kotlin/lotto/domain/Lotto.kt | 4 +- src/main/kotlin/lotto/domain/LottoNumber.kt | 4 ++ src/main/kotlin/lotto/domain/LottoWinner.kt | 2 +- src/main/kotlin/lotto/view/ResultView.kt | 8 ++- .../kotlin/lotto/common/DoubleNumberTest.kt | 72 +++++++++++++++++++ .../kotlin/lotto/common/IntegerNumberTest.kt | 61 ++++++++++++++++ 9 files changed, 156 insertions(+), 7 deletions(-) create mode 100644 src/test/kotlin/lotto/common/DoubleNumberTest.kt create mode 100644 src/test/kotlin/lotto/common/IntegerNumberTest.kt diff --git a/src/main/kotlin/lotto/application/Application.kt b/src/main/kotlin/lotto/application/Application.kt index 7ff75d68ed..391c1480bb 100644 --- a/src/main/kotlin/lotto/application/Application.kt +++ b/src/main/kotlin/lotto/application/Application.kt @@ -16,7 +16,7 @@ class Application { fun run() { val inputPayment = inputView.inputPayment() val lottoList = lottoShop.buyLotto(inputPayment) - resultView.printLotto(lottoList) + resultView.printLottoList(lottoList) val inputLuckyNumbers = inputView.inputLuckyNumbers() val inputBonusNumber = inputView.inputBonusNumber() diff --git a/src/main/kotlin/lotto/common/DoubleNumber.kt b/src/main/kotlin/lotto/common/DoubleNumber.kt index a1a097353a..a4c7532b20 100644 --- a/src/main/kotlin/lotto/common/DoubleNumber.kt +++ b/src/main/kotlin/lotto/common/DoubleNumber.kt @@ -3,7 +3,7 @@ package lotto.common import kotlin.math.pow data class DoubleNumber( - val number: Double + private val number: Double ) { constructor(number: IntegerNumber) : this(number.number.toDouble()) @@ -30,4 +30,8 @@ data class DoubleNumber( fun isGreaterThanEquals(other: DoubleNumber): Boolean { return number >= other.number } + + override fun toString(): String { + return "$number" + } } diff --git a/src/main/kotlin/lotto/common/IntegerNumber.kt b/src/main/kotlin/lotto/common/IntegerNumber.kt index 3206ba1938..558dc8895f 100644 --- a/src/main/kotlin/lotto/common/IntegerNumber.kt +++ b/src/main/kotlin/lotto/common/IntegerNumber.kt @@ -18,4 +18,8 @@ data class IntegerNumber( fun divide(other: IntegerNumber): IntegerNumber { return IntegerNumber(number.div(other.number)) } + + override fun toString(): String { + return "$number" + } } diff --git a/src/main/kotlin/lotto/domain/Lotto.kt b/src/main/kotlin/lotto/domain/Lotto.kt index 4426875f09..c0396afa18 100644 --- a/src/main/kotlin/lotto/domain/Lotto.kt +++ b/src/main/kotlin/lotto/domain/Lotto.kt @@ -1,10 +1,10 @@ package lotto.domain class Lotto( - val numbers: List + val lottoNumbers: List ) { init { - require(numbers.size == LOTTO_NUMBERS_SIZE) { "로또 번호는 ${LOTTO_NUMBERS_SIZE}개가 필요합니다." } + require(lottoNumbers.size == LOTTO_NUMBERS_SIZE) { "로또 번호는 ${LOTTO_NUMBERS_SIZE}개가 필요합니다." } } companion object { diff --git a/src/main/kotlin/lotto/domain/LottoNumber.kt b/src/main/kotlin/lotto/domain/LottoNumber.kt index 1e5d9ac30e..f862192569 100644 --- a/src/main/kotlin/lotto/domain/LottoNumber.kt +++ b/src/main/kotlin/lotto/domain/LottoNumber.kt @@ -14,4 +14,8 @@ data class LottoNumber( const val LOTTO_MIN_NUMBER = 1 const val LOTTO_MAX_NUMBER = 45 } + + override fun toString(): String { + return "$number" + } } diff --git a/src/main/kotlin/lotto/domain/LottoWinner.kt b/src/main/kotlin/lotto/domain/LottoWinner.kt index 3de8422885..6893e5fb4f 100644 --- a/src/main/kotlin/lotto/domain/LottoWinner.kt +++ b/src/main/kotlin/lotto/domain/LottoWinner.kt @@ -5,7 +5,7 @@ class LottoWinner( ) { fun findWinLottoList(lottoList: List): List { return lottoList - .map { luckyNumbers.rank(it.numbers) } + .map { luckyNumbers.rank(it.lottoNumbers) } .filter { hasPrize(it) } } diff --git a/src/main/kotlin/lotto/view/ResultView.kt b/src/main/kotlin/lotto/view/ResultView.kt index 3875d9cfb1..3acece2089 100644 --- a/src/main/kotlin/lotto/view/ResultView.kt +++ b/src/main/kotlin/lotto/view/ResultView.kt @@ -8,14 +8,18 @@ import lotto.domain.LottoStatisticsTotal class ResultView { - fun printLotto(lottoList: List) { + fun printLottoList(lottoList: List) { println("${lottoList.size}개를 구매했습니다.") lottoList.forEach { - println(it.numbers) + printLotto(it) } emptyLine() } + private fun printLotto(lotto: Lotto) { + println(lotto.lottoNumbers) + } + fun printLottoStatistics(statisticsResult: LottoStatisticsTotal) { emptyLine() println(STATISTICS_GUIDE) diff --git a/src/test/kotlin/lotto/common/DoubleNumberTest.kt b/src/test/kotlin/lotto/common/DoubleNumberTest.kt new file mode 100644 index 0000000000..b66ce6d2a3 --- /dev/null +++ b/src/test/kotlin/lotto/common/DoubleNumberTest.kt @@ -0,0 +1,72 @@ +package lotto.common + +import io.kotest.core.spec.style.StringSpec +import io.kotest.matchers.shouldBe + +class DoubleNumberTest : StringSpec({ + "양수값을 가졌을때 isNegative 에서 false를 반환한다." { + // given + val double = DoubleNumber(1.0) + // when + val actual = double.isNegative() + // then + actual shouldBe false + } + + "음수값을 가졌을때 isNegative 에서 true를 반환한다." { + // given + val double = DoubleNumber(-1.0) + // when + val actual = double.isNegative() + // then + actual shouldBe true + } + + "제곱 테스트" { + // given + val double = DoubleNumber(2.0) + // when + val actual = double.pow(DoubleNumber(2.0)) + // then + actual shouldBe DoubleNumber(4.0) + } + + "곱셈 테스트" { + // given + val x = DoubleNumber(2.0) + val y = DoubleNumber(3.0) + // when + val actual = x.multiply(y) + // then + actual shouldBe DoubleNumber(6.0) + } + + "나눗셈 테스트" { + // given + val x = DoubleNumber(6.0) + val y = DoubleNumber(3.0) + // when + val actual = x.divide(y) + // then + actual shouldBe DoubleNumber(2.0) + } + + "소숫점 버림 테스트" { + // given + val double = DoubleNumber(1.2) + // when + val actual = double.floor() + // then + actual shouldBe DoubleNumber(1.0) + } + + "x보다 y가 크거나 같으면 true를 반환한다." { + // given + val x = DoubleNumber(1.2) + val y = DoubleNumber(1.1) + // when + val actual = x.isGreaterThanEquals(y) + // then + actual shouldBe true + } +}) diff --git a/src/test/kotlin/lotto/common/IntegerNumberTest.kt b/src/test/kotlin/lotto/common/IntegerNumberTest.kt new file mode 100644 index 0000000000..dd7953251d --- /dev/null +++ b/src/test/kotlin/lotto/common/IntegerNumberTest.kt @@ -0,0 +1,61 @@ +package lotto.common + +import io.kotest.core.spec.style.StringSpec +import io.kotest.matchers.shouldBe + +class IntegerNumberTest : StringSpec({ + "양수값을 가졌을때 isPositive 에서 true를 반환한다." { + // given + val integer = IntegerNumber(1) + // when + val actual = integer.isPositive() + // then + actual shouldBe true + } + + "음수값을 가졌을때 isPositive 에서 false를 반환한다." { + // given + val integer = IntegerNumber(-1) + // when + val actual = integer.isPositive() + // then + actual shouldBe false + } + + "양수값을 가졌을때 isNegative 에서 false를 반환한다." { + // given + val integer = IntegerNumber(1) + // when + val actual = integer.isNegative() + // then + actual shouldBe false + } + + "음수값을 가졌을때 isNegative 에서 true를 반환한다." { + // given + val integer = IntegerNumber(-1) + // when + val actual = integer.isNegative() + // then + actual shouldBe true + } + + "Double 변환 테스트" { + // given + val integer = IntegerNumber(1) + // when + val actual = integer.toDouble() + // then + actual shouldBe DoubleNumber(1.0) + } + + "나눗셈 테스트" { + // given + val x = IntegerNumber(6) + val y = IntegerNumber(2) + // when + val actual = x.divide(y) + // then + actual shouldBe IntegerNumber(3) + } +}) From 51f1799808b727ae8353fbb10a0560e3f3713999 Mon Sep 17 00:00:00 2001 From: dajeong Date: Mon, 12 Dec 2022 22:50:50 +0900 Subject: [PATCH 15/21] =?UTF-8?q?[step4]=20Lotto=EC=99=80=20LottoWinner=20?= =?UTF-8?q?=ED=81=B4=EB=9E=98=EC=8A=A4=EB=A5=BC=20=ED=95=A9=EC=B9=A8,=20?= =?UTF-8?q?=EB=B6=88=ED=95=84=EC=9A=94=20=EC=BD=94=EB=93=9C=20=EC=82=AD?= =?UTF-8?q?=EC=A0=9C,=20=EC=98=88=EC=99=B8=20=EC=BC=80=EC=9D=B4=EC=8A=A4?= =?UTF-8?q?=20=EC=B2=98=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/lotto/README.md | 8 +- .../kotlin/lotto/application/Application.kt | 5 +- src/main/kotlin/lotto/domain/Lotto.kt | 12 ++ .../kotlin/lotto/domain/LottoResultService.kt | 21 ++- src/main/kotlin/lotto/domain/LottoWinner.kt | 13 -- src/main/kotlin/lotto/domain/LuckyNumbers.kt | 39 ----- src/test/kotlin/lotto/domain/LottoTest.kt | 78 ++++++++++ .../kotlin/lotto/domain/LottoWinnerTest.kt | 69 --------- .../kotlin/lotto/domain/LuckyNumbersTest.kt | 133 ------------------ 9 files changed, 112 insertions(+), 266 deletions(-) delete mode 100644 src/main/kotlin/lotto/domain/LottoWinner.kt delete mode 100644 src/main/kotlin/lotto/domain/LuckyNumbers.kt delete mode 100644 src/test/kotlin/lotto/domain/LottoWinnerTest.kt delete mode 100644 src/test/kotlin/lotto/domain/LuckyNumbersTest.kt diff --git a/src/main/kotlin/lotto/README.md b/src/main/kotlin/lotto/README.md index 2b780fecd4..11866563d7 100644 --- a/src/main/kotlin/lotto/README.md +++ b/src/main/kotlin/lotto/README.md @@ -9,12 +9,8 @@ - [ ] 수동 로또 생성 기능 - [x] 지불한 금액 클래스 생성 - [x] 로또 번호 클래스 생성 -- [ ] 로또 당첨금 클래스 생성 -- [ ] 로또 당첨 번호 개수와 당첨 로또 개수를 위한 개수 클래스 생성 -- [ ] 수익률 클래스 생성 -- [ ] 숫자를 추상화한 클래스 생성 (로또 번호의 상위 타입) -- [ ] 입력 문자열 클래스 생성 -- [ ] 예외 케이스 검토하여 처리 +- [x] 입력 문자열 클래스 생성 +- [x] 예외 케이스 검토하여 처리 ## 구현할 '로또(2등)' 기능 목록 - [x] 보너스 볼 입력 기능 diff --git a/src/main/kotlin/lotto/application/Application.kt b/src/main/kotlin/lotto/application/Application.kt index 391c1480bb..78e5c7a7c3 100644 --- a/src/main/kotlin/lotto/application/Application.kt +++ b/src/main/kotlin/lotto/application/Application.kt @@ -1,9 +1,10 @@ package lotto.application +import lotto.domain.Lotto import lotto.domain.LottoGenerator +import lotto.domain.LottoNumber import lotto.domain.LottoResultService import lotto.domain.LottoShop -import lotto.domain.LuckyNumbers import lotto.util.RandomNumberGenerator import lotto.view.InputView import lotto.view.ResultView @@ -21,7 +22,7 @@ class Application { val inputLuckyNumbers = inputView.inputLuckyNumbers() val inputBonusNumber = inputView.inputBonusNumber() - val lottoResultService = LottoResultService(LuckyNumbers(inputLuckyNumbers, inputBonusNumber)) + val lottoResultService = LottoResultService(Lotto(inputLuckyNumbers.map { LottoNumber(it) }), LottoNumber(inputBonusNumber)) val statistics = lottoResultService.inquireStatistics(inputPayment, lottoList) resultView.printLottoStatistics(statistics) } diff --git a/src/main/kotlin/lotto/domain/Lotto.kt b/src/main/kotlin/lotto/domain/Lotto.kt index c0396afa18..ff01a206fe 100644 --- a/src/main/kotlin/lotto/domain/Lotto.kt +++ b/src/main/kotlin/lotto/domain/Lotto.kt @@ -1,10 +1,22 @@ package lotto.domain +import lotto.common.IntegerNumber + class Lotto( val lottoNumbers: List ) { init { require(lottoNumbers.size == LOTTO_NUMBERS_SIZE) { "로또 번호는 ${LOTTO_NUMBERS_SIZE}개가 필요합니다." } + require(lottoNumbers.toSet().size == LOTTO_NUMBERS_SIZE) { "번호에 중복이 있습니다." } + } + + fun countHitNumbers(luckyLotto: Lotto): IntegerNumber { + val count = lottoNumbers.count { luckyLotto.lottoNumbers.contains(it) } + return IntegerNumber(count) + } + + fun hasBonusNumber(bonusNumber: LottoNumber): Boolean { + return lottoNumbers.contains(bonusNumber) } companion object { diff --git a/src/main/kotlin/lotto/domain/LottoResultService.kt b/src/main/kotlin/lotto/domain/LottoResultService.kt index aba1eb1c08..737e41e058 100644 --- a/src/main/kotlin/lotto/domain/LottoResultService.kt +++ b/src/main/kotlin/lotto/domain/LottoResultService.kt @@ -1,13 +1,26 @@ package lotto.domain class LottoResultService( - luckyNumbers: LuckyNumbers, + private val luckyLotto: Lotto, + private val bonusNumber: LottoNumber ) { - private val lottoWinner = LottoWinner(luckyNumbers) - fun inquireStatistics(payment: Payment, lottoList: List): LottoStatisticsTotal { - val winLottoList = lottoWinner.findWinLottoList(lottoList) + val winLottoList = findWinLottoList(lottoList) val lottoStatisticsService = LottoStatisticsService(payment, winLottoList) return lottoStatisticsService.statistics() } + + private fun findWinLottoList(lottoList: List): List { + return lottoList + .map { rank(it, bonusNumber) } + .filter { hasPrize(it) } + } + + private fun rank(lotto: Lotto, bonusNumber: LottoNumber): LottoRank { + val hitCount = lotto.countHitNumbers(luckyLotto) + val hasBonusNumber = lotto.hasBonusNumber(bonusNumber) + return LottoRank.from(hitCount, hasBonusNumber) + } + + private fun hasPrize(lottoRank: LottoRank) = lottoRank.prizeMoney.isPositive() } diff --git a/src/main/kotlin/lotto/domain/LottoWinner.kt b/src/main/kotlin/lotto/domain/LottoWinner.kt deleted file mode 100644 index 6893e5fb4f..0000000000 --- a/src/main/kotlin/lotto/domain/LottoWinner.kt +++ /dev/null @@ -1,13 +0,0 @@ -package lotto.domain - -class LottoWinner( - private val luckyNumbers: LuckyNumbers -) { - fun findWinLottoList(lottoList: List): List { - return lottoList - .map { luckyNumbers.rank(it.lottoNumbers) } - .filter { hasPrize(it) } - } - - private fun hasPrize(lottoRank: LottoRank) = lottoRank.prizeMoney.isPositive() -} diff --git a/src/main/kotlin/lotto/domain/LuckyNumbers.kt b/src/main/kotlin/lotto/domain/LuckyNumbers.kt deleted file mode 100644 index a320fb39ee..0000000000 --- a/src/main/kotlin/lotto/domain/LuckyNumbers.kt +++ /dev/null @@ -1,39 +0,0 @@ -package lotto.domain - -import lotto.common.IntegerNumber - -class LuckyNumbers( - luckyNumbers: List, - bonusNumber: IntegerNumber -) { - private val _luckyNumbers: List - private val _bonusNumber: LottoNumber - - init { - require(luckyNumbers.size == LUCKY_NUMBER_SIZE) { "당첨 번호는 ${LUCKY_NUMBER_SIZE}개가 필요합니다." } - require(luckyNumbers.toSet().size == LUCKY_NUMBER_SIZE) { "당첨 번호에 중복이 있습니다." } - require(!luckyNumbers.contains(bonusNumber)) { "보너스볼은 당첨번호와 중복될 수 없습니다." } - - _luckyNumbers = luckyNumbers.map { LottoNumber(it) } - _bonusNumber = LottoNumber(bonusNumber) - } - - fun rank(numbers: List): LottoRank { - val hitCount = countHitNumbers(numbers) - val hasBonusNumber = containsBonusNumber(numbers) - return LottoRank.from(hitCount, hasBonusNumber) - } - - private fun countHitNumbers(numbers: List): IntegerNumber { - val count = numbers.count { number -> _luckyNumbers.contains(number) } - return IntegerNumber(count) - } - - private fun containsBonusNumber(numbers: List): Boolean { - return numbers.contains(_bonusNumber) - } - - companion object { - const val LUCKY_NUMBER_SIZE = 6 - } -} diff --git a/src/test/kotlin/lotto/domain/LottoTest.kt b/src/test/kotlin/lotto/domain/LottoTest.kt index aab6c9f646..efcc5c35f2 100644 --- a/src/test/kotlin/lotto/domain/LottoTest.kt +++ b/src/test/kotlin/lotto/domain/LottoTest.kt @@ -22,4 +22,82 @@ class LottoTest : StringSpec({ } exception.message shouldBe "로또 번호는 6개가 필요합니다." } + + "로또 번호 중복 에러 테스트" { + val numbers = listOf( + LottoNumber(IntegerNumber(1)), + LottoNumber(IntegerNumber(2)), + LottoNumber(IntegerNumber(3)), + LottoNumber(IntegerNumber(4)), + LottoNumber(IntegerNumber(5)), + LottoNumber(IntegerNumber(1)) + ) + val exception = shouldThrowExactly { + Lotto(numbers) + } + exception.message shouldBe "번호에 중복이 있습니다." + } + + "당첨 번호 개수 카운트 테스트" { + // given + val numbers = listOf( + LottoNumber(IntegerNumber(1)), + LottoNumber(IntegerNumber(2)), + LottoNumber(IntegerNumber(3)), + LottoNumber(IntegerNumber(4)), + LottoNumber(IntegerNumber(5)), + LottoNumber(IntegerNumber(6)) + ) + val lotto = Lotto(numbers) + + val luckyNumbers = listOf( + LottoNumber(IntegerNumber(2)), + LottoNumber(IntegerNumber(4)), + LottoNumber(IntegerNumber(6)), + LottoNumber(IntegerNumber(8)), + LottoNumber(IntegerNumber(10)), + LottoNumber(IntegerNumber(12)) + ) + val luckyLotto = Lotto(luckyNumbers) + // when + val actual = lotto.countHitNumbers(luckyLotto) + // then + actual shouldBe IntegerNumber(3) + } + + "보너스 번호 포함되면 containsBonusNumber에서 true를 반환" { + // given + val numbers = listOf( + LottoNumber(IntegerNumber(1)), + LottoNumber(IntegerNumber(2)), + LottoNumber(IntegerNumber(3)), + LottoNumber(IntegerNumber(4)), + LottoNumber(IntegerNumber(5)), + LottoNumber(IntegerNumber(6)) + ) + val lotto = Lotto(numbers) + val bonusNumber = LottoNumber(IntegerNumber(1)) + // when + val actual = lotto.hasBonusNumber(bonusNumber) + // then + actual shouldBe true + } + + "보너스 번호 포함 안되면 containsBonusNumber에서 false를 반환" { + // given + val numbers = listOf( + LottoNumber(IntegerNumber(1)), + LottoNumber(IntegerNumber(2)), + LottoNumber(IntegerNumber(3)), + LottoNumber(IntegerNumber(4)), + LottoNumber(IntegerNumber(5)), + LottoNumber(IntegerNumber(6)) + ) + val lotto = Lotto(numbers) + val bonusNumber = LottoNumber(IntegerNumber(7)) + // when + val actual = lotto.hasBonusNumber(bonusNumber) + // then + actual shouldBe false + } }) diff --git a/src/test/kotlin/lotto/domain/LottoWinnerTest.kt b/src/test/kotlin/lotto/domain/LottoWinnerTest.kt deleted file mode 100644 index cc4f4379f8..0000000000 --- a/src/test/kotlin/lotto/domain/LottoWinnerTest.kt +++ /dev/null @@ -1,69 +0,0 @@ -package lotto.domain - -import io.kotest.core.spec.style.StringSpec -import io.kotest.matchers.collections.shouldContainInOrder -import io.kotest.matchers.shouldBe -import lotto.common.IntegerNumber - -class LottoWinnerTest : StringSpec({ - - "당첨 로또 선택 테스트" { - // given - val luckyNumbers = listOf( - IntegerNumber(1), - IntegerNumber(3), - IntegerNumber(5), - IntegerNumber(7), - IntegerNumber(9), - IntegerNumber(11) - ) - val bonusNumber = IntegerNumber(13) - val lottoWinner = LottoWinner(LuckyNumbers(luckyNumbers, bonusNumber)) - - val fourthWinLotto = Lotto( - listOf( - LottoNumber(IntegerNumber(5)), - LottoNumber(IntegerNumber(7)), - LottoNumber(IntegerNumber(9)), - LottoNumber(IntegerNumber(11)), - LottoNumber(IntegerNumber(12)), - LottoNumber(IntegerNumber(13)) - ) - ) - val secondWinLotto = Lotto( - listOf( - LottoNumber(IntegerNumber(3)), - LottoNumber(IntegerNumber(5)), - LottoNumber(IntegerNumber(7)), - LottoNumber(IntegerNumber(9)), - LottoNumber(IntegerNumber(11)), - LottoNumber(IntegerNumber(13)) - ) - ) - val thirdWinLotto = Lotto( - listOf( - LottoNumber(IntegerNumber(3)), - LottoNumber(IntegerNumber(5)), - LottoNumber(IntegerNumber(7)), - LottoNumber(IntegerNumber(9)), - LottoNumber(IntegerNumber(11)), - LottoNumber(IntegerNumber(15)) - ) - ) - val notWintLotto = Lotto( - listOf( - LottoNumber(IntegerNumber(2)), - LottoNumber(IntegerNumber(4)), - LottoNumber(IntegerNumber(6)), - LottoNumber(IntegerNumber(8)), - LottoNumber(IntegerNumber(10)), - LottoNumber(IntegerNumber(13)) - ) - ) - // when - val result = lottoWinner.findWinLottoList(listOf(fourthWinLotto, notWintLotto, secondWinLotto, thirdWinLotto)) - // then - result.size shouldBe 3 - result shouldContainInOrder listOf(LottoRank.FOURTH, LottoRank.SECOND, LottoRank.THIRD) - } -}) diff --git a/src/test/kotlin/lotto/domain/LuckyNumbersTest.kt b/src/test/kotlin/lotto/domain/LuckyNumbersTest.kt deleted file mode 100644 index 3d09cd0d66..0000000000 --- a/src/test/kotlin/lotto/domain/LuckyNumbersTest.kt +++ /dev/null @@ -1,133 +0,0 @@ -package lotto.domain - -import io.kotest.assertions.throwables.shouldThrowExactly -import io.kotest.core.spec.style.StringSpec -import io.kotest.data.forAll -import io.kotest.data.row -import io.kotest.matchers.shouldBe -import lotto.common.IntegerNumber - -class LuckyNumbersTest : StringSpec({ - "당첨 번호가 6개를 넘으면 에러 발생 테스트" { - val numbers = listOf( - IntegerNumber(1), - IntegerNumber(2), - IntegerNumber(3), - IntegerNumber(4), - IntegerNumber(5), - IntegerNumber(6), - IntegerNumber(7) - ) - val exception = shouldThrowExactly { - LuckyNumbers(luckyNumbers = numbers, bonusNumber = IntegerNumber(10)) - } - exception.message shouldBe "당첨 번호는 6개가 필요합니다." - } - - "당첨 번호가 중복이면 에러 발생 테스트" { - val numbers = listOf( - IntegerNumber(1), - IntegerNumber(2), - IntegerNumber(3), - IntegerNumber(4), - IntegerNumber(6), - IntegerNumber(6) - ) - val exception = shouldThrowExactly { - LuckyNumbers(luckyNumbers = numbers, bonusNumber = IntegerNumber(10)) - } - exception.message shouldBe "당첨 번호에 중복이 있습니다." - } - - "보너스볼 중복 에러 테스트" { - val numbers = listOf( - IntegerNumber(1), - IntegerNumber(2), - IntegerNumber(3), - IntegerNumber(4), - IntegerNumber(5), - IntegerNumber(6) - ) - val exception = shouldThrowExactly { - LuckyNumbers(luckyNumbers = numbers, bonusNumber = IntegerNumber(6)) - } - exception.message shouldBe "보너스볼은 당첨번호와 중복될 수 없습니다." - } - - "로또 Hit Count 계산 테스트" { - forAll( - // given - row( - "0개 일치하는 로또", - LuckyNumbers( - luckyNumbers = listOf( - IntegerNumber(1), - IntegerNumber(3), - IntegerNumber(5), - IntegerNumber(7), - IntegerNumber(9), - IntegerNumber(11) - ), bonusNumber = IntegerNumber(13) - ), - listOf( - LottoNumber(IntegerNumber(2)), - LottoNumber(IntegerNumber(4)), - LottoNumber(IntegerNumber(6)), - LottoNumber(IntegerNumber(8)), - LottoNumber(IntegerNumber(10)), - LottoNumber(IntegerNumber(12)) - ), - LottoRank.MISS - ), - row( - "3개 일치하는 로또", - LuckyNumbers( - luckyNumbers = listOf( - IntegerNumber(1), - IntegerNumber(3), - IntegerNumber(5), - IntegerNumber(7), - IntegerNumber(9), - IntegerNumber(11) - ), bonusNumber = IntegerNumber(13) - ), - listOf( - LottoNumber(IntegerNumber(7)), - LottoNumber(IntegerNumber(8)), - LottoNumber(IntegerNumber(9)), - LottoNumber(IntegerNumber(10)), - LottoNumber(IntegerNumber(11)), - LottoNumber(IntegerNumber(12)) - ), - LottoRank.FIFTH - ), - row( - "5개 일치+보너스번호 일치하는 로또", - LuckyNumbers( - luckyNumbers = listOf( - IntegerNumber(1), - IntegerNumber(3), - IntegerNumber(5), - IntegerNumber(7), - IntegerNumber(9), - IntegerNumber(11) - ), bonusNumber = IntegerNumber(13) - ), - listOf( - LottoNumber(IntegerNumber(1)), - LottoNumber(IntegerNumber(3)), - LottoNumber(IntegerNumber(5)), - LottoNumber(IntegerNumber(7)), - LottoNumber(IntegerNumber(9)), - LottoNumber(IntegerNumber(13)) - ), - LottoRank.SECOND - ) - ) { title, luckyNumbers, lottoNumbers, expectedRank -> - // when - val actual = luckyNumbers.rank(lottoNumbers) - // then - actual shouldBe expectedRank - } - } -}) From 0f569101c7924ca16c5f8f5c733b5602e8a6a9b6 Mon Sep 17 00:00:00 2001 From: dajeong Date: Mon, 12 Dec 2022 23:54:50 +0900 Subject: [PATCH 16/21] =?UTF-8?q?[step4]=20=EC=88=98=EB=8F=99=20=EB=A1=9C?= =?UTF-8?q?=EB=98=90=20=EA=B0=9C=EC=88=98,=20=EB=B2=88=ED=98=B8=20?= =?UTF-8?q?=EC=9E=85=EB=A0=A5=20=EA=B8=B0=EB=8A=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kotlin/lotto/common/IntegerNumberList.kt | 6 ++++ src/main/kotlin/lotto/view/InputView.kt | 28 +++++++++++++++++-- .../kotlin/lotto/common/IntegerNumberTest.kt | 20 +++++++++++++ 3 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 src/main/kotlin/lotto/common/IntegerNumberList.kt diff --git a/src/main/kotlin/lotto/common/IntegerNumberList.kt b/src/main/kotlin/lotto/common/IntegerNumberList.kt new file mode 100644 index 0000000000..3a9223c872 --- /dev/null +++ b/src/main/kotlin/lotto/common/IntegerNumberList.kt @@ -0,0 +1,6 @@ +package lotto.common + +class IntegerNumberList( + val integerNumberList: List +) { +} \ No newline at end of file diff --git a/src/main/kotlin/lotto/view/InputView.kt b/src/main/kotlin/lotto/view/InputView.kt index 79c8087559..21d5e2c0ed 100644 --- a/src/main/kotlin/lotto/view/InputView.kt +++ b/src/main/kotlin/lotto/view/InputView.kt @@ -1,6 +1,7 @@ package lotto.view import lotto.common.IntegerNumber +import lotto.common.IntegerNumberList import lotto.common.NumberString import lotto.common.NumberStringList import lotto.domain.Payment @@ -13,18 +14,41 @@ class InputView { return Payment(payment.toIntegerNumber()) } - fun inputLuckyNumbers(): List { + fun inputManualLottoCount(): IntegerNumber { + println(INPUT_MANUAL_LOTTO_COUNT_GUIDE) + return inputNumber() + } + + fun inputManualLottoNumbers(count: IntegerNumber): List { + println(INPUT_MANUAL_LOTTO_NUMBERS_GUIDE) + return List(count.number) { + inputNumberList() + } + } + + fun inputLuckyNumbers(): IntegerNumberList { println(INPUT_LUCKY_NUMBERS_GUIDE) - return NumberStringList(readln()).toIntegerNumberList() + return inputNumberList() + } + + private fun inputNumberList(): IntegerNumberList { + val numberList = NumberStringList(readln()).toIntegerNumberList() + return IntegerNumberList(numberList) } fun inputBonusNumber(): IntegerNumber { println(INPUT_BONUS_BALL) + return inputNumber() + } + + private fun inputNumber(): IntegerNumber { return NumberString(readln()).toIntegerNumber() } private companion object { const val INPUT_PAYMENT_GUIDE = "# 구입금액을 입력해주세요." + const val INPUT_MANUAL_LOTTO_COUNT_GUIDE = "# 수동으로 구매할 로또 수를 입력해 주세요." + const val INPUT_MANUAL_LOTTO_NUMBERS_GUIDE = "# 수동으로 구매할 번호를 입력해 주세요." const val INPUT_LUCKY_NUMBERS_GUIDE = "# 지난 주 당첨 번호를 입력해 주세요." const val INPUT_BONUS_BALL = "# 보너스 볼을 입력해 주세요." } diff --git a/src/test/kotlin/lotto/common/IntegerNumberTest.kt b/src/test/kotlin/lotto/common/IntegerNumberTest.kt index dd7953251d..3e69bd1e14 100644 --- a/src/test/kotlin/lotto/common/IntegerNumberTest.kt +++ b/src/test/kotlin/lotto/common/IntegerNumberTest.kt @@ -58,4 +58,24 @@ class IntegerNumberTest : StringSpec({ // then actual shouldBe IntegerNumber(3) } + + "뺄셈 테스트" { + // given + val x = IntegerNumber(6) + val y = IntegerNumber(2) + // when + val actual = x.minus(y) + // then + actual shouldBe IntegerNumber(4) + } + + "곱셈 테스트" { + // given + val x = IntegerNumber(6) + val y = IntegerNumber(2) + // when + val actual = x.multiply(y) + // then + actual shouldBe IntegerNumber(12) + } }) From 431d97a31b0f391c39e98ca28b0d8c21f69026ff Mon Sep 17 00:00:00 2001 From: dajeong Date: Tue, 13 Dec 2022 00:04:24 +0900 Subject: [PATCH 17/21] =?UTF-8?q?[step4]=20=EC=88=98=EB=8F=99=20=EB=A1=9C?= =?UTF-8?q?=EB=98=90=20=EC=83=9D=EC=84=B1=20=EA=B8=B0=EB=8A=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/lotto/README.md | 6 ++-- .../kotlin/lotto/application/Application.kt | 9 ++++-- src/main/kotlin/lotto/common/IntegerNumber.kt | 8 +++++ .../kotlin/lotto/common/IntegerNumberList.kt | 3 +- .../kotlin/lotto/domain/LottoGenerator.kt | 8 +++++ src/main/kotlin/lotto/domain/LottoShop.kt | 17 +++++++++-- src/main/kotlin/lotto/domain/Payment.kt | 7 ++++- src/test/kotlin/lotto/domain/LottoShopTest.kt | 30 +++++++++++++++++-- src/test/kotlin/lotto/domain/PaymentTest.kt | 20 +++++++++++++ 9 files changed, 95 insertions(+), 13 deletions(-) diff --git a/src/main/kotlin/lotto/README.md b/src/main/kotlin/lotto/README.md index 11866563d7..847b961530 100644 --- a/src/main/kotlin/lotto/README.md +++ b/src/main/kotlin/lotto/README.md @@ -4,9 +4,9 @@ - [x] 정수값 포장 클래스 생성 - [x] 실수값 포장 클래스 생성 - [x] 입력 문자열 포장 클래스 생성 -- [ ] 수동으로 구매할 로또 수 입력 기능 -- [ ] 수동으로 구매할 로또 번호 입력 기능 -- [ ] 수동 로또 생성 기능 +- [x] 수동으로 구매할 로또 수 입력 기능 +- [x] 수동으로 구매할 로또 번호 입력 기능 +- [x] 수동 로또 생성 기능 - [x] 지불한 금액 클래스 생성 - [x] 로또 번호 클래스 생성 - [x] 입력 문자열 클래스 생성 diff --git a/src/main/kotlin/lotto/application/Application.kt b/src/main/kotlin/lotto/application/Application.kt index 78e5c7a7c3..bb377233ab 100644 --- a/src/main/kotlin/lotto/application/Application.kt +++ b/src/main/kotlin/lotto/application/Application.kt @@ -16,13 +16,18 @@ class Application { fun run() { val inputPayment = inputView.inputPayment() - val lottoList = lottoShop.buyLotto(inputPayment) + val manualLottoCount = inputView.inputManualLottoCount() + val manualNumberList = inputView.inputManualLottoNumbers(manualLottoCount) + + val manualLottoList = lottoShop.buyManualLotto(inputPayment, manualNumberList) + val autoLottoList = lottoShop.buyAutoLotto(inputPayment) + val lottoList = manualLottoList + autoLottoList resultView.printLottoList(lottoList) val inputLuckyNumbers = inputView.inputLuckyNumbers() val inputBonusNumber = inputView.inputBonusNumber() - val lottoResultService = LottoResultService(Lotto(inputLuckyNumbers.map { LottoNumber(it) }), LottoNumber(inputBonusNumber)) + val lottoResultService = LottoResultService(Lotto(inputLuckyNumbers.integerNumberList.map { LottoNumber(it) }), LottoNumber(inputBonusNumber)) val statistics = lottoResultService.inquireStatistics(inputPayment, lottoList) resultView.printLottoStatistics(statistics) } diff --git a/src/main/kotlin/lotto/common/IntegerNumber.kt b/src/main/kotlin/lotto/common/IntegerNumber.kt index 558dc8895f..43446e0f87 100644 --- a/src/main/kotlin/lotto/common/IntegerNumber.kt +++ b/src/main/kotlin/lotto/common/IntegerNumber.kt @@ -22,4 +22,12 @@ data class IntegerNumber( override fun toString(): String { return "$number" } + + fun minus(other: IntegerNumber): IntegerNumber { + return IntegerNumber(number - other.number) + } + + fun multiply(other: IntegerNumber): IntegerNumber { + return IntegerNumber(number * other.number) + } } diff --git a/src/main/kotlin/lotto/common/IntegerNumberList.kt b/src/main/kotlin/lotto/common/IntegerNumberList.kt index 3a9223c872..a89bad19d4 100644 --- a/src/main/kotlin/lotto/common/IntegerNumberList.kt +++ b/src/main/kotlin/lotto/common/IntegerNumberList.kt @@ -2,5 +2,4 @@ package lotto.common class IntegerNumberList( val integerNumberList: List -) { -} \ No newline at end of file +) diff --git a/src/main/kotlin/lotto/domain/LottoGenerator.kt b/src/main/kotlin/lotto/domain/LottoGenerator.kt index cebbd4107a..f2e0f0372c 100644 --- a/src/main/kotlin/lotto/domain/LottoGenerator.kt +++ b/src/main/kotlin/lotto/domain/LottoGenerator.kt @@ -1,6 +1,7 @@ package lotto.domain import lotto.common.IntegerNumber +import lotto.common.IntegerNumberList import lotto.util.NumberGenerator class LottoGenerator( @@ -14,4 +15,11 @@ class LottoGenerator( return numberGenerator.generate(Lotto.LOTTO_START_NUMBER, Lotto.LOTTO_END_NUMBER, Lotto.LOTTO_NUMBERS_SIZE) .map { LottoNumber(it) } } + + fun generate(numberList: List): List { + return numberList.map { + val lottoNumberList = it.integerNumberList.map { number -> LottoNumber(number) } + Lotto(lottoNumberList) + } + } } diff --git a/src/main/kotlin/lotto/domain/LottoShop.kt b/src/main/kotlin/lotto/domain/LottoShop.kt index 786a3f3722..0e2a72a497 100644 --- a/src/main/kotlin/lotto/domain/LottoShop.kt +++ b/src/main/kotlin/lotto/domain/LottoShop.kt @@ -1,13 +1,24 @@ package lotto.domain import lotto.common.IntegerNumber +import lotto.common.IntegerNumberList class LottoShop( private val lottoGenerator: LottoGenerator ) { - fun buyLotto(inputPayment: Payment): List { - val lottoCount = calculateLottoCount(inputPayment) - return lottoGenerator.generate(lottoCount) + fun buyAutoLotto(payment: Payment): List { + val totalLottoCount = calculateLottoCount(payment) + return lottoGenerator.generate(totalLottoCount) + } + + fun buyManualLotto(payment: Payment, manualNumberList: List): List { + val totalLottoCount = calculateLottoCount(payment) + val manualLottoCount = IntegerNumber(manualNumberList.size) + + require(manualLottoCount.number <= totalLottoCount.number) { "수동 로또 개수가 지불 금액보다 많아 로또를 구입할 수 없습니다." } + + payment.charge(manualLottoCount.multiply(LOTTO_PRICE)) + return lottoGenerator.generate(manualNumberList) } private fun calculateLottoCount(payment: Payment): IntegerNumber { diff --git a/src/main/kotlin/lotto/domain/Payment.kt b/src/main/kotlin/lotto/domain/Payment.kt index 4903c2e502..e39c935a2d 100644 --- a/src/main/kotlin/lotto/domain/Payment.kt +++ b/src/main/kotlin/lotto/domain/Payment.kt @@ -3,9 +3,14 @@ package lotto.domain import lotto.common.IntegerNumber class Payment( - val payment: IntegerNumber + var payment: IntegerNumber ) { init { require(payment.isNegative().not()) { "지불액은 음수가 될 수 없습니다." } } + + fun charge(charge: IntegerNumber) { + require(payment.number >= charge.number) { "잔액 부족으로 금액을 차감할 수 없습니다." } + payment = payment.minus(charge) + } } diff --git a/src/test/kotlin/lotto/domain/LottoShopTest.kt b/src/test/kotlin/lotto/domain/LottoShopTest.kt index 8496944a09..4a522d3ce3 100644 --- a/src/test/kotlin/lotto/domain/LottoShopTest.kt +++ b/src/test/kotlin/lotto/domain/LottoShopTest.kt @@ -3,15 +3,17 @@ package lotto.domain import io.kotest.core.spec.style.StringSpec import io.kotest.data.forAll import io.kotest.data.row +import io.kotest.matchers.equality.shouldBeEqualToComparingFields import io.kotest.matchers.shouldBe import lotto.common.IntegerNumber +import lotto.common.IntegerNumberList import lotto.util.RandomNumberGenerator class LottoShopTest : StringSpec({ val lottoShop = LottoShop(LottoGenerator(RandomNumberGenerator())) - "로또 구매 테스트" { + "로또 구매 개수 검증 테스트" { forAll( // given row("0원을 내면 0개를 반환한다.", Payment(IntegerNumber(0)), 0), @@ -19,9 +21,33 @@ class LottoShopTest : StringSpec({ row("5500원을 내면 5개를 반환한다.", Payment(IntegerNumber(5500)), 5) ) { title, payment, expectedSize -> // when - val actual = lottoShop.buyLotto(payment) + val actual = lottoShop.buyAutoLotto(payment) // then actual.size shouldBe expectedSize } } + + "수동 로또 구매 테스트" { + val manualNumberList = IntegerNumberList( + listOf( + IntegerNumber(1), + IntegerNumber(2), + IntegerNumber(3), + IntegerNumber(4), + IntegerNumber(5), + IntegerNumber(6) + ) + ) + val result = lottoShop.buyManualLotto(Payment(IntegerNumber(1000)), listOf(manualNumberList)) + result[0] shouldBeEqualToComparingFields Lotto( + listOf( + LottoNumber(IntegerNumber(1)), + LottoNumber(IntegerNumber(2)), + LottoNumber(IntegerNumber(3)), + LottoNumber(IntegerNumber(4)), + LottoNumber(IntegerNumber(5)), + LottoNumber(IntegerNumber(6)) + ) + ) + } }) diff --git a/src/test/kotlin/lotto/domain/PaymentTest.kt b/src/test/kotlin/lotto/domain/PaymentTest.kt index 90baf9b22a..724dc81882 100644 --- a/src/test/kotlin/lotto/domain/PaymentTest.kt +++ b/src/test/kotlin/lotto/domain/PaymentTest.kt @@ -12,4 +12,24 @@ class PaymentTest : StringSpec({ } exception.message shouldBe "지불액은 음수가 될 수 없습니다." } + + "지불금 차감 테스트" { + // given + val payment = Payment(IntegerNumber(1000)) + // when + payment.charge(IntegerNumber(100)) + // then + payment.payment shouldBe IntegerNumber(900) + } + + "잔액 부족으로 지불금 차감 에러 테스트" { + // given + val payment = Payment(IntegerNumber(1000)) + // when + val exception = shouldThrowExactly { + payment.charge(IntegerNumber(1100)) + } + // then + exception.message shouldBe "잔액 부족으로 금액을 차감할 수 없습니다." + } }) From 8a3e35b1c2a73ca3360c1c4f639683d759639eba Mon Sep 17 00:00:00 2001 From: dajeong Date: Tue, 13 Dec 2022 00:10:00 +0900 Subject: [PATCH 18/21] =?UTF-8?q?[step4]=20=EC=83=9D=EC=84=B1=EB=90=9C=20?= =?UTF-8?q?=EB=A1=9C=EB=98=90=20=EC=B6=9C=EB=A0=A5=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/README.md | 1 + src/main/kotlin/lotto/application/Application.kt | 4 ++-- src/main/kotlin/lotto/view/ResultView.kt | 9 ++++++--- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/main/kotlin/lotto/README.md b/src/main/kotlin/lotto/README.md index 847b961530..33cfac6d72 100644 --- a/src/main/kotlin/lotto/README.md +++ b/src/main/kotlin/lotto/README.md @@ -7,6 +7,7 @@ - [x] 수동으로 구매할 로또 수 입력 기능 - [x] 수동으로 구매할 로또 번호 입력 기능 - [x] 수동 로또 생성 기능 +- [x] 수동 로또, 자동 로또 출력 기능 - [x] 지불한 금액 클래스 생성 - [x] 로또 번호 클래스 생성 - [x] 입력 문자열 클래스 생성 diff --git a/src/main/kotlin/lotto/application/Application.kt b/src/main/kotlin/lotto/application/Application.kt index bb377233ab..702b2a7ac4 100644 --- a/src/main/kotlin/lotto/application/Application.kt +++ b/src/main/kotlin/lotto/application/Application.kt @@ -21,9 +21,9 @@ class Application { val manualLottoList = lottoShop.buyManualLotto(inputPayment, manualNumberList) val autoLottoList = lottoShop.buyAutoLotto(inputPayment) - val lottoList = manualLottoList + autoLottoList - resultView.printLottoList(lottoList) + resultView.printLottoList(manualLottoList, autoLottoList) + val lottoList = manualLottoList + autoLottoList val inputLuckyNumbers = inputView.inputLuckyNumbers() val inputBonusNumber = inputView.inputBonusNumber() diff --git a/src/main/kotlin/lotto/view/ResultView.kt b/src/main/kotlin/lotto/view/ResultView.kt index 3acece2089..07cc0f5e42 100644 --- a/src/main/kotlin/lotto/view/ResultView.kt +++ b/src/main/kotlin/lotto/view/ResultView.kt @@ -8,9 +8,12 @@ import lotto.domain.LottoStatisticsTotal class ResultView { - fun printLottoList(lottoList: List) { - println("${lottoList.size}개를 구매했습니다.") - lottoList.forEach { + fun printLottoList(manualLottoList: List, autoLottoList: List) { + println("수동으로 ${manualLottoList.size}개, 자동으로 ${autoLottoList.size}개를 구매했습니다.") + manualLottoList.forEach { + printLotto(it) + } + autoLottoList.forEach { printLotto(it) } emptyLine() From 533f5fb4eb8cef1287badb854668169ab45b4ec9 Mon Sep 17 00:00:00 2001 From: dajeong Date: Tue, 27 Dec 2022 11:38:55 +0900 Subject: [PATCH 19/21] =?UTF-8?q?[step4]=20=ED=94=BC=EB=93=9C=EB=B0=B1=20?= =?UTF-8?q?=EB=B0=98=EC=98=81=20-=20=EB=AA=A8=EB=93=A0=20=EC=9B=90?= =?UTF-8?q?=EC=8B=9C=EA=B0=92=20=ED=8F=AC=EC=9E=A5=EC=9D=B4=20=EC=95=84?= =?UTF-8?q?=EB=8B=8C=20=EC=9D=98=EB=AF=B8=EC=9E=88=EB=8A=94=20=EA=B0=92?= =?UTF-8?q?=EB=A7=8C=20=ED=8F=AC=EC=9E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 모든 원시값 포장을 잘못이해하여 생서한 클래스들 제거 --- src/main/kotlin/lotto/common/DoubleNumber.kt | 37 --------- src/main/kotlin/lotto/common/IntegerNumber.kt | 33 -------- .../kotlin/lotto/common/IntegerNumberList.kt | 2 +- src/main/kotlin/lotto/common/NumberString.kt | 4 +- .../kotlin/lotto/common/NumberStringList.kt | 2 +- src/main/kotlin/lotto/domain/Lotto.kt | 6 +- .../kotlin/lotto/domain/LottoGenerator.kt | 5 +- src/main/kotlin/lotto/domain/LottoNumber.kt | 6 +- src/main/kotlin/lotto/domain/LottoRank.kt | 22 +++-- .../kotlin/lotto/domain/LottoResultService.kt | 2 +- src/main/kotlin/lotto/domain/LottoShop.kt | 13 ++- .../kotlin/lotto/domain/LottoStatistics.kt | 12 ++- .../lotto/domain/LottoStatisticsResult.kt | 4 +- .../lotto/domain/LottoStatisticsTotal.kt | 4 +- src/main/kotlin/lotto/domain/Payment.kt | 10 +-- src/main/kotlin/lotto/util/NumberGenerator.kt | 4 +- src/main/kotlin/lotto/util/NumberUtil.kt | 12 +-- .../lotto/util/RandomNumberGenerator.kt | 6 +- src/main/kotlin/lotto/view/InputView.kt | 11 ++- src/main/kotlin/lotto/view/ResultView.kt | 5 +- .../kotlin/lotto/common/DoubleNumberTest.kt | 72 ----------------- .../kotlin/lotto/common/IntegerNumberTest.kt | 81 ------------------- .../lotto/common/NumberStringListTest.kt | 2 +- .../kotlin/lotto/common/NumberStringTest.kt | 27 ------- .../kotlin/lotto/domain/LottoGeneratorTest.kt | 5 +- src/test/kotlin/lotto/domain/LottoShopTest.kt | 33 ++++---- .../domain/LottoStatisticsServiceTest.kt | 23 +++--- .../lotto/domain/LottoStatisticsTest.kt | 18 ++--- src/test/kotlin/lotto/domain/LottoTest.kt | 81 +++++++++---------- src/test/kotlin/lotto/domain/PaymentTest.kt | 13 ++- src/test/kotlin/lotto/util/NumberUtilTest.kt | 7 +- .../lotto/util/RandomNumberGeneratorTest.kt | 2 +- 32 files changed, 142 insertions(+), 422 deletions(-) delete mode 100644 src/main/kotlin/lotto/common/DoubleNumber.kt delete mode 100644 src/main/kotlin/lotto/common/IntegerNumber.kt delete mode 100644 src/test/kotlin/lotto/common/DoubleNumberTest.kt delete mode 100644 src/test/kotlin/lotto/common/IntegerNumberTest.kt diff --git a/src/main/kotlin/lotto/common/DoubleNumber.kt b/src/main/kotlin/lotto/common/DoubleNumber.kt deleted file mode 100644 index a4c7532b20..0000000000 --- a/src/main/kotlin/lotto/common/DoubleNumber.kt +++ /dev/null @@ -1,37 +0,0 @@ -package lotto.common - -import kotlin.math.pow - -data class DoubleNumber( - private val number: Double -) { - constructor(number: IntegerNumber) : this(number.number.toDouble()) - - fun divide(other: DoubleNumber): DoubleNumber { - return DoubleNumber(number.div(other.number)) - } - - fun pow(x: DoubleNumber): DoubleNumber { - return DoubleNumber(number.pow(x.number)) - } - - fun multiply(other: DoubleNumber): DoubleNumber { - return DoubleNumber(number.times(other.number)) - } - - fun floor(): DoubleNumber { - return DoubleNumber(kotlin.math.floor(number)) - } - - fun isNegative(): Boolean { - return number < 0 - } - - fun isGreaterThanEquals(other: DoubleNumber): Boolean { - return number >= other.number - } - - override fun toString(): String { - return "$number" - } -} diff --git a/src/main/kotlin/lotto/common/IntegerNumber.kt b/src/main/kotlin/lotto/common/IntegerNumber.kt deleted file mode 100644 index 43446e0f87..0000000000 --- a/src/main/kotlin/lotto/common/IntegerNumber.kt +++ /dev/null @@ -1,33 +0,0 @@ -package lotto.common - -data class IntegerNumber( - val number: Int -) { - fun isPositive(): Boolean { - return number > 0 - } - - fun isNegative(): Boolean { - return number < 0 - } - - fun toDouble(): DoubleNumber { - return DoubleNumber(this) - } - - fun divide(other: IntegerNumber): IntegerNumber { - return IntegerNumber(number.div(other.number)) - } - - override fun toString(): String { - return "$number" - } - - fun minus(other: IntegerNumber): IntegerNumber { - return IntegerNumber(number - other.number) - } - - fun multiply(other: IntegerNumber): IntegerNumber { - return IntegerNumber(number * other.number) - } -} diff --git a/src/main/kotlin/lotto/common/IntegerNumberList.kt b/src/main/kotlin/lotto/common/IntegerNumberList.kt index a89bad19d4..6b4b00c753 100644 --- a/src/main/kotlin/lotto/common/IntegerNumberList.kt +++ b/src/main/kotlin/lotto/common/IntegerNumberList.kt @@ -1,5 +1,5 @@ package lotto.common class IntegerNumberList( - val integerNumberList: List + val integerNumberList: List ) diff --git a/src/main/kotlin/lotto/common/NumberString.kt b/src/main/kotlin/lotto/common/NumberString.kt index d895209d6a..45f54cc0b7 100644 --- a/src/main/kotlin/lotto/common/NumberString.kt +++ b/src/main/kotlin/lotto/common/NumberString.kt @@ -12,7 +12,7 @@ class NumberString( return string.toCharArray().all { it in '0'..'9' } } - fun toIntegerNumber(): IntegerNumber { - return IntegerNumber(string.toInt()) + fun toIntegerNumber(): Int { + return string.toInt() } } diff --git a/src/main/kotlin/lotto/common/NumberStringList.kt b/src/main/kotlin/lotto/common/NumberStringList.kt index 87e0a0f01d..2a5e8f1751 100644 --- a/src/main/kotlin/lotto/common/NumberStringList.kt +++ b/src/main/kotlin/lotto/common/NumberStringList.kt @@ -5,7 +5,7 @@ class NumberStringList( ) { val list = string.split(",").map { NumberString(it.trim()) } - fun toIntegerNumberList(): List { + fun toIntegerNumberList(): List { return list.map { it.toIntegerNumber() } } } diff --git a/src/main/kotlin/lotto/domain/Lotto.kt b/src/main/kotlin/lotto/domain/Lotto.kt index ff01a206fe..4ccef854fc 100644 --- a/src/main/kotlin/lotto/domain/Lotto.kt +++ b/src/main/kotlin/lotto/domain/Lotto.kt @@ -1,7 +1,5 @@ package lotto.domain -import lotto.common.IntegerNumber - class Lotto( val lottoNumbers: List ) { @@ -10,9 +8,9 @@ class Lotto( require(lottoNumbers.toSet().size == LOTTO_NUMBERS_SIZE) { "번호에 중복이 있습니다." } } - fun countHitNumbers(luckyLotto: Lotto): IntegerNumber { + fun countHitNumbers(luckyLotto: Lotto): Int { val count = lottoNumbers.count { luckyLotto.lottoNumbers.contains(it) } - return IntegerNumber(count) + return count } fun hasBonusNumber(bonusNumber: LottoNumber): Boolean { diff --git a/src/main/kotlin/lotto/domain/LottoGenerator.kt b/src/main/kotlin/lotto/domain/LottoGenerator.kt index f2e0f0372c..578b0679e3 100644 --- a/src/main/kotlin/lotto/domain/LottoGenerator.kt +++ b/src/main/kotlin/lotto/domain/LottoGenerator.kt @@ -1,14 +1,13 @@ package lotto.domain -import lotto.common.IntegerNumber import lotto.common.IntegerNumberList import lotto.util.NumberGenerator class LottoGenerator( private val numberGenerator: NumberGenerator ) { - fun generate(size: IntegerNumber): List { - return List(size.number) { Lotto(randomNumber()) } + fun generate(size: Int): List { + return List(size) { Lotto(randomNumber()) } } private fun randomNumber(): List { diff --git a/src/main/kotlin/lotto/domain/LottoNumber.kt b/src/main/kotlin/lotto/domain/LottoNumber.kt index f862192569..3a6cd962b7 100644 --- a/src/main/kotlin/lotto/domain/LottoNumber.kt +++ b/src/main/kotlin/lotto/domain/LottoNumber.kt @@ -1,13 +1,11 @@ package lotto.domain -import lotto.common.IntegerNumber - data class LottoNumber( - val number: IntegerNumber + val number: Int ) { init { - require(number.number in LOTTO_MIN_NUMBER..LOTTO_MAX_NUMBER) { "로또 번호는 ${LOTTO_MIN_NUMBER}와 $LOTTO_MAX_NUMBER 사이 값 이여야 합니다." } + require(number in LOTTO_MIN_NUMBER..LOTTO_MAX_NUMBER) { "로또 번호는 ${LOTTO_MIN_NUMBER}와 $LOTTO_MAX_NUMBER 사이 값 이여야 합니다." } } companion object { diff --git a/src/main/kotlin/lotto/domain/LottoRank.kt b/src/main/kotlin/lotto/domain/LottoRank.kt index 86bca13353..5f12dd7915 100644 --- a/src/main/kotlin/lotto/domain/LottoRank.kt +++ b/src/main/kotlin/lotto/domain/LottoRank.kt @@ -1,18 +1,16 @@ package lotto.domain -import lotto.common.IntegerNumber - enum class LottoRank( - val hitCount: IntegerNumber, - val prizeMoney: IntegerNumber, + val hitCount: Int, + val prizeMoney: Int, val hasBonusNumber: Boolean = false ) { - FIRST(hitCount = IntegerNumber(6), prizeMoney = IntegerNumber(2000000000)), - SECOND(hitCount = IntegerNumber(5), prizeMoney = IntegerNumber(30000000), hasBonusNumber = true), - THIRD(hitCount = IntegerNumber(5), prizeMoney = IntegerNumber(1500000)), - FOURTH(hitCount = IntegerNumber(4), prizeMoney = IntegerNumber(50000)), - FIFTH(hitCount = IntegerNumber(3), prizeMoney = IntegerNumber(5000)), - MISS(hitCount = IntegerNumber(0), prizeMoney = IntegerNumber(0)); + FIRST(hitCount = 6, prizeMoney = 2000000000), + SECOND(hitCount = 5, prizeMoney = 30000000, hasBonusNumber = true), + THIRD(hitCount = 5, prizeMoney = 1500000), + FOURTH(hitCount = 4, prizeMoney = 50000), + FIFTH(hitCount = 3, prizeMoney = 5000), + MISS(hitCount = 0, prizeMoney = 0); private fun isHitBonusNumber(it: LottoRank, hasBonusNumber: Boolean): Boolean { if (it.hasBonusNumber) { @@ -22,12 +20,12 @@ enum class LottoRank( } companion object { - fun from(hitCount: IntegerNumber, hasBonusNumber: Boolean): LottoRank { + fun from(hitCount: Int, hasBonusNumber: Boolean): LottoRank { return values().find { it.hitCount == hitCount && it.isHitBonusNumber(it, hasBonusNumber) } ?: return MISS } fun winRanks(): List { - return values().filter { it.prizeMoney.isPositive() }.reversed() + return values().filter { it.prizeMoney > 0 }.reversed() } } } diff --git a/src/main/kotlin/lotto/domain/LottoResultService.kt b/src/main/kotlin/lotto/domain/LottoResultService.kt index 737e41e058..15cf926ac7 100644 --- a/src/main/kotlin/lotto/domain/LottoResultService.kt +++ b/src/main/kotlin/lotto/domain/LottoResultService.kt @@ -22,5 +22,5 @@ class LottoResultService( return LottoRank.from(hitCount, hasBonusNumber) } - private fun hasPrize(lottoRank: LottoRank) = lottoRank.prizeMoney.isPositive() + private fun hasPrize(lottoRank: LottoRank) = lottoRank.prizeMoney > 0 } diff --git a/src/main/kotlin/lotto/domain/LottoShop.kt b/src/main/kotlin/lotto/domain/LottoShop.kt index 0e2a72a497..cc3929dd89 100644 --- a/src/main/kotlin/lotto/domain/LottoShop.kt +++ b/src/main/kotlin/lotto/domain/LottoShop.kt @@ -1,6 +1,5 @@ package lotto.domain -import lotto.common.IntegerNumber import lotto.common.IntegerNumberList class LottoShop( @@ -13,19 +12,19 @@ class LottoShop( fun buyManualLotto(payment: Payment, manualNumberList: List): List { val totalLottoCount = calculateLottoCount(payment) - val manualLottoCount = IntegerNumber(manualNumberList.size) + val manualLottoCount = manualNumberList.size - require(manualLottoCount.number <= totalLottoCount.number) { "수동 로또 개수가 지불 금액보다 많아 로또를 구입할 수 없습니다." } + require(manualLottoCount <= totalLottoCount) { "수동 로또 개수가 지불 금액보다 많아 로또를 구입할 수 없습니다." } - payment.charge(manualLottoCount.multiply(LOTTO_PRICE)) + payment.charge(manualLottoCount * LOTTO_PRICE) return lottoGenerator.generate(manualNumberList) } - private fun calculateLottoCount(payment: Payment): IntegerNumber { - return payment.payment.divide(LOTTO_PRICE) + private fun calculateLottoCount(payment: Payment): Int { + return payment.payment / LOTTO_PRICE } companion object { - private val LOTTO_PRICE = IntegerNumber(1000) + private val LOTTO_PRICE = 1000 } } diff --git a/src/main/kotlin/lotto/domain/LottoStatistics.kt b/src/main/kotlin/lotto/domain/LottoStatistics.kt index 08404f78cf..3eac6586b8 100644 --- a/src/main/kotlin/lotto/domain/LottoStatistics.kt +++ b/src/main/kotlin/lotto/domain/LottoStatistics.kt @@ -1,16 +1,14 @@ package lotto.domain -import lotto.common.DoubleNumber -import lotto.common.IntegerNumber import lotto.util.NumberUtil class LottoStatistics( private val winLottoList: List ) { - private val totalPrize: IntegerNumber = IntegerNumber(winLottoList.sumOf { it.prizeMoney.number }) + private val totalPrize: Int = winLottoList.sumOf { it.prizeMoney } - fun earningRate(inputPayment: Payment): DoubleNumber { - val earningRate = totalPrize.toDouble().divide(inputPayment.payment.toDouble()) + fun earningRate(inputPayment: Payment): Double { + val earningRate = totalPrize.toDouble() / inputPayment.payment.toDouble() return NumberUtil.floor(earningRate, EARNING_RATE_DECIMAL_PLACE) } @@ -20,12 +18,12 @@ class LottoStatistics( return LottoRank.winRanks().map { LottoStatisticsResult( lottoRank = it, - winLottoCount = IntegerNumber(hitCountMap.getOrDefault(it, emptyList()).size) + winLottoCount = hitCountMap.getOrDefault(it, emptyList()).size ) } } companion object { - private val EARNING_RATE_DECIMAL_PLACE = DoubleNumber(2.0) + private val EARNING_RATE_DECIMAL_PLACE = 2.0 } } diff --git a/src/main/kotlin/lotto/domain/LottoStatisticsResult.kt b/src/main/kotlin/lotto/domain/LottoStatisticsResult.kt index c07b2e174a..955a7464f4 100644 --- a/src/main/kotlin/lotto/domain/LottoStatisticsResult.kt +++ b/src/main/kotlin/lotto/domain/LottoStatisticsResult.kt @@ -1,8 +1,6 @@ package lotto.domain -import lotto.common.IntegerNumber - class LottoStatisticsResult( val lottoRank: LottoRank, - val winLottoCount: IntegerNumber + val winLottoCount: Int ) diff --git a/src/main/kotlin/lotto/domain/LottoStatisticsTotal.kt b/src/main/kotlin/lotto/domain/LottoStatisticsTotal.kt index e006ffb058..f261a191f1 100644 --- a/src/main/kotlin/lotto/domain/LottoStatisticsTotal.kt +++ b/src/main/kotlin/lotto/domain/LottoStatisticsTotal.kt @@ -1,8 +1,6 @@ package lotto.domain -import lotto.common.DoubleNumber - class LottoStatisticsTotal( - val earningRate: DoubleNumber, + val earningRate: Double, val winLottoStatisticsResult: List ) diff --git a/src/main/kotlin/lotto/domain/Payment.kt b/src/main/kotlin/lotto/domain/Payment.kt index e39c935a2d..3c001dcaaa 100644 --- a/src/main/kotlin/lotto/domain/Payment.kt +++ b/src/main/kotlin/lotto/domain/Payment.kt @@ -1,16 +1,14 @@ package lotto.domain -import lotto.common.IntegerNumber - class Payment( - var payment: IntegerNumber + var payment: Int ) { init { - require(payment.isNegative().not()) { "지불액은 음수가 될 수 없습니다." } + require(payment >= 0) { "지불액은 음수가 될 수 없습니다." } } - fun charge(charge: IntegerNumber) { - require(payment.number >= charge.number) { "잔액 부족으로 금액을 차감할 수 없습니다." } + fun charge(charge: Int) { + require(payment >= charge) { "잔액 부족으로 금액을 차감할 수 없습니다." } payment = payment.minus(charge) } } diff --git a/src/main/kotlin/lotto/util/NumberGenerator.kt b/src/main/kotlin/lotto/util/NumberGenerator.kt index 1ab05319dc..9d2cf01b7c 100644 --- a/src/main/kotlin/lotto/util/NumberGenerator.kt +++ b/src/main/kotlin/lotto/util/NumberGenerator.kt @@ -1,7 +1,5 @@ package lotto.util -import lotto.common.IntegerNumber - interface NumberGenerator { - fun generate(start: Int, end: Int, size: Int): List + fun generate(start: Int, end: Int, size: Int): List } diff --git a/src/main/kotlin/lotto/util/NumberUtil.kt b/src/main/kotlin/lotto/util/NumberUtil.kt index 820a69b123..9c2bd66be7 100644 --- a/src/main/kotlin/lotto/util/NumberUtil.kt +++ b/src/main/kotlin/lotto/util/NumberUtil.kt @@ -1,14 +1,14 @@ package lotto.util -import lotto.common.DoubleNumber +import kotlin.math.pow object NumberUtil { - fun floor(number: DoubleNumber, decimalPlace: DoubleNumber): DoubleNumber { - if (decimalPlace.isNegative()) { + fun floor(number: Double, decimalPlace: Double): Double { + if (decimalPlace < 0) { return number } - val pow = DoubleNumber(10.0).pow(decimalPlace) - val floor = number.multiply(pow).floor() - return floor.divide(pow) + val pow = 10.0.pow(decimalPlace) + val floor = kotlin.math.floor(number * pow) + return floor / pow } } diff --git a/src/main/kotlin/lotto/util/RandomNumberGenerator.kt b/src/main/kotlin/lotto/util/RandomNumberGenerator.kt index 5c2a6dc931..3818b02baf 100644 --- a/src/main/kotlin/lotto/util/RandomNumberGenerator.kt +++ b/src/main/kotlin/lotto/util/RandomNumberGenerator.kt @@ -1,12 +1,10 @@ package lotto.util -import lotto.common.IntegerNumber - class RandomNumberGenerator : NumberGenerator { - override fun generate(start: Int, end: Int, size: Int): List { + override fun generate(start: Int, end: Int, size: Int): List { val range = start..end val shuffled = range.shuffled() val subList = shuffled.subList(0, size) - return subList.map { IntegerNumber(it) } + return subList.map { it } } } diff --git a/src/main/kotlin/lotto/view/InputView.kt b/src/main/kotlin/lotto/view/InputView.kt index 21d5e2c0ed..c173d0f256 100644 --- a/src/main/kotlin/lotto/view/InputView.kt +++ b/src/main/kotlin/lotto/view/InputView.kt @@ -1,6 +1,5 @@ package lotto.view -import lotto.common.IntegerNumber import lotto.common.IntegerNumberList import lotto.common.NumberString import lotto.common.NumberStringList @@ -14,14 +13,14 @@ class InputView { return Payment(payment.toIntegerNumber()) } - fun inputManualLottoCount(): IntegerNumber { + fun inputManualLottoCount(): Int { println(INPUT_MANUAL_LOTTO_COUNT_GUIDE) return inputNumber() } - fun inputManualLottoNumbers(count: IntegerNumber): List { + fun inputManualLottoNumbers(count: Int): List { println(INPUT_MANUAL_LOTTO_NUMBERS_GUIDE) - return List(count.number) { + return List(count) { inputNumberList() } } @@ -36,12 +35,12 @@ class InputView { return IntegerNumberList(numberList) } - fun inputBonusNumber(): IntegerNumber { + fun inputBonusNumber(): Int { println(INPUT_BONUS_BALL) return inputNumber() } - private fun inputNumber(): IntegerNumber { + private fun inputNumber(): Int { return NumberString(readln()).toIntegerNumber() } diff --git a/src/main/kotlin/lotto/view/ResultView.kt b/src/main/kotlin/lotto/view/ResultView.kt index 07cc0f5e42..ce1dc5c555 100644 --- a/src/main/kotlin/lotto/view/ResultView.kt +++ b/src/main/kotlin/lotto/view/ResultView.kt @@ -1,6 +1,5 @@ package lotto.view -import lotto.common.DoubleNumber import lotto.domain.Lotto import lotto.domain.LottoRank import lotto.domain.LottoStatisticsResult @@ -47,9 +46,9 @@ class ResultView { return "" } - private fun printEarningRate(earningRate: DoubleNumber) { + private fun printEarningRate(earningRate: Double) { print("총 수익률은 ${earningRate}입니다. ") - if (earningRate.isGreaterThanEquals(DoubleNumber(1.0))) { + if (earningRate >= 1.0) { println("(기준이 1이기 때문에 이익입니다.)") return } diff --git a/src/test/kotlin/lotto/common/DoubleNumberTest.kt b/src/test/kotlin/lotto/common/DoubleNumberTest.kt deleted file mode 100644 index b66ce6d2a3..0000000000 --- a/src/test/kotlin/lotto/common/DoubleNumberTest.kt +++ /dev/null @@ -1,72 +0,0 @@ -package lotto.common - -import io.kotest.core.spec.style.StringSpec -import io.kotest.matchers.shouldBe - -class DoubleNumberTest : StringSpec({ - "양수값을 가졌을때 isNegative 에서 false를 반환한다." { - // given - val double = DoubleNumber(1.0) - // when - val actual = double.isNegative() - // then - actual shouldBe false - } - - "음수값을 가졌을때 isNegative 에서 true를 반환한다." { - // given - val double = DoubleNumber(-1.0) - // when - val actual = double.isNegative() - // then - actual shouldBe true - } - - "제곱 테스트" { - // given - val double = DoubleNumber(2.0) - // when - val actual = double.pow(DoubleNumber(2.0)) - // then - actual shouldBe DoubleNumber(4.0) - } - - "곱셈 테스트" { - // given - val x = DoubleNumber(2.0) - val y = DoubleNumber(3.0) - // when - val actual = x.multiply(y) - // then - actual shouldBe DoubleNumber(6.0) - } - - "나눗셈 테스트" { - // given - val x = DoubleNumber(6.0) - val y = DoubleNumber(3.0) - // when - val actual = x.divide(y) - // then - actual shouldBe DoubleNumber(2.0) - } - - "소숫점 버림 테스트" { - // given - val double = DoubleNumber(1.2) - // when - val actual = double.floor() - // then - actual shouldBe DoubleNumber(1.0) - } - - "x보다 y가 크거나 같으면 true를 반환한다." { - // given - val x = DoubleNumber(1.2) - val y = DoubleNumber(1.1) - // when - val actual = x.isGreaterThanEquals(y) - // then - actual shouldBe true - } -}) diff --git a/src/test/kotlin/lotto/common/IntegerNumberTest.kt b/src/test/kotlin/lotto/common/IntegerNumberTest.kt deleted file mode 100644 index 3e69bd1e14..0000000000 --- a/src/test/kotlin/lotto/common/IntegerNumberTest.kt +++ /dev/null @@ -1,81 +0,0 @@ -package lotto.common - -import io.kotest.core.spec.style.StringSpec -import io.kotest.matchers.shouldBe - -class IntegerNumberTest : StringSpec({ - "양수값을 가졌을때 isPositive 에서 true를 반환한다." { - // given - val integer = IntegerNumber(1) - // when - val actual = integer.isPositive() - // then - actual shouldBe true - } - - "음수값을 가졌을때 isPositive 에서 false를 반환한다." { - // given - val integer = IntegerNumber(-1) - // when - val actual = integer.isPositive() - // then - actual shouldBe false - } - - "양수값을 가졌을때 isNegative 에서 false를 반환한다." { - // given - val integer = IntegerNumber(1) - // when - val actual = integer.isNegative() - // then - actual shouldBe false - } - - "음수값을 가졌을때 isNegative 에서 true를 반환한다." { - // given - val integer = IntegerNumber(-1) - // when - val actual = integer.isNegative() - // then - actual shouldBe true - } - - "Double 변환 테스트" { - // given - val integer = IntegerNumber(1) - // when - val actual = integer.toDouble() - // then - actual shouldBe DoubleNumber(1.0) - } - - "나눗셈 테스트" { - // given - val x = IntegerNumber(6) - val y = IntegerNumber(2) - // when - val actual = x.divide(y) - // then - actual shouldBe IntegerNumber(3) - } - - "뺄셈 테스트" { - // given - val x = IntegerNumber(6) - val y = IntegerNumber(2) - // when - val actual = x.minus(y) - // then - actual shouldBe IntegerNumber(4) - } - - "곱셈 테스트" { - // given - val x = IntegerNumber(6) - val y = IntegerNumber(2) - // when - val actual = x.multiply(y) - // then - actual shouldBe IntegerNumber(12) - } -}) diff --git a/src/test/kotlin/lotto/common/NumberStringListTest.kt b/src/test/kotlin/lotto/common/NumberStringListTest.kt index 996f127503..74c446d2fd 100644 --- a/src/test/kotlin/lotto/common/NumberStringListTest.kt +++ b/src/test/kotlin/lotto/common/NumberStringListTest.kt @@ -33,6 +33,6 @@ class NumberStringListTest : StringSpec({ // when val numberList = numberStringList.toIntegerNumberList() // then - numberList shouldBe listOf(IntegerNumber(1), IntegerNumber(2), IntegerNumber(3), IntegerNumber(4), IntegerNumber(5)) + numberList shouldBe listOf(1, 2, 3, 4, 5) } }) diff --git a/src/test/kotlin/lotto/common/NumberStringTest.kt b/src/test/kotlin/lotto/common/NumberStringTest.kt index 9ab9e93a8b..ea121ec8bb 100644 --- a/src/test/kotlin/lotto/common/NumberStringTest.kt +++ b/src/test/kotlin/lotto/common/NumberStringTest.kt @@ -26,31 +26,4 @@ class NumberStringTest : StringSpec({ } exception.message shouldBe "값이 비어있습니다." } - - "양수 판단 테스트" { - // given - val number = IntegerNumber(1) - // when - val actual = number.isPositive() - // then - actual shouldBe true - } - - "음수 판단 테스트" { - // given - val number = IntegerNumber(-1) - // when - val actual = number.isNegative() - // then - actual shouldBe true - } - - "double 타입으로 변환 테스트" { - // given - val number = IntegerNumber(1) - // when - val actual = number.toDouble() - // then - actual shouldBe DoubleNumber(1.0) - } }) diff --git a/src/test/kotlin/lotto/domain/LottoGeneratorTest.kt b/src/test/kotlin/lotto/domain/LottoGeneratorTest.kt index c0cc4fd176..507e9a6f46 100644 --- a/src/test/kotlin/lotto/domain/LottoGeneratorTest.kt +++ b/src/test/kotlin/lotto/domain/LottoGeneratorTest.kt @@ -2,7 +2,6 @@ package lotto.domain import io.kotest.core.spec.style.StringSpec import io.kotest.matchers.collections.shouldHaveSize -import lotto.common.IntegerNumber import lotto.util.RandomNumberGenerator class LottoGeneratorTest : StringSpec({ @@ -11,10 +10,10 @@ class LottoGeneratorTest : StringSpec({ "로또 생성 개수 확인 테스트" { // given - val inputSize = IntegerNumber(24) + val inputSize = 24 // when val resultLotto = lottoGenerator.generate(inputSize) // then - resultLotto shouldHaveSize inputSize.number + resultLotto shouldHaveSize inputSize } }) diff --git a/src/test/kotlin/lotto/domain/LottoShopTest.kt b/src/test/kotlin/lotto/domain/LottoShopTest.kt index 4a522d3ce3..ca474cdf1e 100644 --- a/src/test/kotlin/lotto/domain/LottoShopTest.kt +++ b/src/test/kotlin/lotto/domain/LottoShopTest.kt @@ -5,7 +5,6 @@ import io.kotest.data.forAll import io.kotest.data.row import io.kotest.matchers.equality.shouldBeEqualToComparingFields import io.kotest.matchers.shouldBe -import lotto.common.IntegerNumber import lotto.common.IntegerNumberList import lotto.util.RandomNumberGenerator @@ -16,9 +15,9 @@ class LottoShopTest : StringSpec({ "로또 구매 개수 검증 테스트" { forAll( // given - row("0원을 내면 0개를 반환한다.", Payment(IntegerNumber(0)), 0), - row("5000원을 내면 5개를 반환한다.", Payment(IntegerNumber(5000)), 5), - row("5500원을 내면 5개를 반환한다.", Payment(IntegerNumber(5500)), 5) + row("0원을 내면 0개를 반환한다.", Payment(0), 0), + row("5000원을 내면 5개를 반환한다.", Payment(5000), 5), + row("5500원을 내면 5개를 반환한다.", Payment(5500), 5) ) { title, payment, expectedSize -> // when val actual = lottoShop.buyAutoLotto(payment) @@ -30,23 +29,23 @@ class LottoShopTest : StringSpec({ "수동 로또 구매 테스트" { val manualNumberList = IntegerNumberList( listOf( - IntegerNumber(1), - IntegerNumber(2), - IntegerNumber(3), - IntegerNumber(4), - IntegerNumber(5), - IntegerNumber(6) + 1, + 2, + 3, + 4, + 5, + 6 ) ) - val result = lottoShop.buyManualLotto(Payment(IntegerNumber(1000)), listOf(manualNumberList)) + val result = lottoShop.buyManualLotto(Payment(1000), listOf(manualNumberList)) result[0] shouldBeEqualToComparingFields Lotto( listOf( - LottoNumber(IntegerNumber(1)), - LottoNumber(IntegerNumber(2)), - LottoNumber(IntegerNumber(3)), - LottoNumber(IntegerNumber(4)), - LottoNumber(IntegerNumber(5)), - LottoNumber(IntegerNumber(6)) + LottoNumber(1), + LottoNumber(2), + LottoNumber(3), + LottoNumber(4), + LottoNumber(5), + LottoNumber(6) ) ) } diff --git a/src/test/kotlin/lotto/domain/LottoStatisticsServiceTest.kt b/src/test/kotlin/lotto/domain/LottoStatisticsServiceTest.kt index a2dee42b5e..baea82e0a1 100644 --- a/src/test/kotlin/lotto/domain/LottoStatisticsServiceTest.kt +++ b/src/test/kotlin/lotto/domain/LottoStatisticsServiceTest.kt @@ -5,32 +5,31 @@ import io.kotest.data.forAll import io.kotest.data.row import io.kotest.matchers.equality.shouldBeEqualToComparingFields import io.kotest.matchers.shouldBe -import lotto.common.IntegerNumber class LottoStatisticsServiceTest : StringSpec({ "당첨자 통계 통합 결과 테스트" { // given - val payment = Payment(IntegerNumber(15000)) + val payment = Payment(15000) forAll( row( LottoRank.FOURTH, listOf( - LottoStatisticsResult(LottoRank.FIFTH, IntegerNumber(0)), - LottoStatisticsResult(LottoRank.FOURTH, IntegerNumber(1)), - LottoStatisticsResult(LottoRank.THIRD, IntegerNumber(0)), - LottoStatisticsResult(LottoRank.SECOND, IntegerNumber(0)), - LottoStatisticsResult(LottoRank.FIRST, IntegerNumber(0)), + LottoStatisticsResult(LottoRank.FIFTH, 0), + LottoStatisticsResult(LottoRank.FOURTH, 1), + LottoStatisticsResult(LottoRank.THIRD, 0), + LottoStatisticsResult(LottoRank.SECOND, 0), + LottoStatisticsResult(LottoRank.FIRST, 0), ) ), row( LottoRank.FIRST, listOf( - LottoStatisticsResult(LottoRank.FIFTH, IntegerNumber(0)), - LottoStatisticsResult(LottoRank.FOURTH, IntegerNumber(0)), - LottoStatisticsResult(LottoRank.THIRD, IntegerNumber(0)), - LottoStatisticsResult(LottoRank.SECOND, IntegerNumber(0)), - LottoStatisticsResult(LottoRank.FIRST, IntegerNumber(1)), + LottoStatisticsResult(LottoRank.FIFTH, 0), + LottoStatisticsResult(LottoRank.FOURTH, 0), + LottoStatisticsResult(LottoRank.THIRD, 0), + LottoStatisticsResult(LottoRank.SECOND, 0), + LottoStatisticsResult(LottoRank.FIRST, 1), ) ) ) { lottoResult, expected -> diff --git a/src/test/kotlin/lotto/domain/LottoStatisticsTest.kt b/src/test/kotlin/lotto/domain/LottoStatisticsTest.kt index 5d398a4127..5b837b7d9c 100644 --- a/src/test/kotlin/lotto/domain/LottoStatisticsTest.kt +++ b/src/test/kotlin/lotto/domain/LottoStatisticsTest.kt @@ -4,15 +4,13 @@ import io.kotest.core.spec.style.StringSpec import io.kotest.data.forAll import io.kotest.data.row import io.kotest.matchers.shouldBe -import lotto.common.DoubleNumber -import lotto.common.IntegerNumber class LottoStatisticsTest : StringSpec({ "로또 수익률 계산 테스트" { forAll( - row(listOf(LottoRank.FOURTH, LottoRank.FIFTH), Payment(IntegerNumber(100000)), DoubleNumber(0.55)), - row(listOf(LottoRank.FIFTH), Payment(IntegerNumber(5000)), DoubleNumber(1.0)), - row(listOf(LottoRank.FOURTH), Payment(IntegerNumber(5000)), DoubleNumber(10.0)), + row(listOf(LottoRank.FOURTH, LottoRank.FIFTH), Payment(100000), 0.55), + row(listOf(LottoRank.FIFTH), Payment(5000), 1.0), + row(listOf(LottoRank.FOURTH), Payment(5000), 10.0), ) { prizeList, payment, expectedEarningRate -> // given val lottoStatistics = LottoStatistics(prizeList) @@ -36,11 +34,11 @@ class LottoStatisticsTest : StringSpec({ val lottoStatistics = LottoStatistics(winLottoList) val expected = listOf( - LottoStatisticsResult(LottoRank.FIFTH, IntegerNumber(3)), - LottoStatisticsResult(LottoRank.FOURTH, IntegerNumber(0)), - LottoStatisticsResult(LottoRank.THIRD, IntegerNumber(2)), - LottoStatisticsResult(LottoRank.SECOND, IntegerNumber(0)), - LottoStatisticsResult(LottoRank.FIRST, IntegerNumber(1)), + LottoStatisticsResult(LottoRank.FIFTH, 3), + LottoStatisticsResult(LottoRank.FOURTH, 0), + LottoStatisticsResult(LottoRank.THIRD, 2), + LottoStatisticsResult(LottoRank.SECOND, 0), + LottoStatisticsResult(LottoRank.FIRST, 1), ) // when diff --git a/src/test/kotlin/lotto/domain/LottoTest.kt b/src/test/kotlin/lotto/domain/LottoTest.kt index efcc5c35f2..f7289d29b8 100644 --- a/src/test/kotlin/lotto/domain/LottoTest.kt +++ b/src/test/kotlin/lotto/domain/LottoTest.kt @@ -3,19 +3,18 @@ package lotto.domain import io.kotest.assertions.throwables.shouldThrowExactly import io.kotest.core.spec.style.StringSpec import io.kotest.matchers.shouldBe -import lotto.common.IntegerNumber class LottoTest : StringSpec({ "로또 번호가 6개를 넘으면 에러 발생 테스트" { val numbers = listOf( - LottoNumber(IntegerNumber(1)), - LottoNumber(IntegerNumber(2)), - LottoNumber(IntegerNumber(3)), - LottoNumber(IntegerNumber(4)), - LottoNumber(IntegerNumber(5)), - LottoNumber(IntegerNumber(6)), - LottoNumber(IntegerNumber(7)) + LottoNumber(1), + LottoNumber(2), + LottoNumber(3), + LottoNumber(4), + LottoNumber(5), + LottoNumber(6), + LottoNumber(7) ) val exception = shouldThrowExactly { Lotto(numbers) @@ -25,12 +24,12 @@ class LottoTest : StringSpec({ "로또 번호 중복 에러 테스트" { val numbers = listOf( - LottoNumber(IntegerNumber(1)), - LottoNumber(IntegerNumber(2)), - LottoNumber(IntegerNumber(3)), - LottoNumber(IntegerNumber(4)), - LottoNumber(IntegerNumber(5)), - LottoNumber(IntegerNumber(1)) + LottoNumber(1), + LottoNumber(2), + LottoNumber(3), + LottoNumber(4), + LottoNumber(5), + LottoNumber(1) ) val exception = shouldThrowExactly { Lotto(numbers) @@ -41,42 +40,42 @@ class LottoTest : StringSpec({ "당첨 번호 개수 카운트 테스트" { // given val numbers = listOf( - LottoNumber(IntegerNumber(1)), - LottoNumber(IntegerNumber(2)), - LottoNumber(IntegerNumber(3)), - LottoNumber(IntegerNumber(4)), - LottoNumber(IntegerNumber(5)), - LottoNumber(IntegerNumber(6)) + LottoNumber(1), + LottoNumber(2), + LottoNumber(3), + LottoNumber(4), + LottoNumber(5), + LottoNumber(6) ) val lotto = Lotto(numbers) val luckyNumbers = listOf( - LottoNumber(IntegerNumber(2)), - LottoNumber(IntegerNumber(4)), - LottoNumber(IntegerNumber(6)), - LottoNumber(IntegerNumber(8)), - LottoNumber(IntegerNumber(10)), - LottoNumber(IntegerNumber(12)) + LottoNumber(2), + LottoNumber(4), + LottoNumber(6), + LottoNumber(8), + LottoNumber(10), + LottoNumber(12) ) val luckyLotto = Lotto(luckyNumbers) // when val actual = lotto.countHitNumbers(luckyLotto) // then - actual shouldBe IntegerNumber(3) + actual shouldBe 3 } "보너스 번호 포함되면 containsBonusNumber에서 true를 반환" { // given val numbers = listOf( - LottoNumber(IntegerNumber(1)), - LottoNumber(IntegerNumber(2)), - LottoNumber(IntegerNumber(3)), - LottoNumber(IntegerNumber(4)), - LottoNumber(IntegerNumber(5)), - LottoNumber(IntegerNumber(6)) + LottoNumber(1), + LottoNumber(2), + LottoNumber(3), + LottoNumber(4), + LottoNumber(5), + LottoNumber(6) ) val lotto = Lotto(numbers) - val bonusNumber = LottoNumber(IntegerNumber(1)) + val bonusNumber = LottoNumber(1) // when val actual = lotto.hasBonusNumber(bonusNumber) // then @@ -86,15 +85,15 @@ class LottoTest : StringSpec({ "보너스 번호 포함 안되면 containsBonusNumber에서 false를 반환" { // given val numbers = listOf( - LottoNumber(IntegerNumber(1)), - LottoNumber(IntegerNumber(2)), - LottoNumber(IntegerNumber(3)), - LottoNumber(IntegerNumber(4)), - LottoNumber(IntegerNumber(5)), - LottoNumber(IntegerNumber(6)) + LottoNumber(1), + LottoNumber(2), + LottoNumber(3), + LottoNumber(4), + LottoNumber(5), + LottoNumber(6) ) val lotto = Lotto(numbers) - val bonusNumber = LottoNumber(IntegerNumber(7)) + val bonusNumber = LottoNumber(7) // when val actual = lotto.hasBonusNumber(bonusNumber) // then diff --git a/src/test/kotlin/lotto/domain/PaymentTest.kt b/src/test/kotlin/lotto/domain/PaymentTest.kt index 724dc81882..5d3be66fa3 100644 --- a/src/test/kotlin/lotto/domain/PaymentTest.kt +++ b/src/test/kotlin/lotto/domain/PaymentTest.kt @@ -3,31 +3,30 @@ package lotto.domain import io.kotest.assertions.throwables.shouldThrowExactly import io.kotest.core.spec.style.StringSpec import io.kotest.matchers.shouldBe -import lotto.common.IntegerNumber class PaymentTest : StringSpec({ "숫자 아닐때 에러 발생 테스트" { val exception = shouldThrowExactly { - Payment(IntegerNumber(-1)) + Payment(-1) } exception.message shouldBe "지불액은 음수가 될 수 없습니다." } "지불금 차감 테스트" { // given - val payment = Payment(IntegerNumber(1000)) + val payment = Payment(1000) // when - payment.charge(IntegerNumber(100)) + payment.charge(100) // then - payment.payment shouldBe IntegerNumber(900) + payment.payment shouldBe 900 } "잔액 부족으로 지불금 차감 에러 테스트" { // given - val payment = Payment(IntegerNumber(1000)) + val payment = Payment(1000) // when val exception = shouldThrowExactly { - payment.charge(IntegerNumber(1100)) + payment.charge(1100) } // then exception.message shouldBe "잔액 부족으로 금액을 차감할 수 없습니다." diff --git a/src/test/kotlin/lotto/util/NumberUtilTest.kt b/src/test/kotlin/lotto/util/NumberUtilTest.kt index 1a7ed55152..5e6558654c 100644 --- a/src/test/kotlin/lotto/util/NumberUtilTest.kt +++ b/src/test/kotlin/lotto/util/NumberUtilTest.kt @@ -4,16 +4,15 @@ import io.kotest.core.spec.style.StringSpec import io.kotest.data.forAll import io.kotest.data.row import io.kotest.matchers.shouldBe -import lotto.common.DoubleNumber class NumberUtilTest : StringSpec({ "소숫점 지정 자리수 이하 버림 테스트" { forAll( // given - row(DoubleNumber(1.2345), DoubleNumber(0.0), DoubleNumber(1.0)), - row(DoubleNumber(1.2345), DoubleNumber(3.0), DoubleNumber(1.234)), - row(DoubleNumber(1.2345), DoubleNumber(-1.0), DoubleNumber(1.2345)) + row(1.2345, 0.0, 1.0), + row(1.2345, 3.0, 1.234), + row(1.2345, -1.0, 1.2345) ) { number, decimalPlace, expectedResult -> // when val actualResult = NumberUtil.floor(number, decimalPlace) diff --git a/src/test/kotlin/lotto/util/RandomNumberGeneratorTest.kt b/src/test/kotlin/lotto/util/RandomNumberGeneratorTest.kt index 568f30c158..665c4f3e38 100644 --- a/src/test/kotlin/lotto/util/RandomNumberGeneratorTest.kt +++ b/src/test/kotlin/lotto/util/RandomNumberGeneratorTest.kt @@ -18,7 +18,7 @@ class RandomNumberGeneratorTest : StringSpec({ // then randomNumbers shouldHaveSize inputSize randomNumbers.forEach { - it.number shouldBeInRange start..end + it shouldBeInRange start..end } } }) From c0e7831b14b627a983acbdcc24a5596dae435c52 Mon Sep 17 00:00:00 2001 From: dajeong Date: Tue, 27 Dec 2022 12:25:02 +0900 Subject: [PATCH 20/21] =?UTF-8?q?[step4]=20=ED=94=BC=EB=93=9C=EB=B0=B1=20?= =?UTF-8?q?=EB=B0=98=EC=98=81=20-=20=EB=8B=B9=EC=B2=A8=EA=B8=88=EC=9D=84?= =?UTF-8?q?=20=EC=9C=84=ED=95=9C=20Money=20=ED=81=B4=EB=9E=98=EC=8A=A4=20?= =?UTF-8?q?=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit + 수익률 오류 수정 + Payment 클래스의 프로퍼티명 수정 --- .../kotlin/lotto/application/Application.kt | 5 ++-- src/main/kotlin/lotto/domain/LottoRank.kt | 16 ++++++------- .../kotlin/lotto/domain/LottoResultService.kt | 2 +- src/main/kotlin/lotto/domain/LottoShop.kt | 4 ++-- .../kotlin/lotto/domain/LottoStatistics.kt | 6 ++--- src/main/kotlin/lotto/domain/Money.kt | 13 ++++++++++ src/main/kotlin/lotto/domain/Payment.kt | 12 +++++----- src/test/kotlin/lotto/domain/MoneyTest.kt | 24 +++++++++++++++++++ src/test/kotlin/lotto/domain/PaymentTest.kt | 2 +- 9 files changed, 61 insertions(+), 23 deletions(-) create mode 100644 src/main/kotlin/lotto/domain/Money.kt create mode 100644 src/test/kotlin/lotto/domain/MoneyTest.kt diff --git a/src/main/kotlin/lotto/application/Application.kt b/src/main/kotlin/lotto/application/Application.kt index 702b2a7ac4..dd5a7323dc 100644 --- a/src/main/kotlin/lotto/application/Application.kt +++ b/src/main/kotlin/lotto/application/Application.kt @@ -19,8 +19,9 @@ class Application { val manualLottoCount = inputView.inputManualLottoCount() val manualNumberList = inputView.inputManualLottoNumbers(manualLottoCount) - val manualLottoList = lottoShop.buyManualLotto(inputPayment, manualNumberList) - val autoLottoList = lottoShop.buyAutoLotto(inputPayment) + val usedPayment = inputPayment.copy() + val manualLottoList = lottoShop.buyManualLotto(usedPayment, manualNumberList) + val autoLottoList = lottoShop.buyAutoLotto(usedPayment) resultView.printLottoList(manualLottoList, autoLottoList) val lottoList = manualLottoList + autoLottoList diff --git a/src/main/kotlin/lotto/domain/LottoRank.kt b/src/main/kotlin/lotto/domain/LottoRank.kt index 5f12dd7915..5c632eadb1 100644 --- a/src/main/kotlin/lotto/domain/LottoRank.kt +++ b/src/main/kotlin/lotto/domain/LottoRank.kt @@ -2,15 +2,15 @@ package lotto.domain enum class LottoRank( val hitCount: Int, - val prizeMoney: Int, + val prizeMoney: Money, val hasBonusNumber: Boolean = false ) { - FIRST(hitCount = 6, prizeMoney = 2000000000), - SECOND(hitCount = 5, prizeMoney = 30000000, hasBonusNumber = true), - THIRD(hitCount = 5, prizeMoney = 1500000), - FOURTH(hitCount = 4, prizeMoney = 50000), - FIFTH(hitCount = 3, prizeMoney = 5000), - MISS(hitCount = 0, prizeMoney = 0); + FIRST(hitCount = 6, prizeMoney = Money(2000000000)), + SECOND(hitCount = 5, prizeMoney = Money(30000000), hasBonusNumber = true), + THIRD(hitCount = 5, prizeMoney = Money(1500000)), + FOURTH(hitCount = 4, prizeMoney = Money(50000)), + FIFTH(hitCount = 3, prizeMoney = Money(5000)), + MISS(hitCount = 0, prizeMoney = Money(0)); private fun isHitBonusNumber(it: LottoRank, hasBonusNumber: Boolean): Boolean { if (it.hasBonusNumber) { @@ -25,7 +25,7 @@ enum class LottoRank( } fun winRanks(): List { - return values().filter { it.prizeMoney > 0 }.reversed() + return values().filter { it.prizeMoney.amount > 0 }.reversed() } } } diff --git a/src/main/kotlin/lotto/domain/LottoResultService.kt b/src/main/kotlin/lotto/domain/LottoResultService.kt index 15cf926ac7..737e41e058 100644 --- a/src/main/kotlin/lotto/domain/LottoResultService.kt +++ b/src/main/kotlin/lotto/domain/LottoResultService.kt @@ -22,5 +22,5 @@ class LottoResultService( return LottoRank.from(hitCount, hasBonusNumber) } - private fun hasPrize(lottoRank: LottoRank) = lottoRank.prizeMoney > 0 + private fun hasPrize(lottoRank: LottoRank) = lottoRank.prizeMoney.isPositive() } diff --git a/src/main/kotlin/lotto/domain/LottoShop.kt b/src/main/kotlin/lotto/domain/LottoShop.kt index cc3929dd89..98e30daeeb 100644 --- a/src/main/kotlin/lotto/domain/LottoShop.kt +++ b/src/main/kotlin/lotto/domain/LottoShop.kt @@ -21,10 +21,10 @@ class LottoShop( } private fun calculateLottoCount(payment: Payment): Int { - return payment.payment / LOTTO_PRICE + return payment.amount / LOTTO_PRICE } companion object { - private val LOTTO_PRICE = 1000 + private const val LOTTO_PRICE = 1000 } } diff --git a/src/main/kotlin/lotto/domain/LottoStatistics.kt b/src/main/kotlin/lotto/domain/LottoStatistics.kt index 3eac6586b8..faf3faa3b3 100644 --- a/src/main/kotlin/lotto/domain/LottoStatistics.kt +++ b/src/main/kotlin/lotto/domain/LottoStatistics.kt @@ -5,10 +5,10 @@ import lotto.util.NumberUtil class LottoStatistics( private val winLottoList: List ) { - private val totalPrize: Int = winLottoList.sumOf { it.prizeMoney } + private val totalPrize: Int = winLottoList.sumOf { it.prizeMoney.amount } fun earningRate(inputPayment: Payment): Double { - val earningRate = totalPrize.toDouble() / inputPayment.payment.toDouble() + val earningRate = totalPrize.toDouble() / inputPayment.amount.toDouble() return NumberUtil.floor(earningRate, EARNING_RATE_DECIMAL_PLACE) } @@ -24,6 +24,6 @@ class LottoStatistics( } companion object { - private val EARNING_RATE_DECIMAL_PLACE = 2.0 + private const val EARNING_RATE_DECIMAL_PLACE = 2.0 } } diff --git a/src/main/kotlin/lotto/domain/Money.kt b/src/main/kotlin/lotto/domain/Money.kt new file mode 100644 index 0000000000..dea49b576f --- /dev/null +++ b/src/main/kotlin/lotto/domain/Money.kt @@ -0,0 +1,13 @@ +package lotto.domain + +data class Money( + val amount: Int +) { + fun isPositive(): Boolean { + return amount > 0 + } + + override fun toString(): String { + return "$amount" + } +} diff --git a/src/main/kotlin/lotto/domain/Payment.kt b/src/main/kotlin/lotto/domain/Payment.kt index 3c001dcaaa..fd86ab0255 100644 --- a/src/main/kotlin/lotto/domain/Payment.kt +++ b/src/main/kotlin/lotto/domain/Payment.kt @@ -1,14 +1,14 @@ package lotto.domain -class Payment( - var payment: Int +data class Payment( + var amount: Int ) { init { - require(payment >= 0) { "지불액은 음수가 될 수 없습니다." } + require(amount >= 0) { "지불액은 음수가 될 수 없습니다." } } - fun charge(charge: Int) { - require(payment >= charge) { "잔액 부족으로 금액을 차감할 수 없습니다." } - payment = payment.minus(charge) + fun charge(charge: Int): Payment { + require(amount >= charge) { "잔액 부족으로 금액을 차감할 수 없습니다." } + return Payment(amount.minus(charge)) } } diff --git a/src/test/kotlin/lotto/domain/MoneyTest.kt b/src/test/kotlin/lotto/domain/MoneyTest.kt new file mode 100644 index 0000000000..3605234f0e --- /dev/null +++ b/src/test/kotlin/lotto/domain/MoneyTest.kt @@ -0,0 +1,24 @@ +package lotto.domain + +import io.kotest.core.spec.style.StringSpec +import io.kotest.matchers.shouldBe + +class MoneyTest : StringSpec({ + "양수값을 가졌을때 isPositive 에서 true를 반환한다." { + // given + val integer = Money(1) + // when + val actual = integer.isPositive() + // then + actual shouldBe true + } + + "음수값을 가졌을때 isPositive 에서 false를 반환한다." { + // given + val integer = Money(-1) + // when + val actual = integer.isPositive() + // then + actual shouldBe false + } +}) diff --git a/src/test/kotlin/lotto/domain/PaymentTest.kt b/src/test/kotlin/lotto/domain/PaymentTest.kt index 5d3be66fa3..c953b5283a 100644 --- a/src/test/kotlin/lotto/domain/PaymentTest.kt +++ b/src/test/kotlin/lotto/domain/PaymentTest.kt @@ -18,7 +18,7 @@ class PaymentTest : StringSpec({ // when payment.charge(100) // then - payment.payment shouldBe 900 + payment.amount shouldBe 900 } "잔액 부족으로 지불금 차감 에러 테스트" { From 01d85e42863711f9eb4a31c9af5e41c219cd00f8 Mon Sep 17 00:00:00 2001 From: dajeong Date: Tue, 27 Dec 2022 12:31:05 +0900 Subject: [PATCH 21/21] =?UTF-8?q?[step4]=20=ED=94=BC=EB=93=9C=EB=B0=B1=20?= =?UTF-8?q?=EB=B0=98=EC=98=81=20-=20=EB=A1=9C=EB=98=90=20=EA=B5=AC?= =?UTF-8?q?=EB=A7=A4=20=EB=A1=9C=EC=A7=81=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit payment(지불금액)이 로또를 구매할 때 변경돼야하기 때문에 lottoShop에서 payment를 가지는 구조로 변경 --- src/main/kotlin/lotto/application/Application.kt | 9 +++++---- src/main/kotlin/lotto/domain/LottoShop.kt | 14 ++++++++++---- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/main/kotlin/lotto/application/Application.kt b/src/main/kotlin/lotto/application/Application.kt index dd5a7323dc..9520ed1f59 100644 --- a/src/main/kotlin/lotto/application/Application.kt +++ b/src/main/kotlin/lotto/application/Application.kt @@ -12,16 +12,17 @@ import lotto.view.ResultView class Application { private val inputView = InputView() private val resultView = ResultView() - private val lottoShop = LottoShop(LottoGenerator(RandomNumberGenerator())) + private val lottoGenerator = LottoGenerator(RandomNumberGenerator()) fun run() { val inputPayment = inputView.inputPayment() val manualLottoCount = inputView.inputManualLottoCount() val manualNumberList = inputView.inputManualLottoNumbers(manualLottoCount) - val usedPayment = inputPayment.copy() - val manualLottoList = lottoShop.buyManualLotto(usedPayment, manualNumberList) - val autoLottoList = lottoShop.buyAutoLotto(usedPayment) + val lottoShop = LottoShop(lottoGenerator, inputPayment) + + val manualLottoList = lottoShop.buyManualLotto(manualNumberList) + val autoLottoList = lottoShop.buyAutoLotto() resultView.printLottoList(manualLottoList, autoLottoList) val lottoList = manualLottoList + autoLottoList diff --git a/src/main/kotlin/lotto/domain/LottoShop.kt b/src/main/kotlin/lotto/domain/LottoShop.kt index 98e30daeeb..1d3b747979 100644 --- a/src/main/kotlin/lotto/domain/LottoShop.kt +++ b/src/main/kotlin/lotto/domain/LottoShop.kt @@ -3,20 +3,22 @@ package lotto.domain import lotto.common.IntegerNumberList class LottoShop( - private val lottoGenerator: LottoGenerator + private val lottoGenerator: LottoGenerator, + private var payment: Payment ) { - fun buyAutoLotto(payment: Payment): List { + fun buyAutoLotto(): List { val totalLottoCount = calculateLottoCount(payment) return lottoGenerator.generate(totalLottoCount) } - fun buyManualLotto(payment: Payment, manualNumberList: List): List { + fun buyManualLotto(manualNumberList: List): List { val totalLottoCount = calculateLottoCount(payment) val manualLottoCount = manualNumberList.size require(manualLottoCount <= totalLottoCount) { "수동 로또 개수가 지불 금액보다 많아 로또를 구입할 수 없습니다." } - payment.charge(manualLottoCount * LOTTO_PRICE) + val change = payment.charge(manualLottoCount * LOTTO_PRICE) + updatePayment(change) return lottoGenerator.generate(manualNumberList) } @@ -24,6 +26,10 @@ class LottoShop( return payment.amount / LOTTO_PRICE } + private fun updatePayment(newPayment: Payment) { + this.payment = newPayment + } + companion object { private const val LOTTO_PRICE = 1000 }