Skip to content
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

[자동차 경주] 천지윤 미션 제출합니다. #100

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 21 additions & 61 deletions src/main/kotlin/racingcar/Application.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,11 @@ package racingcar

import camp.nextstep.edu.missionutils.Randoms
import camp.nextstep.edu.missionutils.Console
import java.lang.NumberFormatException
import racingcar.Validation.Validation
import racingcar.View.InputView
import racingcar.View.OutputView


class InputView {
fun inputCars() {
println("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)")
}

fun inputCount() {
println("시도할 횟수는 몇 회인가요?")
}
}

class OutputView {
fun outputPrint() {
println("실행 결과")
}

fun gameResult(carName: String, movingCount: Int) {
println("$carName : ${"-".repeat(movingCount)}")
}

fun winnerPrint(winners: List<String>) {
println("최종 우승자 : ${winners.joinToString(", ")}")

}
}

fun carsNameTrim(cars : List<String>): List<String> {
return cars.map { it.trim() }
}
Expand Down Expand Up @@ -75,52 +52,35 @@ fun getWinnerIndex(carsMoving: Array<Int>): MutableList<Int> {
return winners
}

fun inputCars(inputView: InputView, validation: Validation): List<String> {
inputView.inputCars()
val inputCar = Console.readLine()
validation.checkInputCarIsNotEmpty(inputCar)

fun checkCarNameLength(cars: List<String>) {
if (!(cars.all { it -> it.count() <= 5 })) {
throw IllegalArgumentException("자동차 이름은 5자 이하만 가능합니다")
}
}
var cars: List<String> = inputCar.split(',')
validation.checkCarNameDuplication(cars)
cars = carsNameTrim(cars)
validation.checkCarNameLength(cars)

fun checkInputCarIsNotEmpty(inputCar: String) {
if (inputCar == "") {
throw IllegalArgumentException("자동차 이름을 입력하지 않았습니다")
}
return cars
}

fun checkCarNameDuplication(cars: List<String>) {
if (cars.toSet().size != cars.size) {
throw IllegalArgumentException("자동차 이름이 중복입니다")
}

}
fun inputCount(inputView: InputView, validation: Validation): Int {
inputView.inputCount()
val inputCount = Console.readLine()
validation.checkCountIsNumber(inputCount)
val count = inputCount.toInt()

fun checkCountIsNumber(inputCount: String) {
try {
inputCount.toInt()
} catch (e: NumberFormatException) {
throw IllegalArgumentException("정수만 입력가능합니다")
}
return count
}


fun main() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

보통 메인 함수가 가장 최상단에 위치하도록 하는 것 같습니다 :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

그렇군요! 몰랐었는데 감사합니다!

val outputView = OutputView()
val inputView = InputView()
val validation = Validation()

inputView.inputCars()
val inputCar = Console.readLine()
checkInputCarIsNotEmpty(inputCar)
var cars: List<String> = inputCar.split(',')
checkCarNameDuplication(cars)
cars = carsNameTrim(cars)
checkCarNameLength(cars)


inputView.inputCount()
val inputCount = Console.readLine()
checkCountIsNumber(inputCount)
val count = inputCount.toInt()
val cars = inputCars(inputView, validation)
val count = inputCount(inputView, validation)

val carsMovingArray = Array(cars.count()) { 0 }

Expand Down
31 changes: 31 additions & 0 deletions src/main/kotlin/racingcar/Validation/Validation.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package racingcar.Validation

import java.lang.NumberFormatException

class Validation {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저는 아래 케이스에 대해서도 고민을 해봤는데요 한번 생각해보셔도 좋을거 같습니다 !

게임 플레이를 한명만 입력되도 허용할 것인지?
이름에 특수문자를 허용하실 것인지?
,가 아닌 구분자가 들어왔을 때 : 3;4;5

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

감사합니다. 오류는 아니라고 생각해서 하지 않았는데, 사용자가 실수하는 것도 고려해볼 수가 있겠네요!

fun checkCarNameLength(cars: List<String>) {
if (!(cars.all { it -> it.count() <= 5 })) {
throw IllegalArgumentException("자동차 이름은 5자 이하만 가능합니다")
}
Comment on lines +7 to +9

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

람다식에서 사용되는 기본 변수 네임을 it 그대로 사용하실 거라면
it ->은 생략해도 될 것 같네요 !

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오 생략해도 되는지 몰랐네요. 알려주셔서 감사합니다!

}

fun checkInputCarIsNotEmpty(inputCar: String) {
if (inputCar == "") {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isBlank()라는 메서드도 있어요!

inputCar.isBlank()

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

와 감사합니다. 코틀린 문법 하나 또 배웠습니다!!

throw IllegalArgumentException("자동차 이름을 입력하지 않았습니다")
}
}

fun checkCarNameDuplication(cars: List<String>) {
if (cars.toSet().size != cars.size) {
throw IllegalArgumentException("자동차 이름이 중복입니다")
}

}
fun checkCountIsNumber(inputCount: String) {
try {
inputCount.toInt()
} catch (e: NumberFormatException) {
throw IllegalArgumentException("정수만 입력가능합니다")
}
}
}
11 changes: 11 additions & 0 deletions src/main/kotlin/racingcar/View/InputView.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package racingcar.View

class InputView {
fun inputCars() {
println("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)")
}

fun inputCount() {
println("시도할 횟수는 몇 회인가요?")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"시도할 횟수는 몇 회인가요?" 와 같이 변할 수 있는 값을 변수로 빼서 관리하면 유지보수가 수월해질꺼 같습니다!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오 문자열로 한 번 더 빼는 거군요. 감사합니다

}
}
16 changes: 16 additions & 0 deletions src/main/kotlin/racingcar/View/OutputView.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package racingcar.View

class OutputView {
fun outputPrint() {
println("실행 결과")
}

fun gameResult(carName: String, movingCount: Int) {
println("$carName : ${"-".repeat(movingCount)}")
}
Comment on lines +8 to +10

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

함수의 이름이 "게임의 결과"를 출력한다는 의미일까요 ? 그렇다면 printGameResult 는 어떨까요 !


fun winnerPrint(winners: List<String>) {
println("최종 우승자 : ${winners.joinToString(", ")}")

}
Comment on lines +12 to +15

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

코틀린 함수 네이밍 컨벤션에 따르면 동사 + 명사 구조로 네이밍을 하도록 가이드하고 있습니다 !
println처럼 printWinner로 네이밍 하는 것이 맞을 것 같습니다 🤔

}