import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// List
ArrayList productsName = new ArrayList();
// ÐеÑеменнÑе
int humans;
double price;
String products;
float summ = 0;
// СканеÑÑ
Scanner scanner_humans = new Scanner(System.in);
Scanner scanner_products = new Scanner(System.in);
Scanner scanner_prices = new Scanner(System.in);
// ÐоздоÑоваемÑÑ!
System.out.println("ÐобÑого вÑемени ÑÑÑок!\n" +
"Ðа ÑколÑкиÑ
Ñеловек необÑ
одимо ÑазделиÑÑ ÑÑÑÑ:");
// ÐолÑÑаем колиÑеÑÑво лÑдей
while (true) {
if (scanner_humans.hasNextInt()) {
humans = scanner_humans.nextInt();
while (humans <= 1) {
System.out.println("ÐолиÑеÑÑво лÑдей должно бÑÑÑ Ð±Ð¾Ð»ÐµÐµ 1");
humans = scanner_humans.nextInt();
if (humans > 1) break;
}
break;
} else {
System.out.println("Ðозможно Ð²Ñ Ð²Ð²ÐµÐ»Ð¸ не ÑиÑло, попÑобÑйÑе еÑÑ Ñаз!");
scanner_humans.nextLine();
}
}
// ÐолÑÑаем ÑоваÑÑ
while (true) {
System.out.println("Ðазвание ÑоваÑа:");
if (scanner_products.hasNext()) {
products = scanner_products.nextLine();
if (!products.equalsIgnoreCase("завеÑÑиÑÑ")) {
productsName.add(new Product(products));
System.out.println("УкажиÑе ÑÐµÐ½Ñ ÑоваÑа в ÑоÑмаÑе ÑÑбли.копейки: ");
if (scanner_prices.hasNextDouble()) {
price = scanner_prices.nextDouble();
while (price <= 0) {
System.out.println("ÐнаÑение не Ð¼Ð¾Ð¶ÐµÑ Ð±ÑÑÑ 0 или менÑÑе!");
price = scanner_prices.nextDouble();
if (price > 0) {
System.out.println(String.format("Цена: %.2f", price));
break;
}
}
summ += price;
System.out.println("ÐобавленнÑе ÑоваÑÑ:");
for (Product name : productsName) {
System.out.println("* " + productsName.get(i).productName);
}
System.out.println("СÑмма: " + String.format("%.2f", summ));
} else {
System.out.println("ÐажеÑÑÑ Ð²Ñ Ñказали невеÑнÑй ÑоÑмаÑ!");
scanner_prices.hasNextDouble();
}
} else {
float forPerson = summ / humans;
int rubles = (int) forPerson;
System.out.println("СÑмма на каждого Ñеловека " + String.format("%.2f", forPerson) + " " + grammar(rubles));
break;
}
} else {
System.out.println("Ðе полÑÑено даннÑÑ
о ÑоваÑе!");
scanner_products.nextLine();
}
}
}
private static String grammar(int rubles) {
if (lastIndexRub(rubles).equals("0")) {
return "ÑÑблей";
} else if (lastIndexRub(rubles).equals("1")) {
return "ÑÑблÑ";
} else if (lastIndexRub(rubles).equals("2") || lastIndexRub(rubles).equals("3") || lastIndexRub(rubles).equals("4")) {
return "ÑÑблÑ";
} else {
return "ÑÑблей";
}
}
private static String lastIndexRub(int rubles) {
String str = String.valueOf(rubles);
str.substring(str.length() - 1);
return str;
}
}