forked from Yandex-Practicum/Java-Module-Project-YP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProducts.java
More file actions
54 lines (47 loc) · 1.92 KB
/
Products.java
File metadata and controls
54 lines (47 loc) · 1.92 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
import java.util.Scanner;
class Products {
Scanner scan = new Scanner(System.in);
boolean ifWantFinish = true;
String[] dish = new String[100];
int i =0;
double[] expense = new double[100];
double price = 0;
double total = 0;
double calculatingTotalExpense() {
while (ifWantFinish) {
System.out.println("Введите название товара");
dish[i] = scan.next();
expense[i] = checkingIfValueIsNumber();
total = total + expense[i];
System.out.println("Товар " + dish[i] + " стоимостью " + expense[i] + " руб. успешно добавлен!");
i++;
System.out.println("\nВы хотите ввести еще товары? Если да, то введите любой текст. Если нет, введите 'Завершить'");
ifWantFinish = !scan.next().equalsIgnoreCase("Завершить");
}
System.out.println("Добавленные товары:");
for (int l = 0; l < i; l++) {
System.out.println((l + 1) + ". " + dish[l] + " Цена " + expense[l] + " руб.");
}
return total;
}
double checkingIfValueIsNumber () {
while(true) {
System.out.println("Введите цену товара в формате рубли,копейки");
while (true) {
if (scan.hasNextDouble()) {
break;
} else {
System.out.println("Ошибка! Введите число!");
scan.next();
}
}
price = scan.nextDouble();
if (price <= 0) {
System.out.println("Цена блюда не может быть отрицательной и не может быть равна нулю");
} else {
break;
}
}
return price;
}
}