-
Notifications
You must be signed in to change notification settings - Fork 411
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
π 5λ¨κ³ - μλμ°¨ κ²½μ£Ό(리ν©ν°λ§) #1742
base: aimbe
Are you sure you want to change the base?
Changes from all commits
02c22f8
6733ca7
466aabf
8fa1eef
a2bfca2
4043a93
9eae42f
4ba9fb2
a33ed7e
38cfee7
a9a9aaf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,6 @@ | ||
|
||
# μꡬμ¬ν μ 리 | ||
|
||
## Step 4. μλμ°¨ κ²½μ£Ό(μ°μΉμ) | ||
- [x] κ²μ μ§ν μ μλμ°¨μ μ΄λ¦μ λ°λλ€. | ||
- [x] RacingCar κ°μ²΄μ μ΄λ¦μ λΆμ¬νλ κΈ°λ₯μ λ§λ λ€ | ||
- [x] μ°μΉμλ₯Ό μ μ νλ μ± μμ κ°μ§ κ°μ²΄λ₯Ό μμ±νλ€ | ||
- [x] κ²μ μλ£ ν μ°μΉμλ₯Ό μΆλ ₯νλ€. | ||
##π 5λ¨κ³ - μλμ°¨ κ²½μ£Ό(리ν©ν°λ§) | ||
- [x] ν΅μ¬ λΉμ§λμ€ λ‘μ§μ κ°μ§λ κ°μ²΄λ₯Ό domain ν¨ν€μ§, UI κ΄λ ¨ν κ°μ²΄λ₯Ό view ν¨ν€μ§μ ꡬννλ€. | ||
- [x] λͺ¨λ μμ‘΄μ±μ λν΄ κ³ λ €νλ©° μ€κ³νλ€. | ||
- [x] μ΄μ 리뷰 νΌλλ°±μ λ°μνμ¬ κ΅¬ννλ€. |
This file was deleted.
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package misson.car.domain | ||
|
||
class CarPositionFormatter { | ||
companion object { | ||
fun formatCarPosition(car: RacingCar): String { | ||
return "μλμ°¨ ${car.name} : ${formatPosition(car.position)}" | ||
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. λ·° μꡬ μ¬νμ μμ‘΄νκ³ μλ μ΄ λ‘μ§μ΄
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. μ무λλ domainμ μκΈ΄ μλ§κ³ viewμ μκΈ°μλ μ 맀ν΄μ util ν¨ν€μ§ μμ± ν μ΄λμμΌ°μ΅λλ€! γ γ |
||
} | ||
|
||
fun formatPosition(position: Int): String { | ||
return "-".repeat(position) | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package misson.car.domain | ||
|
||
// μ½νλ¦° λ¬Έλ²μμ positionμ privateμΌλ‘ νμ§ μμλ λ΄λΆμ μΌλ‘λ getterλ‘ μ κ·Ό | ||
class RacingCar(val name: String = "λΆλΆμ΄", var position: Int = 0) { | ||
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. νμ¬λ positionμ΄ privateνμ§ μκΈ° λλ¬Έμ μΈλΆμμ position κ°μ λ§μλλ‘ μμ ν μ μμ΄μ.
μ΄λ»κ² κ°μ ν΄λ³Ό μ μμκΉμ? 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. "λΆλΆμ΄"λ λ무 κ·μ½μ§λ§(γ γ γ ) ν μ€νΈ νΈμμ±μ μν΄ λ§λ€μ΄μ§ κ°μ΄λ―λ‘ νλ‘λμ μ½λμ μΆκ°λκΈ°μλ μ ν©νμ§ μμ κ² κ°μμ. |
||
init { | ||
validateName(name) | ||
} | ||
|
||
private fun validateName(name: String) { | ||
require(name.length <= 5) { "μ΄λ¦μ 5μ μ΄νλ§ κ°λ₯ν©λλ€." } | ||
require(name.isNotBlank()) { "μ΄λ¦μ κ³΅λ°±μΌ μ μμ΅λλ€." } | ||
} | ||
|
||
fun move(randomNumberGenerator: () -> Int) { | ||
if (randomNumberGenerator() >= 4) { | ||
position++ | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package misson.car.domain | ||
|
||
class RacingCars(val cars: List<RacingCar>) { | ||
fun moveAll(randomNumberGenerator: () -> Int) { | ||
cars.forEach { it.move(randomNumberGenerator) } | ||
} | ||
|
||
// μ°μΉμλ₯Ό μ°Ύλ μ± μ μ΄λ | ||
fun findWinners(): Winners { | ||
val maxPosition = cars.maxOf { it.position } | ||
val winners = cars.filter { it.position == maxPosition } | ||
return Winners(winners) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package misson.car.domain | ||
|
||
@JvmInline | ||
value class Winners(private val winners: List<RacingCar>) { | ||
fun getNames(): List<String> { | ||
return winners.map { it.name } | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,19 @@ | ||
package view | ||
|
||
import misson.car.domain.CarPositionFormatter | ||
import misson.car.domain.RacingCars | ||
import misson.car.domain.Winners | ||
|
||
object ResultView { | ||
fun printRoundResult(positions: List<String>) { | ||
positions.forEach { println(it) } | ||
fun printRoundResult(racingCars: RacingCars) { | ||
racingCars.cars | ||
.map { CarPositionFormatter.formatCarPosition(it) } | ||
.forEach { println(it) } | ||
println() | ||
} | ||
|
||
fun printWinner(winners: String) { | ||
println("μ΅μ’ μ°μΉμλ $winners μ λλ€.") | ||
fun printWinner(winners: Winners) { | ||
val winnerNames = winners.getNames().joinToString(", ") | ||
println("μ΅μ’ μ°μΉμλ $winnerNames μ λλ€.") | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,21 @@ | ||
package misson | ||
|
||
import misson.car.RacingCar | ||
import misson.car.domain.RacingCar | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.assertThrows | ||
|
||
class RacingCarTest { | ||
@Test | ||
fun `μ΄λ¦μ΄ 5μλ₯Ό μ΄κ³Όνλ©΄ μμΈκ° λ°μνλ€`() { | ||
assertThrows<IllegalArgumentException> { | ||
RacingCar("1234567") | ||
} | ||
assertThat(assertThrows<IllegalArgumentException> { RacingCar("1234567") }.message) | ||
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. RacingCarμ position κ°μ μ΄κΈ°ννλ€λ κ²μ λͺ μμ μΌλ‘ λλ¬λ΄κΈ° μν΄ named argumentsλ₯Ό νμ©νμλ©΄ μ΄λ¨κΉμ? RacingCar(position = "1234567") μ΄ μΈμλ named argumentsλ₯Ό νμ©ν κ³³μ΄ μλμ? νλ‘μ νΈ μ λ°μ μΌλ‘ μ μ©λ μ μλ λ³κ²½μ¬νμΈ κ² κ°μμ! |
||
.isEqualTo("μ΄λ¦μ 5μ μ΄νλ§ κ°λ₯ν©λλ€.") | ||
} | ||
|
||
@Test | ||
fun `μ΄λ¦μ΄ λΉ λ¬Έμμ΄μ΄λ©΄ μμΈκ° λ°μνλ€`() { | ||
assertThrows<IllegalArgumentException> { | ||
RacingCar("") | ||
} | ||
assertThat(assertThrows<IllegalArgumentException> { RacingCar("") }.message) | ||
.isEqualTo("μ΄λ¦μ κ³΅λ°±μΌ μ μμ΅λλ€.") | ||
} | ||
|
||
@Test | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,31 @@ | ||
package misson | ||
|
||
import misson.car.RacingCar | ||
import misson.car.RacingCars | ||
import misson.car.domain.RacingCar | ||
import misson.car.domain.RacingCars | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.params.ParameterizedTest | ||
import org.junit.jupiter.params.provider.CsvSource | ||
|
||
class RacingCarsTest { | ||
@Test | ||
fun `μλμ°¨ κ·Έλ£Ήμ νλ²μ μ΄λμν¨λ€`() { | ||
val racingCars = RacingCars(List(3) { RacingCar() }) | ||
racingCars.moveAll { 4 } | ||
|
||
val positions = racingCars.getPositions() | ||
assertThat(positions).containsExactly("-", "-", "-") | ||
racingCars.cars.forEach { | ||
assertThat(it.position).isEqualTo(1) | ||
} | ||
} | ||
|
||
@Test | ||
fun `μλμ°¨ κ·Έλ£Ήμ κ°μ΄ 4μ΄ν μ΄λ©΄ μ΄λνμ§ μλλ€`() { | ||
val racingCars = RacingCars(List(3) { RacingCar() }) | ||
racingCars.moveAll { 3 } | ||
|
||
val positions = racingCars.getPositions() | ||
assertThat(positions).containsExactly("", "", "") | ||
racingCars.cars.forEach { | ||
assertThat(it.position).isEqualTo(1) | ||
} | ||
} | ||
|
||
@Test | ||
|
@@ -36,7 +40,7 @@ class RacingCarsTest { | |
) | ||
|
||
val winners = racingCars.findWinners() | ||
assertThat(winners.representWinners()).isEqualTo("car1") | ||
assertThat(winners.getNames()).containsExactly("car1") | ||
} | ||
|
||
@Test | ||
|
@@ -51,6 +55,25 @@ class RacingCarsTest { | |
) | ||
|
||
val winners = racingCars.findWinners() | ||
assertThat(winners.representWinners()).isEqualTo("car1, car2") | ||
assertThat(winners.getNames()).containsExactly("car1", "car2") | ||
} | ||
|
||
@ParameterizedTest | ||
@CsvSource( | ||
"4, 1", | ||
"5, 1", | ||
"3, 0", | ||
"2, 0", | ||
) | ||
fun `Param κ°μ λ°λΌ μλμ°¨ κ·Έλ£Ήμ΄ μ΄λνλ€`( | ||
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. ν μ€νΈλͺ μ ν΅ν΄ μ΄λ€ κ²μ ν μ€νΈνκ³ μ νλ κ²μ΄ μ λλ‘ λλ¬λμ§ μλ κ² κ°μμ. ν μ€νΈλͺ μ μμ±νμ€ λμλ
μ΄μμ μΌλ‘λ κΈ°λ₯ μꡬ μ¬νμΌλ‘ μ 리νμ λ¬Έμμ μλ리μ€κ° κ·Έλλ‘ ν μ€νΈ μλ리μ€λ‘λ μ΄μ΄μ§λ©΄ μ’μ΅λλ€ π |
||
randomValue: Int, | ||
expectedPosition: Int, | ||
) { | ||
val racingCars = RacingCars(List(3) { RacingCar() }) | ||
racingCars.moveAll { randomValue } | ||
|
||
racingCars.cars.forEach { car -> | ||
assertThat(car.position).isEqualTo(expectedPosition) | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package misson.car | ||
|
||
import misson.car.domain.CarPositionFormatter | ||
import misson.car.domain.RacingCar | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.params.ParameterizedTest | ||
import org.junit.jupiter.params.provider.CsvSource | ||
|
||
class CarPositionFormatterTest { | ||
@Test | ||
fun `μλμ°¨ μμΉλ§λ€ -λ₯Ό , μμ μλμ°¨ μ΄λ¦μ λΆμΈλ€`() { | ||
val car = RacingCar("car1", 3) | ||
val result = CarPositionFormatter.formatCarPosition(car) | ||
|
||
assertThat(result).isEqualTo("μλμ°¨ car1 : ---") | ||
} | ||
|
||
@ParameterizedTest | ||
@CsvSource( | ||
"0, ''", | ||
"1, '-'", | ||
"3, '---'", | ||
"5, '-----'", | ||
) | ||
fun `positionλ§λ€ -λ₯Ό λΆμΈλ€`( | ||
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. λ€μ μꡬ μ¬νμ λ€μ ν λ² κ³ λ €ν΄λ³΄λ©΄ μ΄λ¨κΉμ~?
|
||
position: Int, | ||
expected: String, | ||
) { | ||
val result = CarPositionFormatter.formatPosition(position) | ||
|
||
assertThat(result).isEqualTo(expected) | ||
} | ||
} |
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.
λ§μ§λ§ λ¨κ³μΈ λ§νΌ μ’ λ κ°μ ν΄λ³Ό μ μλ μ¬λ¬ μμ΄λμ΄λ₯Ό μ μνκ³ μΆμ΄μ π
ResultView νΈμΆμ μ΅λν μ€μ¬λ³΄λ©΄ μ΄λ¨κΉμ?
μ°λ¦¬κ° νν μΉ νλ‘κ·Έλ¨μμ μλ²μ ν΄λΌμ΄μΈνΈ κ°μ μν΅νλκ±Έ μκ°ν΄λ³΄λ©΄
μλ²μ ν΄λΌμ΄μΈνΈκ° μλ‘ μμ²κ³Ό μλ΅μ μ£Όκ³ λ°λ κ³Όμ μ λͺ¨λ λΉμ©μ λλ€. μ΄λ¬ν λΉμ©μ μ€μ΄λ κ²μ΄ μΉ νλ‘κ·Έλλ°μμ μ€μνκ² μ¬κ²¨μ§κΈ°λ ν©λλ€.
λ μ§κΈμ λ¨μν μ½μ νλ‘κ·Έλ¨μ΄μ§λ§, λμ€μ λ·°κ° μλλ‘μ΄λ μ±μΌλ‘ λ°λλ€λμ§, μΉνμ΄μ§ λ±μΌλ‘ λ°λλ€κ³ νλ©΄ μ΄λ¬ν νΈμΆμ΄ λͺ¨λ λΉμ©μ΄ λ©λλ€.