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
81 lines (65 loc) · 2.65 KB
/
Main.java
File metadata and controls
81 lines (65 loc) · 2.65 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
import static java.lang.Math.floor;
import java.util.Scanner;
public class Main {
static Scanner scanner = new Scanner(System.in);
static String[] tovar = new String[100]; //массив сохранения наименований товара
static String buf; //буфер проверки ввода да - нет
static int i=0, N=0; //i - счетчик товара, N - кол.человек
static double[] cost = new double[100]; // массив цен
public static void main(String[] args) {
System.out.println("На сколько человек разделить счет?");
do {
System.out.println("Введите число больше 1");
while (!scanner.hasNextInt()) {
System.out.println("Это не целое число!");
scanner.next();
}
N = scanner.nextInt();
} while (N<=1);
for (; ; ) {
Tovar.inputTovar();
tovar[i] = Tovar.nameTovar;
Tovar.inputCost();
cost[i] = Tovar.bufCost;
i++;
System.out.println("Товар успешно введен.");
System.out.println("Желаете продолжить ввод товара? " + "Завершено - нет " + "любая буква - да. ");
buf = scanner.next();
if (buf.equalsIgnoreCase("завершено")) {
endProgram();
break;
}
}
}
private static void endProgram() {
scanner.close();
System.out.println("Добавлены товары:");
for (int j = 0; j < i; j++) {
System.out.println(tovar[j]);
}
double result = 0;
for (int j = 0; j < i; j++) {
result = result + cost[j];
}
result = result / N;
System.out.print("Каждый человек должен заплатить - ");
int x = (int)floor(result);
int lastX=x % 100;
while(lastX>20) {
lastX = lastX%10;
}
int cop = (int)floor((result-x)*100);
if(lastX==0) {
System.out.print(x + " рублей " + cop + " коп.");
}
if (lastX==1) {
System.out.println(x + " рубль " + cop + " коп.");
}
if(lastX>1 && lastX<5) {
System.out.printf(x + " рубля " + cop + " коп.");
}
if(lastX>4){
System.out.print(x + " рублей " + cop +" коп.");
}
}
}