Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
92 lines (71 loc) · 2.61 KB
/
Copy pathMain.java
File metadata and controls
92 lines (71 loc) · 2.61 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
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int peopleCount = -1;
while(peopleCount <= 1) {
print("На скольких человек необходимо разделить счёт");
peopleCount = scanner.nextInt();
if (peopleCount <= 1) {
print("Кол-во человек должно быть больше 1");
}
}
Calc calc = new Calc(peopleCount);
while(true) {
print("Введите информацию о товаре");
print("Название:");
String inputName = scanner.next();
if (inputName.trim().equalsIgnoreCase("завершить")) {
break;
}
print("Цена:");
double inputPrice;
try {
inputPrice = scanner.nextDouble();
} catch (Exception e) {
print("Цена должна быть числом, попробуйте снова.");
continue;
}
if (inputPrice <= 0) {
print("Цена не может быть меньше или равной нулю, попробуйте снова.");
continue;
}
Product product = new Product(inputName, inputPrice);
calc.add(product);
double total = calc.getTotalPrice();
String end = getRubEnd(total);
print(String.format("Товар '%s' успешно добавлен! Общая стоимость: %.2f %s", product.name, total, end));
}
print(String.format("Добавленные товары: %s", calc.getAllProductNames()));
print(String.format(
"Каждый должен по: %.2f %s",
calc.getPricePerPerson(),
getRubEnd(calc.getPricePerPerson())
));
}
public static void print(String text) {
System.out.println(text);
}
public static String getRubEnd(double d) {
int num = (int) Math.floor(d);
int preLastDigit = num % 100 / 10;
if (preLastDigit == 1)
{
return "рублей";
}
switch (num % 10)
{
case 1:
return "рубль";
case 2:
case 3:
case 4:
return "рубля";
default:
return "рублей";
}
}
}