-
Notifications
You must be signed in to change notification settings - Fork 118
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[둘리] 2단계 자동차 경주 제출합니다. #68
Changes from 1 commit
3e97c71
cb95bad
89d4dd8
e37d339
affbefb
b6834c8
1b39386
e818230
18e68de
f282093
4e8ad55
90c2b1d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,20 +2,15 @@ package model | |
|
||
import generator.RandomGenerator | ||
|
||
class Cars(private val cars: List<Car>) { | ||
constructor(input: String) : this(input.split(",").mapIndexed { _, name -> Car(name.trim()) }) | ||
class Cars(private val _cars: List<Car>) { | ||
val cars: List<Car> | ||
get() = _cars | ||
|
||
fun getCarInfos(): List<CarInfo> { | ||
val carInfos = mutableListOf<CarInfo>() | ||
repeat(cars.size) { | ||
carInfos.add(cars[it].getInfo()) | ||
} | ||
return carInfos | ||
} | ||
constructor(input: String) : this(input.split(",").mapIndexed { _, name -> Car(name.trim()) }) | ||
|
||
fun getCarInfo(index: Int): CarInfo = cars[index].getInfo() | ||
fun getCarSize(): Int = cars.size | ||
fun getCar(index: Int): Car = _cars[index] | ||
fun getCarSize(): Int = _cars.size | ||
fun move(index: Int) { | ||
cars[index].move(RandomGenerator().getRandomNumber()) | ||
_cars[index].move(RandomGenerator().getRandomNumber()) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. move 함수를 잘 구현해주셨네요. 👍 |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,8 +4,7 @@ import model.Cars | |
|
||
object CarsHelper { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 객체 이름만 봤을 때는 "자동차들의 도우미"로 해석이 되는데요. |
||
fun findWinners(cars: Cars): List<String> { | ||
val carInfos = cars.getCarInfos() | ||
val equalCars = carInfos.groupBy({ it.position }, { it.name }) | ||
val equalCars = cars.cars.groupBy({ it.position }, { it.name }) | ||
return equalCars[equalCars.keys.max()]?.toList() ?: listOf() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 방어적 복사 활용 👍 |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,10 +6,10 @@ import org.junit.jupiter.api.assertThrows | |
class CarTest { | ||
|
||
@Test | ||
fun getInfoTest() { | ||
fun initTest() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 지금과 같은 초기화 테스트보다는, 내가 원하는 자동차 이름을 넣었을 때 해당 이름으로 객체가 생성되는지를 확인한다는 의미를 담아보면 어떨까요? |
||
val car = Car("dool") | ||
assertThat(car.getInfo().name).isEqualTo("dool") | ||
assertThat(car.getInfo().position).isEqualTo(0) | ||
assertThat(car.name).isEqualTo("dool") | ||
assertThat(car.position).isEqualTo(0) | ||
} | ||
|
||
@Test | ||
|
@@ -23,13 +23,13 @@ class CarTest { | |
fun moveTest() { | ||
val car = Car("dool") | ||
car.move(3) | ||
assertThat(car.getInfo().position).isEqualTo(0) | ||
assertThat(car.position).isEqualTo(0) | ||
} | ||
|
||
@Test | ||
fun dontMoveTest() { | ||
val car = Car("dool") | ||
car.move(4) | ||
assertThat(car.getInfo().position).isEqualTo(1) | ||
assertThat(car.position).isEqualTo(1) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
코틀린에서 변수에 언더바(_)를 붙이는 것은, backing property를 사용할 때 붙여요.
생성자에 언더바를 붙이지 않고 backing property를 사용하려면 아래와 같이 하시면 됩니다. (이하 관련 내용 동일)
ref. https://kotlinlang.org/docs/coding-conventions.html#names-for-backing-properties