Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
101 lines (81 loc) · 3.79 KB
/
Copy pathMain.java
File metadata and controls
101 lines (81 loc) · 3.79 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
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int numberPeople;
while (true) {
System.out.print("На скольких человек необходимо разделить счет: ");
if (scanner.hasNextInt()) {
numberPeople = scanner.nextInt();
if (numberPeople <= 1) {
System.out.println("Некорректное значение. Введите количество людей больше 1.");
} else {
break;
}
} else {
scanner.next();
System.out.println("Некорректное значение. Введите целое число.");
}
}
System.out.println("Создан калькулятор для " + numberPeople + " человек");
List<Product> products = new ArrayList<>();
while (true) {
System.out.print("Введите название товара или 'Завершить' для окончания: ");
String name = scanner.next();
if (name.equalsIgnoreCase("Завершить")) {
break;
}
System.out.print("Введите стоимость товара в формате рубли.копейки: ");
double price;
while (true) {
String priceInput = scanner.next();
try {
price = Double.parseDouble(priceInput);
if (price < 0) {
System.out.println("Некорректное значение. Введите положительное число.");
} else {
break;
}
} catch (NumberFormatException e) {
System.out.println("Некорректное значение. Введите число.");
}
}
Product product = new Product(name, price);
products.add(product);
System.out.println("Товар успешно добавлен.");
System.out.print("Хотите добавить еще один товар? (Введите 'Завершить' для окончания): ");
String continueInput = scanner.next();
if (continueInput.equalsIgnoreCase("Завершить")) {
break;
}
}
System.out.println("Список товаров: ");
double total = 0;
for (Product product : products) {
System.out.println(product.getName() + " - " + product.getPrice());
total += product.getPrice();
}
long roundedTotal = Math.round(total);
System.out.println("Общая сумма: " + roundedTotal);
double amountPerPerson = (double) roundedTotal / numberPeople;
long roundedAmount = Math.round(amountPerPerson);
String currency = getCurrencyString(roundedAmount);
System.out.println("Каждому человеку необходимо заплатить: " + roundedAmount + " " + currency);
}
private static String getCurrencyString(long amount) {
int roundedAmount = (int) amount;
int lastDigit = roundedAmount % 10;
int lastTwoDigits = roundedAmount % 100;
if (lastTwoDigits >= 11 && lastTwoDigits <= 19) {
return "рублей";
} else if (lastDigit == 1) {
return "рубль";
} else if (lastDigit >= 2 && lastDigit <= 4) {
return "рубля";
} else {
return "рублей";
}
}
}