diff --git a/src/main/java/Car.java b/src/main/java/Car.java new file mode 100644 index 000000000..7938ddc62 --- /dev/null +++ b/src/main/java/Car.java @@ -0,0 +1,17 @@ +public class Car { + private String name; + private int speed; + + public Car(String name, int speed) { + this.name = name; + this.speed = speed; + } + + public int getSpeed() { + return speed; + } + + public String getName() { + return name; + } +} \ No newline at end of file diff --git a/src/main/java/Main.java b/src/main/java/Main.java index db9356a08..b89e9a5f0 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -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(); + int hours = 24; + + for (int i = 0; i < 3; i++) { + System.out.print("Введите название машины No" + (i + 1) + ": "); + String name = scanner.next(); + + double speed = 0; + boolean validInput = false; + + while (!validInput) { + System.out.print("Введите скорость машины No" + (i + 1) + ": "); + if (scanner.hasNextDouble()) { + speed = scanner.nextDouble(); + if (speed> 0 && speed <= 250) { + validInput = true; + } else { + System.out.println("Неправильная скорость. Скорость должна быть от 0 до 250. Попробуйте снова."); + } + } else { + System.out.println("Ошибка: введено не число. Попробуйте снова."); + scanner.next(); + } + } + + Car car = new Car(name, (int) speed); + race.determineLeader(car, hours); + } + + System.out.println("Самая быстрая машина: " + race.getCurrentLeader()); + scanner.close(); } } \ No newline at end of file diff --git a/src/main/java/Race.java b/src/main/java/Race.java new file mode 100644 index 000000000..a7d0407d1 --- /dev/null +++ b/src/main/java/Race.java @@ -0,0 +1,16 @@ +public class Race { + private String currentLeader; + private int currentMaxSpeed; + + public void determineLeader(Car car, int hours) { + int distance = (int) (car.getSpeed() * hours); + if (distance> currentMaxSpeed) { + currentMaxSpeed = distance; + currentLeader = car.getName(); + } + } + + public String getCurrentLeader() { + return currentLeader; + } +} \ No newline at end of file