forked from Yandex-Practicum/Java-Module-Project-YP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalculator.java
More file actions
101 lines (80 loc) · 3.11 KB
/
Calculator.java
File metadata and controls
101 lines (80 loc) · 3.11 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
import java.text.DecimalFormat;
import java.util.Scanner;
public class Calculator {
public static void calculator(int numberPeople) {
String finish = "Завершить";
double sum = 00.00;
StringBuilder list = new StringBuilder();
String endingRub = "";
String endingSum = "";
DecimalFormat decimalFormat = new DecimalFormat("#.##");
while (true) {
System.out.println("Введите название товара или слово 'Завершить', чтобы получить результат");
Scanner scanner = new Scanner(System.in);
String nameGoods = scanner.nextLine();
if (nameGoods.equalsIgnoreCase(finish)) {
break;
}
System.out.println("Введите стоимость в формате 00,00");
if (scanner.hasNextDouble()) {
double price = scanner.nextDouble();
if (price > 0) {
sum = sum + price;
endingRub = ending(price);
decimalFormat.format(price);
list.append(" \n" + nameGoods + " – " + price + " рубл" + endingRub);
} else {
System.out.println("Ошибка! Неверный формат цены. Попробуйте ввести данные о товаре снова");
}
} else {
System.out.println("Ошибка! Неверный формат цены. Попробуйте ввести данные о товаре снова");
}
}
endingSum = ending(sum / numberPeople);
System.out.println(list + "\n" + "С каждого: " + decimalFormat.format(sum / numberPeople) + " рубл" + endingSum);
}
public static String ending(double price) {
String endingOne = "ь";
String endingTwo = "я";
String endingThree = "ей";
String ending;
int priceInt = (int) price;
if (price > 10) {
price %= 100;
if (price == 11 || price == 12 || price == 13 || price == 14) {
ending = endingThree;
} else {
priceInt %= 10;
switch (priceInt) {
case 1:
ending = endingOne;
break;
case 2:
case 3:
case 4:
ending = endingTwo;
break;
default:
ending = endingThree;
break;
}
}
} else {
priceInt %= 10;
switch (priceInt) {
case 1:
ending = endingOne;
break;
case 2:
case 3:
case 4:
ending = endingTwo;
break;
default:
ending = endingThree;
break;
}
}
return ending;
}
}