Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
76 lines (63 loc) · 2.96 KB
/
Copy pathMain.java
File metadata and controls
76 lines (63 loc) · 2.96 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
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("На скольких человек необходимо разделить счёт?");
int numPeople = readInt(scanner);
while (numPeople <= 1) {
System.out.println("Некорректное значение для подсчёта, количество людей должно быть больше 1.");
numPeople = readInt(scanner);
}
Calculator calculator = new Calculator();
String input;
do {
System.out.println("Введите название товара:");
String itemName = scanner.next();
System.out.println("Введите стоимость товара в формате рубли.копейки:");
double itemPrice = readPrice(scanner);
calculator.addItem(itemName, itemPrice);
System.out.println("Товар успешно добавлен.");
System.out.println("Хотите добавить ещё один товар? (Введите 'Завершить', чтобы закончить добавление):");
input = scanner.next();
} while (!input.equalsIgnoreCase("Завершить"));
System.out.println("Добавленные товары:");
System.out.println(calculator.getItems());
double perPerson = calculator.getTotal() / numPeople;
System.out.println(formatRubles(perPerson));
}
public static String formatRubles(double sum) {
int integerPart = (int) sum;
int remainder = integerPart % 10;
String rubleSuffix;
if (integerPart == 1) {
rubleSuffix = "рубль";
} else if (remainder > 1 && remainder < 5) {
rubleSuffix = "рубля";
} else {
rubleSuffix = "рублей";
}
return String.format("Каждый человек должен заплатить по %.2f %s", sum, rubleSuffix);
}
private static int readInt(Scanner scanner) {
while (!scanner.hasNextInt()) {
System.out.println("Ошибка: введите целое число:");
scanner.next();
}
return scanner.nextInt();
}
private static double readDouble (Scanner scanner) {
while (!scanner.hasNextDouble()) {
System.out.println("Ошибка: введите число в формате рубли.копейки:");
scanner.next();
}
return scanner.nextDouble();
}
private static double readPrice(Scanner scanner) {
double price = readDouble(scanner);
while (price <= 0.0) {
System.out.println("Ошибка: введите число в формате рубли.копейки:");
price = readDouble(scanner);
}
return price;
}
}