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
63 lines (58 loc) · 2.42 KB
/
Calculator.java
File metadata and controls
63 lines (58 loc) · 2.42 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
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
public class Calculator {
int count = -1;
Scanner scanner = new Scanner(System.in);
List<Product> cart = new LinkedList<>();
double total;
Formatter formatter = new Formatter();
public void start() {
do {
System.out.println("на скольких человек необходимо разделить счёт?");
if (scanner.hasNextInt()) {
count = scanner.nextInt();
} else {
scanner.next();
}
if (count == 1) {
System.out.println("тут ничего считать и делить");
} else if (count < 1) {
System.out.println("укажите целое число, больше 1");
} else {
setCart();
result();
}
} while (count < 1);
scanner.close();
}
/**
* Добавление товаров в калькулятор
*/
private void setCart() {
while (true) {
System.out.println("укажите название товара или введите команду \"Завершить\"");
Product product = new Product(scanner.next());
if (product.getName().equalsIgnoreCase("Завершить")) {
break;
}
do {
System.out.println("укажите стоимость " + product.getName());
if (scanner.hasNextDouble()) {
product.setPrice(scanner.nextDouble());
} else {
scanner.next();
}
} while (product.getPrice() < 0);
cart.add(product);
total = cart.stream().mapToDouble(Product::getPrice).sum();
product.print();
}
}
private void result() {
System.out.println("Добавленные товары:");
cart.forEach(product -> System.out.println(product.getName() + " " + formatter.formatPrice(product.getPrice())));
System.out.printf("Текущая общая сумма всех товаров %s %s%n", formatter.formatPrice(total), formatter.formatCurrency(total));
System.out.printf("сумма, которую должен заплатить каждый поровну = %s%n", formatter.formatPrice(total / count));
}
}