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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# Пустой репозиторий для работы с Java кодом в Android Studio
# 24 часа Ле-Мана - консольное Java приложение
2 changes: 2 additions & 0 deletions src/main/java/Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
public record Car(String name, int speed) {
}
43 changes: 42 additions & 1 deletion src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,47 @@
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<>();

for (int i = 0; i < 3; i++) {
String name = getValidName(scanner, i + 1);
int speed = getValidSpeed(scanner, i + 1);
cars.add(new Car(name, speed));
}

Race race = new Race();
race.calculateFastestCar(cars);
}

private static String getValidName(Scanner scanner, int carNumber) {
while (true) {
System.out.printf("Введите название машины №%d:%n", carNumber);
String name = scanner.nextLine().trim();
if (name.isBlank()) {
System.out.println("Пустое название");
continue;
}
return name;
}
}

private static int getValidSpeed(Scanner scanner, int carNumber) {
while (true) {
System.out.printf("Введите скорость машины №%d:%n", carNumber);
try {
int speed = Integer.parseInt(scanner.nextLine());
if (speed <= 0 || speed > 250) {
System.out.println("Неправильная скорость");
continue;
}
return speed;
} catch (NumberFormatException e) {
System.out.println("Неправильная скорость");
}
}
}
}
17 changes: 17 additions & 0 deletions src/main/java/Race.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import java.util.List;

public class Race {
private static final int RACE_TIME = 24;

public void calculateFastestCar(List<Car> cars) {
Car fastestCar = null;
for (Car car : cars) {
if (fastestCar == null || car.speed() > fastestCar.speed()) {
fastestCar = car;
}
System.out.printf("Машина \"%s\" проехала %d км за %d часа%n",
car.name(), car.speed() * RACE_TIME, RACE_TIME);
}
System.out.println("Самая быстрая машина: " + fastestCar.name());
}
}