-
Notifications
You must be signed in to change notification settings - Fork 0
Request for check #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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| import java.security.PublicKey; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class Calculator { | ||
| private double currentSum = 0; | ||
| final private List<Item> itemList = new ArrayList<>(); | ||
|
|
||
| public double getCurrentSum() { | ||
| return currentSum; | ||
| } | ||
|
|
||
| public boolean addItem(Item item) { | ||
| if (findItem(item)) { | ||
| return false; | ||
| } else { | ||
| itemList.add(item); | ||
| currentSum = currentSum + item.getPrice(); | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| private boolean findItem(Item lookingItem) { | ||
| for (Item item : itemList) { | ||
| if (item.equals(lookingItem)) { | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| public String getItemList() { | ||
| String resultString = ""; | ||
| for (Item item : itemList) { | ||
| resultString = resultString + item.getName() + ", " | ||
| + Formatter.formatDoubleToString(item.getPrice()) + "\n"; | ||
| } | ||
| return resultString; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| public class Formatter { | ||
| public static String formatDoubleToString(double inputNumber) { | ||
|
|
||
| int roundedNumber = (int) Math.floor(inputNumber); | ||
| int preLastDigit = roundedNumber % 100 / 10; | ||
| int lastDigit = roundedNumber % 10; | ||
|
|
||
| String rublesValue; | ||
|
|
||
| if (preLastDigit == 1) { | ||
| rublesValue = "рублей"; | ||
| } else { | ||
| if (lastDigit == 1) { | ||
| rublesValue = "рубль"; | ||
| } else if (lastDigit >= 2 && lastDigit <= 4) { | ||
| rublesValue = "рубля"; | ||
| } else { | ||
| rublesValue = "рублей"; | ||
| } | ||
|
Comment on lines
+13
to
+19
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. 🍏 Такого рода блоки, как if-then-else, в Java можно заменить на switch case. Дело вкуса |
||
| } | ||
|
|
||
| return String.format("%.2f", inputNumber) + " " + rublesValue; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| public class Item { | ||
|
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. Круто, что используешь класс, как модель данных для описания товара 👍 |
||
| final private String name; | ||
| final private double price; | ||
|
|
||
| public Item(String name, double price) { | ||
| this.name = name; | ||
| this.price = price; | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public double getPrice() { | ||
| return price; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) return true; | ||
| if (o == null || getClass() != o.getClass()) return false; | ||
|
|
||
| Item item = (Item) o; | ||
|
|
||
| if (Double.compare(item.price, price) != 0) return false; | ||
| return name != null ? name.equals(item.name) : item.name == null; | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| int result; | ||
| long temp; | ||
| result = name != null ? name.hashCode() : 0; | ||
| temp = Double.doubleToLongBits(price); | ||
| result = 31 * result + (int) (temp ^ (temp >>> 32)); | ||
| return result; | ||
| } | ||
|
Comment on lines
+18
to
+37
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. Весьма полезное решение переопределять hashCode и equals 👍 |
||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,59 @@ | ||
| import java.util.Scanner; | ||
|
|
||
| // dev branch for Y.Practicum | ||
| public class Main { | ||
|
|
||
| public static void main(String[] args) { | ||
| // ваш код начнется здесь | ||
| // вы не должны ограничиваться только классом Main и можете создавать свои классы по необходимости | ||
| System.out.println("Привет Мир"); | ||
| Scanner scanner = new Scanner(System.in); | ||
| System.out.println("Привет! Введите количество гостей:"); | ||
| int personCount; | ||
| while (true) { | ||
|
|
||
| if (scanner.hasNextInt()) { | ||
| personCount = scanner.nextInt(); | ||
| if (personCount <= 1) { | ||
| System.out.println("Число гостей должно быть больше 1. Давайте еще раз"); | ||
| } else { | ||
| scanner.nextLine(); | ||
| break; | ||
| } | ||
| } else { | ||
| System.out.println("Нет, нужно число! Давайте еще раз! "); | ||
| scanner.nextLine(); | ||
| } | ||
| } | ||
|
|
||
| String userResponce = ""; | ||
| Calculator calculator = new Calculator(); | ||
|
|
||
| while (!"Завершить".equalsIgnoreCase(userResponce)) { | ||
| System.out.println("Введите название товара"); | ||
| String itemName = scanner.nextLine(); | ||
| double itemPrice; | ||
| System.out.println("Введите цену товара, в формате хх,хх"); | ||
| while (true) { | ||
| if (scanner.hasNextDouble()) { | ||
| itemPrice = scanner.nextDouble(); | ||
| scanner.nextLine(); | ||
| break; | ||
| } else { | ||
| System.out.println("Нет, цена должна быть числом, в формате хх,хх." + | ||
| " Давайте еще раз!"); | ||
| scanner.nextLine(); | ||
| } | ||
| } | ||
| Item item = new Item(itemName, itemPrice); | ||
| calculator.addItem(item); | ||
| System.out.println("Если желаете продолжить добавление товаров - введите любой символ. " + | ||
| "Если желаете закончить добавление - введите \"Завершить\". "); | ||
| userResponce = scanner.nextLine(); | ||
| } | ||
|
|
||
| System.out.println("Добавленные товары:"); | ||
| System.out.println(calculator.getItemList()); | ||
|
|
||
| System.out.println("Общая сумма товаров: " + Formatter.formatDoubleToString(calculator.getCurrentSum()) + "\n"); | ||
|
|
||
| System.out.println("Каждый должен заплатить: " + Formatter.formatDoubleToString(calculator.getCurrentSum() / personCount)); | ||
| } | ||
| } |
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.
🍏 Можно вместо boolean добавить void и убрать return, т.к. возвращаемое значение у тебя нигде не используется