From 731e15437230742adffe9163f133b9fd8b059f1d Mon Sep 17 00:00:00 2001 From: amdoit-git Date: Tue, 28 Nov 2023 20:38:03 +0700 Subject: [PATCH 1/3] =?UTF-8?q?=D0=9F=D1=80=D0=BE=D0=BC=D0=B5=D0=B6=D1=83?= =?UTF-8?q?=D1=82=D0=BE=D1=87=D0=BD=D1=8B=D0=B5=20=D0=B8=D1=82=D0=BE=D0=B3?= =?UTF-8?q?=D0=B8=20=D1=81=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD=D0=B8=D1=8F=20?= =?UTF-8?q?=D0=BA=D0=B0=D0=BB=D1=8C=D0=BA=D1=83=D0=BB=D1=8F=D1=82=D0=BE?= =?UTF-8?q?=D1=80=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/AddFormat.java | 47 +++++++++++++++++++++++++++++ src/main/java/Main.java | 53 ++++++++++++++++++++++++++++++++- src/main/java/MyCalc.java | 31 +++++++++++++++++++ src/main/java/ProductItem.java | 54 ++++++++++++++++++++++++++++++++++ 4 files changed, 184 insertions(+), 1 deletion(-) create mode 100644 src/main/java/AddFormat.java create mode 100644 src/main/java/MyCalc.java create mode 100644 src/main/java/ProductItem.java diff --git a/src/main/java/AddFormat.java b/src/main/java/AddFormat.java new file mode 100644 index 000000000..1195f7b6d --- /dev/null +++ b/src/main/java/AddFormat.java @@ -0,0 +1,47 @@ +import java.util.Formatter; +import java.util.Locale; +public class AddFormat { + public static String format(int total){ + + int count = total%100; + + String result; + + if (count >= 5 && count <= 20) { + result = "товаров"; + } else { + count = count % 10; + if (count == 1) { + result = "товар"; + } else if (count >= 2 && count <= 4) { + result = "товара"; + } else { + result = "товаров"; + } + } + + return String.format("%d %s", total, result); + } + + public static String format(double price){ + + int count = ((int)Math.floor(price))%100; + + String result; + + if (count >= 5 && count <= 20) { + result = "рублей"; + } else { + count = count % 10; + if (count == 1) { + result = "рубль"; + } else if (count >= 2 && count <= 4) { + result = "рубля"; + } else { + result = "рублей"; + } + } + + return String.format("%.2f %s", price, result); + } +} diff --git a/src/main/java/Main.java b/src/main/java/Main.java index db9356a08..09fef64ed 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,6 +1,57 @@ +import java.util.Locale; +import java.util.Scanner; + public class Main { public static void main(String[] args) { - System.out.println("Hello world!"); + + Locale.setDefault(Locale.US); + + Scanner scanner = new Scanner(System.in); + + System.out.println("На скольких человек необходимо разделить счёт?"); + + int persons; + + while (true){ + try{ + persons = scanner.nextInt(); + + if(persons>1){ + break; + } + + System.out.println("Число человек должно быть больше 1\nВведите корректное значение:"); + + }catch (Exception e) { + //System.out.println(e); + //scanner = new Scanner(System.in); + if(scanner.hasNextLine()) scanner.nextLine(); + System.out.println("Введите число, а не строку:"); + } + } + + System.out.println("Вы ввели "+persons+" человек"); + + MyCalc calc = new MyCalc(persons); + + System.out.println("Теперь давайте добавим товары в счет"); + + if(scanner.hasNextLine()) scanner.nextLine(); + + while (true){ + + calc.addProduct(); + + System.out.println("Хотите добавить ещё один товар? [да/завершить]"); + + String str = scanner.nextLine(); + + if(str.trim().equalsIgnoreCase("завершить")){ + break; + } + } + + calc.calculateAndPrint(); } } \ No newline at end of file diff --git a/src/main/java/MyCalc.java b/src/main/java/MyCalc.java new file mode 100644 index 000000000..2466867cc --- /dev/null +++ b/src/main/java/MyCalc.java @@ -0,0 +1,31 @@ +public class MyCalc { + private int persons; + public String products = ""; + public double total_price = 0; + + private int total = 0; + MyCalc(int persons){ + this.persons = persons; + } + + public void addProduct(){ + + ProductItem item = new ProductItem(); + + item.askProductData(); + + System.out.println("Добавлен продукт: " + item.name + " стоимостью "+item.getPrice()); + + products += item.getNameAndPrice() + "\n"; + + this.total_price += item.price; + this.total++; + } + + public void calculateAndPrint(){ + System.out.println("Заказанные товары:"); + System.out.println(products); + System.out.println("Итого "+AddFormat.format(total)+" на сумму "+AddFormat.format(total_price)); + System.out.println("Каждый человек должен заплатить по "+ total_price/persons); + } +} diff --git a/src/main/java/ProductItem.java b/src/main/java/ProductItem.java new file mode 100644 index 000000000..5235cf611 --- /dev/null +++ b/src/main/java/ProductItem.java @@ -0,0 +1,54 @@ +import java.util.Scanner; + +public class ProductItem { + + public String name; + public double price; + + public void askProductData(){ + + Scanner scanner = new Scanner(System.in); + + while (true){ + System.out.println("Введите название товара:"); + + name = scanner.nextLine(); + + if(!name.trim().equals("")){ + break; + } + + System.out.println("Ошибка! Название не должно быть пустой строкой"); + } + + while(true) { + + System.out.println("Введите стоимость товара:"); + + try { + + price = scanner.nextDouble(); + + if (price > 0) { + break; + } + + } catch (Exception e) { + + if(scanner.hasNextLine()) scanner.nextLine(); + //scanner = new Scanner(System.in); + } + + System.out.println("Введите число в формате 0.00"); + } + } + + + public String getNameAndPrice(){ + return name + " " + AddFormat.format(price); + } + + public String getPrice(){ + return AddFormat.format(price); + } +} From 457f4780867ed0a78b7caffff203f57e1e220d5b Mon Sep 17 00:00:00 2001 From: amdoit-git Date: Wed, 29 Nov 2023 20:20:50 +0700 Subject: [PATCH 2/3] =?UTF-8?q?=D0=93=D0=BE=D1=82=D0=BE=D0=B2=D0=B0=D1=8F?= =?UTF-8?q?=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=2029=20=D0=BD=D0=BE?= =?UTF-8?q?=D1=8F=D0=B1=D1=80=D1=8F=202023=20=D0=B3=D0=BE=D0=B4=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/AddFormat.java | 41 +++++++++++++++------------------- src/main/java/Main.java | 12 ++++++---- src/main/java/MyCalc.java | 14 ++++++++---- src/main/java/ProductItem.java | 9 ++++++-- 4 files changed, 43 insertions(+), 33 deletions(-) diff --git a/src/main/java/AddFormat.java b/src/main/java/AddFormat.java index 1195f7b6d..4ba3a2a9a 100644 --- a/src/main/java/AddFormat.java +++ b/src/main/java/AddFormat.java @@ -1,47 +1,42 @@ import java.util.Formatter; import java.util.Locale; public class AddFormat { - public static String format(int total){ + public static String format(int num, String s1, String s2, String s3, boolean print_num){ - int count = total%100; + int count = num%100; String result; if (count >= 5 && count <= 20) { - result = "товаров"; + result = s3; } else { count = count % 10; if (count == 1) { - result = "товар"; + result = s1; } else if (count >= 2 && count <= 4) { - result = "товара"; + result = s2; } else { - result = "товаров"; + result = s3; } } - return String.format("%d %s", total, result); - } + if(print_num){ + result = String.format("%d %s", num, result); + } - public static String format(double price){ + return result; + } - int count = ((int)Math.floor(price))%100; + public static String rub(double price){ - String result; + int num = (int)Math.floor(price); - if (count >= 5 && count <= 20) { - result = "рублей"; - } else { - count = count % 10; - if (count == 1) { - result = "рубль"; - } else if (count >= 2 && count <= 4) { - result = "рубля"; - } else { - result = "рублей"; - } - } + String result = format(num, "рубль", "рубля", "рублей", false); return String.format("%.2f %s", price, result); } + + public static String rub(float price){ + return rub((double)price); + } } diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 09fef64ed..d8cec64d3 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -17,11 +17,17 @@ public static void main(String[] args) { try{ persons = scanner.nextInt(); + if(!scanner.nextLine().trim().equals("")){ + //100 раз! Уже 100 раз я повторял, что хочу поделить счет на 5 человек + System.out.println("Введите просто число без дополнительных знаков:"); + continue; + } + if(persons>1){ break; } - System.out.println("Число человек должно быть больше 1\nВведите корректное значение:"); + System.out.println("Введите число человек больше 1:"); }catch (Exception e) { //System.out.println(e); @@ -31,14 +37,12 @@ public static void main(String[] args) { } } - System.out.println("Вы ввели "+persons+" человек"); + System.out.println("Будем делить счет на "+AddFormat.format(persons, "человека", "человека","человек", true)); MyCalc calc = new MyCalc(persons); System.out.println("Теперь давайте добавим товары в счет"); - if(scanner.hasNextLine()) scanner.nextLine(); - while (true){ calc.addProduct(); diff --git a/src/main/java/MyCalc.java b/src/main/java/MyCalc.java index 2466867cc..ec6af0bee 100644 --- a/src/main/java/MyCalc.java +++ b/src/main/java/MyCalc.java @@ -14,7 +14,7 @@ public void addProduct(){ item.askProductData(); - System.out.println("Добавлен продукт: " + item.name + " стоимостью "+item.getPrice()); + System.out.println("Добавлен товар " + item.name + " стоимостью "+item.getPrice()); products += item.getNameAndPrice() + "\n"; @@ -23,9 +23,15 @@ public void addProduct(){ } public void calculateAndPrint(){ + System.out.println("\n"); System.out.println("Заказанные товары:"); - System.out.println(products); - System.out.println("Итого "+AddFormat.format(total)+" на сумму "+AddFormat.format(total_price)); - System.out.println("Каждый человек должен заплатить по "+ total_price/persons); + System.out.println(products.trim()); + + //продавец и копейки не простит, так что если поровну не поделится будут чаевые + double price_per_person = Math.ceil(100*total_price/persons)/100; + + System.out.println("Итого "+AddFormat.format(total, "товар", "товара","товаров", true)+" на сумму "+AddFormat.rub(total_price)); + System.out.println("Делим счет на "+ AddFormat.format(persons, "человека", "человек","человек", true)); + System.out.println("Каждый должен заплатить по "+ AddFormat.rub(price_per_person)); } } diff --git a/src/main/java/ProductItem.java b/src/main/java/ProductItem.java index 5235cf611..331778065 100644 --- a/src/main/java/ProductItem.java +++ b/src/main/java/ProductItem.java @@ -29,6 +29,11 @@ public void askProductData(){ price = scanner.nextDouble(); + if(!scanner.nextLine().trim().equals("")){ + System.out.println("Введите только число в формате 0.00 без дополнительных знаков"); + continue; + } + if (price > 0) { break; } @@ -45,10 +50,10 @@ public void askProductData(){ public String getNameAndPrice(){ - return name + " " + AddFormat.format(price); + return name + " " + AddFormat.rub(price); } public String getPrice(){ - return AddFormat.format(price); + return AddFormat.rub(price); } } From f9fa30968c7eb1c14e27470a1bf53de2638a3d3d Mon Sep 17 00:00:00 2001 From: amdoit-git Date: Thu, 30 Nov 2023 02:23:09 +0700 Subject: [PATCH 3/3] =?UTF-8?q?=D0=A3=D0=B1=D1=80=D0=B0=D0=BD=20=D0=B8?= =?UTF-8?q?=D0=BC=D0=BF=D0=BE=D1=80=D1=82=20=D0=BD=D0=B5=D0=B8=D1=81=D0=BF?= =?UTF-8?q?=D0=BE=D0=BB=D1=8C=D0=B7=D1=83=D0=B5=D0=BC=D1=8B=D1=85=20=D0=BA?= =?UTF-8?q?=D0=BB=D0=B0=D1=81=D1=81=D0=BE=D0=B2=20=D0=B8=D0=B7=20=D1=84?= =?UTF-8?q?=D0=B0=D0=B9=D0=BB=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/AddFormat.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/main/java/AddFormat.java b/src/main/java/AddFormat.java index 4ba3a2a9a..319720bdf 100644 --- a/src/main/java/AddFormat.java +++ b/src/main/java/AddFormat.java @@ -1,5 +1,3 @@ -import java.util.Formatter; -import java.util.Locale; public class AddFormat { public static String format(int num, String s1, String s2, String s3, boolean print_num){