-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResult.java
More file actions
62 lines (51 loc) · 1.93 KB
/
Result.java
File metadata and controls
62 lines (51 loc) · 1.93 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
62
package baseball.domain;
import baseball.domain.constants.ResultType;
import baseball.view.OutputView;
import static baseball.config.GlobalConfig.NUMBER_LENGTH;
import static baseball.domain.constants.ResultType.*;
import static java.lang.String.format;
public class Result {
private final int ballCount;
private final int strikeCount;
private Result(final Number playerNumber, final Number computerNumber) {
ballCount = playerNumber.countBallCount(computerNumber);
strikeCount = playerNumber.countStrikeCount(computerNumber);
}
public static Result create(final Number playerNumber, final Number computerNumber) {
return new Result(playerNumber, computerNumber);
}
private ResultType inspectResultType() {
if (!hasBall() && !hasStrike()) {
return NOTHING;
} else if (hasBall() && hasStrike()) {
return BALL_AND_STRIKE;
} else if (hasBall()) {
return ONLY_BALL;
} else if (hasStrike()) {
return ONLY_STRIKE;
}
throw new IllegalArgumentException("result type error");
}
private String generateResultMessage(ResultType resultType) {
return switch (resultType) {
case NOTHING -> resultType.getValue();
case BALL_AND_STRIKE -> format(BALL_AND_STRIKE.getValue(), ballCount, strikeCount);
case ONLY_BALL -> format(ONLY_BALL.getValue(), ballCount);
case ONLY_STRIKE -> format(ONLY_STRIKE.getValue(), strikeCount);
};
}
public void print() {
ResultType resultType = inspectResultType();
String message = generateResultMessage(resultType);
OutputView.printResult(message);
}
public boolean hasBall() {
return ballCount > 0;
}
public boolean hasStrike() {
return strikeCount > 0;
}
public boolean checkGameOver() {
return strikeCount == NUMBER_LENGTH.getValue();
}
}