import java.util.Scanner; public class Main { private static Scanner scanner = new Scanner(System.in); public static void main(String[] args) { 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)); } }