forked from Yandex-Practicum/Java-Module-Project-YP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAccountCalculator.java
More file actions
104 lines (97 loc) · 3.95 KB
/
AccountCalculator.java
File metadata and controls
104 lines (97 loc) · 3.95 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
96
97
98
99
100
101
102
103
104
import java.util.ArrayList;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class AccountCalculator {
public int setPeopleAmount() {
while (true) {
Scanner scanner = new Scanner(System.in);
System.out.println("На скольких человек необходимо разделить счёт?");
if(scanner.hasNextInt()) {
int peopleAmount = scanner.nextInt();
if (peopleAmount <= 1){
showErrorMessage(Integer.toString(peopleAmount));
} else {
return peopleAmount;
}
} else {
showErrorMessage(scanner.nextLine());
}
}
}
public ArrayList<Product> createList() {
ArrayList<Product> products = new ArrayList<>();
String nameProduct;
double priceProduct;
while (true) {
nameProduct = addProductName();
priceProduct = addProductPrice();
Product product = new Product(nameProduct, priceProduct);
products.add(product);
System.out.println("Товар успешно добавлен в калькулятор");
Formatter formatter = new Formatter();
System.out.println("Текущая сумма товаров - " + formatter.show(sum(products)));
System.out.println("\nЧтобы продолжить заполнять счет - Введите любой символ\n" +
"Для завершения - Введите команду 'завершить'");
Scanner scanner = new Scanner(System.in);
String command = scanner.next();
if (command.equalsIgnoreCase("Завершить")) {
break;
}
}
return products;
}
private String addProductName () {
String nameProduct;
while (true) {
Scanner scanner = new Scanner(System.in);
System.out.println("Введите название товара");
nameProduct = scanner.nextLine();
Pattern pattern = Pattern.compile("\\S");
Matcher matcher = pattern.matcher(nameProduct);
if (nameProduct.isEmpty() || !matcher.find()) {
showErrorMessage("null");
} else {
break;
}
}
return nameProduct;
}
private Double addProductPrice (){
double priceProduct;
while (true){
Scanner scanner = new Scanner(System.in);
System.out.println("Введите стоимость товара");
if (scanner.hasNextDouble()) {
priceProduct = scanner.nextDouble();
if(priceProduct>=0){
break;
} else {
showErrorMessage(Double.toString(priceProduct));
}
} else {
showErrorMessage(scanner.nextLine());
}
}
return priceProduct;
}
private double sum (ArrayList<Product> products){
double sum = 0;
for (Product product : products){
sum += product.price;
}
return sum;
}
public void showList (int peopleAmount, ArrayList<Product> products){
System.out.println("Добавленные товары:\n");
Formatter formatter = new Formatter();
for (Product product: products){
System.out.println(product.name + " " + formatter.show(product.price));
}
double amountPerPerson = sum(products)/peopleAmount;
System.out.println("\nКаждый человек должен заплатить -> " + formatter.show(amountPerPerson));
}
private void showErrorMessage(String input) {
System.out.println("Ошибка: '" + input + "' - некорректное значение для подсчёта");
}
}