Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
54 lines (45 loc) · 1.81 KB
/
Copy pathMain.java
File metadata and controls
54 lines (45 loc) · 1.81 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
import java.util.Scanner;
public class Main {
private static final Race RACE = new Race();
private static final Scanner SCANNER = new Scanner(System.in);
private static final int MIN_SPEED = 0;
private static final int MAX_SPEED = 250;
private static final int CAR_COUNT = 3;
public static void main(String[] args) {
addCar();
System.out.printf("— Самая быстрая машина: %s", RACE.getWinner());
SCANNER.close();
}
private static void addCar() {
for (int i = 1; i < CAR_COUNT; i++) {
int carNumber = i + 1;
String carName = getInputCarName(carNumber );
int carSpeed = getInputCarSpeed(carNumber );
RACE.calculateRaceLeader(new Car(carName, carSpeed));
}
}
private static String getInputCarName(int carNumber) {
System.out.printf("— Введите название машины №%d:\n", carNumber);
return SCANNER.next();
}
private static int getInputCarSpeed(int carNumber) {
int carSpeed;
while (true) {
System.out.printf("— Введите скорость машины №%d:\n", carNumber);
if (SCANNER.hasNextInt()) {
carSpeed = SCANNER.nextInt();
if (isValidSpeed(carSpeed)) {
return carSpeed;
} else {
System.out.println("— Скрость должна быть в диапазоне от 0 до 250!");
}
} else {
System.out.println("— Скрость должна быть целым числом!");
SCANNER.next();
}
}
}
private static boolean isValidSpeed(int carSpeed) {
return carSpeed >= MIN_SPEED && carSpeed <= MAX_SPEED;
}
}