Repository navigation
Complete all tasks. #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
Open
JohnGudron
wants to merge
2
commits into
main
Choose a base branch
from
dev
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| public class Car { | ||
|
|
||
| private final String name; | ||
| private final int speed; | ||
|
|
||
| public int getSpeed() { | ||
| return speed; | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public Car(String name, int speed) { | ||
| this.name = name; | ||
| this.speed = speed; | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,14 @@ | ||
|
|
||
| public class Main { | ||
|
|
||
| public static void main(String[] args) { | ||
| System.out.println("Hello world!"); | ||
|
|
||
| Race race = new Race(); | ||
| System.out.println("Приветствуем Вас на гонке 24 часа Ле-Мана!"); | ||
| race.takeInput("первого"); | ||
| race.takeInput("второго"); | ||
| race.takeInput("третьего"); | ||
|
|
||
| System.out.println(race.findWinner()); | ||
| } | ||
| } | ||
| } | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| import java.util.ArrayList; | ||
| import java.util.Comparator; | ||
| import java.util.Scanner; | ||
|
|
||
| public class Race { | ||
|
|
||
| private final Scanner scanner = new Scanner(System.in); | ||
| private final ArrayList<Car> cars = new ArrayList<>(); | ||
|
|
||
| public void takeInput(String carNum) { | ||
|
|
||
| System.out.printf("Введите через пробел имя и скорость %s автомобиля. Обращаем внимание, что скорость не может быть меньше 1 и больше 250.%n", carNum); | ||
| String[] input = scanner.nextLine().trim().split(" "); | ||
| if (input.length != 2) { // проверка правильности ввода через пробел | ||
| System.out.println("Не соблюден формат ввода, повторите ввод."); | ||
| takeInput(carNum); | ||
| return; // чтобы после отработки рекурсивного вызова метод не продолжал работу | ||
| } | ||
| while (!checkSpeed(input[1])) { | ||
| System.out.printf("Введите через пробел имя и скорость %s автомобиля. Обращаем внимание, что скорость не может быть меньше 1 и больше 250.%n", carNum); | ||
| input = scanner.nextLine().split(" "); | ||
| } | ||
| cars.add(new Car(input[0], Integer.parseInt(input[1]))); // фактический сеттер для cars | ||
| } | ||
|
|
||
| public static boolean checkSpeed(String speed) { | ||
| if (!(speed.matches("\\d+"))) { | ||
| System.out.println("Для ввода скорости можно использовать только цифры от 0 до 9"); | ||
| return false; | ||
| } else if (!(Integer.parseInt(speed) > 0) || !(Integer.parseInt(speed) <= 250)) { | ||
| System.out.println("Введена недопустимая скорость, повторите ввод"); | ||
| return false; | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| public String findWinner() { | ||
| cars.sort(Comparator.comparing(car -> car.getSpeed() * 24)); | ||
| return String.format("Победителем стал автомобиль %s, проехав %d километров", cars.get(2).getName(), cars.get(2).getSpeed() * 24); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Я бы лучше использовал что-то вроде такого: https://stackoverflow.com/questions/8391979/does-java-have-a-int-tryparse-that-doesnt-throw-an-exception-for-bad-data
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.
Да, хотелось что-то типа toIntOrNull(), но сходу не нашел, решил так. Еще лишние скобки зачем-то)
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.
В котоине будет такое, в джаве нет, насколько я помню. В целом реджексы лучше аккуратно использовать, потому что довольно дорогая вещь.
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.
Не задумывался про затратность реджексов - буду знать, спасибо. В целом, да, логичнее было бы трай кеч (раз строгих ограничений на имя нету) и завернуть бы его в экстеншн, для красоты)