forked from Yandex-Practicum/Java-Module-Project-YP
-
Notifications
You must be signed in to change notification settings - Fork 0
Домашнее задание 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
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
14 changes: 14 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,14 @@ | ||
|
|
||
| public class Auto implements Comparable<Auto> { | ||
| String name; | ||
| int speed; | ||
|
|
||
|
|
||
| Auto(String name, int speed) { | ||
| this.name = name; | ||
| this.speed = speed; | ||
| } | ||
| public int compareTo(Auto o){ | ||
| return (o.speed-speed); | ||
| } | ||
| } |
48 changes: 47 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,52 @@ | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Scanner; | ||
|
|
||
| public class Main { | ||
| public static void main(String[] args) { | ||
| System.out.println("Hello world!"); | ||
| ArrayList<Auto> autoList = new ArrayList<>(); | ||
|
|
||
| competitors(autoList); | ||
| Race.check(autoList); | ||
| } | ||
|
|
||
| public static void competitors(ArrayList<Auto> autoList) { | ||
| Scanner scanner = new Scanner(System.in); | ||
| System.out.println("Объявляем участников гонки!"); | ||
| String name; | ||
|
|
||
| int speed = 0; | ||
| for(int i = 0; i < 3; i++){ | ||
| System.out.println("Введите название автомобиля No" + (i+1)); | ||
| name = scanner.next(); | ||
| speed = getValidSpeed(); | ||
| autoList.add(new Auto(name, speed)); | ||
| Race.isWinner(name, speed); | ||
| } | ||
| } | ||
|
|
||
| public static int getValidSpeed () { | ||
| String spStr; | ||
| int speed = 0; | ||
| boolean flag = true; | ||
|
|
||
| while (flag) { | ||
| System.out.println("Введите скорость"); | ||
| Scanner scanner = new Scanner(System.in); | ||
| spStr = scanner.next(); | ||
| try { | ||
| speed = Integer.parseInt(spStr); | ||
| if (speed <=0 || speed > 250){ | ||
| flag = true; | ||
| System.out.println("Ошибка, только целые числа от 0 да 250, еще раз..."); | ||
| }else { | ||
| flag = false; | ||
| } | ||
| } catch (NumberFormatException e) { | ||
| System.out.println("Ошибка, только целые числа от 0 да 250, еще раз..."); | ||
| flag = true; | ||
| } | ||
| } | ||
| return speed; | ||
| } | ||
| } |
39 changes: 39 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,39 @@ | ||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
|
|
||
| public class Race { | ||
| static String winner; | ||
| static int start = 0; | ||
| static int winnerDistance = 0; | ||
|
|
||
| public static int distance24h(int speed) { | ||
| return speed * 24; | ||
| } | ||
|
|
||
| public static void isWinner(String name, int distance) { | ||
| if (distance > start) { | ||
| start = distance; | ||
| winner = name; | ||
| winnerDistance = distance24h(distance); | ||
| } | ||
| } | ||
|
|
||
| public static void check (ArrayList<Auto> checkList) { | ||
| int count = 0; | ||
| Collections.sort(checkList); | ||
| for (int i = 0; i < checkList.size()-1; i++) { | ||
| if (checkList.get(i).speed == checkList.get(i+1).speed){ | ||
| count++; | ||
| } | ||
| } | ||
| if (count == (checkList.size()-1)){ | ||
| System.out.println("Все финишировали одновременно!"); | ||
| } else if (count == 1) { | ||
| System.out.println("Две машины: \'"+checkList.get(0).name+"\' и \'"+ checkList.get(1).name+"\' пришли одновременно, проехав "+distance24h(checkList.get(0).speed)+ " километров за 24 часа."); | ||
| } else if (count == 0) { | ||
| System.out.println("Победитель финишировал на машине: \'"+checkList.get(0).name+"\', проехав "+distance24h(checkList.get(0).speed) + " километров за 24 часа."); | ||
| } else { | ||
| System.out.println("Несколько участников пришли к финишу одновременно!"); | ||
| } | ||
| } | ||
| } |
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.