forked from Yandex-Practicum/Java-Module-Project-YP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
103 lines (80 loc) · 3.98 KB
/
Main.java
File metadata and controls
103 lines (80 loc) · 3.98 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
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.text.DecimalFormat;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
List<String> items = new ArrayList<>();
List<Double> prices = new ArrayList<>();
System.out.println("Здравствуйте! Сколько гостей присутствовало?: ");
while (!scanner.hasNextInt()) {
scanner.next();
System.out.println("Ошибка! Введите положительное целое число: ");
}
int number = scanner.nextInt();
while (number <= 1) {
if (number == 1) {
System.out.println("Ошибка! Введите число больше 1: ");
} else {
System.out.println("Ошибка! Вы ввели число меньше 1. Введите количество гостей: ");
}
while (!scanner.hasNextInt()) {
scanner.next();
System.out.println("Ошибка! Введите положительное целое число: ");
}
number = scanner.nextInt();
}
System.out.println("Спасибо! Количество гостей: " + number);
scanner.nextLine();
double total = 0.0;
boolean continueAddingItems = true;
while (continueAddingItems) {
System.out.println("Введите название товара (для завершения введите 'Завершить'): ");
String itemName = scanner.nextLine();
if (itemName.equalsIgnoreCase("Завершить")) {
continueAddingItems = false;
} else if (!itemName.matches("[а-яА-ЯёЁ\\s]+")) {
System.out.println("Ошибка! Название товара может содержать только кириллицу. Начнём заново.");
continue;
} else {
items.add(itemName);
System.out.println("Вы успешно добавили товар. \nВведите цену товара с копейками (например, 07.50, 10.05): ");
String priceString = scanner.nextLine();
if (!priceString.matches("\\d+(\\.\\d{2})")) {
System.out.println("Ошибка! Стоимость нужно ввести с копейками (например, 07.50, 10.05). Начнём заново ");
continue;
}
double price = Double.parseDouble(priceString);
prices.add(price);
total += price;
}
}
System.out.println("\nСписок добавленных товаров: ");
for (int i = 0; i < items.size(); i++) {
System.out.println(items.get(i) + ": " + prices.get(i));
}
System.out.println("\nОбщая стоимость товаров: " + formatRubles(total));
double paymentPerGuest = total / number;
System.out.println("Каждый гость должен заплатить: " + formatRubles(paymentPerGuest));
}
public static String formatRubles(double amount) {
DecimalFormat decimalFormat = new DecimalFormat("#0.00");
String amountStr = decimalFormat.format(amount);
int rubles = (int) amount;
int lastTwoDigits = rubles % 100;
if (lastTwoDigits >= 11 && lastTwoDigits <= 14) {
return amountStr + " рублей";
}
int lastDigit = lastTwoDigits % 10;
String rublesStr;
if (lastDigit == 1) {
rublesStr = amountStr + " рубль";
} else if (lastDigit >= 2 && lastDigit <= 4) {
rublesStr = amountStr + " рубля";
} else {
rublesStr = amountStr + " рублей";
}
return rublesStr;
}
}