Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
58 lines (46 loc) · 1.83 KB
/
Copy pathMain.java
File metadata and controls
58 lines (46 loc) · 1.83 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
import java.util.Scanner;
public class Main {
private static final int CAR_COUNT = 3;
public static void main(String[] args) {
Race race = new Race();
Scanner scanner = new Scanner(System.in);
for (int i = 1; i <= CAR_COUNT; i++) {
String name = readName(scanner, i, race);
int speed = readSpeed(scanner, i);
race.addCar(new Car(name, speed));
}
scanner.close();
System.out.println("Самая быстрая машина: " + race.getLeaderName());
}
private static String readName(Scanner scanner, int carNumber, Race race) {
while(true) {
System.out.println("Введите название машины №" + carNumber);
String name = scanner.nextLine().trim();
try {
Car.validateName(name);
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
continue;
}
if (race.containsCar(name)) {
System.out.println("Машина с таким именем уже есть. Введите другое имя");
continue;
}
return name;
}
}
private static int readSpeed(Scanner scanner, int carNumber) {
while(true) {
System.out.println("Введите скорость машины №" + carNumber);
try {
int speed = Integer.parseInt(scanner.nextLine().trim());
Car.validateSpeed(speed);
return speed;
} catch (NumberFormatException e) {
System.out.println("Ожидается целое число");
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
}
}
}
}