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
70 lines (64 loc) · 2.92 KB
/
Calculator.java
File metadata and controls
70 lines (64 loc) · 2.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import java.io.BufferedReader;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
public class Calculator {
private static final String TERMINATE_COMMAND = "завершить";
private static final char PRICE_DELIMITER = '.';
private static final char MIN_DIGIT = '0';
private static final char MAX_DIGIT = '9';
private final BufferedReader reader;
private final int peopleCount;
private final PricePlurals formatter;
public Calculator(BufferedReader reader, int peopleCount, PricePlurals formatter) {
this.reader = reader;
this.peopleCount = peopleCount;
this.formatter = formatter;
}
public CalculationResult calculate() throws IOException {
final List<String> goodList = new LinkedList<>();
float totalPrice = 0F;
while (true) {
System.out.println("Введите наименование товара");
final String good = reader.readLine();
float price = 0F;
while (price <= 0) {
System.out.println("Введите цену товара. Число должно быть положительным.");
final String potentialPrice = reader.readLine();
if (isNotValidPrice(potentialPrice)) {
System.out.println("Цена введена неправильно. Разрешены только цифры и формате 0,00 или 0 ");
continue;
}
price += Float.parseFloat(potentialPrice);
}
goodList.add(good);
totalPrice += price;
System.out.println("Товар успешно добален");
System.out.println("Напишите любой символ для добавления товара\n" + "или слово \"Завершить\" если хотите перейти к расчетам ");
final String command = reader.readLine();
if (TERMINATE_COMMAND.equalsIgnoreCase(command)) {
break;
}
}
System.out.println("Общая сумма составила "+ totalPrice +" руб.");
final float pricePerPerson = totalPrice / peopleCount;
final String currencySuffix = formatter.getSuffix(pricePerPerson);
return new CalculationResult(goodList, pricePerPerson, currencySuffix);
}
private boolean isNotValidPrice(String price) {
boolean dotIncluded = false;
final int length = price.length();
for (int i = 0; i < length; ++i) {
final char symbol = price.charAt(i);
if (symbol == PRICE_DELIMITER) {
if (dotIncluded) return true;
dotIncluded = true;
continue;
}
if (symbol < MIN_DIGIT || symbol > MAX_DIGIT) {
return true;
}
}
return false;
}
}