-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathRacingCarController.java
More file actions
66 lines (55 loc) · 1.75 KB
/
RacingCarController.java
File metadata and controls
66 lines (55 loc) · 1.75 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
package controller;
import domain.Car;
import domain.Cars;
import domain.TryCount;
import utils.RandomNumberGenerator;
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() {
Cars cars = getCars();
TryCount tryCount = getTryCount();
race(cars, tryCount);
printWinner(cars);
}
private Cars getCars() {
List<String> carNames = InputView.readCarNames();
try {
return new Cars(carNames);
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
return getCars(); //올바른 입력 넣을 때까지 반복
}
}
private TryCount getTryCount() {
try {
int number = InputView.readTryCount();
return new TryCount(number);
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
return getTryCount(); //올바른 입력 넣을 때까지 반복
}
}
private void race(Cars cars, TryCount tryCount) {
OutputView.printResult();
while (tryCount.isRemain()) {
cars.moveCars(randomNumberGenerator);
printStatus(cars);
tryCount.decrease();
}
}
private void printStatus(Cars cars) {
List<Car> carList = cars.getCars();
OutputView.printStatus(carList);
}
private void printWinner(Cars cars) {
List<Car> winnerList = cars.findWinner();
OutputView.printWinners(winnerList);
}
}