forked from Yandex-Practicum/Java-Module-Project-YP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBillSplitter.java
More file actions
80 lines (64 loc) · 3.02 KB
/
BillSplitter.java
File metadata and controls
80 lines (64 loc) · 3.02 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
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class BillSplitter {
public List<String> items;
public double totalAmount;
public int numberOfPeople;
public BillSplitter() {
items = new ArrayList<>();
totalAmount = 0.0;
}
public void splitBill() {
Scanner scanner = new Scanner(System.in);
System.out.print("Введите количество персон для разделения счета: ");
while (!scanner.hasNextInt()) {
System.out.println("Ошибка. Введите корректное количество персон: ");
scanner.next();
}
numberOfPeople = scanner.nextInt();
if (numberOfPeople <= 0) {
System.out.println("Ошибка. Количество персон должно быть больше нуля.");
return;
}
while (true) {
System.out.print("Введите название товара или 'Завершить' для итога: ");
String itemName = scanner.next();
if (itemName.equalsIgnoreCase("завершить")) {
break;
}
System.out.print("Введите количество товара: ");
while (!scanner.hasNextInt()) {
System.out.println("Ошибка. Введите корректное количество товара: ");
scanner.next();
}
int quantity = scanner.nextInt();
if (quantity <= 0) {
System.out.println("Ошибка. Количество товара должно быть больше нуля.");
return;
}
System.out.print("Введите цену товара: ");
while (!scanner.hasNextDouble()) {
System.out.println("Ошибка. Введите корректную цену товара: ");
scanner.next();
}
double price = scanner.nextDouble();
if (price < 0) {
System.out.println("Ошибка. Цена товара не может быть отрицательной.");
return;
}
double itemTotal = quantity * price;
totalAmount += itemTotal;
items.add(itemName + " - " + String.format("%.2f", itemTotal) + " " + Rubles.getSingularOrPlural(itemTotal));
}
}
public void printBill() {
double amountPerPerson = totalAmount / numberOfPeople;
System.out.println("\nСчет:");
for (String item : items) {
System.out.println(item);
}
System.out.println("\nОбщая сумма: " + String.format("%.2f", totalAmount) + " " + Rubles.getSingularOrPlural(totalAmount));
System.out.println("Сумма на каждого: " + String.format("%.2f", amountPerPerson) + " " + Rubles.getSingularOrPlural(amountPerPerson) + " на человека");
}
}