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
64 lines (51 loc) · 2.21 KB
/
Main.java
File metadata and controls
64 lines (51 loc) · 2.21 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
import java.util.Scanner;
public class Main {
private static final Scanner scanner = new Scanner(System.in);
private static final int CARS_COUNT = 3;
private static final int MAX_SPEED = 250;
private static final int MIN_SPEED = 0;
public static void main(String[] args) {
Race race = new Race();
for (int i = 1; i <= CARS_COUNT; i++) {
String carName = getCarName(i);
int carSpeed = getCarSpeed(i);
Car newCar = new Car(carName, carSpeed);
race.checkLeader(newCar);
}
System.out.println("Самая быстрая машина: " + race.leader);
}
private static String getCarName(int i) {
String carName;
while (true) {
System.out.println("— Введите название машины №" + i + ": ");
carName = scanner.nextLine().trim();
if (carName.isEmpty()) {
System.out.println("— Неправильное название: название машины не может быть пустым");
} else {
break;
}
}
return carName;
}
private static int getCarSpeed(int i) {
String carSpeedStr;
int carSpeed;
while (true) {
System.out.println("— Введите скорость машины №" + i + ": ");
carSpeedStr = scanner.nextLine().trim();
if (!carSpeedStr.matches("-?\\d+")) {
System.out.println("— Неправильная скорость: скорость должна быть целым числом");
} else {
carSpeed = Integer.parseInt(carSpeedStr);
if (carSpeed < MIN_SPEED) {
System.out.println("— Неправильная скорость: скорость должна быть не меньше 0");
} else if (carSpeed > MAX_SPEED) {
System.out.println("— Неправильная скорость: скорость должна быть не больше 250");
} else {
break;
}
}
}
return carSpeed;
}
}