forked from Yandex-Practicum/Java-Module-Project-YP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGoods.java
More file actions
38 lines (31 loc) · 1.29 KB
/
Goods.java
File metadata and controls
38 lines (31 loc) · 1.29 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
import java.util.ArrayList;
public class Goods {
public String productName;
public Double productPrice;
Goods(String productName, Double productPrice) {
this.productName = productName;
this.productPrice = productPrice;
}
public static void calculate(ArrayList<Goods> goods, int quantity){
double sum = 0;
for (int i = 0; i < goods.size(); i++){
System.out.println(String.format("Товар %d: %s цена: %.2f", i + 1, goods.get(i).productName, goods.get(i).productPrice));
sum += goods.get(i).productPrice;
}
Double sumPerPeople = sum / quantity;
String textRub = takeEnding(sumPerPeople.intValue());
System.out.println(String.format("Итого с каждого человека по %.2f %s.", sumPerPeople, textRub));
}
private static String takeEnding (int sumPerPeopleInt){
int remainderOne = sumPerPeopleInt % 10;
int remainderTwo = sumPerPeopleInt % 100;
if (remainderTwo >= 11 && remainderTwo <= 14)
return "рублей";
else if (remainderOne == 1)
return "рубль";
else if (remainderOne > 1 && remainderOne <=4)
return "рубля";
else
return "рублей";
}
}