Repository navigation
Expand file tree
/
Copy pathsumAllProducts.java
More file actions
61 lines (43 loc) · 1.99 KB
/
Copy pathsumAllProducts.java
File metadata and controls
61 lines (43 loc) · 1.99 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
import java.util.ArrayList;
import static java.lang.Math.floor;
public class sumAllProducts {
public static void sumForEveryone(ArrayList<String> productsArrayList, int people) {
ArrayList<Double> costArrayList = new ArrayList<>();
//добавление в массив costArrayList 099
int size = productsArrayList.size();
for (int i = 0; i < (size-1); i = i + 2){
costArrayList.add(Double.valueOf(productsArrayList.get(i+1)));
}
System.out.println();
System.out.println("=========================");
System.out.println();
System.out.println("Каждый должен заплатить:");
double sum = costArrayList.stream()
.mapToDouble(Double::doubleValue)
.sum();
if (sum == 0) {
System.out.printf("%.2f рублей%n", sum);
} else {
double finalSum = sum / people;
int roundedNumber = (int) floor(finalSum);
String sumFinal = String.valueOf(roundedNumber); //округляем число в меньшую сторону
int twoLastLetter = sumFinal.length();
String lastLetter = sumFinal.substring(Math.max(0, twoLastLetter-2) ); //находим 2 последние цифры
String str = "23456789";
if(str.contains(String.valueOf(lastLetter.charAt(0)) )) {
lastLetter = lastLetter.substring(1);
}
switch (lastLetter) {
case "1":
System.out.printf("%.2f рубль%n", finalSum);
break;
case "2", "3", "4":
System.out.printf("%.2f рубля%n", finalSum);
break;
case "5", "6", "7", "8", "9", "0", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19":
System.out.printf("%.2f рублей%n", finalSum);
break;
}
}
}
}