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
48 lines (41 loc) · 1.67 KB
/
Calculator.java
File metadata and controls
48 lines (41 loc) · 1.67 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
import java.util.HashMap;
import java.util.Locale;
public class Calculator {
private final HashMap<String, Double> goods = new HashMap<>();
public void addGoods() {
while (true) {
String command;
System.out.println("Добавить товар? Для окончания введите 'завершить'");
command = Main.scanner.nextLine();
if (command.equalsIgnoreCase("завершить")) {
break;
}
System.out.println("Введите стоимость товара например: '10.45'");
if (Main.scanner.hasNextDouble()) {
double price = Main.scanner.nextDouble();
storeGoods(command, price);
Main.scanner.nextLine();
} else {
System.out.println("Введено неверное значение");
Main.scanner.nextLine();
}
}
Main.scanner.close();
}
private void storeGoods(String name, double price) {
if (goods.containsKey(name)) {
double calculated = goods.get(name) + price;
goods.put(name, calculated);
} else {
goods.put(name, price);
}
System.out.printf("Товар %s успешно добавлен%n", name);
}
public void printGoods() {
System.out.println("Добавленные товары:");
goods.forEach((k, v) -> System.out.printf(Locale.US, "%s %.2f%n", k, v));
}
public double getFinalSum(int numberOfPersons) {
return goods.values().stream().mapToDouble(Double::doubleValue).sum() / numberOfPersons;
}
}