From 2eb1e872ea94cd7f4e0e03f4f8e3d5cd518ce454 Mon Sep 17 00:00:00 2001 From: Gammius Date: Tue, 2 Apr 2024 03:27:09 +0300 Subject: [PATCH 1/2] =?UTF-8?q?=D0=9F=D0=B8=D1=88=D0=B8=D0=BC=20=D0=BF?= =?UTF-8?q?=D0=B5=D1=80=D0=B2=D0=BE=D0=B5=20=D0=BA=D0=BE=D0=BD=D1=81=D0=BE?= =?UTF-8?q?=D0=BB=D1=8C=D0=BD=D0=BE=20=D0=BF=D1=80=D0=B8=D0=BB=D0=BE=D0=B6?= =?UTF-8?q?=D0=B5=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 | 20 +++++++++++++++++ src/main/java/Formatter.java | 15 +++++++++++++ src/main/java/Main.java | 41 +++++++++++++++++++++++++++++++++-- src/main/java/Products.java | 12 ++++++++++ 4 files changed, 86 insertions(+), 2 deletions(-) create mode 100644 src/main/java/Calculator.java create mode 100644 src/main/java/Formatter.java create mode 100644 src/main/java/Products.java diff --git a/src/main/java/Calculator.java b/src/main/java/Calculator.java new file mode 100644 index 000000000..670703114 --- /dev/null +++ b/src/main/java/Calculator.java @@ -0,0 +1,20 @@ +import java.util.ArrayList; +public class Calculator { + public float sum = 0; + public int result2; + ArrayList prices = new ArrayList<>(); + Formatter rub = new Formatter(); + public void addPrice(float price){ + prices.add(price); + } + public void samib() { + for(int i = 0; i < prices.size(); i++) { + sum = sum + prices.get(i); + } + result2 = (int)Math.floor(sum / Main.quantity); + float result = sum / Main.quantity; + rub.formatRub(result2); + String format = "С каждого по %.2f %s"; + System.out.println(String.format(format, result, Formatter.padezh)); + } +} \ No newline at end of file diff --git a/src/main/java/Formatter.java b/src/main/java/Formatter.java new file mode 100644 index 000000000..05bb9b202 --- /dev/null +++ b/src/main/java/Formatter.java @@ -0,0 +1,15 @@ +public class Formatter { + public static String padezh; + public String formatRub(int result2) { + int x = result2 % 10; + int y = result2 % 100; + if(x == 0 || (x >= 5 && x <= 9) || (y>= 11 && y <= 14)){ + padezh = "рублей"; + }else if (x == 1 ) { + padezh = "рубль"; + }else { + padezh = "рубля"; + } + return padezh; + } +} \ No newline at end of file diff --git a/src/main/java/Main.java b/src/main/java/Main.java index db9356a08..547b9304f 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,6 +1,43 @@ - +import java.util.Scanner; public class Main { + static String product; + static float price; + public static int quantity; public static void main(String[] args) { - System.out.println("Hello world!"); + Scanner scanner = new Scanner(System.in); + Products products = new Products(); + Calculator result = new Calculator(); + + while (true) { + System.out.println("На скольких человек необходимо разделить счёт?"); + quantity = scanner.nextInt(); + if (quantity > 1) { + while (true) { + System.out.println("Введите название блюда или команду Завершить"); + product = scanner.next(); + if (product.equalsIgnoreCase("Завершить")) { + break; + } + while (true) { + System.out.println("Введите цену блюда"); + if (scanner.hasNextFloat()) { + float price = scanner.nextFloat(); + result.addPrice(price); + break; + } else { + System.out.println("Введите корректное значение"); + } + } + products.addProduct(product); + System.out.println("Товар успешно добавлен"); + } + break; + } else { + System.out.println("Введите значение больше 2"); + } + } + products.outputProducts(); + result.samib(); + scanner.close(); } } \ No newline at end of file diff --git a/src/main/java/Products.java b/src/main/java/Products.java new file mode 100644 index 000000000..9179fc0ed --- /dev/null +++ b/src/main/java/Products.java @@ -0,0 +1,12 @@ +import java.util.ArrayList; +public class Products { + ArrayList products = new ArrayList<>(); + public void addProduct(String product){ + products.add(product); + } + public void outputProducts() { + for(int i = 0; i < products.size(); i++) { + System.out.println(products.get(i)); + } + } +} \ No newline at end of file From 2225a1f96d5f0b82778db0f1c8d7732dc6389e9a Mon Sep 17 00:00:00 2001 From: Gammius Date: Sun, 7 Apr 2024 05:26:02 +0300 Subject: [PATCH 2/2] =?UTF-8?q?=D0=BF=D0=BE=D0=BF=D1=8B=D1=82=D0=BA=D0=B0?= =?UTF-8?q?=202=20=D0=BA=D1=80=D0=B8=D1=82=D0=B8=D1=87=D0=B5=D1=81=D0=BA?= =?UTF-8?q?=D0=B8=D0=B5=20=D0=B8=D1=81=D0=BF=D1=80=D0=B2=D0=B8=D0=BB=20?= =?UTF-8?q?=D0=BD=D0=B5=20=D0=BA=D1=80=D0=B8=D1=82=D0=B8=D1=87=D0=B5=D1=81?= =?UTF-8?q?=D0=BA=D0=B8=D0=B5=20=D1=87=D0=B0=D1=81=D1=82=D0=B8=D1=87=D0=BD?= =?UTF-8?q?=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/Calculator.java | 8 ++--- src/main/java/Main.java | 65 +++++++++++++++++++++-------------- src/main/java/Products.java | 4 +-- 3 files changed, 46 insertions(+), 31 deletions(-) diff --git a/src/main/java/Calculator.java b/src/main/java/Calculator.java index 670703114..8cf81b08d 100644 --- a/src/main/java/Calculator.java +++ b/src/main/java/Calculator.java @@ -1,16 +1,16 @@ import java.util.ArrayList; public class Calculator { - public float sum = 0; - public int result2; ArrayList prices = new ArrayList<>(); Formatter rub = new Formatter(); public void addPrice(float price){ prices.add(price); } public void samib() { - for(int i = 0; i < prices.size(); i++) { - sum = sum + prices.get(i); + float sum = 0; + for(Float price : prices) { + sum += price; } + int result2; result2 = (int)Math.floor(sum / Main.quantity); float result = sum / Main.quantity; rub.formatRub(result2); diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 547b9304f..3ee565125 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,43 +1,58 @@ import java.util.Scanner; public class Main { - static String product; - static float price; + public static int quantity; public static void main(String[] args) { Scanner scanner = new Scanner(System.in); Products products = new Products(); - Calculator result = new Calculator(); + Calculator prices = new Calculator(); + String product; + float price; while (true) { System.out.println("На скольких человек необходимо разделить счёт?"); - quantity = scanner.nextInt(); - if (quantity > 1) { - while (true) { - System.out.println("Введите название блюда или команду Завершить"); - product = scanner.next(); - if (product.equalsIgnoreCase("Завершить")) { + if(scanner.hasNextInt()){ + quantity = scanner.nextInt(); + scanner.nextLine(); + if (quantity > 1 ) { + break; + } else { + System.out.println("Введите значение больше 1"); + } + } else { + System.out.println("Введите целое число"); + scanner.nextLine(); + } + } + + String end = ""; + while (!end.equalsIgnoreCase("Завершить")) { + System.out.println("Введите название блюда"); + product = scanner.nextLine(); + products.addProduct(product); + while (true) { + System.out.println("Введите цену блюда"); + if (scanner.hasNextFloat() ) { + price = scanner.nextFloat(); + scanner.nextLine(); + if(price > 0) { + prices.addPrice(price); break; + } else { + System.out.println("Введите цену больше 0"); } - while (true) { - System.out.println("Введите цену блюда"); - if (scanner.hasNextFloat()) { - float price = scanner.nextFloat(); - result.addPrice(price); - break; - } else { - System.out.println("Введите корректное значение"); - } - } - products.addProduct(product); - System.out.println("Товар успешно добавлен"); + } else { + System.out.println("введите цифровое значение"); + scanner.nextLine(); } - break; - } else { - System.out.println("Введите значение больше 2"); } + System.out.println("Товар успешно добавлен"); + System.out.println("Добавить новый товар или завершить?"); + end = scanner.nextLine(); } + System.out.println("Добавленные товары:"); products.outputProducts(); - result.samib(); + prices.samib(); scanner.close(); } } \ No newline at end of file diff --git a/src/main/java/Products.java b/src/main/java/Products.java index 9179fc0ed..5d06a670e 100644 --- a/src/main/java/Products.java +++ b/src/main/java/Products.java @@ -5,8 +5,8 @@ public void addProduct(String product){ products.add(product); } public void outputProducts() { - for(int i = 0; i < products.size(); i++) { - System.out.println(products.get(i)); + for(String product : products) { + System.out.println(product); } } } \ No newline at end of file