forked from Yandex-Practicum/Java-Module-Project-YP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
146 lines (116 loc) · 4.38 KB
/
Main.java
File metadata and controls
146 lines (116 loc) · 4.38 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
class RubFormat{
int rub;
String formatter(int rub){
this.rub = rub;
String ruble = "";
if (rub >= 10){
ruble = "Рублей";
} else {
switch (rub){
case 1:
ruble = "Рубль";
break;
case 2:
case 3:
case 4:
ruble = "Рубля";
break;
case 0:
case 5:
case 6:
case 7:
case 8:
case 9:
ruble = "Рублей";
break;
}
}
return ruble;
}
}
class Product{
String name;
float price;
Product(String name,float price){
this.name = name;
this.price = price;
}
}
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
ArrayList<Product> products = new ArrayList<>();
System.out.println("Введите количество гостей среди которых нужно разделить счёт");
int peoples = 0;
float sum = 0;
while (true){
if (sc.hasNextInt()) {
peoples = sc.nextInt();
if (peoples < 1) {
System.out.println("Введите правильное количество гостей");
continue;
} else if (peoples == 1){
System.out.println("Делить не нужно, конец программы");
return;
} else {
System.out.println("Количество гостей: " + peoples);
break;
}
} else {
System.out.println("Введите целое число гостей");
//sc.next();
continue;
}
}
sc.nextLine();
while(true){
System.out.println("Введите название товара или 'Завершить' для отмены");
String name = sc.nextLine();
if (name.isEmpty()){
System.out.println("Название товара не может быть пустым");
continue;
} else if (name.matches("\\d+")){
System.out.println("Название товара не может состоять только из цифр");
continue;
} else if (name.equalsIgnoreCase("Завершить")) {
break;
}
System.out.println("Введите цену товара");
while(true){
if(sc.hasNextFloat()){
float price = sc.nextFloat();
sc.nextLine();
if (price > 0){
products.add(new Product(name,price));
sum += price;
System.out.println("Товар " + name + " добавлен со стоимостью " + price);
break;
} else System.out.println("Введите корректную цену товара");
} else {
System.out.println("Введите число");
sc.next();
}
}
}
if (products.isEmpty()) {
System.out.println("Вы не добавили ни одного товара");
} else System.out.println("Добавленные товары: ");
;
for (int i = 0; i < products.size(); i++){
System.out.println("Товар: " + products.get(i).name + " стоимостью: " + products.get(i).price);
}
RubFormat rubFormat = new RubFormat();
float totalSum = sum/(float)peoples;
int rub = (int) totalSum;
if ((int)totalSum > 10 && (int)totalSum <= 20) {
rub = (int) totalSum;
} else {
rub = (int) totalSum % 10;
}
String ruble = rubFormat.formatter(rub);
System.out.print(String.format("Каждый гость должен заплатить: %.2f %s",totalSum, ruble ));
}
}