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
59 lines (48 loc) · 1.86 KB
/
Main.java
File metadata and controls
59 lines (48 loc) · 1.86 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
import java.util.Scanner;
public class Main {
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
Race race = new Race();
for (int i = 1; i <= 3; i++) {
Car car = createCarFromUserInput(i);
race.updateLeader(car);
}
displayWinner(race);
scanner.close();
}
private static Car createCarFromUserInput(int carNumber) {
String name = getCarName(carNumber);
int speed = getCarSpeed(carNumber);
return new Car(name, speed);
}
private static String getCarName(int carNumber) {
while (true) {
System.out.print("Название автомобиля №" + carNumber + ": ");
String name = scanner.nextLine().trim();
if (!name.isEmpty()) {
return name;
}
System.out.println("Название не может быть пустым.");
}
}
private static int getCarSpeed(int carNumber) {
while (true) {
System.out.print("Скорость автомобиля №" + carNumber + ": ");
if (scanner.hasNextInt()) {
int speed = scanner.nextInt();
if (speed > 0 && speed <= 250) {
scanner.nextLine();
return speed;
}
System.out.println("Скорость должна быть от 1 до 250 км/ч.");
} else {
System.out.println("Скорость может быть только целым числом.");
scanner.next();
}
}
}
private static void displayWinner(Race race) {
System.out.println("Лидер: " + race.LeaderName);
System.out.println("Дистанция: " + race.LeaderDistance + " км");
}
}