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
62 lines (49 loc) · 2.47 KB
/
Main.java
File metadata and controls
62 lines (49 loc) · 2.47 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
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
String greeting = "Здравствуйте! Вас приветствует консольное приложение '24 часа Ле-Мана'!\n" +
"Для определения лидера гонки введите данные %d автомобилей";
Scanner scanner = new Scanner(System.in);
int participantCount = 3;
Race race = new Race();
System.out.println("=".repeat(greeting.length()));
System.out.printf((greeting) + "%n", participantCount);
for (int i = 1; i <= participantCount; ++i) {
System.out.println("Введите название машины №" + i);
String name = scanCarName(scanner);
System.out.println("Введите скорость машины №" + i);
int speed = scanCarSpeed(scanner);
race.processParticipant(new Car(name, speed));
}
System.out.println("Самая быстрая машина: " + race.leaderName);
System.out.println("=".repeat(greeting.length()));
}
private static String scanCarName(Scanner scanner) {
while (true) {
String name = scanner.nextLine();
if (name.isBlank()) {
System.out.println("Название машины не может быть пустым, попробуйте еще раз");
} else {
return name;
}
}
}
private static int scanCarSpeed(Scanner scanner) {
while (true) {
int speed;
if (scanner.hasNextInt()) {
speed = scanner.nextInt();
scanner.nextLine();//нужно, чтобы съедать символ переноса строки
} else {
System.out.println("Скорость должна быть указана в целочисленном типе, попробуйте еще раз");
scanner.nextLine();//нужно, чтобы съедать символ переноса строки
continue;
}
if (!(speed >=0 && speed <= 250)) {
System.out.println("Скорость машины должна быть в допустимом диапазоне от 0 до 250, попробуйте еще раз");
} else {
return speed;
}
}
}
}