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
57 lines (55 loc) · 2.51 KB
/
Calculator.java
File metadata and controls
57 lines (55 loc) · 2.51 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
public class Calculator {
String productNameAndPrice = "Добавленные товары:\n";
double productPrice = 0;
public double finalPrice = 0;
//Основная логика калькулятора
public void count(){
while (true) {
System.out.println("Введите название товара :");
String name = Main.scanner.next();
System.out.println("Введите стоимость в формате рубли.копейки :");
double price;
//Проверка ввода
do {
try {
String checkPrice = Main.scanner.next();
price = Double.parseDouble(checkPrice);
if (price > 0){
break;
}
else {
System.out.println("Введено отрицательное значение. Повторите ввод:");
}
}
catch (Exception e){
System.out.println("Введено некоректное значение. Повторите ввод:");
}
} while(true);
listing(name,price);
check(price);
System.out.println("Хотите добавить еще один товар? Введите \"Завершить\" для итогового подсчета.");
String confirm = Main.scanner.next();
if(confirm.equalsIgnoreCase("завершить")){
break;
}
}
System.out.println(productNameAndPrice);
perPersonCheck();
}
//Список всех товаров
public void listing(String name, double price){
Formatter formatter = new Formatter();
productNameAndPrice = productNameAndPrice.concat(name.trim()).concat(" - " + price + formatter.formatPrice(price) + "\n");
System.out.println("Товар успешно добавлен.");
}
//Подсчет общей суммы заказа
public void check(double price){
productPrice += price;
}
//Расчет на каждого гостя
public void perPersonCheck(){
Formatter formatter = new Formatter();
finalPrice = productPrice / Main.guestCounter;
System.out.println("Итоговая сумма на кажлого человека: " + String.format("%.2f", finalPrice) + formatter.formatPrice(finalPrice));
}
}