diff --git a/src/main/java/Main.java b/src/main/java/Main.java index a9198c435..4ee1298d7 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,8 +1,85 @@ +import java.util.Scanner; + public class Main { + private static Scanner scanner = new Scanner(System.in); public static void main(String[] args) { - // ваш код начнется здесь - // вы не должны ограничиваться только классом Main и можете создавать свои классы по необходимости - System.out.println("Привет Мир"); + System.out.println("Введите на сколько человек нужно разделить счёт:"); + int peopleAmount = readPeopleAmount(); + + String command = ""; + final String endCommand = "завершить"; + while (!endCommand.equalsIgnoreCase(command)) { + ProductCalculator.addProduct(readProduct()); + System.out.println("Если хотите завершить добавление товаров, то введите слово " + + "\"Завершить\".\n В противном случае введите, что угодно."); + command = scanner.nextLine(); + } + + System.out.println("Добавленные товары:"); + ProductCalculator.printAllProducts(); + printAveragePrice(ProductCalculator.getTotalCost(), peopleAmount); + } + + public static int readPeopleAmount() { + int amount = 0; + final int minPeopleAmount = 1; + while (true) { + String input = scanner.nextLine(); + try { + amount = Integer.parseInt(input); + if (amount >= minPeopleAmount) { + break; + } + } catch (NumberFormatException | NullPointerException ignored) { + } + System.out.println("Введите корректное количество человек:"); + } + return amount; + } + + public static Product readProduct() { + System.out.println("Введите название товара, который хотите добавить:"); + String productName = scanner.nextLine(); + System.out.println("Введите стоимость этого товара:"); + double price = 0.; + while (true) { + String input = scanner.nextLine(); + try { + price = Double.parseDouble(input); + if (price > 0.) { + break; + } + } catch (NumberFormatException | NullPointerException ignored) { + } + System.out.println("Введите корректное значение цены товара '" + productName + "':"); + } + return new Product(productName, price); + } + + public static void printAveragePrice(double totalCost, int peopleAmount) { + double avgPrice = totalCost / peopleAmount; + System.out.println("\nКаждый должен заплатить по:"); + String rubleWord = getRubleWordByNum(avgPrice); + System.out.println(String.format("%.2f %s", avgPrice, rubleWord)); + } + + public static String getRubleWordByNum(double rubles) { + int roundedRubles = ((int) Math.floor(rubles)); + final int divHundredReminder = roundedRubles % 100; + final int divTenReminder = roundedRubles % 10; + if (!InRange(divHundredReminder, 11, 14)) { + if (InRange(divTenReminder, 2, 4)) { + return "рубля"; + } + if (divTenReminder == 1) { + return "рубль"; + } + } + return "рублей"; + } + + public static boolean InRange(int value, int lower, int upper) { + return ((lower <= value) && (value <= upper)); } } diff --git a/src/main/java/Product.java b/src/main/java/Product.java new file mode 100644 index 000000000..894a3d3cb --- /dev/null +++ b/src/main/java/Product.java @@ -0,0 +1,17 @@ +public class Product { + private String name_; + private double price_; + + Product(String name, double price) { + name_ = name; + price_ = price; + } + + public String getName() { + return name_; + } + + public double getPrice() { + return price_; + } +} diff --git a/src/main/java/ProductCalculator.java b/src/main/java/ProductCalculator.java new file mode 100644 index 000000000..de37a31e3 --- /dev/null +++ b/src/main/java/ProductCalculator.java @@ -0,0 +1,21 @@ +import java.util.ArrayList; + +public class ProductCalculator { + private static double totalCost_ = 0.; + private static ArrayList products_ = new ArrayList<>(); + + public static void addProduct(Product product) { + products_.add(product.getName()); + totalCost_ += product.getPrice(); + } + + public static void printAllProducts() { + for (String product : products_) { + System.out.println(product); + } + } + + public static double getTotalCost() { + return totalCost_; + } +}