Repository navigation
Expand file tree
/
Copy pathRacing.java
More file actions
63 lines (46 loc) · 1.89 KB
/
Copy pathRacing.java
File metadata and controls
63 lines (46 loc) · 1.89 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
55
56
57
58
59
60
61
62
63
import java.util.ArrayList;
public class Racing {
private final ArrayList<Car> cars = new ArrayList<>();
private Car winner;
private double lengthPathWinner = 0;
private int countCars;
public Racing(int countCars) {
this.countCars = countCars;
}
public void addCar(Car car) {
cars.add(car);
}
public ArrayList<Car> getCars() {
return new ArrayList<>(cars);
}
public void startRace(double durationRacing) throws InterruptedException { //время в часах
if (cars.size() != countCars) throw new IllegalArgumentException("Количество машин не равно заданному");
System.out.println("Гонка началась!");
winner = cars.get(0);
double lengthPath = cars.get(0).getSpeed() * durationRacing;
for (int i = 1; i < cars.size(); i++) {
lengthPath = cars.get(i).getSpeed() * durationRacing;
if (lengthPath >= lengthPathWinner) {
winner = cars.get(i);
lengthPathWinner = lengthPath;
}
}
for (int i = 1; i < 24; i++) {
Thread.sleep(200);
System.out.print(". ");
}
System.out.println("\nГонка закончилась!");
}
public void startRace() throws InterruptedException {
startRace(24);
}
public void printWinner() {
if (winner == null) {
System.out.println("Гонка не началась или в конке не было победителя");
} else {
System.out.println("Победитель в гонке автомобиль - " + winner.getName()
+ "\n со скоростью - " + winner.getSpeed() + " км/ч"
+ "\n пройденное расстояние - " + lengthPathWinner + " км");
}
}
}