Repository navigation
Expand file tree
/
Copy pathCalculator.java
More file actions
75 lines (65 loc) · 2.69 KB
/
Copy pathCalculator.java
File metadata and controls
75 lines (65 loc) · 2.69 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
import java.util.Scanner;
import java.util.ArrayList;
public class Calculator {
private final ArrayList<Product> products;
private double totalCost;
public Calculator() {
products = new ArrayList<>();
totalCost = 0;
}
public void addProduct() {
Scanner scanner = new Scanner(System.in);
System.out.print("Введите название товара: ");
String name = scanner.nextLine();
double price;
while (true) {
System.out.print("Введите стоимость товара (в формате рубли,копейки): ");
if (scanner.hasNextDouble()) {
price = scanner.nextDouble();
if (price <= 0) {
System.out.println("Стоимость не может быть отрицательной или равна нулю, повторите ввод.");
} else {
break;
}
} else {
System.out.println("Не введена стоимость, повторите ввод.");
scanner.next();
}
}
Product product = new Product(name, price);
totalCost += price;
products.add(product);
System.out.println("Товар успешно добавлен!");
}
public void printProducts() {
if (products.size() == 0) {
System.out.println("Корзина пуста");
return;
}
System.out.println("Список добавленных товаров:");
for (int i = 0; i < products.size(); i++) {
String formattedPrice = Formatter.formatTotalCost(products.get(i).getPrice());
System.out.println((i+1) + ". " + products.get(i).getName() + " - " + formattedPrice);
}
String formattedTotalCost = Formatter.formatTotalCost(totalCost);
System.out.println("Общая стоимость товаров: " + formattedTotalCost);
}
public void makeBasketProducts() {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print("Хотите добавить товар? (Введите Да или Завершить): ");
String answer = scanner.nextLine();
if (answer.equalsIgnoreCase("Да")) {
addProduct();
} else if (answer.equalsIgnoreCase("Завершить")) {
break;
} else {
System.out.println("Неверный ответ. Попробуйте ещё раз.");
}
}
printProducts();
}
public double getTotalCost() {
return totalCost;
}
}