Skip to content
Open
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
82 changes: 81 additions & 1 deletion src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,86 @@
import java.util.ArrayList;
import java.util.Scanner;

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

for (int i = 1; i <= 3; i++) {
System.out.println("— Введите название машины №" + i);
String name = scanner.nextLine().trim();

while (name.isEmpty()) {
System.out.println("— Название не может быть пустым");
System.out.println("— Введите название машины №" + i);
name = scanner.nextLine().trim();
}

int speed = -1;
boolean validSpeed = false;

while (!validSpeed) {
System.out.println("— Введите скорость машины №" + i);
String speedInput = scanner.nextLine().trim();

if (speedInput.isEmpty()) {
System.out.println("— Неправильная скорость.");
continue;
}

try {
speed = Integer.parseInt(speedInput);

if (speed < 0 || speed > 250) {
System.out.println("— Неправильная скорость.");
} else {
validSpeed = true;
}
} catch (NumberFormatException e) {
System.out.println("— Неправильная скорость.");
}
}

automobiles.add(new Automobile(name, speed));
}

Automobile winner = Race.calculateWinner(automobiles);
System.out.println("Самая быстрая машина: " + winner.name);

scanner.close();
}
}

class Race {
public static Automobile calculateWinner(ArrayList<Automobile> automobiles) {
Automobile winner = automobiles.getFirst();
double maxDistance = calculateDistance(winner);

for (int i = 1; i < automobiles.size(); i++) {
Automobile currentAuto = automobiles.get(i);
double currentDistance = calculateDistance(currentAuto);

if (currentDistance > maxDistance) {
maxDistance = currentDistance;
winner = currentAuto;
}
}

return winner;
}


private static double calculateDistance(Automobile automobile) {
return automobile.speed * 24;
}
}

class Automobile {
String name;
Integer speed;

Automobile(String autoname, Integer autospeed) {
name = autoname;
speed = autospeed;
}
}