forked from Yandex-Practicum/Java-Module-Project-YP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
79 lines (68 loc) · 2.69 KB
/
Main.java
File metadata and controls
79 lines (68 loc) · 2.69 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
68
69
70
71
72
73
74
75
76
77
78
79
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
ArrayList<Car> cars = new ArrayList<>();
Race race = new Race();
for (int i = 0; i < 3; i++) {
System.out.println("Введите название " + (i + 1) + "-го автомобиля: ");
String name = input.nextLine();
boolean nameExists = false;
for (Car car : cars) {
if (car != null && car.name.equalsIgnoreCase(name)) {
nameExists = true;
break;
}
}
if (!name.isEmpty() && !nameExists) {
int speed;
while (true) {
System.out.println("Введите скорость " + (i + 1) + "-го автомобиля: ");
if (input.hasNextInt()) {
speed = input.nextInt();
input.nextLine();
if (speed >= 0 && speed <= 250) {
break;
} else {
System.out.println("Скорость должна быть от 0 до 250. Попробуйте еще раз.");
}
} else {
System.out.println("Ошибка: введите целое число для скорости.");
input.nextLine();
}
}
Car newCar = new Car(name, speed);
cars.add(newCar);
race.determineNewLeader(newCar);
} else {
System.out.println("Название автомобиля не может быть пустым или дублироваться. Попробуйте еще раз.");
i--;
}
}
System.out.println("\nСписок добавленных автомобилей:");
for (Car car : cars) {
System.out.println("Название: " + car.name + ", Скорость: " + car.speed);
}
System.out.println("\nПобедитель гонки: " + race.winner);
}
}
class Car {
String name;
int speed;
public Car(String name, int speed) {
this.name = name;
this.speed = speed;
}
}
class Race {
String winner = "";
int maxDistance = 0;
public void determineNewLeader(Car newCar) {
int newCarDistance = 24 * newCar.speed;
if (newCarDistance > this.maxDistance) {
this.maxDistance = newCarDistance;
this.winner = newCar.name;
}
}
}