diff --git a/README.md b/README.md index 49a3646e03..965c56a1ff 100644 --- a/README.md +++ b/README.md @@ -2,5 +2,5 @@ ## 기능목록 -- [ ] 자동차 이름 입력받는다 -- [ ] 우승자를 선정한다 +- [x] 자동차 이름 입력받는다 +- [x] 우승자를 선정한다 diff --git a/src/main/kotlin/racingcar/Cars.kt b/src/main/kotlin/racingcar/Cars.kt index adec065d09..1f872513c6 100644 --- a/src/main/kotlin/racingcar/Cars.kt +++ b/src/main/kotlin/racingcar/Cars.kt @@ -1,12 +1,21 @@ package racingcar class Cars(val cars: List, private val powerGenerator: PowerGenerator) { + init { + require(cars.isNotEmpty()) { "자동차는 최소 한 대 이상 존재해야 합니다." } + } + fun move(): Cars { val movedCars = mutableListOf() cars.forEach { movedCars.add(it.move(powerGenerator.generate())) } return Cars(movedCars, powerGenerator) } + fun findWinner(): List { + val maxPosition = cars.maxBy { it.position }.position + return cars.filter { it.position == maxPosition } + } + companion object { fun initialize(numberOfCars: Int, names: List, powerGenerator: PowerGenerator): Cars { val cars = mutableListOf() diff --git a/src/main/kotlin/racingcar/Main.kt b/src/main/kotlin/racingcar/Main.kt index b21814ff1b..b06b4089b5 100644 --- a/src/main/kotlin/racingcar/Main.kt +++ b/src/main/kotlin/racingcar/Main.kt @@ -1,10 +1,14 @@ package racingcar fun main() { - val numberOfCars = InputView.inputNumberOfCars() - val namesOfCars = InputView.inputNameOfCars() - val cars = Cars.initialize(numberOfCars, namesOfCars, RandomPowerGenerator) + val cars = initializeCars() + + val movedCars = moveCars(cars) + val winners = movedCars.findWinner() + OutputView.printWinners(winners) +} +private fun moveCars(cars: Cars): Cars { val numberOfMoves = InputView.inputNumberOfMoves() OutputView.printResultTitle() @@ -14,4 +18,11 @@ fun main() { movedCars = movedCars.move() OutputView.printResult(movedCars.cars) } + return movedCars +} + +private fun initializeCars(): Cars { + val numberOfCars = InputView.inputNumberOfCars() + val namesOfCars = InputView.inputNameOfCars() + return Cars.initialize(numberOfCars, namesOfCars, RandomPowerGenerator) } diff --git a/src/main/kotlin/racingcar/OutputView.kt b/src/main/kotlin/racingcar/OutputView.kt index 9ff5c624aa..2d1e9ee0df 100644 --- a/src/main/kotlin/racingcar/OutputView.kt +++ b/src/main/kotlin/racingcar/OutputView.kt @@ -12,4 +12,9 @@ object OutputView { } println() } + + fun printWinners(winners: List) { + val winnerNames = winners.joinToString(", ") { it.name } + println("$winnerNames 가 최종 우승했습니다.") + } } diff --git a/src/test/kotlin/racingcar/CarsTest.kt b/src/test/kotlin/racingcar/CarsTest.kt index a4d1a319df..2f961c4c03 100644 --- a/src/test/kotlin/racingcar/CarsTest.kt +++ b/src/test/kotlin/racingcar/CarsTest.kt @@ -2,9 +2,15 @@ package racingcar import org.junit.jupiter.api.Assertions.assertTrue import org.junit.jupiter.api.Test +import org.junit.jupiter.api.assertThrows internal class CarsTest { + @Test + fun `자동차들은 1대 이상 있어야 한다`() { + assertThrows { Cars.initialize(0, listOf("a", "b", "c"), FixedPowerGenerator(5)) } + } + @Test fun `자동차들은 특정 숫자 이상이 들어오면 움직인다`() { val cars = Cars.initialize(3, listOf("a", "b", "c"), FixedPowerGenerator(4))