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
53 changes: 51 additions & 2 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,55 @@
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 carName;
while (true) {
carName = scanner.nextLine().trim();
if (carName.isEmpty()) {
System.out.println("Название автомобиля не может быть пустым или состоять из пробелов, повторите ввод");
} else {
break;
}
}
Comment on lines +11 to +18

Choose a reason for hiding this comment

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

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


System.out.println("Введите скорость автомобиля №" + i);
int carSpeed;
while (true) {
if (scanner.hasNextInt()) {
carSpeed = scanner.nextInt();
if (carSpeed < 0 || carSpeed > 250) {

Choose a reason for hiding this comment

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

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

System.out.println("Скорость не может быть отрицательной или свыше 250, повторите ввод");
} else {
scanner.nextLine();
break;
}
}
else {
System.out.println("Некорректное значение скорости, повторите ввод");
scanner.nextLine();
}

}
new Vehicle(carName, carSpeed);
race.Race(carName, carSpeed);
}
scanner.close();
System.out.println("Машина под названием " + race.leader + " является лидером гонки");
}
}


}









Comment on lines +46 to +55

Choose a reason for hiding this comment

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

Лишние пустые строчки лучше удалить, чтобы не засоряли код. Достаточно использовать 1 пустую строку для разделения логически делимых блоков в коде

14 changes: 14 additions & 0 deletions src/main/java/Race.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
public class Race {
String leader;

Choose a reason for hiding this comment

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

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

private int distance = 0;

public String Race(String carName, int speed) {

int carDistance = 24 * speed;
if (carDistance > distance) {
distance = carDistance;
leader = carName;
}
return leader;
}
}
9 changes: 9 additions & 0 deletions src/main/java/Vehicle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
public class Vehicle {
String carName;
int carSpeed;
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 Vehicle(String carName, int speed) {
this.carName = carName;
this.carSpeed = speed;
}
}