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
58 lines (51 loc) · 2.32 KB
/
Calculator.java
File metadata and controls
58 lines (51 loc) · 2.32 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
import java.util.ArrayList;
import java.util.Scanner;
public class Calculator {
double totalPrice;
ArrayList<Product> product;
Scanner scanner = new Scanner(System.in);
public Calculator() {
product = new ArrayList<>();
}
public void add() {
while (true) {
System.out.println("Введите товар:");
String productInput = scanner.next();
while (true) {
System.out.println("Введите цену:");
double priceInput = 0;
if (scanner.hasNextDouble()) {
priceInput = scanner.nextDouble();
if (priceInput < 0) {
System.out.println("Цена не может быть меньше ноля");
} else {
System.out.println("Товар добавлен");
product.add(new Product(productInput, priceInput));
totalPrice += priceInput;
break;
}
} else {
System.out.println("Ошибка ввода, нужны цифры");
scanner.nextLine();
}
}
System.out.println("Хотите добавить еще товар?");
System.out.println("Введите Завершить для подсчёта, или любой символ что-бы добавить еще товар");
String end = scanner.next();
if (end.equalsIgnoreCase("Завершить") || productInput.equalsIgnoreCase("Завершить")) {
System.out.println("Расчёт завершён.");
break;
}
}
}
public void print(int people) {
Formatter formatter = new Formatter();
System.out.println("Добавленные товары");
for (int i = 0; i < product.size(); i++) {
System.out.println(product.get(i).name);
}
double summa = totalPrice / people;
System.out.println("Общая сумма: " + String.format("%.2f", totalPrice) + " " + formatter.formatter(totalPrice));
System.out.println("Каждый должен: " + String.format("%.2f", summa) + " " + formatter.formatter(summa));
}
}