From adaab96c0946ae7667d5a581fe4e2b9b7e08e6f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B5=D0=BD=D0=B8=D1=81?= Date: Sat, 10 Feb 2024 13:18:35 +0200 Subject: [PATCH 1/2] =?UTF-8?q?=D0=A1=D0=BF=D1=80=D0=B8=D0=BD=D1=82=202/23?= =?UTF-8?q?=20=E2=86=92=20=D0=A2=D0=B5=D0=BC=D0=B0=205/6:=20=D0=9F=D1=80?= =?UTF-8?q?=D0=BE=D0=B5=D0=BA=D1=82=D0=BD=D0=B0=D1=8F=20=D1=80=D0=B0=D0=B1?= =?UTF-8?q?=D0=BE=D1=82=D0=B0=20=E2=84=961?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/Main.java | 124 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 122 insertions(+), 2 deletions(-) diff --git a/src/main/java/Main.java b/src/main/java/Main.java index db9356a08..2a37babd7 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,6 +1,126 @@ +import java.util.ArrayList; +import java.util.Scanner; public class Main { public static void main(String[] args) { - System.out.println("Hello world!"); + AccountCalculator сalculator = new AccountCalculator(); + + сalculator.start(); +} +} + class AccountCalculator { + ArrayList сalculatorList = new ArrayList<>(); + ArrayList сalculatorDouble = new ArrayList<>(); + + int userInput; + String productName; + Double productPrice; + String productPriceString; + String finish; + Double totalСost = 00.00; + + private void сalculator() { + Scanner scanner = new Scanner(System.in); + + + System.out.println("Введите название товара:"); + productName = scanner.next(); + + сalculatorList.add(productName); + System.out.println("Введите стоимость в формате 00,00 Руб.коп.:"); + productPriceString = scanner.next(); + productPriceString = productPriceString.replaceAll(",", "."); + while (!isNumeric(productPriceString)) { + System.out.print("Вы ввели некорректное значение\n"); + productPriceString = scanner.next(); + } + + productPrice = Double.parseDouble(productPriceString); + сalculatorDouble.add(productPrice); + totalСost = totalСost + productPrice; + String result = String.format("%.2f", totalСost); + System.out.print("\nСписок добавленного товара:\n"); + for (int i = 0; i < сalculatorList.size(); i++) { + String name = сalculatorList.get(i); + double price = сalculatorDouble.get(i); + int namb = i; + String rubName = " рубль"; + int rub1 = (int) price; + switch (rub1 % 10){ case 1: + rubName = " рубль"; break; + case 2: case 3: case 4: + rubName = " рубля"; break; + case 5: case 6: case 7: case 8: case 9: case 0: + rubName = " рублей"; + } + System.out.print(++namb + " " + name + " " + price + " " + rubName + "\n"); + } + + System.out.print("Общая стоимость: " + result + "\n \n"); + System.out.print("Товар добавлен успешно! \nЧтобы продолжить добавление товара введите любой символ \nЧтобы завершить добавления товара введите команду 'Завершить'\n"); + finish = scanner.next(); + finish (finish, userInput, totalСost); + } + private void finish (String finish, int userInput, double totalСost) { + String fin = "Завершить"; + String finСost = String.format("%.2f", (totalСost/userInput)); + System.out.print("\nСписок добавленного товара:\n"); + for (int i = 0; i < сalculatorList.size(); i++) { + String name = сalculatorList.get(i); + double price = сalculatorDouble.get(i); + int namb = i; + String rubName = " рубль"; + int rub1 = (int) price; + switch (rub1 % 10){ case 1: + rubName = " рубль"; break; + case 2: case 3: case 4: + rubName = " рубля"; break; + case 5: case 6: case 7: case 8: case 9: case 0: + rubName = " рублей"; + } + System.out.print(++namb + " " + name + " " + price + " " + rubName + "\n"); + } + if (finish.equalsIgnoreCase(fin)) { + System.out.print("Делим счет\n"); + System.out.print("Каждый должен заплатить " + finСost); + int rub = (int) totalСost/userInput; + switch (rub % 10){ case 1: + System.out.println(" рубль"); break; + case 2: case 3: case 4: + System.out.println(" рубля"); break; + case 5: case 6: case 7: case 8: case 9: case 0: + System.out.println(" рублей"); break; + } + } else { + сalculator(); + } + } + public static boolean isNumeric(String productPriceString) { + try { + Double.parseDouble(productPriceString); + return true; + } catch (NumberFormatException e) { + return false; + } + } + + public void start() { + Scanner scanner = new Scanner(System.in); + + do { + System.out.println("На скольких человек необходимо разделить счёт \n(Введите количество человек более 1):"); + while (!scanner.hasNextInt()) { + System.out.println("Это некорректное значение для подсчёта \nНа скольких человек необходимо разделить счёт \n(Введите количество человек более 1):"); + scanner.next(); + } + userInput = scanner.nextInt(); + } while (userInput <= 1); + System.out.println("Будем делить счёт на " + userInput + " чел."); + сalculator(); } -} \ No newline at end of file + } + + + + + From 1f1f199848c5b971949255f6e7bc827237ec3732 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B5=D0=BD=D0=B8=D1=81?= Date: Sun, 11 Feb 2024 12:03:01 +0200 Subject: [PATCH 2/2] =?UTF-8?q?=D0=A1=D0=BF=D1=80=D0=B8=D0=BD=D1=82=202/23?= =?UTF-8?q?=20=E2=86=92=20=D0=A2=D0=B5=D0=BC=D0=B0=205/6:=20=D0=9F=D1=80?= =?UTF-8?q?=D0=BE=D0=B5=D0=BA=D1=82=D0=BD=D0=B0=D1=8F=20=D1=80=D0=B0=D0=B1?= =?UTF-8?q?=D0=BE=D1=82=D0=B0=20=E2=84=961=20=D0=92=D0=B0=D1=80=D0=B8?= =?UTF-8?q?=D0=B0=D0=BD=D1=82=202?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/Main.java | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 2a37babd7..b0cdcc3cc 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -27,7 +27,7 @@ class AccountCalculator { productName = scanner.next(); сalculatorList.add(productName); - System.out.println("Введите стоимость в формате 00,00 Руб.коп.:"); + System.out.println("Введите стоимость в формате 00,00 руб.коп.:"); productPriceString = scanner.next(); productPriceString = productPriceString.replaceAll(",", "."); while (!isNumeric(productPriceString)) { @@ -43,7 +43,6 @@ class AccountCalculator { for (int i = 0; i < сalculatorList.size(); i++) { String name = сalculatorList.get(i); double price = сalculatorDouble.get(i); - int namb = i; String rubName = " рубль"; int rub1 = (int) price; switch (rub1 % 10){ case 1: @@ -53,7 +52,7 @@ class AccountCalculator { case 5: case 6: case 7: case 8: case 9: case 0: rubName = " рублей"; } - System.out.print(++namb + " " + name + " " + price + " " + rubName + "\n"); + System.out.print(i+1 + " " + name + " " + price + " " + rubName + "\n"); } System.out.print("Общая стоимость: " + result + "\n \n"); @@ -110,10 +109,14 @@ public void start() { do { System.out.println("На скольких человек необходимо разделить счёт \n(Введите количество человек более 1):"); while (!scanner.hasNextInt()) { - System.out.println("Это некорректное значение для подсчёта \nНа скольких человек необходимо разделить счёт \n(Введите количество человек более 1):"); + System.out.println("Это некорректное значение для подсчёта \n\n На скольких человек необходимо разделить счёт \n(Введите количество человек более 1):"); scanner.next(); } userInput = scanner.nextInt(); + if (userInput <=1) { + System.out.println("Ошибка"); + start(); + } } while (userInput <= 1); System.out.println("Будем делить счёт на " + userInput + " чел."); сalculator();