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
76 lines (63 loc) · 2.93 KB
/
Calculator.java
File metadata and controls
76 lines (63 loc) · 2.93 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
71
72
73
74
75
76
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Locale;
public class Calculator {
private ArrayList<Product> products = new ArrayList<>();
private int numberOfPeople;
private Scanner scanner = new Scanner(System.in);
private Formatter formatter = new Formatter();
private void addItem(Product product) {
products.add(product);
}
private void showSummary() {
System.out.println("Список добавленных товаров:");
double totalCost = 0;
for (Product product : products) {
System.out.println("- " + product.getName());
totalCost += product.getPrice();
}
System.out.println("Общая стоимость: " + totalCost + " руб.");
System.out.println("Сумма, которую должен заплатить каждый человек: " + formatter.formatTotalCost(totalCost, numberOfPeople));
}
void action() {
while (true) {
System.out.print("Введите название товара (или 'Завершить' для завершения): ");
String productName = scanner.nextLine().trim();
if (productName.equalsIgnoreCase("Завершить")) {
break;
}
double productCost;
while (true) {
System.out.print("Введите стоимость товара (в формате рубли.копейки): ");
try {
productCost = Double.parseDouble(scanner.nextLine());
if (productCost > 0) {
break;
} else {
System.out.println("Введите положительное число");
}
} catch (NumberFormatException e) {
System.out.println("Некорректный формат стоимости товара. Пожалуйста, введите число (в формате рубли.копейки).");
}
}
addItem(new Product(productName, productCost));
System.out.println("Товар успешно добавлен!");
while (true) {
System.out.print("Хотите добавить ещё один товар? (да/нет): ");
String answer = scanner.next().trim();
if (answer.equalsIgnoreCase("да")) {
scanner.nextLine();
break;
} else if (answer.equalsIgnoreCase("нет")) {
showSummary();
return;
} else {
System.out.println("Некорректный ввод. Введите только 'да' или 'нет'.");
}
}
}
}
public Calculator(int numberOfPeople) {
this.numberOfPeople = numberOfPeople;
}
}