import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Locale;
import java.util.Scanner;
public class Calculator {
private int peopleNum;
private ArrayList products;
private double totalPrice;
private double pricePerOne;
private double coins;
private Scanner scanner;
public Calculator() {
products = new ArrayList<>();
scanner = new Scanner(System.in);
}
public void getPeopleNumber() {
String peopleNum;
do {
System.out.println("ÐведиÑе колиÑеÑÑво Ñеловек, коÑоÑÑе бÑдÑÑ Ð´ÐµÐ»Ð¸ÑÑ ÑÑеÑ:");
peopleNum = scanner.nextLine();
this.peopleNum = Validator.validatePeopleNumber(peopleNum);
}
while (this.peopleNum == -1);
}
public void addProduct() {
String cancelInput = "";
while (!cancelInput.equalsIgnoreCase("завеÑÑиÑÑ")) {
System.out.println("ÐобавÑÑе пÑодÑÐºÑ Ð² калÑкÑлÑÑоÑ");
var product = new Product().create();
products.add(product);
System.out.println("ÐÑодÑÐºÑ " + product + " ÑÑпеÑно добавлен");
System.out.println("ХоÑиÑе добавиÑÑ ÐµÑе? ÐÑли неÑ, Ñо напеÑаÑайÑе \"ÐавеÑÑиÑÑ\"");
cancelInput = scanner.nextLine();
}
}
public void calculate() {
for (var product : products) {
totalPrice = totalPrice + product.price;
}
var count = 100;
pricePerOne = (Math.ceil(totalPrice / peopleNum * count) / count);
coins = Double.parseDouble(String.format(Locale.ENGLISH, "%.2f", pricePerOne * peopleNum - totalPrice));
}
public void printInputData() {
System.out.println("ÐолиÑеÑÑво Ñеловек: " + peopleNum);
System.out.println("ÐобавленнÑе ÑоваÑÑ:");
for (var product : products) {
System.out.println(product);
}
}
public void printResult() {
System.out.println("СÑмма Ð´Ð»Ñ Ð¾Ð¿Ð»Ð°ÑÑ Ð½Ð° Ñеловека " + pricePerOne + " " + defineRubleLabel(pricePerOne));
System.out.println("СÑÐ¼Ð¼Ñ Ð¿Ð¾Ð´ÐµÐ»Ð¸Ð»Ð¸ поÑовнÑ, оÑиÑÐ¸Ð°Ð½Ñ Ð¿ÑÐ¸Ð½ÐµÑ ÑдаÑÑ " + coins + " " + defineRubleLabel(coins));
}
private String defineRubleLabel(double rubles) {
BigDecimal bigDecimal = new BigDecimal(String.valueOf(rubles));
String rublesLabel;
int totalRubles = bigDecimal.intValue();
if (totalRubles % 100 >= 11 & totalRubles % 100 <= 20) {
rublesLabel = "ÑÑблей";
return rublesLabel;
}
switch (totalRubles % 10) {
case 1:
rublesLabel = "ÑÑблÑ";
break;
case 2:
case 3:
case 4:
rublesLabel = "ÑÑблÑ";
break;
default:
rublesLabel = "ÑÑблей";
break;
}
return rublesLabel;
}
}