forked from Yandex-Practicum/Java-Module-Project-YP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCarInputManager.java
More file actions
57 lines (45 loc) · 1.7 KB
/
CarInputManager.java
File metadata and controls
57 lines (45 loc) · 1.7 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
import java.util.ArrayList;
import java.util.Scanner;
class CarInputManager {
private Scanner scanner = new Scanner(System.in);
ArrayList<Car> inputCars() {
ArrayList<Car> cars = new ArrayList<>();
for (int i = 0; i < 3; i++) {
String carName;
Integer carSpeed;
do {
carName = inputName();
} while (carName == null);
do {
carSpeed = inputSpeed();
} while (carSpeed == null);
cars.add(new Car(carName, carSpeed));
}
return cars;
}
private String inputName() {
System.out.println("Введите название машины:");
String name = scanner.nextLine();
if (name.isEmpty()) {
System.out.println("Название не может быть пустым.\nПопробуйте ещё раз...\n");
return null;
} else {
return name;
}
}
private Integer inputSpeed() {
System.out.println("Введите скорость машины");
String speedText = scanner.nextLine();
if (!speedText.matches("-?\\d+")) {
System.out.println("Скорость должна быть целым положительным числом.\nПопробуйте ещё раз...\n");
return null;
}
Integer speed = Integer.valueOf(speedText);
if (speed > 250 || speed < 0) {
System.out.println("Скорость должна быть не больше 250 и не меньше 0.\nПопробуйте ещё раз...\n");
return null;
} else {
return speed;
}
}
}