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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# Пустой репозиторий для работы с Java кодом в Android Studio
# Репозиторий для работы с Java кодом в Android Studio.
9 changes: 9 additions & 0 deletions src/main/java/Automobile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
public class Automobile {
String autoBrand;
int autoSpeed;

public Automobile(String autoBrand, int autoSpeed ){
this.autoBrand = autoBrand;
this.autoSpeed = autoSpeed;
}
}
72 changes: 70 additions & 2 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,74 @@
import java.util.ArrayList;
import java.util.Scanner;

public class Main {
//private static final Scanner scanner = new Scanner(System.in);
static ArrayList<Automobile> listOfAuto = new ArrayList<>();

public static void main(String[] args) {
System.out.println("Hello world!");

// Получаем список участников
for ( int i = 1; i <= 3; i++ ) {
GetUserData(i);
}
// Вычисляем победителя и выводим результат
WhoIsWinner winner = new WhoIsWinner(listOfAuto);
winner.ThisIsAWinner();
System.out.println("Победитель этой непростой гонки автомобиль " + winner.ThisIsAWinner() + "!\n" +
"Поздравляем нового Чемпиона!!!");

}

public static void GetUserData(int member) {
String autoBrand = "";
String autoSpeedAsStr = "";
int autoSpeed = 0;
Scanner scan = new Scanner(System.in);

System.out.print("Введите название машины - " + member + "-ого участника соревнований: ");

while (true) {
autoBrand = scan.nextLine();
if (!autoBrand.trim().isEmpty() ) {
boolean isPresent = false;
for ( int i = 0; i < listOfAuto.size() ; i++ ) {
if (listOfAuto.get(i).autoBrand.equals(autoBrand)) {
isPresent = true;
break; // break для цикла for
}
}
// Пройдены следующие проверки на ввод названия машины:
// Введено не пустое значение
// Введеное название не было введено ранее. Уникальность важна для определения победителя
if (!isPresent) {
break; // break для цикла while
} else {
System.out.print("Такой участник уже есть. ");
}
}
System.out.print("Введите, пожалуйста, название автомобиля для регистрации в системе " + member + "-ого участника соревнований: ");
}

System.out.print("Введите скорость машины: ");
while (true) {
autoSpeedAsStr = scan.nextLine();
if (autoSpeedAsStr.matches("[0-9]+") && (autoSpeedAsStr.length() < 4 ) ) {
autoSpeed = Integer.parseInt(autoSpeedAsStr);
if ( ( autoSpeed > 0) && (autoSpeed <= 250) ) {
// Пройдены следующие проверки на ввод скорости:
// Введено только числовое значение
// Введено трехзначное значение
// Введено значение в рамках диапазона техзадания
break;
}
}
System.out.print("Вы ввели неверную скорость. Введите скорость машины в пределах от 0 до 250 включительно: ");
}

// Добавляем участника в список
listOfAuto.add(new Automobile(autoBrand, autoSpeed));
}
}

}


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

public class WhoIsWinner {

private final ArrayList<Automobile> listAuto;
// Конструктор получает параметром список участников
public WhoIsWinner(ArrayList<Automobile> listOfAuto) {
this.listAuto = listOfAuto;
}

// Метод вычисления победителя, возвращает марку автомобиля.
public String ThisIsAWinner() {
int distance = 0;
int maxDistance = 0;
String brandWinner = "";
// Проходим forEach по списку участников и сравниваем скорости
for (Automobile i : listAuto) {
// Расстояние пройденное автомобилем под указателем forEach
distance = i.autoSpeed * 24;
if (distance > maxDistance ){
maxDistance = distance;
brandWinner = i.autoBrand;
// Если победителя более одного (скорости были равны), выводим их тоже
} else if (distance == maxDistance) {
brandWinner = brandWinner + " и " + i.autoBrand;
}
}
// Возвращаем марку автомобиля победителя
return brandWinner;
}

}