forked from Yandex-Practicum/Java-Module-Project-YP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalculation.java
More file actions
58 lines (43 loc) · 1.38 KB
/
Calculation.java
File metadata and controls
58 lines (43 loc) · 1.38 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
public class Calculation {
String productList = "Добавленные товары:";
int numberPersons;
double totalPrice;
Calculation(int numberPersons) {
this.numberPersons = numberPersons;
}
public void addProduct(String product, double price) {
productList = String.format("%s\n%s стоимость: %.2f %s", productList, product, price, getEnding(price));
totalPrice = totalPrice + price;
}
public String divideCheck() {
if (numberPersons == 0) {
return "Ошибка: нет персон";
}
double result = totalPrice / numberPersons;
return String.format("С одного человека: %.2f %s", result, getEnding(result));
}
public String getEnding(double number){
int lastNumber = (int) Math.floor(number);
if (lastNumber > 100) {
lastNumber = lastNumber % 100;
}
if (lastNumber > 20) {
lastNumber = lastNumber % 10;
}
String ending;
switch (lastNumber) {
case 1:
ending = "рубль";
break;
case 2:
case 3:
case 4:
ending = "рубля";
break;
default:
ending = "рублей";
break;
}
return ending;
}
}