From 85f1882f284d4c33990fd7e4fe4d0661c68a9676 Mon Sep 17 00:00:00 2001 From: Aleksandra R Date: Fri, 7 Jun 2024 20:01:55 +0300 Subject: [PATCH 1/2] =?UTF-8?q?=20=D0=9F=D1=80=D0=BE=D0=B5=D0=BA=D1=82?= =?UTF-8?q?=D0=BD=D0=B0=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=20?= =?UTF-8?q?=D0=BD=D0=B0=20=D0=BF=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D0=BA=D1=83?= =?UTF-8?q?=201?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/BillCalculator.java | 127 ++++++++++++++++++++++++++++++ src/main/java/Goods.java | 11 +++ src/main/java/Main.java | 7 +- 3 files changed, 142 insertions(+), 3 deletions(-) create mode 100644 src/main/java/BillCalculator.java create mode 100644 src/main/java/Goods.java diff --git a/src/main/java/BillCalculator.java b/src/main/java/BillCalculator.java new file mode 100644 index 000000000..fa9f55fd4 --- /dev/null +++ b/src/main/java/BillCalculator.java @@ -0,0 +1,127 @@ +import java.util.ArrayList; +import java.util.InputMismatchException; +import java.util.Scanner; + +public class BillCalculator { + + Scanner scanner = new Scanner(System.in); + + public void start() { + + + System.out.println("Добро пожаловать в калькулятор счетов!"); + System.out.println("Разделите свой счет с друзьями :"); + System.out.println("Для этого сначала введите количество друзей."); + System.out.println("Далее добавляйте товары и их стоимость."); + System.out.println("Для того, чтобы закончить работу программы введите слово 'Завершить'."); + System.out.println("Введите количество друзей, которые разделят счет:"); + + while (true) { + int friends = friends(); + if (friends < 2) { + System.out.println("Вам не разделить этот счет на " + friends + " %d друзей"); + if (printMenuExit()) { + break; + } + } else { + executeCalculate(friends); + } + } + } + + public boolean printMenuExit() { + + scanner.nextLine(); + + System.out.println("................................"); + System.out.println("Хотите продолжить или завершить?\nДля завершения программы введите слово 'Завершить' в любом регистре,\nдля продолжения нажмите на любую клавишу."); + String commandExit = scanner.nextLine(); + commandExit = commandExit.toLowerCase(); + if (commandExit.equals("завершить")) { + System.out.println("Выход. Спасибо за использование!"); + return true; + } else { + return false; + } + } + + public double priceGoods() { + + while (true) { + try { + return scanner.nextDouble(); + } + catch (InputMismatchException e) { + System.out.println("Вы ввели неправильную цену, повторите ввод!"); + scanner.next(); + } + } + } + + public int friends() { + + while (true) { + try { + return scanner.nextInt(); + } + catch (InputMismatchException e) { + System.out.println("Вы ввели неправильно количество друзей."); + if (printMenuExit()) { + return 0; + } else { + System.out.println("Введите количество друзей:"); + } + } + } + } + + public String wordCurrency(double price) { + + String[] wordCurrencyList = { + "рубль", "рубля", "рублей" + }; + + int lastSimbolPrice = (int) price % 10; + int twoLastSimbolPrice = (int) price % 100; + + if (twoLastSimbolPrice>= 11 && twoLastSimbolPrice<= 19) { + return wordCurrencyList[2]; + } else { + return switch (lastSimbolPrice) { + case 1 -> wordCurrencyList[0]; + case 2, 3, 4 -> wordCurrencyList[1]; + default -> wordCurrencyList[2]; + }; + } + } + + private void executeCalculate(int friends) { + int i = 0; + ArrayList goodsList = new ArrayList(); + + while (true) { + + i++; + System.out.println("Введите наименование товара " + i + ":"); + String nameGoods = scanner.next(); + System.out.println("Введите цену товара " + i +":"); + Goods goods = new Goods(nameGoods, priceGoods(), i); + goodsList.add(goods); + System.out.println("----\nСЧЕТ\n----"); + + double totalPrice = 0; + for (int x = 0; x < goodsList.size(); x++) { + System.out.println(String.format("%d. %s - %.2f %s;", goodsList.get(x).numberGoogs, goodsList.get(x).nameGoods, goodsList.get(x).priceGoods, wordCurrency(goodsList.get(x).priceGoods))); + totalPrice = totalPrice + goodsList.get(x).priceGoods; + } + double friendsPrice = totalPrice / friends; + + System.out.println(String.format("Итоговая сумма: %.2f %s.", totalPrice, wordCurrency(totalPrice))); + System.out.println(String.format("Друзей: %d, каждому необходимо заплатить - %.2f %s!", friends, friendsPrice, wordCurrency(friendsPrice))); + + if (printMenuExit()) { + break; + } + } + } +} diff --git a/src/main/java/Goods.java b/src/main/java/Goods.java new file mode 100644 index 000000000..09f0fed01 --- /dev/null +++ b/src/main/java/Goods.java @@ -0,0 +1,11 @@ +public class Goods { + String nameGoods; + double priceGoods; + int numberGoogs; + + Goods (String nameGoods, double priceGoods, int numberGoogs) { + this.nameGoods = nameGoods ; + this.priceGoods = priceGoods ; + this.numberGoogs = numberGoogs; + } +} diff --git a/src/main/java/Main.java b/src/main/java/Main.java index db9356a08..e393fdc46 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,6 +1,7 @@ - public class Main { public static void main(String[] args) { - System.out.println("Hello world!"); + + BillCalculator billCalculator = new BillCalculator(); + billCalculator.start(); } -} \ No newline at end of file +} From fd722fe1295b20c6e091277ff7b9751923a9ceb0 Mon Sep 17 00:00:00 2001 From: Aleksandra R Date: Fri, 7 Jun 2024 20:45:07 +0300 Subject: [PATCH 2/2] =?UTF-8?q?=20=D0=9F=D1=80=D0=BE=D0=B5=D0=BA=D1=82?= =?UTF-8?q?=D0=BD=D0=B0=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=20?= =?UTF-8?q?=D0=BD=D0=B0=20=D0=BF=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D0=BA=D1=83?= =?UTF-8?q?=20#1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/BillCalculator.java | 1 - src/main/java/Goods.java | 1 + src/main/java/Main.java | 1 + 3 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/BillCalculator.java b/src/main/java/BillCalculator.java index fa9f55fd4..e3b0b9a1f 100644 --- a/src/main/java/BillCalculator.java +++ b/src/main/java/BillCalculator.java @@ -8,7 +8,6 @@ public class BillCalculator { public void start() { - System.out.println("Добро пожаловать в калькулятор счетов!"); System.out.println("Разделите свой счет с друзьями :"); System.out.println("Для этого сначала введите количество друзей."); diff --git a/src/main/java/Goods.java b/src/main/java/Goods.java index 09f0fed01..acb9922d7 100644 --- a/src/main/java/Goods.java +++ b/src/main/java/Goods.java @@ -1,4 +1,5 @@ public class Goods { + String nameGoods; double priceGoods; int numberGoogs; diff --git a/src/main/java/Main.java b/src/main/java/Main.java index e393fdc46..9c1b47fd3 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,4 +1,5 @@ public class Main { + public static void main(String[] args) { BillCalculator billCalculator = new BillCalculator();

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