Skip to content

Commit

Permalink
feat(InputValidator): 숫자로 입력하지 않은 경우 예외 발생
Browse files Browse the repository at this point in the history
  • Loading branch information
homebdy committed Sep 8, 2023
1 parent 068ebe5 commit c9a0cd1
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 3 deletions.
2 changes: 1 addition & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
- [x] 이름이 5글자가 넘을 경우 예외 발생
- [x] 횟수 입력 안내 출력
- [x] 횟수 입력
- [ ] 숫자로 입력하지 않은 경우 예외 발생
- [x] 숫자로 입력하지 않은 경우 예외 발생
- [x] 입력받은 횟수만큼 자동차 이동
- [x] 랜덤으로 숫자를 뽑는다.
- [x] 뽑은 숫자가 4이상일 경우 이동한다.
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/racingcar/constant/ExceptionMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
public enum ExceptionMessage {

INVALID_LENGTH("이름은 5글자 이하로 입력해야합니다."),
INCORRECT_DELIMITER("','로 구분하여 입력해야합니다.");
INCORRECT_DELIMITER("','로 구분하여 입력해야합니다."),
NOT_INTEGER("숫자로 입력해야 합니다.");

private static final String PREFIX = "[ERROR] ";
private final String message;
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/racingcar/validator/InputValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,19 @@
public class InputValidator {

private static final String DELIMITER_REGEXP = "^[a-zA-Zㄱ-힣0-9,]*$";
private static final String NUMBER_REGEXP = "^\\d*$";

public void validateDelimiter(String input) {
if (!Pattern.matches(DELIMITER_REGEXP, input)) {
ExceptionMessage exceptionMessage = ExceptionMessage.INCORRECT_DELIMITER;
throw new IllegalArgumentException(exceptionMessage.toString());
}
}

public void validateIsNumber(String input) {
if (!Pattern.matches(NUMBER_REGEXP, input)) {
ExceptionMessage exceptionMessage = ExceptionMessage.NOT_INTEGER;
throw new IllegalArgumentException(exceptionMessage.toString());
}
}
}
4 changes: 3 additions & 1 deletion src/main/java/racingcar/view/InputView.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public List<Name> readNames() {
}

public int readTryCount() {
return Integer.parseInt(Console.readLine());
String input = Console.readLine();
inputValidator.validateIsNumber(input);
return Integer.parseInt(input);
}
}

0 comments on commit c9a0cd1

Please sign in to comment.