Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
102 lines (84 loc) · 3.23 KB
/
Copy pathMain.java
File metadata and controls
102 lines (84 loc) · 3.23 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
import java.util.Scanner;
import java.util.HashSet;
import java.util.Set;
import java.util.List;
class Main
{
private static final int CONFIG_CARS_COUNT = 3;
private static final int CONFIG_MIN_SPEED = 1;
private static final int CONFIG_MAX_SPEED = 250;
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
Race race = new Race();
Set<String> usedNames = new HashSet<>();
for (int i = 1; i <= CONFIG_CARS_COUNT; i++)
{
String carName = readCarName(scanner, i, usedNames);
int carSpeed = readCarSpeed(scanner, carName);
Car car = new Car(carName, carSpeed);
race.addCarLead(car);
}
List<Car> winners = race.getLeaders();
if (winners.size() == 1)
{
System.out.println("Самая быстрая машина: " + winners.get(0).getNameCar());
} else
{
System.out.println("Самые быстрые машины:");
for (Car winner : winners)
{
System.out.println(winner.getNameCar());
}
}
}
private static String readCarName(Scanner scanner, int carNumber, Set<String> usedNames)
{
while (true)
{
System.out.print("Введите название автомобиля №" + carNumber + ": ");
String inputName = scanner.nextLine().trim();
if (inputName.isEmpty())
{
System.out.println("Ошибка: название автомобиля не может быть пустым.");
continue;
}
String tryCarName = inputName.toLowerCase();
if (usedNames.contains(tryCarName))
{
System.out.println("Ошибка: такое название уже введено. Введите уникальное название.");
continue;
}
usedNames.add(tryCarName);
return inputName;
}
}
private static int readCarSpeed(Scanner scanner, String carName)
{
while (true)
{
System.out.print("Введите скорость автомобиля \"" + carName + "\" (от "
+ CONFIG_MIN_SPEED + " до " + CONFIG_MAX_SPEED + "): ");
String inputSpeed = scanner.nextLine().trim();
if (inputSpeed.isEmpty())
{
System.out.println("Ошибка: скорость не может быть пустой.");
continue;
}
try
{
int speed = Integer.parseInt(inputSpeed);
if (speed < CONFIG_MIN_SPEED || speed > CONFIG_MAX_SPEED)
{
System.out.println("Ошибка: скорость должна быть в диапазоне от "
+ CONFIG_MIN_SPEED + " до " + CONFIG_MAX_SPEED + ".");
continue;
}
return speed;
} catch (NumberFormatException e)
{
System.out.println("Ошибка: введите целое число.");
}
}
}
}