diff --git a/src/main/java/Calculator.java b/src/main/java/Calculator.java new file mode 100644 index 000000000..8ead91da9 --- /dev/null +++ b/src/main/java/Calculator.java @@ -0,0 +1,33 @@ +public class Calculator { + + private double check = 0; + private Product product; + private Guest guest; + + public Calculator(Product product, Guest guest) { + this.product = product; + this.guest = guest; + } + + public void cheakCalcForm() { + check = product.sum / guest.guests; + double checkF = Math.floor(check); + int checkO = (int) checkF % 10; + switch (checkO) + { + case 1: + System.out.println(String.format("%.2f", check) + " рубль"); + break; + case 2: + case 3: + case 4: + System.out.println(String.format("%.2f", check) + " рубля"); + break; + default: + System.out.println(String.format("%.2f", check) + " рублей"); + break; + } + } +} + + diff --git a/src/main/java/Guest.java b/src/main/java/Guest.java new file mode 100644 index 000000000..e03d7ecfb --- /dev/null +++ b/src/main/java/Guest.java @@ -0,0 +1,20 @@ +public class Guest { + int guests; + public void numGuests() { + System.out.println("Здравствуйте. Введите количество гостей:"); + while (true) { + String guestsStr = Main.scanner.next(); + try { + int ans = Integer.parseInt(guestsStr); + if (ans <= 1) { + throw new RuntimeException(); + } + guests = ans; + return; + } catch (Exception e) { + System.out.println("Ошибка, попробуйте еще раз!\n----------"); + System.out.println("Введите количество гостей:"); + } + } + } +} diff --git a/src/main/java/Main.java b/src/main/java/Main.java index db9356a08..3a96f2623 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,6 +1,16 @@ +import java.util.Scanner; public class Main { + static Scanner scanner = new Scanner(System.in); + public static void main(String[] args) { - System.out.println("Hello world!"); + Guest guest = new Guest(); + Product product = new Product(); + Calculator calc = new Calculator(product, guest); + + guest.numGuests(); + product.products(); + product.shoplist(); + calc.cheakCalcForm(); } -} \ 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..d12fd8f60 --- /dev/null +++ b/src/main/java/Product.java @@ -0,0 +1,51 @@ +import java.util.ArrayList; + +public class Product { + String name; + double price; + double sum = 0; + ArrayList listPro = new ArrayList<>(); + + public void products() { + + System.out.println("Укажите название и стоимость каждого товара?"); + name = ""; + boolean isProductInputFinished = false; + while (!isProductInputFinished) { + System.out.println("Название товара:"); + name = Main.scanner.next(); + if (name.equalsIgnoreCase("завершить")) { + isProductInputFinished = true; + continue; + } + listPro.add(name); + priceSum(); + System.out.println("Товар успешно добавлен!\n----------"); + System.out.println("Хотите добавить еще один товар? Если нет, то введите - Завершить"); + } + + } + + private void priceSum() { + System.out.println("Стоимость товара = "); + String priceStr = Main.scanner.next(); + try { + price = Double.parseDouble(priceStr); + } catch (Exception e) { + price = -1; + } + if (price < 0.0) { + System.out.println("Введите корректную стоимость товара:"); + priceSum(); + return; + } + sum = sum + price; + } + public void shoplist() { + System.out.println("----------\nДобавленные товары:"); + for (int i = 0; i < listPro.size(); i++) { + System.out.println(listPro.get(i)); + } + } +} +