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
9 changes: 9 additions & 0 deletions src/main/java/Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
public class Car {
String name;
double speed;

Car(String name, double speed) {
this.name = name;
this.speed = 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.Scanner;
import java.util.ArrayList;

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

for (int i = 0; i < 3; i++) {
System.out.printf("— Введите название машины №%d:\n", i + 1);
name = scanner.next();

while (true) {
System.out.printf("— Введите скорость машины #%d\n", i + 1);
speed = scanner.next();
if (isSpeedLegit(speed)) {
break;
}
System.out.println("— Неправильная скорость");
}

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

scanner.close();
Race race = new Race(cars);
System.out.printf("Самая быстрая машина: %s\n", race.getWinner().name);
}

private static boolean isSpeedLegit(String userInput) {
try {
double speed = Double.parseDouble(userInput);
return speed >= 0 && speed <= 250;

} catch (NumberFormatException e) {
return false;
}
}
}
}
24 changes: 24 additions & 0 deletions src/main/java/Race.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import java.util.ArrayList;

public class Race {
private final ArrayList<Car> cars;

Race(ArrayList<Car> cars) {
this.cars = cars;
}

public Car getWinner() {

Car winner = cars.getFirst();
for (int i = 1; i < cars.size(); i++) {
if (cars.get(i).speed > winner.speed) {
winner = cars.get(i);
}
}


return winner;
}


}