Repository navigation
Expand file tree
/
Copy pathCalculator.java
More file actions
54 lines (50 loc) · 2.24 KB
/
Copy pathCalculator.java
File metadata and controls
54 lines (50 loc) · 2.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
import java.util.ArrayList;
import java.util.Scanner;
public class Calculator {
Scanner scanner = new Scanner(System.in);
public void calculate(int peopleCount) {
ArrayList<Order> orders = new ArrayList<>();
double sum = 0;
String name;
double price;
while (true) {
System.out.println("Введите название товара и сумму, в формате рубли.копейки, " +
"для завершения добавления товаров введите \"Завершить\"");
String input = scanner.nextLine();
if (input.equalsIgnoreCase("Завершить")) {
break;
} else if (input.contains("-")) {
System.out.println("Отрицательная сумма, введите корректную сумму");
continue;
} else {
try {
name = input.replaceAll("[\\d.]", "").trim();
price = Double.parseDouble(input.replaceAll("[^.\\d]", "").trim());
} catch (NumberFormatException e) {
System.out.println("Не введено наименование позиции или сумма.");
continue;
}
orders.add(new Order(name, price));
}
}
System.out.println("Добавленные товары:");
for (int i = 0; i < orders.size(); i++) {
sum += orders.get(i).price;
System.out.println(orders.get(i).name + " " + orders.get(i).price);
}
double sumPerCount = sum / peopleCount;
System.out.println("Каждый человек должен заплатить " + String.format("%.2f", sumPerCount) + " "
+ pluralsCase(sumPerCount));
}
private static String pluralsCase(Double sum) {
int sumCase = sum.intValue();
if (sumCase % 10 == 1 && sumCase % 100 == 1) {
return "рубль";
} else if (sumCase % 10 >= 2 && sumCase % 10 <= 4 &&
(sumCase % 100 < 10 || sumCase % 100 >= 20)) {
return "рубля";
} else {
return "рублей";
}
}
}