Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
77 lines (63 loc) · 3.23 KB
/
Copy pathMain.java
File metadata and controls
77 lines (63 loc) · 3.23 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
import java.util.Scanner;
public class Main {
static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("На скольких человек нужно разделить счет?");
int number;
while (true) {
if (scanner.hasNextInt()) {
number = scanner.nextInt();
if (number <= 1) {
System.out.println("Число должно быть больше одного. Введите корректное число");
} else {
// Пропускаем перевод строки после числа
scanner.nextLine();
break;
}
} else {
System.out.println("Введен неверный формат числа. Пример: '2' , '5' и т.д.");
}
// Пропускаем перевод строки после числа
scanner.nextLine();
}
Calculator calculator = new Calculator();
calculator.numberOfPersons = number;
while (true) {
System.out.println("Введите название товара:");
String name = scanner.nextLine();
if (!name.isBlank()) {
while (true) {
System.out.println("Введите стоимость в формате 'рубли,копейки':");
if (scanner.hasNextDouble()) {
double price = scanner.nextDouble();
if (price <= 0) {
System.out.println("Стоимость товара должна быть больше нуля");
// Пропускаем перевод строки после числа
scanner.nextLine();
} else {
Product product = new Product(name, price);
calculator.add(product);
System.out.println("Товар успешно добавлен");
// Пропускаем перевод строки после числа
scanner.nextLine();
break;
}
} else {
System.out.println("Неверный формат стоимости");
// Пропускаем перевод строки после числа
scanner.nextLine();
}
}
} else {
System.out.println("Неверное название товара:");
}
System.out.println("Добавить еще товар? \n" +
"После добавления всех товаров введите команду 'завершить'");
String command = scanner.nextLine();
if (command.equalsIgnoreCase("завершить")) {
new ProductPrinter().print(calculator.productsList, calculator.getSumPerPerson());
break;
}
}
}
}