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
14 changes: 14 additions & 0 deletions src/main/java/Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Car {
private String name;

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 - тогда можно удалить геттеры и получать доступ к полям напрямую по их имени - будет меньше кода, но также надёжно как с геттерами

private int speed;
public Car(String name, int speed) {
this.name= name;
this.speed= speed;
}
public String getName() {
return name;
}
public int getSpeed() {
return speed;
}
}
39 changes: 38 additions & 1 deletion src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,43 @@
import java.util.InputMismatchException;
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
{System.out.println("Гонка! 24 часа Ле-Мана");}
Scanner scanner= new Scanner(System.in);
Race race = new Race();

for (int i = 0; i < 3; i++) {
String carName = readCarName(scanner);
int carSpeed = readCarSpeed(scanner);

Car car;
car = new Car(carName, carSpeed);
race.updateLeader(car);
}
System.out.println("Самая быстрая машина : " + race.getCurrentLeader().getName());
scanner.close();
} private static String readCarName(Scanner scanner) {

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("Введите название автомобиля:");
return scanner.nextLine();
}
private static int readCarSpeed(Scanner scanner) {
int carSpeed;
while (true) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Не рекомендую писать бесконечные циклы через while (true) - лучше всегда явно прописывать условие выхода из цикла, чтобы уменьшить вероятность ошибиться и повысить читабельность кода

System.out.println("Введите скорость автомобиля (от 0 до 250 км/ч):");
try {
carSpeed = scanner.nextInt();
if (carSpeed >= 0 && carSpeed <= 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.

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

scanner.nextLine();
break;
} else {
System.out.println("Скорость должна быть (от 0 до 250 км/ч.):");
}
} catch (InputMismatchException e) {
System.out.println("Введите целое числовое значение.");
scanner.nextLine(); // Очистить буфер сканера
}
}
return carSpeed;
}
}
15 changes: 15 additions & 0 deletions src/main/java/Race.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Race {
private Car currentLeader= null;
private int currentLeaderDistance= 0;
public void updateLeader(Car car) {
int newCarDistance = 24 * car.getSpeed();

if (newCarDistance > currentLeaderDistance) {
currentLeader= car;
currentLeaderDistance= newCarDistance;
}
}
public Car getCurrentLeader() {
return currentLeader;
}
}