forked from Yandex-Practicum/Java-Module-Project-YP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
79 lines (66 loc) · 2.33 KB
/
Main.java
File metadata and controls
79 lines (66 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Car[] cars = new Car[3];
Race race = new Race();
for (int i = 0; i < cars.length; i++) {
System.out.printf("— Введите название машины №%d:%n", i + 1);
String name = scanner.next();
while (true) {
System.out.printf("— Введите скорость машины №%d:%n", i + 1);
String speedLine = scanner.next();
if (isDigits(speedLine)) {
int speed = Integer.parseInt(speedLine);
if (speed > 0 && speed <= 250) {
cars[i] = new Car(name, speed);
break;
}
}
System.out.println("— Неправильная скорость");
}
}
race.startRace(cars);
}
//Так как в курсе ещё не знакомы с regexp и исключениями, метод для определения является ли строка трехзначным числом и менее
public static boolean isDigits(String line) {
String numbers = "0123456789";
if (line.length() > 3) {
return false;
} else {
boolean allContains = true;
for (int i = 0; i < line.length(); i++) {
if (!numbers.contains(String.valueOf(line.charAt(i)))) {
allContains = false;
break;
}
}
return allContains;
}
}
}
class Car {
String name;
int speed;
public Car(String name, int speed) {
this.name = name;
this.speed = speed;
}
}
class Race {
private String winner = "";
private int currentDistance = 0;
void startRace(Car[] cars) {
for (Car car : cars) {
int distance = calculateDistance(car);
if (distance > currentDistance) {
currentDistance = distance;
winner = car.name;
}
}
System.out.println("Самая быстрая машина: " + winner);
}
private int calculateDistance(Car car) {
return car.speed * 24;
}
}