-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathCars.java
More file actions
54 lines (42 loc) · 1.1 KB
/
Cars.java
File metadata and controls
54 lines (42 loc) · 1.1 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
package racing.domain;
import racing.view.ResultView;
import java.util.ArrayList;
import java.util.List;
public class Cars {
private final List<Car> cars;
public Cars(int carCount) {
this.cars = collectCars(carCount);
}
public void operate(int tryCount) {
ResultView resultView = new ResultView();
while (remainTryCount(tryCount--)) {
moveCars();
resultView.printResult(this);
}
}
public Car getCar(int idx) {
return cars.get(idx);
}
public int getSize() {
return cars.size();
}
private List<Car> collectCars(int carCount) {
List<Car> cars = new ArrayList<>();
while (remainCar(carCount--)) {
cars.add(new Car(0));
}
return cars;
}
private boolean remainCar(int carCount) {
return carCount > 0;
}
private boolean remainTryCount(int tryCount) {
return tryCount > 0;
}
private void moveCars() {
for (int idx = 0; idx < getSize(); idx++) {
Car car = getCar(idx);
car.move();
}
}
}