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
67 lines (56 loc) · 2.18 KB
/
Race.java
File metadata and controls
67 lines (56 loc) · 2.18 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
64
65
66
67
import java.util.ArrayList;
import java.util.Scanner;
public class Race {
ArrayList<Car> raceParticipants = new ArrayList<>();
public ArrayList<Car> createRace() {
Scanner scanner = new Scanner(System.in);
String name;
int speed;
for (int i = 1; i < 4; i++) {
while (true) {
Print("Введите название машины №" + i + ":");
name = scanner.nextLine().trim();
if (!name.isEmpty()) {
break;
} else {
Print("Ошибка!!! Имя машины НЕ может быть пустым. Попробуйте снова:");
}
}
while (true) {
Print("Введите скорость машины №" + i + ":");
if (scanner.hasNextInt()) {
speed = scanner.nextInt();
scanner.nextLine();
if (speed > 0 && speed <= 250) {
break;
} else {
Print("Ошибка!!! Скорость должна быть от 1 до 250. Попробуйте снова.");
}
} else {
Print("Ошибка!!! Введите целое число!");
scanner.next();
}
}
raceParticipants.add(new Car(name, speed));
}
return raceParticipants;
}
public Car findRaceWinner(ArrayList<Car> participants) {
int maxSpeed = 0;
Car winner = null;
for (Car car : participants) {
if (car.name.equalsIgnoreCase("Lada") || car.name.equalsIgnoreCase("Лада") || car.name.equalsIgnoreCase("Жигули")) {
System.out.println("🚗 " + car.name + " получает бонус! +100 к скорости 💨");
car.speed += 100;
}
if (car.speed > maxSpeed) {
winner = car;
maxSpeed = car.speed;
}
}
return winner;
}
static void Print(String text) {
System.out.println(text);
}
}