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
15 changes: 15 additions & 0 deletions src/main/java/Auto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
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;
}
public String getName() {
return name;
}
public int getSpeed() {
return speed;
}
}

40 changes: 40 additions & 0 deletions src/main/java/InputRacerData.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import java.util.Scanner;

public class InputRacerData {
public static Auto inputRacerData(Scanner scanner) {
String inputName;

while (true) {
System.out.println("Введите имя гонщика: ");
inputName = scanner.nextLine();

Choose a reason for hiding this comment

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

Лучше тут ещё воспользоваться функцией trim(), чтобы не позволять вводить название машин, состоящие только из пробелов и переносов строк


if (!inputName.isEmpty()) {
break;
} else {
System.out.println("Введите непустое имя");
}
}

int inputSpeed;

while (true) {
System.out.println("Введите скорость гонщика: ");

if (scanner.hasNextInt()) {
inputSpeed = scanner.nextInt();
scanner.nextLine();

if (inputSpeed > 0 && inputSpeed < 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("Введите значение в пределах от 0 до 250");
}

} else {
System.out.println("Введите корректное значение");
scanner.nextLine();
}
}
return new Auto(inputName, inputSpeed);
}
}
11 changes: 10 additions & 1 deletion src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import java.util.ArrayList;
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
Scanner scanner = new Scanner(System.in);
ArrayList<Auto> autos = new ArrayList<>();

Choose a reason for hiding this comment

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

От хранения массива машин и лишнего цикла при определении победителя можно избавиться, если при вводе данных сразу вычислять победителя и хранить его в отдельной переменной, тогда программа будет требовать меньше памяти и работать быстрее


for (int i = 0; i < 3; i++) {
autos.add(InputRacerData.inputRacerData(scanner));
}
Race race = new Race();
race.findLeader(autos);
}
}
21 changes: 21 additions & 0 deletions src/main/java/Race.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import java.util.ArrayList;

public class Race {
String leader = "";
int distance = 0;
int time = 24;

public void findLeader(ArrayList<Auto> autos) {

for (Auto auto : autos) {
int distanceOfRacer = time * auto.getSpeed();

if (distanceOfRacer > distance) {
distance = distanceOfRacer;
leader = auto.getName();
}
}
System.out.printf("Лидер гонки - %s, дистанция - %d км", leader, distance );
}

}