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
86 lines (67 loc) · 2.71 KB
/
Main.java
File metadata and controls
86 lines (67 loc) · 2.71 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
80
81
82
83
84
85
86
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
ArrayList<Automobile> automobiles = new ArrayList<>();
Scanner scanner = new Scanner(System.in);
for (int i = 1; i <= 3; i++) {
System.out.println("— Введите название машины №" + i);
String name = scanner.nextLine().trim();
while (name.isEmpty()) {
System.out.println("— Название не может быть пустым");
System.out.println("— Введите название машины №" + i);
name = scanner.nextLine().trim();
}
int speed = -1;
boolean validSpeed = false;
while (!validSpeed) {
System.out.println("— Введите скорость машины №" + i);
String speedInput = scanner.nextLine().trim();
if (speedInput.isEmpty()) {
System.out.println("— Неправильная скорость.");
continue;
}
try {
speed = Integer.parseInt(speedInput);
if (speed < 0 || speed > 250) {
System.out.println("— Неправильная скорость.");
} else {
validSpeed = true;
}
} catch (NumberFormatException e) {
System.out.println("— Неправильная скорость.");
}
}
automobiles.add(new Automobile(name, speed));
}
Automobile winner = Race.calculateWinner(automobiles);
System.out.println("Самая быстрая машина: " + winner.name);
scanner.close();
}
}
class Race {
public static Automobile calculateWinner(ArrayList<Automobile> automobiles) {
Automobile winner = automobiles.getFirst();
double maxDistance = calculateDistance(winner);
for (int i = 1; i < automobiles.size(); i++) {
Automobile currentAuto = automobiles.get(i);
double currentDistance = calculateDistance(currentAuto);
if (currentDistance > maxDistance) {
maxDistance = currentDistance;
winner = currentAuto;
}
}
return winner;
}
private static double calculateDistance(Automobile automobile) {
return automobile.speed * 24;
}
}
class Automobile {
String name;
Integer speed;
Automobile(String autoname, Integer autospeed) {
name = autoname;
speed = autospeed;
}
}