-
Notifications
You must be signed in to change notification settings - Fork 174
Expand file tree
/
Copy pathBaseballController.java
More file actions
73 lines (61 loc) · 1.85 KB
/
BaseballController.java
File metadata and controls
73 lines (61 loc) · 1.85 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
63
64
65
66
67
68
69
70
71
72
73
package baseball.controller;
import baseball.model.Baseball;
import baseball.model.Result;
import baseball.view.InputView;
import baseball.view.OutputView;
public class BaseballController {
private final InputView inputView;
private final OutputView outputView;
private final Baseball baseball;
public BaseballController(
InputView inputView,
OutputView outputView,
Baseball baseball
) {
this.inputView = inputView;
this.outputView = outputView;
this.baseball = baseball;
}
public void run() {
while (!baseball.getStop()) {
String input = readInputWithRetry();
Result result = baseball.calculate(input);
checkResult(result);
}
}
private String readInputWithRetry() {
while (true) {
try {
outputView.printInputMessage();
return inputView.readInputNumber();
} catch (IllegalArgumentException e) {
outputView.printError(e.getMessage());
}
}
}
private void checkResult(Result result) {
outputView.printResultMessage(result);
if (result.getStrike() == 3) {
outputView.printGameOver();
String input = readRestartInputWithRetry();
checkRestart(input);
}
}
private String readRestartInputWithRetry() {
while (true) {
try {
outputView.printRestartMessage();
return inputView.readRestartInput();
} catch (IllegalArgumentException e) {
outputView.printError(e.getMessage());
}
}
}
private void checkRestart(String input) {
if (input.equals("2")) {
baseball.setStop(true);
return;
}
baseball.setAnswer();
}
}