Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
94 lines (78 loc) · 2.38 KB
/
Copy pathMain.java
File metadata and controls
94 lines (78 loc) · 2.38 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
94
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
class Car {
private String name;
private double speed;
public Car(String name, double speed) {
this.name = name;
this.speed = speed;
}
public String getName() {
return name;
}
public double getSpeed() {
return speed;
}
public double distance() {
return speed * 24;
}
}
class Race {
private List<Car> cars;
public Race() {
cars = new ArrayList<>();
}
public void addCar(Car car) {
cars.add(car);
}
public Car Leader() {
if (cars.isEmpty()) {
return null;
}
Car leader = cars.get(0);
for (Car car : cars) {
if (car.distance() > leader.distance()) {
leader = car;
}
}
return leader;
}
}
public class Main {
private static Car getCarData() {
Scanner scanner = new Scanner(System.in);
String name;
double speed;
while (true) {
System.out.print("Введите название автомобиля: ");
name = scanner.nextLine();
System.out.print("Введите скорость автомобиля: ");
String speedInput = scanner.nextLine();
try {
if (speedInput.contains(".")) {
throw new NumberFormatException("Число с плавающей точкой не допустимо.");
}
speed = Double.parseDouble(speedInput);
if (speed > 0 && speed <= 250) {
return new Car(name, speed);
} else {
System.out.println("Неправильная скорость автомобиля");
}
} catch (NumberFormatException e) {
System.out.println("Пожалуйста, введите корректное число для скорости.");
}
}
}
public static void main(String[] args) {
Race race = new Race();
for (int i = 0; i < 3; i++) {
Car car = getCarData();
race.addCar(car);
}
Car leader = race.Leader();
if (leader != null) {
System.out.println("Самая быстрая машина: " + leader.getName());
}
}
}