From 91c715307ec244b29e9b922e8ab205860a5d945a Mon Sep 17 00:00:00 2001 From: SvetlanaTimchenko Date: Sun, 23 Oct 2022 21:45:03 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9A=D0=BE=D0=BC=D0=BC=D0=B5=D0=BD=D1=82?= =?UTF-8?q?=D0=B0=D1=80=D0=B8=D0=B5=D0=B2=20=D0=BF=D0=BE=D0=BA=D0=B0=20?= =?UTF-8?q?=D0=BD=D0=B5=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/.gitignore | 3 ++ .idea/compiler.xml | 6 +++ .idea/gradle.xml | 18 ++++++++ .idea/misc.xml | 10 +++++ .idea/vcs.xml | 6 +++ src/main/java/Calculator.java | 45 ++++++++++++++++++++ src/main/java/Good.java | 12 ++++++ src/main/java/Main.java | 77 +++++++++++++++++++++++++++++++++-- 8 files changed, 174 insertions(+), 3 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/compiler.xml create mode 100644 .idea/gradle.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/vcs.xml create mode 100644 src/main/java/Calculator.java create mode 100644 src/main/java/Good.java diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..61a9130 --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/gradle.xml b/.idea/gradle.xml new file mode 100644 index 0000000..6cec569 --- /dev/null +++ b/.idea/gradle.xml @@ -0,0 +1,18 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..a47d29e --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,10 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/main/java/Calculator.java b/src/main/java/Calculator.java new file mode 100644 index 0000000..9e5382e --- /dev/null +++ b/src/main/java/Calculator.java @@ -0,0 +1,45 @@ + +import java.util.Scanner; + +public class Calculator { + + String receipt = ""; + double summa = 0; + + public void addGood(String title, double price) { + Good tovar = new Good(title, price); + + receipt = receipt.concat("\n").concat(tovar.title); + summa = summa + tovar.amount; + } + + public String getReceipt() { + return receipt; + } + + public double getSumma() { + return summa; + } + + public String getRubleAddition(int num) + { + int preLastDigit = num % 100 / 10; + if (preLastDigit == 1) + { + return "рублей"; + } + + switch (num % 10) + { + case 1: + return "рубль"; + case 2: + case 3: + case 4: + return "рубля"; + default: + return "рублей"; + } + } + +} diff --git a/src/main/java/Good.java b/src/main/java/Good.java new file mode 100644 index 0000000..01c6bb8 --- /dev/null +++ b/src/main/java/Good.java @@ -0,0 +1,12 @@ +public class Good { + + // поля + String title; + double amount; + + Good (String name, double price) { + title = name; + amount = price; + } + +} diff --git a/src/main/java/Main.java b/src/main/java/Main.java index a9198c4..1b87f08 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,8 +1,79 @@ + +import java.util.Scanner; + public class Main { public static void main(String[] args) { - // ваш код начнется здесь - // вы не должны ограничиваться только классом Main и можете создавать свои классы по необходимости - System.out.println("Привет Мир"); + + Scanner scanner = new Scanner(System.in); + // шаг 1: запрашиваем количество участников + int numberOfFriends; + + while (true) { + System.out.println("На сколько человек вы хотели бы разделить счет?"); + // проверяем наличие целого числа в вводе + if (!scanner.hasNextInt()) { // если нет целое число, то просим ввести еще раз + System.out.println("Введите целое число: пожалуйста."); + scanner.next(); + continue; + } + else { + numberOfFriends = scanner.nextInt(); + } + + // проверяем количество человек + if (numberOfFriends > 1) { + break; + } else { + System.out.println("Вас слишком мало, чтобы делить счет :)"); + } + + } + + // шаг 2. добавление товаров и подсчет товаров + Calculator calculator = new Calculator(); + while (true) { + System.out.println("Введите название товара"); + scanner.nextLine(); + String name = scanner.nextLine(); + + System.out.println("Введите цену"); + double price; + + // проверяем, что цена введена корректно + if (!scanner.hasNextDouble()) { + System.out.println("Ошибка в цене товара. Попробуйте снова."); + continue; + } + else { + price = scanner.nextDouble(); + } + + if (price > 0) { + calculator.addGood(name, price); // добавляем товар + System.out.println("Товар успешно добавлен. Если хотите завершить ввод, напишите - завершить."); + String exit = scanner.next(); + if (exit.equalsIgnoreCase("завершить")) { + break; + } + } else { + System.out.println("Вы ввели отрицательную цену. Попробуйте еще раз ввести товар."); + } + } + + // шаг 3. вывод результата + System.out.println("Добавленные товары:"); + System.out.println(calculator.getReceipt()); + System.out.println("Сумма к оплате: " + String.format("%.2f",calculator.getSumma())); + + // определяем окончание слова рубль + double finalAmount = calculator.getSumma() / numberOfFriends; + String formattedDouble = String.format("%.2f", finalAmount); + String rubles = calculator.getRubleAddition((int) Math.floor(finalAmount)); + + System.out.println("Каждый должен заплатить = " + formattedDouble + " " + rubles); + } } + +