forked from Yandex-Practicum/Java-Module-Project-YP
-
Notifications
You must be signed in to change notification settings - Fork 0
Auto racers #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
Auto racers #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
35 changes: 35 additions & 0 deletions
src/main/java/Auto.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,35 @@ | ||
| import java.util.Scanner; | ||
|
|
||
| public class Auto { | ||
| private String model; | ||
| private int speed; | ||
|
|
||
| public void inputAutoData() { | ||
| Scanner console = new Scanner(System.in); | ||
|
|
||
| System.out.println("Введите название автомобиля: "); | ||
| model = console.nextLine(); | ||
|
|
||
| while (model.isEmpty()) { | ||
| System.out.println("Введено неверное значение."); | ||
| System.out.println("Введите название автомобиля: "); | ||
| model = console.nextLine(); | ||
| } | ||
|
|
||
| System.out.println("Введите скорость автомобиля (от 0 до 250): "); | ||
| speed = console.nextInt(); | ||
|
|
||
| while (speed < 0 || speed > 250) { | ||
| System.out.println("Введено неверное значение."); | ||
| System.out.println("Введите скорость автомобиля (от 0 до 250): "); | ||
| speed = console.nextInt(); | ||
| } | ||
| } | ||
| public String getModel() { | ||
| return model; | ||
| } | ||
|
|
||
| public int getSpeed() { | ||
| return speed; | ||
| } | ||
| } |
22 changes: 21 additions & 1 deletion
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,6 +1,26 @@ | ||
| import java.util.ArrayList; | ||
|
|
||
|
|
||
| public class Main { | ||
| public static void main(String[] args) { | ||
| System.out.println("Hello world!"); | ||
| Auto newAuto1 = new Auto(); | ||
| newAuto1.inputAutoData(); | ||
|
|
||
| Auto newAuto2 = new Auto(); | ||
| newAuto2.inputAutoData(); | ||
|
|
||
| Auto newAuto3 = new Auto(); | ||
| newAuto3.inputAutoData(); | ||
|
|
||
| ArrayList<Auto> members = new ArrayList<>(); | ||
| members.add(newAuto1); | ||
| members.add(newAuto2); | ||
| members.add(newAuto3); | ||
|
|
||
| Race leaderRace = new Race(); | ||
| Auto leader = leaderRace.findLeader(members); | ||
|
|
||
| System.out.println("Самая быстрая машина: " + leader.getModel()); | ||
|
|
||
| } | ||
| } |
22 changes: 22 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,22 @@ | ||
| import java.util.ArrayList; | ||
|
|
||
| public class Race { | ||
| private final int hours = 24; | ||
|
|
||
| public Auto findLeader(ArrayList<Auto> members) { | ||
| int longestDestination = 0; | ||
| Auto leader = null; | ||
| for (int i = 0; i < members.size(); i++) { | ||
| int destination = hours * members.get(i).getSpeed(); | ||
| if (destination > longestDestination) { | ||
| longestDestination = destination; | ||
| leader = members.get(i); | ||
| } | ||
| } | ||
| return leader; | ||
| } | ||
|
|
||
|
|
||
|
|
||
| } | ||
| //km = speed * h |
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.