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
15 changes: 15 additions & 0 deletions src/main/java/Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
public class Car {
final static int HOURS = 24;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Константу лучше вынести в класс Race, так как эта константа означает продолжительность гонки

private String name;
private int speed;
public Car(String name, int speed) {
this.name = name;
this.speed = speed;
}
public String getName() {
return name;
}
public int calcDist() {
return speed * HOURS;
}
}
47 changes: 46 additions & 1 deletion src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,51 @@
import java.util.Scanner;
import java.util.ArrayList;

public class Main {
final static int MIN_SPEED = 1;
final static int MAX_SPEED = 250;
final static int NUM_CARS = 3;
Comment on lines +5 to +7

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Всю логику из класса Main лучше вынести в отдельный класс и файл, чтобы в Main остался только запуск программы

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

От хранения массива машин и лишнего цикла при определении победителя можно избавиться, если при вводе данных сразу вычислять победителя и хранить его в отдельной переменной, тогда программа будет требовать меньше памяти и работать быстрее


for (int i = 1; i <= NUM_CARS; i++) {
boolean trueName = false;
System.out.println("Введите автомобиль №" + i);
String name = "";
while (!trueName) {
System.out.print("Наименование: ");
name = scanner.nextLine().trim();
if (name.isEmpty())
System.out.println("Ошибка. Пустое название недопустимо");
else
trueName = true;
}
Comment on lines +16 to +23

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Код для считывания непустой строки с ввода лучше вынести в отдельную функцию - код, разделённый на небольшие функции, легче читать, поддерживать и переиспользовать


int speed = 0;
boolean trueSpeed = false;

while (!trueSpeed) {
System.out.print("Скорость в км/ч, от " + MIN_SPEED + " до " + MAX_SPEED + ": ");
if (scanner.hasNextInt()) {
speed = scanner.nextInt();
if ((speed >= MIN_SPEED) && (speed <= MAX_SPEED))
trueSpeed = true;
else
System.out.println("Ошибка. Введите допустимую скорость");
} else
System.out.println("Ошибка. Введите целое число");
scanner.nextLine();
}
Comment on lines +28 to +39

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Код для считывания скорости тоже лучше вынести в отдельную функцию


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

Race race = new Race();
race.winner(cars);
System.out.println("\nСамая быстрая машина: " + race.getWinner().getName());

scanner.close();
}

}
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.ArrayList;
public class Race {
private Car winner;
private int maxDist = 0;
public void winner(ArrayList<Car> cars) {
for (Car car : cars) {
int dist = car.calcDist();
if (dist > maxDist) {
maxDist = dist;
winner = car;
}
}
}
public Car getWinner() {
return winner;
}
}