diff --git a/README.md b/README.md index 63be1bfe0..466d0be29 100644 --- a/README.md +++ b/README.md @@ -1 +1 @@ -# Пустой репозиторий для работы с Java кодом в Android Studio +# Этот репозиторий Машарова Егора. diff --git a/src/main/java/Car.java b/src/main/java/Car.java new file mode 100644 index 000000000..8c132e49c --- /dev/null +++ b/src/main/java/Car.java @@ -0,0 +1,31 @@ + +public class Car { + private String name; + private int speed; + + public Car(String name, int speed) { + this.name = name; + this.speed = speed; + } + + public String getName() { + return name; + } + + public int getSpeed() { + return speed; + } + + public void setSpeed(int speed) { + this.speed = speed; + } + + @Override + public String toString() { + return "Car{" + + "name='" + name + '\'' + + ", speed=" + speed + + '}'; + } +} + diff --git a/src/main/java/InputHandler.java b/src/main/java/InputHandler.java new file mode 100644 index 000000000..e69de29bb diff --git a/src/main/java/Main.java b/src/main/java/Main.java index db9356a08..a498d87fa 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,6 +1,115 @@ +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; public class Main { + private static Scanner scanner; + public static void main(String[] args) { - System.out.println("Hello world!"); + scanner = new Scanner(System.in); + Race race = new Race(); + + while (true) { + int choice = getChoice(); + switch (choice) { + case 1: + Car car = getCarFromInput(); + race.addCar(car); + System.out.println("Автомобиль добавлен."); + break; + case 2: + List carList = getCarsFromInput(); + race.addCars(carList); + System.out.println("Автомобили добавлены."); + break; + case 3: + Car leader = race.getLeader(); + if (leader != null) { + System.out.println("Текущий лидер: " + leader); + } else { + System.out.println("Еще нет автомобилей в гонке."); + } + break; + case 0: + System.out.println("Выход из программы."); + scanner.close(); + return; + default: + System.out.println("Неверный выбор. Попробуйте еще раз."); + } + } + } + + private static Car getCarFromInput() { + String carName; + int carSpeed; + System.out.print("Введите название автомобиля: "); + carName = scanner.nextLine(); + + while (true) { + System.out.print("Введите скорость автомобиля (0-250): "); + if (scanner.hasNextInt()) { + carSpeed = scanner.nextInt(); + scanner.nextLine(); // Consume the newline + if (carSpeed>= 0 && carSpeed <= 250) { + break; // Correct speed, exit loop + } else { + System.out.println("Неправильная скорость. Пожалуйста, введите значение от 0 до 250."); + } + } else { + System.out.println("Неверный ввод. Пожалуйста, введите целое число."); + scanner.next(); // Consume the invalid input + } + } + return new Car(carName, carSpeed); + } + + private static List getCarsFromInput() { + List carList = new ArrayList(); + System.out.print("Введите количество автомобилей для добавления: "); + int numberOfCars; + while (true) { + if(scanner.hasNextInt()) { + numberOfCars = scanner.nextInt(); + scanner.nextLine(); + if(numberOfCars> 0) { + break; + } else { + System.out.println("Неправильный ввод. Пожалуйста, введите число больше 0"); + } + } else { + System.out.println("Неверный ввод. Пожалуйста, введите целое число."); + scanner.next(); // Consume the invalid input + } + } + for(int i = 0; i < numberOfCars; i++) { + carList.add(getCarFromInput()); + } + return carList; + + } + + private static int getChoice() { + int choice; + while (true) { + System.out.println("\nВыберите действие:"); + System.out.println("1. Добавить автомобиль"); + System.out.println("2. Добавить список автомобилей"); + System.out.println("3. Показать текущего лидера"); + System.out.println("0. Выход"); + + System.out.print("Ваш выбор: "); + + if(scanner.hasNextInt()) { + choice = scanner.nextInt(); + scanner.nextLine(); // Consume the newline + break; + } else { + System.out.println("Неверный ввод. Пожалуйста, введите целое число."); + scanner.next(); // Consume the invalid input + } + + } + return choice; } } \ 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..e53aa515f --- /dev/null +++ b/src/main/java/Race.java @@ -0,0 +1,43 @@ +import java.util.ArrayList; +import java.util.List; +public class Race { + private List cars; + private Car leader; + + public Race() { + this.cars = new ArrayList(); + this.leader = null; + } + + public void addCar(Car car) { + cars.add(car); + updateLeader(); + } + + public void addCars(List carList) { + this.cars.addAll(carList); + updateLeader(); + } + + private void updateLeader() { + if (cars.isEmpty()) { + leader = null; + return; + } + Car currentLeader = cars.get(0); + for (Car car : cars) { + if (car.getSpeed()> currentLeader.getSpeed()) { + currentLeader = car; + } + } + leader = currentLeader; + } + + public Car getLeader() { + return leader; + } + + public List getCars() { + return cars; + } +}

AltStyle によって変換されたページ (->オリジナル) /