-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathRacingCarController.java
More file actions
64 lines (54 loc) · 1.85 KB
/
RacingCarController.java
File metadata and controls
64 lines (54 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
package controller;
import model.AttemptNumber;
import model.RacingCar;
import model.RacingCars;
import java.io.IOException;
import java.util.List;
import view.*;
public class RacingCarController {
private final NumberGenerator numberGenerator;
public RacingCarController() {
this.numberGenerator = new RandomNumberGenerator();
}
public void run() throws IOException {
RacingCars cars = getCars();
AttemptNumber attemptNumber = getAttemptNumber();
race(cars, attemptNumber);
printWinners(cars);
}
private RacingCars getCars() throws IOException {
List<String> carNames = Input.carNameInput();
try {
return RacingCars.from(carNames);
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
return getCars();
}
}
private AttemptNumber getAttemptNumber() throws IOException {
try {
int number = Input.playTimeInput();
return new AttemptNumber(number);
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
return getAttemptNumber();
}
}
private void race(RacingCars cars, final AttemptNumber attemptNumber) throws IOException {
Output.printResult();
while (attemptNumber.isRemain()) {
attemptNumber.decrease();
cars.moveAll(numberGenerator);
printStatus(cars);
}
}
private void printStatus(final RacingCars cars) {
List<RacingCar> carDtos = RacingCar.getInstances(cars);
Output.printStatus(carDtos);
}
private void printWinners(final RacingCars cars) {
RacingCars winnerCars = cars.findWinners();
List<RacingCar> winnerCarDtos = RacingCar.getInstances(winnerCars);
Output.printWinners(winnerCarDtos);
}
}