diff --git a/src/main/java/Main.java b/src/main/java/Main.java deleted file mode 100644 index db9356a08..000000000 --- a/src/main/java/Main.java +++ /dev/null @@ -1,6 +0,0 @@ - -public class Main { - public static void main(String[] args) { - System.out.println("Hello world!"); - } -} \ No newline at end of file diff --git a/src/main/java/org/example/Calculator.java b/src/main/java/org/example/Calculator.java new file mode 100644 index 000000000..9a84f635b --- /dev/null +++ b/src/main/java/org/example/Calculator.java @@ -0,0 +1,26 @@ +package org.example; + +import java.util.ArrayList; + +class Calculator { + ArrayList itemsList = new ArrayList(); + + // проверяю все товары и суммирую + public void addMyItem(String itemName, double itemPrice) { + itemsList.add(new Item(itemName, itemPrice)); + } + + public double calculateMyTotal() { + double totalSumIs = 0.0; + for (Item myItem : itemsList) { + totalSumIs += myItem.itemPrice; + } + return totalSumIs; + } + + public void showItems() { + for (Item myItem : itemsList) { + System.out.println(myItem.itemName); + } + } +} \ No newline at end of file diff --git a/src/main/java/org/example/Item.java b/src/main/java/org/example/Item.java new file mode 100644 index 000000000..40185c8f8 --- /dev/null +++ b/src/main/java/org/example/Item.java @@ -0,0 +1,11 @@ +package org.example; + +class Item { + String itemName; + double itemPrice; + + Item(String itemName, double itemPrice) { + this.itemName = itemName; + this.itemPrice = itemPrice; + } +} \ No newline at end of file diff --git a/src/main/java/org/example/Main.java b/src/main/java/org/example/Main.java new file mode 100644 index 000000000..510c032cb --- /dev/null +++ b/src/main/java/org/example/Main.java @@ -0,0 +1,77 @@ +package org.example; + +import java.util.Scanner; + +public class Main { + static Scanner scan = new Scanner(System.in); + static Calculator calculatorObject = new Calculator(); + static MyFormatter myFormatter = new MyFormatter(); + + public static void main(String[] args) { + int allGuests; + while (true) { + System.out.println("На скольких человек необходимо разделить счёт?"); + if (scan.hasNextInt()) { + allGuests = scan.nextInt(); + if (allGuests < 1) { + System.out.println("Некорректное значение. Введите корректное число гостей.\n"); + } else if (allGuests == 1) { + System.out.println("Один человек - считать нечего. До свидания!\n"); + return; + } else { + scan.nextLine(); + // если ввел более 1, то цикл стопаю и продолжаю программу + // тоже съел пустой символ, оставшийся от некстИнта + break; + } + } else if (!(scan.hasNextInt())) { + System.out.println("Некорректное значение. Введите корректное число гостей."); + scan.nextLine(); // поглотил лишний символ (строку) + } + } + + //далее калькулятор после второй партии вопросов + while (true) { + System.out.println("Какое название у товара?"); + // String itemName = scan.nextLine(); //Это был 1-й вар. Ниже проверяю на пустоту. isBlank() метод + // не находился почему-то и красным светился, потому сделал через hasNextLine и trim+isEmpty + String itemName = ""; + while (scan.hasNextLine()) { + itemName = scan.nextLine(); + if (itemName.trim().isEmpty()) { + System.out.println("Ничего не введено. Какое название у товара?"); + } else { + break; + } + } + // далее обработка, чтобы у юзера при вводе строки не упало прил. + double itemPrice = 0.0; + while (true) { + System.out.println("Какая цена у товара?"); + if (scan.hasNextDouble()) { + itemPrice = scan.nextDouble(); + if (itemPrice < 0) { + System.out.println("Отрицательное значение. Введите заново."); + } else { + scan.nextLine(); + break; + } + } else if (!scan.hasNextDouble()) { + System.out.println("Некорректное значение. Надо ввести в формате: 00,00"); + scan.nextLine(); + } + } + calculatorObject.addMyItem(itemName, itemPrice); + System.out.println("Товар " + itemName + " успешно добавлен!" + + " Хотите ли добавить ещё один товар? Если да - введите что-нибудь. Если нет - введите Завершить"); + if (scan.hasNextLine()) { + if (scan.nextLine().equalsIgnoreCase("Завершить")) { + break; + } + } + } + System.out.println("Добавленные товары:"); + calculatorObject.showItems(); + myFormatter.printFormatResult(calculatorObject.calculateMyTotal(), allGuests); + } +} \ No newline at end of file diff --git a/src/main/java/org/example/MyFormatter.java b/src/main/java/org/example/MyFormatter.java new file mode 100644 index 000000000..5b88965a2 --- /dev/null +++ b/src/main/java/org/example/MyFormatter.java @@ -0,0 +1,29 @@ +package org.example; + +class MyFormatter { + public void printFormatResult(double myTotal, int allGuests) { + double sumResult = myTotal / allGuests; + int endNumber = (int) sumResult % 10; + if ((Math.floor(sumResult)>= 1.0) && (Math.floor(sumResult) < 2)) { + String myResult = "Счет на каждого: %.2f рубль"; + System.out.printf(myResult, sumResult); + } else if ((Math.floor(sumResult)>= 2) && (Math.floor(sumResult) < 4.99)) { + String myResult = "Счет на каждого: %.2f рубля"; + System.out.printf(myResult, sumResult); + } else if ((Math.floor(sumResult) < 1) || ((Math.floor(sumResult)>= 5.0) && (Math.floor(sumResult) < 20.99))) { + String myResult = "Счет на каждого: %.2f рублей"; + System.out.printf(myResult, sumResult); + } else if (Math.floor(sumResult)>= 21) { + if (endNumber>= 2 && endNumber <= 4) { + String myResult = "Счет на каждого: %.2f рубля"; + System.out.printf(myResult, sumResult); + } else if (endNumber == 1) { + String myResult = "Счет на каждого: %.2f рубль"; + System.out.printf(myResult, sumResult); + } else if (endNumber>= 5 && endNumber <= 9) { + String myResult = "Счет на каждого: %.2f рублей"; + System.out.printf(myResult, sumResult); + } + } + } +} \ No newline at end of file

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