Repository navigation
Expand file tree
/
Copy pathCalculator.java
More file actions
65 lines (43 loc) · 2.35 KB
/
Copy pathCalculator.java
File metadata and controls
65 lines (43 loc) · 2.35 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
import java.util.Scanner;
public class Calculator {
String productList = "";
double totalProductPrice = 0;
public void calculate() {
Scanner scanner = new Scanner(System.in);
Formater formater = new Formater();
while (true) {
System.out.print("Введите название товара или \"Завершить\": ");
String productName = scanner.nextLine();
if (productName.equalsIgnoreCase("Завершить") || productName.equalsIgnoreCase("pfdthibnm")) {
break;
} else {
System.out.print("Введите стоимость товара в формате рубли.копейки:");
double priceForOne;
while (true) {
if (scanner.hasNextDouble()) {
priceForOne = scanner.nextDouble();
scanner.nextLine();
break;
} else {
System.out.println("Неверный формат стоимости товара. Попробуйте снова!");
scanner.nextLine();
}
}
if (priceForOne < 0) {
System.out.println("Ценане может быть отрицательной. Попробуйте снова!");
continue;
}
if (productName.trim().isEmpty()) {
System.out.println("Название товара не может быть пустым. Попробуйте снова!");
continue;
}
this.productList = productList + productName + " : " + String.format("%.2f", priceForOne) + " " +formater.formater(priceForOne) + "\n" ;
this.totalProductPrice = totalProductPrice + priceForOne;
System.out.println("Товар " + productName + " : " + String.format("%.2f", priceForOne) + " " + formater.formater(priceForOne) +" добавлен");
}
}
scanner.close();
System.out.println("Добавленные товары:\n" + productList);
System.out.println("Сумма всех товаров: " + String.format("%.2f", totalProductPrice) + " " + " " +formater.formater(totalProductPrice));
}
}