import java.util.Scanner; public class Main { public static void main(String[] args) { int peopleCount; String productName; double productPrice; Product product; double result; String answer; Scanner scanner = new Scanner(System.in); Calculator calculator = new Calculator(); while (true) { System.out.println("Ðа ÑколÑÐºÐ¸Ñ Ñеловек Ð½ÐµÐ¾Ð±Ñ Ð¾Ð´Ð¸Ð¼Ð¾ ÑазделиÑÑ ÑÑÑÑ?"); if (!scanner.hasNextInt()) { System.out.println("ÐзвиниÑе, но ÑÑо Ñвно не Ñелое ÑиÑло."); scanner.nextLine(); continue; } peopleCount = scanner.nextInt(); if (peopleCount < 1) { System.out.println("ÐолиÑеÑÑво Ñеловек не Ð¼Ð¾Ð¶ÐµÑ Ð±ÑÑÑ Ð¼ÐµÐ½ÑÑе 1."); } else if (peopleCount == 1) { System.out.println("ÐолиÑеÑÑво Ñеловек не Ð¼Ð¾Ð¶ÐµÑ Ð±ÑÑÑ Ñавно 1."); } else { scanner.nextLine(); break; } scanner.nextLine(); } do { while (true) { System.out.println("УкажиÑе название ÑоваÑа:"); productName = scanner.nextLine(); if (productName.length() > 1) { break; } System.out.println("ÐзвиниÑе, но название ÑоваÑа не Ð¼Ð¾Ð¶ÐµÑ Ð±ÑÑÑ Ð¿ÑÑÑой ÑÑÑокой."); } while (true) { System.out.println("УкажиÑе ÑÑоимоÑÑÑ ÑоваÑа:"); if (!scanner.hasNextDouble()) { System.out.println("ÐзвиниÑе, но ÑÑо Ñвно не ÑиÑло."); scanner.nextLine(); continue; } productPrice = scanner.nextDouble(); if (productPrice < 0) { System.out.println("ÐзвиниÑе, но ÑÑоимоÑÑÑ ÑоваÑа не Ð¼Ð¾Ð¶ÐµÑ Ð±ÑÑÑ Ð¾ÑÑиÑаÑелÑной."); } else { scanner.nextLine(); break; } scanner.nextLine(); } product = new Product(productName, productPrice); calculator.add(product); System.out.println("ÐÑ Ñ Ð¾ÑиÑе добавиÑÑ ÐµÑе один ÑоваÑ? ÐÑли неÑ, ÑкажиÑе 'завеÑÑиÑÑ'"); answer = scanner.nextLine(); } while (!answer.equalsIgnoreCase("завеÑÑиÑÑ")); System.out.println("ÐобавленнÑе ÑоваÑÑ:" + calculator.getProducts()); result = calculator.getTotal() / peopleCount; System.out.println("ÐаждÑй Ñеловек должен заплаÑиÑÑ: " + Formatter.getFormatPrice(result) + " " + Formatter.getEnding(result)); scanner.close(); } } class Calculator { private double total = 0; private String products = ""; public void add(Product product) { this.total = this.total + product.price; this.products = this.products + "\n" + product.name; System.out.println("Ð¢Ð¾Ð²Ð°Ñ ÑÑпеÑно добавлен!"); } String getProducts() { return products; } double getTotal() { return this.total; } } class Product { final String name; final double price; Product(String name, double price) { this.name = name; this.price = price; } } class Formatter { public static String getFormatPrice(double price) { return String.format("%.2f", price); } public static String getEnding(double price) { double priceRoundedDown = Math.floor(price); double remainderPerHundred = priceRoundedDown % 100; if (remainderPerHundred >= 11 && remainderPerHundred <= 19) { return "ÑÑблей"; } double remainderByTen = priceRoundedDown % 10; if (remainderByTen == 1) { return "ÑÑблÑ"; } else if (remainderByTen >= 2 && remainderByTen <= 4) { return "ÑÑблÑ"; } else { return "ÑÑблей"; } } }