Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
80 lines (64 loc) · 2.39 KB
/
Copy pathMain.java
File metadata and controls
80 lines (64 loc) · 2.39 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// Читаем авто
Auto[] arrayAuto = requestCar();
// Определяем самого быстрого
Auto fastest = Race.fastest(arrayAuto);
// Выводим
System.out.println(String.format("Самая быстрая машина: %s(%d)", fastest.name, fastest.speed));
}
static Auto[] requestCar() {
Scanner scanner = new Scanner(System.in);
Auto[] arrayAuto = new Auto[3];
for (int i = 0; i <= 2; i++) {
String name = "";
int speed = 0;
String question = "Введите имя авто:";
while (!Auto.nameVerf(name)) {
System.out.println(question);
name = scanner.next();
question = "Некорректное имя авто, повторите:";
}
question = "Введите скорость авто (от 1 до 250, целое):";
while (!Auto.speedVerf(speed)) {
System.out.println(question);
if (scanner.hasNextInt()) {
speed = scanner.nextInt();
question = "Число не в диапазоне(от 1 до 250), повторите:";
} else {
question = scanner.next() + " - это не целое число, повторите:";
}
}
arrayAuto[i] = new Auto(name, speed);
}
return arrayAuto;
}
}
class Race {
static Auto fastest(Auto[] arrayAuto) {
Auto fastest = arrayAuto[0];
int maxSpeed = 0;
for (Auto auto : arrayAuto) {
if (auto.speed > maxSpeed) {
maxSpeed = auto.speed; // вместо скорости можно посчитать расстояние, но зачем?
fastest = auto;
}
}
return fastest;
}
}
class Auto {
String name;
int speed;
public Auto(String name, int speed) {
this.name = name;
this.speed = speed;
}
static boolean nameVerf(String name) {
return !name.isEmpty(); // сюда можно будет добавить проверки для имени
}
static boolean speedVerf(int speed) {
return 0 < speed & speed <= 250;
}
}