Repository navigation
Expand file tree
/
Copy pathCalculator.java
More file actions
128 lines (108 loc) · 5.03 KB
/
Copy pathCalculator.java
File metadata and controls
128 lines (108 loc) · 5.03 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import java.util.ArrayList;
import java.util.Scanner;
class BillCalculator {
private int numberOfPeople;
private final ArrayList<Item> items;
private double totalAmount;
public BillCalculator() {
this.items = new ArrayList<>();
this.totalAmount = 0.0;
}
// Запросить у пользователя количество людей и проверять правильность ввода
public void askNumberOfPeople() {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print("На скольких человек необходимо разделить счёт? ");
if (scanner.hasNextInt()) {
int numPeople = scanner.nextInt();
if (numPeople > 1) {
this.numberOfPeople = numPeople;
System.out.println("Количество человек: " + numPeople);
break;
} else if (numPeople == 1) {
System.out.println("Нет смысла ничего считать и делить на одного человека.");
} else {
System.out.println("Некорректное значение. Введите число больше 1.");
}
} else {
System.out.println("Некорректное значение. Введите число больше 1.");
scanner.next();
}
}
}
// Добавление товаров с проверкой на корректность ввода
public void addItems() {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print("Введите название товара: ");
String itemName = scanner.nextLine();
double itemPrice;
while (true) {
System.out.print("Введите стоимость товара (в формате рубли.копейки): ");
if (scanner.hasNextDouble()) {
itemPrice = scanner.nextDouble();
scanner.nextLine();
if (itemPrice >= 0) {
break;
} else {
System.out.println("Стоимость товара не может быть отрицательной. Пожалуйста, введите положительное значение.");
}
} else {
System.out.println("Некорректный формат стоимости. Пожалуйста, введите стоимость в формате рубли.копейки.");
scanner.next();
}
}
Item item = new Item(itemName, itemPrice);
items.add(item);
totalAmount += itemPrice;
System.out.println("Товар успешно добавлен!");
System.out.print("Хотите добавить ещё один товар? (Введите 'Завершить' для завершения): ");
String userResponse = scanner.nextLine().toLowerCase();
if (userResponse.equalsIgnoreCase("завершить")) {
break;
}
}
System.out.println("Добавление товаров завершено.");
}
// Печать итогового счета
public void printBill() {
System.out.println("Добавленные товары:");
for (Item item : items) {
System.out.println("- " + item);
}
String totalFormatted = CurrencyFormatter.formatCurrency(totalAmount);
String perPersonFormatted = CurrencyFormatter.formatCurrency(totalAmount / numberOfPeople);
String rublesWordTotal = CurrencyFormatter.getRublesWord(totalAmount);
String rublesWordPerPerson = CurrencyFormatter.getRublesWord(totalAmount / numberOfPeople);
System.out.println("Общая сумма: " + totalFormatted + " " + rublesWordTotal);
System.out.println("Сумма на каждого человека: " + perPersonFormatted + " " + rublesWordPerPerson);
}
}
class Item {
private final String name;
private final double price;
public Item(String name, double price) {
this.name = name;
this.price = price;
}
@Override
public String toString() {
return name + " - " + String.format("%.2f", price) + " руб.";
}
}
class CurrencyFormatter {
public static String formatCurrency(double amount) {
return String.format("%.2f", amount);
}
public static String getRublesWord(double amount) {
int intPart = (int) Math.floor(amount);
if (intPart % 100 >= 11 && intPart % 100 <= 14) {
return "рублей";
}
return switch (intPart % 10) {
case 1 -> "рубль";
case 2, 3, 4 -> "рубля";
default -> "рублей";
};
}
}