Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/main/java/CAR.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Car {
String nameCar;
int speedCar;
Comment on lines +2 to +3
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Поля лучше пометить final, тем самым исключив возможность их модификации извне

public Car(String nameCar, int speedCar) {
this.nameCar = nameCar;
this.speedCar = speedCar;
}
}
42 changes: 41 additions & 1 deletion src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,46 @@

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
Scanner scanner = new Scanner(System.in);
Race race=new Race();
for (int number=1;number<=3;number++) {
String nameCar;
int speedCar = 0;
boolean isValid = false;
while (true) {
System.out.println("Введите название машины №" + number);
nameCar = scanner.nextLine();
if (nameCar.isEmpty()) {
System.out.println("Строка пустая, введите заново.");
} else
break;
}
while (!isValid) {
System.out.println("Введите скорость машины №" + number);
String speedCars = scanner.nextLine();

if (speedCars.trim().isEmpty()) {
System.out.println("Ошибка: ввод не может быть пустым!");
continue;
}

try {
speedCar = Integer.parseInt(speedCars.trim());
if (speedCar < 0 || speedCar > 250) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Минимальную и максимальную скорости лучше вынести в константы для повышения читабельности кода

System.out.println("Введена некорректная скорость, введите от 0 до 250!");
} else {
isValid = true;
}
} catch (NumberFormatException e) {
System.out.println("Ошибка: введено не целое число или буквы!");
}
}
Car car = new Car(nameCar, speedCar);

race.race(car.nameCar,car.speedCar);
}
System.out.println("Самая бастрая машина: "+ race.liderCar);
scanner.close();
}
}
13 changes: 13 additions & 0 deletions src/main/java/RACE.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Race {
String liderCar=" ";
int liderDistance=0;
Comment on lines +2 to +3
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Поля лучше сделать приватными, чтобы поведение данного класса нельзя было изменить извне, а для получения названия машины-победителя написать отдельную функцию-геттер

void race(String nameCar,int speedCar){
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Нейминг лучше подобрать другой - в названии функции лучше всегда указывать глагол, для данной функции хорошим названием будет evaluateLeader

int distance=speedCar*24;
if(distance>liderDistance)
{
liderDistance=distance;
liderCar=nameCar;
}
}

}