forked from Yandex-Practicum/Java-Module-Project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalculator.java
More file actions
51 lines (43 loc) · 1.61 KB
/
Calculator.java
File metadata and controls
51 lines (43 loc) · 1.61 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
public class Calculator {
private int peopleCounter;
private String addedProducts = "Добавленные товары:\n";
private double totalPrice;
public Calculator(int peopleCounter){
this.peopleCounter = peopleCounter;
}
public void addProduct(Product product){
addedProducts = addedProducts + product.getName() + "\n";
totalPrice += product.getPrice();
System.out.println("Товар успешно добавлен.");
}
public void showAddedProducts(){
System.out.println("\n" + addedProducts);
}
public void divide(){
double dividedPrice = totalPrice / (double) peopleCounter;
String formattedPrice = formatPrice(dividedPrice);
String currency = getCurrencyWriting(dividedPrice);
System.out.println("Каждый человек должен заплатить " + formattedPrice + " " + currency + ".");
}
private String formatPrice(double price){
return String.format("%,.2f", price);
}
private String getCurrencyWriting(double price){
double roundedPrice = Math.floor(price);
double remainder = roundedPrice % 100.00;
if (remainder >= 11 && remainder <= 14){
return "рублей";
} else {
remainder = remainder % 10.00;
if (remainder >= 1 &&
remainder < 2) {
return "рубль";
} else if (remainder >= 2 &&
remainder <= 4) {
return "рубля";
} else {
return "рублей";
}
}
}
}