forked from Yandex-Practicum/Java-Module-Project-YP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRace.java
More file actions
43 lines (37 loc) · 892 Bytes
/
Race.java
File metadata and controls
43 lines (37 loc) · 892 Bytes
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
import java.util.ArrayList;
import java.util.List;
public class Race {
private List<Car> cars;
private Car leader;
public Race() {
this.cars = new ArrayList<>();
this.leader = null;
}
public void addCar(Car car) {
cars.add(car);
updateLeader();
}
public void addCars(List<Car> carList) {
this.cars.addAll(carList);
updateLeader();
}
private void updateLeader() {
if (cars.isEmpty()) {
leader = null;
return;
}
Car currentLeader = cars.get(0);
for (Car car : cars) {
if (car.getSpeed() > currentLeader.getSpeed()) {
currentLeader = car;
}
}
leader = currentLeader;
}
public Car getLeader() {
return leader;
}
public List<Car> getCars() {
return cars;
}
}