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
70 lines (57 loc) · 1.8 KB
/
Calculator.java
File metadata and controls
70 lines (57 loc) · 1.8 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
62
63
64
65
66
67
68
69
70
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Calculator {
private List<Item> items;
public Calculator() {
this.items = new ArrayList<>();
}
public void addItem(String name, double price) {
items.add(new Item(name, price));
}
public double getTotalBill() {
double total = 0.0;
for (Item item : items) {
total += item.getPrice();
}
return total;
}
public List<Item> getItems() {
return items;
}
public void splitBill(int kGuests) {
double totalBill = getTotalBill();
double perPerson;
if ( kGuests > 1) {
perPerson = totalBill / kGuests;
} else {
perPerson = totalBill;
}
// Определение окончания для "рубль"
String suffix;
int part = (int) totalBill;
if (part % 10 == 1 &&part % 100 != 11) {
suffix = "рубль";
} else if (part % 10 >= 2 &&part % 10 <= 4 && (part % 100 < 10 ||part % 100 >= 20)) {
suffix = "рубля";
} else {
suffix = "рублей";
}
System.out.println("Общая сумма счета: " + String.format("%.2f", totalBill) + " " + suffix + ".");
System.out.println("Каждый гость должен заплатить по: " + String.format("%.2f", perPerson) + " " + suffix + ".");
}
static class Item {
private String name;
private double price;
public Item(String name, double price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
}
}