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
104 lines (87 loc) · 3.11 KB
/
Main.java
File metadata and controls
104 lines (87 loc) · 3.11 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
import java.util.Scanner;
// Класс Автомобиль
class Car {
private String name;
private int speed;
public Car(String name, int speed) {
this.name = name;
this.speed = speed;
}
public int getDistance() {
return speed * 24;
}
public String getName() {
return name;
}
public int getSpeed() {
return speed;
}
}
// Класс Гонка
class Race {
private Car winner;
public void determineWinner(Car[] cars) {
winner = cars[0];
for (int i = 1; i < cars.length; i++) {
if (cars[i].getDistance() > winner.getDistance()) {
winner = cars[i];
}
}
}
public Car getWinner() {
return winner;
}
}
// Главный класс
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int numberOfCars = 3; // можно легко изменить
Car[] cars = new Car[numberOfCars];
for (int i = 0; i < numberOfCars; i++) {
cars[i] = createCar(i + 1, scanner);
}
Race race = new Race();
race.determineWinner(cars);
Car winner = race.getWinner();
System.out.println("\n🏁 Победитель гонки: " + winner.getName());
System.out.println("Скорость: " + winner.getSpeed() + " км/ч");
System.out.println("Проехал за 24 часа: " + winner.getDistance() + " км");
}
// Метод создания машины с вводом и валидацией
private static Car createCar(int number, Scanner scanner) {
String name;
int speed;
System.out.println("Введите название машины №" + number + ":");
while (true) {
name = scanner.nextLine().trim();
if (!name.isEmpty()) break;
System.out.println("Название не может быть пустым. Введите ещё раз:");
}
System.out.println("Введите скорость машины №" + number + ":");
while (true) {
String input = scanner.nextLine().trim();
if (isValidSpeed(input)) {
speed = Integer.parseInt(input);
if (speed >= 0 && speed <= 250) { // ✅ от 0 до 250
break;
} else {
System.out.println("Неправильная скорость. Введите число от 0 до 250:");
}
} else {
System.out.println("Неправильная скорость. Введите целое число:");
}
}
return new Car(name, speed);
}
// Метод для проверки, является ли строка числом
private static boolean isValidSpeed(String input) {
if (input.isEmpty()) return false;
for (int i = 0; i < input.length(); i++) {
if (!Character.isDigit(input.charAt(i))) {
return false;
}
}
return true;
}
}