-
Notifications
You must be signed in to change notification settings - Fork 174
Expand file tree
/
Copy pathUserGuess.java
More file actions
53 lines (43 loc) · 1.54 KB
/
UserGuess.java
File metadata and controls
53 lines (43 loc) · 1.54 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
50
51
52
53
package baseball.model;
import baseball.exception.CommonErrorCode;
import baseball.exception.InvalidInputException;
import lombok.AllArgsConstructor;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@AllArgsConstructor
public class UserGuess {
private final List<Integer> digits;
private static final int DIGIT_COUNT = 3;
private static final int MIN_NUMBER = 1;
private static final int MAX_NUMBER = 9;
public static UserGuess from(String input) {
if (input == null) {
throw new InvalidInputException(CommonErrorCode.EMPTY_INPUT);
}
if (input.length() != DIGIT_COUNT) {
throw new InvalidInputException(CommonErrorCode.INVALID_LENGTH);
}
List<Integer> digits = new ArrayList<>(DIGIT_COUNT);
Set<Integer> dupCheck = new HashSet<>();
for (int i = 0; i < DIGIT_COUNT; i++) {
char c = input.charAt(i);
if (!Character.isDigit(c)) {
throw new InvalidInputException(CommonErrorCode.NOT_A_NUMBER);
}
int d = c - '0';
if (d < MIN_NUMBER || d > MAX_NUMBER) {
throw new InvalidInputException(CommonErrorCode.NUMBER_OUT_OF_RANGE);
}
if (!dupCheck.add(d)) {
throw new InvalidInputException(CommonErrorCode.DUPLICATED_NUMBER);
}
digits.add(d);
}
return new UserGuess(digits);
}
public int digitAt(int index) {
return digits.get(index);
}
}