Repository navigation
Expand file tree
/
Copy pathDivision.java
More file actions
75 lines (71 loc) · 3.18 KB
/
Copy pathDivision.java
File metadata and controls
75 lines (71 loc) · 3.18 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.ArrayList;
import java.util.Scanner;
public class Division {
public static String rur (double sum) {
if (sum % 10 == 1 && sum % 100 != 11) {
return "рубль";
} else if ((sum % 10 == 2 || sum % 10 == 3 || sum % 10 == 4)&&
sum % 100 != 12 && sum % 100 != 13 && sum % 100 != 14) {
return "рубля";
} else {return "рублей";}
}
public static void divCheck (int count) {
int i = 0;
String name;
double price;
double sum = 0;
ArrayList<Good> arrayOfGoods = new ArrayList<>();
while (true) {
i++;
while(true) {
try{
System.out.printf("Введите наименование товара%d: ", i);
Scanner scanner = new Scanner(System.in);
name = scanner.next();
break;
} catch(Exception e) {
System.out.print("Ошибка ввода. Возможно введены спецсимволы. ");
}
}
while(true) {
try{
System.out.printf("Введите стоимость товара%d: ", i);
Scanner scanner = new Scanner(System.in);
price = scanner.nextDouble();
if (price > 0) {
break;
} else {System.out.print("Ошибка ввода. Цена не может быть отрицательной или нулевой. ");}
} catch(Exception e) {
System.out.print("Ошибка ввода. Введено не число. ");
}
}
arrayOfGoods.add(new Good(name, price));
sum += price;
System.out.print("Товар успешно добавлен в корзину. Желаете продолжить? ");
Scanner scanner = new Scanner(System.in);
String answer = scanner.next();
if (answer.equalsIgnoreCase("завершить")) {break;}
}
System.out.println("Итого добавленные товары");
System.out.println("-".repeat(48));
System.out.println("|" + " ".repeat(8) + "Наименование товара" +
" ".repeat(8) +"|" + " Стоимость|");
System.out.println("|" + "-".repeat(35) + "|" + "-".repeat(10) + "|");
for (i = 0; i < arrayOfGoods.size(); i++) {
System.out.println("|" + String.format("%-35s", arrayOfGoods.get(i).name) + "|" +
String.format("%-10.2f", arrayOfGoods.get(i).price) + "|");
}
System.out.println("-".repeat(48));
System.out.printf("Итого сумма по счету: %.2f %s\t\n", sum, rur(sum));
System.out.printf("Количество человек: %d\t\t\n", count);
System.out.printf("Итого на человека: %.2f %s\t\t\n", sum / count, rur(sum / count));
}
}
class Good {
String name;
double price;
Good (String name, double price) {
this.name = name;
this.price = price;
}
}