Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
117 lines (114 loc) · 5.39 KB
/
Copy pathMain.java
File metadata and controls
117 lines (114 loc) · 5.39 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
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// счетчик
int quantity;
String quantityString;
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("На скольких человек необходимо разделить счёт?");
quantityString = scanner.nextLine();
try {
quantity = Integer.parseInt(quantityString);
if (quantity > 1) {
break;
} else {
System.out.println("Ошибка ввода. Число людей должно быть больше одного.");
}
}
catch (NumberFormatException nfe) {
System.out.println("Ошибка ввода. Необходимо ввести целое число.");
}
}
//калькулятор
Calculator calc = new Calculator();
calc.inputProduct();
double part = Calculator.sum/quantity;
Declension dec = new Declension();
dec.decline(Math.floor(part));
String partPrint = String.format ("%.2f", part);
partPrint = partPrint.replace(',','.');
System.out.println("Каждый человек должен заплатить " + partPrint + " " + Declension.rouble + ".");
scanner.close();
}
}
class Calculator {
static double sum;
int costRoubles, costCopecks;
String list = "Добавленные товары:\n", printCostCopecks;
void inputProduct() {
while (true) {
Scanner scanner = new Scanner(System.in);
System.out.println("Какой товар вы хотите заказать?");
String product = scanner.nextLine();
System.out.println("Какова стоимость товара '" + product + "'?\nСтоимость товара введите в формате\nРубли: 11\nКопейки: 45");
while (true) {
System.out.print("Рубли: ");
String amountRoubles = scanner.nextLine();
try {
costRoubles = Integer.parseInt(amountRoubles);
if (costRoubles>-1) {
break;
} else {
System.out.println("Ошибка ввода. Число рублей не может быть отрицательной величиной.");
}
}
catch (NumberFormatException nfe) {
System.out.println("Ошибка ввода. Необходимо ввести целое число.");
}
}
while (true) {
System.out.print("Копейки: ");
String amountCopecks = scanner.nextLine();
try {
costCopecks = Integer.parseInt(amountCopecks);
if (costCopecks>-1 && costCopecks<100) {
break;
} else {
System.out.println("Ошибка ввода. Число копеек должно быть в диапазоне от 0 до 99.");
}
}
catch (NumberFormatException nfe) {
System.out.println("Ошибка ввода. Необходимо ввести целое число.");
}
}
Declension dec = new Declension();
double dcostRoubles = costRoubles;
dec.decline(dcostRoubles);
if (costCopecks<10) {
printCostCopecks = "0" + costCopecks;
} else {
printCostCopecks = "" + costCopecks;
}
System.out.println("Вы добавили товар " + product + " стоимостью " + costRoubles + "." + printCostCopecks + " " + Declension.rouble + " в список покупок.\nЕсли хотите закончить формирование списка товаров, введите 'Завершить'.\nЕсли хотите добавить товар, введите любой другой символ.");
double dcostCopecks = costCopecks;
sum = sum + costRoubles + (dcostCopecks / 100);
list = list + product + " " + costRoubles + "." + printCostCopecks + " " + Declension.rouble + ".\n";
String request = scanner.nextLine();
if (request.equalsIgnoreCase("Завершить")){
System.out.println(list);
dec.decline(Math.floor(sum));
String sumPrint = String.format ("%.2f", sum);
sumPrint = sumPrint.replace(',','.');
System.out.println("Общая сумма - " + sumPrint + " " + Declension.rouble + ".");
break;
}
}
}
}
class Declension {
static String rouble;
void decline (double a) {
String cut = String.format ("%.2f", a);
int c2 = cut.charAt(cut.length() - 4) - 48;
if (c2 == 0 || c2 > 4) rouble = "рублей";
if (c2 == 1) rouble = "рубль";
if (c2 > 1 && c2 < 5) rouble = "рубля";
if (a>=10) {
int c1 = cut.charAt(cut.length() - 5) - 48;
if (c1 == 1) {
rouble = "рублей";
}
}
}
}