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
61 lines (47 loc) · 1.94 KB
/
Calculator.java
File metadata and controls
61 lines (47 loc) · 1.94 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
import java.util.ArrayList;
import java.util.Scanner;
public class Calculator {
final private int numbPeople;
Scanner scanner = new Scanner(System.in);
final private ArrayList<Product> arrProducts = new ArrayList<>();
Calculator(int numbPeople) {
this.numbPeople = numbPeople;
addProduct();
}
private void addProduct() {
while (true) {
arrProducts.add(new Product());
System.out.println("Товар \""
+ arrProducts.get(arrProducts.size() - 1).getProductName()
+ "\" успешно добавлен. \n Введите \"Завершить\" чтобы перейти к подсчёту "
+ "или нажмите \"Enter\" чтобы продолжить работу.");
if (isFinish()) {
finalCount();
break;
}
}
}
private void finalCount() {
double sum = 0, res;
StringBuilder resStrB = new StringBuilder("Добавленные товары: \n");
for (Product product : arrProducts) {
resStrB.append( product.getProductName()
.concat(": ")
.concat(
String.format("%.2f", product.getProductPrice()))
.concat(Formatter.getEndWord(product.getProductPrice()))
.concat(",\n"));
sum += product.getProductPrice();
}
System.out.println(resStrB.substring(0, resStrB.length() - 2) + ".");
res = sum / numbPeople;
System.out.println("--------");
System.out.println(
String.format("ИТОГ: каждый должен заплатить %.2f ", res) +
Formatter.getEndWord(res));
}
private boolean isFinish() {
String tmpStr = scanner.nextLine().trim();
return "завершить".equalsIgnoreCase(tmpStr);
}
}