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/src/main/java/AskInfo.java b/src/main/java/AskInfo.java new file mode 100644 index 0000000..73bcdfb --- /dev/null +++ b/src/main/java/AskInfo.java @@ -0,0 +1,73 @@ +import java.util.Scanner; + +public class AskInfo { + int peopleAmount; + double checkCount; + StringBuilder productList; + + AskInfo() { + productList = new StringBuilder(); + } + + void askPeopleAmount() { + Scanner scanner = new Scanner(System.in); + boolean exceptionHappened; + while (true) { + exceptionHappened = false; + System.out.println("Введите количество гостей:"); + try { + peopleAmount = scanner.nextInt(); + } + catch (java.util.InputMismatchException e) { + System.out.println("Ошибка ввода, попробуйте еще раз."); + scanner.nextLine(); + exceptionHappened = true; + } + + if (!exceptionHappened && peopleAmount <= 1) { + System.out.println("Ошибка ввода количества гостей. Количество гостей должно быть больше 1."); + } else if (!exceptionHappened){ + break; + } + } + } + + void addProductsNameAndPrice() { + String productName; + double price = 0; + boolean exceptionHappened; + Scanner scanner = new Scanner(System.in); + + while (true) { + System.out.println("Введите название товара:\nЕсли хотите вывести счет введите: Завершить."); + productName = scanner.next(); + + if (!productName.equalsIgnoreCase("Завершить")) { + productList.append(productName).append("\n"); + } + else { + break; + } + + while (true) { + exceptionHappened = false; + System.out.println("Введите цену товара:"); + try { + price = scanner.nextDouble(); + } + catch (java.util.InputMismatchException e) { + System.out.println("Ошибка ввода. Возможно неверный формат ввода. Повторите ввод."); + scanner.nextLine(); + exceptionHappened = true; + } + if (price>= 0 && !exceptionHappened){ + checkCount += price; + break; + } + else if (!exceptionHappened) { + System.out.println("Ошибка ввода. Цена не может быть меньше нуля. Повторите ввод."); + } + } + } + } +} diff --git a/src/main/java/Calculator.java b/src/main/java/Calculator.java new file mode 100644 index 0000000..6515579 --- /dev/null +++ b/src/main/java/Calculator.java @@ -0,0 +1,13 @@ +public class Calculator { + double value; + int divider; + + Calculator(double value, int divider) { + this.value = value; + this.divider = divider; + } + + double division() { + return value / divider; + } +} \ No newline at end of file diff --git a/src/main/java/FormatToRubles.java b/src/main/java/FormatToRubles.java new file mode 100644 index 0000000..874d5cf --- /dev/null +++ b/src/main/java/FormatToRubles.java @@ -0,0 +1,28 @@ +public class FormatToRubles { + double rubbles; + + FormatToRubles(double rubbles) { + this.rubbles = rubbles; + } + + String formatRubbles() { + int rubleOrRubles; + rubleOrRubles = (int) Math.floor(rubbles); + String checkMessage = null; + + if (rubleOrRubles <= 1) { + checkMessage = "С человека по %.2f рубль"; + checkMessage = String.format(checkMessage,rubbles ); + } else if (rubleOrRubles < 9) { + checkMessage = "С человека по %.2f рубля"; + checkMessage = String.format(checkMessage,rubbles ); + } else if (rubleOrRubles>= 10) { + checkMessage = "С человека по %.2f рублей"; + checkMessage = String.format(checkMessage,rubbles ); + } + return checkMessage; + } +} + + + diff --git a/src/main/java/Main.java b/src/main/java/Main.java index a9198c4..4ded2fe 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,8 +1,25 @@ public class Main { public static void main(String[] args) { - // ваш код начнется здесь - // вы не должны ограничиваться только классом Main и можете создавать свои классы по необходимости - System.out.println("Привет Мир"); + // Собираю вводные данные + AskInfo askPeople = new AskInfo(); + askPeople.askPeopleAmount(); + askPeople.addProductsNameAndPrice(); + + //Произвожу вычисления + Calculator calculator = new Calculator(askPeople.checkCount, askPeople.peopleAmount); + double calculatedCheck = calculator.division(); + + //Вывожу список товаров + ShowProductList showProductList = new ShowProductList(askPeople.productList); + showProductList.printProductList(); + + //Форматирую окончание слова рубль для сообщения + FormatToRubles formatToRubles = new FormatToRubles(calculatedCheck); + String checkMessage = formatToRubles.formatRubbles(); + + //Вывожу сколько должен заплатить каждый гость + ShowFinalCheck showFinalCheck = new ShowFinalCheck(checkMessage); + showFinalCheck.printFinalCheck(); } } diff --git a/src/main/java/ShowFinalCheck.java b/src/main/java/ShowFinalCheck.java new file mode 100644 index 0000000..c9323f2 --- /dev/null +++ b/src/main/java/ShowFinalCheck.java @@ -0,0 +1,12 @@ +public class ShowFinalCheck { + String priceMessage; + + ShowFinalCheck(String priceMessage) { + this.priceMessage = priceMessage; + } + + void printFinalCheck() { + System.out.println(priceMessage); + } + +} diff --git a/src/main/java/ShowProductList.java b/src/main/java/ShowProductList.java new file mode 100644 index 0000000..1daf3b5 --- /dev/null +++ b/src/main/java/ShowProductList.java @@ -0,0 +1,11 @@ +public class ShowProductList { + StringBuilder productList; + + ShowProductList(StringBuilder productList) { + this.productList = productList; + } + + void printProductList(){ + System.out.println(productList); + } +}