import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
CurrencyFormatter myCurrencyFormatter = new CurrencyFormatter();
System.out.println("Ðа ÑколÑко лÑдей ÑазделиÑÑ ÑÑммÑ?: ");
Calculator myCalculator = new Calculator(myCurrencyFormatter.nextPositiveIntegerHigherThan1FromSystem(scanner));
String stopWord = "";
while (true) {
String itemName;
System.out.println("ÐведиÑе наименование ваÑего ÑоваÑа: ");
itemName = scanner.nextLine();
System.out.println("ÐÐ¼Ñ ÑоваÑа пÑинÑÑо");
System.out.println("ÐведиÑе ÑÑоимоÑÑÑ Ð²Ð°Ñего ÑоваÑа: ");
myCalculator.addNewItemToTheList(itemName, myCurrencyFormatter.nextPositiveDoubleHigherThan0FromSystem(scanner));
System.out.println("ÐведиÑе \"ÐавеÑÑиÑÑ\", еÑли законÑили Ñ ÑоÑÑавлением ÑпиÑка ÑоваÑов, введиÑе лÑбой дÑÑгой ÑекÑÑ, еÑли Ñ
оÑиÑе ввеÑÑи еÑе ÑоваÑ:");
stopWord = scanner.nextLine();
if (stopWord.equalsIgnoreCase("ÐавеÑÑиÑÑ")) {
break;
}
}
System.out.println("Ðобавление ÑоваÑов завеÑÑено\nÐобавленнÑе ÑоваÑÑ:");
myCalculator.showItems();
System.out.println("СÑмма коÑоÑÑÑ Ð´Ð¾Ð»Ð¶ÐµÐ½ заплаÑиÑÑ ÐºÐ°Ð¶Ð´Ñй Ñеловек:" + myCurrencyFormatter.showPrice(myCalculator.returnCheck()));
scanner.close();
}
}
class Calculator {
Integer amountOfPeople;
Double totalMoney;
ArrayList- itemList = new ArrayList<>();
Calculator(Integer people) {
amountOfPeople = people;
totalMoney = 0.0;
}
void addNewItemToTheList(String name, Double price) {
itemList.add(new Item(name, price));
System.out.println("Ð¢Ð¾Ð²Ð°Ñ Ð´Ð¾Ð±Ð°Ð²Ð»ÐµÐ½ в ÑпиÑок");
totalMoney += price;
}
void showItems() {
for (int i = 0; i < itemList.size(); i++) {
System.out.println((i + 1) + "-ÑоваÑ: " + itemList.get(i).name);
}
}
double returnCheck() {
return totalMoney / amountOfPeople;
}
}
class CurrencyFormatter {
String showPrice(double price) {
int integerPartOfPrice = (int) price;
int changeValue = (int) ((price * 100) % 100); //валÑÑÑ Ð¼ÐµÐ½ÑÑе копейки неÑ
String currencyPronunciation = " ÑÑблей ";
String changePronunciation = " копеек";
if ((integerPartOfPrice % 10) == 1) {
currencyPronunciation = " ÑÑÐ±Ð»Ñ ";
}
if (((integerPartOfPrice % 10) > 1) && ((integerPartOfPrice % 10) <= 4)) {
currencyPronunciation = " ÑÑÐ±Ð»Ñ ";
}
if (10 < ((integerPartOfPrice % 100)) && ((integerPartOfPrice % 100) < 15)) {
currencyPronunciation = " ÑÑблей ";
}
if ((changeValue % 10) == 1) {
changePronunciation = " копейка";
}
if (((changeValue % 10) > 1) && ((changeValue % 10) <= 4)) {
changePronunciation = " копейки";
}
if ((10 < changeValue) && (changeValue < 15)) {
changePronunciation = " копеек";
}
return (" " + integerPartOfPrice + currencyPronunciation + changeValue + changePronunciation);
}
Integer nextPositiveIntegerHigherThan1FromSystem(Scanner sn) {
String myLine;
Integer myNumber = null;
boolean incorrectInput = true;
while (incorrectInput) {
myLine = sn.nextLine();
try {
myNumber = Integer.parseInt(myLine);
} catch (NumberFormatException e) {
System.err.println("ÐеадекваÑное знаÑение. ÐводиÑе ÑолÑко ÑелÑе ÑиÑла болÑÑе 1: " + e.getMessage());
continue;
}
if (myNumber > 1) {
incorrectInput = false;
} else {
System.err.println("ÐеадекваÑÐ½Ð°Ñ Ð²ÐµÐ»Ð¸Ñина Ñелого ÑиÑла. ÐводиÑе ÑолÑко ÑелÑе ÑиÑла болÑÑе 1: " + myLine);
}
}
return myNumber;
}
Double nextPositiveDoubleHigherThan0FromSystem(Scanner sn) {
String myLine;
Double myNumber = null;
boolean incorrectInput = true;
while (incorrectInput) {
myLine = sn.nextLine();
try {
myNumber = Double.parseDouble(myLine);
if (myNumber > 0.0) {
incorrectInput = false;
} else {
System.err.println("ÐеадекваÑÐ½Ð°Ñ Ð²ÐµÐ»Ð¸Ñина дÑобного ÑиÑла. ÐводиÑе ÑолÑко дÑобнÑе ÑиÑла болÑÑе 0: " + myLine);
}
} catch (NumberFormatException e) {
System.err.println("ÐеадекваÑное знаÑение. ÐводиÑе ÑолÑко дÑобнÑе ÑиÑла болÑÑе 0: " + e.getMessage());
continue;
}
}
return myNumber;
}
}
class Item {
String name;
Double price;
Item(String itemName, Double itemPrice) {
name = itemName;
price = itemPrice;
}
}