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

[둘리] 2단계 자동차 경주 제출합니다. #68

Merged
merged 12 commits into from
Feb 15, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
Prev Previous commit
Next Next commit
refactor(InputView,RaceGame): 잘못된 값에 대한 함수 반복 여부 RaceGame에서 결정하도록 수정
  • Loading branch information
hyemdooly committed Feb 13, 2023
commit f282093546cff228613769cc9aa2464b95015631
23 changes: 21 additions & 2 deletions src/main/kotlin/controller/RaceGame.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package controller

import generator.RandomGenerator
import model.Cars
import util.Constants
import util.WinnersFinder
import view.InputView
import view.OutputView
Expand All @@ -10,9 +11,9 @@ class RaceGame(private val outputView: OutputView, private val inputView: InputV

fun run() {
outputView.outputCarNames()
val cars = Cars(inputView.inputCarNames())
val cars = Cars(inputCarNames())
outputView.outputTryNumber()
val tryNumber = inputView.inputTryNumber().toInt()
val tryNumber = inputTryNumber().toInt()
outputView.outputResults()
repeat(tryNumber) {
tryMove(cars)
Expand All @@ -27,4 +28,22 @@ class RaceGame(private val outputView: OutputView, private val inputView: InputV
}
outputView.outputNextLine()
}

private fun inputCarNames(): String {
var input = inputView.inputCarNames()
while (input.isNullOrBlank() || input.contains("[ERROR]")) {
outputView.outputErrorMessage(input ?: Constants.INPUT_IS_NULL)
input = inputView.inputCarNames()
}
return input
}

private fun inputTryNumber(): String {

Choose a reason for hiding this comment

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

함수는 행위 혹은 행동을 의미하기 때문에, 동사구로 작성하곤 합니다.
하지만 이 함수에는 동사가 2개가 존재합니다. (input, try)
함수에는 하나의 동사만 포함시켜보면 어떨까요? (이하 관련 내용 동일)

var input = inputView.inputTryNumber()
while (input.isNullOrBlank() || input.contains("[ERROR]")) {
outputView.outputErrorMessage(input ?: Constants.INPUT_IS_NULL)
input = inputView.inputTryNumber()
}
return input
}
}
11 changes: 6 additions & 5 deletions src/main/kotlin/util/Constants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ object Constants {
const val INPUT_TRY_NUMBER = "시도할 횟수는 몇 회인가요?"
const val OUTPUT_RESULT = "실행 결과"
const val OUTPUT_WINNER = "최종 우승자: "
const val INPUT_NAME_SIZE_ERROR_MESSAGE = "자동차 이름의 길이가 5를 초괴합니다."
const val INPUT_NAME_NULL_ERROR_MESSAGE = "자동차 이름이 입력되지 않았습니다."
const val INPUT_TRY_NUMBER_NULL_ERROR_MESSAGE = "시도할 횟수가 입력되지 않았습니다."
const val INPUT_TRY_NUMBER_RIGHT_ERROR_MESSAGE = "시도할 횟수가 올바르게 입력되지 않았습니다."
const val INPUT_NAME_RIGHT_ERROR_MESSAGE = "자동차 이름은 한글과 영어만 가능합니다."
const val INPUT_NAME_SIZE_ERROR_MESSAGE = "[ERROR] 자동차 이름의 길이가 5를 초괴합니다."
const val INPUT_NAME_NULL_ERROR_MESSAGE = "[ERROR] 자동차 이름이 입력되지 않았습니다."
const val INPUT_TRY_NUMBER_NULL_ERROR_MESSAGE = "[ERROR] 시도할 횟수가 입력되지 않았습니다."
const val INPUT_TRY_NUMBER_RIGHT_ERROR_MESSAGE = "[ERROR] 시도할 횟수가 올바르게 입력되지 않았습니다."
const val INPUT_NAME_RIGHT_ERROR_MESSAGE = "[ERROR] 자동차 이름은 한글과 영어만 가능합니다."
const val INPUT_IS_NULL = "[ERROR] 입력 값이 널입니다."
}
19 changes: 6 additions & 13 deletions src/main/kotlin/util/Validator.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package util

import view.OutputView

class Validator {

fun checkNames(names: String?) {
Expand All @@ -24,31 +22,26 @@ class Validator {
}

private fun checkNameNull(name: String?) {
require(name != null) { OutputView().outputErrorMessage(Constants.INPUT_NAME_NULL_ERROR_MESSAGE) }
require(name != null) { Constants.INPUT_NAME_NULL_ERROR_MESSAGE }
}

private fun checkNameSize(name: String) {
require(name.length < 5) { OutputView().outputErrorMessage(Constants.INPUT_NAME_SIZE_ERROR_MESSAGE) }
require(name.length < 5) { Constants.INPUT_NAME_SIZE_ERROR_MESSAGE }
}

private fun checkNameEmpty(name: String) {
require(name != "") { OutputView().outputErrorMessage(Constants.INPUT_NAME_NULL_ERROR_MESSAGE) }
require(name != "") { Constants.INPUT_NAME_NULL_ERROR_MESSAGE }
}

private fun checkNameRight(name: String) {
require(name.contains("^[a-zA-Z가-힣]*$".toRegex())) { OutputView().outputErrorMessage(Constants.INPUT_NAME_RIGHT_ERROR_MESSAGE) }
require(name.contains("^[a-zA-Z가-힣]*$".toRegex())) { Constants.INPUT_NAME_RIGHT_ERROR_MESSAGE }
}

private fun checkTryNumberNull(number: String?) {
require(number != null) { OutputView().outputErrorMessage(Constants.INPUT_TRY_NUMBER_NULL_ERROR_MESSAGE) }
require(number != null) { Constants.INPUT_TRY_NUMBER_NULL_ERROR_MESSAGE }
}

private fun checkTryNumberIsRight(number: String) {
require(
number.isNotEmpty() && number.chars().allMatch { Character.isDigit(it) }) {
OutputView().outputErrorMessage(
Constants.INPUT_TRY_NUMBER_RIGHT_ERROR_MESSAGE
)
}
require(number.isNotEmpty() && number.chars().allMatch { Character.isDigit(it) }) { Constants.INPUT_TRY_NUMBER_RIGHT_ERROR_MESSAGE }
}
}
20 changes: 10 additions & 10 deletions src/main/kotlin/view/InputView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@ package view
import util.Validator

class InputView {
fun inputCarNames(): String {
val input = readlnOrNull()
fun inputCarNames(): String? {
var input = readlnOrNull()
runCatching {
Validator().checkNames(input)
}.onFailure {
return inputCarNames()
}.getOrNull()
return input ?: inputCarNames()
input = it.message
}
return input
}

fun inputTryNumber(): String {
val input = readlnOrNull()
fun inputTryNumber(): String? {
var input = readlnOrNull()
runCatching {
Validator().checkTryNumber(input)
}.onFailure {
return inputTryNumber()
}.getOrNull()
return input ?: inputTryNumber()
input = it.message
}
return input
}
}