Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Проектная работа №1 #281

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
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
17 changes: 17 additions & 0 deletions src/main/java/Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
public class Car {
private final int speed;
private final String model;

public Car(int speed, String model) {
this.speed = speed;
this.model = model;
}

public int getSpeed() {
return speed;
}

public String getModel() {
return model;
}
}
56 changes: 54 additions & 2 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,58 @@
import java.util.Scanner;

public class Main {
private static final int FIRST_CAR = 1;
private static final int SECOND_CAR = 2;
private static final int THIRD_CAR = 3;
private static final int MIN_SPEED = 0;
private static final int MAX_SPEED = 250;

public static void main(String[] args) {
System.out.println("Hello world!");
System.out.println("Добро пожаловать в самую реалистичную гоночную игру!");
Scanner scanner = new Scanner(System.in);

Car carOne = getCarFromUser(scanner, FIRST_CAR);
Car carTwo = getCarFromUser(scanner, SECOND_CAR);
Car carThree = getCarFromUser(scanner, THIRD_CAR);

defineLeaderAndTextOut(carOne, carTwo, carThree);
}

public static Car getCarFromUser(Scanner scanner, int carNumber) {
String carModel = "";
boolean isValid = false;

while (!isValid) {
System.out.println("Введите название машины № " + carNumber);
carModel = scanner.nextLine().trim();
if (!carModel.isEmpty()) {
isValid = true;
} else {
System.err.println("Ошибка: Недопустимое название автомобиля. Пожалуйста, попробуйте снова.");
}
}

int carSpeed;
System.out.println("Введите скорость машины № " + carNumber);
do {
try {
carSpeed = Integer.parseInt(scanner.nextLine());
if (carSpeed < MIN_SPEED || carSpeed > MAX_SPEED) {
System.out.println("Введите корректную скорость машины № " + carNumber + "!");
continue;
}
break;
} catch (NumberFormatException e) {
System.err.println("Ошибка ввода: скорость должна быть целым числом.");
}
} while (true);

return new Car(carSpeed, carModel);
}

public static void defineLeaderAndTextOut(Car one, Car two, Car three) {
Race.defineLeaderFromParticipants(one, two, three);
Car raceLeader = Race.getLeader();
System.out.println("Гонку выиграл автомобиль " + raceLeader.getModel());
}
}
}
21 changes: 21 additions & 0 deletions src/main/java/Race.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
public class Race {
private static Car currentLeader;

public static void defineLeaderFromParticipants(Car one, Car two, Car three) {
if (one.getSpeed() >= two.getSpeed() && one.getSpeed() >= three.getSpeed()) {
setLeader(one);
} else if (two.getSpeed() >= one.getSpeed() && two.getSpeed() >= three.getSpeed()) {
setLeader(two);
} else {
setLeader(three);
}
}

private static void setLeader(Car leader) {
currentLeader = leader;
}

public static Car getLeader() {
return currentLeader;
}
}