Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/main/java/Calculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
public class Calculator {
static double totalAmount;

void calculator() {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Имена функций должны содержать в себе действие. Функция делает что-либо. Хорошо помогает практика переводить имена функций дословно, если после перевода понятно чем занимается функция, то название хорошее

StringBuilder allProducts = new StringBuilder("\"Добавленные товары\":");//Используем класс StringBuilder, так как он является изменяемым и эффективенее работает с памятью, при конкатинации
Product product = new Product();
System.out.println("Давайте добавим товар в счет.");
while (true) {
System.out.print("Введите название товара: ");
product.productName = Main.scanner.next();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

старайся не использовать статические поля и функции, если только это не константы

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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);
}
}
26 changes: 26 additions & 0 deletions src/main/java/Formatter.java
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 = "рубля";