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
41 lines (33 loc) · 945 Bytes
/
Race.java
File metadata and controls
41 lines (33 loc) · 945 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
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
public class Race {
private final List<Car> cars;
public Race() {
this.cars = new ArrayList<>();
}
public List<Car> getCars() {
return cars;
}
public String getRaceResults() {
return cars.stream()
.max(Comparator.comparingInt(c -> c.getSpeed() * 24))
.map(Car::getName)
.orElse(null);
}
//Альтернативное решение (если стримы считаются читерством)
/*
public String getRaceResults() {
Car winner = null;
int maxDistance = -1;
for (Car car : cars) {
int distance = car.getSpeed() * 24;
if (distance > maxDistance) {
maxDistance = distance;
winner = car;
}
}
return winner.getName();
}
*/
}