Repository navigation
Expand file tree
/
Copy pathSharedCalc.java
More file actions
50 lines (44 loc) · 1.88 KB
/
Copy pathSharedCalc.java
File metadata and controls
50 lines (44 loc) · 1.88 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
import java.util.ArrayList;
public class SharedCalc {
private int persons;
private double total;
ArrayList<CheckItem> items = new ArrayList<>();
public void setPersons(int persons) {
this.persons = persons;
}
public SharedCalc() {
this.persons = 0;
this.total = 0f;
}
public void addItem(CheckItem item) {
items.add(item);
total += item.value;
}
public double getTotal() {
return total;
}
public String getResultAsString() {
StringBuilder completeList = new StringBuilder();
// Формируем чек в виде строки
System.out.println("Добавленные товары:");
for (int i = 0; i < items.size(); i++) {
completeList.append(String.format("%s\n\t\t\t%.2f %s\n",
items.get(i).itemName,
items.get(i).value,
WordEndings.convertRubles((int) Math.floor(items.get(i).value))));
}
completeList.append("------------------------\n");
completeList.append(String.format("Итого:\n\t\t\t%.2f %s\n",
total,
WordEndings.convertRubles((int) Math.floor(total))));
completeList.append("------------------------\n");
double eachPart = Math.ceil(total / persons * 100) / 100;
completeList.append(String.format("С каждого по %.2f %s",
eachPart,
WordEndings.convertRubles((int) Math.floor(eachPart))));
// Проверяем остаток и если он есть, то делаем приписку
double rest = total - eachPart * persons;
if (rest < 0) completeList.append(String.format(",\nа лишние %.2f %s в качестве чаевых.", -rest, WordEndings.convertRubles((int) Math.floor(-rest))));
return completeList.toString();
}
}