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이 아닌 InputView에서 하도록 수정
  • Loading branch information
hyemdooly committed Feb 12, 2023
commit 89d4dd8227b62b254c88dbe6843061765f3bd2a3
41 changes: 5 additions & 36 deletions src/main/kotlin/controller/RaceGame.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package controller

import model.Cars
import util.CarsHelper
import util.Validator
import view.InputView
import view.OutputView

class RaceGame(private val outputView: OutputView, private val inputView: InputView) {

fun run() {
outputView.outputCarNames()
val cars = executeInputCarNames()
outputView.outputTryNumber()
val tryNumber = executeInputTryNumber()
outputView.outputResults()
repeat(tryNumber) {
Expand All @@ -25,39 +27,6 @@ class RaceGame(private val outputView: OutputView, private val inputView: InputV
outputView.outputNextLine()
}

private fun executeInputTryNumber(): Int {
var result: Int?
do {
outputView.outputTryNumber()
result = getInputTryNumber(inputView.inputTryNumber())
} while (result == null)
return result
}

private fun getInputTryNumber(number: String): Int? {
return runCatching {
Validator().checkTryNumber(number)
number.toInt()
}.onFailure {
outputView.outputErrorMessage(it.message ?: "에러가 발생했습니다.")
}.getOrNull()
}

private fun executeInputCarNames(): Cars {
var result: Cars?
do {
outputView.outputCarNames()
result = getInputCarNames(inputView.inputCarNames())
} while (result == null)
return result
}

private fun getInputCarNames(cars: String): Cars? {
return runCatching {
Validator().checkNames(cars)
Cars(cars)
}.onFailure {
outputView.outputErrorMessage(it.message ?: "에러가 발생했습니다.")
}.getOrNull()
}
private fun executeInputTryNumber(): Int = inputView.inputTryNumber().toInt()

Choose a reason for hiding this comment

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

함수명만 봤을 때에는 "InputTryNumber를 실행한다"라는 의미로 보여요.
하지만, executeInputTryNumber함수보다는 inputTryNumber라는 함수가 더 잘 이해가 돼요.
executeInputTryNumber함수를 사용하는 것을 "간접 호출"이라고 합니다.
"간접 호출"은 유용한 경우도 많지만, 자칫 잘못하게 되면 과하게 함수를 호출하게 되는 상황을 만들기도 해요.
불필요한 간접 호출을 줄여보면 어떨까요? (이하 관련 내용 동일)

private fun executeInputCarNames(): Cars = Cars(inputView.inputCarNames())
}
25 changes: 14 additions & 11 deletions src/main/kotlin/util/Validator.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package util

import view.OutputView

Choose a reason for hiding this comment

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

Suggested change
import view.OutputView

domain과 view에 둘 다 접근이 가능한 것은 Controller입니다.
Controller의 역할에 대해서 다시 한번 학습해보시고, Validator는 Controller가 아니므로 OutputView의 의존성을 제거해보면 어떨까요?


class Validator {

fun checkNames(names: String?) {
Expand All @@ -18,34 +20,35 @@ class Validator {

fun checkTryNumber(name: String?) {
checkTryNumberNull(name)
checkTryNumberIsRight(name)
checkTryNumberIsRight(name ?: "")
}

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

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

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

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

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

private fun checkTryNumberIsRight(number: String?) {
try {
number!!.toInt()
} catch (e: NumberFormatException) {
throw IllegalArgumentException(Constants.INPUT_TRY_NUMBER_RIGHT_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
)
}
}
}
18 changes: 16 additions & 2 deletions src/main/kotlin/view/InputView.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
package view

import util.Validator

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

Choose a reason for hiding this comment

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

input이 null일 경우 자신의 함수를 다시 호출해주고 있네요.
InputView의 책임은 입력된 값을 받아서 전달해주는 역할입니다.
해당 함수를 반복할지에 대한 여부는 Controller에서 처리해보면 어떨까요? (이하 관련 내용 동일)

}

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