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
16 changes: 16 additions & 0 deletions src/main/java/Auto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
public class Auto {
String brand;
int speed;
Comment on lines +2 to +3

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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

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

public String getBrand() {
return brand;
}

public int getSpeed() {
return speed;
}
}
35 changes: 33 additions & 2 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,37 @@
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 speed = 0;
for (int i = 1; i <= 3; i = i + 1) {
System.out.println("Введите название машины №" + i);
String brand = scanner.next();


boolean validInput = false;
while (!validInput) {
System.out.println("Введите скорость машины №" + i);
if (scanner.hasNextInt()) {
speed = scanner.nextInt();
if (speed >= 0 && speed <= 250) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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

validInput = true;
} else {
System.out.println("Неправильная скорость");
}
} else {
System.out.println("Неправильная скорость");
scanner.next();
}
}
Auto auto = new Auto(brand, speed);
race.updateWinner(auto.getBrand(), auto.getSpeed());
}

scanner.close();
System.out.println(race.getCurrentWinner());
}
}
}

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 @@
public class Race {
String currentWinner = "";
int currentDistance = 0;

public void updateWinner(String newBrand, int newSpeed) {
int newDistance = newSpeed * 24;

if (newDistance > currentDistance) {
currentWinner = newBrand;
currentDistance = newDistance;
}
}

public String getCurrentWinner() {
return "Самая быстрая машина: " + currentWinner;
}
}