forked from Yandex-Practicum/Java-Module-Project-YP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalculat.java
More file actions
76 lines (62 loc) · 2.74 KB
/
Calculat.java
File metadata and controls
76 lines (62 loc) · 2.74 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
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
class Calculat{
private int numberOfGuests;
private List<Product> products = new ArrayList<>();
public Calculat(int numberOfGuests) {
this.numberOfGuests = numberOfGuests;
}
public void addProducts() {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print("Введите название товара или 'Завершить' для окончания: ");
String productName = scanner.nextLine();
if (productName.equalsIgnoreCase("завершить")) {
break;
} else if (productName.equalsIgnoreCase("Завершить")) {
break;
} else if (productName.equalsIgnoreCase("ЗАВЕРШИТЬ")) {
break;
} else if (productName.equalsIgnoreCase("заВЕрШиТь")) {
break;
}
System.out.print("Введите стоимость товара в рублях: ");
double productPrice = scanner.nextDouble();
if (productPrice <= 0) {
System.out.println("Некорректный ввод. Цена товара должна быть положительной.");
continue;
}
products.add(new Product(productName, productPrice));
System.out.println("Товар успешно добавлен!");
scanner.nextLine();
}
}
public void calculateAndDisplay() {
double totalBill = 0;
for (Product product : products) {
totalBill += product.getPrice();
}
double amountPerPerson = totalBill / numberOfGuests;
System.out.println("\nДобавленные товары:");
for (Product product : products) {
System.out.println(product.getName() + " - " + formatPrice(product.getPrice()));
}
System.out.println("\nОбщий счёт: " + formatPrice(totalBill));
System.out.println("Сумма на человека: " + formatPrice(amountPerPerson));
}
private String formatPrice(double price) {
double roundedPrice = Math.floor(price * 100) / 100;
long integerPart = (long) roundedPrice;
long lastTwoDigits = integerPart % 100;
if (lastTwoDigits >= 11 && lastTwoDigits <= 14) {
return roundedPrice + " рублей";
} else if (lastTwoDigits % 10 == 1) {
return roundedPrice + " рубль";
} else if (lastTwoDigits % 10 >= 2 && lastTwoDigits % 10 <= 4) {
return roundedPrice + " рубля";
} else {
return roundedPrice + " рублей";
}
}
}