From 3aaf193ab828397af4ae3a471c5df4a68e580c5e Mon Sep 17 00:00:00 2001 From: "haein.lee" Date: Thu, 3 Nov 2022 07:18:16 +0900 Subject: [PATCH 1/6] first commit --- src/main/kotlin/main.kt | 14 ++++++++++++++ src/test/kotlin/MainTest.kt | 12 ++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 src/main/kotlin/main.kt create mode 100644 src/test/kotlin/MainTest.kt diff --git a/src/main/kotlin/main.kt b/src/main/kotlin/main.kt new file mode 100644 index 0000000000..fcb1995814 --- /dev/null +++ b/src/main/kotlin/main.kt @@ -0,0 +1,14 @@ +import jdk.nashorn.internal.objects.Global.print + +class main { + fun main(): List { + + var str = "Kotlin TutorialsepTutorial KartsepExamples" + var delimiter = "sep" + + val parts = str.split(" ") + + println(parts) + return parts + } +} \ No newline at end of file diff --git a/src/test/kotlin/MainTest.kt b/src/test/kotlin/MainTest.kt new file mode 100644 index 0000000000..46ab9048d7 --- /dev/null +++ b/src/test/kotlin/MainTest.kt @@ -0,0 +1,12 @@ +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test + +class PersonTest { + @Test + fun `해인테스트`() { + val person = main() + assertThat(person.main()).hasSize(3) + } + + +} \ No newline at end of file From 0f9ec881e175418c6ce0b4730b2a536b16b4f7c2 Mon Sep 17 00:00:00 2001 From: "haein.lee" Date: Thu, 3 Nov 2022 07:19:21 +0900 Subject: [PATCH 2/6] first commit --- src/main/kotlin/main.kt | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/main/kotlin/main.kt b/src/main/kotlin/main.kt index fcb1995814..7b9a0954eb 100644 --- a/src/main/kotlin/main.kt +++ b/src/main/kotlin/main.kt @@ -2,12 +2,9 @@ import jdk.nashorn.internal.objects.Global.print class main { fun main(): List { - var str = "Kotlin TutorialsepTutorial KartsepExamples" var delimiter = "sep" - val parts = str.split(" ") - println(parts) return parts } From 351d841f5889964e40cbf37b62468150da0e474c Mon Sep 17 00:00:00 2001 From: "haein.lee" Date: Thu, 3 Nov 2022 07:26:17 +0900 Subject: [PATCH 3/6] first commit --- src/main/kotlin/{main.kt => Calculator.kt} | 7 ++----- src/test/kotlin/{MainTest.kt => CalculatorTest.kt} | 8 +++----- 2 files changed, 5 insertions(+), 10 deletions(-) rename src/main/kotlin/{main.kt => Calculator.kt} (57%) rename src/test/kotlin/{MainTest.kt => CalculatorTest.kt} (74%) diff --git a/src/main/kotlin/main.kt b/src/main/kotlin/Calculator.kt similarity index 57% rename from src/main/kotlin/main.kt rename to src/main/kotlin/Calculator.kt index 7b9a0954eb..6b5b6c873f 100644 --- a/src/main/kotlin/main.kt +++ b/src/main/kotlin/Calculator.kt @@ -1,11 +1,8 @@ -import jdk.nashorn.internal.objects.Global.print -class main { +class Calculator { fun main(): List { var str = "Kotlin TutorialsepTutorial KartsepExamples" - var delimiter = "sep" val parts = str.split(" ") - println(parts) return parts } -} \ No newline at end of file +} diff --git a/src/test/kotlin/MainTest.kt b/src/test/kotlin/CalculatorTest.kt similarity index 74% rename from src/test/kotlin/MainTest.kt rename to src/test/kotlin/CalculatorTest.kt index 46ab9048d7..e37d2b25e2 100644 --- a/src/test/kotlin/MainTest.kt +++ b/src/test/kotlin/CalculatorTest.kt @@ -1,12 +1,10 @@ import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.Test -class PersonTest { +class CalculatorTest { @Test fun `해인테스트`() { - val person = main() + val person = Calculator() assertThat(person.main()).hasSize(3) } - - -} \ No newline at end of file +} From ef17734e409aeb8f6219e6270933e0f3353a9d99 Mon Sep 17 00:00:00 2001 From: "haein.lee" Date: Sun, 13 Nov 2022 22:13:47 +0900 Subject: [PATCH 4/6] =?UTF-8?q?step2:=20=EB=AC=B8=EC=9E=90=EC=97=B4?= =?UTF-8?q?=EA=B3=84=EC=82=B0=EA=B8=B0=20=EA=B5=AC=ED=98=84=20draft?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/Calculator.kt | 55 ++++++++++++++++++++++++++++--- src/test/kotlin/CalculatorTest.kt | 4 +-- 2 files changed, 52 insertions(+), 7 deletions(-) diff --git a/src/main/kotlin/Calculator.kt b/src/main/kotlin/Calculator.kt index 6b5b6c873f..c904b8f140 100644 --- a/src/main/kotlin/Calculator.kt +++ b/src/main/kotlin/Calculator.kt @@ -1,8 +1,53 @@ - class Calculator { - fun main(): List { - var str = "Kotlin TutorialsepTutorial KartsepExamples" - val parts = str.split(" ") - return parts + fun main(string: String?): Double { + if (string.isNullOrBlank()) { + throw IllegalArgumentException("입력값이 없습니다.") + } else { + var total: Double = 0.0 + var now: Double = 0.0 + var indicator: String = "" + string.split(" ").forEachIndexed { index, it -> + if (index == 0) { + total = it.toDouble() + } else if (index == 1) { + indicator = it + } else if (index % 2 == 0 && it.toDoubleOrNull() != null) { + now = it.toDouble() + total = calculator(indicator, total, now) + } else { + indicator = it + } + } + return total + } + } + + private fun calculator(indicator: String, first: Double, last: Double): Double { + return when (indicator) { + "+" -> plus(first, last) + "-" -> minus(first, last) + "*" -> multiple(first, last) + "/" -> divide(first, last) + else -> { + throw IllegalArgumentException("연산자가 틀렸습니다.") + } + } + } + + fun plus(first: Double, last: Double): Double { + return first + last + } + + fun minus(first: Double, last: Double): Double { + return first - last + } + + fun multiple(first: Double, last: Double): Double { + return first * last + } + + fun divide(first: Double, last: Double): Double { + return first / last } } + diff --git a/src/test/kotlin/CalculatorTest.kt b/src/test/kotlin/CalculatorTest.kt index e37d2b25e2..dc71c37a23 100644 --- a/src/test/kotlin/CalculatorTest.kt +++ b/src/test/kotlin/CalculatorTest.kt @@ -3,8 +3,8 @@ import org.junit.jupiter.api.Test class CalculatorTest { @Test - fun `해인테스트`() { + fun `문자열 계산기 테스트`() { val person = Calculator() - assertThat(person.main()).hasSize(3) + assertThat(person.main("2 + 3 * 4 / 2")).isEqualTo(10.0) } } From ab6baa10eeb535d3519ca37492f8b5e334f14fd5 Mon Sep 17 00:00:00 2001 From: "haein.lee" Date: Mon, 14 Nov 2022 20:22:20 +0900 Subject: [PATCH 5/6] =?UTF-8?q?step2:=20=EB=AC=B8=EC=9E=90=EC=97=B4?= =?UTF-8?q?=EA=B3=84=EC=82=B0=EA=B8=B0=20=EA=B5=AC=ED=98=84=20draft?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle.kts | 1 + src/main/kotlin/Calculator.kt | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/build.gradle.kts b/build.gradle.kts index e32e685f0c..fa6915c103 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -28,5 +28,6 @@ tasks { } ktlint { verbose.set(true) + outputToConsole.set(true) } } diff --git a/src/main/kotlin/Calculator.kt b/src/main/kotlin/Calculator.kt index c904b8f140..c6951927cd 100644 --- a/src/main/kotlin/Calculator.kt +++ b/src/main/kotlin/Calculator.kt @@ -50,4 +50,3 @@ class Calculator { return first / last } } - From b7c5b954da639de25e9babe4500e992483cf7c82 Mon Sep 17 00:00:00 2001 From: "haein.lee" Date: Fri, 30 Dec 2022 22:01:07 +0900 Subject: [PATCH 6/6] =?UTF-8?q?=EC=9E=90=EB=8F=99=EC=B0=A8=EA=B2=BD?= =?UTF-8?q?=EC=A3=BC=20=EB=A6=AC=ED=8C=A9=ED=86=A0=EB=A7=81=20=EC=BD=94?= =?UTF-8?q?=EB=93=9C=20=EA=B5=AC=ED=98=84=20/=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=EC=BD=94=EB=93=9C=20=ED=95=84=EC=9A=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/Calculator.kt | 52 -------------------- src/main/kotlin/RacingGameController.kt | 20 ++++++++ src/main/kotlin/domain/Car.kt | 20 ++++++++ src/main/kotlin/domain/Winner.kt | 11 +++++ src/main/kotlin/service/RacingGameService.kt | 51 +++++++++++++++++++ src/main/kotlin/view/InputView.kt | 17 +++++++ src/main/kotlin/view/ResultView.kt | 13 +++++ src/test/kotlin/CalculatorTest.kt | 10 ---- 8 files changed, 132 insertions(+), 62 deletions(-) delete mode 100644 src/main/kotlin/Calculator.kt create mode 100644 src/main/kotlin/RacingGameController.kt create mode 100644 src/main/kotlin/domain/Car.kt create mode 100644 src/main/kotlin/domain/Winner.kt create mode 100644 src/main/kotlin/service/RacingGameService.kt create mode 100644 src/main/kotlin/view/InputView.kt create mode 100644 src/main/kotlin/view/ResultView.kt delete mode 100644 src/test/kotlin/CalculatorTest.kt diff --git a/src/main/kotlin/Calculator.kt b/src/main/kotlin/Calculator.kt deleted file mode 100644 index c6951927cd..0000000000 --- a/src/main/kotlin/Calculator.kt +++ /dev/null @@ -1,52 +0,0 @@ -class Calculator { - fun main(string: String?): Double { - if (string.isNullOrBlank()) { - throw IllegalArgumentException("입력값이 없습니다.") - } else { - var total: Double = 0.0 - var now: Double = 0.0 - var indicator: String = "" - string.split(" ").forEachIndexed { index, it -> - if (index == 0) { - total = it.toDouble() - } else if (index == 1) { - indicator = it - } else if (index % 2 == 0 && it.toDoubleOrNull() != null) { - now = it.toDouble() - total = calculator(indicator, total, now) - } else { - indicator = it - } - } - return total - } - } - - private fun calculator(indicator: String, first: Double, last: Double): Double { - return when (indicator) { - "+" -> plus(first, last) - "-" -> minus(first, last) - "*" -> multiple(first, last) - "/" -> divide(first, last) - else -> { - throw IllegalArgumentException("연산자가 틀렸습니다.") - } - } - } - - fun plus(first: Double, last: Double): Double { - return first + last - } - - fun minus(first: Double, last: Double): Double { - return first - last - } - - fun multiple(first: Double, last: Double): Double { - return first * last - } - - fun divide(first: Double, last: Double): Double { - return first / last - } -} diff --git a/src/main/kotlin/RacingGameController.kt b/src/main/kotlin/RacingGameController.kt new file mode 100644 index 0000000000..2b18758827 --- /dev/null +++ b/src/main/kotlin/RacingGameController.kt @@ -0,0 +1,20 @@ +package step3.racingcar.controller + +import view.InputView +import view.RacingGameInput +import view.ResultView + +class RacingGameController { + private val service: RacingGameService = RacingGameService() + private val resultView: ResultView = ResultView() + + fun main() { + startGame(InputView.executeInputScreen()) + } + + private fun startGame(input: RacingGameInput) { + service.race(input).let { + resultView.outputScreen(it) + } + } +} \ No newline at end of file diff --git a/src/main/kotlin/domain/Car.kt b/src/main/kotlin/domain/Car.kt new file mode 100644 index 0000000000..ae20b82d41 --- /dev/null +++ b/src/main/kotlin/domain/Car.kt @@ -0,0 +1,20 @@ +package domain + +class Car( + val name: String, + var distance: Int = 0 +) { + fun move(number: Int): Int { + return if (number > 5) drive() + else stop() + } + + private fun drive(): Int { + distance++ + return distance + } + + private fun stop(): Int { + return distance + } +} \ No newline at end of file diff --git a/src/main/kotlin/domain/Winner.kt b/src/main/kotlin/domain/Winner.kt new file mode 100644 index 0000000000..0b190296f1 --- /dev/null +++ b/src/main/kotlin/domain/Winner.kt @@ -0,0 +1,11 @@ +package domain + +class Winner( + private val cars: List +) { + fun getWinner(): String { + val firstRecord = cars.maxOf { it.distance } + val winners = cars.filter { firstRecord == it.distance }.map { it.name } + return winners.joinToString { "," } + } +} \ No newline at end of file diff --git a/src/main/kotlin/service/RacingGameService.kt b/src/main/kotlin/service/RacingGameService.kt new file mode 100644 index 0000000000..2d1b7ecc50 --- /dev/null +++ b/src/main/kotlin/service/RacingGameService.kt @@ -0,0 +1,51 @@ +package step3.racingcar.controller + +import domain.Car +import domain.Winner +import view.RacingGameInput + +class RacingGameService { + private val randomNumberGenerator: RandomNumberGenerator = RandomNumberGenerator(1, 9) + fun race(input: RacingGameInput): Pair { + val candidate = input.carNames.split(",").map { + Car(it) + } + var trackHistory = "" + for (i: Int in 0..input.tryCount) { + trackHistory += lap(candidate) + } + return trackHistory to Winner(candidate).getWinner() + } + + private fun lap(candidate: List): String { + var trackHistory = "" + candidate.forEach { + trackHistory += drawTrack( + it.name, + it.move(randomNumberGenerator.getRandomNumber()) + ) + "\n" + } + return trackHistory + } + + private fun drawTrack(name: String, count: Int): String { + var result = "" + result += "$name : " + for (i: Int in 0..count) { + result += "-" + } + result += "\n" + return result + } +} + +interface NumberGenerator { + fun getRandomNumber(): Int + val RANGE_START: Int + val RANGE_END: Int +} + +class RandomNumberGenerator(override val RANGE_START: Int, override val RANGE_END: Int) : + NumberGenerator { + override fun getRandomNumber() = (RANGE_START..RANGE_END).random() +} \ No newline at end of file diff --git a/src/main/kotlin/view/InputView.kt b/src/main/kotlin/view/InputView.kt new file mode 100644 index 0000000000..64a5cbb089 --- /dev/null +++ b/src/main/kotlin/view/InputView.kt @@ -0,0 +1,17 @@ +package view + +object InputView { + fun executeInputScreen(): RacingGameInput { + println("경주할 자동차 이름을 입력하세요(이름은 쉼표(,)를 기준으로 구분).") + val carNames: String = readln() + println("시도할 횟수는 몇 회인가요?") + val tryCount = readln().toInt() + + return RacingGameInput(carNames, tryCount) + } +} + +data class RacingGameInput( + val carNames: String, + val tryCount: Int +) \ No newline at end of file diff --git a/src/main/kotlin/view/ResultView.kt b/src/main/kotlin/view/ResultView.kt new file mode 100644 index 0000000000..558652365a --- /dev/null +++ b/src/main/kotlin/view/ResultView.kt @@ -0,0 +1,13 @@ +package view + +class ResultView { + + fun outputScreen(result: Pair) { + println(result.first) + printWinner(result.second) + } + + private fun printWinner(winners: String) { + println("${winners}가 최종 우승했습니다.") + } +} diff --git a/src/test/kotlin/CalculatorTest.kt b/src/test/kotlin/CalculatorTest.kt deleted file mode 100644 index dc71c37a23..0000000000 --- a/src/test/kotlin/CalculatorTest.kt +++ /dev/null @@ -1,10 +0,0 @@ -import org.assertj.core.api.Assertions.assertThat -import org.junit.jupiter.api.Test - -class CalculatorTest { - @Test - fun `문자열 계산기 테스트`() { - val person = Calculator() - assertThat(person.main("2 + 3 * 4 / 2")).isEqualTo(10.0) - } -}