-
Notifications
You must be signed in to change notification settings - Fork 959
Expand file tree
/
Copy pathMain.java
More file actions
93 lines (73 loc) · 2.36 KB
/
Main.java
File metadata and controls
93 lines (73 loc) · 2.36 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
87
88
89
90
91
92
93
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
final int RaceleMan = 24;
final int CarCount = 3;
Auto[] cars = new Auto[CarCount];
for (int i = 0; i < CarCount; i++) {
System.out.println("Введи название машины " + (i + 1) + ":");
String name = scanner.nextLine().trim();
int speed;
while (true) {
System.out.println("Введи скорость " + (i + 1) + " (целое число, >0 и ≤250):");
String input = scanner.nextLine().trim();
try {
speed = Integer.parseInt(input);
if (speed > 0 && speed <= 250) {
break;
} else {
System.out.println("Некорректное значение. Пробуй снова.");
}
} catch (NumberFormatException e) {
System.out.println("Введи целое число.");
}
}
cars[i] = new Auto(name, speed);
}
Race race = new Race(cars);
Auto leader = race.madeLider();
System.out.println("Самая быстрая машина: " + (leader != null ? leader.getName() : "не найден"));
scanner.close();
}
}
class Auto {
private final String name;
private final int speed;
public Auto(String name, int speed) {
this.name = name;
this.speed = speed;
}
public String getName() {
return name;
}
public int getSpeed() {
return speed;
}
public int distanceЗа24Часа() {
return speed * 24;
}
}
class Race {
private final Auto[] cars;
private final Auto leader;
public Race(Auto[] cars) {
this.cars = cars;
this.leader = determineLeader();
}
private Auto determineLeader() {
Auto maxCar = null;
int maxDistance = -1;
for (Auto car : cars) {
int dist = car.distanceЗа24Часа();
if (dist > maxDistance) {
maxDistance = dist;
maxCar = car;
}
}
return maxCar;
}
public Auto madeLider() {
return leader;
}
}