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
11 changes: 11 additions & 0 deletions src/main/java/Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

public class Car {
public String name;
public int speed;
Comment on lines +3 to +4

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, тем самым исключив возможность их модификации извне


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

}
44 changes: 43 additions & 1 deletion src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,48 @@
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 carNumber = 1; carNumber <= 3; carNumber++) {

String name = askCarName(scanner, carNumber);
int speed = askCarSpeed(scanner, carNumber);

Car car = new Car(name, speed);
race.checkNewLeader(car);
}

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Молодец, что не забыл закрыть сканнер - это очень важно)


}


public static String askCarName(Scanner scanner, int carNumber) {
System.out.print("Введите название машины №" + carNumber + ": ");
return scanner.nextLine();
}


public static int askCarSpeed(Scanner scanner, int carNumber) {

while (true) {
System.out.print("Введите скорость машины №" + carNumber + " (от 1 до 250): ");
String input = scanner.nextLine();

int speed = Integer.parseInt(input);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Данный вызов может выбросить креш, если вместо числа ввести символы - этот эксепшн необходимо обработать, предложив пользователю ввести данные заново


if (speed >= 1 && speed <= 250) {
return speed;
} else {
System.out.println("Ошибка: скорость должна быть в диапазоне от 1 до 250. Попробуйте снова.");
}
}
}


}
16 changes: 16 additions & 0 deletions src/main/java/Race.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
public class Race {
String leaderName = "";
int maxDistance = 0;
int timeOfTheRace = 24;
Comment on lines +2 to +4

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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



public void checkNewLeader(Car car) {
int distance = car.speed * timeOfTheRace;
if (distance > maxDistance) {
maxDistance = distance;
leaderName = car.name;
}
}


}