From c920c81ae59a4e4d2df4d7ded3b177b71f14b86b Mon Sep 17 00:00:00 2001 From: alex1261bat Date: Sun, 6 Nov 2022 19:45:56 +0300 Subject: [PATCH 1/2] =?UTF-8?q?feat:=20=D1=80=D0=B5=D0=B0=D0=BB=D0=B8?= =?UTF-8?q?=D0=B7=D0=BE=D0=B2=D0=B0=D1=82=D1=8C=20=D0=BA=D0=BE=D0=BD=D1=81?= =?UTF-8?q?=D0=BE=D0=BB=D1=8C=D0=BD=D1=8B=D0=B9=20=D0=BA=D0=B0=D0=BB=D1=8C?= =?UTF-8?q?=D0=BA=D1=83=D0=BB=D1=8F=D1=82=D0=BE=D1=80=20=D1=81=D1=87=D0=B5?= =?UTF-8?q?=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/.gitignore | 8 ++ build.gradle | 3 +- src/main/java/Main.java | 10 +- src/main/java/calculator/Calculator.java | 111 +++++++++++++++++++++++ src/main/java/calculator/Product.java | 19 ++++ 5 files changed, 147 insertions(+), 4 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 src/main/java/calculator/Calculator.java create mode 100644 src/main/java/calculator/Product.java diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/build.gradle b/build.gradle index 1b1d075..1843428 100644 --- a/build.gradle +++ b/build.gradle @@ -1,6 +1,6 @@ apply plugin: 'java' -sourceCompatibility = 1.8 +sourceCompatibility = 11 version = '1.0' repositories { @@ -8,3 +8,4 @@ repositories { dependencies { } +targetCompatibility = JavaVersion.VERSION_11 diff --git a/src/main/java/Main.java b/src/main/java/Main.java index a9198c4..d5f94f9 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,8 +1,12 @@ +import calculator.Calculator; + public class Main { public static void main(String[] args) { - // ваш код начнется здесь - // вы не должны ограничиваться только классом Main и можете создавать свои классы по необходимости - System.out.println("Привет Мир"); + Calculator calculator = new Calculator(); + + System.out.println("Добро пожаловать в приложение Калькулятор счета"); + calculator.calculate(); + System.out.println("Спасибо что выбрали наше приложение"); } } diff --git a/src/main/java/calculator/Calculator.java b/src/main/java/calculator/Calculator.java new file mode 100644 index 0000000..eabfc41 --- /dev/null +++ b/src/main/java/calculator/Calculator.java @@ -0,0 +1,111 @@ +package calculator; + +import java.util.*; + +public class Calculator { + private final Scanner scanner = new Scanner(System.in); + private final List products = new ArrayList<>(); + + public void calculate() { + int numberOfPersons = addNumberOfPersons(); + double sum = addProducts(); + double result = sum / numberOfPersons; + + printAllProducts(); + printResult(result); + } + + private int addNumberOfPersons() { + int numberOfPersons = 0; + + System.out.println("Введите количество персон"); + + while (numberOfPersons < 2) { + if (!scanner.hasNextInt()) { + System.out.println("Вы ввели некорректное значение \nПопробуйте еще раз"); + scanner.next(); + } else { + numberOfPersons = scanner.nextInt(); + if (numberOfPersons < 2) { + System.out.println("Количество персон должно быть более 1 \nПопробуйте еще раз"); + } + } + } + + return numberOfPersons; + } + + private double addProducts() { + String exit = "Завершить"; + double sumOfProducts = 0.0; + + do { + String productTitle = addProductTitle(); + double productPrice = addProductPrice(); + Product product = new Product(productTitle, productPrice); + sumOfProducts += productPrice; + + products.add(product); + System.out.println("Товар успешно добавлен"); + System.out.println("Хотите добавить еще товар? " + + "\nЕсли да, то введите любой символ " + + "\nЕсли хотите завершить, то введите 'Завершить'"); + + } while (!scanner.next().equalsIgnoreCase(exit)); + + return sumOfProducts; + } + + private String addProductTitle() { + System.out.println("Введите название товара"); + + return scanner.next(); + } + + private double addProductPrice() { + double price = 0.0; + + System.out.println("Введите стоимость товара в формате 'рубли,копейки' [10,45, 11,40]"); + + while (price <= 0.01) { + if (!scanner.hasNextDouble()) { + System.out.println("Вы ввели некорректное значение " + + "\nВведите стоимость товара в формате 'рубли,копейки' [10,45, 11,40] " + + "\nПопробуйте еще раз"); + scanner.next(); + } else { + price = scanner.nextDouble(); + if (price <= 0.0) { + System.out.println("Стоимость товара должна быть больше 0 \nПопробуйте еще раз"); + } + } + } + + return price; + } + + private void printAllProducts() { + System.out.println("Добавленные товары:"); + + for (Product product : products) { + System.out.println(product.getTitle() + " : " + String.format("%.2f", product.getPrice())); + } + } + + private void printResult(double price) { + String format = String.valueOf((int) price); + String result; + + if (format.endsWith("11")) { + result = "рублей"; + } else if (format.endsWith("1")) { + result = "рубль"; + } else if (format.endsWith("2") || format.endsWith("3") || format.endsWith("4")) { + result = "рубля"; + } else { + result = "рублей"; + } + + System.out.println("Сумма к оплате: " + String.format("%.2f", price) + " " + result); + } +} diff --git a/src/main/java/calculator/Product.java b/src/main/java/calculator/Product.java new file mode 100644 index 0000000..01a5ef1 --- /dev/null +++ b/src/main/java/calculator/Product.java @@ -0,0 +1,19 @@ +package calculator; + +class Product { + private final String title; + private final double price; + + public Product(String title, double price) { + this.title = title; + this.price = price; + } + + String getTitle() { + return title; + } + + double getPrice() { + return price; + } +} From 149f6871d8a6d375c021b3d3ccadc43381226ce2 Mon Sep 17 00:00:00 2001 From: alex1261bat Date: Sun, 6 Nov 2022 19:49:47 +0300 Subject: [PATCH 2/2] =?UTF-8?q?feat:=20=D1=80=D0=B5=D0=B0=D0=BB=D0=B8?= =?UTF-8?q?=D0=B7=D0=BE=D0=B2=D0=B0=D1=82=D1=8C=20=D0=BA=D0=BE=D0=BD=D1=81?= =?UTF-8?q?=D0=BE=D0=BB=D1=8C=D0=BD=D1=8B=D0=B9=20=D0=BA=D0=B0=D0=BB=D1=8C?= =?UTF-8?q?=D0=BA=D1=83=D0=BB=D1=8F=D1=82=D0=BE=D1=80=20=D1=81=D1=87=D0=B5?= =?UTF-8?q?=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/codeStyles/Project.xml | 10 ++++++++++ .idea/codeStyles/codeStyleConfig.xml | 5 +++++ .idea/compiler.xml | 6 ++++++ .idea/gradle.xml | 16 ++++++++++++++++ .idea/misc.xml | 8 ++++++++ .idea/vcs.xml | 6 ++++++ 6 files changed, 51 insertions(+) create mode 100644 .idea/codeStyles/Project.xml create mode 100644 .idea/codeStyles/codeStyleConfig.xml create mode 100644 .idea/compiler.xml create mode 100644 .idea/gradle.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/vcs.xml diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml new file mode 100644 index 0000000..1bec35e --- /dev/null +++ b/.idea/codeStyles/Project.xml @@ -0,0 +1,10 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/codeStyles/codeStyleConfig.xml b/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 0000000..79ee123 --- /dev/null +++ b/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..fb7f4a8 --- /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..ba1ec5c --- /dev/null +++ b/.idea/gradle.xml @@ -0,0 +1,16 @@ + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..08a0d19 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,8 @@ + + + + + + + + \ 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