import java.util.Scanner;
import java.util.ArrayList;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class Main {
static int numOfPersons;
static double check = 0;
static Scanner scanner = new Scanner(System.in);
static ArrayList products = new ArrayList<>();
static Calculate calc = new Calculate();
public static void main(String[] args) {
String s;
System.out.println("Ðа ÑколÑкиÑ
Ñеловек необÑ
одимо ÑазделиÑÑ ÑÑÑÑ:");
s = scanner.next();
while (true) {
if (s.equalsIgnoreCase("завеÑÑиÑÑ")) {
System.out.println("ÐÑогÑамма завеÑÑена");
return;
}
if (s.matches("\\d+")) {
numOfPersons = Integer.parseInt(s);
if (numOfPersons == 1) {
System.out.println("ÐолиÑеÑÑво Ñеловек Ñавно 1.\nÐÑо некоÑÑекÑное знаÑение Ð´Ð»Ñ Ð¿Ð¾Ð´ÑÑÑÑа");
System.out.println("ÐведиÑе колиÑеÑÑво Ñеловек:");
s = scanner.next();
} else if (numOfPersons < 1) {
System.out.println("ÐолиÑеÑÑво Ñеловек менÑÑе 1.\nÐÑо некоÑÑекÑное знаÑение Ð´Ð»Ñ Ð¿Ð¾Ð´ÑÑÑÑа");
System.out.println("ÐведиÑе колиÑеÑÑво Ñеловек:");
s = scanner.next();;
} else break;
} else {
System.out.println("ÐÑо некоÑÑекÑное знаÑение Ð´Ð»Ñ Ð¿Ð¾Ð´ÑÑÑÑа");
System.out.println("ÐведиÑе колиÑеÑÑво Ñеловек:");
s = scanner.next();
}
}
while (true) {
System.out.println("ÐведиÑе блÑдо и ÑенÑ.\nÐÑли добавили вÑе позиÑии введиÑе 'ÐавеÑÑиÑÑ'");
s = scanner.next();
if (!s.equalsIgnoreCase("завеÑÑиÑÑ")) {
s += scanner.nextLine();
check += calc.calculate(s, products);
} else {
System.out.println("ÐобавленнÑе ÑоваÑÑ:");
for (Product product : products) {
product.print();
}
if (check / numOfPersons > 1)
System.out.println(String.format("ÐÑого: %.2f ÑÑÐ±Ð»Ñ Ñ ÐºÐ°Ð¶Ð´Ð¾Ð³Ð¾", check / numOfPersons));
else
System.out.println(String.format("ÐÑого: %.2f ÑÑÐ±Ð»Ñ Ñ ÐºÐ°Ð¶Ð´Ð¾Ð³Ð¾", check / numOfPersons));
return;
}
}
}
public static class Calculate {
public static double calculate(String s, ArrayList products) {
Pattern pattern = Pattern.compile(" [0-9]*\\.?[0-9]+$");
Matcher matcher = pattern.matcher(s);
if (matcher.find())
if (matcher.start() > 1) {
double check = 0;
check += Double.parseDouble(s.substring(matcher.start(), matcher.end()));
Product product = new Product(s.substring(0, matcher.start() - 1), Double.parseDouble(s.substring(matcher.start(), matcher.end())));
product.printAdd();
products.add(product);
return check;
}
System.out.println("ÐекоÑекÑнÑй ввод!\nÐвод оÑÑÑеÑÑвлÑеÑÑÑ Ð² ÑоÑмаÑе:Ðазвание блÑда РУÐÐÐ.ÐÐÐÐÐÐÐ");
// System.out.println(s.substring(matcher.start(), matcher.end()));
return 0;
}
}
public static class Product {
private String name;
private double price;
Product(String name, double price) {
this.name = name;
this.price = price;
}
public void printAdd() {
System.out.println(String.format("ÐлÑдо: %s по Ñене %.2f ÑÑблей добавленно", name, price));
}
public void print() {
System.out.println(String.format("ÐлÑдо: %s по Ñене %.2f ÑÑблей", name, price));
}
}
}