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
75 lines (63 loc) · 2.35 KB
/
Main.java
File metadata and controls
75 lines (63 loc) · 2.35 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
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Race race = new Race();
for (int i = 1; i <=3; i++) {
String name;
while (true) {
System.out.println("Введите название автомобиля №" + i + ": ");
name = scanner.nextLine().trim();
if (name.isEmpty()) {
System.out.println("Вы забыли ввести название автомобиля, попробуйте еще!");
continue;
}
break;
}
int speed;
while (true) {
System.out.println("Введите скорость автомобиля №" + i + ": ");
String input = scanner.nextLine().trim();
if (input.isEmpty()) {
System.out.println("Вы забыли ввести скорость, попробуйте еще!");
continue;
}
if (!input.matches("\\d+")) {
System.out.println("Скоростью может быть только целое число, попробуйте еще!");
continue;
}
speed = Integer.parseInt(input);
if (speed > 0 && speed <= 250) {
break;
} else {
System.out.println("Скорость должна быть от 1 до 250. Попробуйте снова.");
}
}
Car car = new Car(name, speed);
race.updateLeader(car);
}
System.out.println("Самая быстрая машина: " + race.getWinnerName());
}
}
class Car {
String name;
int speed;
public Car(String carName, int carSpeed) {
this.name = carName;
this.speed = carSpeed;
}
}
class Race {
String leaderName = "";
int leaderDistance = 0;
public void updateLeader(Car newCar) {
int newDistance = 24 * newCar.speed;
if (newDistance > this.leaderDistance) {
leaderName = newCar.name;
leaderDistance = newDistance;
}
}
public String getWinnerName() {
return leaderName;
}
}