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
53 lines (46 loc) · 2.21 KB
/
Main.java
File metadata and controls
53 lines (46 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
import java.util.InputMismatchException;
import java.util.Scanner;
public class Main {
private static final int MIN_SPEED = 1;
private static final int MAX_SPEED = 250;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Race race = new Race();
System.out.println("Добро пожаловать в гонку!");
System.out.println("Введите данные для трех автомобилей");
for (int i = 0; i < 3; i++) {
String name = readCarName(scanner, i + 1);
int speed = readCarSpeed(scanner, i + 1);
Car newCar = new Car(name, speed);
race.checkLeader(newCar);
}
System.out.println("Самая быстрая машина: " + race.getLeaderName());
}
private static String readCarName(Scanner scanner, int carNumber) {
while (true) {
System.out.println("Введите название автомобиля № " + carNumber + ":");
String name = scanner.nextLine().trim();
if (!name.isEmpty()) {
return name;
}
System.out.println("Ошибка: Название не может быть пустым. Попробуйте еще раз.");
}
}
private static int readCarSpeed(Scanner scanner, int carNumber){
while (true){
System.out.println("Введите скорость автомобиля № " + carNumber + " (" + MIN_SPEED + "-" + MAX_SPEED + "км/ч):");
try{
int speed = scanner.nextInt();
scanner.nextLine();
if (speed >= MIN_SPEED && speed <= MAX_SPEED) {
return speed;
} else {
System.out.println("Ошибка: скорость должна быть от " + MIN_SPEED + " до " + MAX_SPEED + " км/ч. Попробуйте еще раз.");
}
} catch (InputMismatchException e) {
System.out.println("Ошибка: введите целое число. Попробуйте еще раз.");
scanner.next();
}
}
}
}