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
# Проектная работа №1
10 changes: 10 additions & 0 deletions src/main/java/Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Класс «Автомобиль»
public class Car {
final String CAR_NAME;
final int CAR_SPEED;

public Car(String carName, int carSpeed) {
this.CAR_NAME = carName;
this.CAR_SPEED = carSpeed;
}
}
81 changes: 80 additions & 1 deletion src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,85 @@
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
Scanner scanner = new Scanner(System.in);
Race race = new Race();

System.out.println("Добро пожаловать на '24 часа Ле-Мана'!");
System.out.println("Нужно будет ввести три автомобиля - название, скорость");

// Ввод автоммобилей
for (int i = 1; i <= 3; i++) {
System.out.println("Введите название " + i + "-го автомобиля:");
String carName = getCorrectCarName(scanner);

System.out.println("Введите скорость " + i + "-го автомобиля (больше 0 и меньше 250):");
int carSpeed = getCorrectCarSpeed(scanner);

race.whoIsLeader(carName, carSpeed);

createCar(carName, carSpeed);
}

// Вывод победителя
System.out.println("🏁🏁🏁🏁🏁🏁🏁🏁🏁🏁🏁🏁");
System.out.println("Победитель гонки: " + race.getLeader());
System.out.println("🏁🏁🏁🏁🏁🏁🏁🏁🏁🏁🏁🏁");
}

// Получение корректного имени
public static String getCorrectCarName(Scanner scanner) {
String name;

while (true) {
name = scanner.nextLine();

if (name.trim().isEmpty()) {
System.out.println("Ошибка: название не может быть пустым");
} else if (!name.matches("[a-zA-Zа-яА-ЯёЁ]+")) {
System.out.println("Ошибка: название должно содержать только буквы");
} else {
break;
}
}

return name;
}

// Получение корректной скорости
public static int getCorrectCarSpeed(Scanner scanner) {
int speed;
final int MAX_SPEED = 250;
final int MIN_SPEED = 0;

while (true) {
String input = scanner.nextLine();

if (input.trim().isEmpty()) {
System.out.println("Ошибка: скорость не может быть пустым значением");
continue;
}

try {
speed = Integer.parseInt(input);

if (speed > MIN_SPEED && speed < MAX_SPEED) {
break;
} else {
System.out.println("Ошибка: скорость должна быть больше 0 и меньше 250. Попробуйте снова.");
}
} catch (NumberFormatException e) {
System.out.println("Ошибка: введено не целое число. Попробуйте снова.");
}
}

return speed;
}

// Создание автомобилей
public static void createCar(String carName, int carSpeed) {
new Car(carName, carSpeed);
System.out.println("Автомобиль '" + carName + "' со скорость '" + carSpeed + "' создан и добавлен на трассу");
System.out.println("---------------------------");
}
}
19 changes: 19 additions & 0 deletions src/main/java/Race.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Класс «Гонка»
public class Race {
String leader = "";
int maxDistance = 0;

// Определение лидера
public void whoIsLeader(String carName, int carSpeed) {
int distance = carSpeed * 24;
if (distance > this.maxDistance) {
leader = carName;
maxDistance = distance;
}
}

// Получение лидера
public String getLeader() {
return leader;
}
}