Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
49 lines (41 loc) · 1.85 KB
/
Copy pathMain.java
File metadata and controls
49 lines (41 loc) · 1.85 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
import java.util.InputMismatchException;
import java.util.Scanner;
public class Main {
private static final Scanner scanner = new Scanner(System.in);
private static final int PARTICIPANTS_AMOUNT = 3;
public static void main(String[] args) {
Race currentRace = new Race();
Car participantCar;
for (int carNumber = 1; carNumber <= PARTICIPANTS_AMOUNT; carNumber++) {
participantCar = inputData(carNumber);
currentRace.findWinner(participantCar);
}
currentRace.showWinner();
scanner.close();
}
private static Car inputData(int carNumber) {
String speedValue, participantName = null;
int participantSpeed;
Car participantCar = new Car();
boolean isValid = false;
while (!isValid) {
try {
if (participantName == null || participantName.isBlank()) {
System.out.printf("— Введите название машины №%d:\n", carNumber);
participantName = scanner.nextLine();
participantCar.setName(participantName);
}
System.out.printf("— Введите скорость машины №%d:\n", carNumber);
speedValue = scanner.nextLine();
participantSpeed = Integer.parseInt(speedValue);
participantCar.setSpeed(participantSpeed);
isValid = true;
} catch (InputMismatchException | NumberFormatException e) {
System.out.println("Ошибка: введено нецелое число. Попробуйте снова!\n");
} catch (IllegalArgumentException e) {
System.out.printf("Ошибка : %s. Попробуйте снова!\n", e.getMessage());
}
}
return participantCar;
}
}