diff --git a/src/main/java/Calculate.java b/src/main/java/Calculate.java new file mode 100644 index 000000000..1dda671e9 --- /dev/null +++ b/src/main/java/Calculate.java @@ -0,0 +1,95 @@ +import java.util.ArrayList; +import java.util.Scanner; + +public class Calculate { + + private final Scanner scanner = new Scanner(System.in); + + String check = ""; + Double countProducts = 0.0; + + public void calculate(String name) { + ArrayList products = new ArrayList<>(); + + System.out.println(name + " Напишите название блюда"); + while (true) { + String productName = scanner.next(); + + if (productName.equalsIgnoreCase("Завершить")) { + if (products.size() == 0) { + System.out.println("У " + name + " не добавлено ни одного блюда"); + } else { + System.out.println("Добавденные товары: "); + for (int j = 0; j < products.size(); j++) { + double temp2 = products.get(j).count; + int convertRub2 = (int) temp2; + System.out.println(products.get(j).name + " - " + products.get(j).count + " " + convertRubDeclination(convertRub2)); + } + } + break; + } + + if (checkString(productName)) { + System.out.println("Вы ввели некорректное название блюда"); + continue; + } + + System.out.println(name + " Напишите цену блюда"); + double productPrice; + while (true) { + try { + productPrice = Double.parseDouble(scanner.next()); + if (productPrice <= 0) { + System.out.println("Вы ввели минусовую цену. Попробуйте ще раз."); + continue; + } + break; + } catch (Exception e) { + System.out.println("Вы ввели некорректное значение цены. Попробуйте еще раз."); + } + + } + + products.add(new Product(productName, productPrice)); + double temp1 = productPrice; + int convertRub = (int) temp1; + System.out.println("Вы успешно добавили блюдо " + "\"" + productName + "\"" + " стоимостью " + productPrice + " " + convertRubDeclination(convertRub) + " в счет"); + countProducts += productPrice; + check += productName + " - " + productPrice + " " + convertRubDeclination(convertRub) + "\n"; + + System.out.println("Хотите ли вы добавить ещё один товар?\nЕсли нет, напишите - Завершить, если хотите продолжить, напишите название следующего блюда."); + + + } + + + } + + public String convertRubDeclination(int number) { + int lastDigit = number % 100 / 10; + if (lastDigit == 1) { + return "рублей"; + } + + switch (number % 10) { + case 1: + return "рубль"; + case 2: + case 3: + case 4: + return "рубля"; + default: + return "рублей"; + } + } + + private boolean checkString(String s) throws NumberFormatException { + try { + Integer.parseInt(s); + return true; + } catch (NumberFormatException e) { + return false; + } + } + +} diff --git a/src/main/java/Main.java b/src/main/java/Main.java index a9198c435..2f8178a9f 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,8 +1,5 @@ public class Main { - public static void main(String[] args) { - // ваш код начнется здесь - // вы не должны ограничиваться только классом Main и можете создавать свои классы по необходимости - System.out.println("Привет Мир"); + Person.start(); } } diff --git a/src/main/java/Person.java b/src/main/java/Person.java new file mode 100644 index 000000000..18a3e6fbf --- /dev/null +++ b/src/main/java/Person.java @@ -0,0 +1,46 @@ +import java.util.ArrayList; +import java.util.Scanner; + +public class Person { + + private static final Scanner scanner = new Scanner(System.in); + private static final ArrayList persons = new ArrayList<>(); + private static final Calculate calculate = new Calculate(); + + public static void start() { + System.out.println("На скольких человек необходимо разделить счёт? Введите число."); + + while (true) { + int countPeople; + + try { + countPeople = Integer.parseInt(scanner.next()); + } catch (Exception e) { + System.out.println("Вы ввели некорректное значение. Введите целое число."); + continue; + } + + if (countPeople == 1) { + System.out.println("Количество гостей должно быть больше одного"); + } else if (countPeople < 1) { + System.out.println("Количество гостей должно быть больше одного"); + } else { + for (int i = 0; i < countPeople; i++) { + persons.add("Гость_" + (i + 1)); + } + + System.out.println("Счет будет разделён на " + countPeople + " человек"); + for (int i = 0; i < persons.size(); i++) { + calculate.calculate(persons.get(i)); + } + double result = calculate.countProducts / persons.size(); + int rubText = (int) result; + System.out.printf("\nДобавленные блюда всех гостей:\n%s \nСумма к оплате с каждого гостя: %.2f %s", calculate.check, result, calculate.convertRubDeclination(rubText)); + break; + } + + } + } + + +} diff --git a/src/main/java/Product.java b/src/main/java/Product.java new file mode 100644 index 000000000..027a1972a --- /dev/null +++ b/src/main/java/Product.java @@ -0,0 +1,12 @@ +public class Product { + + String name; + Double count; + + public Product(String name, Double count) { + this.name = name; + this.count = count; + } + + +}