From 287d4576c4b4b1177f4e72afd28b5288327c6978 Mon Sep 17 00:00:00 2001 From: ChoiYounho Date: Thu, 24 Nov 2022 19:01:07 +0900 Subject: [PATCH 01/74] =?UTF-8?q?docs=20:=20=EB=A6=AC=EB=93=9C=EB=AF=B8=20?= =?UTF-8?q?=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 20 +++++++++++++++++++- local.properties | 8 ++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 local.properties diff --git a/README.md b/README.md index cbae739405..0013f56e87 100644 --- a/README.md +++ b/README.md @@ -1 +1,19 @@ -# kotlin-lotto \ No newline at end of file +# kotlin-lotto + +### 요구사항 +1. 쉼표(,) 또는 콜론(:)을 구분자로 가지는 문자열을 전달하는 경우 구분자를 기준으로 분리한 각 숫자의 합을 반환 (예: “” => 0, "1,2" => 3, "1,2,3" => 6, “1,2:3” => 6) +2. 앞의 기본 구분자(쉼표, 콜론) 외에 커스텀 구분자를 지정할 수 있다. 커스텀 구분자는 문자열 앞부분의 “//”와 “\n” 사이에 위치하는 문자를 커스텀 구분자로 사용한다. +예를 들어 “//;\n1;2;3”과 같이 값을 입력할 경우 커스텀 구분자는 세미콜론(;)이며, 결과 값은 6이 반환되어야 한다. +3. 문자열 계산기에 숫자 이외의 값 또는 음수를 전달하는 경우 RuntimeException 예외를 throw 한다. + + + +1번은 split를 이용해서 변환한다. +- 매직 리터럴 사용 주의 +- 공백은 없으니 0 + +2번은 //와 \n 사이의 문자는 커스텀 가능하게 해야한다. +- 리스트에 , 와 : 를 미리 넣어두고, 커스텀 가능한 문자를 확인할 시 리스트에 추가한다. +- 이 또한 매직 리터럴 사용주의 + +3. 숫자나 커스텀 가능한 문자, 혹은 음수 전달하면 예외 \ No newline at end of file diff --git a/local.properties b/local.properties new file mode 100644 index 0000000000..ad02abe3e0 --- /dev/null +++ b/local.properties @@ -0,0 +1,8 @@ +## This file must *NOT* be checked into Version Control Systems, +# as it contains information specific to your local configuration. +# +# Location of the SDK. This is only used by Gradle. +# For customization when using a Version Control System, please read the +# header note. +#Thu Nov 24 18:53:46 KST 2022 +sdk.dir=/Users/newlink/Library/Android/sdk From 0301978c1b1a48bf7410a19318bf3138abc145fd Mon Sep 17 00:00:00 2001 From: ChoiYounho Date: Thu, 24 Nov 2022 19:19:16 +0900 Subject: [PATCH 02/74] =?UTF-8?q?feat=20:=20=EB=AC=B8=EC=9E=90=EC=97=B4=20?= =?UTF-8?q?=EB=B6=84=EB=A6=AC=20=EC=A0=84=EB=9E=B5=20=EA=B8=B0=EC=B4=88=20?= =?UTF-8?q?=EC=84=A4=EA=B3=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/SplitStranegy.kt | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 src/main/kotlin/SplitStranegy.kt diff --git a/src/main/kotlin/SplitStranegy.kt b/src/main/kotlin/SplitStranegy.kt new file mode 100644 index 0000000000..d12dcd9371 --- /dev/null +++ b/src/main/kotlin/SplitStranegy.kt @@ -0,0 +1,25 @@ +enum class ExpressionSplitStrategy( + val split: (Expression) -> List +) { + + BASIC({ expression -> + val expressionReplace = expression.value + .replace(SPLIT_TERMS_COLONS, SPLIT_RESULT) + .replace(SPLIT_TERMS_COMMA, SPLIT_RESULT) + + expressionReplace.split(SPLIT_TERMS_EMPTY) + }), + CUSTOM({ + + listOf() + }); + + companion object { + private const val SPLIT_TERMS_COLONS = ":" + private const val SPLIT_TERMS_EMPTY = "" + private const val SPLIT_TERMS_COMMA = "," + + private const val SPLIT_RESULT = "" + } + +} \ No newline at end of file From a6cdcb2471a65af4a961f2e5b55f3ed77c745dec Mon Sep 17 00:00:00 2001 From: ChoiYounho Date: Thu, 24 Nov 2022 19:19:42 +0900 Subject: [PATCH 03/74] =?UTF-8?q?feat=20:=20Expression=20=EC=88=98?= =?UTF-8?q?=EB=8F=84=20=EC=BD=94=EB=93=9C=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/Expression.kt | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 src/main/kotlin/Expression.kt diff --git a/src/main/kotlin/Expression.kt b/src/main/kotlin/Expression.kt new file mode 100644 index 0000000000..e85dd81dd6 --- /dev/null +++ b/src/main/kotlin/Expression.kt @@ -0,0 +1,4 @@ +class Expression( + val value: String +) { +} \ No newline at end of file From 80ae9025beda15c675e5dd62505c57988ec33667 Mon Sep 17 00:00:00 2001 From: Choiyounho Date: Sun, 27 Nov 2022 20:02:59 +0900 Subject: [PATCH 04/74] =?UTF-8?q?refactor=20:=20=EC=88=98=EC=8B=9D=20?= =?UTF-8?q?=EB=B0=94=EA=BE=B8=EA=B8=B0=20=EC=A0=84=EB=9E=B5=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/SplitStranegy.kt | 25 ------------------- .../stranegy/ExpressionReplaceStrategy.kt | 24 ++++++++++++++++++ 2 files changed, 24 insertions(+), 25 deletions(-) delete mode 100644 src/main/kotlin/SplitStranegy.kt create mode 100644 src/main/kotlin/calculator/stranegy/ExpressionReplaceStrategy.kt diff --git a/src/main/kotlin/SplitStranegy.kt b/src/main/kotlin/SplitStranegy.kt deleted file mode 100644 index d12dcd9371..0000000000 --- a/src/main/kotlin/SplitStranegy.kt +++ /dev/null @@ -1,25 +0,0 @@ -enum class ExpressionSplitStrategy( - val split: (Expression) -> List -) { - - BASIC({ expression -> - val expressionReplace = expression.value - .replace(SPLIT_TERMS_COLONS, SPLIT_RESULT) - .replace(SPLIT_TERMS_COMMA, SPLIT_RESULT) - - expressionReplace.split(SPLIT_TERMS_EMPTY) - }), - CUSTOM({ - - listOf() - }); - - companion object { - private const val SPLIT_TERMS_COLONS = ":" - private const val SPLIT_TERMS_EMPTY = "" - private const val SPLIT_TERMS_COMMA = "," - - private const val SPLIT_RESULT = "" - } - -} \ No newline at end of file diff --git a/src/main/kotlin/calculator/stranegy/ExpressionReplaceStrategy.kt b/src/main/kotlin/calculator/stranegy/ExpressionReplaceStrategy.kt new file mode 100644 index 0000000000..e1422f79c6 --- /dev/null +++ b/src/main/kotlin/calculator/stranegy/ExpressionReplaceStrategy.kt @@ -0,0 +1,24 @@ +package calculator.stranegy + +import calculator.domain.Expression +import calculator.domain.SplitTerms + +enum class ExpressionReplaceStrategy( + val split: (Expression, SplitTerms) -> Expression +) { + + CALCULATOR_PLUS({ expression, splitTerms -> + var expressionReplace = expression.value + + splitTerms.value.forEach { terms -> + expressionReplace = expressionReplace.replace(terms, SPLIT_RESULT) + } + + Expression(expressionReplace) + }); + + companion object { + private const val SPLIT_RESULT = "" + } + +} \ No newline at end of file From 78b6536c3f8176abac6d82fa8b318a77b1f1ad3b Mon Sep 17 00:00:00 2001 From: Choiyounho Date: Sun, 27 Nov 2022 20:03:32 +0900 Subject: [PATCH 05/74] =?UTF-8?q?feat=20:=20=EC=88=98=EC=8B=9D=20=EA=B8=B0?= =?UTF-8?q?=EB=B3=B8=20=ED=8B=80=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- local.properties | 4 ++-- src/main/kotlin/Expression.kt | 4 ---- src/main/kotlin/calculator/domain/Expression.kt | 11 +++++++++++ 3 files changed, 13 insertions(+), 6 deletions(-) delete mode 100644 src/main/kotlin/Expression.kt create mode 100644 src/main/kotlin/calculator/domain/Expression.kt diff --git a/local.properties b/local.properties index ad02abe3e0..e31792997f 100644 --- a/local.properties +++ b/local.properties @@ -4,5 +4,5 @@ # Location of the SDK. This is only used by Gradle. # For customization when using a Version Control System, please read the # header note. -#Thu Nov 24 18:53:46 KST 2022 -sdk.dir=/Users/newlink/Library/Android/sdk +#Sun Nov 27 18:11:28 KST 2022 +sdk.dir=/Users/choiyounho/Library/Android/sdk diff --git a/src/main/kotlin/Expression.kt b/src/main/kotlin/Expression.kt deleted file mode 100644 index e85dd81dd6..0000000000 --- a/src/main/kotlin/Expression.kt +++ /dev/null @@ -1,4 +0,0 @@ -class Expression( - val value: String -) { -} \ No newline at end of file diff --git a/src/main/kotlin/calculator/domain/Expression.kt b/src/main/kotlin/calculator/domain/Expression.kt new file mode 100644 index 0000000000..dbd57834d2 --- /dev/null +++ b/src/main/kotlin/calculator/domain/Expression.kt @@ -0,0 +1,11 @@ +package calculator.domain + +class Expression( + val value: String = DEFAULT_EXPRESSION_VALUE +) { + + companion object { + private const val DEFAULT_EXPRESSION_VALUE = "" + } + +} \ No newline at end of file From 93865380423676916c758bfaf279156a2a449f13 Mon Sep 17 00:00:00 2001 From: Choiyounho Date: Sun, 27 Nov 2022 20:03:56 +0900 Subject: [PATCH 06/74] =?UTF-8?q?feat=20:=20=EC=8A=A4=ED=94=8C=EB=A6=BF=20?= =?UTF-8?q?=EC=A1=B0=EA=B1=B4=20=ED=81=B4=EB=9E=98=EC=8A=A4=20=EA=B8=B0?= =?UTF-8?q?=EB=B3=B8=20=ED=8B=80=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/calculator/domain/SplitTerms.kt | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 src/main/kotlin/calculator/domain/SplitTerms.kt diff --git a/src/main/kotlin/calculator/domain/SplitTerms.kt b/src/main/kotlin/calculator/domain/SplitTerms.kt new file mode 100644 index 0000000000..6c2865a295 --- /dev/null +++ b/src/main/kotlin/calculator/domain/SplitTerms.kt @@ -0,0 +1,12 @@ +package calculator.domain + +class SplitTerms( + val value: List = listOf(TERMS_COMMA, TERMS_COLONS) +) { + + companion object { + private const val TERMS_COMMA = "," + private const val TERMS_COLONS = ":" + } + +} \ No newline at end of file From c9c948df0342d379e2c0d13fbdb095ec718c24ad Mon Sep 17 00:00:00 2001 From: Choiyounho Date: Sun, 27 Nov 2022 20:04:08 +0900 Subject: [PATCH 07/74] =?UTF-8?q?feat=20:=20=EC=8A=A4=ED=94=8C=EB=A6=BF=20?= =?UTF-8?q?=EC=9C=A0=ED=8B=B8=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/calculator/util/SplitUtil.kt | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 src/main/kotlin/calculator/util/SplitUtil.kt diff --git a/src/main/kotlin/calculator/util/SplitUtil.kt b/src/main/kotlin/calculator/util/SplitUtil.kt new file mode 100644 index 0000000000..b8027de98a --- /dev/null +++ b/src/main/kotlin/calculator/util/SplitUtil.kt @@ -0,0 +1,13 @@ +package calculator.util + +import calculator.domain.Expression + +object SplitUtil { + + private const val SPLIT_DEFAULT = "" + + fun splitExpressionToInts(expression: Expression): List { + return expression.value.split(SPLIT_DEFAULT).map { it.toInt() } + } + +} \ No newline at end of file From 9dad6317eb24be97bad48b8db4d7d9eaf1183439 Mon Sep 17 00:00:00 2001 From: Choiyounho Date: Sun, 27 Nov 2022 20:04:18 +0900 Subject: [PATCH 08/74] =?UTF-8?q?feat=20:=20=EA=B3=84=EC=82=B0=EA=B8=B0=20?= =?UTF-8?q?=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/calculator/domain/Calculator.kt | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 src/main/kotlin/calculator/domain/Calculator.kt diff --git a/src/main/kotlin/calculator/domain/Calculator.kt b/src/main/kotlin/calculator/domain/Calculator.kt new file mode 100644 index 0000000000..59bdfe88f4 --- /dev/null +++ b/src/main/kotlin/calculator/domain/Calculator.kt @@ -0,0 +1,17 @@ +package calculator.domain + +import calculator.util.SplitUtil.splitExpressionToInts + +class Calculator(private val expression: Expression) { + + fun calculate(): Int { + var result = 0 + + splitExpressionToInts(expression).forEach { number -> + result = result.plus(number) + } + + return result + } + +} \ No newline at end of file From b0e1d45a19e6c0fc881e6f04a082a52f932d3dc0 Mon Sep 17 00:00:00 2001 From: Choiyounho Date: Sun, 27 Nov 2022 20:49:35 +0900 Subject: [PATCH 09/74] =?UTF-8?q?refactor=20:=20=EC=88=98=EC=8B=9D=20?= =?UTF-8?q?=EC=83=9D=EC=84=B1=20=EC=A0=84=EB=9E=B5=20=EB=A6=AC=ED=8C=A9?= =?UTF-8?q?=ED=86=A0=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...ionReplaceStrategy.kt => ExpressionCreateStrategy.kt} | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) rename src/main/kotlin/calculator/stranegy/{ExpressionReplaceStrategy.kt => ExpressionCreateStrategy.kt} (60%) diff --git a/src/main/kotlin/calculator/stranegy/ExpressionReplaceStrategy.kt b/src/main/kotlin/calculator/stranegy/ExpressionCreateStrategy.kt similarity index 60% rename from src/main/kotlin/calculator/stranegy/ExpressionReplaceStrategy.kt rename to src/main/kotlin/calculator/stranegy/ExpressionCreateStrategy.kt index e1422f79c6..fbcc04ed8a 100644 --- a/src/main/kotlin/calculator/stranegy/ExpressionReplaceStrategy.kt +++ b/src/main/kotlin/calculator/stranegy/ExpressionCreateStrategy.kt @@ -2,13 +2,14 @@ package calculator.stranegy import calculator.domain.Expression import calculator.domain.SplitTerms +import calculator.extensions.removeCustomRegex -enum class ExpressionReplaceStrategy( - val split: (Expression, SplitTerms) -> Expression +enum class ExpressionCreateStrategy( + val create: (String, SplitTerms) -> Expression ) { - CALCULATOR_PLUS({ expression, splitTerms -> - var expressionReplace = expression.value + CALCULATOR_PLUS({ input, splitTerms -> + var expressionReplace = input.removeCustomRegex() splitTerms.value.forEach { terms -> expressionReplace = expressionReplace.replace(terms, SPLIT_RESULT) From 5de9f481c26a4b70da2e03efc84bece4826ce38d Mon Sep 17 00:00:00 2001 From: Choiyounho Date: Sun, 27 Nov 2022 20:49:47 +0900 Subject: [PATCH 10/74] =?UTF-8?q?feat=20:=20=EC=83=81=EC=88=98=20=EC=BD=94?= =?UTF-8?q?=ED=8B=80=EB=A6=B0=20=ED=8C=8C=EC=9D=BC=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/calculator/Constants.kt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 src/main/kotlin/calculator/Constants.kt diff --git a/src/main/kotlin/calculator/Constants.kt b/src/main/kotlin/calculator/Constants.kt new file mode 100644 index 0000000000..dd55b79112 --- /dev/null +++ b/src/main/kotlin/calculator/Constants.kt @@ -0,0 +1,3 @@ +package calculator + +const val EMPTY_STRING = "" From c2ddf8edd8d44e40bb80b9b7849bb7741d89b446 Mon Sep 17 00:00:00 2001 From: Choiyounho Date: Sun, 27 Nov 2022 20:50:33 +0900 Subject: [PATCH 11/74] =?UTF-8?q?feat=20:=20=EC=BB=A4=EC=8A=A4=ED=85=80=20?= =?UTF-8?q?=EC=A0=95=EA=B7=9C=EC=8B=9D=20=EB=AC=B8=EC=9E=90=20=EC=A0=9C?= =?UTF-8?q?=EA=B1=B0=20=ED=99=95=EC=9E=A5=ED=95=A8=EC=88=98=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/calculator/extensions/StringExtension.kt | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 src/main/kotlin/calculator/extensions/StringExtension.kt diff --git a/src/main/kotlin/calculator/extensions/StringExtension.kt b/src/main/kotlin/calculator/extensions/StringExtension.kt new file mode 100644 index 0000000000..2ea2910ccb --- /dev/null +++ b/src/main/kotlin/calculator/extensions/StringExtension.kt @@ -0,0 +1,7 @@ +package calculator.extensions + + +fun String.removeCustomRegex(): String { + return replace("//", "").replace("\n", "") +} + From 2cf2fb7c13993e1003bd924540b5d372a47f268c Mon Sep 17 00:00:00 2001 From: Choiyounho Date: Sun, 27 Nov 2022 20:50:51 +0900 Subject: [PATCH 12/74] =?UTF-8?q?feat=20:=20SplitTerms=20=ED=8C=A9?= =?UTF-8?q?=ED=86=A0=EB=A6=AC=20=ED=95=A8=EC=88=98=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/calculator/domain/SplitTerms.kt | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/main/kotlin/calculator/domain/SplitTerms.kt b/src/main/kotlin/calculator/domain/SplitTerms.kt index 6c2865a295..e7aeda358e 100644 --- a/src/main/kotlin/calculator/domain/SplitTerms.kt +++ b/src/main/kotlin/calculator/domain/SplitTerms.kt @@ -5,6 +5,19 @@ class SplitTerms( ) { companion object { + fun create(input: String): SplitTerms { + val terms = Regex("//(.)\n(.*)").find(input)?.run { + groupValues[1] + } + + println("create $terms") + return if (terms != null) { + SplitTerms(listOf(TERMS_COMMA, TERMS_COLONS) + terms) + } else { + SplitTerms() + } + } + private const val TERMS_COMMA = "," private const val TERMS_COLONS = ":" } From bf307954c23fe9b50857aef07536cfe8a2caf9ec Mon Sep 17 00:00:00 2001 From: Choiyounho Date: Sun, 27 Nov 2022 20:51:23 +0900 Subject: [PATCH 13/74] =?UTF-8?q?refactor=20:=20SplitUtil=20=EB=B9=88?= =?UTF-8?q?=EB=AC=B8=EC=9E=90=EC=9D=BC=20=EA=B2=BD=EC=9A=B0=200=EC=9C=BC?= =?UTF-8?q?=EB=A1=9C=20=EB=B3=80=ED=99=98=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/calculator/util/SplitUtil.kt | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/calculator/util/SplitUtil.kt b/src/main/kotlin/calculator/util/SplitUtil.kt index b8027de98a..3cb60acc12 100644 --- a/src/main/kotlin/calculator/util/SplitUtil.kt +++ b/src/main/kotlin/calculator/util/SplitUtil.kt @@ -1,5 +1,6 @@ package calculator.util +import calculator.EMPTY_STRING import calculator.domain.Expression object SplitUtil { @@ -7,7 +8,10 @@ object SplitUtil { private const val SPLIT_DEFAULT = "" fun splitExpressionToInts(expression: Expression): List { - return expression.value.split(SPLIT_DEFAULT).map { it.toInt() } + return expression.value.split(SPLIT_DEFAULT).map { input -> + if (input == EMPTY_STRING) 0 + else input.toInt() + } } } \ No newline at end of file From 9db90e16739544f169c5cd2913471f430ce2ff5d Mon Sep 17 00:00:00 2001 From: Choiyounho Date: Sun, 27 Nov 2022 20:51:41 +0900 Subject: [PATCH 14/74] =?UTF-8?q?test=20:=20=EC=88=98=EC=8B=9D=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8=20=EC=BD=94=EB=93=9C=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../calculator/domain/ExpressionTest.kt | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 src/test/kotlin/calculator/domain/ExpressionTest.kt diff --git a/src/test/kotlin/calculator/domain/ExpressionTest.kt b/src/test/kotlin/calculator/domain/ExpressionTest.kt new file mode 100644 index 0000000000..6d730ec83a --- /dev/null +++ b/src/test/kotlin/calculator/domain/ExpressionTest.kt @@ -0,0 +1,22 @@ +package calculator.domain + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test + +internal class ExpressionTest { + + @Test + fun `수식 클래스 생성하여 파라미터 값 비교`() { + // given + val value = "1,2:3" + + // when + val expression = Expression(value) + val actual = expression.value == value + + // then + assertThat(actual).isTrue + } + + +} \ No newline at end of file From 605a085ec74a0ba75d6586172ad9c93b6df6733c Mon Sep 17 00:00:00 2001 From: Choiyounho Date: Sun, 27 Nov 2022 22:56:49 +0900 Subject: [PATCH 15/74] =?UTF-8?q?test=20:=20=EC=88=98=EC=8B=9D=20=EC=83=9D?= =?UTF-8?q?=EC=84=B1=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BD=94=EB=93=9C=20?= =?UTF-8?q?=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kotlin/calculator/domain/SplitTerms.kt | 1 - .../calculator/domain/SplitTermsTest.kt | 21 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 src/test/kotlin/calculator/domain/SplitTermsTest.kt diff --git a/src/main/kotlin/calculator/domain/SplitTerms.kt b/src/main/kotlin/calculator/domain/SplitTerms.kt index e7aeda358e..be081015a3 100644 --- a/src/main/kotlin/calculator/domain/SplitTerms.kt +++ b/src/main/kotlin/calculator/domain/SplitTerms.kt @@ -10,7 +10,6 @@ class SplitTerms( groupValues[1] } - println("create $terms") return if (terms != null) { SplitTerms(listOf(TERMS_COMMA, TERMS_COLONS) + terms) } else { diff --git a/src/test/kotlin/calculator/domain/SplitTermsTest.kt b/src/test/kotlin/calculator/domain/SplitTermsTest.kt new file mode 100644 index 0000000000..b6183ffad1 --- /dev/null +++ b/src/test/kotlin/calculator/domain/SplitTermsTest.kt @@ -0,0 +1,21 @@ +package calculator.domain + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test + +internal class SplitTermsTest { + + @Test + fun `입력값을 이용하여 문자열 자르기 조건을 만든다`() { + // given + val input = "//;\n1;2;3" + + // when + val actual = SplitTerms.create(input).value + + // then + val result = listOf(",", ":", ";") + assertThat(actual).isEqualTo(result) + } + +} \ No newline at end of file From 12e1d0b73a5ca593be0fca3a0406a43068cb42c7 Mon Sep 17 00:00:00 2001 From: Choiyounho Date: Sun, 27 Nov 2022 23:05:16 +0900 Subject: [PATCH 16/74] =?UTF-8?q?feat=20:=20=EC=88=98=EC=8B=9D=EC=9D=84=20?= =?UTF-8?q?=EC=83=9D=EC=84=B1=ED=95=A0=20=EB=95=8C=20=EC=88=AB=EC=9E=90?= =?UTF-8?q?=EB=A7=8C=20=EC=9E=88=EB=8A=94=20=EA=B2=80=EC=A6=9D=20=EB=A1=9C?= =?UTF-8?q?=EC=A7=81=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/calculator/domain/Expression.kt | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/main/kotlin/calculator/domain/Expression.kt b/src/main/kotlin/calculator/domain/Expression.kt index dbd57834d2..7e9e03576b 100644 --- a/src/main/kotlin/calculator/domain/Expression.kt +++ b/src/main/kotlin/calculator/domain/Expression.kt @@ -4,6 +4,17 @@ class Expression( val value: String = DEFAULT_EXPRESSION_VALUE ) { + init { + validateExpression() + } + + private fun validateExpression() { + value.forEach { + runCatching { it.digitToInt() } + .onFailure { throw IllegalArgumentException("숫자 외에 다른 문자는 들어올 수 없습니다.") } + } + } + companion object { private const val DEFAULT_EXPRESSION_VALUE = "" } From f45cb69437e62a44d7d8bb4c2694b31ef4fcd97d Mon Sep 17 00:00:00 2001 From: Choiyounho Date: Sun, 27 Nov 2022 23:06:22 +0900 Subject: [PATCH 17/74] =?UTF-8?q?test=20:=20=EC=88=98=EC=8B=9D=20=EC=83=9D?= =?UTF-8?q?=EC=84=B1=20=EC=A0=84=EB=9E=B5=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20?= =?UTF-8?q?=EC=BD=94=EB=93=9C=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../stranegy/ExpressionCreateStrategyTest.kt | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 src/test/kotlin/calculator/stranegy/ExpressionCreateStrategyTest.kt diff --git a/src/test/kotlin/calculator/stranegy/ExpressionCreateStrategyTest.kt b/src/test/kotlin/calculator/stranegy/ExpressionCreateStrategyTest.kt new file mode 100644 index 0000000000..fb385aeaf7 --- /dev/null +++ b/src/test/kotlin/calculator/stranegy/ExpressionCreateStrategyTest.kt @@ -0,0 +1,33 @@ +package calculator.stranegy + +import calculator.domain.Expression +import calculator.domain.SplitTerms +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.assertThrows + +internal class ExpressionCreateStrategyTest { + + @Test + fun `문자열 구분자를 제거하여 숫자만 있는 수식 만들기`() { + // given + val input = "//;\n1;2;3" + val splitTerms = SplitTerms.create(input) + + // when + val expression = ExpressionCreateStrategy.CALCULATOR_PLUS.create(input, splitTerms) + val actual = expression.value + + // then + val expected = "123" + assertThat(actual).isEqualTo(expected) + } + + @Test + fun `문자열 구분자 외 다른 문자가 포함되어 있어서 숫자만 있는 수식 생성 실패`() { + assertThrows { + Expression("1j23") + } + } + +} \ No newline at end of file From 1264c10d5abc96f3e05e5fedad47d5546f16d74e Mon Sep 17 00:00:00 2001 From: Choiyounho Date: Sun, 27 Nov 2022 23:17:14 +0900 Subject: [PATCH 18/74] =?UTF-8?q?refactor=20:=20=EC=88=98=EC=8B=9D=20?= =?UTF-8?q?=ED=85=8C=EC=8A=A4=ED=8A=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/test/kotlin/calculator/domain/ExpressionTest.kt | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/test/kotlin/calculator/domain/ExpressionTest.kt b/src/test/kotlin/calculator/domain/ExpressionTest.kt index 6d730ec83a..3739af6cf8 100644 --- a/src/test/kotlin/calculator/domain/ExpressionTest.kt +++ b/src/test/kotlin/calculator/domain/ExpressionTest.kt @@ -2,13 +2,14 @@ package calculator.domain import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.Test +import org.junit.jupiter.api.assertThrows internal class ExpressionTest { @Test - fun `수식 클래스 생성하여 파라미터 값 비교`() { + fun `숫자만 있는 입력값을 이용하여 수식 생성하여 프로퍼티 비교`() { // given - val value = "1,2:3" + val value = "123" // when val expression = Expression(value) @@ -18,5 +19,10 @@ internal class ExpressionTest { assertThat(actual).isTrue } - + @Test + fun `숫자 외에 다른 문자가 포함되어 있어 생성 실패`() { + assertThrows { + Expression("1;,2:34") + } + } } \ No newline at end of file From a2b890d1b23d4f2ee93071823453b717957986ba Mon Sep 17 00:00:00 2001 From: Choiyounho Date: Sun, 27 Nov 2022 23:17:37 +0900 Subject: [PATCH 19/74] =?UTF-8?q?test=20:=20=EA=B3=84=EC=82=B0=EA=B8=B0=20?= =?UTF-8?q?=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BD=94=EB=93=9C=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 --- .../calculator/domain/CalculatorTest.kt | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 src/test/kotlin/calculator/domain/CalculatorTest.kt diff --git a/src/test/kotlin/calculator/domain/CalculatorTest.kt b/src/test/kotlin/calculator/domain/CalculatorTest.kt new file mode 100644 index 0000000000..61df9f99fd --- /dev/null +++ b/src/test/kotlin/calculator/domain/CalculatorTest.kt @@ -0,0 +1,22 @@ +package calculator.domain + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test + +internal class CalculatorTest { + + @Test + fun `수식을 이용하여 계산에 성공한다`() { + // given + val expression = Expression("123") + val calculator = Calculator(expression) + + // when + val actual = calculator.calculate() + + // then + val expected = 6 + assertThat(actual).isEqualTo(expected) + } + +} \ No newline at end of file From 2377f2c6257712067d14e3f072751e9bad6081ed Mon Sep 17 00:00:00 2001 From: Choiyounho Date: Sun, 27 Nov 2022 23:19:51 +0900 Subject: [PATCH 20/74] =?UTF-8?q?refactor=20:=20=EC=88=98=EC=8B=9D=20?= =?UTF-8?q?=EC=83=9D=EC=84=B1=EC=A0=84=EB=9E=B5=EC=9D=B4=20=EC=9E=88?= =?UTF-8?q?=EC=96=B4=20=EC=88=98=EC=8B=9D=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20?= =?UTF-8?q?=EC=BD=94=EB=93=9C=20=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kotlin/calculator/domain/Expression.kt | 2 +- .../calculator/domain/ExpressionTest.kt | 28 ------------------- .../stranegy/ExpressionCreateStrategyTest.kt | 13 +++++++-- 3 files changed, 12 insertions(+), 31 deletions(-) delete mode 100644 src/test/kotlin/calculator/domain/ExpressionTest.kt diff --git a/src/main/kotlin/calculator/domain/Expression.kt b/src/main/kotlin/calculator/domain/Expression.kt index 7e9e03576b..954db58ba9 100644 --- a/src/main/kotlin/calculator/domain/Expression.kt +++ b/src/main/kotlin/calculator/domain/Expression.kt @@ -1,6 +1,6 @@ package calculator.domain -class Expression( +class Expression ( val value: String = DEFAULT_EXPRESSION_VALUE ) { diff --git a/src/test/kotlin/calculator/domain/ExpressionTest.kt b/src/test/kotlin/calculator/domain/ExpressionTest.kt deleted file mode 100644 index 3739af6cf8..0000000000 --- a/src/test/kotlin/calculator/domain/ExpressionTest.kt +++ /dev/null @@ -1,28 +0,0 @@ -package calculator.domain - -import org.assertj.core.api.Assertions.assertThat -import org.junit.jupiter.api.Test -import org.junit.jupiter.api.assertThrows - -internal class ExpressionTest { - - @Test - fun `숫자만 있는 입력값을 이용하여 수식 생성하여 프로퍼티 비교`() { - // given - val value = "123" - - // when - val expression = Expression(value) - val actual = expression.value == value - - // then - assertThat(actual).isTrue - } - - @Test - fun `숫자 외에 다른 문자가 포함되어 있어 생성 실패`() { - assertThrows { - Expression("1;,2:34") - } - } -} \ No newline at end of file diff --git a/src/test/kotlin/calculator/stranegy/ExpressionCreateStrategyTest.kt b/src/test/kotlin/calculator/stranegy/ExpressionCreateStrategyTest.kt index fb385aeaf7..acfeb7915b 100644 --- a/src/test/kotlin/calculator/stranegy/ExpressionCreateStrategyTest.kt +++ b/src/test/kotlin/calculator/stranegy/ExpressionCreateStrategyTest.kt @@ -1,6 +1,5 @@ package calculator.stranegy -import calculator.domain.Expression import calculator.domain.SplitTerms import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.Test @@ -26,7 +25,17 @@ internal class ExpressionCreateStrategyTest { @Test fun `문자열 구분자 외 다른 문자가 포함되어 있어서 숫자만 있는 수식 생성 실패`() { assertThrows { - Expression("1j23") + // given + val input = "//;\n1;2;3dasd" + val splitTerms = SplitTerms.create(input) + + // when + val expression = ExpressionCreateStrategy.CALCULATOR_PLUS.create(input, splitTerms) + val actual = expression.value + + // then + val expected = "123" + assertThat(actual).isEqualTo(expected) } } From 44451cbd5d705b0c2b4cf675c9e0d15a79308fc8 Mon Sep 17 00:00:00 2001 From: Choiyounho Date: Sun, 27 Nov 2022 23:20:46 +0900 Subject: [PATCH 21/74] =?UTF-8?q?refactor=20:=20=EC=88=98=EC=8B=9D=20?= =?UTF-8?q?=EC=83=9D=EC=84=B1=20=EC=A0=84=EB=9E=B5=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EC=BD=94=EB=93=9C=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/calculator/domain/Expression.kt | 2 +- .../calculator/stranegy/ExpressionCreateStrategyTest.kt | 9 +-------- 2 files changed, 2 insertions(+), 9 deletions(-) diff --git a/src/main/kotlin/calculator/domain/Expression.kt b/src/main/kotlin/calculator/domain/Expression.kt index 954db58ba9..7e9e03576b 100644 --- a/src/main/kotlin/calculator/domain/Expression.kt +++ b/src/main/kotlin/calculator/domain/Expression.kt @@ -1,6 +1,6 @@ package calculator.domain -class Expression ( +class Expression( val value: String = DEFAULT_EXPRESSION_VALUE ) { diff --git a/src/test/kotlin/calculator/stranegy/ExpressionCreateStrategyTest.kt b/src/test/kotlin/calculator/stranegy/ExpressionCreateStrategyTest.kt index acfeb7915b..2b00b8e4d4 100644 --- a/src/test/kotlin/calculator/stranegy/ExpressionCreateStrategyTest.kt +++ b/src/test/kotlin/calculator/stranegy/ExpressionCreateStrategyTest.kt @@ -25,17 +25,10 @@ internal class ExpressionCreateStrategyTest { @Test fun `문자열 구분자 외 다른 문자가 포함되어 있어서 숫자만 있는 수식 생성 실패`() { assertThrows { - // given val input = "//;\n1;2;3dasd" val splitTerms = SplitTerms.create(input) - // when - val expression = ExpressionCreateStrategy.CALCULATOR_PLUS.create(input, splitTerms) - val actual = expression.value - - // then - val expected = "123" - assertThat(actual).isEqualTo(expected) + ExpressionCreateStrategy.CALCULATOR_PLUS.create(input, splitTerms) } } From 1121e918ac481cec708fc4f63763f7e66ebd507d Mon Sep 17 00:00:00 2001 From: Choiyounho Date: Mon, 5 Dec 2022 00:53:38 +0900 Subject: [PATCH 22/74] =?UTF-8?q?refactor=20:=20=EC=88=98=EC=8B=9D=20?= =?UTF-8?q?=EC=83=9D=EC=84=B1=20=EC=A0=84=EB=9E=B5=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../stranegy/ExpressionCreateStrategy.kt | 25 ------------- .../stranegy/ExpressionCreateStrategyTest.kt | 35 ------------------- 2 files changed, 60 deletions(-) delete mode 100644 src/main/kotlin/calculator/stranegy/ExpressionCreateStrategy.kt delete mode 100644 src/test/kotlin/calculator/stranegy/ExpressionCreateStrategyTest.kt diff --git a/src/main/kotlin/calculator/stranegy/ExpressionCreateStrategy.kt b/src/main/kotlin/calculator/stranegy/ExpressionCreateStrategy.kt deleted file mode 100644 index fbcc04ed8a..0000000000 --- a/src/main/kotlin/calculator/stranegy/ExpressionCreateStrategy.kt +++ /dev/null @@ -1,25 +0,0 @@ -package calculator.stranegy - -import calculator.domain.Expression -import calculator.domain.SplitTerms -import calculator.extensions.removeCustomRegex - -enum class ExpressionCreateStrategy( - val create: (String, SplitTerms) -> Expression -) { - - CALCULATOR_PLUS({ input, splitTerms -> - var expressionReplace = input.removeCustomRegex() - - splitTerms.value.forEach { terms -> - expressionReplace = expressionReplace.replace(terms, SPLIT_RESULT) - } - - Expression(expressionReplace) - }); - - companion object { - private const val SPLIT_RESULT = "" - } - -} \ No newline at end of file diff --git a/src/test/kotlin/calculator/stranegy/ExpressionCreateStrategyTest.kt b/src/test/kotlin/calculator/stranegy/ExpressionCreateStrategyTest.kt deleted file mode 100644 index 2b00b8e4d4..0000000000 --- a/src/test/kotlin/calculator/stranegy/ExpressionCreateStrategyTest.kt +++ /dev/null @@ -1,35 +0,0 @@ -package calculator.stranegy - -import calculator.domain.SplitTerms -import org.assertj.core.api.Assertions.assertThat -import org.junit.jupiter.api.Test -import org.junit.jupiter.api.assertThrows - -internal class ExpressionCreateStrategyTest { - - @Test - fun `문자열 구분자를 제거하여 숫자만 있는 수식 만들기`() { - // given - val input = "//;\n1;2;3" - val splitTerms = SplitTerms.create(input) - - // when - val expression = ExpressionCreateStrategy.CALCULATOR_PLUS.create(input, splitTerms) - val actual = expression.value - - // then - val expected = "123" - assertThat(actual).isEqualTo(expected) - } - - @Test - fun `문자열 구분자 외 다른 문자가 포함되어 있어서 숫자만 있는 수식 생성 실패`() { - assertThrows { - val input = "//;\n1;2;3dasd" - val splitTerms = SplitTerms.create(input) - - ExpressionCreateStrategy.CALCULATOR_PLUS.create(input, splitTerms) - } - } - -} \ No newline at end of file From ebe87bfd88769235668c594a498c06b88a35a9ba Mon Sep 17 00:00:00 2001 From: Choiyounho Date: Mon, 5 Dec 2022 00:54:08 +0900 Subject: [PATCH 23/74] =?UTF-8?q?refactor=20:=20=EC=88=98=EC=8B=9D=20?= =?UTF-8?q?=EA=B0=9D=EC=B2=B4=20=EB=A6=AC=ED=8C=A9=ED=86=A0=EB=A7=81=20?= =?UTF-8?q?=EB=B0=8F=20=EB=84=A4=EC=9D=B4=EB=B0=8D=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kotlin/calculator/domain/Delimiters.kt | 41 +++++++++++++++++++ .../calculator/domain/DelimitersTest.kt | 32 +++++++++++++++ .../calculator/domain/SplitTermsTest.kt | 21 ---------- 3 files changed, 73 insertions(+), 21 deletions(-) create mode 100644 src/main/kotlin/calculator/domain/Delimiters.kt create mode 100644 src/test/kotlin/calculator/domain/DelimitersTest.kt delete mode 100644 src/test/kotlin/calculator/domain/SplitTermsTest.kt diff --git a/src/main/kotlin/calculator/domain/Delimiters.kt b/src/main/kotlin/calculator/domain/Delimiters.kt new file mode 100644 index 0000000000..fcd0503339 --- /dev/null +++ b/src/main/kotlin/calculator/domain/Delimiters.kt @@ -0,0 +1,41 @@ +package calculator.domain + +import calculator.util.RegexUtil.customRegex + +class Delimiters( + private val value: List = listOf(DEFAULT_DELIMITER_COMMA, DEFAULT_DELIMITER_COLONS), + private val operator: Operator = Operator.PLUS +) { + + fun toOperator(input: String): Operator { + return if (input in value) { + operator + } else { + throw IllegalArgumentException("등록된 구분자가 아닙니다.") + } + } + + companion object { + + private const val DEFAULT_DELIMITER_COMMA = "," + private const val DEFAULT_DELIMITER_COLONS = ":" + + private const val INDEX_DELIMITER = 1 + + fun create(input: String): Delimiters { + val delimiter = customRegex.find(input)?.run { + groupValues[INDEX_DELIMITER] + } + + if (delimiter != null) { + val delimiters = listOf( + DEFAULT_DELIMITER_COMMA, + DEFAULT_DELIMITER_COLONS + ) + delimiter + + return Delimiters(delimiters) + } + return Delimiters() + } + } +} diff --git a/src/test/kotlin/calculator/domain/DelimitersTest.kt b/src/test/kotlin/calculator/domain/DelimitersTest.kt new file mode 100644 index 0000000000..fe138a85cb --- /dev/null +++ b/src/test/kotlin/calculator/domain/DelimitersTest.kt @@ -0,0 +1,32 @@ +package calculator.domain + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.assertThrows + +internal class DelimitersTest { + + @Test + fun `문자열 구분자를 이용히여 연산자로 변환한다`() { + // given + val input = "//;\n1;2;3" + + // when + val actual = Delimiters.create(input).toOperator(";") + + // then + val expected = Operator.PLUS + assertThat(actual).isEqualTo(expected) + } + + @Test + fun `문자열 구분자를 이용하여 연산자 변환에 실패한다`() { + assertThrows { + // given + val input = "//;\n1;2;3" + + // when + val actual = Delimiters.create(input).toOperator(";") + } + } +} diff --git a/src/test/kotlin/calculator/domain/SplitTermsTest.kt b/src/test/kotlin/calculator/domain/SplitTermsTest.kt deleted file mode 100644 index b6183ffad1..0000000000 --- a/src/test/kotlin/calculator/domain/SplitTermsTest.kt +++ /dev/null @@ -1,21 +0,0 @@ -package calculator.domain - -import org.assertj.core.api.Assertions.assertThat -import org.junit.jupiter.api.Test - -internal class SplitTermsTest { - - @Test - fun `입력값을 이용하여 문자열 자르기 조건을 만든다`() { - // given - val input = "//;\n1;2;3" - - // when - val actual = SplitTerms.create(input).value - - // then - val result = listOf(",", ":", ";") - assertThat(actual).isEqualTo(result) - } - -} \ No newline at end of file From 7f89b72e17301858717afb8af4319027b61ab6d2 Mon Sep 17 00:00:00 2001 From: Choiyounho Date: Mon, 5 Dec 2022 00:54:24 +0900 Subject: [PATCH 24/74] =?UTF-8?q?refactor=20:=20=EC=83=81=EC=88=98=20?= =?UTF-8?q?=ED=8C=8C=EC=9D=BC=20=EB=B0=8F=20=EB=AC=B8=EC=9E=90=EC=97=B4=20?= =?UTF-8?q?=ED=99=95=EC=9E=A5=ED=8C=8C=EC=9D=BC=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/calculator/Constants.kt | 3 --- src/main/kotlin/calculator/extensions/StringExtension.kt | 7 ------- 2 files changed, 10 deletions(-) delete mode 100644 src/main/kotlin/calculator/Constants.kt delete mode 100644 src/main/kotlin/calculator/extensions/StringExtension.kt diff --git a/src/main/kotlin/calculator/Constants.kt b/src/main/kotlin/calculator/Constants.kt deleted file mode 100644 index dd55b79112..0000000000 --- a/src/main/kotlin/calculator/Constants.kt +++ /dev/null @@ -1,3 +0,0 @@ -package calculator - -const val EMPTY_STRING = "" diff --git a/src/main/kotlin/calculator/extensions/StringExtension.kt b/src/main/kotlin/calculator/extensions/StringExtension.kt deleted file mode 100644 index 2ea2910ccb..0000000000 --- a/src/main/kotlin/calculator/extensions/StringExtension.kt +++ /dev/null @@ -1,7 +0,0 @@ -package calculator.extensions - - -fun String.removeCustomRegex(): String { - return replace("//", "").replace("\n", "") -} - From 02fa4754b8b11f8a7d466514a3a118b679f46de0 Mon Sep 17 00:00:00 2001 From: Choiyounho Date: Mon, 5 Dec 2022 00:54:38 +0900 Subject: [PATCH 25/74] =?UTF-8?q?refactor=20:=20SplitUtil=20=EC=A0=9C?= =?UTF-8?q?=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kotlin/calculator/domain/SplitTerms.kt | 24 ------------------- src/main/kotlin/calculator/util/SplitUtil.kt | 17 ------------- 2 files changed, 41 deletions(-) delete mode 100644 src/main/kotlin/calculator/domain/SplitTerms.kt delete mode 100644 src/main/kotlin/calculator/util/SplitUtil.kt diff --git a/src/main/kotlin/calculator/domain/SplitTerms.kt b/src/main/kotlin/calculator/domain/SplitTerms.kt deleted file mode 100644 index be081015a3..0000000000 --- a/src/main/kotlin/calculator/domain/SplitTerms.kt +++ /dev/null @@ -1,24 +0,0 @@ -package calculator.domain - -class SplitTerms( - val value: List = listOf(TERMS_COMMA, TERMS_COLONS) -) { - - companion object { - fun create(input: String): SplitTerms { - val terms = Regex("//(.)\n(.*)").find(input)?.run { - groupValues[1] - } - - return if (terms != null) { - SplitTerms(listOf(TERMS_COMMA, TERMS_COLONS) + terms) - } else { - SplitTerms() - } - } - - private const val TERMS_COMMA = "," - private const val TERMS_COLONS = ":" - } - -} \ No newline at end of file diff --git a/src/main/kotlin/calculator/util/SplitUtil.kt b/src/main/kotlin/calculator/util/SplitUtil.kt deleted file mode 100644 index 3cb60acc12..0000000000 --- a/src/main/kotlin/calculator/util/SplitUtil.kt +++ /dev/null @@ -1,17 +0,0 @@ -package calculator.util - -import calculator.EMPTY_STRING -import calculator.domain.Expression - -object SplitUtil { - - private const val SPLIT_DEFAULT = "" - - fun splitExpressionToInts(expression: Expression): List { - return expression.value.split(SPLIT_DEFAULT).map { input -> - if (input == EMPTY_STRING) 0 - else input.toInt() - } - } - -} \ No newline at end of file From 25a68acd4911a631afdcceec4554e8cc27a3f140 Mon Sep 17 00:00:00 2001 From: Choiyounho Date: Mon, 5 Dec 2022 00:55:03 +0900 Subject: [PATCH 26/74] =?UTF-8?q?refactor=20:=20=EC=88=98=EC=8B=9D=20?= =?UTF-8?q?=ED=81=B4=EB=9E=98=EC=8A=A4=20=EC=BD=94=EB=93=9C=20=EB=A6=AC?= =?UTF-8?q?=ED=8C=A9=ED=86=A0=EB=A7=81=20=EB=B0=8F=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EC=BD=94=EB=93=9C=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kotlin/calculator/domain/Expression.kt | 41 +++++++++++++------ .../calculator/domain/ExpressionElement.kt | 7 ++++ .../calculator/domain/ExpressionTest.kt | 39 ++++++++++++++++++ 3 files changed, 74 insertions(+), 13 deletions(-) create mode 100644 src/main/kotlin/calculator/domain/ExpressionElement.kt create mode 100644 src/test/kotlin/calculator/domain/ExpressionTest.kt diff --git a/src/main/kotlin/calculator/domain/Expression.kt b/src/main/kotlin/calculator/domain/Expression.kt index 7e9e03576b..9616ca6c8d 100644 --- a/src/main/kotlin/calculator/domain/Expression.kt +++ b/src/main/kotlin/calculator/domain/Expression.kt @@ -1,22 +1,37 @@ package calculator.domain +import calculator.util.RegexUtil.customRegex + class Expression( - val value: String = DEFAULT_EXPRESSION_VALUE + val value: List = emptyList() ) { - init { - validateExpression() - } + companion object { + fun create(input: String, delimiters: Delimiters): Expression { + if (input.isEmpty()) { + return Expression(emptyList()) + } + + val value = removeCustomRegex(input) + .asSequence() + .map { char -> + if (char.isDigit()) { + ExpressionElement.OperandElement(char.digitToInt()) + } else { + ExpressionElement.OperatorElement(delimiters.toOperator(char.toString())) + } + }.toList() - private fun validateExpression() { - value.forEach { - runCatching { it.digitToInt() } - .onFailure { throw IllegalArgumentException("숫자 외에 다른 문자는 들어올 수 없습니다.") } + return Expression(value) } - } - companion object { - private const val DEFAULT_EXPRESSION_VALUE = "" + private fun removeCustomRegex(text: String): String { + if (customRegex.matches(text)) { + return customRegex.find(text) + ?.run { groupValues[2] } + .toString() + } + return text + } } - -} \ No newline at end of file +} diff --git a/src/main/kotlin/calculator/domain/ExpressionElement.kt b/src/main/kotlin/calculator/domain/ExpressionElement.kt new file mode 100644 index 0000000000..08490ffe3c --- /dev/null +++ b/src/main/kotlin/calculator/domain/ExpressionElement.kt @@ -0,0 +1,7 @@ +package calculator.domain + +sealed class ExpressionElement { + + class OperandElement(val value: Int) : ExpressionElement() + class OperatorElement(val value: Operator) : ExpressionElement() +} diff --git a/src/test/kotlin/calculator/domain/ExpressionTest.kt b/src/test/kotlin/calculator/domain/ExpressionTest.kt new file mode 100644 index 0000000000..b6436f17fe --- /dev/null +++ b/src/test/kotlin/calculator/domain/ExpressionTest.kt @@ -0,0 +1,39 @@ +package calculator.domain + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.assertThrows + +internal class ExpressionTest { + + @Test + fun `입력값을 이용하여 수식을 만든다`() { + // given + val input = "//;\n1;2;3" + val delimiters = Delimiters.create(input) + + // when + val expression = Expression.create(input, delimiters) + val actual = expression.value.size + + // then + val expected = 5 + assertThat(actual).isEqualTo(expected) + } + + @Test + fun `구분자 외에 다른 문자가 입력되어 수식 만들기 실패`() { + assertThrows { + val input = "//;\n1;2;3ddd" + val delimiters = Delimiters.create(input) + + // when + val expression = Expression.create(input, delimiters) + val actual = expression.value.size + + // then + val expected = 5 + assertThat(actual).isEqualTo(expected) + } + } +} From 10414cded58e90deccd048e3c14093f507dda454 Mon Sep 17 00:00:00 2001 From: Choiyounho Date: Mon, 5 Dec 2022 00:55:16 +0900 Subject: [PATCH 27/74] =?UTF-8?q?refactor=20:=20=EC=97=B0=EC=82=B0?= =?UTF-8?q?=EC=9E=90=20=EC=9D=B4=EB=84=98=20=ED=81=B4=EB=9E=98=EC=8A=A4=20?= =?UTF-8?q?=EC=83=9D=EC=84=B1=20=EB=B0=8F=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20?= =?UTF-8?q?=EC=BD=94=EB=93=9C=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/calculator/domain/Operator.kt | 14 ++++ .../kotlin/calculator/domain/OperatorTest.kt | 82 +++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 src/main/kotlin/calculator/domain/Operator.kt create mode 100644 src/test/kotlin/calculator/domain/OperatorTest.kt diff --git a/src/main/kotlin/calculator/domain/Operator.kt b/src/main/kotlin/calculator/domain/Operator.kt new file mode 100644 index 0000000000..5cef6eba32 --- /dev/null +++ b/src/main/kotlin/calculator/domain/Operator.kt @@ -0,0 +1,14 @@ +package calculator.domain + +enum class Operator( + val symbol: String, + val calculator: (Int, Int) -> Int +) { + PLUS("+", { x, y -> x + y }), + MINUS("-", { x, y -> x - y }), + MULTIPLY("-", { x, y -> x * y }), + DIVIDE("-", { x, y -> + val isZero = x == 0 || y == 0 + if (isZero) 0 else x / y + }); +} diff --git a/src/test/kotlin/calculator/domain/OperatorTest.kt b/src/test/kotlin/calculator/domain/OperatorTest.kt new file mode 100644 index 0000000000..f0cb876b74 --- /dev/null +++ b/src/test/kotlin/calculator/domain/OperatorTest.kt @@ -0,0 +1,82 @@ +package calculator.domain + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test + +internal class OperatorTest { + + @Test + fun `더하기 연산자를 이용한 덧셈`() { + // given + val operator = Operator.PLUS + val firstOperand = 1 + val secondOperand = 1 + + // when + val actual = operator.calculator(firstOperand, secondOperand) + + // then + val expected = 2 + assertThat(actual).isEqualTo(expected) + } + + @Test + fun `빼기 연산자를 이용한 뺄셈`() { + // given + val operator = Operator.MINUS + val firstOperand = 1 + val secondOperand = 1 + + // when + val actual = operator.calculator(firstOperand, secondOperand) + + // then + val expected = 0 + assertThat(actual).isEqualTo(expected) + } + + @Test + fun `곱셈 연산자를 이용한 곱셈`() { + // given + val operator = Operator.MULTIPLY + val firstOperand = 1 + val secondOperand = 1 + + // when + val actual = operator.calculator(firstOperand, secondOperand) + + // then + val expected = 1 + assertThat(actual).isEqualTo(expected) + } + + @Test + fun `나누기 연산자를 이용한 나눗셈`() { + // given + val operator = Operator.DIVIDE + val firstOperand = 1 + val secondOperand = 1 + + // when + val actual = operator.calculator(firstOperand, secondOperand) + + // then + val expected = 1 + assertThat(actual).isEqualTo(expected) + } + + @Test + fun `0이 포함된 피연산자를 이용한 나눗셈`() { + // given + val operator = Operator.DIVIDE + val firstOperand = 1 + val secondOperand = 0 + + // when + val actual = operator.calculator(firstOperand, secondOperand) + + // then + val expected = 0 + assertThat(actual).isEqualTo(expected) + } +} From 8c3b02df4184ed39eb0f6ab17be7f31cabafe462 Mon Sep 17 00:00:00 2001 From: Choiyounho Date: Mon, 5 Dec 2022 00:55:41 +0900 Subject: [PATCH 28/74] =?UTF-8?q?refactor=20:=20=EC=A0=95=EA=B7=9C?= =?UTF-8?q?=EC=8B=9D=20=EC=9C=A0=ED=8B=B8=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/calculator/util/RegexUtil.kt | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 src/main/kotlin/calculator/util/RegexUtil.kt diff --git a/src/main/kotlin/calculator/util/RegexUtil.kt b/src/main/kotlin/calculator/util/RegexUtil.kt new file mode 100644 index 0000000000..8b51a4f149 --- /dev/null +++ b/src/main/kotlin/calculator/util/RegexUtil.kt @@ -0,0 +1,6 @@ +package calculator.util + +object RegexUtil { + + val customRegex = Regex("//(.)\n(.*)") +} From 6825b55e02eb58486e8becd68d0f0e7e41795eba Mon Sep 17 00:00:00 2001 From: Choiyounho Date: Mon, 5 Dec 2022 00:55:58 +0900 Subject: [PATCH 29/74] =?UTF-8?q?refactor=20:=20=EA=B3=84=EC=82=B0?= =?UTF-8?q?=EA=B8=B0=20=EC=BD=94=EB=93=9C=20=EB=A6=AC=ED=8C=A9=ED=86=A0?= =?UTF-8?q?=EB=A7=81=20=EB=B0=8F=20=ED=85=8C=EC=8A=A4=ED=8A=B8=EC=BD=94?= =?UTF-8?q?=EB=93=9C=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kotlin/calculator/domain/Calculator.kt | 22 +++++++++++-------- .../calculator/domain/CalculatorTest.kt | 17 ++++++++++---- 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/src/main/kotlin/calculator/domain/Calculator.kt b/src/main/kotlin/calculator/domain/Calculator.kt index 59bdfe88f4..9498a137fe 100644 --- a/src/main/kotlin/calculator/domain/Calculator.kt +++ b/src/main/kotlin/calculator/domain/Calculator.kt @@ -1,17 +1,21 @@ package calculator.domain -import calculator.util.SplitUtil.splitExpressionToInts - class Calculator(private val expression: Expression) { fun calculate(): Int { - var result = 0 - - splitExpressionToInts(expression).forEach { number -> - result = result.plus(number) - } + val operands = expression.value.mapNotNull { it as? ExpressionElement.OperandElement } + val operators = expression.value.mapNotNull { it as? ExpressionElement.OperatorElement } - return result + return operands.reduceIndexed { index, acc, operandElement -> + ExpressionElement.OperandElement( + operators[index - BUFFER_OPERATOR] + .value + .calculator(acc.value, operandElement.value) + ) + }.value } -} \ No newline at end of file + companion object { + private const val BUFFER_OPERATOR = 1 + } +} diff --git a/src/test/kotlin/calculator/domain/CalculatorTest.kt b/src/test/kotlin/calculator/domain/CalculatorTest.kt index 61df9f99fd..dd01c07361 100644 --- a/src/test/kotlin/calculator/domain/CalculatorTest.kt +++ b/src/test/kotlin/calculator/domain/CalculatorTest.kt @@ -8,15 +8,24 @@ internal class CalculatorTest { @Test fun `수식을 이용하여 계산에 성공한다`() { // given - val expression = Expression("123") + val expression = Expression( + listOf( + ExpressionElement.OperandElement(2), + ExpressionElement.OperatorElement(Operator.PLUS), + ExpressionElement.OperandElement(2), + ExpressionElement.OperatorElement(Operator.PLUS), + ExpressionElement.OperandElement(2), + ExpressionElement.OperatorElement(Operator.PLUS), + ExpressionElement.OperandElement(2) + ) + ) val calculator = Calculator(expression) // when val actual = calculator.calculate() // then - val expected = 6 + val expected = 8 assertThat(actual).isEqualTo(expected) } - -} \ No newline at end of file +} From bd83bbc5ebf83a03f6b351ed8090296a06bccaf0 Mon Sep 17 00:00:00 2001 From: Choiyounho Date: Mon, 5 Dec 2022 01:02:25 +0900 Subject: [PATCH 30/74] =?UTF-8?q?refactor=20:=20=EC=88=98=EC=8B=9D=20?= =?UTF-8?q?=ED=81=B4=EB=9E=98=EC=8A=A4=20=EC=83=9D=EC=84=B1=EC=9E=90=20pri?= =?UTF-8?q?vate?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/calculator/domain/Expression.kt | 2 +- src/test/kotlin/calculator/domain/CalculatorTest.kt | 12 +----------- 2 files changed, 2 insertions(+), 12 deletions(-) diff --git a/src/main/kotlin/calculator/domain/Expression.kt b/src/main/kotlin/calculator/domain/Expression.kt index 9616ca6c8d..58fc96f14b 100644 --- a/src/main/kotlin/calculator/domain/Expression.kt +++ b/src/main/kotlin/calculator/domain/Expression.kt @@ -2,7 +2,7 @@ package calculator.domain import calculator.util.RegexUtil.customRegex -class Expression( +class Expression private constructor( val value: List = emptyList() ) { diff --git a/src/test/kotlin/calculator/domain/CalculatorTest.kt b/src/test/kotlin/calculator/domain/CalculatorTest.kt index dd01c07361..f5023b3353 100644 --- a/src/test/kotlin/calculator/domain/CalculatorTest.kt +++ b/src/test/kotlin/calculator/domain/CalculatorTest.kt @@ -8,17 +8,7 @@ internal class CalculatorTest { @Test fun `수식을 이용하여 계산에 성공한다`() { // given - val expression = Expression( - listOf( - ExpressionElement.OperandElement(2), - ExpressionElement.OperatorElement(Operator.PLUS), - ExpressionElement.OperandElement(2), - ExpressionElement.OperatorElement(Operator.PLUS), - ExpressionElement.OperandElement(2), - ExpressionElement.OperatorElement(Operator.PLUS), - ExpressionElement.OperandElement(2) - ) - ) + val expression = Expression.create("2,2,2,2", Delimiters()) val calculator = Calculator(expression) // when From 9058a0d49b7a32cc2985a0e4e126601de26ec3d9 Mon Sep 17 00:00:00 2001 From: Choiyounho Date: Mon, 5 Dec 2022 01:05:22 +0900 Subject: [PATCH 31/74] =?UTF-8?q?refactor=20:=20=EB=B9=88=20=EC=88=98?= =?UTF-8?q?=EC=8B=9D=EC=9D=B4=20=EB=93=A4=EC=96=B4=EC=98=AC=20=EA=B2=BD?= =?UTF-8?q?=EC=9A=B0=200=EC=9C=BC=EB=A1=9C=20=EB=B0=98=ED=99=98=ED=95=98?= =?UTF-8?q?=EB=8A=94=20=EC=BD=94=EB=93=9C=20=EB=B0=8F=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EC=BD=94=EB=93=9C=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/calculator/domain/Calculator.kt | 4 ++++ src/test/kotlin/calculator/domain/CalculatorTest.kt | 13 +++++++++++++ 2 files changed, 17 insertions(+) diff --git a/src/main/kotlin/calculator/domain/Calculator.kt b/src/main/kotlin/calculator/domain/Calculator.kt index 9498a137fe..438243a725 100644 --- a/src/main/kotlin/calculator/domain/Calculator.kt +++ b/src/main/kotlin/calculator/domain/Calculator.kt @@ -3,6 +3,10 @@ package calculator.domain class Calculator(private val expression: Expression) { fun calculate(): Int { + if (expression.value.isEmpty()) { + return 0 + } + val operands = expression.value.mapNotNull { it as? ExpressionElement.OperandElement } val operators = expression.value.mapNotNull { it as? ExpressionElement.OperatorElement } diff --git a/src/test/kotlin/calculator/domain/CalculatorTest.kt b/src/test/kotlin/calculator/domain/CalculatorTest.kt index f5023b3353..635e7b2ab5 100644 --- a/src/test/kotlin/calculator/domain/CalculatorTest.kt +++ b/src/test/kotlin/calculator/domain/CalculatorTest.kt @@ -18,4 +18,17 @@ internal class CalculatorTest { val expected = 8 assertThat(actual).isEqualTo(expected) } + + @Test + fun `빈 수식으로 계산결과가 0이된다`() { + // given + val expression = Expression.create("", Delimiters()) + + // when + val actual = Calculator(expression).calculate() + + // then + val expected = 0 + assertThat(actual).isEqualTo(expected) + } } From dc8e1fe54166ab51d79a7430b9f6e388420d56b0 Mon Sep 17 00:00:00 2001 From: Choiyounho Date: Tue, 6 Dec 2022 22:38:40 +0900 Subject: [PATCH 32/74] =?UTF-8?q?refactor=20:=20=EA=B5=AC=EB=B6=84?= =?UTF-8?q?=EC=9E=90=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BD=94=EB=93=9C=20?= =?UTF-8?q?=EC=98=A4=ED=83=80=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/kotlin/calculator/domain/DelimitersTest.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/kotlin/calculator/domain/DelimitersTest.kt b/src/test/kotlin/calculator/domain/DelimitersTest.kt index fe138a85cb..5478d6365c 100644 --- a/src/test/kotlin/calculator/domain/DelimitersTest.kt +++ b/src/test/kotlin/calculator/domain/DelimitersTest.kt @@ -26,7 +26,7 @@ internal class DelimitersTest { val input = "//;\n1;2;3" // when - val actual = Delimiters.create(input).toOperator(";") + val actual = Delimiters.create(input).toOperator("ㅇ") } } } From ab93e293754188844dfe0815e6d33815fd024705 Mon Sep 17 00:00:00 2001 From: ChoiYounho Date: Mon, 12 Dec 2022 20:21:35 +0900 Subject: [PATCH 33/74] feat : RunTimeException Contrat --- src/main/kotlin/calculator/util/ContractUtil.kt | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 src/main/kotlin/calculator/util/ContractUtil.kt diff --git a/src/main/kotlin/calculator/util/ContractUtil.kt b/src/main/kotlin/calculator/util/ContractUtil.kt new file mode 100644 index 0000000000..11a638ac09 --- /dev/null +++ b/src/main/kotlin/calculator/util/ContractUtil.kt @@ -0,0 +1,9 @@ +package calculator.extensions + + +inline fun validate(value: Boolean, lazyMessage: () -> Any) { + if (value.not()) { + val message = lazyMessage() + throw RuntimeException(message.toString()) + } +} \ No newline at end of file From 66ef1e0a7d1a208d7cf216a4b806e9f18f82d2ae Mon Sep 17 00:00:00 2001 From: ChoiYounho Date: Mon, 12 Dec 2022 20:22:20 +0900 Subject: [PATCH 34/74] =?UTF-8?q?refactor=20:=20=EC=97=B0=EC=82=B0?= =?UTF-8?q?=EC=9E=90=20=ED=98=84=EC=9E=AC=20=EC=82=AC=EC=9A=A9=ED=95=98?= =?UTF-8?q?=EC=A7=80=20=EC=95=8A=EB=8A=94=20=EC=BD=94=EB=93=9C=20=EC=A0=9C?= =?UTF-8?q?=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/calculator/domain/Operator.kt | 8 +-- .../kotlin/calculator/domain/OperatorTest.kt | 60 ------------------- 2 files changed, 1 insertion(+), 67 deletions(-) diff --git a/src/main/kotlin/calculator/domain/Operator.kt b/src/main/kotlin/calculator/domain/Operator.kt index 5cef6eba32..5b9c5c5d2b 100644 --- a/src/main/kotlin/calculator/domain/Operator.kt +++ b/src/main/kotlin/calculator/domain/Operator.kt @@ -4,11 +4,5 @@ enum class Operator( val symbol: String, val calculator: (Int, Int) -> Int ) { - PLUS("+", { x, y -> x + y }), - MINUS("-", { x, y -> x - y }), - MULTIPLY("-", { x, y -> x * y }), - DIVIDE("-", { x, y -> - val isZero = x == 0 || y == 0 - if (isZero) 0 else x / y - }); + PLUS("+", { x, y -> x + y }) } diff --git a/src/test/kotlin/calculator/domain/OperatorTest.kt b/src/test/kotlin/calculator/domain/OperatorTest.kt index f0cb876b74..65a365a8d1 100644 --- a/src/test/kotlin/calculator/domain/OperatorTest.kt +++ b/src/test/kotlin/calculator/domain/OperatorTest.kt @@ -19,64 +19,4 @@ internal class OperatorTest { val expected = 2 assertThat(actual).isEqualTo(expected) } - - @Test - fun `빼기 연산자를 이용한 뺄셈`() { - // given - val operator = Operator.MINUS - val firstOperand = 1 - val secondOperand = 1 - - // when - val actual = operator.calculator(firstOperand, secondOperand) - - // then - val expected = 0 - assertThat(actual).isEqualTo(expected) - } - - @Test - fun `곱셈 연산자를 이용한 곱셈`() { - // given - val operator = Operator.MULTIPLY - val firstOperand = 1 - val secondOperand = 1 - - // when - val actual = operator.calculator(firstOperand, secondOperand) - - // then - val expected = 1 - assertThat(actual).isEqualTo(expected) - } - - @Test - fun `나누기 연산자를 이용한 나눗셈`() { - // given - val operator = Operator.DIVIDE - val firstOperand = 1 - val secondOperand = 1 - - // when - val actual = operator.calculator(firstOperand, secondOperand) - - // then - val expected = 1 - assertThat(actual).isEqualTo(expected) - } - - @Test - fun `0이 포함된 피연산자를 이용한 나눗셈`() { - // given - val operator = Operator.DIVIDE - val firstOperand = 1 - val secondOperand = 0 - - // when - val actual = operator.calculator(firstOperand, secondOperand) - - // then - val expected = 0 - assertThat(actual).isEqualTo(expected) - } } From 225559ac7cf6d24583f202c006cf5a44dc651cac Mon Sep 17 00:00:00 2001 From: ChoiYounho Date: Mon, 12 Dec 2022 20:22:31 +0900 Subject: [PATCH 35/74] =?UTF-8?q?refactor=20:=20=EC=97=B0=EC=82=B0?= =?UTF-8?q?=EC=9E=90=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/kotlin/calculator/domain/ExpressionTest.kt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/test/kotlin/calculator/domain/ExpressionTest.kt b/src/test/kotlin/calculator/domain/ExpressionTest.kt index b6436f17fe..cc36097bfa 100644 --- a/src/test/kotlin/calculator/domain/ExpressionTest.kt +++ b/src/test/kotlin/calculator/domain/ExpressionTest.kt @@ -23,7 +23,7 @@ internal class ExpressionTest { @Test fun `구분자 외에 다른 문자가 입력되어 수식 만들기 실패`() { - assertThrows { + assertThrows { val input = "//;\n1;2;3ddd" val delimiters = Delimiters.create(input) @@ -36,4 +36,6 @@ internal class ExpressionTest { assertThat(actual).isEqualTo(expected) } } + + } From 7647619801486b0a4cfa4f6e6c456ec49aa72613 Mon Sep 17 00:00:00 2001 From: ChoiYounho Date: Mon, 12 Dec 2022 20:23:14 +0900 Subject: [PATCH 36/74] refactor : seald class -> seald interface --- src/main/kotlin/calculator/domain/ExpressionElement.kt | 6 +++--- src/main/kotlin/calculator/util/ContractUtil.kt | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/kotlin/calculator/domain/ExpressionElement.kt b/src/main/kotlin/calculator/domain/ExpressionElement.kt index 08490ffe3c..47124771cb 100644 --- a/src/main/kotlin/calculator/domain/ExpressionElement.kt +++ b/src/main/kotlin/calculator/domain/ExpressionElement.kt @@ -1,7 +1,7 @@ package calculator.domain -sealed class ExpressionElement { +sealed interface ExpressionElement { - class OperandElement(val value: Int) : ExpressionElement() - class OperatorElement(val value: Operator) : ExpressionElement() + class OperandElement(val value: Int) : ExpressionElement + class OperatorElement(val value: Operator) : ExpressionElement } diff --git a/src/main/kotlin/calculator/util/ContractUtil.kt b/src/main/kotlin/calculator/util/ContractUtil.kt index 11a638ac09..bd4bcd98e1 100644 --- a/src/main/kotlin/calculator/util/ContractUtil.kt +++ b/src/main/kotlin/calculator/util/ContractUtil.kt @@ -1,4 +1,4 @@ -package calculator.extensions +package calculator.util inline fun validate(value: Boolean, lazyMessage: () -> Any) { From fdf7d084f765b24965416522596b30e0631ca2be Mon Sep 17 00:00:00 2001 From: ChoiYounho Date: Mon, 12 Dec 2022 20:23:25 +0900 Subject: [PATCH 37/74] =?UTF-8?q?refactor=20:=20=EA=B3=84=EC=82=B0?= =?UTF-8?q?=EA=B8=B0=20=EC=BD=94=EB=93=9C=20=EB=A6=AC=ED=8C=A9=ED=86=A0?= =?UTF-8?q?=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/calculator/domain/Calculator.kt | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/src/main/kotlin/calculator/domain/Calculator.kt b/src/main/kotlin/calculator/domain/Calculator.kt index 438243a725..37f2ac0340 100644 --- a/src/main/kotlin/calculator/domain/Calculator.kt +++ b/src/main/kotlin/calculator/domain/Calculator.kt @@ -4,22 +4,19 @@ class Calculator(private val expression: Expression) { fun calculate(): Int { if (expression.value.isEmpty()) { - return 0 + return EMPTY_CALCULATE_RESULT } - val operands = expression.value.mapNotNull { it as? ExpressionElement.OperandElement } - val operators = expression.value.mapNotNull { it as? ExpressionElement.OperatorElement } + val operands = expression.fetchOperands() + val operators = expression.fetchOperators() - return operands.reduceIndexed { index, acc, operandElement -> - ExpressionElement.OperandElement( - operators[index - BUFFER_OPERATOR] - .value - .calculator(acc.value, operandElement.value) - ) - }.value + return operands.reduceIndexed { index, acc, operand -> + operators[index - BUFFER_OPERATOR].calculator(acc, operand) + } } companion object { private const val BUFFER_OPERATOR = 1 + private const val EMPTY_CALCULATE_RESULT = 0 } } From df428ea945a214a76312408d3a7fe2e8d2ebf831 Mon Sep 17 00:00:00 2001 From: ChoiYounho Date: Mon, 12 Dec 2022 20:23:43 +0900 Subject: [PATCH 38/74] =?UTF-8?q?refactor=20:=20=EA=B5=AC=EB=B6=84?= =?UTF-8?q?=EC=9E=90=20=EC=BD=94=EB=93=9C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kotlin/calculator/domain/Delimiters.kt | 34 +++++++++++-------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/src/main/kotlin/calculator/domain/Delimiters.kt b/src/main/kotlin/calculator/domain/Delimiters.kt index fcd0503339..aba5addb41 100644 --- a/src/main/kotlin/calculator/domain/Delimiters.kt +++ b/src/main/kotlin/calculator/domain/Delimiters.kt @@ -3,35 +3,41 @@ package calculator.domain import calculator.util.RegexUtil.customRegex class Delimiters( - private val value: List = listOf(DEFAULT_DELIMITER_COMMA, DEFAULT_DELIMITER_COLONS), + val value: List = DEFAULT_DELIMITER, private val operator: Operator = Operator.PLUS ) { fun toOperator(input: String): Operator { - return if (input in value) { - operator - } else { - throw IllegalArgumentException("등록된 구분자가 아닙니다.") + if (input !in value) { + throw RuntimeException("등록된 구분자가 아닙니다.") } + + return operator } - companion object { + fun removeCustomRegex(text: String): String { + if (customRegex.matches(text)) { + return customRegex.find(text) + ?.let { it.groupValues[INDEX_REMOVE_REGEX] } + .toString() + } + return text + } - private const val DEFAULT_DELIMITER_COMMA = "," - private const val DEFAULT_DELIMITER_COLONS = ":" + companion object { private const val INDEX_DELIMITER = 1 + private const val INDEX_REMOVE_REGEX = 1 + + private val DEFAULT_DELIMITER = listOf(",", ":") fun create(input: String): Delimiters { - val delimiter = customRegex.find(input)?.run { - groupValues[INDEX_DELIMITER] + val delimiter = customRegex.find(input)?.let { + it.groupValues[INDEX_DELIMITER] } if (delimiter != null) { - val delimiters = listOf( - DEFAULT_DELIMITER_COMMA, - DEFAULT_DELIMITER_COLONS - ) + delimiter + val delimiters = DEFAULT_DELIMITER + delimiter return Delimiters(delimiters) } From 2b5432ff5abe0d92abc400caebb0e3b15aa0281c Mon Sep 17 00:00:00 2001 From: ChoiYounho Date: Mon, 12 Dec 2022 20:23:56 +0900 Subject: [PATCH 39/74] =?UTF-8?q?refactor=20:=20=EC=88=98=EC=8B=9D=20?= =?UTF-8?q?=EC=BD=94=EB=93=9C=20=EB=A6=AC=ED=8C=A9=ED=86=A0=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kotlin/calculator/domain/Expression.kt | 44 +++++++++++-------- 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/src/main/kotlin/calculator/domain/Expression.kt b/src/main/kotlin/calculator/domain/Expression.kt index 58fc96f14b..807f99381d 100644 --- a/src/main/kotlin/calculator/domain/Expression.kt +++ b/src/main/kotlin/calculator/domain/Expression.kt @@ -1,37 +1,45 @@ package calculator.domain -import calculator.util.RegexUtil.customRegex +import calculator.util.validate class Expression private constructor( val value: List = emptyList() ) { + fun fetchOperands(): List { + return value.filterIsInstance().map { it.value } + } + + fun fetchOperators(): List { + return value.filterIsInstance().map { it.value } + } + companion object { + + private const val MESSAGE_NOT_POSITIVE_NUMBER = "양수가 아닌 숫자를 전달 받을 수 없습니다." + fun create(input: String, delimiters: Delimiters): Expression { if (input.isEmpty()) { - return Expression(emptyList()) + return Expression() } - val value = removeCustomRegex(input) - .asSequence() - .map { char -> - if (char.isDigit()) { - ExpressionElement.OperandElement(char.digitToInt()) + val value = delimiters.removeCustomRegex(input) + .map { + if (it.isDigit()) { + val digit = it.digitToInt() + + val isNotNegativeNumber = digit > -1 + validate(isNotNegativeNumber) { MESSAGE_NOT_POSITIVE_NUMBER } + + ExpressionElement.OperandElement(digit) } else { - ExpressionElement.OperatorElement(delimiters.toOperator(char.toString())) + ExpressionElement.OperatorElement(delimiters.toOperator(it.toString())) + } - }.toList() + } - return Expression(value) - } - private fun removeCustomRegex(text: String): String { - if (customRegex.matches(text)) { - return customRegex.find(text) - ?.run { groupValues[2] } - .toString() - } - return text + return Expression(value) } } } From 1ad9f826bc16701e488c9ff1b74e17148ab0a98f Mon Sep 17 00:00:00 2001 From: ChoiYounho Date: Mon, 19 Dec 2022 17:46:53 +0900 Subject: [PATCH 40/74] =?UTF-8?q?feat=20:=20=EA=B3=84=EC=82=B0=EA=B8=B0=20?= =?UTF-8?q?=EC=9D=B8=ED=84=B0=ED=8E=98=EC=9D=B4=EC=8A=A4=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/calculator/domain/Calculate.kt | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 src/main/kotlin/calculator/domain/Calculate.kt diff --git a/src/main/kotlin/calculator/domain/Calculate.kt b/src/main/kotlin/calculator/domain/Calculate.kt new file mode 100644 index 0000000000..22a3819bb0 --- /dev/null +++ b/src/main/kotlin/calculator/domain/Calculate.kt @@ -0,0 +1,4 @@ +package calculator.domain + +interface Calculate { +} \ No newline at end of file From 3b39bd52e7e5ed1360012730630c89cbf68c650c Mon Sep 17 00:00:00 2001 From: ChoiYounho Date: Mon, 19 Dec 2022 17:47:16 +0900 Subject: [PATCH 41/74] =?UTF-8?q?refactor=20:=20=EB=AC=B8=EC=9E=90?= =?UTF-8?q?=EC=97=B4=20=EB=8D=A7=EC=85=88=20=EA=B3=84=EC=82=B0=EA=B8=B0=20?= =?UTF-8?q?=ED=81=B4=EB=9E=98=EC=8A=A4=EB=A1=9C=20=EB=84=A4=EC=9D=B4?= =?UTF-8?q?=EB=B0=8D=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../calculator/domain/AddStringCalculator.kt | 22 +++++++++++++++++++ .../kotlin/calculator/domain/Calculate.kt | 2 ++ 2 files changed, 24 insertions(+) create mode 100644 src/main/kotlin/calculator/domain/AddStringCalculator.kt diff --git a/src/main/kotlin/calculator/domain/AddStringCalculator.kt b/src/main/kotlin/calculator/domain/AddStringCalculator.kt new file mode 100644 index 0000000000..b35f40d399 --- /dev/null +++ b/src/main/kotlin/calculator/domain/AddStringCalculator.kt @@ -0,0 +1,22 @@ +package calculator.domain + +class AddStringCalculator : Calculate { + + override fun calculate(expression: Expression): Int { + if (expression.value.isEmpty()) { + return EMPTY_CALCULATE_RESULT + } + + val operands = expression.fetchOperands() + val operators = expression.fetchOperators() + + return operands.reduceIndexed { index, acc, operand -> + operators[index - BUFFER_OPERATOR].calculator(acc, operand) + } + } + + companion object { + private const val BUFFER_OPERATOR = 1 + private const val EMPTY_CALCULATE_RESULT = 0 + } +} diff --git a/src/main/kotlin/calculator/domain/Calculate.kt b/src/main/kotlin/calculator/domain/Calculate.kt index 22a3819bb0..45000cad76 100644 --- a/src/main/kotlin/calculator/domain/Calculate.kt +++ b/src/main/kotlin/calculator/domain/Calculate.kt @@ -1,4 +1,6 @@ package calculator.domain interface Calculate { + + fun calculate(expression: Expression): Int } \ No newline at end of file From 5855986ec10b98cddac467011ea3136ce8903912 Mon Sep 17 00:00:00 2001 From: ChoiYounho Date: Mon, 19 Dec 2022 17:47:35 +0900 Subject: [PATCH 42/74] =?UTF-8?q?refactor=20:=20=EA=B5=AC=EB=B6=84?= =?UTF-8?q?=EC=9E=90=20=ED=81=B4=EB=9E=98=EC=8A=A4=20=EB=A6=AC=ED=8C=A9?= =?UTF-8?q?=ED=86=A0=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kotlin/calculator/domain/Calculator.kt | 22 ------------------- .../kotlin/calculator/domain/Delimiters.kt | 18 ++++----------- 2 files changed, 4 insertions(+), 36 deletions(-) delete mode 100644 src/main/kotlin/calculator/domain/Calculator.kt diff --git a/src/main/kotlin/calculator/domain/Calculator.kt b/src/main/kotlin/calculator/domain/Calculator.kt deleted file mode 100644 index 37f2ac0340..0000000000 --- a/src/main/kotlin/calculator/domain/Calculator.kt +++ /dev/null @@ -1,22 +0,0 @@ -package calculator.domain - -class Calculator(private val expression: Expression) { - - fun calculate(): Int { - if (expression.value.isEmpty()) { - return EMPTY_CALCULATE_RESULT - } - - val operands = expression.fetchOperands() - val operators = expression.fetchOperators() - - return operands.reduceIndexed { index, acc, operand -> - operators[index - BUFFER_OPERATOR].calculator(acc, operand) - } - } - - companion object { - private const val BUFFER_OPERATOR = 1 - private const val EMPTY_CALCULATE_RESULT = 0 - } -} diff --git a/src/main/kotlin/calculator/domain/Delimiters.kt b/src/main/kotlin/calculator/domain/Delimiters.kt index aba5addb41..b12133edd4 100644 --- a/src/main/kotlin/calculator/domain/Delimiters.kt +++ b/src/main/kotlin/calculator/domain/Delimiters.kt @@ -3,18 +3,9 @@ package calculator.domain import calculator.util.RegexUtil.customRegex class Delimiters( - val value: List = DEFAULT_DELIMITER, - private val operator: Operator = Operator.PLUS + val value: List = DEFAULT_DELIMITER ) { - fun toOperator(input: String): Operator { - if (input !in value) { - throw RuntimeException("등록된 구분자가 아닙니다.") - } - - return operator - } - fun removeCustomRegex(text: String): String { if (customRegex.matches(text)) { return customRegex.find(text) @@ -27,14 +18,13 @@ class Delimiters( companion object { private const val INDEX_DELIMITER = 1 - private const val INDEX_REMOVE_REGEX = 1 + private const val INDEX_REMOVE_REGEX = 2 private val DEFAULT_DELIMITER = listOf(",", ":") fun create(input: String): Delimiters { - val delimiter = customRegex.find(input)?.let { - it.groupValues[INDEX_DELIMITER] - } + val delimiter = customRegex.find(input) + ?.let { it.groupValues[INDEX_DELIMITER] } if (delimiter != null) { val delimiters = DEFAULT_DELIMITER + delimiter From fe95b3b322defa57affa30187687d6fb950478c2 Mon Sep 17 00:00:00 2001 From: ChoiYounho Date: Mon, 19 Dec 2022 17:47:47 +0900 Subject: [PATCH 43/74] =?UTF-8?q?refactor=20:=20=EC=88=98=EC=8B=9D=20?= =?UTF-8?q?=ED=81=B4=EB=9E=98=EC=8A=A4=20=EB=A6=AC=ED=8C=A9=ED=86=A0?= =?UTF-8?q?=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kotlin/calculator/domain/Expression.kt | 31 ++++++++++++------- 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/src/main/kotlin/calculator/domain/Expression.kt b/src/main/kotlin/calculator/domain/Expression.kt index 807f99381d..9e82d15bf0 100644 --- a/src/main/kotlin/calculator/domain/Expression.kt +++ b/src/main/kotlin/calculator/domain/Expression.kt @@ -1,5 +1,7 @@ package calculator.domain +import calculator.extensions.isNumeric +import calculator.extensions.split import calculator.util.validate class Expression private constructor( @@ -17,29 +19,34 @@ class Expression private constructor( companion object { private const val MESSAGE_NOT_POSITIVE_NUMBER = "양수가 아닌 숫자를 전달 받을 수 없습니다." + private const val MESSAGE_NOT_OPERAND = "피연산자만 들어올 수 있습니다." fun create(input: String, delimiters: Delimiters): Expression { if (input.isEmpty()) { return Expression() } - val value = delimiters.removeCustomRegex(input) - .map { - if (it.isDigit()) { - val digit = it.digitToInt() + val text = delimiters.removeCustomRegex(input) + val splitDelimiters = text.split(delimiters.value) + val lastIndex = splitDelimiters.size - 1 - val isNotNegativeNumber = digit > -1 - validate(isNotNegativeNumber) { MESSAGE_NOT_POSITIVE_NUMBER } + val expressionElements = mutableListOf() - ExpressionElement.OperandElement(digit) - } else { - ExpressionElement.OperatorElement(delimiters.toOperator(it.toString())) + splitDelimiters.mapIndexed { index, it -> + if (it.isNumeric().not()) throw IllegalArgumentException(MESSAGE_NOT_OPERAND) - } - } + val digit = it.toInt() + + val isNotNegativeNumber = digit > -1 + validate(isNotNegativeNumber) { MESSAGE_NOT_POSITIVE_NUMBER } + expressionElements.add(ExpressionElement.OperandElement(digit)) + if (index != lastIndex) { + expressionElements.add(ExpressionElement.OperatorElement(Operator.PLUS)) + } + } - return Expression(value) + return Expression(expressionElements.toList()) } } } From eac142cc7bb41ebf87f992e725c99db82ff34eae Mon Sep 17 00:00:00 2001 From: ChoiYounho Date: Mon, 19 Dec 2022 17:47:57 +0900 Subject: [PATCH 44/74] =?UTF-8?q?feat=20:=20=EB=AC=B8=EC=9E=90=EC=97=B4=20?= =?UTF-8?q?=ED=99=95=EC=9E=A5=ED=95=A8=EC=88=98=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kotlin/calculator/extensions/StringExtensions.kt | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 src/main/kotlin/calculator/extensions/StringExtensions.kt diff --git a/src/main/kotlin/calculator/extensions/StringExtensions.kt b/src/main/kotlin/calculator/extensions/StringExtensions.kt new file mode 100644 index 0000000000..bc9f6760a1 --- /dev/null +++ b/src/main/kotlin/calculator/extensions/StringExtensions.kt @@ -0,0 +1,9 @@ +package calculator.extensions + +fun String.isNumeric(): Boolean { + return toDoubleOrNull() != null +} + +fun String.split(list: List): List { + return split(delimiters = list.toTypedArray()) +} \ No newline at end of file From c9744bde1c19646286ead51f45d65881aba43f0d Mon Sep 17 00:00:00 2001 From: ChoiYounho Date: Mon, 19 Dec 2022 17:48:09 +0900 Subject: [PATCH 45/74] =?UTF-8?q?refactor=20:=20=EA=B3=84=EC=82=B0?= =?UTF-8?q?=EA=B8=B0=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BD=94=EB=93=9C=20?= =?UTF-8?q?=EB=A6=AC=ED=8C=A9=ED=86=A0=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../{CalculatorTest.kt => AddStringCalculatorTest.kt} | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) rename src/test/kotlin/calculator/domain/{CalculatorTest.kt => AddStringCalculatorTest.kt} (75%) diff --git a/src/test/kotlin/calculator/domain/CalculatorTest.kt b/src/test/kotlin/calculator/domain/AddStringCalculatorTest.kt similarity index 75% rename from src/test/kotlin/calculator/domain/CalculatorTest.kt rename to src/test/kotlin/calculator/domain/AddStringCalculatorTest.kt index 635e7b2ab5..ce08aa3ad9 100644 --- a/src/test/kotlin/calculator/domain/CalculatorTest.kt +++ b/src/test/kotlin/calculator/domain/AddStringCalculatorTest.kt @@ -3,16 +3,16 @@ package calculator.domain import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.Test -internal class CalculatorTest { +internal class AddStringCalculatorTest { @Test fun `수식을 이용하여 계산에 성공한다`() { // given val expression = Expression.create("2,2,2,2", Delimiters()) - val calculator = Calculator(expression) + val calculator = AddStringCalculator() // when - val actual = calculator.calculate() + val actual = calculator.calculate(expression) // then val expected = 8 @@ -25,7 +25,7 @@ internal class CalculatorTest { val expression = Expression.create("", Delimiters()) // when - val actual = Calculator(expression).calculate() + val actual = AddStringCalculator().calculate(expression) // then val expected = 0 From 06e2425ce54cdbe825f1b82c93493734b29619ba Mon Sep 17 00:00:00 2001 From: ChoiYounho Date: Mon, 19 Dec 2022 17:48:17 +0900 Subject: [PATCH 46/74] =?UTF-8?q?refactor=20:=20=EA=B5=AC=EB=B6=84?= =?UTF-8?q?=EC=9E=90=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BD=94=EB=93=9C=20?= =?UTF-8?q?=EB=A6=AC=ED=8C=A9=ED=86=A0=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../calculator/domain/DelimitersTest.kt | 44 +++++++++++++------ 1 file changed, 30 insertions(+), 14 deletions(-) diff --git a/src/test/kotlin/calculator/domain/DelimitersTest.kt b/src/test/kotlin/calculator/domain/DelimitersTest.kt index 5478d6365c..2322150016 100644 --- a/src/test/kotlin/calculator/domain/DelimitersTest.kt +++ b/src/test/kotlin/calculator/domain/DelimitersTest.kt @@ -1,32 +1,48 @@ package calculator.domain -import org.assertj.core.api.Assertions.assertThat +import org.assertj.core.api.AssertionsForClassTypes.assertThat +import org.junit.jupiter.api.DisplayName import org.junit.jupiter.api.Test -import org.junit.jupiter.api.assertThrows internal class DelimitersTest { @Test - fun `문자열 구분자를 이용히여 연산자로 변환한다`() { + fun `콤마와 콜론을 가지고 있는 구분자를 생성한다`() { // given - val input = "//;\n1;2;3" + val input = "" // when - val actual = Delimiters.create(input).toOperator(";") + val actual = Delimiters.create(input).value.size // then - val expected = Operator.PLUS + val expected = 2 assertThat(actual).isEqualTo(expected) } @Test - fun `문자열 구분자를 이용하여 연산자 변환에 실패한다`() { - assertThrows { - // given - val input = "//;\n1;2;3" - - // when - val actual = Delimiters.create(input).toOperator("ㅇ") - } + fun `콤마와 콜론 외에 다른 문자도 포함된 구분자를 생성한다`() { + // given + val input = "//;\n1;2;-3" + + // when + val actual = Delimiters.create(input).value.size + + // then + val expected = 3 + assertThat(actual).isEqualTo(expected) + } + + @Test + @DisplayName("//로 시작하여 \n로 끝나는 부분을 제거한다.") + fun `입력값에 커스텀 구분자 추가하는 부분을 제거한다`() { + // given + val input = "//;\n1;2;-3" + + // when + val actual = Delimiters().removeCustomRegex(input) + + // then + val expected = "1;2;-3" + assertThat(actual).isEqualTo(expected) } } From 77ec270013487edb32b013a70be92f9dda58c8b3 Mon Sep 17 00:00:00 2001 From: ChoiYounho Date: Mon, 19 Dec 2022 17:48:22 +0900 Subject: [PATCH 47/74] =?UTF-8?q?refactor=20:=20=EC=88=98=EC=8B=9D=20?= =?UTF-8?q?=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BD=94=EB=93=9C=20=EB=A6=AC?= =?UTF-8?q?=ED=8C=A9=ED=86=A0=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kotlin/calculator/domain/ExpressionTest.kt | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/test/kotlin/calculator/domain/ExpressionTest.kt b/src/test/kotlin/calculator/domain/ExpressionTest.kt index cc36097bfa..e0103e2852 100644 --- a/src/test/kotlin/calculator/domain/ExpressionTest.kt +++ b/src/test/kotlin/calculator/domain/ExpressionTest.kt @@ -37,5 +37,20 @@ internal class ExpressionTest { } } + @Test + fun `음수가 입력되어 수식 만들기에 실패`() { + assertThrows { + // given + val input = "//;\n1;2;-3" + val delimiters = Delimiters.create(input) + + // when + val expression = Expression.create(input, delimiters) + val actual = expression.value.size + // then + val expected = 5 + assertThat(actual).isEqualTo(expected) + } + } } From a54c059ac56fb5c393f3c23d2a3caee12e1ac86c Mon Sep 17 00:00:00 2001 From: Choiyounho Date: Sun, 25 Dec 2022 17:33:56 +0900 Subject: [PATCH 48/74] =?UTF-8?q?feat=20:=202=EB=8B=A8=EA=B3=84=20?= =?UTF-8?q?=EB=A1=9C=EB=98=90=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kotlin/calculator/domain/Calculate.kt | 2 +- .../calculator/extensions/StringExtensions.kt | 2 +- .../kotlin/calculator/util/ContractUtil.kt | 3 +- src/main/kotlin/lotto/LottoApp.kt | 34 ++++++++++++++ src/main/kotlin/lotto/domain/BuyPrice.kt | 12 +++++ src/main/kotlin/lotto/domain/Lotto.kt | 14 ++++++ src/main/kotlin/lotto/domain/LottoNumber.kt | 3 ++ src/main/kotlin/lotto/domain/ProfitRate.kt | 31 +++++++++++++ src/main/kotlin/lotto/domain/Ranking.kt | 23 ++++++++++ .../kotlin/lotto/domain/WinLotteryNumber.kt | 30 ++++++++++++ .../domain/strategy/LottoAutoStrategy.kt | 33 +++++++++++++ .../lotto/domain/strategy/LottoGenerator.kt | 10 ++++ .../domain/strategy/LottoGeneratorStrategy.kt | 8 ++++ src/main/kotlin/lotto/ui/InputViews.kt | 23 ++++++++++ src/main/kotlin/lotto/ui/OutputViews.kt | 39 ++++++++++++++++ src/test/kotlin/lotto/domain/BuyPriceTest.kt | 21 +++++++++ .../kotlin/lotto/domain/ProfitRateTest.kt | 27 +++++++++++ src/test/kotlin/lotto/domain/RankingTest.kt | 46 +++++++++++++++++++ .../lotto/domain/WinLotteryNumberTest.kt | 42 +++++++++++++++++ .../domain/strategy/LottoAutoStrategyTest.kt | 35 ++++++++++++++ 20 files changed, 434 insertions(+), 4 deletions(-) create mode 100644 src/main/kotlin/lotto/LottoApp.kt create mode 100644 src/main/kotlin/lotto/domain/BuyPrice.kt create mode 100644 src/main/kotlin/lotto/domain/Lotto.kt create mode 100644 src/main/kotlin/lotto/domain/LottoNumber.kt create mode 100644 src/main/kotlin/lotto/domain/ProfitRate.kt create mode 100644 src/main/kotlin/lotto/domain/Ranking.kt create mode 100644 src/main/kotlin/lotto/domain/WinLotteryNumber.kt create mode 100644 src/main/kotlin/lotto/domain/strategy/LottoAutoStrategy.kt create mode 100644 src/main/kotlin/lotto/domain/strategy/LottoGenerator.kt create mode 100644 src/main/kotlin/lotto/domain/strategy/LottoGeneratorStrategy.kt create mode 100644 src/main/kotlin/lotto/ui/InputViews.kt create mode 100644 src/main/kotlin/lotto/ui/OutputViews.kt create mode 100644 src/test/kotlin/lotto/domain/BuyPriceTest.kt create mode 100644 src/test/kotlin/lotto/domain/ProfitRateTest.kt create mode 100644 src/test/kotlin/lotto/domain/RankingTest.kt create mode 100644 src/test/kotlin/lotto/domain/WinLotteryNumberTest.kt create mode 100644 src/test/kotlin/lotto/domain/strategy/LottoAutoStrategyTest.kt diff --git a/src/main/kotlin/calculator/domain/Calculate.kt b/src/main/kotlin/calculator/domain/Calculate.kt index 45000cad76..86a4a3e3d1 100644 --- a/src/main/kotlin/calculator/domain/Calculate.kt +++ b/src/main/kotlin/calculator/domain/Calculate.kt @@ -3,4 +3,4 @@ package calculator.domain interface Calculate { fun calculate(expression: Expression): Int -} \ No newline at end of file +} diff --git a/src/main/kotlin/calculator/extensions/StringExtensions.kt b/src/main/kotlin/calculator/extensions/StringExtensions.kt index bc9f6760a1..c592c51b18 100644 --- a/src/main/kotlin/calculator/extensions/StringExtensions.kt +++ b/src/main/kotlin/calculator/extensions/StringExtensions.kt @@ -6,4 +6,4 @@ fun String.isNumeric(): Boolean { fun String.split(list: List): List { return split(delimiters = list.toTypedArray()) -} \ No newline at end of file +} diff --git a/src/main/kotlin/calculator/util/ContractUtil.kt b/src/main/kotlin/calculator/util/ContractUtil.kt index bd4bcd98e1..f07197e7d2 100644 --- a/src/main/kotlin/calculator/util/ContractUtil.kt +++ b/src/main/kotlin/calculator/util/ContractUtil.kt @@ -1,9 +1,8 @@ package calculator.util - inline fun validate(value: Boolean, lazyMessage: () -> Any) { if (value.not()) { val message = lazyMessage() throw RuntimeException(message.toString()) } -} \ No newline at end of file +} diff --git a/src/main/kotlin/lotto/LottoApp.kt b/src/main/kotlin/lotto/LottoApp.kt new file mode 100644 index 0000000000..e6d32de621 --- /dev/null +++ b/src/main/kotlin/lotto/LottoApp.kt @@ -0,0 +1,34 @@ +package lotto + +import lotto.domain.BuyPrice +import lotto.domain.ProfitRate +import lotto.domain.WinLotteryNumber +import lotto.domain.strategy.LottoAutoStrategy +import lotto.domain.strategy.LottoGenerator +import lotto.ui.InputViews.inputPrice +import lotto.ui.InputViews.inputWinningNumber +import lotto.ui.InputViews.printBoughtLotto +import lotto.ui.OutputViews.printBoughtLottos +import lotto.ui.OutputViews.printLottoMatchResult +import lotto.ui.OutputViews.printProfitRate + +fun main() { + val inputPrice = inputPrice() ?: 0 + + val buyPrice = BuyPrice(inputPrice) + val lottoCount = buyPrice.getLottoCount() + printBoughtLotto(lottoCount) + + val lottoGenerator = LottoGenerator() + val generateStrategy = LottoAutoStrategy() + + val lotto = lottoGenerator.generate(lottoCount, generateStrategy) + + printBoughtLottos(lotto.value) + + val winningNumbers = WinLotteryNumber(inputWinningNumber()).winningNumbers + val matching = lotto.matchWinningNumbers(winningNumbers) + + printLottoMatchResult(matching) + printProfitRate(ProfitRate(matching, lottoCount).calculate()) +} diff --git a/src/main/kotlin/lotto/domain/BuyPrice.kt b/src/main/kotlin/lotto/domain/BuyPrice.kt new file mode 100644 index 0000000000..7725cf0185 --- /dev/null +++ b/src/main/kotlin/lotto/domain/BuyPrice.kt @@ -0,0 +1,12 @@ +package lotto.domain + +class BuyPrice( + private val price: Int +) { + + fun getLottoCount() = price.div(LOTTO_PRICE) + + companion object { + private const val LOTTO_PRICE = 1000 + } +} diff --git a/src/main/kotlin/lotto/domain/Lotto.kt b/src/main/kotlin/lotto/domain/Lotto.kt new file mode 100644 index 0000000000..ca61559c9c --- /dev/null +++ b/src/main/kotlin/lotto/domain/Lotto.kt @@ -0,0 +1,14 @@ +package lotto.domain + +class Lotto(val value: List) { + + fun matchWinningNumbers(winningNumbers: List): Map { + return value.groupBy { lottoNumber -> + lottoNumber.value.count { + winningNumbers.contains(it) + } + } + .map { Pair(Ranking.valueOf(it.key), it.value.size) } + .toMap() + } +} diff --git a/src/main/kotlin/lotto/domain/LottoNumber.kt b/src/main/kotlin/lotto/domain/LottoNumber.kt new file mode 100644 index 0000000000..f871f30dfd --- /dev/null +++ b/src/main/kotlin/lotto/domain/LottoNumber.kt @@ -0,0 +1,3 @@ +package lotto.domain + +class LottoNumber(val value: List) diff --git a/src/main/kotlin/lotto/domain/ProfitRate.kt b/src/main/kotlin/lotto/domain/ProfitRate.kt new file mode 100644 index 0000000000..94cb6455fd --- /dev/null +++ b/src/main/kotlin/lotto/domain/ProfitRate.kt @@ -0,0 +1,31 @@ +package lotto.domain + +import java.math.RoundingMode +import java.text.DecimalFormat + +class ProfitRate( + private val matchResult: Map, + private val inputPrice: Int +) { + + fun calculate(): Double { + val totalWinningMoney = getTotalWinningMoney() + return calculate(totalWinningMoney) + } + + private fun getTotalWinningMoney(): Int { + return matchResult.entries + .sumOf { it.key.getTotalWinningMoney(it.value) } + } + + private fun calculate(totalWinningMoney: Int): Double { + val decimalFormat = DecimalFormat(PROFIT_RATE_PATTERN) + decimalFormat.roundingMode = RoundingMode.DOWN + val profitRate = totalWinningMoney / inputPrice.toFloat() + return decimalFormat.format(profitRate).toDouble() + } + + companion object { + private const val PROFIT_RATE_PATTERN = "#.##" + } +} diff --git a/src/main/kotlin/lotto/domain/Ranking.kt b/src/main/kotlin/lotto/domain/Ranking.kt new file mode 100644 index 0000000000..e6665e96c5 --- /dev/null +++ b/src/main/kotlin/lotto/domain/Ranking.kt @@ -0,0 +1,23 @@ +package lotto.domain + +enum class Ranking( + val matchCount: Int, + val winningMoney: Int +) { + + FIRST(6, 2_000_000_000), + SECOND(5, 1_500_000), + THIRD(4, 50_000), + FOURTH(3, 5_000), + MISS(0, 0); + + fun getTotalWinningMoney(matchResultValue: Int): Int { + return winningMoney * matchResultValue + } + + companion object { + fun valueOf(matchCount: Int): Ranking { + return values().find { it.matchCount == matchCount } ?: MISS + } + } +} diff --git a/src/main/kotlin/lotto/domain/WinLotteryNumber.kt b/src/main/kotlin/lotto/domain/WinLotteryNumber.kt new file mode 100644 index 0000000000..26c760a5be --- /dev/null +++ b/src/main/kotlin/lotto/domain/WinLotteryNumber.kt @@ -0,0 +1,30 @@ +package lotto.domain + +class WinLotteryNumber( + inputNumbers: List +) { + + val winningNumbers = inputNumbers.map { it.toInt() } + + init { + if (winningNumbers.isEmpty() || winningNumbers.size != LIMIT_WINNING_NUMBER) { + throw IllegalArgumentException("당첨 번호는 6개 입니다.") + } + validateWinningNumber() + } + + private fun validateWinningNumber() { + for (winningNumber in winningNumbers) { + if (winningNumber !in LOTTO_FIRST_NUMBER..LOTTO_LAST_NUMBER) { + throw IllegalArgumentException("당첨 번호는 1 ~ 45 범위의 숫자로만 구성될 수 있습니다.") + } + } + } + + companion object { + private const val LIMIT_WINNING_NUMBER = 6 + + private const val LOTTO_FIRST_NUMBER = 1 + private const val LOTTO_LAST_NUMBER = 45 + } +} diff --git a/src/main/kotlin/lotto/domain/strategy/LottoAutoStrategy.kt b/src/main/kotlin/lotto/domain/strategy/LottoAutoStrategy.kt new file mode 100644 index 0000000000..e8c725f719 --- /dev/null +++ b/src/main/kotlin/lotto/domain/strategy/LottoAutoStrategy.kt @@ -0,0 +1,33 @@ +package lotto.domain.strategy + +import lotto.domain.Lotto +import lotto.domain.LottoNumber + +class LottoAutoStrategy : LottoGeneratorStrategy { + + private val lottoNumbers = (LOTTO_FIRST_NUMBER..LOTTO_LAST_NUMBER).toList() + + override fun generate(lottoCount: Int): Lotto { + return Lotto( + (0 until lottoCount) + .map { generateLottoNumber() } + .toList() + ) + } + + private fun generateLottoNumber(): LottoNumber { + val lottoNumbers = lottoNumbers + .shuffled() + .subList(LOTTO_FIRST_INDEX, LOTTO_LAST_INDEX) + .sorted() + .toList() + return LottoNumber(lottoNumbers) + } + + companion object { + private const val LOTTO_FIRST_NUMBER = 1 + private const val LOTTO_LAST_NUMBER = 45 + private const val LOTTO_FIRST_INDEX = 0 + private const val LOTTO_LAST_INDEX = 6 + } +} diff --git a/src/main/kotlin/lotto/domain/strategy/LottoGenerator.kt b/src/main/kotlin/lotto/domain/strategy/LottoGenerator.kt new file mode 100644 index 0000000000..94d48e5eb7 --- /dev/null +++ b/src/main/kotlin/lotto/domain/strategy/LottoGenerator.kt @@ -0,0 +1,10 @@ +package lotto.domain.strategy + +import lotto.domain.Lotto + +class LottoGenerator { + + fun generate(lottoCount: Int, lottoGeneratorStrategy: LottoGeneratorStrategy): Lotto { + return lottoGeneratorStrategy.generate(lottoCount) + } +} diff --git a/src/main/kotlin/lotto/domain/strategy/LottoGeneratorStrategy.kt b/src/main/kotlin/lotto/domain/strategy/LottoGeneratorStrategy.kt new file mode 100644 index 0000000000..62d7e83fd3 --- /dev/null +++ b/src/main/kotlin/lotto/domain/strategy/LottoGeneratorStrategy.kt @@ -0,0 +1,8 @@ +package lotto.domain.strategy + +import lotto.domain.Lotto + +interface LottoGeneratorStrategy { + + fun generate(lottoCount: Int): Lotto +} diff --git a/src/main/kotlin/lotto/ui/InputViews.kt b/src/main/kotlin/lotto/ui/InputViews.kt new file mode 100644 index 0000000000..2c1e49fd7f --- /dev/null +++ b/src/main/kotlin/lotto/ui/InputViews.kt @@ -0,0 +1,23 @@ +package lotto.ui + +object InputViews { + + private const val MESSAGE_INPUT_PRICE = "구입금액을 입력해 주세요." + private const val MESSAGE_BOUGHT_LOTTO = "개를 구매했습니다." + private const val MESSAGE_INPUT_WINNING_NUMBER = "지난 주 당첨 번호를 입력해 주세요." + private const val DELIMITERS = ", " + + fun inputPrice(): Int? { + println(MESSAGE_INPUT_PRICE) + return readLine()?.toIntOrNull() + } + + fun printBoughtLotto(lottoCount: Int) { + println("${lottoCount}$MESSAGE_BOUGHT_LOTTO") + } + + fun inputWinningNumber(): List { + println(MESSAGE_INPUT_WINNING_NUMBER) + return readLine()?.split(DELIMITERS) ?: emptyList() + } +} diff --git a/src/main/kotlin/lotto/ui/OutputViews.kt b/src/main/kotlin/lotto/ui/OutputViews.kt new file mode 100644 index 0000000000..a46a54fa81 --- /dev/null +++ b/src/main/kotlin/lotto/ui/OutputViews.kt @@ -0,0 +1,39 @@ +package lotto.ui + +import lotto.domain.LottoNumber +import lotto.domain.Ranking +import lotto.domain.Ranking.* + +object OutputViews { + + private const val SEPARATOR = ", " + private const val PREFIX = "[" + private const val POSTFIX = "]" + private const val PRINT_LOTTO_RESULT = "당첨 통계" + + fun printBoughtLottos(lotto: List) { + repeat(lotto.size) { + val lottery = lotto[it] + println(lottery.value.joinToString(SEPARATOR, PREFIX, POSTFIX)) + } + } + + fun printLottoMatchResult(matchResult: Map) { + println(PRINT_LOTTO_RESULT) + println("---------") + println("${FOURTH.matchCount}개 일치 (${FOURTH.winningMoney}원) - ${matchResult[FOURTH] ?: 0}개") + println("${THIRD.matchCount}개 일치 (${THIRD.winningMoney}원) - ${matchResult[THIRD] ?: 0}개") + println("${SECOND.matchCount}개 일치 (${SECOND.winningMoney}원) - ${matchResult[SECOND] ?: 0}개") + println("${FIRST.matchCount}개 일치 (${FIRST.winningMoney}원) - ${matchResult[FIRST] ?: 0}개") + } + + fun printProfitRate(profitRate: Double) { + val result = if (profitRate >= 1.0) { + "이득" + } else { + "손해" + } + + println("총 수익률은 ${profitRate}입니다. (기준이 1이기 때문에 결과적으로 ${result}라는 의미임)") + } +} diff --git a/src/test/kotlin/lotto/domain/BuyPriceTest.kt b/src/test/kotlin/lotto/domain/BuyPriceTest.kt new file mode 100644 index 0000000000..7418aa0ab3 --- /dev/null +++ b/src/test/kotlin/lotto/domain/BuyPriceTest.kt @@ -0,0 +1,21 @@ +package lotto.domain + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.CsvSource + +internal class BuyPriceTest { + + @ParameterizedTest + @CsvSource(value = ["1000,1", "3000,3", "14000,14"]) + fun `1000원 단위로 n장의 로또가 생성된다`(input: String, expected: String) { + // given + val buyPrice = BuyPrice(input.toInt()) + + // when + val actual = buyPrice.getLottoCount() + + // then + assertThat(actual).isEqualTo(expected.toInt()) + } +} diff --git a/src/test/kotlin/lotto/domain/ProfitRateTest.kt b/src/test/kotlin/lotto/domain/ProfitRateTest.kt new file mode 100644 index 0000000000..729bc2cb7a --- /dev/null +++ b/src/test/kotlin/lotto/domain/ProfitRateTest.kt @@ -0,0 +1,27 @@ +package lotto.domain + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test + +internal class ProfitRateTest { + + @Test + fun `구매금액 대비 당첨금액의 수익률을 계산한다`() { + // given + val matchResult = mapOf( + Ranking.FIRST to 0, + Ranking.SECOND to 0, + Ranking.THIRD to 0, + Ranking.FOURTH to 1 + ) + + val price = 14000 + + // when + val actual = ProfitRate(matchResult, price).calculate() + + // then + val expected = 0.35 + assertThat(actual).isEqualTo(expected) + } +} diff --git a/src/test/kotlin/lotto/domain/RankingTest.kt b/src/test/kotlin/lotto/domain/RankingTest.kt new file mode 100644 index 0000000000..1d81329581 --- /dev/null +++ b/src/test/kotlin/lotto/domain/RankingTest.kt @@ -0,0 +1,46 @@ +package lotto.domain + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test + +internal class RankingTest { + + @Test + fun `당첨된 개수에 맞는 랭킹 반환`() { + // given + val matchCount = 5 + + // when + val actual = Ranking.valueOf(matchCount) + + // then + val expected = Ranking.SECOND + assertThat(actual).isEqualTo(expected) + } + + @Test + fun `당첨 개수가 3개 미만일 경우 Miss 반환`() { + // given + val matchCount = 2 + + // when + val actual = Ranking.valueOf(matchCount) + + // then + val expected = Ranking.MISS + assertThat(actual).isEqualTo(expected) + } + + @Test + fun `당첨된 랭킹의 개수만큼 총 당첨 금액 반환`() { + // given + val matchResultValue = 3 + + // when + val actual = Ranking.SECOND.getTotalWinningMoney(matchResultValue) + + // then + val expected = 4500000 + assertThat(actual).isEqualTo(expected) + } +} diff --git a/src/test/kotlin/lotto/domain/WinLotteryNumberTest.kt b/src/test/kotlin/lotto/domain/WinLotteryNumberTest.kt new file mode 100644 index 0000000000..2cb567e14b --- /dev/null +++ b/src/test/kotlin/lotto/domain/WinLotteryNumberTest.kt @@ -0,0 +1,42 @@ +package lotto.domain + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.assertThrows + +internal class WinLotteryNumberTest { + + @Test + fun `당첨번호를 6개 입력한다`() { + // given + val inputWinningNumber = listOf("1", "2", "3", "4", "5", "6") + + // when + val actual = WinLotteryNumber(inputWinningNumber).winningNumbers.size + + // then + val expected = 6 + assertThat(actual).isEqualTo(expected) + } + + @Test + fun `당첨번호가 입력되지 않으면 예외가 발생한다`() { + assertThrows { + WinLotteryNumber(emptyList()) + } + } + + @Test + fun `당첨번호가 6개가 아니면 예외가 발생한다`() { + assertThrows { + WinLotteryNumber(listOf("1", "2")) + } + } + + @Test + fun `당첨번호가 1에서 45숫자가 아니면 예외가 발생한다`() { + assertThrows { + WinLotteryNumber(listOf("1", "2", "3", "4", "5", "46")) + } + } +} diff --git a/src/test/kotlin/lotto/domain/strategy/LottoAutoStrategyTest.kt b/src/test/kotlin/lotto/domain/strategy/LottoAutoStrategyTest.kt new file mode 100644 index 0000000000..05f1b45ee2 --- /dev/null +++ b/src/test/kotlin/lotto/domain/strategy/LottoAutoStrategyTest.kt @@ -0,0 +1,35 @@ +package lotto.domain.strategy + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource + +internal class LottoAutoStrategyTest { + + @ParameterizedTest + @ValueSource(ints = [1, 2, 5, 10]) + fun `로또를 구매한 개수만큼 로또가 만들어진다`(lottoCount: Int) { + // given + val autoLotto = LottoAutoStrategy() + + // when + val actual = autoLotto.generate(lottoCount).value.size + + // then + assertThat(actual).isEqualTo(lottoCount) + } + + @Test + fun `로또 번호의 범위는 1부터 45까지다`() { + // given + val autoLotto = LottoAutoStrategy() + + // when + val actual = autoLotto.generate(1).value[0].value + + // then + assertThat(actual.first()).isGreaterThanOrEqualTo(1) + assertThat(actual.last()).isLessThanOrEqualTo(45) + } +} From 7e1957e949c004f8a534614f4dadcff11aceac8b Mon Sep 17 00:00:00 2001 From: Choiyounho Date: Sat, 31 Dec 2022 22:00:37 +0900 Subject: [PATCH 49/74] =?UTF-8?q?refactor=20:=20div=20->=20/=20=EC=BD=94?= =?UTF-8?q?=EB=93=9C=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/domain/BuyPrice.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/kotlin/lotto/domain/BuyPrice.kt b/src/main/kotlin/lotto/domain/BuyPrice.kt index 7725cf0185..8d72cbd8e8 100644 --- a/src/main/kotlin/lotto/domain/BuyPrice.kt +++ b/src/main/kotlin/lotto/domain/BuyPrice.kt @@ -4,7 +4,7 @@ class BuyPrice( private val price: Int ) { - fun getLottoCount() = price.div(LOTTO_PRICE) + fun getLottoCount() = price / LOTTO_PRICE companion object { private const val LOTTO_PRICE = 1000 From 1d445281c65c3ad9008eb241b56250e1effa3769 Mon Sep 17 00:00:00 2001 From: Choiyounho Date: Sat, 31 Dec 2022 22:01:58 +0900 Subject: [PATCH 50/74] =?UTF-8?q?refactor=20:=20=EA=B5=AC=EB=A7=A4=20?= =?UTF-8?q?=EA=B0=80=EA=B2=A9=20=ED=81=B4=EB=9E=98=EC=8A=A4=20=EC=9D=B8?= =?UTF-8?q?=EB=9D=BC=EC=9D=B8=20=ED=81=B4=EB=9E=98=EC=8A=A4=EB=A1=9C=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/lotto/domain/BuyPrice.kt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/lotto/domain/BuyPrice.kt b/src/main/kotlin/lotto/domain/BuyPrice.kt index 8d72cbd8e8..653b7c3756 100644 --- a/src/main/kotlin/lotto/domain/BuyPrice.kt +++ b/src/main/kotlin/lotto/domain/BuyPrice.kt @@ -1,6 +1,7 @@ package lotto.domain -class BuyPrice( +@JvmInline +value class BuyPrice( private val price: Int ) { From 9332f39809c4619a8ad7c2a870cc38b4c744bfce Mon Sep 17 00:00:00 2001 From: Choiyounho Date: Sat, 31 Dec 2022 22:05:02 +0900 Subject: [PATCH 51/74] =?UTF-8?q?refactor=20:=20printBoughtLotto=20?= =?UTF-8?q?=ED=95=A8=EC=88=98=20OutputView=EB=A1=9C=20=EC=9D=B4=EB=8F=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/lotto/LottoApp.kt | 2 +- src/main/kotlin/lotto/ui/InputViews.kt | 10 +++------- src/main/kotlin/lotto/ui/OutputViews.kt | 5 +++++ 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/main/kotlin/lotto/LottoApp.kt b/src/main/kotlin/lotto/LottoApp.kt index e6d32de621..44226742bf 100644 --- a/src/main/kotlin/lotto/LottoApp.kt +++ b/src/main/kotlin/lotto/LottoApp.kt @@ -7,7 +7,7 @@ import lotto.domain.strategy.LottoAutoStrategy import lotto.domain.strategy.LottoGenerator import lotto.ui.InputViews.inputPrice import lotto.ui.InputViews.inputWinningNumber -import lotto.ui.InputViews.printBoughtLotto +import lotto.ui.OutputViews.printBoughtLotto import lotto.ui.OutputViews.printBoughtLottos import lotto.ui.OutputViews.printLottoMatchResult import lotto.ui.OutputViews.printProfitRate diff --git a/src/main/kotlin/lotto/ui/InputViews.kt b/src/main/kotlin/lotto/ui/InputViews.kt index 2c1e49fd7f..3e14d7440c 100644 --- a/src/main/kotlin/lotto/ui/InputViews.kt +++ b/src/main/kotlin/lotto/ui/InputViews.kt @@ -3,17 +3,13 @@ package lotto.ui object InputViews { private const val MESSAGE_INPUT_PRICE = "구입금액을 입력해 주세요." - private const val MESSAGE_BOUGHT_LOTTO = "개를 구매했습니다." private const val MESSAGE_INPUT_WINNING_NUMBER = "지난 주 당첨 번호를 입력해 주세요." private const val DELIMITERS = ", " + private const val DEFAULT_PRICE = 0 - fun inputPrice(): Int? { + fun inputPrice(): Int { println(MESSAGE_INPUT_PRICE) - return readLine()?.toIntOrNull() - } - - fun printBoughtLotto(lottoCount: Int) { - println("${lottoCount}$MESSAGE_BOUGHT_LOTTO") + return readLine()?.toIntOrNull() ?: DEFAULT_PRICE } fun inputWinningNumber(): List { diff --git a/src/main/kotlin/lotto/ui/OutputViews.kt b/src/main/kotlin/lotto/ui/OutputViews.kt index a46a54fa81..1f3c830645 100644 --- a/src/main/kotlin/lotto/ui/OutputViews.kt +++ b/src/main/kotlin/lotto/ui/OutputViews.kt @@ -10,6 +10,7 @@ object OutputViews { private const val PREFIX = "[" private const val POSTFIX = "]" private const val PRINT_LOTTO_RESULT = "당첨 통계" + private const val MESSAGE_BOUGHT_LOTTO = "개를 구매했습니다." fun printBoughtLottos(lotto: List) { repeat(lotto.size) { @@ -18,6 +19,10 @@ object OutputViews { } } + fun printBoughtLotto(lottoCount: Int) { + println("${lottoCount}${MESSAGE_BOUGHT_LOTTO}") + } + fun printLottoMatchResult(matchResult: Map) { println(PRINT_LOTTO_RESULT) println("---------") From c5a6e9e1cafcfd57d9d2872368826d694b9c4fea Mon Sep 17 00:00:00 2001 From: Choiyounho Date: Sat, 31 Dec 2022 22:06:02 +0900 Subject: [PATCH 52/74] =?UTF-8?q?refactor=20:=20=EB=A1=9C=EB=98=90=20?= =?UTF-8?q?=EC=9E=90=EB=8F=99=20=EC=83=9D=EC=84=B1=20=EC=A0=84=EB=9E=B5=20?= =?UTF-8?q?=ED=81=B4=EB=9E=98=EC=8A=A4=20=EB=84=A4=EC=9D=B4=EB=B0=8D=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/lotto/LottoApp.kt | 4 ++-- .../{LottoAutoStrategy.kt => LottoAutoGeneratorStrategy.kt} | 2 +- ...utoStrategyTest.kt => LottoAutoGeneratorStrategyTest.kt} | 6 +++--- 3 files changed, 6 insertions(+), 6 deletions(-) rename src/main/kotlin/lotto/domain/strategy/{LottoAutoStrategy.kt => LottoAutoGeneratorStrategy.kt} (93%) rename src/test/kotlin/lotto/domain/strategy/{LottoAutoStrategyTest.kt => LottoAutoGeneratorStrategyTest.kt} (84%) diff --git a/src/main/kotlin/lotto/LottoApp.kt b/src/main/kotlin/lotto/LottoApp.kt index 44226742bf..81560286a0 100644 --- a/src/main/kotlin/lotto/LottoApp.kt +++ b/src/main/kotlin/lotto/LottoApp.kt @@ -3,7 +3,7 @@ package lotto import lotto.domain.BuyPrice import lotto.domain.ProfitRate import lotto.domain.WinLotteryNumber -import lotto.domain.strategy.LottoAutoStrategy +import lotto.domain.strategy.LottoAutoGeneratorStrategy import lotto.domain.strategy.LottoGenerator import lotto.ui.InputViews.inputPrice import lotto.ui.InputViews.inputWinningNumber @@ -20,7 +20,7 @@ fun main() { printBoughtLotto(lottoCount) val lottoGenerator = LottoGenerator() - val generateStrategy = LottoAutoStrategy() + val generateStrategy = LottoAutoGeneratorStrategy() val lotto = lottoGenerator.generate(lottoCount, generateStrategy) diff --git a/src/main/kotlin/lotto/domain/strategy/LottoAutoStrategy.kt b/src/main/kotlin/lotto/domain/strategy/LottoAutoGeneratorStrategy.kt similarity index 93% rename from src/main/kotlin/lotto/domain/strategy/LottoAutoStrategy.kt rename to src/main/kotlin/lotto/domain/strategy/LottoAutoGeneratorStrategy.kt index e8c725f719..384b940d1e 100644 --- a/src/main/kotlin/lotto/domain/strategy/LottoAutoStrategy.kt +++ b/src/main/kotlin/lotto/domain/strategy/LottoAutoGeneratorStrategy.kt @@ -3,7 +3,7 @@ package lotto.domain.strategy import lotto.domain.Lotto import lotto.domain.LottoNumber -class LottoAutoStrategy : LottoGeneratorStrategy { +class LottoAutoGeneratorStrategy : LottoGeneratorStrategy { private val lottoNumbers = (LOTTO_FIRST_NUMBER..LOTTO_LAST_NUMBER).toList() diff --git a/src/test/kotlin/lotto/domain/strategy/LottoAutoStrategyTest.kt b/src/test/kotlin/lotto/domain/strategy/LottoAutoGeneratorStrategyTest.kt similarity index 84% rename from src/test/kotlin/lotto/domain/strategy/LottoAutoStrategyTest.kt rename to src/test/kotlin/lotto/domain/strategy/LottoAutoGeneratorStrategyTest.kt index 05f1b45ee2..66dda6ec53 100644 --- a/src/test/kotlin/lotto/domain/strategy/LottoAutoStrategyTest.kt +++ b/src/test/kotlin/lotto/domain/strategy/LottoAutoGeneratorStrategyTest.kt @@ -5,13 +5,13 @@ import org.junit.jupiter.api.Test import org.junit.jupiter.params.ParameterizedTest import org.junit.jupiter.params.provider.ValueSource -internal class LottoAutoStrategyTest { +internal class LottoAutoGeneratorStrategyTest { @ParameterizedTest @ValueSource(ints = [1, 2, 5, 10]) fun `로또를 구매한 개수만큼 로또가 만들어진다`(lottoCount: Int) { // given - val autoLotto = LottoAutoStrategy() + val autoLotto = LottoAutoGeneratorStrategy() // when val actual = autoLotto.generate(lottoCount).value.size @@ -23,7 +23,7 @@ internal class LottoAutoStrategyTest { @Test fun `로또 번호의 범위는 1부터 45까지다`() { // given - val autoLotto = LottoAutoStrategy() + val autoLotto = LottoAutoGeneratorStrategy() // when val actual = autoLotto.generate(1).value[0].value From eb0baa960def82cb0eb0670a0a1a3124d0f52c1a Mon Sep 17 00:00:00 2001 From: Choiyounho Date: Sat, 31 Dec 2022 22:06:46 +0900 Subject: [PATCH 53/74] =?UTF-8?q?refactor=20:=20LottoGenerator=20->=20Lott?= =?UTF-8?q?oFactory=20=EB=84=A4=EC=9D=B4=EB=B0=8D=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/lotto/LottoApp.kt | 6 +++--- .../domain/strategy/{LottoGenerator.kt => LottoFactory.kt} | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) rename src/main/kotlin/lotto/domain/strategy/{LottoGenerator.kt => LottoFactory.kt} (90%) diff --git a/src/main/kotlin/lotto/LottoApp.kt b/src/main/kotlin/lotto/LottoApp.kt index 81560286a0..effd2c6216 100644 --- a/src/main/kotlin/lotto/LottoApp.kt +++ b/src/main/kotlin/lotto/LottoApp.kt @@ -4,7 +4,7 @@ import lotto.domain.BuyPrice import lotto.domain.ProfitRate import lotto.domain.WinLotteryNumber import lotto.domain.strategy.LottoAutoGeneratorStrategy -import lotto.domain.strategy.LottoGenerator +import lotto.domain.strategy.LottoFactory import lotto.ui.InputViews.inputPrice import lotto.ui.InputViews.inputWinningNumber import lotto.ui.OutputViews.printBoughtLotto @@ -19,10 +19,10 @@ fun main() { val lottoCount = buyPrice.getLottoCount() printBoughtLotto(lottoCount) - val lottoGenerator = LottoGenerator() + val lottoFactory = LottoFactory() val generateStrategy = LottoAutoGeneratorStrategy() - val lotto = lottoGenerator.generate(lottoCount, generateStrategy) + val lotto = lottoFactory.generate(lottoCount, generateStrategy) printBoughtLottos(lotto.value) diff --git a/src/main/kotlin/lotto/domain/strategy/LottoGenerator.kt b/src/main/kotlin/lotto/domain/strategy/LottoFactory.kt similarity index 90% rename from src/main/kotlin/lotto/domain/strategy/LottoGenerator.kt rename to src/main/kotlin/lotto/domain/strategy/LottoFactory.kt index 94d48e5eb7..f9e00813ac 100644 --- a/src/main/kotlin/lotto/domain/strategy/LottoGenerator.kt +++ b/src/main/kotlin/lotto/domain/strategy/LottoFactory.kt @@ -2,7 +2,7 @@ package lotto.domain.strategy import lotto.domain.Lotto -class LottoGenerator { +class LottoFactory { fun generate(lottoCount: Int, lottoGeneratorStrategy: LottoGeneratorStrategy): Lotto { return lottoGeneratorStrategy.generate(lottoCount) From 0bb5bcee7184afdc5874d1c0986da2b665b89a5f Mon Sep 17 00:00:00 2001 From: Choiyounho Date: Sat, 31 Dec 2022 22:07:41 +0900 Subject: [PATCH 54/74] =?UTF-8?q?refactor=20:=20WinningLotteryNumber=20->?= =?UTF-8?q?=20WinningLotto=20=EB=84=A4=EC=9D=B4=EB=B0=8D=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/lotto/LottoApp.kt | 4 ++-- .../domain/{WinLotteryNumber.kt => WinningLotto.kt} | 2 +- .../{WinLotteryNumberTest.kt => WinningLottoTest.kt} | 10 +++++----- 3 files changed, 8 insertions(+), 8 deletions(-) rename src/main/kotlin/lotto/domain/{WinLotteryNumber.kt => WinningLotto.kt} (97%) rename src/test/kotlin/lotto/domain/{WinLotteryNumberTest.kt => WinningLottoTest.kt} (76%) diff --git a/src/main/kotlin/lotto/LottoApp.kt b/src/main/kotlin/lotto/LottoApp.kt index effd2c6216..fd3a7f5d3e 100644 --- a/src/main/kotlin/lotto/LottoApp.kt +++ b/src/main/kotlin/lotto/LottoApp.kt @@ -2,7 +2,7 @@ package lotto import lotto.domain.BuyPrice import lotto.domain.ProfitRate -import lotto.domain.WinLotteryNumber +import lotto.domain.WinningLotto import lotto.domain.strategy.LottoAutoGeneratorStrategy import lotto.domain.strategy.LottoFactory import lotto.ui.InputViews.inputPrice @@ -26,7 +26,7 @@ fun main() { printBoughtLottos(lotto.value) - val winningNumbers = WinLotteryNumber(inputWinningNumber()).winningNumbers + val winningNumbers = WinningLotto(inputWinningNumber()).winningNumbers val matching = lotto.matchWinningNumbers(winningNumbers) printLottoMatchResult(matching) diff --git a/src/main/kotlin/lotto/domain/WinLotteryNumber.kt b/src/main/kotlin/lotto/domain/WinningLotto.kt similarity index 97% rename from src/main/kotlin/lotto/domain/WinLotteryNumber.kt rename to src/main/kotlin/lotto/domain/WinningLotto.kt index 26c760a5be..bae735bc31 100644 --- a/src/main/kotlin/lotto/domain/WinLotteryNumber.kt +++ b/src/main/kotlin/lotto/domain/WinningLotto.kt @@ -1,6 +1,6 @@ package lotto.domain -class WinLotteryNumber( +class WinningLotto( inputNumbers: List ) { diff --git a/src/test/kotlin/lotto/domain/WinLotteryNumberTest.kt b/src/test/kotlin/lotto/domain/WinningLottoTest.kt similarity index 76% rename from src/test/kotlin/lotto/domain/WinLotteryNumberTest.kt rename to src/test/kotlin/lotto/domain/WinningLottoTest.kt index 2cb567e14b..8b3f68e2f0 100644 --- a/src/test/kotlin/lotto/domain/WinLotteryNumberTest.kt +++ b/src/test/kotlin/lotto/domain/WinningLottoTest.kt @@ -4,7 +4,7 @@ import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.Test import org.junit.jupiter.api.assertThrows -internal class WinLotteryNumberTest { +internal class WinningLottoTest { @Test fun `당첨번호를 6개 입력한다`() { @@ -12,7 +12,7 @@ internal class WinLotteryNumberTest { val inputWinningNumber = listOf("1", "2", "3", "4", "5", "6") // when - val actual = WinLotteryNumber(inputWinningNumber).winningNumbers.size + val actual = WinningLotto(inputWinningNumber).winningNumbers.size // then val expected = 6 @@ -22,21 +22,21 @@ internal class WinLotteryNumberTest { @Test fun `당첨번호가 입력되지 않으면 예외가 발생한다`() { assertThrows { - WinLotteryNumber(emptyList()) + WinningLotto(emptyList()) } } @Test fun `당첨번호가 6개가 아니면 예외가 발생한다`() { assertThrows { - WinLotteryNumber(listOf("1", "2")) + WinningLotto(listOf("1", "2")) } } @Test fun `당첨번호가 1에서 45숫자가 아니면 예외가 발생한다`() { assertThrows { - WinLotteryNumber(listOf("1", "2", "3", "4", "5", "46")) + WinningLotto(listOf("1", "2", "3", "4", "5", "46")) } } } From ede278b4002a3a2c7e14a49d88ca7d3e0f137c11 Mon Sep 17 00:00:00 2001 From: Choiyounho Date: Sat, 31 Dec 2022 22:08:50 +0900 Subject: [PATCH 55/74] =?UTF-8?q?refactor=20:=20Lotto,=20LottoNumber=20=20?= =?UTF-8?q?=EB=84=A4=EC=9D=B4=EB=B0=8D=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/lotto/domain/Lotto.kt | 13 +------------ src/main/kotlin/lotto/domain/LottoNumber.kt | 3 --- src/main/kotlin/lotto/domain/Lottos.kt | 14 ++++++++++++++ .../domain/strategy/LottoAutoGeneratorStrategy.kt | 10 +++++----- .../kotlin/lotto/domain/strategy/LottoFactory.kt | 4 ++-- .../domain/strategy/LottoGeneratorStrategy.kt | 4 ++-- src/main/kotlin/lotto/ui/OutputViews.kt | 4 ++-- .../{WinningLottoTest.kt => WinningLottosTest.kt} | 2 +- ...yTest.kt => LottosAutoGeneratorStrategyTest.kt} | 2 +- 9 files changed, 28 insertions(+), 28 deletions(-) delete mode 100644 src/main/kotlin/lotto/domain/LottoNumber.kt create mode 100644 src/main/kotlin/lotto/domain/Lottos.kt rename src/test/kotlin/lotto/domain/{WinningLottoTest.kt => WinningLottosTest.kt} (96%) rename src/test/kotlin/lotto/domain/strategy/{LottoAutoGeneratorStrategyTest.kt => LottosAutoGeneratorStrategyTest.kt} (95%) diff --git a/src/main/kotlin/lotto/domain/Lotto.kt b/src/main/kotlin/lotto/domain/Lotto.kt index ca61559c9c..bcad070f34 100644 --- a/src/main/kotlin/lotto/domain/Lotto.kt +++ b/src/main/kotlin/lotto/domain/Lotto.kt @@ -1,14 +1,3 @@ package lotto.domain -class Lotto(val value: List) { - - fun matchWinningNumbers(winningNumbers: List): Map { - return value.groupBy { lottoNumber -> - lottoNumber.value.count { - winningNumbers.contains(it) - } - } - .map { Pair(Ranking.valueOf(it.key), it.value.size) } - .toMap() - } -} +class Lotto(val value: List) diff --git a/src/main/kotlin/lotto/domain/LottoNumber.kt b/src/main/kotlin/lotto/domain/LottoNumber.kt deleted file mode 100644 index f871f30dfd..0000000000 --- a/src/main/kotlin/lotto/domain/LottoNumber.kt +++ /dev/null @@ -1,3 +0,0 @@ -package lotto.domain - -class LottoNumber(val value: List) diff --git a/src/main/kotlin/lotto/domain/Lottos.kt b/src/main/kotlin/lotto/domain/Lottos.kt new file mode 100644 index 0000000000..9693eb04cc --- /dev/null +++ b/src/main/kotlin/lotto/domain/Lottos.kt @@ -0,0 +1,14 @@ +package lotto.domain + +class Lottos(val value: List) { + + fun matchWinningNumbers(winningNumbers: List): Map { + return value.groupBy { lottoNumber -> + lottoNumber.value.count { + winningNumbers.contains(it) + } + } + .map { Pair(Ranking.valueOf(it.key), it.value.size) } + .toMap() + } +} diff --git a/src/main/kotlin/lotto/domain/strategy/LottoAutoGeneratorStrategy.kt b/src/main/kotlin/lotto/domain/strategy/LottoAutoGeneratorStrategy.kt index 384b940d1e..cf53a25bb4 100644 --- a/src/main/kotlin/lotto/domain/strategy/LottoAutoGeneratorStrategy.kt +++ b/src/main/kotlin/lotto/domain/strategy/LottoAutoGeneratorStrategy.kt @@ -1,27 +1,27 @@ package lotto.domain.strategy +import lotto.domain.Lottos import lotto.domain.Lotto -import lotto.domain.LottoNumber class LottoAutoGeneratorStrategy : LottoGeneratorStrategy { private val lottoNumbers = (LOTTO_FIRST_NUMBER..LOTTO_LAST_NUMBER).toList() - override fun generate(lottoCount: Int): Lotto { - return Lotto( + override fun generate(lottoCount: Int): Lottos { + return Lottos( (0 until lottoCount) .map { generateLottoNumber() } .toList() ) } - private fun generateLottoNumber(): LottoNumber { + private fun generateLottoNumber(): Lotto { val lottoNumbers = lottoNumbers .shuffled() .subList(LOTTO_FIRST_INDEX, LOTTO_LAST_INDEX) .sorted() .toList() - return LottoNumber(lottoNumbers) + return Lotto(lottoNumbers) } companion object { diff --git a/src/main/kotlin/lotto/domain/strategy/LottoFactory.kt b/src/main/kotlin/lotto/domain/strategy/LottoFactory.kt index f9e00813ac..db90bf6437 100644 --- a/src/main/kotlin/lotto/domain/strategy/LottoFactory.kt +++ b/src/main/kotlin/lotto/domain/strategy/LottoFactory.kt @@ -1,10 +1,10 @@ package lotto.domain.strategy -import lotto.domain.Lotto +import lotto.domain.Lottos class LottoFactory { - fun generate(lottoCount: Int, lottoGeneratorStrategy: LottoGeneratorStrategy): Lotto { + fun generate(lottoCount: Int, lottoGeneratorStrategy: LottoGeneratorStrategy): Lottos { return lottoGeneratorStrategy.generate(lottoCount) } } diff --git a/src/main/kotlin/lotto/domain/strategy/LottoGeneratorStrategy.kt b/src/main/kotlin/lotto/domain/strategy/LottoGeneratorStrategy.kt index 62d7e83fd3..198a10e520 100644 --- a/src/main/kotlin/lotto/domain/strategy/LottoGeneratorStrategy.kt +++ b/src/main/kotlin/lotto/domain/strategy/LottoGeneratorStrategy.kt @@ -1,8 +1,8 @@ package lotto.domain.strategy -import lotto.domain.Lotto +import lotto.domain.Lottos interface LottoGeneratorStrategy { - fun generate(lottoCount: Int): Lotto + fun generate(lottoCount: Int): Lottos } diff --git a/src/main/kotlin/lotto/ui/OutputViews.kt b/src/main/kotlin/lotto/ui/OutputViews.kt index 1f3c830645..b1709f11a7 100644 --- a/src/main/kotlin/lotto/ui/OutputViews.kt +++ b/src/main/kotlin/lotto/ui/OutputViews.kt @@ -1,6 +1,6 @@ package lotto.ui -import lotto.domain.LottoNumber +import lotto.domain.Lotto import lotto.domain.Ranking import lotto.domain.Ranking.* @@ -12,7 +12,7 @@ object OutputViews { private const val PRINT_LOTTO_RESULT = "당첨 통계" private const val MESSAGE_BOUGHT_LOTTO = "개를 구매했습니다." - fun printBoughtLottos(lotto: List) { + fun printBoughtLottos(lotto: List) { repeat(lotto.size) { val lottery = lotto[it] println(lottery.value.joinToString(SEPARATOR, PREFIX, POSTFIX)) diff --git a/src/test/kotlin/lotto/domain/WinningLottoTest.kt b/src/test/kotlin/lotto/domain/WinningLottosTest.kt similarity index 96% rename from src/test/kotlin/lotto/domain/WinningLottoTest.kt rename to src/test/kotlin/lotto/domain/WinningLottosTest.kt index 8b3f68e2f0..728e646c5a 100644 --- a/src/test/kotlin/lotto/domain/WinningLottoTest.kt +++ b/src/test/kotlin/lotto/domain/WinningLottosTest.kt @@ -4,7 +4,7 @@ import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.Test import org.junit.jupiter.api.assertThrows -internal class WinningLottoTest { +internal class WinningLottosTest { @Test fun `당첨번호를 6개 입력한다`() { diff --git a/src/test/kotlin/lotto/domain/strategy/LottoAutoGeneratorStrategyTest.kt b/src/test/kotlin/lotto/domain/strategy/LottosAutoGeneratorStrategyTest.kt similarity index 95% rename from src/test/kotlin/lotto/domain/strategy/LottoAutoGeneratorStrategyTest.kt rename to src/test/kotlin/lotto/domain/strategy/LottosAutoGeneratorStrategyTest.kt index 66dda6ec53..3ea8a3b55a 100644 --- a/src/test/kotlin/lotto/domain/strategy/LottoAutoGeneratorStrategyTest.kt +++ b/src/test/kotlin/lotto/domain/strategy/LottosAutoGeneratorStrategyTest.kt @@ -5,7 +5,7 @@ import org.junit.jupiter.api.Test import org.junit.jupiter.params.ParameterizedTest import org.junit.jupiter.params.provider.ValueSource -internal class LottoAutoGeneratorStrategyTest { +internal class LottosAutoGeneratorStrategyTest { @ParameterizedTest @ValueSource(ints = [1, 2, 5, 10]) From 3d8346f29e3707d37e88b0764dc179ffe879d082 Mon Sep 17 00:00:00 2001 From: Choiyounho Date: Sat, 31 Dec 2022 22:10:03 +0900 Subject: [PATCH 56/74] =?UTF-8?q?refactor=20:=20toFloat=20->=20toDouble=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 --- src/main/kotlin/lotto/domain/ProfitRate.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/kotlin/lotto/domain/ProfitRate.kt b/src/main/kotlin/lotto/domain/ProfitRate.kt index 94cb6455fd..d029dd81da 100644 --- a/src/main/kotlin/lotto/domain/ProfitRate.kt +++ b/src/main/kotlin/lotto/domain/ProfitRate.kt @@ -21,7 +21,7 @@ class ProfitRate( private fun calculate(totalWinningMoney: Int): Double { val decimalFormat = DecimalFormat(PROFIT_RATE_PATTERN) decimalFormat.roundingMode = RoundingMode.DOWN - val profitRate = totalWinningMoney / inputPrice.toFloat() + val profitRate = totalWinningMoney / inputPrice.toDouble() return decimalFormat.format(profitRate).toDouble() } From f9e7b4f5c4a29f9abc741e58964766f7b8dceace Mon Sep 17 00:00:00 2001 From: Choiyounho Date: Sat, 31 Dec 2022 22:11:07 +0900 Subject: [PATCH 57/74] =?UTF-8?q?refactor=20:=20=EB=A1=9C=EB=98=90=20?= =?UTF-8?q?=EC=9E=90=EB=8F=99=20=EC=83=9D=EC=84=B1=20=ED=95=A8=EC=88=98=20?= =?UTF-8?q?=EA=B5=AC=EC=A1=B0=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lotto/domain/strategy/LottoAutoGeneratorStrategy.kt | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/main/kotlin/lotto/domain/strategy/LottoAutoGeneratorStrategy.kt b/src/main/kotlin/lotto/domain/strategy/LottoAutoGeneratorStrategy.kt index cf53a25bb4..a52436742b 100644 --- a/src/main/kotlin/lotto/domain/strategy/LottoAutoGeneratorStrategy.kt +++ b/src/main/kotlin/lotto/domain/strategy/LottoAutoGeneratorStrategy.kt @@ -8,11 +8,7 @@ class LottoAutoGeneratorStrategy : LottoGeneratorStrategy { private val lottoNumbers = (LOTTO_FIRST_NUMBER..LOTTO_LAST_NUMBER).toList() override fun generate(lottoCount: Int): Lottos { - return Lottos( - (0 until lottoCount) - .map { generateLottoNumber() } - .toList() - ) + return Lottos(List(lottoCount) { generateLottoNumber() }) } private fun generateLottoNumber(): Lotto { From 59fc7954bf4c9f9b9750f745596b4255f9f755b1 Mon Sep 17 00:00:00 2001 From: Choiyounho Date: Sat, 31 Dec 2022 22:11:49 +0900 Subject: [PATCH 58/74] =?UTF-8?q?refactor=20:=20Lottos=20=ED=95=A8?= =?UTF-8?q?=EC=88=98=20=EA=B5=AC=EC=A1=B0=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/lotto/domain/Lottos.kt | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/main/kotlin/lotto/domain/Lottos.kt b/src/main/kotlin/lotto/domain/Lottos.kt index 9693eb04cc..5b3ecd14ce 100644 --- a/src/main/kotlin/lotto/domain/Lottos.kt +++ b/src/main/kotlin/lotto/domain/Lottos.kt @@ -3,12 +3,12 @@ package lotto.domain class Lottos(val value: List) { fun matchWinningNumbers(winningNumbers: List): Map { - return value.groupBy { lottoNumber -> - lottoNumber.value.count { - winningNumbers.contains(it) - } - } - .map { Pair(Ranking.valueOf(it.key), it.value.size) } - .toMap() + return value.groupingBy { lottoNumber -> + Ranking.valueOf( + lottoNumber.value.count { + winningNumbers.contains(it) + } + ) + }.eachCount() } } From de33bb109d7075485e6ab798384d3c06c1f0a194 Mon Sep 17 00:00:00 2001 From: Choiyounho Date: Sun, 8 Jan 2023 19:48:59 +0900 Subject: [PATCH 59/74] =?UTF-8?q?refactor=20:=20nullable=20=EC=A0=9C?= =?UTF-8?q?=EA=B1=B0=20=EB=B0=8F=20=EC=A0=84=EB=8B=AC=20=EA=B0=92=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/lotto/LottoApp.kt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/kotlin/lotto/LottoApp.kt b/src/main/kotlin/lotto/LottoApp.kt index fd3a7f5d3e..e2f7055092 100644 --- a/src/main/kotlin/lotto/LottoApp.kt +++ b/src/main/kotlin/lotto/LottoApp.kt @@ -13,7 +13,7 @@ import lotto.ui.OutputViews.printLottoMatchResult import lotto.ui.OutputViews.printProfitRate fun main() { - val inputPrice = inputPrice() ?: 0 + val inputPrice = inputPrice() val buyPrice = BuyPrice(inputPrice) val lottoCount = buyPrice.getLottoCount() @@ -26,9 +26,9 @@ fun main() { printBoughtLottos(lotto.value) - val winningNumbers = WinningLotto(inputWinningNumber()).winningNumbers + val winningNumbers = WinningLotto(inputWinningNumber()) val matching = lotto.matchWinningNumbers(winningNumbers) printLottoMatchResult(matching) - printProfitRate(ProfitRate(matching, lottoCount).calculate()) + printProfitRate(ProfitRate(matching, inputPrice).calculate()) } From 0924751aef4a49b919e02431b2a801d95c3c1e28 Mon Sep 17 00:00:00 2001 From: Choiyounho Date: Sun, 8 Jan 2023 19:49:14 +0900 Subject: [PATCH 60/74] =?UTF-8?q?refactor=20:=20Lotto=20inline=20class=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/lotto/domain/Lotto.kt | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/lotto/domain/Lotto.kt b/src/main/kotlin/lotto/domain/Lotto.kt index bcad070f34..5b4b248c0b 100644 --- a/src/main/kotlin/lotto/domain/Lotto.kt +++ b/src/main/kotlin/lotto/domain/Lotto.kt @@ -1,3 +1,9 @@ package lotto.domain -class Lotto(val value: List) +@JvmInline +value class Lotto(val value: List) { + + fun findWinningCount(winningLotto: WinningLotto): Int { + return value.count { winningLotto.winningNumbers.contains(it) } + } +} From 4ffaff35b374e9916287d52dda49fc46ecff3140 Mon Sep 17 00:00:00 2001 From: Choiyounho Date: Sun, 8 Jan 2023 19:49:38 +0900 Subject: [PATCH 61/74] =?UTF-8?q?refactor=20:=20Lottos=20inline=20class=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD=20=EB=B0=8F=20=EA=B0=9D=EC=B2=B4=EC=97=90=20?= =?UTF-8?q?=EB=A9=94=EC=84=B8=EC=A7=80=20=EC=A0=84=EB=8B=AC=20=EA=B8=B0?= =?UTF-8?q?=EB=8A=A5=20=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/lotto/domain/Lottos.kt | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/main/kotlin/lotto/domain/Lottos.kt b/src/main/kotlin/lotto/domain/Lottos.kt index 5b3ecd14ce..f665ac2e3a 100644 --- a/src/main/kotlin/lotto/domain/Lottos.kt +++ b/src/main/kotlin/lotto/domain/Lottos.kt @@ -1,14 +1,11 @@ package lotto.domain -class Lottos(val value: List) { +@JvmInline +value class Lottos(val value: List) { - fun matchWinningNumbers(winningNumbers: List): Map { - return value.groupingBy { lottoNumber -> - Ranking.valueOf( - lottoNumber.value.count { - winningNumbers.contains(it) - } - ) + fun matchWinningNumbers(winningNumbers: WinningLotto): Map { + return value.groupingBy { lotto -> + Ranking.valueOf(lotto.findWinningCount(winningNumbers)) }.eachCount() } } From 60a929b394b0044275f9256a82947691c892a4c7 Mon Sep 17 00:00:00 2001 From: Choiyounho Date: Sun, 8 Jan 2023 19:49:56 +0900 Subject: [PATCH 62/74] =?UTF-8?q?refactor=20:=20toList()=20=EC=A0=9C?= =?UTF-8?q?=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kotlin/lotto/domain/strategy/LottoAutoGeneratorStrategy.kt | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/kotlin/lotto/domain/strategy/LottoAutoGeneratorStrategy.kt b/src/main/kotlin/lotto/domain/strategy/LottoAutoGeneratorStrategy.kt index a52436742b..56b92f49dd 100644 --- a/src/main/kotlin/lotto/domain/strategy/LottoAutoGeneratorStrategy.kt +++ b/src/main/kotlin/lotto/domain/strategy/LottoAutoGeneratorStrategy.kt @@ -16,7 +16,6 @@ class LottoAutoGeneratorStrategy : LottoGeneratorStrategy { .shuffled() .subList(LOTTO_FIRST_INDEX, LOTTO_LAST_INDEX) .sorted() - .toList() return Lotto(lottoNumbers) } From b3376c940e0e444145f48fc0a3be3f0980a921d3 Mon Sep 17 00:00:00 2001 From: Choiyounho Date: Sun, 8 Jan 2023 19:50:03 +0900 Subject: [PATCH 63/74] =?UTF-8?q?refactor=20:=20require=20=EC=A0=81?= =?UTF-8?q?=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/lotto/domain/WinningLotto.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/lotto/domain/WinningLotto.kt b/src/main/kotlin/lotto/domain/WinningLotto.kt index bae735bc31..b279754ff9 100644 --- a/src/main/kotlin/lotto/domain/WinningLotto.kt +++ b/src/main/kotlin/lotto/domain/WinningLotto.kt @@ -15,8 +15,8 @@ class WinningLotto( private fun validateWinningNumber() { for (winningNumber in winningNumbers) { - if (winningNumber !in LOTTO_FIRST_NUMBER..LOTTO_LAST_NUMBER) { - throw IllegalArgumentException("당첨 번호는 1 ~ 45 범위의 숫자로만 구성될 수 있습니다.") + require(winningNumber in LOTTO_FIRST_NUMBER..LOTTO_LAST_NUMBER) { + "당첨 번호는 1 ~ 45 범위의 숫자로만 구성될 수 있습니다." } } } From 62a07becd4cee2951f3ac1d6ebdc5c03896b0697 Mon Sep 17 00:00:00 2001 From: Choiyounho Date: Sun, 8 Jan 2023 19:50:37 +0900 Subject: [PATCH 64/74] =?UTF-8?q?refactor=20:=20=EB=B6=88=ED=95=84?= =?UTF-8?q?=EC=9A=94=ED=95=9C=20=EC=98=A4=EB=B2=84=EB=A1=9C=EB=94=A9=20?= =?UTF-8?q?=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/lotto/domain/ProfitRate.kt | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/main/kotlin/lotto/domain/ProfitRate.kt b/src/main/kotlin/lotto/domain/ProfitRate.kt index d029dd81da..0828664eb9 100644 --- a/src/main/kotlin/lotto/domain/ProfitRate.kt +++ b/src/main/kotlin/lotto/domain/ProfitRate.kt @@ -10,7 +10,10 @@ class ProfitRate( fun calculate(): Double { val totalWinningMoney = getTotalWinningMoney() - return calculate(totalWinningMoney) + val decimalFormat = DecimalFormat(PROFIT_RATE_PATTERN) + decimalFormat.roundingMode = RoundingMode.DOWN + val profitRate = totalWinningMoney / inputPrice.toDouble() + return decimalFormat.format(profitRate).toDouble() } private fun getTotalWinningMoney(): Int { @@ -18,13 +21,6 @@ class ProfitRate( .sumOf { it.key.getTotalWinningMoney(it.value) } } - private fun calculate(totalWinningMoney: Int): Double { - val decimalFormat = DecimalFormat(PROFIT_RATE_PATTERN) - decimalFormat.roundingMode = RoundingMode.DOWN - val profitRate = totalWinningMoney / inputPrice.toDouble() - return decimalFormat.format(profitRate).toDouble() - } - companion object { private const val PROFIT_RATE_PATTERN = "#.##" } From a5d390fc67f7e8624591e104d991764c871d9621 Mon Sep 17 00:00:00 2001 From: Choiyounho Date: Sun, 8 Jan 2023 19:54:53 +0900 Subject: [PATCH 65/74] =?UTF-8?q?refactor=20:=20=EC=A0=95=EC=A0=81=20?= =?UTF-8?q?=ED=8C=A9=ED=86=A0=EB=A6=AC=20=EB=A9=94=EC=84=9C=EB=93=9C=20?= =?UTF-8?q?=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/lotto/LottoApp.kt | 2 +- src/main/kotlin/lotto/domain/WinningLotto.kt | 11 +++++++---- src/test/kotlin/lotto/domain/WinningLottosTest.kt | 6 +++--- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/main/kotlin/lotto/LottoApp.kt b/src/main/kotlin/lotto/LottoApp.kt index e2f7055092..90572fa9e9 100644 --- a/src/main/kotlin/lotto/LottoApp.kt +++ b/src/main/kotlin/lotto/LottoApp.kt @@ -26,7 +26,7 @@ fun main() { printBoughtLottos(lotto.value) - val winningNumbers = WinningLotto(inputWinningNumber()) + val winningNumbers = WinningLotto.of(inputWinningNumber()) val matching = lotto.matchWinningNumbers(winningNumbers) printLottoMatchResult(matching) diff --git a/src/main/kotlin/lotto/domain/WinningLotto.kt b/src/main/kotlin/lotto/domain/WinningLotto.kt index b279754ff9..05f3469c15 100644 --- a/src/main/kotlin/lotto/domain/WinningLotto.kt +++ b/src/main/kotlin/lotto/domain/WinningLotto.kt @@ -1,11 +1,9 @@ package lotto.domain class WinningLotto( - inputNumbers: List + val winningNumbers: List ) { - val winningNumbers = inputNumbers.map { it.toInt() } - init { if (winningNumbers.isEmpty() || winningNumbers.size != LIMIT_WINNING_NUMBER) { throw IllegalArgumentException("당첨 번호는 6개 입니다.") @@ -16,7 +14,7 @@ class WinningLotto( private fun validateWinningNumber() { for (winningNumber in winningNumbers) { require(winningNumber in LOTTO_FIRST_NUMBER..LOTTO_LAST_NUMBER) { - "당첨 번호는 1 ~ 45 범위의 숫자로만 구성될 수 있습니다." + MESSAGE_LOTTO_RANGE } } } @@ -26,5 +24,10 @@ class WinningLotto( private const val LOTTO_FIRST_NUMBER = 1 private const val LOTTO_LAST_NUMBER = 45 + private const val MESSAGE_LOTTO_RANGE = "당첨 번호는 1 ~ 45 범위의 숫자로만 구성될 수 있습니다." + + fun of(winningNumbers: List): WinningLotto { + return WinningLotto(winningNumbers.map { it.toInt() }) + } } } diff --git a/src/test/kotlin/lotto/domain/WinningLottosTest.kt b/src/test/kotlin/lotto/domain/WinningLottosTest.kt index 728e646c5a..e0e2143e19 100644 --- a/src/test/kotlin/lotto/domain/WinningLottosTest.kt +++ b/src/test/kotlin/lotto/domain/WinningLottosTest.kt @@ -12,7 +12,7 @@ internal class WinningLottosTest { val inputWinningNumber = listOf("1", "2", "3", "4", "5", "6") // when - val actual = WinningLotto(inputWinningNumber).winningNumbers.size + val actual = WinningLotto.of(inputWinningNumber).winningNumbers.size // then val expected = 6 @@ -29,14 +29,14 @@ internal class WinningLottosTest { @Test fun `당첨번호가 6개가 아니면 예외가 발생한다`() { assertThrows { - WinningLotto(listOf("1", "2")) + WinningLotto.of(listOf("1", "2")) } } @Test fun `당첨번호가 1에서 45숫자가 아니면 예외가 발생한다`() { assertThrows { - WinningLotto(listOf("1", "2", "3", "4", "5", "46")) + WinningLotto.of(listOf("1", "2", "3", "4", "5", "46")) } } } From 012484d4a8cd71a111f24f5bd4e41f7803b2dccc Mon Sep 17 00:00:00 2001 From: Choiyounho Date: Sun, 8 Jan 2023 20:02:41 +0900 Subject: [PATCH 66/74] =?UTF-8?q?test=20:=20Lottos=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EC=BD=94=EB=93=9C=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/lotto/LottoApp.kt | 6 +++--- src/test/kotlin/lotto/domain/LottosTest.kt | 24 ++++++++++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) create mode 100644 src/test/kotlin/lotto/domain/LottosTest.kt diff --git a/src/main/kotlin/lotto/LottoApp.kt b/src/main/kotlin/lotto/LottoApp.kt index 90572fa9e9..69cca977d2 100644 --- a/src/main/kotlin/lotto/LottoApp.kt +++ b/src/main/kotlin/lotto/LottoApp.kt @@ -22,12 +22,12 @@ fun main() { val lottoFactory = LottoFactory() val generateStrategy = LottoAutoGeneratorStrategy() - val lotto = lottoFactory.generate(lottoCount, generateStrategy) + val lottos = lottoFactory.generate(lottoCount, generateStrategy) - printBoughtLottos(lotto.value) + printBoughtLottos(lottos.value) val winningNumbers = WinningLotto.of(inputWinningNumber()) - val matching = lotto.matchWinningNumbers(winningNumbers) + val matching = lottos.matchWinningNumbers(winningNumbers) printLottoMatchResult(matching) printProfitRate(ProfitRate(matching, inputPrice).calculate()) diff --git a/src/test/kotlin/lotto/domain/LottosTest.kt b/src/test/kotlin/lotto/domain/LottosTest.kt new file mode 100644 index 0000000000..2ba01a7780 --- /dev/null +++ b/src/test/kotlin/lotto/domain/LottosTest.kt @@ -0,0 +1,24 @@ +package lotto.domain + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Assertions.* +import org.junit.jupiter.api.Test + +internal class LottosTest { + + @Test + fun `구매한 로또번호의 랭킹을 확인한다`() { + // given + val lottos = Lottos( + listOf(Lotto(listOf(1, 2, 3, 4, 5, 6))) + ) + val winningLotto = WinningLotto(listOf(1, 2, 3, 4, 5, 6)) + + // when + val actual = lottos.matchWinningNumbers(winningLotto) + + // then + val expected = mapOf(Ranking.FIRST to 1) + assertThat(actual).isEqualTo(expected) + } +} \ No newline at end of file From 3e94c77ba404efbe59609a2da282725fdd2bc6ef Mon Sep 17 00:00:00 2001 From: Choiyounho Date: Sun, 8 Jan 2023 20:05:03 +0900 Subject: [PATCH 67/74] =?UTF-8?q?refactor=20:=20ProfitRate=20->=20LottoRes?= =?UTF-8?q?ult=20=EA=B0=9D=EC=B2=B4=20=EB=84=A4=EC=9D=B4=EB=B0=8D=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/lotto/LottoApp.kt | 4 ++-- .../kotlin/lotto/domain/{ProfitRate.kt => LottoResult.kt} | 2 +- .../lotto/domain/{ProfitRateTest.kt => LottoResultTest.kt} | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) rename src/main/kotlin/lotto/domain/{ProfitRate.kt => LottoResult.kt} (97%) rename src/test/kotlin/lotto/domain/{ProfitRateTest.kt => LottoResultTest.kt} (84%) diff --git a/src/main/kotlin/lotto/LottoApp.kt b/src/main/kotlin/lotto/LottoApp.kt index 69cca977d2..cfc5fffdb0 100644 --- a/src/main/kotlin/lotto/LottoApp.kt +++ b/src/main/kotlin/lotto/LottoApp.kt @@ -1,7 +1,7 @@ package lotto import lotto.domain.BuyPrice -import lotto.domain.ProfitRate +import lotto.domain.LottoResult import lotto.domain.WinningLotto import lotto.domain.strategy.LottoAutoGeneratorStrategy import lotto.domain.strategy.LottoFactory @@ -30,5 +30,5 @@ fun main() { val matching = lottos.matchWinningNumbers(winningNumbers) printLottoMatchResult(matching) - printProfitRate(ProfitRate(matching, inputPrice).calculate()) + printProfitRate(LottoResult(matching, inputPrice).calculate()) } diff --git a/src/main/kotlin/lotto/domain/ProfitRate.kt b/src/main/kotlin/lotto/domain/LottoResult.kt similarity index 97% rename from src/main/kotlin/lotto/domain/ProfitRate.kt rename to src/main/kotlin/lotto/domain/LottoResult.kt index 0828664eb9..9f94eb1753 100644 --- a/src/main/kotlin/lotto/domain/ProfitRate.kt +++ b/src/main/kotlin/lotto/domain/LottoResult.kt @@ -3,7 +3,7 @@ package lotto.domain import java.math.RoundingMode import java.text.DecimalFormat -class ProfitRate( +class LottoResult( private val matchResult: Map, private val inputPrice: Int ) { diff --git a/src/test/kotlin/lotto/domain/ProfitRateTest.kt b/src/test/kotlin/lotto/domain/LottoResultTest.kt similarity index 84% rename from src/test/kotlin/lotto/domain/ProfitRateTest.kt rename to src/test/kotlin/lotto/domain/LottoResultTest.kt index 729bc2cb7a..746855552a 100644 --- a/src/test/kotlin/lotto/domain/ProfitRateTest.kt +++ b/src/test/kotlin/lotto/domain/LottoResultTest.kt @@ -3,7 +3,7 @@ package lotto.domain import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.Test -internal class ProfitRateTest { +internal class LottoResultTest { @Test fun `구매금액 대비 당첨금액의 수익률을 계산한다`() { @@ -18,7 +18,7 @@ internal class ProfitRateTest { val price = 14000 // when - val actual = ProfitRate(matchResult, price).calculate() + val actual = LottoResult(matchResult, price).calculate() // then val expected = 0.35 From 97edf42a85466f22dae0b9e2616a2b76b83f649b Mon Sep 17 00:00:00 2001 From: Choiyounho Date: Sun, 8 Jan 2023 21:16:17 +0900 Subject: [PATCH 68/74] =?UTF-8?q?feat=20:=20=EB=B3=B4=EB=84=88=EC=8A=A4=20?= =?UTF-8?q?=EB=B2=88=ED=98=B8=20=EC=9E=85=EB=A0=A5=20ui=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/lotto/ui/InputViews.kt | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/main/kotlin/lotto/ui/InputViews.kt b/src/main/kotlin/lotto/ui/InputViews.kt index 3e14d7440c..35be7a9b91 100644 --- a/src/main/kotlin/lotto/ui/InputViews.kt +++ b/src/main/kotlin/lotto/ui/InputViews.kt @@ -4,8 +4,10 @@ object InputViews { private const val MESSAGE_INPUT_PRICE = "구입금액을 입력해 주세요." private const val MESSAGE_INPUT_WINNING_NUMBER = "지난 주 당첨 번호를 입력해 주세요." + private const val MESSAGE_INPUT_BONUS_NUMBER = "보너스 볼을 입력해주세요." private const val DELIMITERS = ", " private const val DEFAULT_PRICE = 0 + private const val DEFAULT_BONUS_NUMBER = 0 fun inputPrice(): Int { println(MESSAGE_INPUT_PRICE) @@ -16,4 +18,9 @@ object InputViews { println(MESSAGE_INPUT_WINNING_NUMBER) return readLine()?.split(DELIMITERS) ?: emptyList() } + + fun inputBonusNumber(): Int { + println(MESSAGE_INPUT_BONUS_NUMBER) + return readLine()?.toIntOrNull() ?: DEFAULT_BONUS_NUMBER + } } From 8a32ecdff65cb431870e8989c1c149bdfd1a5b68 Mon Sep 17 00:00:00 2001 From: Choiyounho Date: Sun, 8 Jan 2023 21:16:37 +0900 Subject: [PATCH 69/74] =?UTF-8?q?refactor=20:=20=EB=B3=B4=EB=84=88?= =?UTF-8?q?=EC=8A=A4=20=EB=B2=88=ED=98=B8=20=EC=B6=9C=EB=A0=A5=20=EB=B0=8F?= =?UTF-8?q?=20=EB=8B=B9=EC=B2=A8=20=ED=86=B5=EA=B3=84=20=EB=A9=98=ED=8A=B8?= =?UTF-8?q?=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/lotto/ui/OutputViews.kt | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/lotto/ui/OutputViews.kt b/src/main/kotlin/lotto/ui/OutputViews.kt index b1709f11a7..73c20ee934 100644 --- a/src/main/kotlin/lotto/ui/OutputViews.kt +++ b/src/main/kotlin/lotto/ui/OutputViews.kt @@ -20,7 +20,11 @@ object OutputViews { } fun printBoughtLotto(lottoCount: Int) { - println("${lottoCount}${MESSAGE_BOUGHT_LOTTO}") + println("${lottoCount}$MESSAGE_BOUGHT_LOTTO") + } + + fun printBonusNumber(bonusNumber: Int) { + println(bonusNumber) } fun printLottoMatchResult(matchResult: Map) { @@ -28,7 +32,7 @@ object OutputViews { println("---------") println("${FOURTH.matchCount}개 일치 (${FOURTH.winningMoney}원) - ${matchResult[FOURTH] ?: 0}개") println("${THIRD.matchCount}개 일치 (${THIRD.winningMoney}원) - ${matchResult[THIRD] ?: 0}개") - println("${SECOND.matchCount}개 일치 (${SECOND.winningMoney}원) - ${matchResult[SECOND] ?: 0}개") + println("${SECOND.matchCount}개 일치, 보너스 볼 일치 (${SECOND.winningMoney}원) - ${matchResult[SECOND] ?: 0}개") println("${FIRST.matchCount}개 일치 (${FIRST.winningMoney}원) - ${matchResult[FIRST] ?: 0}개") } From 974ca7eaa1d4e82ecebfc0a10c398a3e805d1115 Mon Sep 17 00:00:00 2001 From: Choiyounho Date: Sun, 8 Jan 2023 21:16:53 +0900 Subject: [PATCH 70/74] =?UTF-8?q?refactor=20:=20=EB=8B=B9=EC=B2=A8?= =?UTF-8?q?=EA=B8=88=EC=95=A1=20=EB=B0=8F=20=EB=B3=B4=EB=84=88=EC=8A=A4=20?= =?UTF-8?q?=EB=B2=88=ED=98=B8=20=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/lotto/domain/Ranking.kt | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/main/kotlin/lotto/domain/Ranking.kt b/src/main/kotlin/lotto/domain/Ranking.kt index e6665e96c5..67e1bb7128 100644 --- a/src/main/kotlin/lotto/domain/Ranking.kt +++ b/src/main/kotlin/lotto/domain/Ranking.kt @@ -6,9 +6,10 @@ enum class Ranking( ) { FIRST(6, 2_000_000_000), - SECOND(5, 1_500_000), - THIRD(4, 50_000), - FOURTH(3, 5_000), + SECOND(5, 30_000_000), + THIRD(5, 1_500_000), + FOURTH(4, 50_000), + FIFTH(3, 5_000), MISS(0, 0); fun getTotalWinningMoney(matchResultValue: Int): Int { @@ -16,8 +17,13 @@ enum class Ranking( } companion object { - fun valueOf(matchCount: Int): Ranking { - return values().find { it.matchCount == matchCount } ?: MISS + fun valueOf(matchCount: Int, isBonus: Boolean = false): Ranking { + return values().find { + if (matchCount == 5 && !isBonus) { + return THIRD + } + it.matchCount == matchCount + } ?: MISS } } } From 2a589900fbf5014533c876ccd388054becba337e Mon Sep 17 00:00:00 2001 From: Choiyounho Date: Sun, 8 Jan 2023 21:17:08 +0900 Subject: [PATCH 71/74] =?UTF-8?q?refactor=20:=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EC=BD=94=EB=93=9C=EC=97=90=20=EB=B3=B4=EB=84=88?= =?UTF-8?q?=EC=8A=A4=20=EB=B2=88=ED=98=B8=20=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/kotlin/lotto/domain/LottosTest.kt | 25 +++++++++++-- .../kotlin/lotto/domain/WinningLottosTest.kt | 37 ++++++++++++++++--- 2 files changed, 53 insertions(+), 9 deletions(-) diff --git a/src/test/kotlin/lotto/domain/LottosTest.kt b/src/test/kotlin/lotto/domain/LottosTest.kt index 2ba01a7780..6989af8b16 100644 --- a/src/test/kotlin/lotto/domain/LottosTest.kt +++ b/src/test/kotlin/lotto/domain/LottosTest.kt @@ -1,7 +1,6 @@ package lotto.domain import org.assertj.core.api.Assertions.assertThat -import org.junit.jupiter.api.Assertions.* import org.junit.jupiter.api.Test internal class LottosTest { @@ -12,13 +11,31 @@ internal class LottosTest { val lottos = Lottos( listOf(Lotto(listOf(1, 2, 3, 4, 5, 6))) ) - val winningLotto = WinningLotto(listOf(1, 2, 3, 4, 5, 6)) + val bonusNumber = 7 + val winningLotto = WinningLotto(listOf(1, 2, 3, 4, 5, 6), bonusNumber) // when - val actual = lottos.matchWinningNumbers(winningLotto) + val actual = lottos.matchWinningNumbers(winningLotto, bonusNumber) // then val expected = mapOf(Ranking.FIRST to 1) assertThat(actual).isEqualTo(expected) } -} \ No newline at end of file + + @Test + fun `구매한 로또번호가 5개 맞고 보너스번호가 맞을 경우 2등이 된다`() { + // given + val lottos = Lottos( + listOf(Lotto(listOf(1, 2, 3, 4, 5, 7))) + ) + val bonusNumber = 7 + val winningLotto = WinningLotto(listOf(1, 2, 3, 4, 5, 6), bonusNumber) + + // when + val actual = lottos.matchWinningNumbers(winningLotto, bonusNumber) + + // then + val expected = mapOf(Ranking.SECOND to 1) + assertThat(actual).isEqualTo(expected) + } +} diff --git a/src/test/kotlin/lotto/domain/WinningLottosTest.kt b/src/test/kotlin/lotto/domain/WinningLottosTest.kt index e0e2143e19..6b4477d222 100644 --- a/src/test/kotlin/lotto/domain/WinningLottosTest.kt +++ b/src/test/kotlin/lotto/domain/WinningLottosTest.kt @@ -7,12 +7,13 @@ import org.junit.jupiter.api.assertThrows internal class WinningLottosTest { @Test - fun `당첨번호를 6개 입력한다`() { + fun `당첨번호를 6개와 보너스 번호를 입력한다`() { // given val inputWinningNumber = listOf("1", "2", "3", "4", "5", "6") + val bonusNumber = 7 // when - val actual = WinningLotto.of(inputWinningNumber).winningNumbers.size + val actual = WinningLotto.of(inputWinningNumber, bonusNumber).winningNumbers.size // then val expected = 6 @@ -22,21 +23,47 @@ internal class WinningLottosTest { @Test fun `당첨번호가 입력되지 않으면 예외가 발생한다`() { assertThrows { - WinningLotto(emptyList()) + WinningLotto(emptyList(), 7) } } @Test fun `당첨번호가 6개가 아니면 예외가 발생한다`() { assertThrows { - WinningLotto.of(listOf("1", "2")) + WinningLotto.of(listOf("1", "2"), 7) } } @Test fun `당첨번호가 1에서 45숫자가 아니면 예외가 발생한다`() { assertThrows { - WinningLotto.of(listOf("1", "2", "3", "4", "5", "46")) + WinningLotto.of(listOf("1", "2", "3", "4", "5", "46"), 7) } } + + @Test + fun `입력한 로또 번호에 보너스 번호가 포함될 경우 true`() { + // given + val inputWinningNumber = listOf("1", "2", "3", "4", "5", "7") + val bonusNumber = 7 + + // when + val actual = WinningLotto.of(inputWinningNumber, bonusNumber).isMatchBonus(bonusNumber) + + // then + assertThat(actual).isTrue + } + + @Test + fun `입력한 로또 번호에 보너스 번호가 포함되지 않은 경우 false`() { + // given + val inputWinningNumber = listOf("1", "2", "3", "4", "5", "6") + val bonusNumber = 7 + + // when + val actual = WinningLotto.of(inputWinningNumber, bonusNumber).isMatchBonus(8) + + // then + assertThat(actual).isFalse + } } From c161ea9353bde025c617c589ea6bc5f1555bd16d Mon Sep 17 00:00:00 2001 From: Choiyounho Date: Sun, 8 Jan 2023 21:17:40 +0900 Subject: [PATCH 72/74] =?UTF-8?q?feat=20:=20=EB=B3=B4=EB=84=88=EC=8A=A4=20?= =?UTF-8?q?=EB=B2=88=ED=98=B8=20=ED=99=95=EC=9D=B8=20=EA=B8=B0=EB=8A=A5=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/lotto/domain/WinningLotto.kt | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/main/kotlin/lotto/domain/WinningLotto.kt b/src/main/kotlin/lotto/domain/WinningLotto.kt index 05f3469c15..f8eceda823 100644 --- a/src/main/kotlin/lotto/domain/WinningLotto.kt +++ b/src/main/kotlin/lotto/domain/WinningLotto.kt @@ -1,7 +1,8 @@ package lotto.domain class WinningLotto( - val winningNumbers: List + val winningNumbers: List, + private val bonusNumber: Int ) { init { @@ -11,6 +12,10 @@ class WinningLotto( validateWinningNumber() } + fun isMatchBonus(bonusNumber: Int): Boolean { + return this.bonusNumber == bonusNumber + } + private fun validateWinningNumber() { for (winningNumber in winningNumbers) { require(winningNumber in LOTTO_FIRST_NUMBER..LOTTO_LAST_NUMBER) { @@ -26,8 +31,8 @@ class WinningLotto( private const val LOTTO_LAST_NUMBER = 45 private const val MESSAGE_LOTTO_RANGE = "당첨 번호는 1 ~ 45 범위의 숫자로만 구성될 수 있습니다." - fun of(winningNumbers: List): WinningLotto { - return WinningLotto(winningNumbers.map { it.toInt() }) + fun of(winningNumbers: List, bonusNumber: Int): WinningLotto { + return WinningLotto(winningNumbers.map { it.toInt() }, bonusNumber) } } } From 5ffbe759b45d31a44d3670c624c3858296fab92b Mon Sep 17 00:00:00 2001 From: Choiyounho Date: Sun, 8 Jan 2023 21:17:58 +0900 Subject: [PATCH 73/74] =?UTF-8?q?refactor=20:=20=EB=B3=B4=EB=84=88?= =?UTF-8?q?=EC=8A=A4=20=EB=B2=88=ED=98=B8=20=EC=A0=81=EC=9A=A9=ED=95=98?= =?UTF-8?q?=EC=97=AC=20=EB=8B=B9=EC=B2=A8=EB=B2=88=ED=98=B8=20=ED=99=95?= =?UTF-8?q?=EC=9D=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/lotto/domain/Lottos.kt | 7 +++++-- .../lotto/domain/strategy/LottoAutoGeneratorStrategy.kt | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/main/kotlin/lotto/domain/Lottos.kt b/src/main/kotlin/lotto/domain/Lottos.kt index f665ac2e3a..c93f54c82f 100644 --- a/src/main/kotlin/lotto/domain/Lottos.kt +++ b/src/main/kotlin/lotto/domain/Lottos.kt @@ -3,9 +3,12 @@ package lotto.domain @JvmInline value class Lottos(val value: List) { - fun matchWinningNumbers(winningNumbers: WinningLotto): Map { + fun matchWinningNumbers(winningNumbers: WinningLotto, bonusNumber: Int): Map { return value.groupingBy { lotto -> - Ranking.valueOf(lotto.findWinningCount(winningNumbers)) + val findWinningCount = lotto.findWinningCount(winningNumbers) + val isBonus = winningNumbers.isMatchBonus(bonusNumber) + + Ranking.valueOf(findWinningCount, isBonus) }.eachCount() } } diff --git a/src/main/kotlin/lotto/domain/strategy/LottoAutoGeneratorStrategy.kt b/src/main/kotlin/lotto/domain/strategy/LottoAutoGeneratorStrategy.kt index 56b92f49dd..67c9611531 100644 --- a/src/main/kotlin/lotto/domain/strategy/LottoAutoGeneratorStrategy.kt +++ b/src/main/kotlin/lotto/domain/strategy/LottoAutoGeneratorStrategy.kt @@ -1,7 +1,7 @@ package lotto.domain.strategy -import lotto.domain.Lottos import lotto.domain.Lotto +import lotto.domain.Lottos class LottoAutoGeneratorStrategy : LottoGeneratorStrategy { From b2891923d9c632f7d2bd8ac8dc199f55e39c6fe2 Mon Sep 17 00:00:00 2001 From: Choiyounho Date: Sun, 8 Jan 2023 21:18:15 +0900 Subject: [PATCH 74/74] =?UTF-8?q?refactor=20:=20=EB=8B=B9=EC=B2=A8?= =?UTF-8?q?=EB=B2=88=ED=98=B8=20=EC=9E=85=EB=A0=A5=20=EB=B0=9B=EA=B2=8C=20?= =?UTF-8?q?App=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/lotto/LottoApp.kt | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/lotto/LottoApp.kt b/src/main/kotlin/lotto/LottoApp.kt index cfc5fffdb0..4eb37d9496 100644 --- a/src/main/kotlin/lotto/LottoApp.kt +++ b/src/main/kotlin/lotto/LottoApp.kt @@ -5,8 +5,10 @@ import lotto.domain.LottoResult import lotto.domain.WinningLotto import lotto.domain.strategy.LottoAutoGeneratorStrategy import lotto.domain.strategy.LottoFactory +import lotto.ui.InputViews.inputBonusNumber import lotto.ui.InputViews.inputPrice import lotto.ui.InputViews.inputWinningNumber +import lotto.ui.OutputViews.printBonusNumber import lotto.ui.OutputViews.printBoughtLotto import lotto.ui.OutputViews.printBoughtLottos import lotto.ui.OutputViews.printLottoMatchResult @@ -26,8 +28,13 @@ fun main() { printBoughtLottos(lottos.value) - val winningNumbers = WinningLotto.of(inputWinningNumber()) - val matching = lottos.matchWinningNumbers(winningNumbers) + val winningNumber = inputWinningNumber() + val bonusNumber = inputBonusNumber() + printBonusNumber(bonusNumber) + + val winningNumbers = WinningLotto.of(winningNumber, bonusNumber) + + val matching = lottos.matchWinningNumbers(winningNumbers, bonusNumber) printLottoMatchResult(matching) printProfitRate(LottoResult(matching, inputPrice).calculate())