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/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 diff --git a/src/main/java/CaseEnding.java b/src/main/java/CaseEnding.java new file mode 100644 index 0000000..6770b0b --- /dev/null +++ b/src/main/java/CaseEnding.java @@ -0,0 +1,20 @@ +public class CaseEnding { + + static String roubleCase = " рублей"; + static double remainder; + + static String setEnding(double price) { + if (Math.floor(price) >= 11 && Math.floor(price) <= 14) { + return roubleCase = " рублей"; + } else { + remainder = Math.floor(price) % 10; + if (remainder == 1) { + return roubleCase = " рубль"; + } else if (remainder >= 2 && remainder <= 4) { + return roubleCase = " рубля"; + } else { + return roubleCase = " рублей"; + } + } + } +} diff --git a/src/main/java/Item.java b/src/main/java/Item.java new file mode 100644 index 0000000..6503ddf --- /dev/null +++ b/src/main/java/Item.java @@ -0,0 +1,53 @@ +import java.util.Scanner; + +public class Item { + + static String name; + static double price = 0; + + static String values = ""; + static double sum = 0; + + static void calculate() { + while (true) { + + Scanner scanner = new Scanner(System.in); + + System.out.println("\nЕсли вы хотите добавить блюдо в калькулятор, введите наименование блюда:"); + System.out.println("Если вы не хотите добавлять блюдо в калькулятор, введите «Завершить»:"); + + name = scanner.nextLine(); + + if (name.equalsIgnoreCase("завершить")) { + System.out.println("Выход из калькулятора (вы ввели «Завершить»)."); + break; + } else { + System.out.println("Введите стоимость блюда в формате [рубли.копейки]:"); + + String stringPrice = scanner.nextLine(); + + try { + price = Double.parseDouble(stringPrice); + } catch (NumberFormatException e) { + System.out.println("Введите численное значение."); + price = 0; + } + + if (price > 0) { + values += name + " " + price + ";\n"; + sum += price; + + System.out.println("Блюдо " + name + " стоимостью " + price + CaseEnding.setEnding(price) + " успешно добавлено!"); + System.out.println("Текущая сумма блюд: " + sum + CaseEnding.setEnding(sum) + "."); + } else { + System.out.println("Введите корректную стоимость блюда больше нуля."); + + } + } + } + + System.out.println("\nДобавленные блюда: "); + System.out.println(values); + System.out.println("Общая сумма блюд: " + sum + CaseEnding.setEnding(sum) + "."); + } +} diff --git a/src/main/java/Main.java b/src/main/java/Main.java index a9198c4..b5c2d6e 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,8 +1,47 @@ +import java.util.Scanner; + public class Main { public static void main(String[] args) { - // ваш код начнется здесь - // вы не должны ограничиваться только классом Main и можете создавать свои классы по необходимости - System.out.println("Привет Мир"); + + Scanner scanner = new Scanner(System.in); + + while (true) { + + printMenu(); + + String quantityOfPersons = scanner.nextLine(); + int number = -1; + + try { + number = Integer.parseInt(quantityOfPersons); + } catch (NumberFormatException e) { + System.out.println("Вы ввели что-то не то... Давайте ещё раз."); + } + + if (number == 1) { + System.out.println("Тебе грустно и одиноко, нечем заняться? Занимайся Джаббой!"); + } else if (number > 1) { + System.out.println("Итак, вас " + quantityOfPersons + ". И вы харчевали..."); + Item.calculate(); + double pricePerPerson = Item.sum / number; + System.out.println("\nКаждый из посетителей должен заплатить " + String.format("%.2f", pricePerPerson) + CaseEnding.setEnding(pricePerPerson) + "."); + System.out.println("Вычисления завершены. Выход."); + break; + } else if (number == 0) { + System.out.println("Выход, потому что кое-кто нажал ноль."); + break; + } else { + System.out.println("Введите положительное число гостей."); + } + + } + } + + public static void printMenu() { + System.out.println("\nДобрый день!"); + System.out.println("На скольких человек вы хотите разделить счёт?"); + System.out.println("Пожалуйста, введите количество гостей - от 1 и выше."); + System.out.println("Или введите 0 для того, чтобы выйти из программы."); } }