From ee4374b3e351a8bc500ff4d9a5fae7fc6a32e84f Mon Sep 17 00:00:00 2001 From: oleg Date: Tue, 15 Oct 2024 21:40:56 +0600 Subject: [PATCH 1/2] yes --- src/main/java/Car.java | 17 +++++++++++++++++ src/main/java/Main.java | 39 ++++++++++++++++++++++++++++++++++----- src/main/java/Race.java | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 83 insertions(+), 5 deletions(-) create mode 100644 src/main/java/Car.java create mode 100644 src/main/java/Race.java diff --git a/src/main/java/Car.java b/src/main/java/Car.java new file mode 100644 index 00000000..5f595440 --- /dev/null +++ b/src/main/java/Car.java @@ -0,0 +1,17 @@ +public class Car { + private final String name; + private final int speed; + + public Car(String name, int speed){ + this.name = name; + this.speed = speed; +} + + public String gName(){ + return name; +} + + public int gSpeed(){ + return speed; +} +} \ No newline at end of file diff --git a/src/main/java/Main.java b/src/main/java/Main.java index db9356a0..5c68488e 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,6 +1,35 @@ - +import java.util.List; +import java.util.Scanner; +import java.util.ArrayList; public class Main { - public static void main(String[] args) { - System.out.println("Hello world!"); - } -} \ No newline at end of file + public static void main(String[] args){ + Scanner scanner = new Scanner(System.in); + System.out.println("Добро пожаловать в самую реалистичную гонку во вселенной"); + + List cars = new ArrayList<>(); + for (int i = 0; i < 3; i++){ + System.out.println("Укажи название автомобиля " + (i+1) + " "); + String name = scanner.nextLine().trim(); + + System.out.println("Укажи скорость автомобиля " + (i+1) + " " ); + int speed; + do { + try { + speed = Integer.parseInt(scanner.nextLine().trim()); + if (speed >= 0 && speed <= 250) { + break; + } else { + System.out.println("Неправильно указал скорость. Введи число от 1 до 250"); + } + } catch (NumberFormatException e) { + System.out.println("Неправилно написал. Нужнно указать чисо от 1 до 250"); + } + } while (true); + + cars.add(new Car(name, speed)); + } + Race race = new Race(cars); + race.printLeadInfo(); + } + + } \ 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 00000000..d81d114f --- /dev/null +++ b/src/main/java/Race.java @@ -0,0 +1,32 @@ +import java.util.List; + +public class Race { + private List cars; + + public Race(List cars) { + this.cars = cars; + } + + public Car fWin() { + double mDistant = 0; + Car win = null; + + for (Car car : cars) { + double distan = calcDistanc(car); + if (distan > mDistant){ + mDistant = distan; + win = car; + } + } + + return win; +} + + private double calcDistanc(Car car) { + return car.gSpeed() * 24; +} + public void printLeadInfo() { + Car win = fWin(); + System.out.printf("Самая быстрая машина: %s, прошла %,.2f км за 24 часа.\n", win.gName(), calcDistanc(win)); + } +} From d5b12926679cdfb4a78a50f901fec07001d7a9a9 Mon Sep 17 00:00:00 2001 From: oleg Date: Tue, 15 Oct 2024 21:42:50 +0600 Subject: [PATCH 2/2] yes --- src/main/java/Main.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 5c68488e..36d1b1d8 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -22,7 +22,7 @@ public static void main(String[] args){ System.out.println("Неправильно указал скорость. Введи число от 1 до 250"); } } catch (NumberFormatException e) { - System.out.println("Неправилно написал. Нужнно указать чисо от 1 до 250"); + System.out.println("Неправильно написал. Нужно указать число от 1 до 250"); } } while (true);