forked from Yandex-Practicum/Java-Module-Project
-
Notifications
You must be signed in to change notification settings - Fork 0
Homework 1 #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
Open
Homework 1 #1
Changes from all commits
Commits
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
26 changes: 26 additions & 0 deletions
src/main/java/Car.java
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,26 @@ | ||
| public class Car { | ||
| private Integer speed; | ||
| private String name; | ||
|
|
||
| public Car(Integer speed, String name) { | ||
| this.setSpeed(speed); | ||
| this.setName(name); | ||
| } | ||
|
|
||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public void setName(String name) { | ||
| this.name = name; | ||
| } | ||
|
|
||
| public Integer getSpeed() { | ||
| return speed; | ||
| } | ||
|
|
||
| public void setSpeed(Integer speed) { | ||
| this.speed = speed; | ||
| } | ||
| } |
5 changes: 2 additions & 3 deletions
src/main/java/Main.java
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,8 +1,7 @@ | ||
| public class Main { | ||
| static Race race = new Race(); | ||
|
|
||
| public static void main(String[] args) { | ||
| // ваш код начнется здесь | ||
| // вы не должны ограничиваться только классом Main и можете создавать свои классы по необходимости | ||
| System.out.println("Привет Мир"); | ||
| race.contestStart(); | ||
| } | ||
| } |
117 changes: 117 additions & 0 deletions
src/main/java/Race.java
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,117 @@ | ||
| import java.util.ArrayList; | ||
| import java.util.InputMismatchException; | ||
| import java.util.Iterator; | ||
| import java.util.Scanner; | ||
|
|
||
| public class Race { | ||
| Car winner; | ||
|
|
||
| Scanner scanner = new Scanner(System.in); | ||
|
|
||
| ArrayList<Car> cars = new ArrayList<>(); | ||
|
|
||
| public void calculateWinner(ArrayList<Car> cars) { | ||
| winner = cars.get(0); | ||
| for(Car car: cars) { | ||
| if (car.getSpeed() >= winner.getSpeed()) { | ||
| winner = car; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public void contestStart() { | ||
|
|
||
| System.out.println("Введите команду "); | ||
|
|
||
| while (true) { | ||
| printMenu(); | ||
| int command = scanner.nextInt(); | ||
| if (command == 1) { | ||
| carInput(); | ||
| } else if (command == 2) { | ||
| calculateWinner(cars); | ||
| System.out.println("Побеждает тачка:" + winner.getName() + " со скоростью " + winner.getSpeed()); | ||
| } else if (command == 0) { | ||
| System.out.println("Выход"); | ||
| break; | ||
| } else { | ||
| System.out.println("Извините, такой команды пока нет"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public void printMenu() { | ||
| System.out.println("Что вы хотите сделать? "); | ||
| System.out.println("1 - Добавить тачку"); | ||
| System.out.println("2 - Узнать победителя гонки"); | ||
| System.out.println("0 - Выход"); | ||
| } | ||
|
|
||
| public void carInput() { | ||
| while (true) { | ||
| Integer speed = inputSpeed(); | ||
| String name = inputName(); | ||
| Car car = new Car(speed, name); | ||
| cars.add(car); | ||
| System.out.println("Тачка успешно создана!"); | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| private String inputName() { | ||
| System.out.println("Введите название тачилы"); | ||
| return scanner.nextLine(); | ||
| } | ||
|
|
||
| private Integer inputSpeed() { | ||
| Integer speed; | ||
| while(true) { | ||
| speed = scanSpeed(); | ||
| if (checkSpeed(speed)) { | ||
| break; | ||
| } else { | ||
| speed = inputSpeed(); | ||
| break; | ||
| } | ||
| } | ||
| return speed; | ||
| } | ||
|
|
||
| private Boolean checkSpeed(Integer speed) { | ||
| Boolean isCorrect; | ||
| if (speed <= 0) { | ||
| System.out.println( | ||
| "Вы ввели отрицательную скорость.Введите скорость > 0 но =< 250" | ||
| ); | ||
| isCorrect = false; | ||
| } else if (speed > 250) { | ||
| System.out.println( | ||
| "Вы ввели слишком большую скорость. Введите скорость > 0 но =< 250" | ||
| ); | ||
| isCorrect = false; | ||
| } else { | ||
| isCorrect = true; | ||
| } | ||
| return isCorrect; | ||
| } | ||
|
|
||
| private Integer scanSpeed() { | ||
| Integer speed; | ||
| System.out.println("Введите скорость > 0 но =< 250"); | ||
| while (true) { | ||
| try { | ||
| speed = scanner.nextInt(); | ||
| scanner.nextLine(); // очищаем буфер | ||
| if (speed > 0 && speed <= 250) { | ||
| break; // выход из цикла при корректном вводе | ||
| } else { | ||
| System.out.println("Введите скорость в пределах > 0 но =< 250"); | ||
| } | ||
| } catch (InputMismatchException e) { | ||
| System.out.println("Вы ввели неверные данные. Введите скорость > 0 но =< 250 как целое число"); | ||
| scanner.nextLine(); // очищаем буфер | ||
| } | ||
| } | ||
| return speed; | ||
| } | ||
| } |
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.