From 0216179adb506dbafd94b21fdb50cbef158e7ccc Mon Sep 17 00:00:00 2001 From: azotGr Date: 2023年10月23日 23:15:54 +0300 Subject: [PATCH] =?UTF-8?q?=D1=8F=20=D0=BF=D1=8B=D1=82=D0=B0=D1=8E=D1=81?= =?UTF-8?q?=D1=8C...?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/Check.java | 112 ++++++++++++++++++++++++++++++++++++ src/main/java/Main.java | 13 +++-- src/main/java/Products.java | 83 ++++++++++++++++++++++++++ 3 files changed, 204 insertions(+), 4 deletions(-) create mode 100644 src/main/java/Check.java create mode 100644 src/main/java/Products.java diff --git a/src/main/java/Check.java b/src/main/java/Check.java new file mode 100644 index 000000000..e7d98d9ac --- /dev/null +++ b/src/main/java/Check.java @@ -0,0 +1,112 @@ +import java.util.Scanner; + +public class Check { + public static boolean isNumeric(String str) + { + try + { + Integer.parseInt(str); + return true; + } + catch(NumberFormatException e) + { + return false; + } + } + + public static boolean isDouble(String str) + { + try + { + Double.parseDouble(str); + return true; + } + catch(NumberFormatException e) + { + return false; + } + } + + public static String enter() + { + Scanner in = new Scanner(System.in); + return in.next(); + } + + public static String countPeople(String enter) + { + boolean flag = true; + while(flag) + { + if(isNumeric(enter)) + { + int countPeople = Integer.parseInt(enter); + if(countPeople == 1) + { + System.out.println("Количество человек, должно быть больше единицы. Введите значение больше единицы."); + enter = enter(); + } + else if (countPeople < 1) + { + System.out.println("Ошибка, введите число больше единицы."); + enter = enter(); + } + else + { + flag = false; + } + } + else + { + System.out.println("Некорректный ввод.Введите числовое значение, больше единицы."); + enter = enter(); + } + } + return enter; + } + public static double checkPrice(String enter) + { + boolean flag = true; + while(flag) + { + if (isDouble(enter.replace(',','.'))) + { + if(Double.parseDouble(enter) <= 0) + { + System.out.println("Ошибка, введите числовое значение, больше нуля"); + enter = enter().replace(',','.'); + } + else + { + flag = false; + } + } + else + { + System.out.println("Ошибка, введите числовое значение"); + enter = enter().replace(',','.'); + } + } + return Double.parseDouble(enter.replace(',','.')); + } + + public static String print(double result) + { + String[] a = String.valueOf(result).split("[.]"); + int integralPart = Integer.parseInt(a[0]); + int endOne = integralPart%10; + int endTwo = integralPart%100; + if(endOne == 1){ + return " рубль"; + } + else if((endOne> 1 && endOne < 5) && !(endTwo>=11 && endTwo<=14)){ + return " рубля"; + } + else if(endTwo>= 11 && endTwo <= 14){ + return " рублей"; + } + else{ + return " рублей"; + } + } +} diff --git a/src/main/java/Main.java b/src/main/java/Main.java index db9356a08..4f6feaf5a 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,6 +1,11 @@ - -public class Main { - public static void main(String[] args) { - System.out.println("Hello world!"); +public class Main +{ + public static void main(String[] args) + { + System.out.println("Добро пожаловать в приложение калькулятор!"); + System.out.println("На скольких человек, вы планируете разделить счет?"); + String enteredValue = Check.enter(); + int countPeople = Integer.parseInt(Check.countPeople(enteredValue)); + Products.productAll(countPeople); } } \ No newline at end of file diff --git a/src/main/java/Products.java b/src/main/java/Products.java new file mode 100644 index 000000000..ef973250e --- /dev/null +++ b/src/main/java/Products.java @@ -0,0 +1,83 @@ +import java.text.DecimalFormat; +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; + +public class Products { + String name; + double price; + + public Products(String name, double price) + { + this.name = name; + this.price = price; + } + + public String getNameOrPrice() { + String rubs = Check.print(price); + String formattedDouble = new DecimalFormat("#0.00").format(price); + formattedDouble = formattedDouble.replace(',','.'); + return name + " цена: " + formattedDouble + rubs; + } + + static double onePeople(int countPeople, double allPriceProduct) + { + return allPriceProduct/countPeople; + } + + static void productAll(int countPeople) { + Scanner in = new Scanner(System.in); + //boolean flag = true; + List products = new ArrayList(); + double allPriceProduct = 0.00f; + while (true) { + System.out.println("Введите название продукта:"); + String nameProduct = in.next(); + System.out.println("Введите цену на данный товар:"); + String enterPrice = in.next(); + enterPrice = enterPrice.replace(',', '.'); + if (Check.isDouble(enterPrice)) { + double priceProduct = Double.parseDouble(enterPrice); + if (priceProduct <= 0) + { + priceProduct = Check.checkPrice(enterPrice); + allPriceProduct += priceProduct; + Products product = new Products(nameProduct, priceProduct); + products.add(product); + } + else + { + allPriceProduct += priceProduct; + Products product = new Products(nameProduct, priceProduct); + products.add(product); + } + } else { + double priceProduct = Check.checkPrice(enterPrice); + allPriceProduct += priceProduct; + Products product = new Products(nameProduct, priceProduct); + products.add(product); + } + + System.out.println("Хотите ли вы ввести еще один продукт? Да - введите любой символ, нет - введите 'завершить'."); + String answer = in.next(); + if (answer.equalsIgnoreCase("Завершить")) { + //flag = false; + break; + } + } + double priceForOne = onePeople(countPeople, allPriceProduct); + String formattedDouble = new DecimalFormat("#0.00").format(priceForOne); + formattedDouble = formattedDouble.replace(',','.'); + double result = Double.parseDouble(formattedDouble); + System.out.println("Добавленные товары:"); + for(Products products1 : products) { + System.out.println(products1.getNameOrPrice()); + } + String formattedDoubleTwo = new DecimalFormat("#0.00").format(allPriceProduct); + formattedDoubleTwo = formattedDoubleTwo.replace(',','.'); + String rubsOne = Check.print(allPriceProduct); + System.out.println("Общая сумма: " + formattedDoubleTwo + " " + rubsOne); + String rubs = Check.print(result); + System.out.println("К оплате для одного человека: " + result + rubs); + } +}

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