Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
75 lines (56 loc) · 2.56 KB
/
Copy pathMain.java
File metadata and controls
75 lines (56 loc) · 2.56 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
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
System.out.println("На скольких человек необходимо разделить счёт?");
PeopleCounter counter = new PeopleCounter();
int peopleCount = counter.countPeople();
double total = 0.00;
double price;
String list = "";
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("Если хотите добавить блюдо, то ведите его название либо \"Закончить\" для завершения");
String dish = sc.next();
if (dish.equalsIgnoreCase("закончить")) {
break;
}
System.out.println("Введите стоимость блюда в формате \"Рубли,копейки\":");
while (true) {
if (sc.hasNextDouble()){
price = sc.nextDouble();
if (price <= 0) {
System.out.println("Введите число больше \"0\"");
sc.nextLine();
} else {
break;
}
} else {
System.out.println("Введено не допустимое значение, повторите ввод");
sc.next();
}
}
System.out.println("Вы успешно добавили новое блюдо");
total += price;
System.out.println(dish + " - " + formatPrice(price));
list = list.concat(dish+"").concat("\n");
}
System.out.println("Добавленные товары:\n" + list);
System.out.println("Итого: " + formatPrice(total));
double split = total / peopleCount;
System.out.println("Сумма на одного гостя: " + formatPrice(split));
sc.close();
}
public static String formatPrice(double price) {
int rubles = (int) price;
int kopecks = (int) ((price - rubles) * 100);
String rubleWord = "";
if (rubles % 10 == 1 && rubles % 100 != 11) {
rubleWord = "рубль";
} else if (rubles % 10 >= 2 && rubles % 10 <= 4 && (rubles % 100 < 10 || rubles % 100 > 20)) {
rubleWord = "рубля";
} else {
rubleWord = "рублей";
}
return rubles + " " + rubleWord + " " + kopecks + " коп.";
}
}