forked from Yandex-Practicum/Java-Module-Project-YP
-
Notifications
You must be signed in to change notification settings - Fork 0
hw1 #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
hw1 #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
18 changes: 18 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,18 @@ | ||
|
|
||
| public class Car { | ||
| private final String name; | ||
| private final int speed; | ||
|
|
||
| public Car(String name, int speed) { | ||
| this.name = name; | ||
| this.speed = speed; | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public int getSpeed() { | ||
| return speed; | ||
| } | ||
| } |
67 changes: 65 additions & 2 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,6 +1,69 @@ | ||
| import java.util.Scanner; | ||
|
|
||
| public class Main { | ||
|
|
||
| private static final int CARS_COUNT = 3; | ||
| private static final int MIN_SPEED = 1; | ||
| private static final int MAX_SPEED = 250; | ||
|
|
||
| public static void main(String[] args) { | ||
| System.out.println("Hello world!"); | ||
| Scanner scanner = new Scanner(System.in); | ||
| Race race = new Race(); | ||
|
|
||
| for (int i = 1; i <= CARS_COUNT; i++) { | ||
| String name = readCarName(scanner, i); | ||
| int speed = readCarSpeed(scanner, i); | ||
|
|
||
| Car car = new Car(name, speed); | ||
| race.consider(car); | ||
| } | ||
|
|
||
| System.out.println("Самая быстрая машина: " + race.getLeaderName()); | ||
| scanner.close(); | ||
| } | ||
|
|
||
| private static String readCarName(Scanner scanner, int carNumber) { | ||
| while (true) { | ||
| System.out.println("Введите название машины No" + carNumber + ":"); | ||
| String name = scanner.nextLine().trim(); | ||
|
|
||
| if (name.isEmpty()) { | ||
| System.out.println("Ошибка: название не может быть пустым. Повторите ввод."); | ||
| continue; | ||
| } | ||
| return name; | ||
| } | ||
| } | ||
|
|
||
| private static int readCarSpeed(Scanner scanner, int carNumber) { | ||
| while (true) { | ||
| System.out.println("Введите скорость машины No" + carNumber + ":"); | ||
| String input = scanner.nextLine().trim(); | ||
|
|
||
| if (input.isEmpty()) { | ||
| System.out.println("Ошибка: скорость не может быть пустой. Повторите ввод."); | ||
| continue; | ||
| } | ||
|
|
||
| if (input.contains(".") || input.contains(",")) { | ||
| System.out.println("Ошибка: скорость должна быть целым числом. Повторите ввод."); | ||
| continue; | ||
| } | ||
|
|
||
| int speed; | ||
| try { | ||
| speed = Integer.parseInt(input); | ||
| } catch (NumberFormatException e) { | ||
| System.out.println("Ошибка: введите целое число. Повторите ввод."); | ||
| continue; | ||
| } | ||
|
|
||
| if (speed < MIN_SPEED || speed > MAX_SPEED) { | ||
| System.out.println("Неправильная скорость (допустимо " + MIN_SPEED + "–" + MAX_SPEED + ")."); | ||
| continue; | ||
| } | ||
|
|
||
| return speed; | ||
| } | ||
| } | ||
| } | ||
| } |
17 changes: 17 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,17 @@ | ||
|
|
||
| public class Race { | ||
| private String leaderName = ""; | ||
| private int leaderDistance = 0; | ||
|
|
||
| public void consider(Car car) { | ||
| int distance = 24 * car.getSpeed(); | ||
| if (distance > leaderDistance) { | ||
| leaderDistance = distance; | ||
| leaderName = car.getName(); | ||
| } | ||
| } | ||
|
|
||
| public String getLeaderName() { | ||
| return leaderName; | ||
| } | ||
| } |
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.