From d9a1ac4ac4492ba43c51f0885dfaa4eb3fc3cf1d Mon Sep 17 00:00:00 2001 From: Olesya Date: 2022年8月21日 23:46:36 +0300 Subject: [PATCH 1/4] =?UTF-8?q?=D0=92=D0=B0=D1=80=D0=B8=D0=B0=D0=BD=D1=82?= =?UTF-8?q?=20=D0=B4=D0=BB=D1=8F=20=D0=BF=D0=B5=D1=80=D0=B2=D0=BE=D0=B3?= =?UTF-8?q?=D0=BE=20=D1=80=D0=B5=D0=B2=D1=8C=D1=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/Main.java | 89 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 86 insertions(+), 3 deletions(-) diff --git a/src/main/java/Main.java b/src/main/java/Main.java index a9198c4..1fe2fdc 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,8 +1,91 @@ +import java.util.Scanner; + public class Main { public static void main(String[] args) { - // ваш код начнется здесь - // вы не должны ограничиваться только классом Main и можете создавать свои классы по необходимости - System.out.println("Привет Мир"); + int countGuest = 0; + Scanner scanner = new Scanner(System.in); + System.out.println("На скольких человек необходимо рзделить счет?"); + while(true) { + countGuest = scanner.nextInt(); + if (countGuest <= 1) { + System.out.println("Ошибка. Введите корректное число гостей:"); + } + if (countGuest> 1) { + break; + } + } + Calculator calculator = new Calculator(); + calculator.addProducts(); + System.out.println(calculator.products); + double priceForPerson = calculator.calcPriceForPerson(countGuest); + String rubleAddition = calculator.getRubleAddition(priceForPerson); + String conclusion = "Каждый должен заплатить по %.2f " + rubleAddition + "."; + System.out.println(String.format(conclusion, priceForPerson)); + } +} + +class Product { + String name; + double price; +} + +class Calculator { + + double total; + String products = "Добавленные продукты:\n"; + Scanner scanner = new Scanner(System.in); + + void addProducts() { + while (true) { + Product product = new Product(); + System.out.println("Введите наазвание товара:"); + product.name = scanner.next(); + while (true) { + + System.out.println("Введите стоимость товара в формате ('рубли.копейки' [10.45, 11.40]):"); + String strPriceProduct = scanner.next(); + try { + product.price = Double.parseDouble(strPriceProduct); + } catch (NumberFormatException error) { + System.out.println("Стоимость указана в некорректном формате."); + continue; + } + if (product.price> 0) { + break; + } + System.out.println("Ошибка. Значение стоимости введено некорректно."); + } + System.out.println("Товар уcпешно добавлен. Хотите добавить еще товар?"); + total = total + product.price; + products = products + product.name + "\n"; + String NoOrYes = scanner.next(); + if (NoOrYes.equalsIgnoreCase("Завершить")) { + break; + } + } + } + + double calcPriceForPerson(int countGuest) { + return total / (double) countGuest; + } + + String getRubleAddition(double moneyValue) { + int preLastDigit = (int)moneyValue % 100 / 10; + if (preLastDigit == 1) { + return "рублей"; + } + int LastDigit = (int)moneyValue % 10; + switch (LastDigit) { + case 1: + return "рубль"; + case 2: + case 3: + case 4: + return "рубля"; + default: + return "рублей"; + + } } } From e4f8e373c15acb77c8ddbdf90bbb09348eed0eda Mon Sep 17 00:00:00 2001 From: Olesya Date: 2022年8月22日 13:20:46 +0300 Subject: [PATCH 2/4] =?UTF-8?q?=D0=92=D0=B0=D1=80=D0=B8=D0=B0=D0=BD=D1=82?= =?UTF-8?q?=20=D0=B4=D0=BB=D1=8F=20=D0=BF=D0=B5=D1=80=D0=B2=D0=BE=D0=B3?= =?UTF-8?q?=D0=BE(2)=20=D1=80=D0=B5=D0=B2=D1=8C=D1=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/Main.java | 64 +++++++++++++++++++++++++++++------------ 1 file changed, 45 insertions(+), 19 deletions(-) diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 1fe2fdc..dfd7b26 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,27 +1,11 @@ +import java.util.InputMismatchException; import java.util.Scanner; public class Main { public static void main(String[] args) { - int countGuest = 0; - Scanner scanner = new Scanner(System.in); - System.out.println("На скольких человек необходимо рзделить счет?"); - while(true) { - countGuest = scanner.nextInt(); - if (countGuest <= 1) { - System.out.println("Ошибка. Введите корректное число гостей:"); - } - if (countGuest> 1) { - break; - } - } - Calculator calculator = new Calculator(); - calculator.addProducts(); - System.out.println(calculator.products); - double priceForPerson = calculator.calcPriceForPerson(countGuest); - String rubleAddition = calculator.getRubleAddition(priceForPerson); - String conclusion = "Каждый должен заплатить по %.2f " + rubleAddition + "."; - System.out.println(String.format(conclusion, priceForPerson)); + BillCalculator billCalculator = new BillCalculator(); + billCalculator.startBillCalculator(); } } @@ -44,6 +28,7 @@ void addProducts() { while (true) { System.out.println("Введите стоимость товара в формате ('рубли.копейки' [10.45, 11.40]):"); + String strPriceProduct = scanner.next(); try { product.price = Double.parseDouble(strPriceProduct); @@ -89,3 +74,44 @@ String getRubleAddition(double moneyValue) { } } } +class Guests { + int countGuest = 0; + Scanner scanner = new Scanner(System.in); + + int defineCountGuests() { + while (true) { + System.out.println("На скольких человек необходимо рзделить счет?"); + try { + countGuest = scanner.nextInt(); + } catch (InputMismatchException er) { + System.out.println("Некорректное значение"); + scanner.nextLine(); + continue; + } + + if (countGuest <= 1) { + System.out.println("Ошибка. Введите корректное число гостей:"); + continue; + } + return countGuest; + + } + } +} + +class BillCalculator { + void startBillCalculator() { + Guests guests = new Guests(); + int countGuests = guests.defineCountGuests(); + + Calculator calculator = new Calculator(); + calculator.addProducts(); + + double priceForPerson = calculator.calcPriceForPerson(countGuests); + String rubleAddition = calculator.getRubleAddition(priceForPerson); + String conclusion = "Каждый должен заплатить по %.2f " + rubleAddition + "."; + + System.out.println(calculator.products); + System.out.println(String.format(conclusion, priceForPerson)); + } +} From 16e09188c9c25bb908cb06d71c6ed8fc1a480660 Mon Sep 17 00:00:00 2001 From: Olesya Date: 2022年8月22日 14:31:19 +0300 Subject: [PATCH 3/4] =?UTF-8?q?=D0=92=D0=B0=D1=80=D0=B8=D0=B0=D0=BD=D1=82?= =?UTF-8?q?=20=D0=B4=D0=BB=D1=8F=20=D0=BF=D0=B5=D1=80=D0=B2=D0=BE=D0=B3?= =?UTF-8?q?=D0=BE(2)=20=D1=80=D0=B5=D0=B2=D1=8C=D1=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/Main.java | 97 ----------------------------------------- 1 file changed, 97 deletions(-) diff --git a/src/main/java/Main.java b/src/main/java/Main.java index dfd7b26..27657ec 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -14,104 +14,7 @@ class Product { double price; } -class Calculator { - double total; - String products = "Добавленные продукты:\n"; - Scanner scanner = new Scanner(System.in); - void addProducts() { - while (true) { - Product product = new Product(); - System.out.println("Введите наазвание товара:"); - product.name = scanner.next(); - while (true) { - System.out.println("Введите стоимость товара в формате ('рубли.копейки' [10.45, 11.40]):"); - String strPriceProduct = scanner.next(); - try { - product.price = Double.parseDouble(strPriceProduct); - } catch (NumberFormatException error) { - System.out.println("Стоимость указана в некорректном формате."); - continue; - } - if (product.price> 0) { - break; - } - System.out.println("Ошибка. Значение стоимости введено некорректно."); - } - System.out.println("Товар уcпешно добавлен. Хотите добавить еще товар?"); - total = total + product.price; - products = products + product.name + "\n"; - String NoOrYes = scanner.next(); - if (NoOrYes.equalsIgnoreCase("Завершить")) { - break; - } - } - } - - double calcPriceForPerson(int countGuest) { - return total / (double) countGuest; - } - - String getRubleAddition(double moneyValue) { - int preLastDigit = (int)moneyValue % 100 / 10; - if (preLastDigit == 1) { - return "рублей"; - } - int LastDigit = (int)moneyValue % 10; - switch (LastDigit) { - case 1: - return "рубль"; - case 2: - case 3: - case 4: - return "рубля"; - default: - return "рублей"; - - } - } -} -class Guests { - int countGuest = 0; - Scanner scanner = new Scanner(System.in); - - int defineCountGuests() { - while (true) { - System.out.println("На скольких человек необходимо рзделить счет?"); - try { - countGuest = scanner.nextInt(); - } catch (InputMismatchException er) { - System.out.println("Некорректное значение"); - scanner.nextLine(); - continue; - } - - if (countGuest <= 1) { - System.out.println("Ошибка. Введите корректное число гостей:"); - continue; - } - return countGuest; - - } - } -} - -class BillCalculator { - void startBillCalculator() { - Guests guests = new Guests(); - int countGuests = guests.defineCountGuests(); - - Calculator calculator = new Calculator(); - calculator.addProducts(); - - double priceForPerson = calculator.calcPriceForPerson(countGuests); - String rubleAddition = calculator.getRubleAddition(priceForPerson); - String conclusion = "Каждый должен заплатить по %.2f " + rubleAddition + "."; - - System.out.println(calculator.products); - System.out.println(String.format(conclusion, priceForPerson)); - } -} From c83c4bb56a66ab66fa9bbe63812a097a486cb2c3 Mon Sep 17 00:00:00 2001 From: Olesya Date: 2022年8月22日 14:31:55 +0300 Subject: [PATCH 4/4] =?UTF-8?q?=D0=92=D0=B0=D1=80=D0=B8=D0=B0=D0=BD=D1=82?= =?UTF-8?q?=20=D0=B4=D0=BB=D1=8F=20=D0=BF=D0=B5=D1=80=D0=B2=D0=BE=D0=B3?= =?UTF-8?q?=D0=BE(2)=20=D1=80=D0=B5=D0=B2=D1=8C=D1=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/BillCalculator.java | 16 ++++++++ src/main/java/Calculator.java | 61 +++++++++++++++++++++++++++++++ src/main/java/Guests.java | 27 ++++++++++++++ 3 files changed, 104 insertions(+) create mode 100644 src/main/java/BillCalculator.java create mode 100644 src/main/java/Calculator.java create mode 100644 src/main/java/Guests.java diff --git a/src/main/java/BillCalculator.java b/src/main/java/BillCalculator.java new file mode 100644 index 0000000..98d4f3c --- /dev/null +++ b/src/main/java/BillCalculator.java @@ -0,0 +1,16 @@ +public class BillCalculator { + void startBillCalculator() { + Guests guests = new Guests(); + int countGuests = guests.defineCountGuests(); + + Calculator calculator = new Calculator(); + calculator.addProducts(); + + double priceForPerson = calculator.calcPriceForPerson(countGuests); + String rubleAddition = calculator.getRubleAddition(priceForPerson); + String conclusion = "Каждый должен заплатить по %.2f " + rubleAddition + "."; + + System.out.println(calculator.products); + System.out.println(String.format(conclusion, priceForPerson)); + } +} diff --git a/src/main/java/Calculator.java b/src/main/java/Calculator.java new file mode 100644 index 0000000..e78526e --- /dev/null +++ b/src/main/java/Calculator.java @@ -0,0 +1,61 @@ +import java.util.Scanner; + +public class Calculator { + double total; + String products = "Добавленные продукты:\n"; + Scanner scanner = new Scanner(System.in); + + void addProducts() { + while (true) { + Product product = new Product(); + System.out.println("Введите наазвание товара:"); + product.name = scanner.next(); + while (true) { + + System.out.println("Введите стоимость товара в формате ('рубли.копейки' [10.45, 11.40]):"); + + String strPriceProduct = scanner.next(); + try { + product.price = Double.parseDouble(strPriceProduct); + } catch (NumberFormatException error) { + System.out.println("Стоимость указана в некорректном формате."); + continue; + } + if (product.price> 0) { + break; + } + System.out.println("Ошибка. Значение стоимости введено некорректно."); + } + System.out.println("Товар уcпешно добавлен. Хотите добавить еще товар?"); + total = total + product.price; + products = products + product.name + "\n"; + String NoOrYes = scanner.next(); + if (NoOrYes.equalsIgnoreCase("Завершить")) { + break; + } + } + } + + double calcPriceForPerson(int countGuest) { + return total / (double) countGuest; + } + + String getRubleAddition(double moneyValue) { + int preLastDigit = (int)moneyValue % 100 / 10; + if (preLastDigit == 1) { + return "рублей"; + } + int LastDigit = (int)moneyValue % 10; + switch (LastDigit) { + case 1: + return "рубль"; + case 2: + case 3: + case 4: + return "рубля"; + default: + return "рублей"; + + } + } +} diff --git a/src/main/java/Guests.java b/src/main/java/Guests.java new file mode 100644 index 0000000..f1164a8 --- /dev/null +++ b/src/main/java/Guests.java @@ -0,0 +1,27 @@ +import java.util.InputMismatchException; +import java.util.Scanner; + +public class Guests { + int countGuest = 0; + Scanner scanner = new Scanner(System.in); + + int defineCountGuests() { + while (true) { + System.out.println("На скольких человек необходимо рзделить счет?"); + try { + countGuest = scanner.nextInt(); + } catch (InputMismatchException er) { + System.out.println("Некорректное значение"); + scanner.nextLine(); + continue; + } + + if (countGuest <= 1) { + System.out.println("Ошибка. Введите корректное число гостей:"); + continue; + } + return countGuest; + + } + } +}

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