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 #272

Open
wants to merge 3 commits into
base: dev
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
20 changes: 20 additions & 0 deletions src/main/java/Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
public class Car {
private String name;
private int speed;

public Car() {
}

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

public String getName() {
return name;
}

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

public class CompetitorsCreation {
private final Scanner scanner = new Scanner(System.in);
private final List<Car> cars = new ArrayList<>();
private final int competitors = 3;
private final int maxSpeed = 250;
private final int minSpeed = 1;

void inputCompetitors() {
while (cars.size() < competitors) {
int countCar = cars.size() + 1;
Car car = new Car(nameCar(countCar), checkSpeed(countCar));
cars.add(car);
}
scanner.close();
}

int checkSpeed(int countCar) {
int speed;
while (true) {
System.out.println("Введите скорость автомобиля №" + countCar + ":");
if (scanner.hasNextInt()) {
speed = scanner.nextInt();
scanner.nextLine();
if (minSpeed <= speed && speed <= maxSpeed) {
return speed;
} else {
System.out.println("Скорость выходит из диапазона значений. Повторите попытку...");
}
} else {
System.out.println("Введено недопустимое значение. Повторите попытку...");
scanner.next();
}
}
}

String nameCar(int countCar) {
System.out.println("Введите название автомобиля №" + countCar + ":");
return scanner.next();
}

List<Car> getCars() {
return cars;
}
}
14 changes: 11 additions & 3 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@

public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
System.out.println("*-------------------------------------------------------------------------*");
System.out.println(" Добро пожаловать на гонку '24 часа Ле-Мана!'");
System.out.println(" Вам необходимо ввести название и скорость автомобиля (от 1 до 250 км/ч).");
System.out.println(" В гонке будут соревноваться три участника.");
System.out.println(" После чего мы узнаем победителя гонки!");
System.out.println("*-------------------------------------------------------------------------*\n");

CompetitorsCreation competitorsCreation = new CompetitorsCreation();
competitorsCreation.inputCompetitors();
new Race().printWinner(competitorsCreation.getCars());
}
}
}
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 {

public void printWinner(List<Car> cars) {
Car winner = new Car();
for (Car car : cars) {
if (winner.getSpeed() <= car.getSpeed()) {
winner = car;
}
}

System.out.println("\n*-------------------------------------------------------------------------*");
System.out.println(" Победитель в нашей гонке - '" + winner.getName() + "'!");
System.out.println("*-------------------------------------------------------------------------*");
}
}