Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
163 lines (155 loc) · 6.1 KB
/
Copy pathMain.java
File metadata and controls
163 lines (155 loc) · 6.1 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import java.io.PrintStream;
import java.util.Scanner;
public class Main {
public static Scanner in = new Scanner(System.in);
public static PrintStream out = System.out;
public static void main(String[] args) {
int persons = InputInt();
Positions[] pos = new Positions[100];
String nextPosition = AddName();
double nextPrice = AddPrice();
while (nextPrice<0){
out.println("Non-correct value for counting");
nextPrice = AddPrice();
}
int i = 0;
while (!(nextPosition.equalsIgnoreCase("Завершить") || nextPosition.equalsIgnoreCase("end"))) {
out.println("The dish has been added successfully!\nDo you want to add one more?");
pos[i] = new Positions(nextPosition, nextPrice);
nextPosition = AddName();
if (!(nextPosition.equalsIgnoreCase("Завершить") || nextPosition.equalsIgnoreCase("end"))) {
nextPrice = AddPrice();
while (nextPrice<0){
out.println("Non-correct value for counting");
nextPrice = AddPrice();
}
}
i += 1;
}
for (int j = 0; j < i; j++)
out.println(j + 1 + ") " + pos[j].name + " --- " + pos[j].price + " rubles");
double sum = 0;
for (int j = 0; j < i; j++)
sum += pos[j].price;
out.print("Total sum: " + String.format("%.2f", sum));
Ending(sum);
out.print(("Everyone has to pay: ") + String.format("%.2f", sum / persons));
Ending((sum / persons));
//далее дополнительная фича
out.println("Do you want to correct the list");
String choice = in.next();
int check = 0;
while (check == 0) {
if (choice.equalsIgnoreCase("Да") || choice.equalsIgnoreCase("Yes")) {
int del = 1;
while (del != 0) {
del = (int) AddCorrections();
if (del > 0 && del < i) {
for (int y = del - 1; y < i - 1; y++) {
pos[y] = pos[y + 1];
}
pos[i] = null;
i--;
for (int j = 0; j < i; j++)
out.println(j + 1 + ") " + pos[j].name + " --- " + pos[j].price + " rubles");
double sum1 = 0;
for (int j = 0; j < i; j++)
sum1 += pos[j].price;
out.println("Total sum: " + String.format("%.2f", sum1));
Ending(sum1);
out.println(("Everyone has to pay: ") + String.format("%.2f", sum1 / persons));
Ending((sum1 / persons));
} else if (del < 0)
out.println("The number cannot be less than zero");
else if (del > i)
out.println("Non-correct value");
}
out.println("Great! See you later");
check = 1;
} else if (choice.equalsIgnoreCase("Нет") || choice.equalsIgnoreCase("No")) {
out.println("Great! See you later");
check = 1;
} else {
out.println("Please, enter Yes/No");
choice = in.next();
}
}
}
public static int InputInt() {
//самодеятельность, проще сделать как для double, но зато сам
out.println("How many people should the bill be divided into?");
String enter = in.next();
int persons0 = 0, flag = 0, flag2 = 0;
while (flag == 0) {
for (int t = 0; t < enter.length(); t++) {
if (enter.charAt(t) < '0' || enter.charAt(t) > '9') {
flag2 += 1;
}
}
if (flag2 == 0) {
int st = 1;
for (int r = enter.length() - 1; r >= 0; r--) {
persons0 += ((int) enter.charAt(r) - 48) * st;
st *= 10;
}
if (persons0 >= 1)
flag = 1;
}
if (flag == 0) {
out.println("Non-correct value for counting\nPlease enter the correct value");
enter = in.next();
}
flag2 = 0;
}
return persons0;
}
public static boolean IsDouble(String input) {
double price;
try {
price = Double.parseDouble(input);
return true;
} catch (NumberFormatException e) {
return false;
}
}
public static boolean IsInt(String inp) {
int price;
try {
price = Integer.parseInt(inp);
return true;
} catch (NumberFormatException e) {
return false;
}
}
public static double AddCorrections(){
out.println("Enter the number of the dish you want to delete, If you have finished editing, enter 0");
String inp = in.next();
while (!IsInt(inp)){
out.println("Non-correct value for counting\nPlease enter the correct value");
inp = in.next();
}
return Integer.parseInt(inp);
}
public static double AddPrice(){
out.println("Enter the price of the dish in the format rub,kop");
String input = in.next();
while (!IsDouble(input)){
out.println("Non-correct value for counting\nPlease enter the correct value");
input = in.next();
}
return Double.parseDouble(input);
}
public static String AddName(){
out.println("Enter the name of the dish");
return in.next();
}
public static void Ending (double coast){
int price = (int) coast;
if (price%10 == 1 && price%100!=11)
out.println(" (рубль)");
else if (price%10<=4 && price%10>=2 && (price%100)/10!=2)
out.println(" (рубля)");
else
out.println(" (рублей)");
}
}