Skip to content

Commit

Permalink
feat: 자동차의 이름을 받는 기능 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
be-student committed Nov 5, 2023
1 parent 0dfdd49 commit 3c8cc6a
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 12 deletions.
14 changes: 11 additions & 3 deletions src/main/kotlin/racingcar/Car.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package racingcar

class Car(val position: Int) {
constructor() : this(INITIAL_POSITION)
class Car(val name: String, val position: Int) {
init {
require(name.length in MINIMUM_CAR_NAME_LENGTH..MAXIMUM_CAR_NAME_LENGTH) {
"자동차의 이름은 ${MINIMUM_CAR_NAME_LENGTH}글자 이상 ${MAXIMUM_CAR_NAME_LENGTH}글자 이하만 가능합니다."
}
}

constructor(name: String) : this(name, INITIAL_POSITION)

fun move(power: Int): Car {
validatePower(power)
if (power >= MINIMUM_AMOUNT_TO_MOVE) {
return Car(position + 1)
return Car(name, position + 1)
}
return this
}
Expand All @@ -22,5 +28,7 @@ class Car(val position: Int) {
const val MINIMUM_AMOUNT_TO_MOVE = 4
const val MAXIMUM_MOVE_POWER = 9
const val MINIMUM_MOVE_POWER = 0
const val MINIMUM_CAR_NAME_LENGTH = 1
const val MAXIMUM_CAR_NAME_LENGTH = 5
}
}
4 changes: 2 additions & 2 deletions src/main/kotlin/racingcar/Cars.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ class Cars(val cars: List<Car>, private val powerGenerator: PowerGenerator) {
}

companion object {
fun initializeWithNumberOfCars(numberOfCars: Int, powerGenerator: PowerGenerator): Cars {
fun initialize(numberOfCars: Int, names: List<String>, powerGenerator: PowerGenerator): Cars {
val cars = mutableListOf<Car>()
repeat(numberOfCars) {
cars.add(Car())
cars.add(Car(names[it]))
}
return Cars(cars, powerGenerator)
}
Expand Down
5 changes: 5 additions & 0 deletions src/main/kotlin/racingcar/InputView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,9 @@ object InputView {
println("시도할 회수는 몇 회 인가요?")
return readln().toInt()
}

fun inputNameOfCars(): List<String> {
println("자동차 이름을 입력하세요. (이름은 쉼표(,) 기준으로 구분)")
return readln().split(",")
}
}
5 changes: 3 additions & 2 deletions src/main/kotlin/racingcar/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package racingcar

fun main() {
val numberOfCars = InputView.inputNumberOfCars()
val numberOfMoves = InputView.inputNumberOfMoves()
val namesOfCars = InputView.inputNameOfCars()
val cars = Cars.initialize(numberOfCars, namesOfCars, RandomPowerGenerator)

val cars = Cars.initializeWithNumberOfCars(numberOfCars, RandomPowerGenerator)
val numberOfMoves = InputView.inputNumberOfMoves()

OutputView.printResultTitle()

Expand Down
15 changes: 12 additions & 3 deletions src/test/kotlin/racingcar/CarTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,34 @@ import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.ValueSource

internal class CarTest {
@Test
fun `자동차의 이름은 1자 이상이어야 한다`() {
assertThrows<IllegalArgumentException> { Car("") }
}

@Test
fun `자동차의 이름은 5자 이하여야 한다`() {
assertThrows<IllegalArgumentException> { Car("123456") }
}

@Test
fun `자동차는 4 이상이 들어오면 움직인다`() {
val car = Car()
val car = Car("name")
val result = car.move(4)
assertThat(result.position).isOne()
}

@Test
fun `자동차는 4 미만이 들어오면 움직이지 않는다`() {
val car = Car()
val car = Car("name")
val result = car.move(3)
assertThat(result.position).isZero()
}

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

assertThrows<IllegalArgumentException> {
car.move(power)
Expand Down
4 changes: 2 additions & 2 deletions src/test/kotlin/racingcar/CarsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ internal class CarsTest {

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

val result = cars.move()

Expand All @@ -16,7 +16,7 @@ internal class CarsTest {

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

val result = cars.move()

Expand Down

0 comments on commit 3c8cc6a

Please sign in to comment.