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
51 lines (43 loc) · 1.82 KB
/
Main.java
File metadata and controls
51 lines (43 loc) · 1.82 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
import java.util.Scanner;
import java.util.ArrayList;
public class Main {
final static int MIN_SPEED = 1;
final static int MAX_SPEED = 250;
final static int NUM_CARS = 3;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
ArrayList<Car> cars = new ArrayList<>();
for (int i = 1; i <= NUM_CARS; i++) {
boolean trueName = false;
System.out.println("Введите автомобиль №" + i);
String name = "";
while (!trueName) {
System.out.print("Наименование: ");
name = scanner.nextLine().trim();
if (name.isEmpty())
System.out.println("Ошибка. Пустое название недопустимо");
else
trueName = true;
}
int speed = 0;
boolean trueSpeed = false;
while (!trueSpeed) {
System.out.print("Скорость в км/ч, от " + MIN_SPEED + " до " + MAX_SPEED + ": ");
if (scanner.hasNextInt()) {
speed = scanner.nextInt();
if ((speed >= MIN_SPEED) && (speed <= MAX_SPEED))
trueSpeed = true;
else
System.out.println("Ошибка. Введите допустимую скорость");
} else
System.out.println("Ошибка. Введите целое число");
scanner.nextLine();
}
cars.add(new Car(name, speed));
}
Race race = new Race();
race.winner(cars);
System.out.println("\nСамая быстрая машина: " + race.getWinner().getName());
scanner.close();
}
}