From e1f6893023e1e00e765909b127129a7ab6ece3d6 Mon Sep 17 00:00:00 2001 From: s-buvaka Date: 2022年7月27日 23:03:27 +0300 Subject: [PATCH 1/5] Remove classes --- src/main/java/Calculator.java | 22 ---------------- src/main/java/Formatter.java | 17 ------------- src/main/java/Item.java | 10 -------- src/main/java/Main.java | 47 +++-------------------------------- 4 files changed, 3 insertions(+), 93 deletions(-) delete mode 100644 src/main/java/Calculator.java delete mode 100644 src/main/java/Formatter.java delete mode 100644 src/main/java/Item.java diff --git a/src/main/java/Calculator.java b/src/main/java/Calculator.java deleted file mode 100644 index 2fbc00d..0000000 --- a/src/main/java/Calculator.java +++ /dev/null @@ -1,22 +0,0 @@ -class Calculator { - - int friendsCount; - - String cart = "Добавленные товары:"; - double totalPrice = 0; - - Calculator(int friendsCount) { - this.friendsCount = friendsCount; - } - - void addItem(Item item) { - totalPrice += item.price; - cart = cart + "\n" + item.name; - - System.out.println(item.name + " в корзине"); - } - - double divideSum() { - return totalPrice / friendsCount; - } -} diff --git a/src/main/java/Formatter.java b/src/main/java/Formatter.java deleted file mode 100644 index 3f915b7..0000000 --- a/src/main/java/Formatter.java +++ /dev/null @@ -1,17 +0,0 @@ -public class Formatter { - - String formatValue(double price) { - double roundedValue = Math.floor(price); - if (roundedValue == 1) { - return "рубль"; - } else if (roundedValue>= 2 && roundedValue <= 4) { - return "рубля"; - } else { - return "рублей"; - } - } - - String roundResult(final double result) { - return String.format("%.2f", result); - } -} diff --git a/src/main/java/Item.java b/src/main/java/Item.java deleted file mode 100644 index fad8a4e..0000000 --- a/src/main/java/Item.java +++ /dev/null @@ -1,10 +0,0 @@ -class Item { - - String name; - double price; - - Item(String name, double price) { - this.name = name; - this.price = price; - } -} diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 11ba5d3..a9198c4 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,49 +1,8 @@ -import java.util.Scanner; - public class Main { public static void main(String[] args) { - Scanner scanner = new Scanner(System.in); - - int friendCount; - while (true) { - System.out.println("На сколько человек необходимо разделить счет?"); - friendCount = scanner.nextInt(); - - if (friendCount> 1) { - break; - } else if (friendCount == 1) { - System.out.println( - "Нет смысла делить сумму на одного человека. Давайте попробуем ввести другое значение, которое будет больше единицы."); - } else { - System.out.println("Неверное количество друзей. Значение должно быть болье единицы, давайте попробуем еще раз."); - } - } - - Calculator calculator = new Calculator(friendCount); - - while (true) { - System.out.println("Введите название товара"); - String name = scanner.next(); - - System.out.println("Введите стоимость товара в формате: 'рубли.копейки' [10.45, 11.40]"); - double price = scanner.nextDouble(); - - calculator.addItem(new Item(name, price)); - - System.out.println( - "Хотите добавить еще один товар? Введите любой символ для продолжения, либо 'Завершить' если больше нет товаров для добавления"); - String answer = scanner.next(); - - if (answer.equalsIgnoreCase("Завершить")) { - break; - } - } - - double result = calculator.divideSum(); - Formatter formatter = new Formatter(); - - System.out.println(calculator.cart); - System.out.println("Каждому человеку к оплате: " + formatter.roundResult(result) + " " + formatter.formatValue(result)); + // ваш код начнется здесь + // вы не должны ограничиваться только классом Main и можете создавать свои классы по необходимости + System.out.println("Привет Мир"); } } From c521234c876be18b465b2fd45ef8a71d814fb7a9 Mon Sep 17 00:00:00 2001 From: s-buvaka Date: 2022年7月27日 23:13:52 +0300 Subject: [PATCH 2/5] Change project name --- settings.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/settings.gradle b/settings.gradle index 716abf2..be110bf 100644 --- a/settings.gradle +++ b/settings.gradle @@ -12,4 +12,4 @@ dependencyResolutionManagement { mavenCentral() } } -rootProject.name = "BillCalculator" +rootProject.name = "Java-Module-Project" From e68dbb83e1dde4192b2fbf1072f792f5b1cf35b9 Mon Sep 17 00:00:00 2001 From: Vasil9202 Date: 2022年11月22日 19:35:25 +0300 Subject: [PATCH 3/5] =?UTF-8?q?=D0=9F=D1=80=D0=BE=D0=B5=D0=BA=D1=82=D0=BD?= =?UTF-8?q?=D0=B0=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=20=E2=84=96?= =?UTF-8?q?1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/Calculator.java | 117 ++++++++++++++++++++++++++++++++++ src/main/java/Main.java | 5 +- src/main/java/Product.java | 18 ++++++ 3 files changed, 138 insertions(+), 2 deletions(-) create mode 100644 src/main/java/Calculator.java create mode 100644 src/main/java/Product.java diff --git a/src/main/java/Calculator.java b/src/main/java/Calculator.java new file mode 100644 index 0000000..6b07617 --- /dev/null +++ b/src/main/java/Calculator.java @@ -0,0 +1,117 @@ +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; + +public class Calculator { + + private int humanCount; + private static final Scanner scanner = new Scanner(System.in); + private List productsList; + private static double priceAllProducts = 0; + + public Calculator(){ + humanCount = divideBuild(); + this.productsList = addProducts(humanCount); + + } + + public static int divideBuild(){ + while (true){ + try{ + System.out.println("На скольких человек необходимо разделить счёт?"); + int count = Integer.parseInt(scanner.nextLine()); + if(count < 1){ + System.out.println("Ошибка. Введено некорректное значение для подсчёта. Попробуйте снова"); + } + else if(count == 1){ + System.out.println("Нет смысла ничего считать и делить. Попробуйте снова"); + } + else { + return count; + }}catch (Exception e){ + System.out.println("Ошибка. Попробуйте снова."); + } + } + } + + public static List addProducts(int humanCount){ + List list = new ArrayList(); + System.out.println("Введите название товара и его стоимость в формате:\n'рубли.копейки' [10.45, 11.40]"); + while(true){ + String products = scanner.nextLine(); + if(products.equalsIgnoreCase("Завершить")){ + if(list.size() == 0){ + System.out.println("Товар не был введен, программа завершена."); + } + else { + System.out.println("Добавленные товары:"); + for(Product product : list){ + System.out.println(product.getName()); + } + double pay = priceAllProducts / humanCount; + //double pay2 = Math.floor(pay); + String payformat = String.format("%.2f", pay); + String rub = floor(pay); + System.out.println("Каждый человек из " + humanCount +" должен заплатить " + payformat + " " + rub); + } + break;} + else if(products.length() < 1){ + continue;} + + String[] names = products.substring(products.indexOf("'") + 1,products.indexOf("' ")).split("\\."); + String[] prices = products.substring(products.indexOf("[")+1,products.indexOf("]")).split(","); + if(names.length != prices.length) { + System.out.println("Количество продуктов и стоимости не совпадает."); + continue; + } + for(int i = 0; i < names.length; i++){ + list.add(new Product(names[i].trim(),Double.parseDouble(prices[i].trim()))); + priceAllProducts += Double.parseDouble(prices[i].trim()); + if(i == 0) + System.out.print("Товар успешно добавлен: " + names[i]); + else if(i == names.length-1) + System.out.print(", " + names[i] + "\n"); + else + System.out.print(", " + names[i]); + } + + System.out.println("Хотите добавить ещё товары? Если нет, введите слово \"Завершить\""); + } + return list; + } + + private static String floor(double pay){ + String rub = Integer.toString((int)Math.floor(pay)); + if(rub.length() == 1){ + switch (rub) { + case "0": + case "5": + case "6": + case "7": + case "8": + case "9": + return "рублей"; + case "1": + return "рубль"; + case "2": + case "3": + case "4": + return "рубля"; + } + } + else if(rub.length()> 1){ + int lastDigit = Integer.parseInt(Character.toString(rub.charAt(rub.length()-1))); + if(rub.length() == 2 && rub.charAt(0) == '1'){return "рублей!!";} + else if(lastDigit>= 5 && lastDigit <= 9 ) + return "рублей"; + else if(lastDigit>= 2 && lastDigit <= 4 ) + return "рубля"; + else if(lastDigit == 1) + return "рубль"; + else + return "рублей"; + } + return "Ошибка"; + } +} diff --git a/src/main/java/Main.java b/src/main/java/Main.java index a9198c4..6311bec 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) { + Calculator calculator = new Calculator(); // ваш код начнется здесь - // вы не должны ограничиваться только классом Main и можете создавать свои классы по необходимости - System.out.println("Привет Мир"); + // вы не должны ограничиваться только классом Main и можете создавать свои классы по необходимости + } } diff --git a/src/main/java/Product.java b/src/main/java/Product.java new file mode 100644 index 0000000..be26005 --- /dev/null +++ b/src/main/java/Product.java @@ -0,0 +1,18 @@ +public class Product{ + private String name; + private double price; + + public String getName() { + return name; + } + + public double getPrice() { + return price; + } + + public Product(String name, double price){ + this.name = name; + this.price = price; + } + +} \ No newline at end of file From 909169959f1a640c133f2c2cf5b472b85322685c Mon Sep 17 00:00:00 2001 From: Vasil9202 Date: 2022年11月22日 19:46:03 +0300 Subject: [PATCH 4/5] =?UTF-8?q?=D0=9F=D1=80=D0=BE=D0=B5=D0=BA=D1=82=D0=BD?= =?UTF-8?q?=D0=B0=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=20=E2=84=96?= =?UTF-8?q?1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/.gitignore | 3 +++ .idea/compiler.xml | 6 ++++++ .idea/gradle.xml | 18 ++++++++++++++++++ .idea/misc.xml | 10 ++++++++++ .idea/vcs.xml | 6 ++++++ 5 files changed, 43 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/compiler.xml create mode 100644 .idea/gradle.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..61a9130 --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/gradle.xml b/.idea/gradle.xml new file mode 100644 index 0000000..6cec569 --- /dev/null +++ b/.idea/gradle.xml @@ -0,0 +1,18 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..a47d29e --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,10 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + +
    + + \ No newline at end of file From 0232c952bb2a2e4306471714204ead1608b88c2a Mon Sep 17 00:00:00 2001 From: Vasil9202 Date: 2022年11月24日 22:42:58 +0300 Subject: [PATCH 5/5] =?UTF-8?q?=D0=9F=D1=80=D0=BE=D0=B5=D0=BA=D1=82=D0=BD?= =?UTF-8?q?=D0=B0=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=20=E2=84=96?= =?UTF-8?q?1=20=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5=20=E2=84=961?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/Calculator.java | 66 ++++++++++++++--------------------- 1 file changed, 27 insertions(+), 39 deletions(-) diff --git a/src/main/java/Calculator.java b/src/main/java/Calculator.java index 6b07617..1b504fe 100644 --- a/src/main/java/Calculator.java +++ b/src/main/java/Calculator.java @@ -37,46 +37,34 @@ else if(count == 1){ public static List addProducts(int humanCount){ List list = new ArrayList(); - System.out.println("Введите название товара и его стоимость в формате:\n'рубли.копейки' [10.45, 11.40]"); while(true){ - String products = scanner.nextLine(); - if(products.equalsIgnoreCase("Завершить")){ - if(list.size() == 0){ - System.out.println("Товар не был введен, программа завершена."); - } - else { - System.out.println("Добавленные товары:"); - for(Product product : list){ - System.out.println(product.getName()); - } - double pay = priceAllProducts / humanCount; - //double pay2 = Math.floor(pay); - String payformat = String.format("%.2f", pay); - String rub = floor(pay); - System.out.println("Каждый человек из " + humanCount +" должен заплатить " + payformat + " " + rub); - } - break;} - else if(products.length() < 1){ - continue;} - - String[] names = products.substring(products.indexOf("'") + 1,products.indexOf("' ")).split("\\."); - String[] prices = products.substring(products.indexOf("[")+1,products.indexOf("]")).split(","); - if(names.length != prices.length) { - System.out.println("Количество продуктов и стоимости не совпадает."); - continue; - } - for(int i = 0; i < names.length; i++){ - list.add(new Product(names[i].trim(),Double.parseDouble(prices[i].trim()))); - priceAllProducts += Double.parseDouble(prices[i].trim()); - if(i == 0) - System.out.print("Товар успешно добавлен: " + names[i]); - else if(i == names.length-1) - System.out.print(", " + names[i] + "\n"); - else - System.out.print(", " + names[i]); - } - - System.out.println("Хотите добавить ещё товары? Если нет, введите слово \"Завершить\""); + System.out.println("Введите название одного товара или слово \"Завершить\" для подсчета стоимости"); + String product = scanner.nextLine(); + if(product.equalsIgnoreCase("Завершить")){ + if(list.size() == 0){ + System.out.println("Товар не был введен, программа завершена."); + } + else { + System.out.println("Добавленные товары:"); + for(Product products : list){ + System.out.println(products.getName()); + } + double pay = priceAllProducts / humanCount; + //double pay2 = Math.floor(pay); + String payformat = String.format("%.2f", pay); + String rub = floor(pay); + System.out.println("Каждый человек из " + humanCount +" должен заплатить " + payformat + " " + rub); + } + break;} + System.out.println("Введите стоимость товара в формате: 'рубли.копейки'"); + try{ + String price0 = scanner.nextLine(); + Double price = Double.parseDouble(price0); + priceAllProducts += price; + list.add(new Product(product,price));} + catch (NumberFormatException e){ + System.out.println("Задан неверный формат стоимости, начните заново"); + } } return list; }

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