-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathRacingCarController.java
More file actions
67 lines (56 loc) · 1.91 KB
/
RacingCarController.java
File metadata and controls
67 lines (56 loc) · 1.91 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
package controller;
import domain.AttemptNumber;
import domain.Cars;
import domain.RandomNumberGenerator;
import dto.CarDto;
import view.InputView;
import view.OutputView;
import java.io.IOException;
import java.util.List;
public class RacingCarController {
private final RandomNumberGenerator randomNumberGenerator;
public RacingCarController() {
this.randomNumberGenerator = new RandomNumberGenerator();
}
public void run() throws IOException {
Cars cars = getCars();
AttemptNumber attemptNumber = getAttemptNumber();
race(cars, attemptNumber);
printWinners(cars);
}
private Cars getCars() throws IOException {
List<String> carNames = InputView.readCarNames();
try {
return Cars.from(carNames);
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
return getCars();
}
}
private AttemptNumber getAttemptNumber() throws IOException {
try {
int number = InputView.readAttemptNumber();
return new AttemptNumber(number);
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
return getAttemptNumber();
}
}
private void race(final Cars cars, final AttemptNumber attemptNumber) throws IOException {
OutputView.printResult();
while (attemptNumber.isRemain()) {
attemptNumber.decrease();
cars.moveAll(randomNumberGenerator);
printStatus(cars);
}
}
private void printStatus(final Cars cars) {
List<CarDto> carDtos = CarDto.getInstances(cars);
OutputView.printStatus(carDtos);
}
private void printWinners(final Cars cars) {
Cars winnerCars = cars.findWinners();
List<CarDto> winnerCarDtos = CarDto.getInstances(winnerCars);
OutputView.printWinners(winnerCarDtos);
}
}