-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathInputCheck.java
More file actions
49 lines (42 loc) · 1.31 KB
/
InputCheck.java
File metadata and controls
49 lines (42 loc) · 1.31 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package utility;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;
import camp.nextstep.edu.missionutils.Console;
public final class InputCheck {
public static List<String> inputCarNames() {
while (true) {
System.out.println("경주할 자동차 이름을 입력하세요.(이름은 쉽표(,) 기준으로 구분)");
List<String> names = Arrays.asList(Console.readLine().split(","));
try {
names.forEach(InputCheck::checkName);
return names;
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
}
}
}
private static void checkName(String name) {
if (name.trim().length() == 0 || name.trim().length() > 5) {
throw new IllegalArgumentException("[ERROR] 이름은 1~5자 사이로 작성하라.");
}
}
public static int inputRound() {
while (true) {
System.out.println("시도할 회수는 몇회인가요?");
try {
String input = Console.readLine();
checkRound(input);
return Integer.parseInt(input);
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
}
}
}
private static void checkRound(String input) {
String pattern = "^[0-9]+$";
if (!Pattern.matches(pattern, input)) {
throw new IllegalArgumentException("[ERROR] 시도 횟수는 자연수인 숫자여야 한다.");
}
}
}