Repository navigation
Expand file tree
/
Copy pathCalculate.java
More file actions
95 lines (76 loc) · 3.48 KB
/
Copy pathCalculate.java
File metadata and controls
95 lines (76 loc) · 3.48 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import java.util.ArrayList;
import java.util.Scanner;
public class Calculate {
private final Scanner scanner = new Scanner(System.in);
String check = "";
Double countProducts = 0.0;
public void calculate(String name) {
ArrayList<Product> products = new ArrayList<>();
System.out.println(name + " Напишите название блюда");
while (true) {
String productName = scanner.next();
if (productName.equalsIgnoreCase("Завершить")) {
if (products.size() == 0) {
System.out.println("У " + name + " не добавлено ни одного блюда");
} else {
System.out.println("Добавденные товары: ");
for (int j = 0; j < products.size(); j++) {
double temp2 = products.get(j).count;
int convertRub2 = (int) temp2;
System.out.println(products.get(j).name + " - " + products.get(j).count + " " + convertRubDeclination(convertRub2));
}
}
break;
}
if (checkString(productName)) {
System.out.println("Вы ввели некорректное название блюда");
continue;
}
System.out.println(name + " Напишите цену блюда");
double productPrice;
while (true) {
try {
productPrice = Double.parseDouble(scanner.next());
if (productPrice <= 0) {
System.out.println("Вы ввели минусовую цену. Попробуйте ще раз.");
continue;
}
break;
} catch (Exception e) {
System.out.println("Вы ввели некорректное значение цены. Попробуйте еще раз.");
}
}
products.add(new Product(productName, productPrice));
double temp1 = productPrice;
int convertRub = (int) temp1;
System.out.println("Вы успешно добавили блюдо " + "\"" + productName + "\"" + " стоимостью " + productPrice + " " + convertRubDeclination(convertRub) + " в счет");
countProducts += productPrice;
check += productName + " - " + productPrice + " " + convertRubDeclination(convertRub) + "\n";
System.out.println("Хотите ли вы добавить ещё один товар?\nЕсли нет, напишите - Завершить, если хотите продолжить, напишите название следующего блюда.");
}
}
public String convertRubDeclination(int number) {
int lastDigit = number % 100 / 10;
if (lastDigit == 1) {
return "рублей";
}
switch (number % 10) {
case 1:
return "рубль";
case 2:
case 3:
case 4:
return "рубля";
default:
return "рублей";
}
}
private boolean checkString(String s) throws NumberFormatException {
try {
Integer.parseInt(s);
return true;
} catch (NumberFormatException e) {
return false;
}
}
}