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
9 changes: 9 additions & 0 deletions src/main/java/Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
public class Car {
String name;
int speed;
Comment on lines +2 to +3

Choose a reason for hiding this comment

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

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


public Car(String name, int speed) {
this.name = name;
this.speed = speed;
}
}
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 i = 1; i <= 3; i++) {

String name = "";
while (name.trim().isEmpty()) {
System.out.println("Введите название машины №" + i + ":");
name = scanner.nextLine();
if (name.trim().isEmpty()) {
System.out.println("Название не может быть пустым!");
}
Comment on lines +12 to +16

Choose a reason for hiding this comment

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

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

}

int speed = 0;
boolean valid = false;

while (!valid) {
System.out.println("Введите скорость машины №" + i + ":");

if (scanner.hasNextInt()) {
speed = scanner.nextInt();
scanner.nextLine();

if (speed > 0 && speed <= 250) {

Choose a reason for hiding this comment

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

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

valid = true;
} else {
System.out.println("Неправильная скорость! Должна быть от 1 до 250.");
}
} else {
System.out.println("Ошибка: введите целое число!");
scanner.nextLine(); }
}

Car car = new Car(name.trim(), speed);

Choose a reason for hiding this comment

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

trim() лучше сразу сделать при чтении строки, что по идее уже выполняется, но делается только для проверки в конструкции if - можно вызов trim() сделать один раз вместо двух в данном случае

race.checkLeader(car);
}

System.out.println("Самая быстрая машина: " + race.getWinner());
scanner.close();
}
}
17 changes: 17 additions & 0 deletions src/main/java/Race.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
public class Race {
String leaderName = "";
int leaderDistance = 0;

Comment on lines +2 to +4

Choose a reason for hiding this comment

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

Обе переменные лучше сделать приватными, т.к. они относятся к внутренней логике работы класса, а для получения машины-победителя у тебя уже есть функция getWinner()

public void checkLeader(Car car) {
int distance = car.speed * 24;

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

public String getWinner() {
return leaderName;
}
}