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
Homework #1 #1
Changes from all commits
Commits
Show all changes
3 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
22 changes: 0 additions & 22 deletions
src/main/java/Calculator.java
Oops, something went wrong.
17 changes: 0 additions & 17 deletions
src/main/java/Formatter.java
Oops, something went wrong.
10 changes: 0 additions & 10 deletions
src/main/java/Item.java
Oops, something went wrong.
50 changes: 8 additions & 42 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,49 +1,15 @@ | ||
| import java.util.Scanner; | ||
| import constructor.Race; | ||
| import constructor.CarConstructor; | ||
|
|
||
| public class Main { | ||
|
|
||
| public static void main(String[] args) { | ||
| Scanner scanner = new Scanner(System.in); | ||
|
|
||
| int friendCount; | ||
| while (true) { | ||
| System.out.println("На сколько человек необходимо разделить счет?"); | ||
| friendCount = scanner.nextInt(); | ||
|
|
||
| if (friendCount > 1) { | ||
| break; | ||
| } else if (friendCount == 1) { | ||
| System.out.println( | ||
| "Нет смысла делить сумму на одного человека. Давайте попробуем ввести другое значение, которое будет больше единицы."); | ||
| } else { | ||
| System.out.println("Неверное количество друзей. Значение должно быть болье единицы, давайте попробуем еще раз."); | ||
| } | ||
| } | ||
|
|
||
| Calculator calculator = new Calculator(friendCount); | ||
|
|
||
| while (true) { | ||
| System.out.println("Введите название товара"); | ||
| String name = scanner.next(); | ||
|
|
||
| System.out.println("Введите стоимость товара в формате: 'рубли.копейки' [10.45, 11.40]"); | ||
| double price = scanner.nextDouble(); | ||
|
|
||
| calculator.addItem(new Item(name, price)); | ||
|
|
||
| System.out.println( | ||
| "Хотите добавить еще один товар? Введите любой символ для продолжения, либо 'Завершить' если больше нет товаров для добавления"); | ||
| String answer = scanner.next(); | ||
|
|
||
| if (answer.equalsIgnoreCase("Завершить")) { | ||
| break; | ||
| } | ||
| CarConstructor constructor = new CarConstructor(); | ||
| Race race = new Race(constructor); | ||
| while (constructor.carsSize() < 3) { | ||
| race.prepare(); | ||
| } | ||
|
|
||
| double result = calculator.divideSum(); | ||
| Formatter formatter = new Formatter(); | ||
|
|
||
| System.out.println(calculator.cart); | ||
| System.out.println("Каждому человеку к оплате: " + formatter.roundResult(result) + " " + formatter.formatValue(result)); | ||
| race.getFasterCar(); | ||
| } | ||
|
|
||
| } |
30 changes: 30 additions & 0 deletions
src/main/java/constructor/CarConstructor.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,30 @@ | ||
| package constructor; | ||
|
|
||
| import java.util.Collections; | ||
| import java.util.HashSet; | ||
| import java.util.Set; | ||
|
|
||
| import models.Car; | ||
|
|
||
| public class CarConstructor { | ||
|
|
||
| private final Set<Car> cars = new HashSet<>(); | ||
|
|
||
| public boolean addCar(String name, int speed) { | ||
| Car newCar = new Car(name, speed); | ||
| if (cars.stream().anyMatch(car -> car.name.equals(name))) { | ||
| System.out.println("Автомобиль с именем '" + name + "' уже существует."); | ||
| return false; | ||
| } | ||
| return cars.add(newCar); | ||
|
|
||
| } | ||
|
|
||
| public int carsSize() { | ||
| return cars.size(); | ||
| } | ||
|
|
||
| public Set<Car> getCars() { | ||
| return Collections.unmodifiableSet(cars); | ||
| } | ||
| } |
62 changes: 62 additions & 0 deletions
src/main/java/constructor/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,62 @@ | ||
| package constructor; | ||
|
|
||
| import java.util.Scanner; | ||
|
|
||
| import models.Car; | ||
|
|
||
| public class Race { | ||
| private static final int MAX_SPEED = 250; | ||
| private static final int MIN_SPEED = 0; | ||
| private static final int HOURS_IN_DAY = 24; | ||
|
|
||
| private final CarConstructor constructor; | ||
| private final Scanner scanner; | ||
|
|
||
| public Race(CarConstructor constructor) { | ||
| this.constructor = constructor; | ||
| this.scanner = new Scanner(System.in); | ||
| } | ||
|
|
||
| public void prepare() { | ||
| String name = carName(); | ||
| int speed = carSpeed(); | ||
| constructor.addCar(name, speed); | ||
| } | ||
|
|
||
| private String carName() { | ||
| System.out.println(formatMessage("- Введите название машины No%d")); | ||
| return scanner.next(); | ||
| } | ||
|
|
||
| private int carSpeed() { | ||
| while (true) { | ||
| System.out.println(formatMessage("- Введите скорость машины No%d")); | ||
| try { | ||
| int speed = Integer.parseInt(scanner.next()); | ||
| if (speed >= MIN_SPEED && speed <= MAX_SPEED) { | ||
| return speed; | ||
| } else { | ||
| System.out.println("- Неправильная скорость."); | ||
| } | ||
| } catch (NumberFormatException e) { | ||
| System.out.println("- Пожалуйста, введите целое число."); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private String formatMessage(String message) { | ||
| return String.format(message, constructor.carsSize() + 1); | ||
| } | ||
|
|
||
| public void getFasterCar() { | ||
| Car fasterCar = constructor.getCars().stream() | ||
| .max((c1, c2) -> Integer.compare(c1.speed * HOURS_IN_DAY, c2.speed * HOURS_IN_DAY)) | ||
| .orElse(null); | ||
|
|
||
| if (fasterCar != null) { | ||
| System.out.printf("- Самая быстрая машина: %s%n", fasterCar.name); | ||
| } else { | ||
| System.out.println("- Нет машин в списке."); | ||
| } | ||
| } | ||
| } |
11 changes: 11 additions & 0 deletions
src/main/java/models/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,11 @@ | ||
| package models; | ||
|
|
||
| public class Car { | ||
| public String name; | ||
| public int speed; | ||
|
|
||
| public Car(String name, int speed) { | ||
| this.name = name; | ||
| this.speed = 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.