forked from next-step/java-racingcar-simple-playground
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCars.java
More file actions
49 lines (40 loc) · 1.15 KB
/
Copy pathCars.java
File metadata and controls
49 lines (40 loc) · 1.15 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
package domain;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class Cars {
private List<Car> cars;
private Cars(List<Car> cars) {
this.cars = cars;
}
public static Cars create(String[] carNames, NumberGenerator numberGenerator) {
List<Car> newCars = new ArrayList<Car>();
for (String carName : carNames) {
newCars.add(new Car(carName, numberGenerator));
}
return new Cars(newCars);
}
public void move() {
for (Car car : cars) {
car.move();
}
}
public int getMaxDistance() {
int maxDistance = 0;
for (Car car : cars) {
maxDistance = Math.max(maxDistance, car.getDistance());
}
return maxDistance;
}
public Cars findCarsInPosition(int position) {
List<Car> carsInPosition = new ArrayList<>();
return new Cars(
cars.stream()
.filter(car -> car.isInPosition(position))
.collect(Collectors.toList())
);
}
public List<Car> getCars() {
return cars;
}
}