-
Notifications
You must be signed in to change notification settings - Fork 218
Expand file tree
/
Copy pathRace.java
More file actions
62 lines (51 loc) · 1.94 KB
/
Race.java
File metadata and controls
62 lines (51 loc) · 1.94 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
55
56
57
58
59
60
61
62
package constructor;
import java.util.Scanner;
import models.Car;
public class Race {
private static final int MAX_SPEED = 250;
private static final int MIN_SPEED = 0;
private static final int HOURS_IN_DAY = 24;
private final CarConstructor constructor;
private final Scanner scanner;
public Race(CarConstructor constructor) {
this.constructor = constructor;
this.scanner = new Scanner(System.in);
}
public void prepare() {
String name = carName();
int speed = carSpeed();
constructor.addCar(name, speed);
}
private String carName() {
System.out.println(formatMessage("- Введите название машины №%d"));
return scanner.next();
}
private int carSpeed() {
while (true) {
System.out.println(formatMessage("- Введите скорость машины №%d"));
try {
int speed = Integer.parseInt(scanner.next());
if (speed >= MIN_SPEED && speed <= MAX_SPEED) {
return speed;
} else {
System.out.println("- Неправильная скорость.");
}
} catch (NumberFormatException e) {
System.out.println("- Пожалуйста, введите целое число.");
}
}
}
private String formatMessage(String message) {
return String.format(message, constructor.carsSize() + 1);
}
public void getFasterCar() {
Car fasterCar = constructor.getCars().stream()
.max((c1, c2) -> Integer.compare(c1.speed * HOURS_IN_DAY, c2.speed * HOURS_IN_DAY))
.orElse(null);
if (fasterCar != null) {
System.out.printf("- Самая быстрая машина: %s%n", fasterCar.name);
} else {
System.out.println("- Нет машин в списке.");
}
}
}