Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
67 lines (62 loc) · 3.06 KB
/
Copy pathMain.java
File metadata and controls
67 lines (62 loc) · 3.06 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
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
BillCalculator billCalculator = null;
Formatter formatter = new Formatter();
int numberOfPeople = 0;
while (true) {
System.out.print("На сколько человек необходимо разделить счёт: ");
if (scanner.hasNextInt()) {
numberOfPeople = scanner.nextInt();
if (numberOfPeople <= 1) {
System.out.println("Ошибка: Количество человек должно быть больше 1.");
} else {
break;
}
} else {
System.out.println("Ошибка: неправильный формат ввода, введите число.");
scanner.next();
}
}
boolean firstProductAdded = false;
while (true) {
System.out.print("Введите название продукта или напишите [Завершить]: ");
String productName = scanner.next();
if (productName.equalsIgnoreCase("Завершить")) {
break;
}
System.out.print("Введите стоимость продукта в формате \"рубли,копейки\": ");
double price = 0.00;
while (true) {
if (scanner.hasNextDouble()) {
price = scanner.nextDouble();
scanner.nextLine();
if (price < 0) {
System.out.println("Ошибка: Стоимость товара не может быть отрицательной.");
} else {
break;
}
} else {
System.out.println("Ошибка: Введите число в формате \"рубли,копейки\".");
scanner.next();
}
}
if (billCalculator == null) {
billCalculator = new BillCalculator(price, numberOfPeople);
billCalculator.addProduct(productName, price);
} else {
billCalculator.addProduct(productName, price);
}
System.out.println("Продукт добавлен успешно.");
}
if (billCalculator != null) {
System.out.println(billCalculator.getProductsList());
System.out.println("Общий счёт: " + String.format("%.2f", billCalculator.getTotalBill()));
System.out.println("Каждый человек должен заплатить: " + String.format("%.2f", billCalculator.calculatePerPersonBill()) + " " + formatter.formatAmount(billCalculator.calculatePerPersonBill()));
} else {
System.out.println("Не были добавлены продукты. Программа завершена.");
}
scanner.close();
}
}