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
71 lines (68 loc) · 2.27 KB
/
Main.java
File metadata and controls
71 lines (68 loc) · 2.27 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
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
List<Car> cars = new ArrayList<>();
System.out.println("Добро пожаловать в гонку '24 часа Ле-Мана'!");
int carCount = 3;
for (int i = 1; i <= carCount; i++) {
System.out.println("Введите название автомобиля №" + i + ":");
String name = scanner.nextLine();
int speed = 0;
while (true) {
System.out.println("Введите скорость автомобиля (1-250 км/ч):");
try {
speed = Integer.parseInt(scanner.nextLine());
if (speed > 0 && speed <= 250) {
break;
} else {
System.out.println("Ошибка: скорость должна быть в диапазоне от 1 до 250 км/ч.");
}
} catch (NumberFormatException e) {
System.out.println("Ошибка: введите число.");
}
}
cars.add(new Car(name, speed));
}
Race race = new Race(cars);
Car winner = race.getWinner();
System.out.println("Самая быстрая машина: " + winner.getName());
}
}
class Car {
private final String name;
private final int speed;
public Car(String name, int speed) {
this.name = name;
this.speed = speed;
}
public int getSpeed() {
return speed;
}
public String getName() {
return name;
}
public int getDistanceAfterHours(int hours) {
return speed * hours;
}
}
class Race {
private final List<Car> cars;
public Race(List<Car> cars) {
this.cars = cars;
}
public Car getWinner() {
Car winner = null;
int maxDistance = -1;
for (Car car : cars) {
int distance = car.getDistanceAfterHours(24);
if (distance > maxDistance) {
maxDistance = distance;
winner = car;
}
}
return winner;
}
}