Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
129 lines (113 loc) · 5.77 KB
/
Copy pathMain.java
File metadata and controls
129 lines (113 loc) · 5.77 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
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
AccountCalculator сalculator = new AccountCalculator();
сalculator.start();
}
}
class AccountCalculator {
ArrayList<String> сalculatorList = new ArrayList<>();
ArrayList<Double> сalculatorDouble = new ArrayList<>();
int userInput;
String productName;
Double productPrice;
String productPriceString;
String finish;
Double totalСost = 00.00;
private void сalculator() {
Scanner scanner = new Scanner(System.in);
System.out.println("Введите название товара:");
productName = scanner.next();
сalculatorList.add(productName);
System.out.println("Введите стоимость в формате 00,00 руб.коп.:");
productPriceString = scanner.next();
productPriceString = productPriceString.replaceAll(",", ".");
while (!isNumeric(productPriceString)) {
System.out.print("Вы ввели некорректное значение\n");
productPriceString = scanner.next();
}
productPrice = Double.parseDouble(productPriceString);
сalculatorDouble.add(productPrice);
totalСost = totalСost + productPrice;
String result = String.format("%.2f", totalСost);
System.out.print("\nСписок добавленного товара:\n");
for (int i = 0; i < сalculatorList.size(); i++) {
String name = сalculatorList.get(i);
double price = сalculatorDouble.get(i);
String rubName = " рубль";
int rub1 = (int) price;
switch (rub1 % 10){ case 1:
rubName = " рубль"; break;
case 2: case 3: case 4:
rubName = " рубля"; break;
case 5: case 6: case 7: case 8: case 9: case 0:
rubName = " рублей";
}
System.out.print(i+1 + " " + name + " " + price + " " + rubName + "\n");
}
System.out.print("Общая стоимость: " + result + "\n \n");
System.out.print("Товар добавлен успешно! \nЧтобы продолжить добавление товара введите любой символ \nЧтобы завершить добавления товара введите команду 'Завершить'\n");
finish = scanner.next();
finish (finish, userInput, totalСost);
}
private void finish (String finish, int userInput, double totalСost) {
String fin = "Завершить";
String finСost = String.format("%.2f", (totalСost/userInput));
System.out.print("\nСписок добавленного товара:\n");
for (int i = 0; i < сalculatorList.size(); i++) {
String name = сalculatorList.get(i);
double price = сalculatorDouble.get(i);
int namb = i;
String rubName = " рубль";
int rub1 = (int) price;
switch (rub1 % 10){ case 1:
rubName = " рубль"; break;
case 2: case 3: case 4:
rubName = " рубля"; break;
case 5: case 6: case 7: case 8: case 9: case 0:
rubName = " рублей";
}
System.out.print(++namb + " " + name + " " + price + " " + rubName + "\n");
}
if (finish.equalsIgnoreCase(fin)) {
System.out.print("Делим счет\n");
System.out.print("Каждый должен заплатить " + finСost);
int rub = (int) totalСost/userInput;
switch (rub % 10){ case 1:
System.out.println(" рубль"); break;
case 2: case 3: case 4:
System.out.println(" рубля"); break;
case 5: case 6: case 7: case 8: case 9: case 0:
System.out.println(" рублей"); break;
}
} else {
сalculator();
}
}
public static boolean isNumeric(String productPriceString) {
try {
Double.parseDouble(productPriceString);
return true;
} catch (NumberFormatException e) {
return false;
}
}
public void start() {
Scanner scanner = new Scanner(System.in);
do {
System.out.println("На скольких человек необходимо разделить счёт \n(Введите количество человек более 1):");
while (!scanner.hasNextInt()) {
System.out.println("Это некорректное значение для подсчёта \n\n На скольких человек необходимо разделить счёт \n(Введите количество человек более 1):");
scanner.next();
}
userInput = scanner.nextInt();
if (userInput <=1) {
System.out.println("Ошибка");
start();
}
} while (userInput <= 1);
System.out.println("Будем делить счёт на " + userInput + " чел.");
сalculator();
}
}