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
53 lines (48 loc) · 1.83 KB
/
Calculator.java
File metadata and controls
53 lines (48 loc) · 1.83 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
import java.util.ArrayList;
public class Calculator {
public Calculator(int personCount, ArrayList<Dish> dishesList) {
calculate(personCount, dishesList);
}
private void calculate(int personCount, ArrayList<Dish> dishesList){
showDishList(dishesList);
totalPriceForPerson(personCount, dishesList);
}
private String getRightEnding(double price){
String ending = "";
String x = "рубль";
String y = "рубля";
String z = "рублей";
if(price%100 >= 11 && price%100 <= 14){
ending = z;
return ending;
}
if(price%10 == 1){
ending = x;
return ending;
}
if(price%10 == 2 || price%10 == 3 || price%10 == 4){
ending = y;
return ending;
}
return z;
}
private void showDishList(ArrayList<Dish> dishesList){
System.out.println("Добавленные товары:");
double price;
for(int i = 0; i < dishesList.size(); i++){
price = dishesList.get(i).getPrice();
String ending = getRightEnding(price);
System.out.println("Позиция: " + dishesList.get(i).getName() + " Цена: " + String.format("%.2f", dishesList.get(i).getPrice()) + " " + ending);
}
}
private void totalPriceForPerson(int personCount, ArrayList<Dish> dishesList){
Double totalPrice = 0.0;
for(int i = 0; i < dishesList.size(); i++){
totalPrice = totalPrice + dishesList.get(i).getPrice();
}
totalPrice = totalPrice / personCount;
double price = totalPrice;
String ending = getRightEnding(price);
System.out.println("Итоговая цена для каждой персоны: " + String.format("%.2f", totalPrice) + " " + ending);
}
}