forked from Yandex-Practicum/Java-Module-Project-YP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalculator.java
More file actions
86 lines (78 loc) · 3.06 KB
/
Calculator.java
File metadata and controls
86 lines (78 loc) · 3.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Locale;
import java.util.Scanner;
public class Calculator {
private int peopleNum;
private ArrayList<Product> 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;
}
}