From eccc37b27cb134443d5516ab8d784dfe3ee0aa5a Mon Sep 17 00:00:00 2001 From: Valhero Date: 2022年10月27日 21:50:07 +0300 Subject: [PATCH 1/3] First commit for review. --- src/main/java/BillSplitCalculator.java | 87 ++++++++++++++++++++++++++ src/main/java/Main.java | 7 ++- src/main/java/Product.java | 9 +++ 3 files changed, 100 insertions(+), 3 deletions(-) create mode 100644 src/main/java/BillSplitCalculator.java create mode 100644 src/main/java/Product.java diff --git a/src/main/java/BillSplitCalculator.java b/src/main/java/BillSplitCalculator.java new file mode 100644 index 0000000..ad49514 --- /dev/null +++ b/src/main/java/BillSplitCalculator.java @@ -0,0 +1,87 @@ +import java.util.ArrayList; +import java.util.Scanner; + +public class BillSplitCalculator { + double totalCost; + short numberOfPersons; + ArrayList products = new ArrayList(); + + public void askForNumberOfPersons() { + Scanner scanner = new Scanner(System.in); + short input; + + System.out.print("Укажите количество людей для разделения счета: "); + while (true) { + while (!scanner.hasNextShort()) { + System.out.println("Это не целое число. Пожалуйста, введите снова: "); + scanner.next(); + } + input = scanner.nextShort(); + + if (input < 2) + System.out.println("Количество людей должно быть больше 1. Введите снова: "); + else + break; + } + numberOfPersons = input; + } + + private void addProductToList(Product product) { + products.add(product); + totalCost += product.price; + System.out.println("Товар успешно добавлен."); + } + + public void createProductFromInput() { + Scanner scanner = new Scanner(System.in); + + while (true) { + System.out.println("Введите название товара: "); + String name = scanner.nextLine(); + while (name.isEmpty() || !name.matches("(?U).*\\w.*")) { + System.out.println("Товар должен иметь название. Введите ещё раз: "); + name = scanner.nextLine(); + } + + System.out.println("Введите стоимость товара в формате 'рубли.копейки' (20.50): "); + String price = scanner.nextLine(); + while (!price.matches("\\d+\\.\\d{1,2}")) { + System.out.println("Неверный формат стоимости. Попробуйте ещё раз: "); + price = scanner.nextLine(); + } + + Product product = new Product(name, Double.parseDouble(price)); + addProductToList(product); + + System.out.println("Если нужно добавить ещё товар - нажмите Enter или введите любой символ, если хотите закончить - введите 'Завершить'"); + if (scanner.nextLine().equalsIgnoreCase("завершить")) + break; + } + } + + public void showFinalBill() { + System.out.println("Добавленные товары:\n-------------------------"); + + for (Product product : products) { + System.out.println(String.format("%s - %.2f\n-------------------------", product.name, product.price)); + } + System.out.println(String.format("\nОбщая стоимость товаров - %.2f %s ", totalCost, getCorrectRubleEnding(totalCost))); + System.out.println(String.format("\nКаждый должен заплатить %.2f %s", totalCost / numberOfPersons, getCorrectRubleEnding(totalCost / numberOfPersons))); + } + + public String getCorrectRubleEnding(double money) { + int moneyAsInt = (int) money; + + if (moneyAsInt % 100>= 11 && moneyAsInt % 100 <= 14) { + return "рублей"; + } else if (moneyAsInt % 10> 1 && moneyAsInt % 10 < 5) { + return "рубля"; + } else if (moneyAsInt % 10 == 1) { + return "рубль"; + } + + return "рублей"; + } + + +} diff --git a/src/main/java/Main.java b/src/main/java/Main.java index a9198c4..0c18583 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,8 +1,9 @@ public class Main { public static void main(String[] args) { - // ваш код начнется здесь - // вы не должны ограничиваться только классом Main и можете создавать свои классы по необходимости - System.out.println("Привет Мир"); + BillSplitCalculator calculator = new BillSplitCalculator(); + calculator.askForNumberOfPersons(); + calculator.createProductFromInput(); + calculator.showFinalBill(); } } diff --git a/src/main/java/Product.java b/src/main/java/Product.java new file mode 100644 index 0000000..7f4c1ac --- /dev/null +++ b/src/main/java/Product.java @@ -0,0 +1,9 @@ +public class Product { + String name; + double price; + + Product(String name, double price) { + this.name = name; + this.price = price; + } +} From a5d3f59d414e81a87636bbae5132b9723103b8a8 Mon Sep 17 00:00:00 2001 From: Valhero Date: 2022年10月27日 21:55:28 +0300 Subject: [PATCH 2/3] First commit for review. --- src/main/java/BillSplitCalculator.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/BillSplitCalculator.java b/src/main/java/BillSplitCalculator.java index ad49514..97fa059 100644 --- a/src/main/java/BillSplitCalculator.java +++ b/src/main/java/BillSplitCalculator.java @@ -65,8 +65,8 @@ public void showFinalBill() { for (Product product : products) { System.out.println(String.format("%s - %.2f\n-------------------------", product.name, product.price)); } - System.out.println(String.format("\nОбщая стоимость товаров - %.2f %s ", totalCost, getCorrectRubleEnding(totalCost))); - System.out.println(String.format("\nКаждый должен заплатить %.2f %s", totalCost / numberOfPersons, getCorrectRubleEnding(totalCost / numberOfPersons))); + System.out.println(String.format("Общая стоимость товаров - %.2f %s ", totalCost, getCorrectRubleEnding(totalCost))); + System.out.println(String.format("Каждый должен заплатить %.2f %s", totalCost / numberOfPersons, getCorrectRubleEnding(totalCost / numberOfPersons))); } public String getCorrectRubleEnding(double money) { From 6e422299a695f303994189c7b613fb6791f70803 Mon Sep 17 00:00:00 2001 From: Valhero Date: 2022年10月27日 21:57:23 +0300 Subject: [PATCH 3/3] First project for review. --- src/main/java/BillSplitCalculator.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/BillSplitCalculator.java b/src/main/java/BillSplitCalculator.java index 97fa059..11db76d 100644 --- a/src/main/java/BillSplitCalculator.java +++ b/src/main/java/BillSplitCalculator.java @@ -70,7 +70,7 @@ public void showFinalBill() { } public String getCorrectRubleEnding(double money) { - int moneyAsInt = (int) money; + int moneyAsInt = (int)money; if (moneyAsInt % 100>= 11 && moneyAsInt % 100 <= 14) { return "рублей";

AltStyle によって変換されたページ (->オリジナル) /