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
55 changes: 54 additions & 1 deletion src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,59 @@
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
RaceCars raceCars = new RaceCars();
RaceTournament tournament = new RaceTournament();

for (int i = 1; i <= 3; i++) {

String carName = getCarName(i);
int carSpeed = getCarSpeed(i);

raceCars.carsSpecs.put(carName, carSpeed);
}

System.out.println("Текущие участники: " + raceCars.carsSpecs);
tournament.startTournament(raceCars);
}

private static String getCarName(int i) {
Scanner scanner = new Scanner(System.in);
Comment thread
Imapol8529 marked this conversation as resolved.
System.out.printf("\n— Введите название машины №%d:\n", i);
String input;

if (scanner.hasNext()) {
input = scanner.next();
if (!input.isEmpty()) {
return input;
} else {
System.out.println("\n— Неправильное название\n");
input = getCarName(i);
}
} else {
System.out.println("\n— Неправильное название\n");
input = getCarName(i);
}
return input;
}

private static int getCarSpeed(int i) {
Scanner scanner = new Scanner(System.in);
System.out.printf("\n— Введите скорость машины №%d:\n", i);
int input = -1;

if (scanner.hasNextInt()) {
input = scanner.nextInt();
if (input >= 0 && input <= 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.

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

return input;
} else {
System.out.println("\n— Неправильная скорость\n");
input = getCarSpeed(i);
}
} else {
System.out.println("\n— Неправильная скорость\n");
input = getCarSpeed(i);
}
return input;
}
}
6 changes: 6 additions & 0 deletions src/main/java/RaceCars.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import java.util.HashMap;

public class RaceCars {
public HashMap<String, Integer> carsSpecs = new HashMap<>();
Comment thread
Imapol8529 marked this conversation as resolved.
}

19 changes: 19 additions & 0 deletions src/main/java/RaceTournament.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import java.util.Map;

public class RaceTournament {
String winnerCarName;
int winnerCarSpeed;

public void startTournament(RaceCars cars) {
for (Map.Entry<String, Integer> carSpec : cars.carsSpecs.entrySet()) {
if (carSpec.getValue() > winnerCarSpeed) {
winnerCarSpeed = carSpec.getValue();
winnerCarName = carSpec.getKey();
}
}

System.out.printf("Самая быстрая машина: '%s' со скоростью '%d'", winnerCarName, winnerCarSpeed);
}
}