Skip to content

Commit 638255f

Browse files
committed
feat: 자동차의 이름을 받는 기능 구현
1 parent 6b202c5 commit 638255f

File tree

6 files changed

+35
-12
lines changed

6 files changed

+35
-12
lines changed

src/main/kotlin/racingcar/Car.kt

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
package racingcar
22

3-
class Car(val position: Int) {
4-
constructor() : this(INITIAL_POSITION)
3+
class Car(val name: String, val position: Int) {
4+
init {
5+
require(name.length in MINIMUM_CAR_NAME_LENGTH..MAXIMUM_CAR_NAME_LENGTH) {
6+
"자동차의 이름은 ${MINIMUM_CAR_NAME_LENGTH}글자 이상 ${MAXIMUM_CAR_NAME_LENGTH}글자 이하만 가능합니다."
7+
}
8+
}
9+
10+
constructor(name: String) : this(name, INITIAL_POSITION)
511

612
fun move(power: Int): Car {
713
validatePower(power)
814
if (power >= MINIMUM_AMOUNT_TO_MOVE) {
9-
return Car(position + 1)
15+
return Car(name, position + 1)
1016
}
1117
return this
1218
}
@@ -22,5 +28,7 @@ class Car(val position: Int) {
2228
const val MINIMUM_AMOUNT_TO_MOVE = 4
2329
const val MAXIMUM_MOVE_POWER = 9
2430
const val MINIMUM_MOVE_POWER = 0
31+
const val MINIMUM_CAR_NAME_LENGTH = 1
32+
const val MAXIMUM_CAR_NAME_LENGTH = 5
2533
}
2634
}

src/main/kotlin/racingcar/Cars.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ class Cars(val cars: List<Car>, private val powerGenerator: PowerGenerator) {
88
}
99

1010
companion object {
11-
fun initializeWithNumberOfCars(numberOfCars: Int, powerGenerator: PowerGenerator): Cars {
11+
fun initialize(numberOfCars: Int, names: List<String>, powerGenerator: PowerGenerator): Cars {
1212
val cars = mutableListOf<Car>()
1313
repeat(numberOfCars) {
14-
cars.add(Car())
14+
cars.add(Car(names[it]))
1515
}
1616
return Cars(cars, powerGenerator)
1717
}

src/main/kotlin/racingcar/InputView.kt

+5
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,9 @@ object InputView {
1111
println("시도할 회수는 몇 회 인가요?")
1212
return readln().toInt()
1313
}
14+
15+
fun inputNameOfCars(): List<String> {
16+
println("자동차 이름을 입력하세요. (이름은 쉼표(,) 기준으로 구분)")
17+
return readln().split(",")
18+
}
1419
}

src/main/kotlin/racingcar/Main.kt

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ package racingcar
22

33
fun main() {
44
val numberOfCars = InputView.inputNumberOfCars()
5-
val numberOfMoves = InputView.inputNumberOfMoves()
5+
val namesOfCars = InputView.inputNameOfCars()
6+
val cars = Cars.initialize(numberOfCars, namesOfCars, RandomPowerGenerator)
67

7-
val cars = Cars.initializeWithNumberOfCars(numberOfCars, RandomPowerGenerator)
8+
val numberOfMoves = InputView.inputNumberOfMoves()
89

910
OutputView.printResultTitle()
1011

src/test/kotlin/racingcar/CarTest.kt

+12-3
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,34 @@ import org.junit.jupiter.params.ParameterizedTest
77
import org.junit.jupiter.params.provider.ValueSource
88

99
internal class CarTest {
10+
@Test
11+
fun `자동차의 이름은 1자 이상이어야 한다`() {
12+
assertThrows<IllegalArgumentException> { Car("") }
13+
}
14+
15+
@Test
16+
fun `자동차의 이름은 5자 이하여야 한다`() {
17+
assertThrows<IllegalArgumentException> { Car("123456") }
18+
}
1019

1120
@Test
1221
fun `자동차는 4 이상이 들어오면 움직인다`() {
13-
val car = Car()
22+
val car = Car("name")
1423
val result = car.move(4)
1524
assertThat(result.position).isOne()
1625
}
1726

1827
@Test
1928
fun `자동차는 4 미만이 들어오면 움직이지 않는다`() {
20-
val car = Car()
29+
val car = Car("name")
2130
val result = car.move(3)
2231
assertThat(result.position).isZero()
2332
}
2433

2534
@ParameterizedTest
2635
@ValueSource(ints = [-1, 10])
2736
fun `자동차는 0 이상 9 이하의 값만 가능하다`(power: Int) {
28-
val car = Car()
37+
val car = Car("name")
2938

3039
assertThrows<IllegalArgumentException> {
3140
car.move(power)

src/test/kotlin/racingcar/CarsTest.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ internal class CarsTest {
77

88
@Test
99
fun `자동차들은 특정 숫자 이상이 들어오면 움직인다`() {
10-
val cars = Cars.initializeWithNumberOfCars(3, FixedPowerGenerator(4))
10+
val cars = Cars.initialize(3, listOf("a", "b", "c"), FixedPowerGenerator(4))
1111

1212
val result = cars.move()
1313

@@ -16,7 +16,7 @@ internal class CarsTest {
1616

1717
@Test
1818
fun `자동차들은 특정 숫자 미만이 들어오면 움직이지 않는다`() {
19-
val cars = Cars.initializeWithNumberOfCars(3, FixedPowerGenerator(3))
19+
val cars = Cars.initialize(3, listOf("a", "b", "c"), FixedPowerGenerator(3))
2020

2121
val result = cars.move()
2222

0 commit comments

Comments
 (0)