forked from Yandex-Practicum/Java-Module-Project-YP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRace.java
More file actions
50 lines (46 loc) · 2.1 KB
/
Race.java
File metadata and controls
50 lines (46 loc) · 2.1 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.Scanner;
import java.util.ArrayList;
public class Race {
Scanner scanner = new Scanner(System.in);
int numberОfРarticipants = 3;
ArrayList<Car> raceРarticipants = new ArrayList<>(numberОfРarticipants);
void getData() {
System.out.println("Ведите, пожауйста, информацию об участниках в формате:\n" +
"название машины, средняя скорость машины целым числом в километрах в час\n" +
"Пример:\nБугатти Вейрон, 220");
for (int i = 1; i <= numberОfРarticipants; i++) {
System.out.println(i + "-й участник:");
String[] data = scanner.nextLine().split(",");
if (data.length != 2) {
System.out.println("Введите название и скорость через запятую. Пример:\nБугатти Вейрон, 220");
i--;
}
else {
int velocity;
try {
velocity = Integer.parseInt(data[1].trim());
String name = data[0].trim();
if (velocity > 250 || velocity <= 0) {
System.out.println("Введено неправдоподобное значение скорости. Попробуйте снова.");
i--;
}
else {
raceРarticipants.add(new Car(data[0].trim(), velocity));
}
} catch (java.lang.NumberFormatException e) {
System.out.println("В качестве значения скорости введите целое число.");
i--;
}
}
}
}
Car getLeader() {
Car leader = raceРarticipants.get(0);
for (Car car : raceРarticipants) {
if (car.getDistance() > leader.getDistance()) {
leader = car;
}
}
return leader;
}
}