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
58 lines (47 loc) · 1.49 KB
/
Calculator.java
File metadata and controls
58 lines (47 loc) · 1.49 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
import java.util.HashMap;
public class Calculator {
private double orderSum;
private final HashMap<Good, Integer> order = new HashMap<>();
public void addGood(Good good, int quantity) {
if (order.containsKey(good)) {
int oldQuantity = order.get(good);
int newQuantity = oldQuantity + quantity;
order.put(good, newQuantity);
} else {
order.put(good, quantity);
}
orderSum = calculate();
}
private double calculate() {
double sum = 0;
for (Good good : order.keySet()) {
int quantity = order.get(good);
sum += good.price * quantity;
}
return sum;
}
public void showOrder() {
System.out.println("Добавленные товары:");
for (Good good : order.keySet()) {
System.out.println(good.name + " " + String.format("%.2f", good.price));
}
}
public double getOrderSum() {
return orderSum;
}
public String getRubleInRightFormat(double rubles) {
int lastDigit = (int) (Math.floor(rubles) % 10);
if (((int) (Math.floor(rubles) % 100))<=19 && ((int) (Math.floor(rubles) % 100))>=11)
return "рублей";
switch (lastDigit) {
case 1:
return "рубль";
case 2:
case 3:
case 4:
return "рубля";
default:
return "рублей";
}
}
}