From d4f916fa56421fdd8c393c194a5faae6f3ae9693 Mon Sep 17 00:00:00 2001 From: leo Date: Mon, 5 Jun 2023 20:43:53 +0300 Subject: [PATCH 1/4] =?UTF-8?q?=D0=A7=D0=B5=D1=80=D0=BD=D0=BE=D0=B2=D0=B0?= =?UTF-8?q?=D1=8F=20=D0=B2=D0=B5=D1=80=D1=81=D0=B8=D1=8F=20=D0=BF=D1=80?= =?UTF-8?q?=D0=B8=D0=BB=D0=BE=D0=B6=D0=B5=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/Main.java | 64 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 62 insertions(+), 2 deletions(-) diff --git a/src/main/java/Main.java b/src/main/java/Main.java index db9356a08..abe500db0 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,6 +1,66 @@ +import java.util.Scanner; public class Main { + public static class Product { + String name; + double price; + + Product(String productName, double productPrice) { + name = productName; + price = productPrice; + } + } + public static class Calculator { + String productNames = ""; + double productPrices = 0; + + void addProduct(Product p) { + productPrices = productPrices + p.price; + productNames = productNames.concat(p.name).concat("\n"); + System.out.println("Товар успешно добавлен!"); + } + + double divideByAll (int guests) { + return productPrices / guests; + } + + } + public static void main(String[] args) { - System.out.println("Hello world!"); + Scanner scanner = new Scanner(System.in); + int guests; + while (true) { + System.out.println("На сколько человек необходимо разделить счёт?"); + guests = scanner.nextInt(); + if (guests> 1) { + break;//тут работаем с калькулятором + } else if (guests == 1) { + System.out.println("нет смысла ничего считать и делить"); + } else { + System.out.println("Это некорректное значение для подсчёта"); + } + } + // System.out.println("Вас " + guests + " человек"); + + Calculator calc = new Calculator(); + while (true) { + System.out.println("Введите наименование товара или 'Завершить'"); + String productName = scanner.next(); + if (productName.equalsIgnoreCase("завершить")) { + break; + } + else { + System.out.println("Введите стоимость товара"); + double productPrice = scanner.nextDouble(); + Product newProduct = new Product(productName, productPrice); + calc.addProduct(newProduct); + } + } + System.out.println("Добавленные товары:"); + System.out.println(calc.productNames); + double result = calc.divideByAll(guests); + System.out.printf("Каждый должен заплатить по: %.2f", result); } -} \ No newline at end of file +} + + From a22382a1eeb0609615f24924f0e5be3ab868fed3 Mon Sep 17 00:00:00 2001 From: leo Date: Tue, 6 Jun 2023 15:51:46 +0300 Subject: [PATCH 2/4] =?UTF-8?q?=D0=A7=D0=B5=D1=80=D0=BD=D0=BE=D0=B2=D0=B0?= =?UTF-8?q?=D1=8F=20=D0=B2=D0=B5=D1=80=D1=81=D0=B8=D1=8F=20=D0=BF=D1=80?= =?UTF-8?q?=D0=B8=D0=BB=D0=BE=D0=B6=D0=B5=D0=BD=D0=B8=D1=8F=20=D1=81=D0=BE?= =?UTF-8?q?=20=D1=81=D0=BA=D0=BB=D0=BE=D0=BD=D0=B5=D0=BD=D0=B8=D1=8F=D0=BC?= =?UTF-8?q?=D0=B8=20=D1=80=D1=83=D0=B1=D0=BB=D0=B5=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 3 ++- src/main/java/Main.java | 45 +++++++++++++++++++++++++++++++++++++---- 2 files changed, 43 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 63be1bfe0..4a2023db3 100644 --- a/README.md +++ b/README.md @@ -1 +1,2 @@ -# Пустой репозиторий для работы с Java кодом в Android Studio +# «Калькулятор счёта» +Консольное приложение, которое будет задавать вопросы в консоли, считывать пользовательский ввод и в результате показывать список товаров и сумму для каждого из друзей. diff --git a/src/main/java/Main.java b/src/main/java/Main.java index abe500db0..b370d97be 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,6 +1,36 @@ +import java.util.InputMismatchException; import java.util.Scanner; public class Main { + public static double getDouble (Scanner scanner) { + try { + double num = scanner.nextDouble(); + return num; + } + catch (InputMismatchException e) { + System.out.println("Это некорректное значение для подсчёта!"); + return 0; + } + } + public static class Formatter { + void printWithRubles (double x) { + int cutPoint = (int) Math.floor(x); + int rest = cutPoint % 10; + int rest1 = cutPoint % 100; + if (rest1>= 10 && rest1 <= 19) { + System.out.printf("%.2f Рублей", x); + } + else if (rest == 0 || rest>= 5 && rest <= 9) { + System.out.printf("%.2f Рублей", x); + } + else if (rest == 1) { + System.out.printf("%.2f Рубль", x); + } + else if (rest>= 2 && rest <= 4) { + System.out.printf("%.2f Рубля", x); + } + } + } public static class Product { String name; double price; @@ -51,15 +81,22 @@ public static void main(String[] args) { } else { System.out.println("Введите стоимость товара"); - double productPrice = scanner.nextDouble(); - Product newProduct = new Product(productName, productPrice); - calc.addProduct(newProduct); + double productPrice = getDouble(scanner); + if (productPrice> 0) { + Product newProduct = new Product(productName, productPrice); + calc.addProduct(newProduct); + } } } System.out.println("Добавленные товары:"); System.out.println(calc.productNames); double result = calc.divideByAll(guests); - System.out.printf("Каждый должен заплатить по: %.2f", result); + System.out.print("Каждый должен заплатить по: "); + + Formatter f = new Formatter(); + f.printWithRubles(result); + + scanner.close(); } } From 1dae54e90afac5ef8be797c94c132f48f9821ef6 Mon Sep 17 00:00:00 2001 From: leo Date: Tue, 6 Jun 2023 16:39:09 +0300 Subject: [PATCH 3/4] =?UTF-8?q?=D0=93=D0=BE=D1=82=D0=BE=D0=B2=D0=B0=D1=8F?= =?UTF-8?q?=20=D0=B2=D0=B5=D1=80=D1=81=D0=B8=D1=8F=20=D0=BF=D1=80=D0=B8?= =?UTF-8?q?=D0=BB=D0=BE=D0=B6=D0=B5=D0=BD=D0=B8=D1=8F=20=D1=81=D0=BE=20?= =?UTF-8?q?=D1=81=D0=BA=D0=BB=D0=BE=D0=BD=D0=B5=D0=BD=D0=B8=D1=8F=D0=BC?= =?UTF-8?q?=D0=B8=20=D1=80=D1=83=D0=B1=D0=BB=D0=B5=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/Main.java | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/src/main/java/Main.java b/src/main/java/Main.java index b370d97be..fbecf9e9e 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -8,7 +8,17 @@ public static double getDouble (Scanner scanner) { return num; } catch (InputMismatchException e) { - System.out.println("Это некорректное значение для подсчёта!"); + scanner.next(); + return 0; + } + } + public static int getInt (Scanner scanner) { + try { + int num = scanner.nextInt(); + return num; + } + catch (InputMismatchException e) { + scanner.next(); return 0; } } @@ -61,16 +71,15 @@ public static void main(String[] args) { int guests; while (true) { System.out.println("На сколько человек необходимо разделить счёт?"); - guests = scanner.nextInt(); + guests = getInt(scanner); if (guests> 1) { - break;//тут работаем с калькулятором + break; } else if (guests == 1) { System.out.println("нет смысла ничего считать и делить"); } else { System.out.println("Это некорректное значение для подсчёта"); } } - // System.out.println("Вас " + guests + " человек"); Calculator calc = new Calculator(); while (true) { @@ -80,11 +89,17 @@ public static void main(String[] args) { break; } else { - System.out.println("Введите стоимость товара"); - double productPrice = getDouble(scanner); - if (productPrice> 0) { - Product newProduct = new Product(productName, productPrice); - calc.addProduct(newProduct); + while (true) { + System.out.println("Введите стоимость товара"); + double productPrice = getDouble(scanner); + if (productPrice> 0) { + Product newProduct = new Product(productName, productPrice); + calc.addProduct(newProduct); + break; + } + else { + System.out.println("Это некорректное значение для подсчёта"); + } } } } From 496d0261b1deee31df78f1d52be3dcb7bc5669fc Mon Sep 17 00:00:00 2001 From: leo Date: Tue, 6 Jun 2023 19:59:18 +0300 Subject: [PATCH 4/4] =?UTF-8?q?=D0=92=D0=B5=D1=80=D1=81=D0=B8=D1=8F=20?= =?UTF-8?q?=D1=81=20=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D1=8F=D0=BC=D0=B8=20=D0=BF=D0=BE=20=D1=80=D0=B5=D0=B2?= =?UTF-8?q?=D1=8C=D1=8E.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/Main.java | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/src/main/java/Main.java b/src/main/java/Main.java index fbecf9e9e..6c175c0ca 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -4,8 +4,7 @@ public class Main { public static double getDouble (Scanner scanner) { try { - double num = scanner.nextDouble(); - return num; + return scanner.nextDouble(); } catch (InputMismatchException e) { scanner.next(); @@ -14,8 +13,7 @@ public static double getDouble (Scanner scanner) { } public static int getInt (Scanner scanner) { try { - int num = scanner.nextInt(); - return num; + return scanner.nextInt(); } catch (InputMismatchException e) { scanner.next(); @@ -30,13 +28,13 @@ void printWithRubles (double x) { if (rest1>= 10 && rest1 <= 19) { System.out.printf("%.2f Рублей", x); } - else if (rest == 0 || rest>= 5 && rest <= 9) { + else if (rest == 0 || rest>= 5) { System.out.printf("%.2f Рублей", x); } else if (rest == 1) { System.out.printf("%.2f Рубль", x); } - else if (rest>= 2 && rest <= 4) { + else if (rest>= 2) { System.out.printf("%.2f Рубля", x); } } @@ -63,11 +61,9 @@ void addProduct(Product p) { double divideByAll (int guests) { return productPrices / guests; } - } - public static void main(String[] args) { - Scanner scanner = new Scanner(System.in); + public static int getGuests(Scanner scanner) { int guests; while (true) { System.out.println("На сколько человек необходимо разделить счёт?"); @@ -80,8 +76,10 @@ public static void main(String[] args) { System.out.println("Это некорректное значение для подсчёта"); } } + return guests; + } - Calculator calc = new Calculator(); + public static void getProducts(Scanner scanner, Calculator calc) { while (true) { System.out.println("Введите наименование товара или 'Завершить'"); String productName = scanner.next(); @@ -103,6 +101,15 @@ public static void main(String[] args) { } } } + } + + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + int guests = getGuests(scanner); + + Calculator calc = new Calculator(); + getProducts(scanner, calc); + System.out.println("Добавленные товары:"); System.out.println(calc.productNames); double result = calc.divideByAll(guests);

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