From d40889a58a47b8e645a9f1b795130a813571d870 Mon Sep 17 00:00:00 2001 From: volkovdimaya Date: Thu, 8 Feb 2024 18:08:56 +0500 Subject: [PATCH 1/5] =?UTF-8?q?=D0=BA=D0=BE=D0=BD=D1=81=D0=BE=D0=BB=D1=8C?= =?UTF-8?q?=D0=BD=D0=BE=D0=B5=20=D0=BF=D1=80=D0=B8=D0=BB=D0=BE=D0=B6=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D0=BA=D0=B0=D0=BB=D1=8C=D0=BA=D1=83=D0=BB?= =?UTF-8?q?=D1=8F=D1=82=D0=BE=D1=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/Calculator.java | 83 +++++++++++++++++++++++++++++++++ src/main/java/FormatterRub.java | 20 ++++++++ src/main/java/Main.java | 48 ++++++++++++++++++- src/main/java/Product.java | 10 ++++ 4 files changed, 159 insertions(+), 2 deletions(-) create mode 100644 src/main/java/Calculator.java create mode 100644 src/main/java/FormatterRub.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 000000000..c05132f6b --- /dev/null +++ b/src/main/java/Calculator.java @@ -0,0 +1,83 @@ +import java.util.ArrayList; +import java.util.Scanner; + +public class Calculator { + double sum; + Scanner scaner = new Scanner(System.in); + ArrayList products = new ArrayList<>(); + + + private String IputName() + { + System.out.println("Введите название товара"); + String name = scaner.next(); + return name; + } + private double InputCost() + { + while (true) { + System.out.println("Введите стоимость"); + try { + double cost = scaner.nextDouble(); + if (cost <= 0) + { + System.out.println("Стоимость не может быть меньше 0"); + scaner.nextLine(); + continue; + } + return cost; + + + } catch (Exception e) { + System.out.println("Ошибка, попробуйте еще раз"); + scaner.nextLine(); + } + } + } + + public void AddProduct() + { + while (true) + { + System.out.println("Добавление товара в калькулятор"); + + Product product = new Product(IputName(), InputCost()); + products.add(product); + sum += product.cost; + + System.out.println("Товар " + product.name + " успешно добавлен!"); + System.out.println("Продолжить добовление товара?"); + System.out.println("Ввести команду \"Завершить\" для того, чтоб завершить процесс добавления товаров"); + + String comand = scaner.next(); + if(comand.equalsIgnoreCase("Завершить")) + { + break; + } + } + + } + + public void ShowProducts() + { + FormatterRub f = new FormatterRub();//для форматирования окончания рубля + System.out.println("Добавленные товары:"); + for (Product product : products) + { + System.out.println("Товар: " + product.name + " цена: " + product.cost + " " + f.correctEnding(product.cost)); + } + } + + public void ShowResult(int countHuman) + { + FormatterRub f = new FormatterRub(); + System.out.println("Всего товаров на сумму: " + sum + " " + f.correctEnding(sum)); + double result = sum/countHuman; + + + String text = "Каждый должен заплатить: %.2f %s"; + + System.out.println(String.format(text, result, f.correctEnding(result))); + } + +} diff --git a/src/main/java/FormatterRub.java b/src/main/java/FormatterRub.java new file mode 100644 index 000000000..58b6f0b0a --- /dev/null +++ b/src/main/java/FormatterRub.java @@ -0,0 +1,20 @@ +public class FormatterRub { + + public String correctEnding(double value) + { + double d = Math.floor(value); + if(d == 1) + { + return "рубль"; + } + else if(d > 1 && d < 5) + { + return "рубля"; + } + else + { + return "рублей"; + } + + } +} diff --git a/src/main/java/Main.java b/src/main/java/Main.java index db9356a08..7f4ebbc32 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,6 +1,50 @@ +import java.util.Scanner; + public class Main { public static void main(String[] args) { - System.out.println("Hello world!"); + + System.out.println("Добрый день"); + Scanner scaner = new Scanner(System.in); + int input=0; + + while (true) + { + System.out.println("На сколько человек необходимо разделить счёт?"); + + try { + input = scaner.nextInt(); + + if(input == 1) + { + System.out.println("Нет смысла ничего считать и делить"); + } + else if(input < 1) + { + System.out.println("Это некорректное значение для подсчёта"); + } + else if(input > 1) + { + Calculator cal = new Calculator(); + cal.AddProduct(); + cal.ShowProducts(); + cal.ShowResult(input); + break; + } + } + catch (Exception e) + { + System.out.println("Ошибка, попробуйте еще раз"); + scaner.nextLine(); + } + + + } + } -} \ 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..cdb83a4ce --- /dev/null +++ b/src/main/java/Product.java @@ -0,0 +1,10 @@ +public class Product { + String name; + double cost; + + Product(String name , double cost) + { + this.name = name; + this.cost = cost; + } +} From e3d3f4cb246bad4ae4e55a554153816866f5f30d Mon Sep 17 00:00:00 2001 From: volkovdimaya Date: Mon, 12 Feb 2024 23:51:39 +0500 Subject: [PATCH 2/5] =?UTF-8?q?=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=BE=20=D0=BE=D0=BA=D0=BE=D0=BD=D1=87=D0=B0=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5=20=D1=81=20=D1=80=D1=83=D0=B1=D0=BB=D0=B5=D0=BC=20?= =?UTF-8?q?=D0=B8=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD=D1=8B=20=D1=80=D0=B5?= =?UTF-8?q?=D0=BA=D0=BE=D0=BC=D0=B5=D0=BD=D0=B4=D0=B0=D1=86=D0=B8=D0=B8=20?= =?UTF-8?q?=D1=84=D0=BE=D1=80=D0=BC=D0=B0=D1=82=D0=B8=D1=80=D0=BE=D0=B2?= =?UTF-8?q?=D0=B0=D0=BD=D0=B8=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/FormatterRub.java | 30 +++++++++++++++--------------- src/main/java/Product.java | 3 +-- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/src/main/java/FormatterRub.java b/src/main/java/FormatterRub.java index 58b6f0b0a..ef5c33def 100644 --- a/src/main/java/FormatterRub.java +++ b/src/main/java/FormatterRub.java @@ -1,20 +1,20 @@ public class FormatterRub { - public String correctEnding(double value) - { - double d = Math.floor(value); - if(d == 1) - { - return "рубль"; - } - else if(d > 1 && d < 5) - { - return "рубля"; - } - else - { - return "рублей"; - } + public String correctEnding(double value) { + value = Math.floor(value); + value = value % 100; + if (value > 10 && value < 20) { + return "рублей"; + } else { + value = value % 10; + if (value == 1) { + return "рубль"; + } else if (value > 1 && value < 5) { + return "рубля"; + } else { + return "рублей"; + } + } } } diff --git a/src/main/java/Product.java b/src/main/java/Product.java index cdb83a4ce..c4dbb2df4 100644 --- a/src/main/java/Product.java +++ b/src/main/java/Product.java @@ -2,8 +2,7 @@ public class Product { String name; double cost; - Product(String name , double cost) - { + Product(String name, double cost) { this.name = name; this.cost = cost; } From fa74f295a9e5db3d0fc4915a3c7e3b535ed3f06b Mon Sep 17 00:00:00 2001 From: volkovdimaya Date: Mon, 12 Feb 2024 23:51:59 +0500 Subject: [PATCH 3/5] =?UTF-8?q?=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=BE=20=D0=BE=D0=BA=D0=BE=D0=BD=D1=87=D0=B0=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5=20=D1=81=20=D1=80=D1=83=D0=B1=D0=BB=D0=B5=D0=BC=20?= =?UTF-8?q?=D0=B8=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD=D1=8B=20=D1=80=D0=B5?= =?UTF-8?q?=D0=BA=D0=BE=D0=BC=D0=B5=D0=BD=D0=B4=D0=B0=D1=86=D0=B8=D0=B8=20?= =?UTF-8?q?=D1=84=D0=BE=D1=80=D0=BC=D0=B0=D1=82=D0=B8=D1=80=D0=BE=D0=B2?= =?UTF-8?q?=D0=B0=D0=BD=D0=B8=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/Calculator.java | 30 +++++++++++------------------- src/main/java/Main.java | 24 +++++++----------------- 2 files changed, 18 insertions(+), 36 deletions(-) diff --git a/src/main/java/Calculator.java b/src/main/java/Calculator.java index c05132f6b..30c83a4bf 100644 --- a/src/main/java/Calculator.java +++ b/src/main/java/Calculator.java @@ -7,20 +7,18 @@ public class Calculator { ArrayList products = new ArrayList<>(); - private String IputName() - { + private String IputName() { System.out.println("Введите название товара"); String name = scaner.next(); return name; } - private double InputCost() - { + + private double InputCost() { while (true) { System.out.println("Введите стоимость"); try { double cost = scaner.nextDouble(); - if (cost <= 0) - { + if (cost <= 0) { System.out.println("Стоимость не может быть меньше 0"); scaner.nextLine(); continue; @@ -35,10 +33,8 @@ private double InputCost() } } - public void AddProduct() - { - while (true) - { + public void AddProduct() { + while (true) { System.out.println("Добавление товара в калькулятор"); Product product = new Product(IputName(), InputCost()); @@ -50,29 +46,25 @@ public void AddProduct() System.out.println("Ввести команду \"Завершить\" для того, чтоб завершить процесс добавления товаров"); String comand = scaner.next(); - if(comand.equalsIgnoreCase("Завершить")) - { + if (comand.equalsIgnoreCase("Завершить")) { break; } } } - public void ShowProducts() - { + public void ShowProducts() { FormatterRub f = new FormatterRub();//для форматирования окончания рубля System.out.println("Добавленные товары:"); - for (Product product : products) - { + for (Product product : products) { System.out.println("Товар: " + product.name + " цена: " + product.cost + " " + f.correctEnding(product.cost)); } } - public void ShowResult(int countHuman) - { + public void ShowResult(int countHuman) { FormatterRub f = new FormatterRub(); System.out.println("Всего товаров на сумму: " + sum + " " + f.correctEnding(sum)); - double result = sum/countHuman; + double result = sum / countHuman; String text = "Каждый должен заплатить: %.2f %s"; diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 7f4ebbc32..9b7728393 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -6,34 +6,26 @@ public static void main(String[] args) { System.out.println("Добрый день"); Scanner scaner = new Scanner(System.in); - int input=0; + int input = 0; - while (true) - { + while (true) { System.out.println("На сколько человек необходимо разделить счёт?"); - try { + if (scaner.hasNextInt()) { input = scaner.nextInt(); - if(input == 1) - { + if (input == 1) { System.out.println("Нет смысла ничего считать и делить"); - } - else if(input < 1) - { + } else if (input < 1) { System.out.println("Это некорректное значение для подсчёта"); - } - else if(input > 1) - { + } else if (input > 1) { Calculator cal = new Calculator(); cal.AddProduct(); cal.ShowProducts(); cal.ShowResult(input); break; } - } - catch (Exception e) - { + } else { System.out.println("Ошибка, попробуйте еще раз"); scaner.nextLine(); } @@ -44,7 +36,5 @@ else if(input > 1) } - - } From 6e67f89057348b8bbdefa48ef17068a69c86f9de Mon Sep 17 00:00:00 2001 From: volkovdimaya Date: Tue, 13 Feb 2024 00:37:51 +0500 Subject: [PATCH 4/5] =?UTF-8?q?=D0=BF=D0=B5=D1=80=D0=B5=D0=B4=D0=B5=D0=BB?= =?UTF-8?q?=D0=B0=D0=BD=D0=BE=20=D0=BA=D0=BE=D0=BD=D0=BA=D0=B0=D1=82=D0=B5?= =?UTF-8?q?=D0=BD=D0=B0=D1=86=D0=B8=D1=8F=20=D1=81=D1=82=D1=80=D0=BE=D0=BA?= =?UTF-8?q?=20=D0=B4=D0=BB=D1=8F=20Product=20=D0=BF=D0=B5=D1=80=D0=B5?= =?UTF-8?q?=D0=BE=D0=BF=D1=80=D0=B5=D0=B4=D0=B5=D0=BB=D0=B5=D0=BD=D1=8B=20?= =?UTF-8?q?equals=20=D0=B8=20hashCode?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/Calculator.java | 44 ++++++++++++++--------------------- src/main/java/Main.java | 14 +++-------- src/main/java/Product.java | 15 ++++++++++++ 3 files changed, 36 insertions(+), 37 deletions(-) diff --git a/src/main/java/Calculator.java b/src/main/java/Calculator.java index 30c83a4bf..8ce99f15c 100644 --- a/src/main/java/Calculator.java +++ b/src/main/java/Calculator.java @@ -2,21 +2,19 @@ import java.util.Scanner; public class Calculator { - double sum; - Scanner scaner = new Scanner(System.in); - ArrayList products = new ArrayList<>(); - - - private String IputName() { + private double sum; + private Scanner scaner = new Scanner(System.in); + private ArrayList products = new ArrayList<>(); + private String iputName() { System.out.println("Введите название товара"); String name = scaner.next(); return name; } - private double InputCost() { + private double inputCost() { while (true) { System.out.println("Введите стоимость"); - try { + if (scaner.hasNextDouble()) { double cost = scaner.nextDouble(); if (cost <= 0) { System.out.println("Стоимость не может быть меньше 0"); @@ -24,24 +22,20 @@ private double InputCost() { continue; } return cost; - - - } catch (Exception e) { + } else { System.out.println("Ошибка, попробуйте еще раз"); scaner.nextLine(); } } } - public void AddProduct() { + public void addProduct() { while (true) { System.out.println("Добавление товара в калькулятор"); - - Product product = new Product(IputName(), InputCost()); + Product product = new Product(iputName(), inputCost()); products.add(product); sum += product.cost; - - System.out.println("Товар " + product.name + " успешно добавлен!"); + System.out.println(String.format("Товар %s успешно добавлен!", product.name)); System.out.println("Продолжить добовление товара?"); System.out.println("Ввести команду \"Завершить\" для того, чтоб завершить процесс добавления товаров"); @@ -50,26 +44,24 @@ public void AddProduct() { break; } } - } - public void ShowProducts() { - FormatterRub f = new FormatterRub();//для форматирования окончания рубля + public void showProducts() { + FormatterRub formatterRub = new FormatterRub();//для форматирования окончания рубля System.out.println("Добавленные товары:"); + String message = "Товар: %s цена: %.2f %s"; for (Product product : products) { - System.out.println("Товар: " + product.name + " цена: " + product.cost + " " + f.correctEnding(product.cost)); + System.out.println(String.format(message, product.name, product.cost, formatterRub.correctEnding(product.cost))); } } - public void ShowResult(int countHuman) { - FormatterRub f = new FormatterRub(); - System.out.println("Всего товаров на сумму: " + sum + " " + f.correctEnding(sum)); + public void showResult(int countHuman) { + FormatterRub formatterRub = new FormatterRub(); + System.out.println(String.format("Всего товаров на сумму: %.2f %s", sum, formatterRub.correctEnding(sum))); double result = sum / countHuman; - String text = "Каждый должен заплатить: %.2f %s"; - - System.out.println(String.format(text, result, f.correctEnding(result))); + System.out.println(String.format(text, result, formatterRub.correctEnding(result))); } } diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 9b7728393..886da3a65 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -7,34 +7,26 @@ public static void main(String[] args) { System.out.println("Добрый день"); Scanner scaner = new Scanner(System.in); int input = 0; - while (true) { System.out.println("На сколько человек необходимо разделить счёт?"); - if (scaner.hasNextInt()) { input = scaner.nextInt(); - if (input == 1) { System.out.println("Нет смысла ничего считать и делить"); } else if (input < 1) { System.out.println("Это некорректное значение для подсчёта"); } else if (input > 1) { Calculator cal = new Calculator(); - cal.AddProduct(); - cal.ShowProducts(); - cal.ShowResult(input); + cal.addProduct(); + cal.showProducts(); + cal.showResult(input); break; } } else { System.out.println("Ошибка, попробуйте еще раз"); scaner.nextLine(); } - - } - } - - } diff --git a/src/main/java/Product.java b/src/main/java/Product.java index c4dbb2df4..5757761a8 100644 --- a/src/main/java/Product.java +++ b/src/main/java/Product.java @@ -6,4 +6,19 @@ public class Product { this.name = name; this.cost = cost; } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Product product = (Product) o; + return name.equals(product.name) && cost == product.cost; + } + + @Override + public int hashCode() { + int result = name == null ? 0 : name.hashCode(); + result = result + (int) cost; + return result; + } } From 29f2b99aaf7ac6dcb1750a61036b31a391941d62 Mon Sep 17 00:00:00 2001 From: volkovdimaya Date: Tue, 13 Feb 2024 00:43:50 +0500 Subject: [PATCH 5/5] =?UTF-8?q?=20=D0=BF=D0=B5=D1=80=D0=B5=D0=B4=D0=B5?= =?UTF-8?q?=D0=BB=D0=B0=D0=BB=20=D0=BC=D0=B5=D1=82=D0=BE=D0=B4=D1=8B=20?= =?UTF-8?q?=D0=B2=20Product=20equals=20=D0=B8=20hashCode=20=D1=82=D0=BE?= =?UTF-8?q?=D0=BB=D1=8C=D0=BA=D0=BE=20=D0=BF=D0=BE=20=D0=B8=D0=BC=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/Product.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/Product.java b/src/main/java/Product.java index 5757761a8..4570a548f 100644 --- a/src/main/java/Product.java +++ b/src/main/java/Product.java @@ -12,13 +12,12 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Product product = (Product) o; - return name.equals(product.name) && cost == product.cost; + return name.equals(product.name); } @Override public int hashCode() { int result = name == null ? 0 : name.hashCode(); - result = result + (int) cost; return result; } }