-
Notifications
You must be signed in to change notification settings - Fork 134
Expand file tree
/
Copy pathInputValidator.java
More file actions
35 lines (30 loc) · 1.21 KB
/
InputValidator.java
File metadata and controls
35 lines (30 loc) · 1.21 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
package utils;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class InputValidator {
public static void validateNames(List<String> input) {
if (input.size() < 2) {
throw new IllegalArgumentException("[ERROR] 자동차 이름은 2개 이상 입력해야 합니다.");
}
for (String name : input) {
if (name.isEmpty() || name.length() > 5) {
throw new IllegalArgumentException("[ERROR] 자동차 이름은 1~5글자여야 합니다.");
}
}
Set<String> names = new HashSet<>(input);
if (names.size() != input.size()) {
throw new IllegalArgumentException("[ERROR] 자동차 이름은 중복될 수 없습니다.");
}
}
public static void validateAttemptNum(String input) {
try {
int num = Integer.parseInt(input);
if (num < 1) {
throw new IllegalArgumentException("[ERROR] 시도 횟수는 0 이하가 될 수 없습니다.");
}
} catch (NumberFormatException e) {
throw new IllegalArgumentException("[ERROR] 시도 횟수는 1 이상의 자연수로 입력해야 합니다.");
}
}
}