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
9 changes: 9 additions & 0 deletions src/main/java/Auto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
public class Auto {
String name;
int speed;

public Auto(String name, int speed) {
this.name = name;
this.speed = speed;
}
}
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.ArrayList;
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
Scanner scanner = new Scanner(System.in);
ArrayList<Auto> autoList = new ArrayList<>();
Auto leader;
int autoCount = 3;
for (int i=0; i < autoCount; i++) {
String name;
while (true) {
System.out.println("Введите название " + (i+1) + " автомобиля:");
name = scanner.nextLine().trim();
if (!name.isEmpty()) {
break;
} else {
System.out.println("Некорректно указано название автомобиля");
}
}
int speed;
String speedInput;
while (true) {
System.out.println("Введите скорость " + (i + 1) + " автомобиля:");
speedInput = scanner.nextLine().trim();
boolean isInt = true;
for (int j=0; j < speedInput.length(); j++) {
char c = speedInput.charAt(j);
if (c < '0' || c > '9') {
isInt = false;
}
}
if (speedInput.isEmpty() || !isInt) {
System.out.println("Некорректно указана скорость автомобиля");
} else {
speed = Integer.parseInt(speedInput);
if (speed > 0 && speed <= 250) {
break;
} else {
System.out.println("Некорректно указана скорость автомобиля");
}
}
}
Auto auto = new Auto(name, speed);
autoList.add(auto);
}
leader = Race.identifyLeader(autoList);
System.out.println("Самая быстрая машина: " + leader.name);
scanner.close();
}
}
16 changes: 16 additions & 0 deletions src/main/java/Race.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import java.util.ArrayList;

public class Race {
public static Auto identifyLeader(ArrayList<Auto> autoList) {
Auto leader = autoList.getFirst();
int maxDistance = leader.speed * 24;
for (Auto auto : autoList) {
int currentDistance = auto.speed * 24;
if (currentDistance > maxDistance) {
leader = auto;
maxDistance = currentDistance;
}
}
return leader;
}
}