Repository navigation
попытка проверки #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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| public class Car { | ||
| private String name; | ||
| private int speed; | ||
|
|
||
| public Car(String name, int speed) { | ||
| this.name = name; | ||
| this.speed = speed; | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public int getSpeed() { | ||
| return speed; | ||
| } | ||
| } | ||
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,49 +1,35 @@ | ||
| import java.util.InputMismatchException; | ||
| import java.util.Scanner; | ||
|
|
||
| public class Main { | ||
|
|
||
| public static void main(String[] args) { | ||
| Scanner scanner = new Scanner(System.in); | ||
| Race race = new Race(); | ||
|
|
||
| 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("Введите название товара"); | ||
| for (int i = 0; i < 3; i++) { | ||
| System.out.println("Введите название автомобиля " + (i + 1) + ":"); | ||
| 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; | ||
| int speed = 0; | ||
| while (true) { | ||
| System.out.println("Введите скорость автомобиля " + name + " (от 1 до 250):"); | ||
| try { | ||
| speed = scanner.nextInt(); | ||
| if (speed > 0 && speed <= 250) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. максимальную и минимальную скорость лучше вынести в константы с говорящим названием |
||
| break; | ||
| } else { | ||
| System.out.println("Скорость должна быть в диапазоне от 1 до 250. Попробуйте снова."); | ||
| } | ||
| } catch (InputMismatchException e) { | ||
| System.out.println("Некорректный ввод. Пожалуйста, введите целое число."); | ||
| scanner.next(); // Очистить некорректный ввод | ||
| } | ||
| } | ||
| } | ||
|
|
||
| double result = calculator.divideSum(); | ||
| Formatter formatter = new Formatter(); | ||
| Car car = new Car(name, speed); | ||
| race.checkNewLeader(car); | ||
| } | ||
|
|
||
| System.out.println(calculator.cart); | ||
| System.out.println("Каждому человеку к оплате: " + formatter.roundResult(result) + " " + formatter.formatValue(result)); | ||
| System.out.println("Самая быстрая машина: " + race.getLeader()); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| public class Race { | ||
| private String leaderName = ""; | ||
| private int leaderDistance = 0; | ||
|
|
||
| public void checkNewLeader(Car car) { | ||
| int distance = car.getSpeed() * 24; | ||
| if (distance > leaderDistance) { | ||
| leaderDistance = distance; | ||
| leaderName = car.getName(); | ||
| } | ||
| } | ||
|
Comment on lines
+5
to
+11
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Неплохо, молодец) |
||
|
|
||
| public String getLeader() { | ||
| return leaderName; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Так как два поля инициализируются в конструкторе и не меняются никогда, то можно не писать геттеры, а пометить два поля как
public final- это будет безопасно