Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/main/java/Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
public class Car {
private final String name;
private final int speed;
public Car(String name, int speed) {
if (speed<=0 || speed > 250) {
throw new IllegalArgumentException("Скорость должна быть от 1 до 250 км/ч");
}
this.name = name;
this.speed = speed;
}

public String getName() {
return name;
}

public int getSpeed() {
return speed;
}
}
39 changes: 37 additions & 2 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,41 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
Scanner scanner = new Scanner(System.in);
List<Car> cars = new ArrayList<>();

System.out.println("Введите данные 3 автомобилей:");

for (int i = 1; i <= 3; i++) {
System.out.print("Автомобиль " + i + ". Название: ");
String name = scanner.nextLine();

int speed = 0;
boolean isValidSpeed = false;

while (!isValidSpeed) {
System.out.print("Скорость (1-250 км/ч): ");
try {
speed = Integer.parseInt(scanner.nextLine());
if (speed > 0 && speed <= 250) {
isValidSpeed = true;
} else {
System.out.println("Ошибка: скорость должна быть от 1 до 250 км/ч!");
}
} catch (NumberFormatException e) {
System.out.println("Ошибка: введите число!");
}
}

cars.add(new Car(name, speed));
}

Race race = new Race(cars);
Car winner = race.determineWinner();

System.out.println("\nСамая быстрая машина: " + winner.getName());
}
}
}
22 changes: 22 additions & 0 deletions src/main/java/Race.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import java.util.List;
public class Race {
private final List<Car> cars;
public Race(List<Car> cars) {
this.cars = cars;
}

public Car determineWinner() {
Car winner = null;
int maxDistance = 0;

for (Car car : cars) {
int distance = car.getSpeed() * 24;
if (distance > maxDistance) {
maxDistance = distance;
winner = car;
}
}

return winner;
}
}