-
Notifications
You must be signed in to change notification settings - Fork 175
Expand file tree
/
Copy pathHintResult.java
More file actions
61 lines (51 loc) · 1.58 KB
/
HintResult.java
File metadata and controls
61 lines (51 loc) · 1.58 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
54
55
56
57
58
59
60
61
package baseball.domain.models;
import baseball.domain.exceptions.InvalidHintResultException;
public class HintResult {
private static final int MAX_COUNT = 3;
private static final String NOTHING_MESSAGE = "낫싱";
private static final String STRIKE_SUFFIX = "스트라이크";
private static final String BALL_SUFFIX = "볼";
private final int strikeCount;
private final int ballCount;
public HintResult(int strikeCount, int ballCount) {
validateCount(strikeCount);
validateCount(ballCount);
validateTotal(strikeCount, ballCount);
this.strikeCount = strikeCount;
this.ballCount = ballCount;
}
public int strikeCount() {
return strikeCount;
}
public int ballCount() {
return ballCount;
}
public String message() {
if (strikeCount == 0 && ballCount == 0) {
return NOTHING_MESSAGE;
}
StringBuilder builder = new StringBuilder();
appendCount(builder, strikeCount, STRIKE_SUFFIX);
appendCount(builder, ballCount, BALL_SUFFIX);
return builder.toString();
}
private void appendCount(StringBuilder builder, int count, String suffix) {
if (count == 0) {
return;
}
if (builder.length() > 0) {
builder.append(" ");
}
builder.append(count).append(suffix);
}
private void validateCount(int count) {
if (count < 0 || count > MAX_COUNT) {
throw new InvalidHintResultException("카운트는 0에서 3 사이여야 해요.");
}
}
private void validateTotal(int strikeCount, int ballCount) {
if (strikeCount + ballCount > MAX_COUNT) {
throw new InvalidHintResultException("스트라이크와 볼의 합은 3을 넘을 수 없어요.");
}
}
}