Skip to content
Open
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
71 changes: 70 additions & 1 deletion src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,75 @@
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 (true) {
System.out.println("Введите название автомобиля №" + i + ": ");
name = scanner.nextLine().trim();
if (name.isEmpty()) {
System.out.println("Вы забыли ввести название автомобиля, попробуйте еще!");
continue;
}
break;
}
Comment on lines +11 to +19

Choose a reason for hiding this comment

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

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

int speed;
while (true) {
System.out.println("Введите скорость автомобиля №" + i + ": ");
String input = scanner.nextLine().trim();

if (input.isEmpty()) {
System.out.println("Вы забыли ввести скорость, попробуйте еще!");
continue;
}
if (!input.matches("\\d+")) {

Choose a reason for hiding this comment

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

Проверка input.matches("\\d+") не гаранитрует, что ниже parseInt не выбросит исключение - лучше обработать исключение, чтобы была гарантия отсутствия ошибок

System.out.println("Скоростью может быть только целое число, попробуйте еще!");
continue;
}

speed = Integer.parseInt(input);
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.

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

break;
} else {
System.out.println("Скорость должна быть от 1 до 250. Попробуйте снова.");
}
}
Car car = new Car(name, speed);
race.updateLeader(car);
}
System.out.println("Самая быстрая машина: " + race.getWinnerName());


}
}

class Car {

Choose a reason for hiding this comment

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

Классы лучше в отдельных файлах объявлять, чтобы один файл не разрастался сильно

String name;
int speed;
Comment on lines +51 to +52

Choose a reason for hiding this comment

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

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


public Car(String carName, int carSpeed) {
this.name = carName;
this.speed = carSpeed;
}
}


class Race {
String leaderName = "";
int leaderDistance = 0;

public void updateLeader(Car newCar) {
int newDistance = 24 * newCar.speed;
if (newDistance > this.leaderDistance) {
leaderName = newCar.name;
leaderDistance = newDistance;
}
}
public String getWinnerName() {
return leaderName;
}
}