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
50 lines (42 loc) · 1.71 KB
/
Main.java
File metadata and controls
50 lines (42 loc) · 1.71 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
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// get first car info
String carName1 = getLabel(1);
int carSpeed1 = getSpeed(1);
// get second car info
String carName2 = getLabel(2);
int carSpeed2 = getSpeed(2);
// get third car info
String carName3 = getLabel(3);
int carSpeed3 = getSpeed(3);
// create list of cars
List<Car> cars = new ArrayList<>(Arrays.asList(new Car(carName1, carSpeed1), new Car(carName2, carSpeed2), new Car(carName3, carSpeed3)));
// get winner
System.out.println("\n— Самая быстрая машина: " + Race.getWinner(cars).name());
}
private static String getLabel(int number) {
Scanner stringScan = new Scanner(System.in);
System.out.printf("\n— Введите название машины №%d: ", number);
if (stringScan.hasNext()) return stringScan.nextLine();
else return getLabel(number);
}
private static int getSpeed(int number) {
Scanner intScan = new Scanner(System.in);
System.out.printf("— Введите скорость машины №%d: ", number);
if (intScan.hasNextInt()) {
int num = intScan.nextInt();
if (num > 0 && num <= 250) return num;
else {
System.out.println("— Неправильная скорость");
return getSpeed(number);
}
} else {
System.out.println("\n— Неправильная скорость\n");
return getSpeed(number);
}
}
}