Repository navigation
Expand file tree
/
Copy pathApp.java
More file actions
63 lines (53 loc) · 1.96 KB
/
Copy pathApp.java
File metadata and controls
63 lines (53 loc) · 1.96 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
import java.util.InputMismatchException;
import java.util.Scanner;
public class App {
private static final Scanner scanner = new Scanner(System.in);
private final int MIN_SPEED = 1;
private final int MAX_SPEED = 250;
private final int CARS_IN_RACE = 3;
public void run() {
Race race = new Race();
for (int i = 1; i <= CARS_IN_RACE; i++) {
Car car = createCarFromUserInput(i);
race.add(car);
}
System.out.println(race.getResult());
scanner.close();
}
private static Car createCarFromUserInput(int position) {
String name = getCarNameFromUserInput(position);
int speed = getCarSpeedFromUserInput(position);
return new Car(speed, name);
}
private static String getCarNameFromUserInput(int position) {
System.out.println("— Введите название машины №" + position + ":");
while (true) {
String name = scanner.next();
if (isNameValid(name)) {
return name;
}
System.out.println("— Неправильное имя");
}
}
private static boolean isNameValid(String name) {
return !name.isEmpty();
}
private static int getCarSpeedFromUserInput(int position) {
System.out.println("— Введите скорость машины №" + position + ":");
while (true) {
try {
int speed = scanner.nextInt();
if (isSpeedValid(speed)) {
return speed;
}
System.out.println("— Неправильная скорость");
} catch (InputMismatchException e) {
scanner.nextLine();
System.out.println("— Неправильная скорость");
}
}
}
private static boolean isSpeedValid(int speed) {
return speed >= MIN_SPEED && speed <= MAX_SPEED;
}
}