Skip to content
Open

df #1

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
60 changes: 58 additions & 2 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,62 @@
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();

int numberOfCars = 3;


for (int i = 0; i < numberOfCars; i++) {
System.out.println("— Введите название машины №" + (i + 1) + ":");
String name = scanner.nextLine();

int speed;
while (true) {
System.out.println("— Введите скорость машины №" + (i + 1) + ":");
if (scanner.hasNextInt()) {
speed = scanner.nextInt();
scanner.nextLine();
int minSpeed = 0;
int maxSpeed = 250;
if (speed > minSpeed && speed <= maxSpeed) {
Comment on lines +21 to +23

Choose a reason for hiding this comment

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

Минимальную и максимальную скорости лучше вынести в константы для повышения читабельности кода

break;
} else {
System.out.println("— Неправильная скорость");
}
}
}
Auto car = new Auto(name, speed);
race.checkNewLeader(car);
}
System.out.println("Самая быстрая машина: " + race.leader);
}
}


class Auto {
String name;
int speed;
Comment on lines +39 to +40

Choose a reason for hiding this comment

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

Поля лучше пометить final, тем самым исключив возможность их модификации извне


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


class Race {
String leader = "";
int distance = 0;
Comment on lines +50 to +51

Choose a reason for hiding this comment

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

Эти два поля лучше пометить private, чтобы инкапсулировать логику определения победителя, а для получения имени победителя - написать отдельную функцию-геттер


void checkNewLeader( Auto car) {
int time = 24;
int carDistance = time * car.speed;

if (carDistance > distance) {
distance = carDistance;
leader = car.name;
}
}
}
}