Skip to content

Commit b42bacd

Browse files
committed
feat: 우승자의 이름을 출력하는 기능 구현
1 parent 638255f commit b42bacd

File tree

5 files changed

+36
-5
lines changed

5 files changed

+36
-5
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22

33
## 기능목록
44

5-
- [ ] 자동차 이름 입력받는다
6-
- [ ] 우승자를 선정한다
5+
- [x] 자동차 이름 입력받는다
6+
- [x] 우승자를 선정한다

src/main/kotlin/racingcar/Cars.kt

+9
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
11
package racingcar
22

33
class Cars(val cars: List<Car>, private val powerGenerator: PowerGenerator) {
4+
init {
5+
require(cars.isNotEmpty()) { "자동차는 최소 한 대 이상 존재해야 합니다." }
6+
}
7+
48
fun move(): Cars {
59
val movedCars = mutableListOf<Car>()
610
cars.forEach { movedCars.add(it.move(powerGenerator.generate())) }
711
return Cars(movedCars, powerGenerator)
812
}
913

14+
fun findWinner(): List<Car> {
15+
val maxPosition = cars.maxBy { it.position }.position
16+
return cars.filter { it.position == maxPosition }
17+
}
18+
1019
companion object {
1120
fun initialize(numberOfCars: Int, names: List<String>, powerGenerator: PowerGenerator): Cars {
1221
val cars = mutableListOf<Car>()

src/main/kotlin/racingcar/Main.kt

+14-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package racingcar
22

33
fun main() {
4-
val numberOfCars = InputView.inputNumberOfCars()
5-
val namesOfCars = InputView.inputNameOfCars()
6-
val cars = Cars.initialize(numberOfCars, namesOfCars, RandomPowerGenerator)
4+
val cars = initializeCars()
5+
6+
val movedCars = moveCars(cars)
7+
val winners = movedCars.findWinner()
8+
OutputView.printWinners(winners)
9+
}
710

11+
private fun moveCars(cars: Cars): Cars {
812
val numberOfMoves = InputView.inputNumberOfMoves()
913

1014
OutputView.printResultTitle()
@@ -14,4 +18,11 @@ fun main() {
1418
movedCars = movedCars.move()
1519
OutputView.printResult(movedCars.cars)
1620
}
21+
return movedCars
22+
}
23+
24+
private fun initializeCars(): Cars {
25+
val numberOfCars = InputView.inputNumberOfCars()
26+
val namesOfCars = InputView.inputNameOfCars()
27+
return Cars.initialize(numberOfCars, namesOfCars, RandomPowerGenerator)
1728
}

src/main/kotlin/racingcar/OutputView.kt

+5
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,9 @@ object OutputView {
1212
}
1313
println()
1414
}
15+
16+
fun printWinners(winners: List<Car>) {
17+
val winnerNames = winners.joinToString(", ") { it.name }
18+
println("$winnerNames 가 최종 우승했습니다.")
19+
}
1520
}

src/test/kotlin/racingcar/CarsTest.kt

+6
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,15 @@ package racingcar
22

33
import org.junit.jupiter.api.Assertions.assertTrue
44
import org.junit.jupiter.api.Test
5+
import org.junit.jupiter.api.assertThrows
56

67
internal class CarsTest {
78

9+
@Test
10+
fun `자동차들은 1대 이상 있어야 한다`() {
11+
assertThrows<IllegalArgumentException> { Cars.initialize(0, listOf("a", "b", "c"), FixedPowerGenerator(5)) }
12+
}
13+
814
@Test
915
fun `자동차들은 특정 숫자 이상이 들어오면 움직인다`() {
1016
val cars = Cars.initialize(3, listOf("a", "b", "c"), FixedPowerGenerator(4))

0 commit comments

Comments
 (0)