From 498b75a41bb9dd24a111b92296999fc7d6b88047 Mon Sep 17 00:00:00 2001 From: Kvadrokom Date: Fri, 9 Feb 2024 18:42:13 +0300 Subject: [PATCH 1/4] Firs version --- src/main/java/Main.java | 59 ++++++++++++++++++++++++++++++++++++-- src/main/java/Product.java | 15 ++++++++++ 2 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 src/main/java/Product.java diff --git a/src/main/java/Main.java b/src/main/java/Main.java index db9356a08..7483b2c38 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,6 +1,61 @@ +import java.util.Scanner; +import java.util.ArrayList; +import java.util.regex.Pattern; +import java.util.regex.Matcher; + public class Main { + static int numOfPersons; + static double check = 0; + static Scanner scanner = new Scanner(System.in); + static ArrayList products = new ArrayList(); + public static void main(String[] args) { - System.out.println("Hello world!"); + String s; + System.out.println("Введите количество человек:"); + numOfPersons = scanner.nextInt(); + + while (true) { + if (numOfPersons> 1) { + System.out.println("Введите блюдо и цену.\nЕсли добавили все позиции введите 'Завершить'"); + s = scanner.next(); + // System.out.println(s); + if (s.equalsIgnoreCase("завершить")) { + System.out.println("Добавленные товары:"); + for (Product product : products) { + product.print(); + } + if (check / numOfPersons> 1) + System.out.println(String.format("Итого: %.2f рубля с каждого", check / numOfPersons)); + else + System.out.println(String.format("Итого: %.2f рубль с каждого", check / numOfPersons)); + break; + } else { + s += scanner.nextLine(); + calculate(s); + } + } else { + System.out.println("Количество человек меньше 1. Это некорректное значение для подсчёта"); + System.out.println("Введите количество человек:"); + numOfPersons = scanner.nextInt(); + } + } + } + + private static void calculate(String s) { + Pattern pattern = Pattern.compile("[0-9]*\\.?[0-9]+$"); + Matcher matcher = pattern.matcher(s); + if (matcher.find()) { + check += Double.parseDouble(s.substring(matcher.start(), matcher.end())); + Product product = new Product(s.substring(0, matcher.start() - 1), Double.parseDouble(s.substring(matcher.start(), matcher.end()))); + product.printAdd(); + products.add(product); + } else { + System.out.println("Некоректный ввод!\nЦена указывается в формате: РУБЛИ.КОПЕЙКИ"); + } + // System.out.println(s.substring(matcher.start(), matcher.end())); + } -} \ No newline at end of file + + +} diff --git a/src/main/java/Product.java b/src/main/java/Product.java new file mode 100644 index 000000000..b4f2bf89b --- /dev/null +++ b/src/main/java/Product.java @@ -0,0 +1,15 @@ +public class Product { + private String name; + private double price; + Product(String name, double price) { + this.name = name; + this.price = price; + } + public void printAdd() { + System.out.println("Блюдо: " + name + " по цене " + price + " рублей" + " добавленно"); + } + + public void print() { + System.out.println("Блюдо: " + name + " по цене " + price + " рублей"); + } +} From c4ff33694e57f41798d8db7fbafd788e952b5383 Mon Sep 17 00:00:00 2001 From: Kvadrokom Date: 2024年2月10日 17:08:53 +0300 Subject: [PATCH 2/4] final version --- src/main/java/Main.java | 104 +++++++++++++++++++++++++------------ src/main/java/Product.java | 15 ------ 2 files changed, 71 insertions(+), 48 deletions(-) delete mode 100644 src/main/java/Product.java diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 7483b2c38..77fb9bd9e 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -9,53 +9,91 @@ public class Main { static double check = 0; static Scanner scanner = new Scanner(System.in); static ArrayList products = new ArrayList(); + static Calculate calc = new Calculate(); public static void main(String[] args) { String s; - System.out.println("Введите количество человек:"); - numOfPersons = scanner.nextInt(); + System.out.println("На скольких человек необходимо разделить счёт:"); + s = scanner.next(); while (true) { - if (numOfPersons> 1) { + if (s.matches("\\d+")) { + numOfPersons = Integer.parseInt(s); + break; + } else { + System.out.println("Это некорректное значение для подсчёта"); + System.out.println("Введите количество человек:"); + s = scanner.next(); + } + if (s.equalsIgnoreCase("завершить")) { + System.out.println("Программа завершена"); + return; + } + } + + while (true) { + if (s.equalsIgnoreCase("завершить")) { + System.out.println("Добавленные товары:"); + for (Product product : products) { + product.print(); + } + if (check / numOfPersons> 1) + System.out.println(String.format("Итого: %.2f рубля с каждого", check / numOfPersons)); + else + System.out.println(String.format("Итого: %.2f рубль с каждого", check / numOfPersons)); + return; + } + if (numOfPersons == 1) { + System.out.println("Количество человек равно 1.\nЭто некорректное значение для подсчёта"); + System.out.println("Введите количество человек:"); + s = scanner.next(); + } else if (numOfPersons < 1) { + System.out.println("Количество человек меньше 1.\nЭто некорректное значение для подсчёта"); + System.out.println("Введите количество человек:"); + s = scanner.next();; + } else { System.out.println("Введите блюдо и цену.\nЕсли добавили все позиции введите 'Завершить'"); s = scanner.next(); - // System.out.println(s); - if (s.equalsIgnoreCase("завершить")) { - System.out.println("Добавленные товары:"); - for (Product product : products) { - product.print(); - } - if (check / numOfPersons> 1) - System.out.println(String.format("Итого: %.2f рубля с каждого", check / numOfPersons)); - else - System.out.println(String.format("Итого: %.2f рубль с каждого", check / numOfPersons)); - break; - } else { + if (!s.equalsIgnoreCase("завершить")) { s += scanner.nextLine(); - calculate(s); + check += calc.calculate(s, products); } - } else { - System.out.println("Количество человек меньше 1. Это некорректное значение для подсчёта"); - System.out.println("Введите количество человек:"); - numOfPersons = scanner.nextInt(); } } } - private static void calculate(String s) { - Pattern pattern = Pattern.compile("[0-9]*\\.?[0-9]+$"); - Matcher matcher = pattern.matcher(s); - if (matcher.find()) { - check += Double.parseDouble(s.substring(matcher.start(), matcher.end())); - Product product = new Product(s.substring(0, matcher.start() - 1), Double.parseDouble(s.substring(matcher.start(), matcher.end()))); - product.printAdd(); - products.add(product); - } else { - System.out.println("Некоректный ввод!\nЦена указывается в формате: РУБЛИ.КОПЕЙКИ"); - } - // System.out.println(s.substring(matcher.start(), matcher.end())); - + public static class Calculate { + public static double calculate(String s, ArrayList products) { + Pattern pattern = Pattern.compile("[0-9]*\\.?[0-9]+$"); + Matcher matcher = pattern.matcher(s); + if (matcher.find()) + if (matcher.start()> 1) { + double check = 0; + check += Double.parseDouble(s.substring(matcher.start(), matcher.end())); + Product product = new Product(s.substring(0, matcher.start() - 1), Double.parseDouble(s.substring(matcher.start(), matcher.end()))); + product.printAdd(); + products.add(product); + return check; + } + System.out.println("Некоректный ввод!\nВвод осуществляется в формате:Название блюда РУБЛИ.КОПЕЙКИ"); + // System.out.println(s.substring(matcher.start(), matcher.end())); + return 0; + } } + public static class Product { + private String name; + private double price; + Product(String name, double price) { + this.name = name; + this.price = price; + } + public void printAdd() { + System.out.println("Блюдо: " + name + " по цене " + price + " рублей" + " добавленно"); + } + public void print() { + System.out.println("Блюдо: " + name + " по цене " + price + " рублей"); + } + } } diff --git a/src/main/java/Product.java b/src/main/java/Product.java deleted file mode 100644 index b4f2bf89b..000000000 --- a/src/main/java/Product.java +++ /dev/null @@ -1,15 +0,0 @@ -public class Product { - private String name; - private double price; - Product(String name, double price) { - this.name = name; - this.price = price; - } - public void printAdd() { - System.out.println("Блюдо: " + name + " по цене " + price + " рублей" + " добавленно"); - } - - public void print() { - System.out.println("Блюдо: " + name + " по цене " + price + " рублей"); - } -} From 839f2142be343a2ac836b41d579eda1e6e620436 Mon Sep 17 00:00:00 2001 From: Kvadrokom Date: 2024年2月11日 14:02:51 +0300 Subject: [PATCH 3/4] final version 2 --- src/main/java/Main.java | 48 ++++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 25 deletions(-) diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 77fb9bd9e..b6bb811da 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -17,22 +17,36 @@ public static void main(String[] args) { s = scanner.next(); while (true) { + if (s.equalsIgnoreCase("завершить")) { + System.out.println("Программа завершена"); + return; + } if (s.matches("\\d+")) { numOfPersons = Integer.parseInt(s); - break; } else { System.out.println("Это некорректное значение для подсчёта"); System.out.println("Введите количество человек:"); s = scanner.next(); } - if (s.equalsIgnoreCase("завершить")) { - System.out.println("Программа завершена"); - return; - } + if (numOfPersons == 1) { + System.out.println("Количество человек равно 1.\nЭто некорректное значение для подсчёта"); + System.out.println("Введите количество человек:"); + s = scanner.next(); + } else if (numOfPersons < 1) { + System.out.println("Количество человек меньше 1.\nЭто некорректное значение для подсчёта"); + System.out.println("Введите количество человек:"); + s = scanner.next();; + } else break; + } while (true) { - if (s.equalsIgnoreCase("завершить")) { + System.out.println("Введите блюдо и цену.\nЕсли добавили все позиции введите 'Завершить'"); + s = scanner.next(); + if (!s.equalsIgnoreCase("завершить")) { + s += scanner.nextLine(); + check += calc.calculate(s, products); + } else { System.out.println("Добавленные товары:"); for (Product product : products) { product.print(); @@ -43,28 +57,12 @@ public static void main(String[] args) { System.out.println(String.format("Итого: %.2f рубль с каждого", check / numOfPersons)); return; } - if (numOfPersons == 1) { - System.out.println("Количество человек равно 1.\nЭто некорректное значение для подсчёта"); - System.out.println("Введите количество человек:"); - s = scanner.next(); - } else if (numOfPersons < 1) { - System.out.println("Количество человек меньше 1.\nЭто некорректное значение для подсчёта"); - System.out.println("Введите количество человек:"); - s = scanner.next();; - } else { - System.out.println("Введите блюдо и цену.\nЕсли добавили все позиции введите 'Завершить'"); - s = scanner.next(); - if (!s.equalsIgnoreCase("завершить")) { - s += scanner.nextLine(); - check += calc.calculate(s, products); - } - } } } public static class Calculate { public static double calculate(String s, ArrayList products) { - Pattern pattern = Pattern.compile("[0-9]*\\.?[0-9]+$"); + Pattern pattern = Pattern.compile(" [0-9]*\\.?[0-9]+$"); Matcher matcher = pattern.matcher(s); if (matcher.find()) if (matcher.start()> 1) { @@ -89,11 +87,11 @@ public static class Product { this.price = price; } public void printAdd() { - System.out.println("Блюдо: " + name + " по цене " + price + " рублей" + " добавленно"); + System.out.println(String.format("Блюдо: %s по цене %.2f рублей добавленно", name, price)); } public void print() { - System.out.println("Блюдо: " + name + " по цене " + price + " рублей"); + System.out.println(String.format("Блюдо: %s по цене %.2f рублей", name, price)); } } } From b7780d5a7708f9b94e16bb363b8d004a290077fd Mon Sep 17 00:00:00 2001 From: Kvadrokom Date: 2024年2月11日 14:11:52 +0300 Subject: [PATCH 4/4] final version 2 --- src/main/java/Main.java | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/src/main/java/Main.java b/src/main/java/Main.java index b6bb811da..8d7e9624e 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -23,21 +23,20 @@ public static void main(String[] args) { } if (s.matches("\\d+")) { numOfPersons = Integer.parseInt(s); + if (numOfPersons == 1) { + System.out.println("Количество человек равно 1.\nЭто некорректное значение для подсчёта"); + System.out.println("Введите количество человек:"); + s = scanner.next(); + } else if (numOfPersons < 1) { + System.out.println("Количество человек меньше 1.\nЭто некорректное значение для подсчёта"); + System.out.println("Введите количество человек:"); + s = scanner.next();; + } else break; } else { System.out.println("Это некорректное значение для подсчёта"); System.out.println("Введите количество человек:"); s = scanner.next(); } - if (numOfPersons == 1) { - System.out.println("Количество человек равно 1.\nЭто некорректное значение для подсчёта"); - System.out.println("Введите количество человек:"); - s = scanner.next(); - } else if (numOfPersons < 1) { - System.out.println("Количество человек меньше 1.\nЭто некорректное значение для подсчёта"); - System.out.println("Введите количество человек:"); - s = scanner.next();; - } else break; - } while (true) {

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