import java.util.ArrayList;
import java.util.Scanner;
public class Main {
static Scanner scanner = new Scanner(System.in);
private static void printOut(String string) {System.out.println(string);}
public static void main(String[] args) {
scanner.useDelimiter("\n");
printResult(getCount(), getProducts());
scanner.close();
}
private static void printResult(int peopleCount, ArrayList calc) {
printOut("ÐобавленнÑе ÑоваÑÑ:");
double sumAll = 0;
for (Calculator calcItem : calc){
System.out.printf("%s %.2f \n", calcItem.product,calcItem.price);
sumAll += calcItem.price;
}
System.out.printf("ÐÑего поÑÑаÑили: %.2f\n", sumAll);
double sumPerPerson = sumAll / (double) peopleCount;
System.out.printf("ÐаждÑй Ñеловек должен заплаÑиÑÑ Ð¿Ð¾ %.2f ÑÑб%s \n", sumPerPerson, Formatter.formatRubles(sumPerPerson));
}
private static ArrayList getProducts() {
ArrayList products = new ArrayList<>();
do {
printOut("ÐавайÑе добавим новÑй ÑоваÑ.\nУкажиÑе его название:");
String product = getProduct();
printOut("ÐÑлиÑно, ÑепеÑÑ ÑкажиÑе его ÑÑоимоÑÑÑ Ð² ÑоÑмаÑе *ÑÑбли*,*копейки*:");
products.add(new Calculator(product, getPrice()));
printOut("Ð¢Ð¾Ð²Ð°Ñ ÑÑпеÑно запиÑан!\nÐÑли Ñ
оÑиÑе добавиÑÑ ÐµÑе ÑоваÑÑ Ð½Ð°Ð¶Ð¼Ð¸Ñе Enter? ÐÑли неÑ, напиÑиÑе \"завеÑÑиÑÑ\".");
} while (!"завеÑÑиÑÑ".equalsIgnoreCase(scanner.next().trim()));
return products;
}
private static double getPrice() {
double price = 0;
do {
try {
price = Double.parseDouble(scanner.next().replace(",", "."));
if (0 >= price) printOut("Цена не Ð¼Ð¾Ð¶ÐµÑ ÑавнÑÑÑÑÑ Ð½ÑÐ»Ñ Ð¸Ð»Ð¸ бÑÑÑ Ð¾ÑÑиÑаÑелÑной! ÐовÑоÑиÑе ввод:");
} catch (Exception e) {
printOut("ÐÑ Ð²Ð²ÐµÐ»Ð¸ невеÑное знаÑение! ÐовÑоÑиÑе ввод:");
}
} while (0 >= price);
return price;
}
private static int getCount() {
int count;
printOut("Ðа ÑколÑко Ñеловек ÑазделиÑÑ ÑÑеÑ:");
do {
while (!scanner.hasNextInt()) {
System.out.println("ÐведиÑе ÑиÑло!");
scanner.nextLine();
}
count = scanner.nextInt();
if (count < 1) {
printOut("ÐÑо некоÑÑекÑное знаÑение Ð´Ð»Ñ Ð¿Ð¾Ð´ÑÑÑÑа.");
} else if (count == 1) {
printOut("Ð ÑÑом ÑлÑÑае Ð½ÐµÑ ÑмÑÑла ниÑего ÑÑиÑаÑÑ Ð¸ делиÑÑ.");
}
} while (count <= 1 );
return count;
}
private static String getProduct(){
String product;
do {
product = scanner.next().trim();
if (product.length() != 0)
break;
printOut("ÐÑÑÑое название бÑÑÑ Ð½Ðµ можеÑ!\nУкажиÑе название ÑоваÑа:");
} while (true);
return product;
}
}