From aa56515eb6a0aa59d2fdb3ed84b54f0212f24b51 Mon Sep 17 00:00:00 2001 From: kamberbattch Date: 2022年10月12日 14:52:49 +1000 Subject: [PATCH 1/3] =?UTF-8?q?=D0=98=D1=82=D0=BE=D0=B3=2012年10月22日?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/Main.java | 61 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 56 insertions(+), 5 deletions(-) diff --git a/src/main/java/Main.java b/src/main/java/Main.java index a9198c4..e6e3831 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,8 +1,59 @@ -public class Main { +import java.util.Scanner; +public class Main { + private static final Scanner scan = new Scanner(System.in); public static void main(String[] args) { - // ваш код начнется здесь - // вы не должны ограничиваться только классом Main и можете создавать свои классы по необходимости - System.out.println("Привет Мир"); + Calculator calc = new Calculator(); + calc.inputPersons(); + calc.addProducts(); + calc.payPeople(); + } + + public static class Calculator { + double totalPrice = 0; + String names = ""; + int persons = 0; + + public void inputPersons() { + while (true) { + System.out.println("How many persons?"); + persons = scan.nextInt(); + if (persons <= 1) { + System.out.println("Amount of persons must be> 1. Try again."); + continue; + } else { + break; + } + } + } + + public void addProducts() { + while (true) { + System.out.println("Product name: "); + String name = scan.next() + scan.nextLine(); + System.out.println("Price: "); + if (scan.hasNextDouble()) { + double price = scan.nextDouble(); + totalPrice += price; + names += name + "\n"; + } else { + System.out.println("Price must be double! Try again"); + scan.nextLine(); + continue; + } + + System.out.println("Product " + name + " successfully added! Enough? (yes - any symbol/no - n): "); + String answer = scan.next(); + if (answer.equalsIgnoreCase("n")) { + continue; + } else {break;} + } + } + + public void payPeople() { + System.out.println("You ordered following products:\n" + names + "for total price " + totalPrice); + String result = String.format("%.2f", totalPrice/persons); + System.out.println(String.format("Each of the " + persons + " people must pay for " + result)); + } } -} +} \ No newline at end of file From f217d68281703b4c0ac8901d8bf6d3b9844d9c5f Mon Sep 17 00:00:00 2001 From: kamberbattch Date: 2022年10月12日 14:55:13 +1000 Subject: [PATCH 2/3] =?UTF-8?q?=D0=98=D1=82=D0=BE=D0=B3=2012年10月22日+?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/.gitignore | 3 + .idea/codeStyles/Project.xml | 123 +++++++++++++++++++++++++++ .idea/codeStyles/codeStyleConfig.xml | 5 ++ .idea/compiler.xml | 6 ++ .idea/git_toolbox_prj.xml | 15 ++++ .idea/gradle.xml | 18 ++++ .idea/misc.xml | 10 +++ .idea/vcs.xml | 6 ++ 8 files changed, 186 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/codeStyles/Project.xml create mode 100644 .idea/codeStyles/codeStyleConfig.xml create mode 100644 .idea/compiler.xml create mode 100644 .idea/git_toolbox_prj.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/codeStyles/Project.xml b/.idea/codeStyles/Project.xml new file mode 100644 index 0000000..7643783 --- /dev/null +++ b/.idea/codeStyles/Project.xml @@ -0,0 +1,123 @@ + + + + + + + + + + \ 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..61a9130 --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/git_toolbox_prj.xml b/.idea/git_toolbox_prj.xml new file mode 100644 index 0000000..02b915b --- /dev/null +++ b/.idea/git_toolbox_prj.xml @@ -0,0 +1,15 @@ + + + + + + + \ 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 From 3f6996a9261fdabf9431273369269d1cb201b3f3 Mon Sep 17 00:00:00 2001 From: kamberbattch Date: 2022年10月13日 19:02:31 +1000 Subject: [PATCH 3/3] =?UTF-8?q?=D0=98=D1=82=D0=BE=D0=B3=2012年10月22日+?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/Main.java | 64 ++++++++++++++++++++++++++++------------- 1 file changed, 44 insertions(+), 20 deletions(-) diff --git a/src/main/java/Main.java b/src/main/java/Main.java index e6e3831..2de2ef6 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -3,45 +3,52 @@ public class Main { private static final Scanner scan = new Scanner(System.in); public static void main(String[] args) { - Calculator calc = new Calculator(); - calc.inputPersons(); - calc.addProducts(); - calc.payPeople(); + Calculator.inputPersons(); + Calculator.addProducts(); + Calculator.payPeople(); } public static class Calculator { - double totalPrice = 0; - String names = ""; - int persons = 0; + static double totalPrice = 0; + static String names = ""; + static int persons = 0; - public void inputPersons() { + public static void inputPersons() { while (true) { System.out.println("How many persons?"); - persons = scan.nextInt(); - if (persons <= 1) { - System.out.println("Amount of persons must be> 1. Try again."); - continue; - } else { + if (scan.hasNextInt()) { + persons = scan.nextInt(); + if (persons <= 1) { + System.out.println("Amount of persons must be integer> 1. Try again."); + continue; + } break; + } else { + System.out.println("Amount of persons must be integer> 1. Try again."); + scan.nextLine(); + continue; } } } - public void addProducts() { + public static void addProducts() { while (true) { System.out.println("Product name: "); String name = scan.next() + scan.nextLine(); System.out.println("Price: "); if (scan.hasNextDouble()) { double price = scan.nextDouble(); + if (price < 0) { + System.out.println("Price must be positive double! Try again"); + continue; + } totalPrice += price; names += name + "\n"; } else { - System.out.println("Price must be double! Try again"); + System.out.println("Price must be positive double! Try again"); scan.nextLine(); continue; } - System.out.println("Product " + name + " successfully added! Enough? (yes - any symbol/no - n): "); String answer = scan.next(); if (answer.equalsIgnoreCase("n")) { @@ -50,10 +57,27 @@ public void addProducts() { } } - public void payPeople() { - System.out.println("You ordered following products:\n" + names + "for total price " + totalPrice); - String result = String.format("%.2f", totalPrice/persons); - System.out.println(String.format("Each of the " + persons + " people must pay for " + result)); + public static void payPeople() { + int ruble = (int) Math.floor(totalPrice); + int payPeople = (int) Math.floor(totalPrice/persons); + System.out.println("Вы заказали следующие продукты:\n" + names + "на сумму: " + totalPrice + " " + Calculator.payPeoples(ruble)); + System.out.println("Каждый из " + persons + " человек заплатит по " + String.format("%.2f", totalPrice/persons) + " " + Calculator.payPeoples(payPeople)); + } + + static String payPeoples(int ruble) { + if (ruble % 100 / 10 == 1) { + return "рублей"; + } else { + switch (ruble % 10) { + case 1 : + return "рубль"; + case 2: + case 3: + case 4: + return "рубля"; + default: return "рублей"; + } + } } } } \ No newline at end of file

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