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

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 spead) {
this.name = name;
this.spead = spead;
}
public String getName() {
return name;
}
public int getSpeed (){
return spead;
}
}
33 changes: 32 additions & 1 deletion src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,37 @@
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 name = scanner.nextLine();

int spead;
while (true) {

System.out.println("Введите скорость машины №" + i + ":");
if (scanner.hasNextInt()) {
spead = scanner.nextInt();
scanner.nextLine();

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

Copy link
Copy Markdown

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("Неправельная скорость.");
}
} else {
System.out.println("Пожалуйста, введите число.");
scanner.nextLine();
}
}
Car car = new Car(name, spead);
race.updateLeader(car);
}
System.out.println("Самая быстрая машина: " + race.getLeaderName());
scanner.close();
}
}
15 changes: 15 additions & 0 deletions src/main/java/Race.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
public class Race {
private String leaderName = "";
private int maxDistance = 0;

public void updateLeader(Car car) {
int distance = (24) * car.getSpeed();

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 (distance > maxDistance) {
maxDistance = distance;
leaderName = car.getName();
}
}
public String getLeaderName() {
return leaderName;
}
}