-
Notifications
You must be signed in to change notification settings - Fork 174
Expand file tree
/
Copy pathInputValidator.java
More file actions
49 lines (40 loc) · 1.41 KB
/
InputValidator.java
File metadata and controls
49 lines (40 loc) · 1.41 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 baseball;
import java.util.HashSet;
import java.util.Set;
public class InputValidator {
public void validate(String input) {
validateLength(input);
validateNumeric(input);
validateRange(input);
validateUnique(input);
}
private void validateLength(String input) {
if (input.length() != 3) {
throw new IllegalArgumentException("[ERROR] 3자리의 숫자를 입력해야 합니다.");
}
}
private void validateNumeric(String input) {
if (!input.matches("\\d+")) {
throw new IllegalArgumentException("[ERROR] 숫자만 입력해야 합니다.");
}
}
private void validateRange(String input) {
if (input.contains("0")) {
throw new IllegalArgumentException("[ERROR] 1에서 9 사이의 숫자여야 합니다.");
}
}
private void validateUnique(String input) {
Set<Character> uniqueChars = new HashSet<>();
for (char c : input.toCharArray()) {
uniqueChars.add(c);
}
if (uniqueChars.size() != 3) {
throw new IllegalArgumentException("[ERROR] 서로 다른 숫자를 입력해야 합니다.");
}
}
public void validateRestart(String input) {
if (!input.equals("1") && !input.equals("2")) {
throw new IllegalArgumentException("[ERROR] 1 또는 2를 입력해야 합니다.");
}
}
}