diff --git a/src/main/java/Calc.java b/src/main/java/Calc.java new file mode 100644 index 000000000..ae4c6cfe3 --- /dev/null +++ b/src/main/java/Calc.java @@ -0,0 +1,18 @@ +public class Calc { + + int personCounter; + + double allPrice; + double personalPrice; + + + double calcPersonPrice(int personCounter, double allPrice) { + + this.allPrice = allPrice; + this.personCounter = personCounter; + personalPrice = this.allPrice / this.personCounter; + + return personalPrice; + + } +} diff --git a/src/main/java/Format.java b/src/main/java/Format.java new file mode 100644 index 000000000..5aec39997 --- /dev/null +++ b/src/main/java/Format.java @@ -0,0 +1,31 @@ +public class Format { + + double allPrice; + + public String formatRuble(double allPrice) { + + this.allPrice = (int)allPrice; + + if((this.allPrice % 100>= 11) && (this.allPrice % 100 <= 14)) { + + return " рублей"; + + } else { + + switch ((int)this.allPrice % 10) { + case 1: + return " рубль"; + case 2: + case 3: + case 4: + return " рубля"; + default: + return " рублей"; + } + } + } + + String roundingAll(double price) { + return String.format("%.2f", price); + } +} diff --git a/src/main/java/Main.java b/src/main/java/Main.java index a9198c435..d582c8214 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,8 +1,87 @@ +import java.util.Scanner; public class Main { - public static void main(String[] args) { - // ваш код начнется здесь - // вы не должны ограничиваться только классом Main и можете создавать свои классы по необходимости - System.out.println("Привет Мир"); + ProductsList productsList = new ProductsList(); + Calc calc = new Calc(); + Format format = new Format(); + + int inputCount; + Scanner scanner = new Scanner(System.in); + + + + + System.out.println("На сколько персон вы бы хотели разделить ваш счет ?"); + while(true) { + if(!scanner.hasNextInt()) { + + System.out.println("Неправильный ввод количества человек, повторите попытку ввода"); + scanner.next(); + + } else { + inputCount = scanner.nextInt(); + if(inputCount> 1) { + break; + } + System.out.println("Неправильное значение для рассчета"); + } + } + System.out.println("Счет необходимо разделить на " + inputCount + " персон"); + + calc.personCounter = inputCount; + while(true) { + System.out.println("Введите название товара или команду Завершить"); + productsList.prodName = scanner.next(); + + if (productsList.prodName.equalsIgnoreCase("Завершить")) { + break; + } + System.out.println("Введите стоимость блюда"); + while(!scanner.hasNextDouble()) { + + System.out.println("Неправильный ввод данных, повторите попытку"); + scanner.next(); + + } + productsList.prodPrice = scanner.nextDouble(); + + while(productsList.prodPrice <= 0) { + + System.out.println("Некорректная цена блюда, введите корректную цену"); + + while(!scanner.hasNextDouble()) { + + System.out.println("Неправильный ввод данных, повторите попытку"); + scanner.next(); + + } + + productsList.prodPrice = scanner.nextDouble(); + + } + + + calc.allPrice = calc.allPrice + productsList.prodPrice; + + + productsList.totalPrice = productsList.addProduct(productsList.prodName, productsList.prodPrice); + + + + System.out.println("Блюдо добавлено в ваш счет"); + } + + if (calc.allPrice <= 0) { + System.out.println("Вы ничего не заказывали, для оплаты нужно что то заказать"); + } else { + + System.out.println("Добавленные товары: \n" + productsList.totalPrice); + System.out.println( + "Каждый человек должен заплатить по: " + + format.roundingAll(calc.calcPersonPrice(calc.personCounter, calc.allPrice)) + + format.formatRuble(calc.calcPersonPrice(calc.personCounter, calc.allPrice)) + ); + } + scanner.close(); } -} +} \ No newline at end of file diff --git a/src/main/java/ProductsList.java b/src/main/java/ProductsList.java new file mode 100644 index 000000000..36802f078 --- /dev/null +++ b/src/main/java/ProductsList.java @@ -0,0 +1,18 @@ +public class ProductsList { + String prodName = ""; + double prodPrice; + String totalPrice = ""; + + String addProduct(String prodName, double prodPrice) { + + Format format = new Format(); + + this.prodName = prodName; + + this.prodPrice = prodPrice; + + totalPrice = totalPrice + (prodName + ": " + format.roundingAll(prodPrice) + format.formatRuble(prodPrice) + "\n"); + return totalPrice; + } + +} \ No newline at end of file

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