Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
112 lines (100 loc) · 4.77 KB
/
Copy pathMain.java
File metadata and controls
112 lines (100 loc) · 4.77 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
import java.math.RoundingMode;
import java.util.Scanner;
import java.util.ArrayList;
import java.math.BigDecimal;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
ArrayList<String> itemsList = new ArrayList<>();
double allItems = 0;
while (true) {
askWhatToDo();
String command = scanner.next();
if (command.equalsIgnoreCase("1")) {
while (true) {
System.out.println("Введите название товара:");
String itemName = scanner.next();
itemsList.add(itemName);
System.out.println("Введите стоимость товара:");
double itemPrice;
while (true) {
while (!scanner.hasNextDouble()) {
System.out.println("Ошибка: введено не число. Попробуйте еще раз:");
scanner.next();
}
itemPrice = scanner.nextDouble();
if (itemPrice <= 0) {
System.out.println("Ошибка: число отрицательное, или ноль. Попробуйте еще раз:");
} else {
break;
}
}
allItems += itemPrice;
System.out.println("Товар внесен в список!");
System.out.println("Хотите добавить еще один товар? \n-Да\n-Нет");
String commandAddMoreItem = scanner.next();
if (commandAddMoreItem.equalsIgnoreCase("Да")) {
continue;
} else {
break;
}
}
} else if (command.equalsIgnoreCase("2")) {
System.out.println("Добавленные товары: ");
printItemsList(itemsList);
System.out.println("Сумма вашего заказа: " + allItems);
System.out.println("Если поделить счёт на всех, то получится: " + formatCurrency(calculate(allItems)));
} else if (command.equalsIgnoreCase("3")) {
System.out.println("Ваши товары:");
printItemsList(itemsList);
System.out.println("Сумма вашего заказа: " + allItems);
} else if (command.equalsIgnoreCase("Завершить")) {
break;
} else {
System.out.println("Извините, такой команды не существует.");
}
}
}
public static void printItemsList(ArrayList<String> itemList) {
for (String item : itemList) {
System.out.println(item);
}
}
public static double calculate(double totalBill) {
System.out.println("Введите количество людей для разделения счета:");
Scanner scanner = new Scanner(System.in);
int givenNumberOfPeople = scanner.nextInt();
while (givenNumberOfPeople < 1) {
System.out.println("Введенное число неверно. Попробуйте еще раз");
givenNumberOfPeople = scanner.nextInt();
}
if (givenNumberOfPeople > 1) {
BigDecimal totalAmount = BigDecimal.valueOf(totalBill);
BigDecimal numberOfPeople = BigDecimal.valueOf(givenNumberOfPeople);
BigDecimal share = totalAmount.divide(numberOfPeople, 2, RoundingMode.HALF_UP);
return share.doubleValue();
} else {
System.out.println("Количество человек: 1. Счет не делится =(");
return totalBill;
}
}
public static void askWhatToDo () {
System.out.println("Что вы хотите сделать?");
System.out.println("1 - Ввести наименование товара");
System.out.println("2 - Разделить счёт");
System.out.println("3 - Принести счёт");
System.out.println("'Завершить' - Выход");
}
public static String formatCurrency(double amount) {
int lastDigit = (int) amount % 10;
String currency;
if (lastDigit == 1) {
currency = "рубль";
} else if (lastDigit >= 2 && lastDigit <= 4) {
currency = "рубля";
} else {
currency = "рублей";
}
return String.format("%.2f %s", amount, currency);
}
}