forked from woowacourse-precourse/java-baseball-6
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathEndNumber.java
More file actions
35 lines (28 loc) · 989 Bytes
/
EndNumber.java
File metadata and controls
35 lines (28 loc) · 989 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.domain;
import java.util.regex.Pattern;
public class EndNumber {
public static final Integer END_LENGTH = 1;
private final String REGEXP_PATTERN_FORMAT = String.format("[1-2]{%d}", END_LENGTH);
private final Integer number;
public EndNumber(String input) {
validate(input);
this.number = Integer.parseInt(input);
}
private void validate(String input) {
isCorrectLength(input);
isCorrectNumber(input);
}
private void isCorrectLength(String input) {
if (input.length() != END_LENGTH) {
throw new IllegalArgumentException("입력의 길이가 알맞지 않습니다.");
}
}
private void isCorrectNumber(String input) {
if (!Pattern.matches(REGEXP_PATTERN_FORMAT, input)) {
throw new IllegalArgumentException("입력값이 형식을 만족하지 않습니다.");
}
}
public boolean isEnd() {
return this.number == 2;
}
}