-
Notifications
You must be signed in to change notification settings - Fork 218
Expand file tree
/
Copy pathRace.java
More file actions
117 lines (104 loc) · 3.76 KB
/
Race.java
File metadata and controls
117 lines (104 loc) · 3.76 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
105
106
107
108
109
110
111
112
113
114
115
116
117
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Iterator;
import java.util.Scanner;
public class Race {
Car winner;
Scanner scanner = new Scanner(System.in);
ArrayList<Car> cars = new ArrayList<>();
public void calculateWinner(ArrayList<Car> cars) {
winner = cars.get(0);
for(Car car: cars) {
if (car.getSpeed() >= winner.getSpeed()) {
winner = car;
}
}
}
public void contestStart() {
System.out.println("Введите команду ");
while (true) {
printMenu();
int command = scanner.nextInt();
if (command == 1) {
carInput();
} else if (command == 2) {
calculateWinner(cars);
System.out.println("Побеждает тачка:" + winner.getName() + " со скоростью " + winner.getSpeed());
} else if (command == 0) {
System.out.println("Выход");
break;
} else {
System.out.println("Извините, такой команды пока нет");
}
}
}
public void printMenu() {
System.out.println("Что вы хотите сделать? ");
System.out.println("1 - Добавить тачку");
System.out.println("2 - Узнать победителя гонки");
System.out.println("0 - Выход");
}
public void carInput() {
while (true) {
Integer speed = inputSpeed();
String name = inputName();
Car car = new Car(speed, name);
cars.add(car);
System.out.println("Тачка успешно создана!");
break;
}
}
private String inputName() {
System.out.println("Введите название тачилы");
return scanner.nextLine();
}
private Integer inputSpeed() {
Integer speed;
while(true) {
speed = scanSpeed();
if (checkSpeed(speed)) {
break;
} else {
speed = inputSpeed();
break;
}
}
return speed;
}
private Boolean checkSpeed(Integer speed) {
Boolean isCorrect;
if (speed <= 0) {
System.out.println(
"Вы ввели отрицательную скорость.Введите скорость > 0 но =< 250"
);
isCorrect = false;
} else if (speed > 250) {
System.out.println(
"Вы ввели слишком большую скорость. Введите скорость > 0 но =< 250"
);
isCorrect = false;
} else {
isCorrect = true;
}
return isCorrect;
}
private Integer scanSpeed() {
Integer speed;
System.out.println("Введите скорость > 0 но =< 250");
while (true) {
try {
speed = scanner.nextInt();
scanner.nextLine(); // очищаем буфер
if (speed > 0 && speed <= 250) {
break; // выход из цикла при корректном вводе
} else {
System.out.println("Введите скорость в пределах > 0 но =< 250");
}
} catch (InputMismatchException e) {
System.out.println("Вы ввели неверные данные. Введите скорость > 0 но =< 250 как целое число");
scanner.nextLine(); // очищаем буфер
}
}
return speed;
}
}