forked from Yandex-Practicum/Java-Module-Project
-
Notifications
You must be signed in to change notification settings - Fork 0
Внесение полученных замечаний #2
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
4 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
48 changes: 35 additions & 13 deletions
src/main/java/Calculator.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,22 +1,44 @@ | ||
| class Calculator { | ||
| public class Calculator { | ||
|
|
||
| int friendsCount; | ||
| static double bill; | ||
|
|
||
| String cart = "Добавленные товары:"; | ||
| double totalPrice = 0; | ||
| public static void addProduct(Product p) { | ||
| bill += p.getPrice(); | ||
| System.out.println(bill); | ||
| } | ||
|
|
||
| Calculator(int friendsCount) { | ||
| this.friendsCount = friendsCount; | ||
| public static String calculateBill(int numberOfCustomers) { | ||
| return correctCurrency(Math.floor(bill / numberOfCustomers)); | ||
| } | ||
|
|
||
| void addItem(Item item) { | ||
| totalPrice += item.price; | ||
| cart = cart + "\n" + item.name; | ||
| public static String correctCurrency (double value) { | ||
| int twoDigits = (int) value % 100; | ||
| int lastValue = (int) value % 10; | ||
| String correctFormat = value + " "; | ||
|
|
||
| System.out.println(item.name + " в корзине"); | ||
| if (String.valueOf(Math.abs((long)twoDigits)).charAt(0) == '1' && twoDigits >= 10 && twoDigits < 20) { | ||
| correctFormat += "рублей"; | ||
| } else { | ||
| switch (lastValue) { | ||
| case 5: | ||
| case 6: | ||
| case 7: | ||
| case 8: | ||
| case 9: | ||
| case 0: | ||
| correctFormat += "рублей"; | ||
| break; | ||
| case 1: | ||
| correctFormat += "рубль"; | ||
| break; | ||
| case 2: | ||
| case 3: | ||
| case 4: | ||
| correctFormat += "рубля"; | ||
| break; | ||
| } | ||
| } | ||
| return correctFormat; | ||
| } | ||
|
|
||
| double divideSum() { | ||
| return totalPrice / friendsCount; | ||
| } | ||
| } |
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.
94 changes: 62 additions & 32 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,79 @@ | ||
| import java.util.ArrayList; | ||
| import java.util.InputMismatchException; | ||
| import java.util.Scanner; | ||
|
|
||
| public class Main { | ||
| static ArrayList<Product> products = new ArrayList<>(); | ||
|
|
||
| public static void main(String[] args) { | ||
| Scanner scanner = new Scanner(System.in); | ||
| boolean correctCustomersNumberEntered = false; | ||
| int customers = 0; | ||
| Scanner s = new Scanner(System.in); | ||
| System.out.println("На скольких человек необходимо разделить счёт?"); | ||
| do { | ||
| try { | ||
| customers = s.nextInt(); | ||
| correctCustomersNumberEntered = true; | ||
|
|
||
| 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("Неверное количество друзей. Значение должно быть болье единицы, давайте попробуем еще раз."); | ||
|
|
||
| } catch (InputMismatchException e) { | ||
| System.out.println ("Это некорректное значение гостей. Введите корректное количество гостей."); | ||
| s.nextLine(); | ||
| } | ||
| } | ||
| } while (!correctCustomersNumberEntered); | ||
|
|
||
| Calculator calculator = new Calculator(friendCount); | ||
| while (customers <= 1) { | ||
| System.out.println("Это некорректное значение для подсчёта.\nВведите корректное количество гостей. Если гостей меньше двух, то расчет не требуется"); | ||
| customers = s.nextInt(); | ||
| } | ||
|
|
||
| outer: | ||
| while (true) { | ||
| System.out.println("Введите название товара"); | ||
| String name = scanner.next(); | ||
| System.out.println("Укажите наименование товара:"); | ||
| String product = s.next(); | ||
| System.out.println("Укажите стоимость товара в формате 'рубли.копейки' [10.45, 11.40]:"); | ||
| double price = 0; | ||
| boolean correctPriceEntered = false; | ||
| do { | ||
|
|
||
| System.out.println("Введите стоимость товара в формате: 'рубли.копейки' [10.45, 11.40]"); | ||
| double price = scanner.nextDouble(); | ||
| try { | ||
| price = s.nextDouble(); | ||
| if (price <= 0) { | ||
| System.out.println("Некорректный ввод.Товар должен быть указан в корректном формате 'рубли.копейки' [10.45, 11.40]"); | ||
| s.nextLine(); | ||
| } else { | ||
| correctPriceEntered = true; | ||
| } | ||
| } catch (InputMismatchException e) { | ||
| System.out.println("Товар должен быть указан в корректном формате 'рубли.копейки' [10.45, 11.40]"); | ||
| s.nextLine(); | ||
| } | ||
| } while (!correctPriceEntered); | ||
|
|
||
| calculator.addItem(new Item(name, price)); | ||
| Product p = new Product(product, price); | ||
| Calculator.addProduct(p); | ||
| products.add(p); | ||
|
|
||
| System.out.println( | ||
| "Хотите добавить еще один товар? Введите любой символ для продолжения, либо 'Завершить' если больше нет товаров для добавления"); | ||
| String answer = scanner.next(); | ||
|
|
||
| if (answer.equalsIgnoreCase("Завершить")) { | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| double result = calculator.divideSum(); | ||
| Formatter formatter = new Formatter(); | ||
| String enough; | ||
| do { | ||
| System.out.println("Добавить еще товаров в заказ (Да/Нет)?"); | ||
| enough = s.next(); | ||
| if (enough.equalsIgnoreCase("нет")) { | ||
| System.out.println("Добавленные товары:"); | ||
| for (int i = 0; i < products.size(); i++) { | ||
| System.out.println(products.get(i).getLabel()); | ||
| } | ||
| System.out.println("Каждый поситетль должен заплатить" + " " + Calculator.calculateBill(customers)); | ||
| break outer; | ||
| } else if (enough.equalsIgnoreCase("да")) { | ||
| break; | ||
| } else { | ||
| System.out.println("Некорректный ввод"); | ||
| } | ||
|
|
||
| System.out.println(calculator.cart); | ||
| System.out.println("Каждому человеку к оплате: " + formatter.roundResult(result) + " " + formatter.formatValue(result)); | ||
| } while (true); | ||
| } | ||
| } | ||
| } | ||
|
|
19 changes: 19 additions & 0 deletions
src/main/java/Product.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,19 @@ | ||
| public class Product { | ||
| String label; | ||
| double price; | ||
|
|
||
| Product(String label, double price) { | ||
| this.label = label; | ||
| this.price = price; | ||
|
|
||
| } | ||
|
|
||
| public String getLabel() { | ||
| return label; | ||
| } | ||
|
|
||
| public double getPrice() { | ||
| return price; | ||
| } | ||
|
|
||
| } |
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.