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
45 lines (38 loc) · 1.99 KB
/
Calculator.java
File metadata and controls
45 lines (38 loc) · 1.99 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
import java.util.Scanner;
public class Calculator {
double sum = 0.00;
String allProduct = "Добавленные товары: \n";
public void sumProduct() {
Scanner scanner = new Scanner(System.in);
Product product = new Product();
while (true) {
System.out.println("Введите название товара:");
product.productName = scanner.next();
allProduct = allProduct + product.productName + "\n";
while (true) {
System.out.println("Ведите стоимость товара (Стоимость должна быть в формате рубли.копейки, например 10,45 или 11,40)");
if (scanner.hasNextDouble()) {
product.cost = scanner.nextDouble();
if (product.cost <= 0) {
System.out.println("Стоимость товара должна быть выше 0");
} else {
break;
}
} else {
System.out.println("Неверный формат ввода");
scanner.next();
}
}
sum = sum + product.cost;
String formatedCost = String.format("%.2f", product.cost);
System.out.println("Товар " + product.productName + " добавлен. Его стоимость " + formatedCost);
System.out.println("Для добавления других товаров, введите любой символ. Для завршения ввода товаров наберите \"Завершить\"");
String exit = scanner.next();
if (exit.equalsIgnoreCase("Завершить")) {
String formatedSum = String.format("%.2f", sum);
System.out.println("Добавлено товаров на сумму: " + formatedSum);
break;
}
}
}
}