Skip to content
Open

1 #1

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
16 changes: 16 additions & 0 deletions src/main/java/Automobile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
public class Automobile {
private String name;
private int speed;
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, а также убрать геттеры и модификатор private - тогда можно получать доступ к переменным напрямую, а не через функции геттеры

public Automobile(String name, int speed) {
this.name = name;
this.speed = speed;
}

public String getName() {
return name;
}

public int getSpeed() {
return speed;
}
}
34 changes: 32 additions & 2 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,36 @@
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 i = 1; i <= 3; i++) {
System.out.println("Введите название автомобиля #" + i + ":");
String name = scanner.nextLine();

int speed;
while (true) {
System.out.println("Введите скорость автомобиля #" + i + " (от 1 до 250):");
try {
speed = Integer.parseInt(scanner.nextLine());
if (speed > 0 && speed <= 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.

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

break;
} else {
System.out.println("Неверная скорость! Скорость должна быть больше 0 и меньше или равна 250.");
}
}
catch (NumberFormatException e) {
System.out.println("Пожалуйста, введите числовое значение для скорости.");
}
}

Automobile car = new Automobile(name, speed);
race.updateLeader(car);
}

System.out.println("Самая быстрая машина: " + race.getLeaderName());
}
}
}


18 changes: 18 additions & 0 deletions src/main/java/Race.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
public class Race {
private String leaderName = "";
private int leaderDistance = 0;

public void updateLeader(Automobile car) {
int distance = 24 * car.getSpeed();

if (distance > leaderDistance) {
leaderDistance = distance;
leaderName = car.getName();
}
}

public String getLeaderName() {
return leaderName;
}
}