forked from Yandex-Practicum/Java-Module-Project-YP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalc.java
More file actions
67 lines (45 loc) · 1.95 KB
/
Calc.java
File metadata and controls
67 lines (45 loc) · 1.95 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
import java.util.ArrayList;
public final class Calc {
private final int personsCount;
private double total;
private final ArrayList<Product> order;
final static String[] valueToFormat = {"рубль", "рубля", "рублей"};
private int maxLength = 0;
public static Calc getInstance(final MyScanner scanner) {
Calc calc = null;
while (calc == null) {
System.out.println("На скольких человек необходимо разделить счёт?");
int personsCount = scanner.nextInt();
if (personsCount >= 2) {
calc = new Calc(personsCount, new ArrayList<Product>());
} else {
MyScanner.errorMessage();
}
}
return calc;
}
public void addProduct(final String name, final double price) {
Product newProduct = new Product(name, price);
order.add(newProduct);
total += price;
maxLength = Math.max(maxLength, name.length());
}
public void printOrder() {
System.out.println("Добавленные товары:");
for (Product product : order) {
String name = product.getName();
double price = product.getPrice();
System.out.println(String.format(name + ".".repeat(maxLength - name.length() + 5) + "%.2f" +
Formatter.formatValue(price, valueToFormat), price));
}
double eachToPay = total / personsCount;
String total = String.format("Сумма к оплате для каждого человека: %.2f %s",
eachToPay, Formatter.formatValue(eachToPay, valueToFormat));
System.out.println("_".repeat(total.length()));
System.out.println(total);
}
Calc(int personsCount, ArrayList<Product> order) {
this.personsCount = personsCount;
this.order = order;
}
}