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/Auto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
public class Auto {
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 Auto(String name, int speed) {
this.name = name;
this.speed = speed;
}
}
34 changes: 33 additions & 1 deletion src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,38 @@
import java.util.Scanner;

public class Main {

public static void main(String[] args) {
System.out.println("Hello world!");
Race race = new Race();
Scanner scanner = new Scanner(System.in);
for (int i = 0; i < 3; i++) {
System.out.println("Введите название машины: ");
String name = scanner.next();
System.out.println("Название машины №" + (i + 1) + ": " + name);
System.out.println("Введите скорость машины от 0 до 250: ");
while (true) {
String input = scanner.nextLine().trim();
if(input.isEmpty()){
continue;
}
Comment on lines +14 to +17

Choose a reason for hiding this comment

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

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

try {
int 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.

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

System.out.println("Скорость машины № " + (i + 1) + ": " + speed);
Auto autosValue = new Auto(name, speed);
race.calculateWinner(autosValue);
break;
} else if (speed <= 0 || speed > 250){
System.out.println("Недопустимое значение скорости. Скорость машины должна быть от 0 до 250");
}
} catch (NumberFormatException error){
System.out.println("Некорректное значение. Попробуйте снова");
}
}

race.winner = race.getWinner();
}
System.out.println("Победитель: " + race.winner);
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 winner = "";
int spaceValue = 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.

Две переменные лучше сделать приватными, тем самым инкапсулировав логику определения победителя в этом классе

public void calculateWinner(Auto autos) {
int space = autos.speed * 24;
if (spaceValue < space) {
winner = autos.name;
spaceValue = space;
}
}

public String getWinner() {

return winner;
}
}