Skip to content

Commit 025b0bd

Browse files
committed
car + race + command menu implemented (but not tested)
1 parent c521234 commit 025b0bd

3 files changed

Lines changed: 91 additions & 3 deletions

File tree

src/main/java/Car.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
public class Car {
2+
Integer speed;
3+
String name;
4+
5+
public Car(Integer speed, String name) {
6+
this.speed = speed;
7+
this.name = name;
8+
}
9+
}

src/main/java/Main.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
import java.util.ArrayList;
2+
import java.util.Scanner;
3+
14
public class Main {
5+
static Race race = new Race();
26

37
public static void main(String[] args) {
4-
// ваш код начнется здесь
5-
// вы не должны ограничиваться только классом Main и можете создавать свои классы по необходимости
6-
System.out.println("Привет Мир");
8+
race.contestStart();
79
}
810
}

src/main/java/Race.java

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import java.util.ArrayList;
2+
import java.util.Iterator;
3+
import java.util.Scanner;
4+
5+
public class Race {
6+
Car winner;
7+
8+
Scanner scanner = new Scanner(System.in);
9+
10+
ArrayList<Car> cars = new ArrayList<>();
11+
12+
public void calculateWinner(ArrayList<Car> cars) {
13+
14+
Iterator<Car> iterator = cars.iterator();
15+
winner = cars.get(0);
16+
while (iterator.hasNext()) {
17+
Car element = iterator.next();
18+
if (element.speed >= winner.speed) {
19+
winner = element;
20+
}
21+
}
22+
}
23+
24+
public void contestStart() {
25+
// ваш код начнется здесь
26+
// вы не должны ограничиваться только классом Main и можете создавать свои классы по необходимости
27+
28+
System.out.println("Введите команду ");
29+
30+
while (true) {
31+
printMenu();
32+
int command = scanner.nextInt();
33+
if (command == 1) {
34+
carInput();
35+
} else if (command == 2) {
36+
calculateWinner(cars);
37+
System.out.println("Побеждает тачка:" + winner.name);
38+
} else if (command == 0) {
39+
System.out.println("Выход");
40+
break;
41+
} else {
42+
System.out.println("Извините, такой команды пока нет.");
43+
}
44+
}
45+
}
46+
47+
public static void printMenu() {
48+
System.out.println("Что вы хотите сделать? ");
49+
System.out.println("1 - Добавить тачку");
50+
System.out.println("2 - Узнать победителя гонки");
51+
System.out.println("0 - Выход");
52+
}
53+
54+
public void carInput() {
55+
while (true) {
56+
System.out.println("Введите название тачилы");
57+
String name = scanner.nextLine();
58+
59+
System.out.println("Введите скорость > 0 но =< 250");
60+
Integer speed = scanner.nextInt();
61+
if (speed <= 0) {
62+
System.out.println(
63+
"Вы ввели отрицательную скорость.Введите скорость > 0 но =< 250"
64+
);
65+
} else if (speed > 250) {
66+
System.out.println(
67+
"Вы ввели слишком большую скорость. Введите скорость > 0 но =< 250"
68+
);
69+
} else {
70+
cars.add(new Car(speed, name));
71+
calculateWinner(cars);
72+
System.out.println("Тачка успешно создана!");
73+
break;
74+
}
75+
}
76+
}
77+
}

0 commit comments

Comments
 (0)