forked from woowacourse-precourse/java-baseball-6
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInputView.java
More file actions
35 lines (28 loc) · 1002 Bytes
/
InputView.java
File metadata and controls
35 lines (28 loc) · 1002 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package baseball.view;
import java.util.Scanner;
import java.util.regex.Pattern;
public class InputView {
private Scanner scanner = new Scanner(System.in);
private final String threeDigitPattern = "^\\d{3}$";
private final String oneOrTwoPattern = "^(1|2)$";
public String scanThreeDigit() {
String input = scanner.next();
isNotThreeDigitNumber(input);
return input;
}
public int scanRestartOrEnd() {
String input = scanner.next();
isNotOneOrTwo(input);
return Integer.parseInt(input);
}
private void isNotThreeDigitNumber(String input) {
if (!Pattern.matches(threeDigitPattern, input)) {
throw new NumberFormatException("3자리 숫자만 입력되어야 합니다.");
}
}
private void isNotOneOrTwo(String input) {
if (!Pattern.matches(oneOrTwoPattern, input)) {
throw new NumberFormatException("1이나 2만 입력되어야 합니다.");
}
}
}