Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
84 lines (74 loc) · 3.24 KB
/
Copy pathMain.java
File metadata and controls
84 lines (74 loc) · 3.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import java.util.Scanner;
public class Main {
public static int customers = 0;
public static String[] strings = {
"Привет. Это калькулятор счета.",
"Введите КОЛ-ВО человек (или 'завершить' для выхода):",
"Неверно: Должно быть целое число. 1 < ЧИСЛО < 1000",
"Товар успешно добавлен в счет",
"Введите НАЗВАНИЕ товара (или 'завершить' для выхода): ",
"Неверно: Название должно содержать текст (буквы, цифры, некоторые знаки препинания, кроме запятой)",
"Введите СТОИМОСТЬ товара (или 'завершить' для выхода): ",
"Неверно: 0.00 <= ЦЕНА < 150000000.00,\n Цена, это число не более 2х знаков после запятой."};
static BillCalc calculator;
public static void main(String[] args) {
printString(0);
if (customersCount()) return;
calculator = new BillCalc(customers);
addItems();
calculator.printTotal();
}
public static boolean customersCount() {
Scanner scanner = new Scanner(System.in);
while (true) {
printString(1);
String input = scanner.nextLine();
if (input.trim().equalsIgnoreCase("завершить")) return true;
try {
customers = Integer.parseInt(input);
if (customers > 1 && customers < 1000) break;
} catch (NumberFormatException ignored) {}
printString(2);
}
return false;
}
public static void addItems() {
Scanner scanner = new Scanner(System.in);
boolean flag = true;
while (flag) {
printString(4);
String itemName = scanner.nextLine();
if (itemName.trim().equalsIgnoreCase("завершить")) flag = false;
else if (!isText(itemName)) {
printString(5);
continue;
}
while(flag) {
printString(6);
String itemPrice = scanner.nextLine();
if (itemPrice.trim().equalsIgnoreCase("завершить")) flag = false;
else if (isPrice(itemPrice)) {
calculator.addItem(itemName, itemPrice);
printString(3);
break;
}
else printString(7);
}
}
}
private static boolean isText(String value) {
return value.matches("[a-zA-Zа-яА-Я\\d\\p{Punct} &&[^,]]+");
}
private static boolean isPrice(String value) {
try {
double number = Double.parseDouble(value);
int decimalPlaces = value.indexOf('.') >= 0 ? value.length() - value.indexOf('.') - 1 : 0;
return number >= 0 && number < 150000000.00 && decimalPlaces <= 2;
} catch (NumberFormatException e) {
return false;
}
}
public static void printString(int arg) {
System.out.println(strings[arg]);
}
}