Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
161 lines (137 loc) · 4.8 KB
/
Copy pathMain.java
File metadata and controls
161 lines (137 loc) · 4.8 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import java.util.Scanner;
class Car {
int id;
String carName;
int carSpeed;
Car(int id, String name, int speed) {
this.id = id;
this.carName = name;
this.carSpeed = speed;
}
public int getId() {
return id;
}
public String getCarName() {
return carName;
}
public int getCarSpeed() {
return carSpeed;
}
static boolean checkSpeed(int speed) {
if (speed < 0) {
System.out.println("Ошибка: скорость не может быть отрицательной");
return false;
}
if (speed > 250) {
System.out.println("Ошибка: скорость превышает максимум (250)");
return false;
}
return true;
}
}
class Race {
String winCarName;
int winCarId;
int distance;
Race() {
this.distance = 0;
}
public int getDistance() {
return distance;
}
public String getWinCar() {
return winCarName;
}
public int getWinCarId() {
return winCarId;
}
public void setWinCarId(int winCarId) {
this.winCarId = winCarId;
}
public void setWinCar(String car) {
this.winCarName = car;
}
public void setDistance(int distance) {
this.distance = distance;
}
public int calcDistance(int speed) {
return speed * 24;
}
public void checkAndSetLeader(Car car) {
int carDistance = calcDistance(car.getCarSpeed());
if (carDistance > this.getDistance()) {
this.setWinCar(car.getCarName());
this.setDistance(carDistance);
this.setWinCarId(car.getId());
}
}
public void printFlag(){
int rows = 8;
int cols = 32;
int cellSize = 2;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if ((i / cellSize + j / cellSize) % 2 == 0) {
System.out.print("██");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
}
public class Main {
static boolean isEmptyCheck(String input){
if (input.trim().isEmpty()) {
System.out.println("Ошибка: ничего не введено!");
return true;
}
return false;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Race race = new Race();
System.out.println();
System.out.println("Приготовьтесь к захватывающей гонке, которая оставит вас без дыхания и подарит незабываемые эмоции!");
race.printFlag();
System.out.println("Присоединяйтесь к нам на невероятном соревновании, где скорость встречается с мастерством, а адреналин зашкаливает!");
for (int i = 1; i <= 3; i++) {
boolean validNameInput = false;
String name = "";
while (!validNameInput){
System.out.println("— Введите название машины гонщика №" + i + ": ");
name = scanner.nextLine();
if (isEmptyCheck(name)){
continue;
}
name = name.trim();
validNameInput = true;
}
boolean validSpeedInput = false;
int speed;
while (!validSpeedInput) {
System.out.println("— Введите скорость машины " + name + " гонщика №" + i + ": ");
String input = scanner.nextLine().trim();
if (!isEmptyCheck(input)) {
try {
speed = Integer.parseInt(input);
if (Car.checkSpeed(speed)) {
Car car = new Car(i, name, speed);
race.checkAndSetLeader(car);
validSpeedInput = true;
}
}
catch (NumberFormatException e) {
System.out.println("Ошибка: введите целое число! Попробуйте ещё раз.");
}
}
}
}
System.out.println("Гонка закончена!!!");
System.out.println("Победитель - гонщик №" + race.getWinCarId());
System.out.println("На машине - " + race.getWinCar());
System.out.println("С результатом - " + race.getDistance() +" километров");
scanner.close();
}
}