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
115 lines (104 loc) · 4.42 KB
/
Main.java
File metadata and controls
115 lines (104 loc) · 4.42 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
private static Scanner scanner;
public static void main(String[] args) {
scanner = new Scanner(System.in);
Race race = new Race();
while (true) {
int choice = getChoice();
switch (choice) {
case 1:
Car car = getCarFromInput();
race.addCar(car);
System.out.println("Автомобиль добавлен.");
break;
case 2:
List<Car> carList = getCarsFromInput();
race.addCars(carList);
System.out.println("Автомобили добавлены.");
break;
case 3:
Car leader = race.getLeader();
if (leader != null) {
System.out.println("Текущий лидер: " + leader);
} else {
System.out.println("Еще нет автомобилей в гонке.");
}
break;
case 0:
System.out.println("Выход из программы.");
scanner.close();
return;
default:
System.out.println("Неверный выбор. Попробуйте еще раз.");
}
}
}
private static Car getCarFromInput() {
String carName;
int carSpeed;
System.out.print("Введите название автомобиля: ");
carName = scanner.nextLine();
while (true) {
System.out.print("Введите скорость автомобиля (0-250): ");
if (scanner.hasNextInt()) {
carSpeed = scanner.nextInt();
scanner.nextLine(); // Consume the newline
if (carSpeed >= 0 && carSpeed <= 250) {
break; // Correct speed, exit loop
} else {
System.out.println("Неправильная скорость. Пожалуйста, введите значение от 0 до 250.");
}
} else {
System.out.println("Неверный ввод. Пожалуйста, введите целое число.");
scanner.next(); // Consume the invalid input
}
}
return new Car(carName, carSpeed);
}
private static List<Car> getCarsFromInput() {
List<Car> carList = new ArrayList<>();
System.out.print("Введите количество автомобилей для добавления: ");
int numberOfCars;
while (true) {
if(scanner.hasNextInt()) {
numberOfCars = scanner.nextInt();
scanner.nextLine();
if(numberOfCars > 0) {
break;
} else {
System.out.println("Неправильный ввод. Пожалуйста, введите число больше 0");
}
} else {
System.out.println("Неверный ввод. Пожалуйста, введите целое число.");
scanner.next(); // Consume the invalid input
}
}
for(int i = 0; i < numberOfCars; i++) {
carList.add(getCarFromInput());
}
return carList;
}
private static int getChoice() {
int choice;
while (true) {
System.out.println("\nВыберите действие:");
System.out.println("1. Добавить автомобиль");
System.out.println("2. Добавить список автомобилей");
System.out.println("3. Показать текущего лидера");
System.out.println("0. Выход");
System.out.print("Ваш выбор: ");
if(scanner.hasNextInt()) {
choice = scanner.nextInt();
scanner.nextLine(); // Consume the newline
break;
} else {
System.out.println("Неверный ввод. Пожалуйста, введите целое число.");
scanner.next(); // Consume the invalid input
}
}
return choice;
}
}