forked from Yandex-Practicum/Java-Module-Project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
74 lines (62 loc) · 3.43 KB
/
Main.java
File metadata and controls
74 lines (62 loc) · 3.43 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
import java.lang.NumberFormatException;
import java.util.Scanner;
public class Main {
private static final Scanner scanner = new Scanner(System.in);
private static String getRoubleWordWithCorrectEnding(int number) {
if (number % 100 > 9 && number % 100 < 20) {
return "рублей";
}
switch (number % 10) {
case 1:
return "рубль";
case 2:
case 3:
case 4:
return "рубля";
default:
return "рублей";
}
}
public static void main(String[] args) {
System.out.println("На скольких человек необходимо разделить счет?");
String amountOfUsersAsString;
int amountOfUsers;
while (true) {
amountOfUsersAsString = scanner.nextLine();
try {
amountOfUsers = Integer.parseInt(amountOfUsersAsString);
if (amountOfUsers > 1) {
break;
}
System.out.println("Должно быть как минимум два человека, чтобы разделить счет!\nПовторите ввод еще раз!");
} catch (NumberFormatException exception) {
System.out.println("Введен недопустимый параметр в качестве количества человек! Ожидалось целое число.\nПовторите ввод еще раз!");
}
}
ProductCalculator calculator = new ProductCalculator();
double productPrice;
String productPriceAsString;
String productName;
System.out.println("Введите товар и его стоимость через пробел:");
while (!(productName = scanner.next()).equalsIgnoreCase("завершить")) {
productPriceAsString = scanner.nextLine();
try {
productPrice = Double.parseDouble(productPriceAsString);
if (productPrice < 0) {
System.out.println("У товара не может быть отрицательная цена!\nТовар не добавлен. Повторите ввод.");
continue;
}
calculator.addProduct(productName, productPrice);
System.out.println("Товар был успешно добавлен! Хотите добавить еще?\nЕсли да, то введите название товара и цену, иначе напишите слово \"завершить\":");
} catch (NumberFormatException exception) {
System.out.println("Не указана допустимая цена товара! Ожидалось вещественное число.\nТовар не был добавлен! Повторите корректный ввод заново.");
}
}
System.out.println("Добавленные товары:");
for (Product product : calculator.getProductsList()) {
System.out.println(product.getProductName());
}
double sumForEachToPay = calculator.getSumOfProducts() / amountOfUsers;
System.out.printf("Сумма, которую должен заплатить каждый: %.2f %s%n", sumForEachToPay, getRoubleWordWithCorrectEnding((int) sumForEachToPay));
}
}