From 0bb887ab373c25ed910b7732edb7347befd4f05c Mon Sep 17 00:00:00 2001 From: kotzimaru Date: 2022年8月18日 21:43:09 +0300 Subject: [PATCH 1/5] Release --- src/main/java/Calculator.java | 77 +++++++++++++++++++++++++++++++++++ src/main/java/Main.java | 38 +++++++++++++++-- src/main/java/Product.java | 34 ++++++++++++++++ 3 files changed, 146 insertions(+), 3 deletions(-) create mode 100644 src/main/java/Calculator.java create mode 100644 src/main/java/Product.java diff --git a/src/main/java/Calculator.java b/src/main/java/Calculator.java new file mode 100644 index 0000000..0c87d67 --- /dev/null +++ b/src/main/java/Calculator.java @@ -0,0 +1,77 @@ +import java.text.DecimalFormat; +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; + +public class Calculator { + + private int countPeople; + private double sum; + + private List goods = new ArrayList(); + + Calculator(int countPeople) { + + this.countPeople = countPeople; + + } + + public void total() { + + DecimalFormat decimalFormat = new DecimalFormat("#.##"); + + System.out.println("Общая сумма " + decimalFormat.format(sum)+ " " + Product.GetRubleAddition(sum)); + System.out.println("С человека " + decimalFormat.format(sum / countPeople) + " " + + Product.GetRubleAddition(sum / countPeople)); + + } + + public void start(Scanner sc) { + + final String REGEX = "\\d+.\\d{2}"; + + while (true) { + System.out.println("Введите название товара"); + + String name = sc.next(); + String input; + System.out.println("Введите цену товара"); + while (true) { + input = sc.next(); + if (input.matches(REGEX)) { + break; + } else { + System.out.println("Неверный ввод, введите цену в формате 00.00"); + } + + } + Double price = Double.parseDouble(input); + sum += price; + Product product = new Product(name, price); + goods.add(product); + System.out.println("Товар добавлен, добавить еще товар?"); + String check = sc.next(); + if (check.equalsIgnoreCase("Завершить")) { + + break; + + } + + + } + } + + public void result() { + System.out.println("Добавленные товары: "); + for (Product product : goods + ) { + product.info(); + + + } + + + } + + +} diff --git a/src/main/java/Main.java b/src/main/java/Main.java index a9198c4..a50ede3 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,8 +1,40 @@ +import java.util.Locale; +import java.util.Scanner; + public class Main { public static void main(String[] args) { - // ваш код начнется здесь - // вы не должны ограничиваться только классом Main и можете создавать свои классы по необходимости - System.out.println("Привет Мир"); + final String DIGITS = "\\d+"; + System.out.println("Привет, сколько вас человек?"); + + int countPeople = 0; + Scanner sc = new Scanner(System.in).useLocale(Locale.US); + while (true) { + + String countPeople1 = sc.nextLine(); + + if (countPeople1.matches(DIGITS)) { + countPeople = Integer.parseInt(countPeople1); + if (countPeople <= 1) { + System.out.println("Недостаточно человек"); + } else if (countPeople>= 2) { + + //double sum = 0; + Calculator calculator = new Calculator(countPeople); + calculator.start(sc); + calculator.result(); + calculator.total(); + break; + } + + } + else { + System.out.println("Доступен ввод только числа"); + } + + + + + } } } diff --git a/src/main/java/Product.java b/src/main/java/Product.java new file mode 100644 index 0000000..ce131bc --- /dev/null +++ b/src/main/java/Product.java @@ -0,0 +1,34 @@ +public class Product { + String name; + double price; + Product(String name, double price){ + this.name = name; + this.price = price; + } + + public void info(){ + System.out.println("Товар "+ name + " цена "+ price + " " + GetRubleAddition(price)); + } + public static String GetRubleAddition(Double value) { + + int num = value.intValue(); + + int preLastDigit = num % 100 / 10; + if (preLastDigit == 1) { + return "рублей"; + } + + switch (num % 10) { + case 1: + return "рубль"; + case 2: + case 3: + case 4: + return "рубля"; + default: + return "рублей"; + } + } + +} + From 4daf8e58bb0d510ad7d93505c2936d5528d73b9f Mon Sep 17 00:00:00 2001 From: kotzimaru Date: 2022年8月18日 21:48:45 +0300 Subject: [PATCH 2/5] Release 2 --- .idea/.gitignore | 3 +++ .idea/codeStyles/Project.xml | 10 ++++++++++ .idea/codeStyles/codeStyleConfig.xml | 5 +++++ .idea/compiler.xml | 6 ++++++ .idea/encodings.xml | 6 ++++++ .idea/gradle.xml | 17 +++++++++++++++++ .idea/misc.xml | 5 +++++ .idea/vcs.xml | 7 +++++++ src/main/java/Calculator.java | 6 +++--- src/main/java/Main.java | 2 +- src/main/java/Product.java | 15 +++++---------- 11 files changed, 68 insertions(+), 14 deletions(-) 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/encodings.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..1bec35e --- /dev/null +++ b/.idea/codeStyles/Project.xml @@ -0,0 +1,10 @@ + + + + + + + + \ 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/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..97626ba --- /dev/null +++ b/.idea/encodings.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..611e7c8 --- /dev/null +++ b/.idea/gradle.xml @@ -0,0 +1,17 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..315e3a4 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..8306744 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,7 @@ + + + +
    +
      + + \ No newline at end of file diff --git a/src/main/java/Calculator.java b/src/main/java/Calculator.java index 0c87d67..6e23b58 100644 --- a/src/main/java/Calculator.java +++ b/src/main/java/Calculator.java @@ -5,10 +5,10 @@ public class Calculator { - private int countPeople; + private final int countPeople; private double sum; - private List goods = new ArrayList(); + private final List goods = new ArrayList(); Calculator(int countPeople) { @@ -45,7 +45,7 @@ public void start(Scanner sc) { } } - Double price = Double.parseDouble(input); + double price = Double.parseDouble(input); sum += price; Product product = new Product(name, price); goods.add(product); diff --git a/src/main/java/Main.java b/src/main/java/Main.java index a50ede3..0e7d3cf 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -7,7 +7,7 @@ public static void main(String[] args) { final String DIGITS = "\\d+"; System.out.println("Привет, сколько вас человек?"); - int countPeople = 0; + int countPeople; Scanner sc = new Scanner(System.in).useLocale(Locale.US); while (true) { diff --git a/src/main/java/Product.java b/src/main/java/Product.java index ce131bc..9f55de6 100644 --- a/src/main/java/Product.java +++ b/src/main/java/Product.java @@ -18,16 +18,11 @@ public static String GetRubleAddition(Double value) { return "рублей"; } - switch (num % 10) { - case 1: - return "рубль"; - case 2: - case 3: - case 4: - return "рубля"; - default: - return "рублей"; - } + return switch (num % 10) { + case 1 -> "рубль"; + case 2, 3, 4 -> "рубля"; + default -> "рублей"; + }; } } From 06809bfad53b99b131e9ca149b7263686fd418cc Mon Sep 17 00:00:00 2001 From: kotzimaru Date: 2022年8月20日 13:48:33 +0300 Subject: [PATCH 3/5] Release 2 --- .idea/compiler.xml | 2 +- .idea/misc.xml | 2 +- build.gradle | 3 ++- src/main/java/Product.java | 24 +++++++++++++++--------- 4 files changed, 19 insertions(+), 12 deletions(-) diff --git a/.idea/compiler.xml b/.idea/compiler.xml index 61a9130..ac216bc 100644 --- a/.idea/compiler.xml +++ b/.idea/compiler.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index 315e3a4..43c9188 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,5 +1,5 @@ - + \ No newline at end of file diff --git a/build.gradle b/build.gradle index 1b1d075..d45c8b6 100644 --- a/build.gradle +++ b/build.gradle @@ -1,6 +1,6 @@ apply plugin: 'java' -sourceCompatibility = 1.8 +sourceCompatibility = 14 version = '1.0' repositories { @@ -8,3 +8,4 @@ repositories { dependencies { } +targetCompatibility = JavaVersion.VERSION_14 diff --git a/src/main/java/Product.java b/src/main/java/Product.java index 9f55de6..66888ef 100644 --- a/src/main/java/Product.java +++ b/src/main/java/Product.java @@ -1,14 +1,16 @@ public class Product { String name; double price; - Product(String name, double price){ + + Product(String name, double price) { this.name = name; this.price = price; } - public void info(){ - System.out.println("Товар "+ name + " цена "+ price + " " + GetRubleAddition(price)); + public void info() { + System.out.println("Товар " + name + " цена " + price + " " + GetRubleAddition(price)); } + public static String GetRubleAddition(Double value) { int num = value.intValue(); @@ -18,12 +20,16 @@ public static String GetRubleAddition(Double value) { return "рублей"; } - return switch (num % 10) { - case 1 -> "рубль"; - case 2, 3, 4 -> "рубля"; - default -> "рублей"; - }; + switch (num % 10) { + case 1: + return "рубль"; + case 2: + case 3: + case 4: + return "рубля"; + default: + return "рублей"; + } } - } From 686952e32c1d0d20a8ad2056763924e84c709cac Mon Sep 17 00:00:00 2001 From: kotzimaru Date: 2022年8月20日 15:58:30 +0300 Subject: [PATCH 4/5] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB?= =?UTF-8?q?=20hasNextDouble?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/Calculator.java | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/main/java/Calculator.java b/src/main/java/Calculator.java index 6e23b58..1a30061 100644 --- a/src/main/java/Calculator.java +++ b/src/main/java/Calculator.java @@ -1,12 +1,14 @@ import java.text.DecimalFormat; import java.util.ArrayList; import java.util.List; +import java.util.Locale; import java.util.Scanner; public class Calculator { private final int countPeople; private double sum; + private double price; private final List goods = new ArrayList(); @@ -28,25 +30,30 @@ public void total() { public void start(Scanner sc) { - final String REGEX = "\\d+.\\d{2}"; - while (true) { System.out.println("Введите название товара"); String name = sc.next(); - String input; + System.out.println("Введите цену товара"); + while (true) { - input = sc.next(); - if (input.matches(REGEX)) { + + Scanner scDouble = new Scanner(System.in).useLocale(Locale.US); + + if (scDouble.hasNextDouble()){ + + price = scDouble.nextDouble(); + sum += price; break; - } else { + + }else{ System.out.println("Неверный ввод, введите цену в формате 00.00"); } + } - double price = Double.parseDouble(input); - sum += price; + Product product = new Product(name, price); goods.add(product); System.out.println("Товар добавлен, добавить еще товар?"); From e1a6997ee1e7edf3749ec626a0222e979458af88 Mon Sep 17 00:00:00 2001 From: kotzimaru Date: 2022年8月20日 16:01:03 +0300 Subject: [PATCH 5/5] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB?= =?UTF-8?q?=20hasNextDouble?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/Calculator.java | 2 +- src/main/java/Main.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/Calculator.java b/src/main/java/Calculator.java index 1a30061..e03e4d4 100644 --- a/src/main/java/Calculator.java +++ b/src/main/java/Calculator.java @@ -56,7 +56,7 @@ public void start(Scanner sc) { Product product = new Product(name, price); goods.add(product); - System.out.println("Товар добавлен, добавить еще товар?"); + System.out.println("Товар добавлен, введи Завершить, чтобы закончить ввод"); String check = sc.next(); if (check.equalsIgnoreCase("Завершить")) { diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 0e7d3cf..6df6a05 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -17,7 +17,7 @@ public static void main(String[] args) { countPeople = Integer.parseInt(countPeople1); if (countPeople <= 1) { System.out.println("Недостаточно человек"); - } else if (countPeople>= 2) { + } else { //double sum = 0; Calculator calculator = new Calculator(countPeople);

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