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
64 lines (59 loc) · 2.24 KB
/
Calculator.java
File metadata and controls
64 lines (59 loc) · 2.24 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.Scanner;
import java.util.ArrayList;
import java.util.List;
public class Calculator {
private final List<Item> list = new ArrayList<>();
public void enterListOfItems() {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("Введите название товара");
String name = scanner.nextLine();
if (name.equals("")) {
System.out.println("Вы не ввели название товара, попробуйте снова");
continue;
}
System.out.println("Введите цену товара");
double cost;
if (scanner.hasNextDouble()) {
cost = scanner.nextDouble();
scanner.nextLine();
if (cost < 0) {
System.out.println("Цена не может быть отрицательной, попробуйте снова");
continue;
}
} else {
System.out.println("Вы некорректно ввели цену, попробуйте снова");
scanner.nextLine();
continue;
}
list.add(new Item(name, cost));
System.out.println("Товар добавлен");
System.out.println("Для завершения ввода товаров напишите \"Завершить\". Если хотите " +
"продолжить, нажмите Enter");
String endPhrase = scanner.nextLine();
if (endPhrase.equalsIgnoreCase("завершить")) {
break;
}
}
}
public double calculate(int people) {
if (list.isEmpty()) {
return 0;
} else {
double summa = 0;
for (Item item : list) {
summa += item.getCost();
}
return summa / people;
}
}
public void printNamesItems() {
if (list.isEmpty()) {
System.out.println("Нет товаров");
} else {
for (Item item : list) {
System.out.println(item.getName());
}
}
}
}