diff --git a/src/main/java/Main.java b/src/main/java/Main.java index db9356a08..053b91270 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -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 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 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; } } \ No newline at end of file