Repository navigation
Expand file tree
/
Copy pathRace.java
More file actions
52 lines (40 loc) · 1.73 KB
/
Copy pathRace.java
File metadata and controls
52 lines (40 loc) · 1.73 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
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class Race {
private final List<Car> cars;
private Map<Car, Integer> carsAndDistance;
public Race(List<Car> cars) {
this.cars = cars;
}
private void getCarsDistance() {
carsAndDistance = cars.stream()
.collect(Collectors.toMap(
car -> car, car -> car.getSpeed() * 24));
}
public void calculateRaceWinner() {
getCarsDistance();
carsAndDistance = carsAndDistance.entrySet()
.stream()
.sorted(Collections.reverseOrder(Map.Entry.comparingByValue()))
.collect(Collectors.toMap(
Map.Entry::getKey,
Map.Entry::getValue,
(oldValue, newValue) -> oldValue, LinkedHashMap::new));
boolean isSpeedEquals = carsAndDistance.values().stream().distinct().count() == 1;
if (isSpeedEquals) {
System.out.printf("Все участники имеют одинаковые показатели, победителя нет!%n");
} else {
Map.Entry<Car, Integer> entry = carsAndDistance.entrySet().iterator().next();
Car winner = entry.getKey();
int winnersDistance = entry.getValue();
System.out.printf("Самый быстрый автомобиль - %s%n" +
"Его скорость составляет %dкм/ч и за 24 часа он проедет %dкм%n",
winner.getName(),
winner.getSpeed(),
winnersDistance);
}
}
}