diff --git a/src/main/java/Car.java b/src/main/java/Car.java new file mode 100644 index 00000000..297c1247 --- /dev/null +++ b/src/main/java/Car.java @@ -0,0 +1,17 @@ +public class Car { + private final int speed; + private final String model; + + public Car(int speed, String model) { + this.speed = speed; + this.model = model; + } + + public int getSpeed() { + return speed; + } + + public String getModel() { + return model; + } +} diff --git a/src/main/java/Main.java b/src/main/java/Main.java index db9356a0..bae81abd 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,6 +1,58 @@ +import java.util.Scanner; public class Main { + private static final int FIRST_CAR = 1; + private static final int SECOND_CAR = 2; + private static final int THIRD_CAR = 3; + private static final int MIN_SPEED = 0; + private static final int MAX_SPEED = 250; + public static void main(String[] args) { - System.out.println("Hello world!"); + System.out.println("Добро пожаловать в самую реалистичную гоночную игру!"); + Scanner scanner = new Scanner(System.in); + + Car carOne = getCarFromUser(scanner, FIRST_CAR); + Car carTwo = getCarFromUser(scanner, SECOND_CAR); + Car carThree = getCarFromUser(scanner, THIRD_CAR); + + defineLeaderAndTextOut(carOne, carTwo, carThree); + } + + public static Car getCarFromUser(Scanner scanner, int carNumber) { + String carModel = ""; + boolean isValid = false; + + while (!isValid) { + System.out.println("Введите название машины № " + carNumber); + carModel = scanner.nextLine().trim(); + if (!carModel.isEmpty()) { + isValid = true; + } else { + System.err.println("Ошибка: Недопустимое название автомобиля. Пожалуйста, попробуйте снова."); + } + } + + int carSpeed; + System.out.println("Введите скорость машины № " + carNumber); + do { + try { + carSpeed = Integer.parseInt(scanner.nextLine()); + if (carSpeed < MIN_SPEED || carSpeed > MAX_SPEED) { + System.out.println("Введите корректную скорость машины № " + carNumber + "!"); + continue; + } + break; + } catch (NumberFormatException e) { + System.err.println("Ошибка ввода: скорость должна быть целым числом."); + } + } while (true); + + return new Car(carSpeed, carModel); + } + + public static void defineLeaderAndTextOut(Car one, Car two, Car three) { + Race.defineLeaderFromParticipants(one, two, three); + Car raceLeader = Race.getLeader(); + System.out.println("Гонку выиграл автомобиль " + raceLeader.getModel()); } -} \ 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..1ecdb194 --- /dev/null +++ b/src/main/java/Race.java @@ -0,0 +1,21 @@ +public class Race { + private static Car currentLeader; + + public static void defineLeaderFromParticipants(Car one, Car two, Car three) { + if (one.getSpeed() >= two.getSpeed() && one.getSpeed() >= three.getSpeed()) { + setLeader(one); + } else if (two.getSpeed() >= one.getSpeed() && two.getSpeed() >= three.getSpeed()) { + setLeader(two); + } else { + setLeader(three); + } + } + + private static void setLeader(Car leader) { + currentLeader = leader; + } + + public static Car getLeader() { + return currentLeader; + } +}