-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathRacingController.java
More file actions
50 lines (39 loc) · 1.24 KB
/
RacingController.java
File metadata and controls
50 lines (39 loc) · 1.24 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
package controller;
import common.ErrorMessages;
import domain.car.Cars;
import service.RacingGame;
import view.InputView;
import view.OutputView;
public class RacingController {
private final InputView inputView = new InputView();
private final OutputView outputView = new OutputView();
public void run() {
outputView.askCarNames();
String namesRaw = inputView.readCarNames();
Cars cars = Cars.of(namesRaw);
outputView.askRoundCount();
String roundsRaw = inputView.readRoundCount();
int rounds = parsePositiveInt(roundsRaw);
RacingGame game = new RacingGame(cars);
outputView.printResult();
for (int i = 0; i < rounds; i++) {
game.playOneRound();
outputView.printRound(cars);
}
outputView.printWinners(cars.findWinners());
}
private int parsePositiveInt(String raw) {
if (raw == null || raw.trim().isEmpty()) {
throw new IllegalArgumentException(ErrorMessages.NUMBER_OF_MOVES);
}
try {
int n = Integer.parseInt(raw.trim());
if (n <= 0) {
throw new IllegalArgumentException(ErrorMessages.NUMBER_OF_MOVES);
}
return n;
} catch (NumberFormatException e) {
throw new IllegalArgumentException(ErrorMessages.NUMBER_OF_MOVES);
}
}
}