From d9d1eb4ae16b4332928720d6694682d180dffe08 Mon Sep 17 00:00:00 2001 From: deiteriy Date: Wed, 5 Oct 2022 18:37:10 +0600 Subject: [PATCH 1/2] =?UTF-8?q?=D0=9F=D1=80=D0=BE=D0=B5=D0=BA=D1=82=D0=BD?= =?UTF-8?q?=D0=B0=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=20=E2=84=96?= =?UTF-8?q?1,=20=D0=9A=D0=BE=D0=BC=D0=B0=D1=80=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/.gitignore | 3 ++ .idea/.name | 1 + .idea/compiler.xml | 6 +++ .idea/gradle.xml | 18 +++++++ .idea/misc.xml | 10 ++++ .idea/vcs.xml | 6 +++ src/main/java/Main.java | 113 ++++++++++++++++++++++++++++++++++++++-- 7 files changed, 154 insertions(+), 3 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/.name 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/.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/.name b/.idea/.name new file mode 100644 index 0000000..962e712 --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +Java-Module-Project \ No newline at end of file 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/Main.java b/src/main/java/Main.java index a9198c4..9fbd711 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,8 +1,115 @@ +import java.util.Scanner; // импорт сканера + public class Main { public static void main(String[] args) { - // ваш код начнется здесь - // вы не должны ограничиваться только классом Main и можете создавать свои классы по необходимости - System.out.println("Привет Мир"); + + Scanner scanner = new Scanner(System.in); + Calculator calc = new Calculator(); + Product product = new Product(); + + System.out.println("На сколько человек нужно разделить счет?"); + int payingGuests = 0;// создаем переменную - число гостей + boolean isGuestsNumberCorrect = false; + while(!isGuestsNumberCorrect) { + if(scanner.hasNextInt()) { + payingGuests = scanner.nextInt(); + if(payingGuests> 1) { + isGuestsNumberCorrect = true; + scanner.nextLine(); + } else { + System.out.println("Я не могу разделить на такое число гостей. Введите значение больше 1"); + scanner.nextLine(); + } + } else { + System.out.println("Вы ввели некорректное значение. Введите числовое значение больше 1"); + scanner.nextLine(); + } + } + System.out.println("Ввод корректен. Число гостей установлено: их ровно " + payingGuests); // тут мы разобрались с гостями, обработав некорректный ввод + + while(true) { + System.out.println("Введите название товара:"); + product.productName = scanner.nextLine(); + System.out.println("Введите стоимость товара в формате \"рубли,копейки\""); + + while(true) + if(scanner.hasNextDouble()) { + product.productPrice = scanner.nextDouble(); + scanner.nextLine(); + break; + } else { + System.out.println("Вы ввели некорректное значение. Введите числовое значение"); + scanner.nextLine(); + } + + calc.add(product); + System.out.println("Добавить еще один товар? Введите \"Завершить\" чтобы узнать итоговую стоимость,\nили любой другой символ, чтобы добавить еще товары"); + String userChoice = scanner.nextLine(); + String enough = "Завершить"; + + if(userChoice.equalsIgnoreCase(enough)) { + break; + } + } + + double guestTotal = calc.total/payingGuests; + String rubleTemplate ="%.2f"; + + System.out.println("Ваш список покупок:\n" + calc.shoppingList); + calc.findEnding(calc.total); + System.out.println("Итоговая стоимость покупок составила: " + String.format(rubleTemplate, calc.total) + " " + calc.ending); + calc.findEnding(guestTotal); + System.out.println("Каждый гость должен заплатить " + String.format(rubleTemplate, guestTotal) + " " + calc.ending); } } + + + + +class Product { + String productName; + double productPrice; +} + + + +class Calculator { + double total = 0; + String shoppingList = new String(); + String ending = new String(); + + public void add(Product product) { + total = total + product.productPrice; + shoppingList = shoppingList + product.productName + " " + product.productPrice + "\n"; + + } + + public void findEnding(double endingPrice) { + + int intPrice = (int) Math.floor(endingPrice); + + if(10 < intPrice % 100 && intPrice % 100 <= 19) { + ending = "рублей"; + } else { + switch(intPrice % 10) { + + case 1: + ending = "рубль"; + break; + case 2: + case 3: + case 4: + ending = "рубля"; + break; + default: + ending = "рублей"; + } + + + } + } + +} + + From c7eb74d2497b10e5ca555407506b4d40a2071d75 Mon Sep 17 00:00:00 2001 From: deiteriy Date: Thu, 6 Oct 2022 12:51:09 +0600 Subject: [PATCH 2/2] =?UTF-8?q?=D0=9F=D1=80=D0=BE=D0=B5=D0=BA=D1=82=D0=BD?= =?UTF-8?q?=D0=B0=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=20=E2=84=96?= =?UTF-8?q?1,=20=D0=9A=D0=BE=D0=BC=D0=B0=D1=80=D0=BE=D0=B2,=20=D0=B2=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D0=B0=D1=8F=20=D0=BF=D0=BE=D0=BF=D1=8B=D1=82=D0=BA?= =?UTF-8?q?=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/Main.java | 42 +++++++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 9fbd711..fdbdb84 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -33,16 +33,20 @@ public static void main(String[] args) { product.productName = scanner.nextLine(); System.out.println("Введите стоимость товара в формате \"рубли,копейки\""); - while(true) - if(scanner.hasNextDouble()) { - product.productPrice = scanner.nextDouble(); + while(true) { + if (scanner.hasNextDouble()) { + product.productPrice = scanner.nextDouble(); + if(product.productPrice> 0) { + scanner.nextLine(); + break; + } + System.out.println("Стоимость товара должна быть больше 0. Введите корректное значение"); scanner.nextLine(); - break; } else { - System.out.println("Вы ввели некорректное значение. Введите числовое значение"); + System.out.println("Вы ввели некорректное значение. Введите числовое значение больше нуля"); scanner.nextLine(); } - + } calc.add(product); System.out.println("Добавить еще один товар? Введите \"Завершить\" чтобы узнать итоговую стоимость,\nили любой другой символ, чтобы добавить еще товары"); String userChoice = scanner.nextLine(); @@ -64,24 +68,14 @@ public static void main(String[] args) { } } - - - -class Product { - String productName; - double productPrice; -} - - - class Calculator { double total = 0; String shoppingList = new String(); String ending = new String(); public void add(Product product) { - total = total + product.productPrice; - shoppingList = shoppingList + product.productName + " " + product.productPrice + "\n"; + total += product.productPrice; + shoppingList += product.productName + " " + product.productPrice + "\n"; } @@ -112,4 +106,16 @@ public void findEnding(double endingPrice) { } +class Product { + String productName; + double productPrice; +} + + + + + + + +

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