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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| public class Calculator { | ||
| static double totalAmount; | ||
|
|
||
| void calculator() { | ||
| StringBuilder allProducts = new StringBuilder("\"Добавленные товары\":");//Используем класс StringBuilder, так как он является изменяемым и эффективенее работает с памятью, при конкатинации | ||
| Product product = new Product(); | ||
| System.out.println("Давайте добавим товар в счет."); | ||
| while (true) { | ||
| System.out.print("Введите название товара: "); | ||
| product.productName = Main.scanner.next(); | ||
|
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. старайся не использовать статические поля и функции, если только это не константы 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. используй метод nextLine, так как иначе название товара может содержать только одно слово |
||
| if (product.productName.equalsIgnoreCase("Завершить")) { | ||
| System.out.println("Вы ввели команду \"Завершить\".\nПроцесс добавления товаров остановлен."); | ||
| break; | ||
| } | ||
| System.out.print("Введите стоимость товара в формате \"рубли,копейки\": ");//проверить что вводить для отделеня дробной части от целой ( точка или запятая) | ||
| while (true) { | ||
| while (!Main.scanner.hasNextDouble() && !Main.scanner.hasNextInt()) { | ||
| Main.scanner.next(); | ||
| System.out.print("Введите стоимость товара числом: "); | ||
| } | ||
| product.productPrice = Main.scanner.nextDouble(); | ||
| if (product.productPrice > 0) { | ||
| break; | ||
| } else { | ||
| System.out.println("Некоректная стоимость. Стоимость товара должна быть больше \"0\"."); | ||
| } | ||
| } | ||
| allProducts = new StringBuilder(allProducts + "\n" + product.productName); | ||
| totalAmount = totalAmount + product.productPrice; | ||
| System.out.println("Товар успешно добавлен!\nДобавленный товар: " + product.productName); | ||
|
|
||
| System.out.println("Хотите добавить еще один товар?"); | ||
| String approved = Main.scanner.next(); | ||
| if (approved.equalsIgnoreCase("Да")) { | ||
| System.out.println("Продолжаем."); | ||
| } else { | ||
| System.out.println("Для прекращения заполнения введите команду \"Завершить\" или продолжите добавлять товар."); | ||
| } | ||
| } | ||
| System.out.println(allProducts + "\n" + "Общая стоимость: " + totalAmount); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| public class Formatter { | ||
| static void formatter() { | ||
| double amountPerPerson = Calculator.totalAmount / Main.numberOfPersons; | ||
| String rub; | ||
| if (10 < (int) amountPerPerson % 100 && (int) amountPerPerson % 100 < 15) { | ||
| rub = "рублей"; | ||
| } else { | ||
| int lastDigit = (int) amountPerPerson % 10; | ||
| switch (lastDigit) { | ||
| case 1: | ||
| rub = "рубль"; | ||
| break; | ||
| case 2: | ||
| case 3: | ||
| case 4: | ||
| rub = "рубля"; | ||
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.
Имена функций должны содержать в себе действие. Функция делает что-либо. Хорошо помогает практика переводить имена функций дословно, если после перевода понятно чем занимается функция, то название хорошее