From 11f0fb3cfdc3a7cecbcf13165add982175f87b00 Mon Sep 17 00:00:00 2001 From: Olga Date: 2023年8月20日 15:28:22 +0700 Subject: [PATCH 1/2] =?UTF-8?q?=C2=AB=D0=9A=D0=B0=D0=BB=D1=8C=D0=BA=D1=83?= =?UTF-8?q?=D0=BB=D1=8F=D1=82=D0=BE=D1=80=20=D1=81=D1=87=D1=91=D1=82=D0=B0?= =?UTF-8?q?=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/Main.java | 93 +++++++++++++++++++++++++++++++++++++- src/main/java/Product.java | 11 +++++ 2 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 src/main/java/Product.java diff --git a/src/main/java/Main.java b/src/main/java/Main.java index db9356a08..06ed43c68 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,6 +1,97 @@ +import java.util.ArrayList; +import java.util.Scanner; public class Main { public static void main(String[] args) { - System.out.println("Hello world!"); + int count = 0; + Scanner scanner = new Scanner(System.in); + do { + System.out.println("На сколько человек необходимо разделить счёт?"); + String countStr = scanner.next(); + + if (!countStr.matches("[1-9]+")) { + System.out.println("Не корректный ввод"); + } else { + count = Integer.parseInt(countStr); + + if (count == 1) { + System.out.println("Подсчет для одного человека"); + addProductList(1); + + } else if (count < 1) { + System.out.println("Некорректное значение для подсчёта"); + + } else { + System.out.println("Подсчет для " + count + " человек"); + addProductList(count); + } + } + } + + while (count < 2); + } + + private static void addProductList(int count) { + + + ArrayList productList = new ArrayList(); + Scanner scanner = new Scanner(System.in); + String nameFormat; + boolean active = true; + + while (active) { + System.out.println("Введите название товара"); + String name = scanner.next(); + nameFormat = name.trim().toUpperCase().toLowerCase(); + if (nameFormat.contains("завершить")) + break; + if (!nameFormat.chars().allMatch(Character::isLetter)) { + System.out.println("Не корректное название товара"); + } else { + do { + System.out.println("Введите цену товара в формате рубли.копейки"); + String str = scanner.next(); + if (str.trim().toUpperCase().toLowerCase().contains("завершить")) { + active = false; + break; + } + if (!str.matches("[0-9]+\\.[0-9]+")) { + System.out.println("Не корректная цена товара"); + } else { + double price = Double.parseDouble(str); + + Product product = new Product(nameFormat, price); + + productList.add(product); + System.out.println("Товар " + product.name + " с ценой " + product.price + " успешно добавлен в корзину"); + System.out.println("Хотите добавить еще?"); + break; + } + } while (true); + } + } + System.out.println("==========================="); + sum(count, productList); + } + + private static void sum(int count, ArrayList productList) { + double sum = productList.stream().mapToDouble(product1 -> product1.price).sum(); + System.out.println("Список продуктов: "); + productList.forEach((s) -> System.out.println(s.name + " : " + s.price)); + System.out.println("Итоговая сумма: " + parseRubCase(sum)); + System.out.println("Итоговая сумма на каждого человека: " + parseRubCase(sum / count)); + System.out.println("==========================="); + } + + public static String parseRubCase(double price) { + int inInt = (int) price; + String cased; + if (inInt % 100 / 10 == 1 || inInt % 10>= 5 || inInt % 10 == 0) + cased = "рублей"; + else if (inInt % 10 == 1) + cased = "рубль"; + else + cased = "рубля"; + return String.format("%.2f", price) + " " + cased; } } \ No newline at end of file diff --git a/src/main/java/Product.java b/src/main/java/Product.java new file mode 100644 index 000000000..9bd453025 --- /dev/null +++ b/src/main/java/Product.java @@ -0,0 +1,11 @@ +public class Product { + String name; + double price; + + + public Product(String name, double price) { + this.name = name; + this.price = price; + + } +} From 1205bfbba871ad6a38a360672217d67cc93e470c Mon Sep 17 00:00:00 2001 From: Olga Date: 2023年8月20日 15:43:17 +0700 Subject: [PATCH 2/2] =?UTF-8?q?=D0=BE=D0=BF=D0=B8=D1=81=D0=B0=D0=BD=D0=B8?= =?UTF-8?q?=D0=B5=20=D0=BF=D1=80=D0=BE=D0=B5=D0=BA=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 63be1bfe0..abf009abe 100644 --- a/README.md +++ b/README.md @@ -1 +1 @@ -# Пустой репозиторий для работы с Java кодом в Android Studio +# «Калькулятор счёта» - проектная работа 1

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