Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
104 lines (92 loc) · 3.25 KB
/
Copy pathMain.java
File metadata and controls
104 lines (92 loc) · 3.25 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Drive drive = new Drive();
Auto[] autos = new Auto[3];
for (int i = 0; i < 3; i++) {
String name = "";
// ПРОВЕРКА НА ПУСТОЕ НАЗВАНИЕ
while (name.isEmpty()) {
System.out.println("Введите название автомобиля № " + (i + 1));
name = scanner.nextLine().trim();
if (name.isEmpty()) {
System.out.println("Вы ввели пустое значение, попробуйте снова");
}
}
// ВВОД СКОРОСТИ
int speed = 0;
while (true) {
System.out.print("Введите скорость автомобиля от 0 до 250 км/ч: ");
if (scanner.hasNextInt()) {
speed = scanner.nextInt();
scanner.nextLine();
if (speed >= 0 && speed <= 250) {
break;
} else {
System.out.println("Скорость автомобиля должна быть от 0 до 250 км/ч");
}
} else {
System.out.println("Введите целое числовое значение, пожалуйста");
scanner.nextLine();
}
}
autos[i] = new Auto(name, speed);
}
for (Auto auto : autos) {
drive.newLeader(auto);
}
// РАСЧЕТ ДИСТАНЦИЙ
int[] distances = new int[3];
for (int j = 0; j < 3; j++) {
distances[j] = autos[j].calculationWay();
}
boolean equal = true;
for (int j = 1; j < 3; j++) {
if (distances[j] != distances[0]) {
equal = false;
break;
}
}
if (equal) {
System.out.println("\nВсе водители проехали одинаковое расстояние. Нет победителя.");
} else {
System.out.println("\nПобедитель: " + drive.getLeader());
System.out.println("Пройденная дистанция: " + drive.getLeaderDistance() + " км");
}
}
}
class Auto {
String name;
int speed;
public Auto (String name,int speed) {
this.name = name;
this.speed = speed;
}
public String getName() {
return name;
}
public int calculationWay () {
return (speed * 24);
}
}
class Drive {
String leaderName = "";
int leaderWay = 0;
// МЕТОД ДЛЯ ЛИДЕРА
public void newLeader (Auto auto) {
// пройденный путь для лидера:
int newLeaderWay = auto.calculationWay();
// Сравнение
if (newLeaderWay > leaderWay) {
leaderName = auto.getName();
leaderWay = newLeaderWay;
}
}
public String getLeader() {
return leaderName;
}
public int getLeaderDistance() {
return leaderWay;
}
}