forked from Yandex-Practicum/Java-Module-Project-YP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalculated.java
More file actions
79 lines (67 loc) · 2.87 KB
/
Calculated.java
File metadata and controls
79 lines (67 loc) · 2.87 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
77
78
79
import java.util.ArrayList;
import java.util.Scanner;
public class Calculated {
Scanner scanner = new Scanner(System.in);
ArrayList<Product> products = new ArrayList<>();
double totalAmount = 0.0;
public void countItems() {
String answer;
do {
System.out.println("Введите название товара:");
String name = readProductName();
double price = readProductPrice();
Product product = new Product(name, price);
products.add(product);
System.out.println("Товар \"" + product.name + "\" добавлен");
totalAmount += price;
System.out.println("Общая сумма всех товаров: " + String.format("%.2f", totalAmount) + " руб.");
do {
System.out.println("Хотите добавить еще один товар? (да/нет)");
answer = scanner.nextLine();
if (!answer.equalsIgnoreCase("да") && !answer.equalsIgnoreCase("нет")) {
System.out.println("Пожалуйста, введите 'да' или 'нет'.");
}
} while (!answer.equalsIgnoreCase("да") && !answer.equalsIgnoreCase("нет"));
} while (!answer.equalsIgnoreCase("нет"));
}
private String readProductName() {
String name;
do {
name = scanner.nextLine();
if (name.trim().isEmpty()) {
System.out.println("Название товара не может быть пустым. Пожалуйста, введите непустое название.");
}
} while (name.trim().isEmpty());
return name;
}
private double readProductPrice() {
double price;
do {
System.out.println("Введите стоимость товара в формате рубли,копейки");
while (!scanner.hasNextDouble()) {
System.out.println("Неверный формат. Пожалуйста, введите число в формате рубли,копейки.");
scanner.nextLine();
}
price = scanner.nextDouble();
scanner.nextLine();
} while (price <= 0);
return price;
}
public void displayProducts() {
System.out.println("Список добавленных товаров:");
for (Product item : products) {
System.out.println("Название: " + item.name + ", Стоимость: " + item.price + " руб.");
}
}
public void calculatePerPerson(int quantity) {
TotalAmountSplitter.calculatePerPerson(totalAmount, quantity);
}
}
class Product {
String name;
double price;
public Product(String name, double price) {
this.name = name;
this.price = price;
}
}