Repository navigation
get the race leader #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| public class Car { | ||
| public String name; | ||
| public int speed; | ||
|
|
||
| public Car(String name, int speed) { | ||
| this.name = name; | ||
| this.speed = speed; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,64 @@ | ||
| import java.util.Scanner; | ||
|
|
||
| public class Main { | ||
| private static final Scanner scanner = new Scanner(System.in); | ||
| private static final int CARS_COUNT = 3; | ||
| private static final int MAX_SPEED = 250; | ||
| private static final int MIN_SPEED = 0; | ||
|
|
||
| public static void main(String[] args) { | ||
| System.out.println("Hello world!"); | ||
| Race race = new Race(); | ||
|
|
||
| for (int i = 1; i <= CARS_COUNT; i++) { | ||
| String carName = getCarName(i); | ||
| int carSpeed = getCarSpeed(i); | ||
|
|
||
| Car newCar = new Car(carName, carSpeed); | ||
| race.checkLeader(newCar); | ||
| } | ||
|
|
||
| System.out.println("Самая быстрая машина: " + race.leader); | ||
| } | ||
|
|
||
| private static String getCarName(int i) { | ||
| String carName; | ||
|
|
||
| while (true) { | ||
| System.out.println("— Введите название машины №" + i + ": "); | ||
| carName = scanner.nextLine().trim(); | ||
| if (carName.isEmpty()) { | ||
| System.out.println("— Неправильное название: название машины не может быть пустым"); | ||
| } else { | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| return carName; | ||
| } | ||
|
|
||
| private static int getCarSpeed(int i) { | ||
| String carSpeedStr; | ||
| int carSpeed; | ||
|
|
||
| while (true) { | ||
| System.out.println("— Введите скорость машины №" + i + ": "); | ||
| carSpeedStr = scanner.nextLine().trim(); | ||
|
|
||
| if (!carSpeedStr.matches("-?\\d+")) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Регулярное выражение тоже лучше вынести в константу с говорящим названием, чтобы повысить читабельность кода |
||
| System.out.println("— Неправильная скорость: скорость должна быть целым числом"); | ||
| } else { | ||
| carSpeed = Integer.parseInt(carSpeedStr); | ||
|
|
||
| if (carSpeed < MIN_SPEED) { | ||
| System.out.println("— Неправильная скорость: скорость должна быть не меньше 0"); | ||
| } else if (carSpeed > MAX_SPEED) { | ||
| System.out.println("— Неправильная скорость: скорость должна быть не больше 250"); | ||
| } else { | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return carSpeed; | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| public class Race { | ||
| private final static int RACE_TIME = 24; | ||
|
|
||
| public String leader; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Данную переменную лучше сделать приватной, чтобы снаружи класса нельзя было её поменять, тем самым сломав логику работы этого класса (т.к. переменная должна менять своё значение только в функции |
||
| private int leadersDistance; | ||
|
|
||
| public void checkLeader(Car newCar) { | ||
| int newCarDistance = RACE_TIME * newCar.speed; | ||
|
|
||
| if (newCarDistance > leadersDistance) { | ||
| leader = newCar.name; | ||
| leadersDistance = newCarDistance; | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Поля лучше пометить
final, тем самым исключив возможность их модификации извне