forked from Yandex-Practicum/Java-Module-Project-YP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCounter.java
More file actions
118 lines (109 loc) · 5.67 KB
/
Counter.java
File metadata and controls
118 lines (109 loc) · 5.67 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
import java.util.Scanner;
//import java.math.MathContext;
public class Counter {
public static int pers() { // ввод количества людей
Scanner scanner = new Scanner(System.in);
int persons = 0;
System.out.println("Введите количество человек");
while (persons <= 1) {
if (scanner.hasNextInt()) {
persons = scanner.nextInt();
if (persons <= 1) {
System.out.println("Количество человек должно быть больше одного");
System.out.println("--------------------------------------------\n");
return pers();
}
} else {
System.out.println("Некорректное число, пожалуйста повторите ввод");
System.out.println("----------------------------------------------\n");
return pers();
}
}
return persons;
}
public static String enterName() { // ввод названия товара
Scanner scanner = new Scanner(System.in);
//while (true) { // убрал лишний цикл
System.out.println("Введите название товара или 'завершить' для выхода:");
String shopAdd = scanner.next();
String end = "завершить";
if (shopAdd.equalsIgnoreCase(end)) {
System.out.println("\n-------------");
System.out.println("Закрытие программы");
System.exit(0);
}
System.out.printf("%s добавлено в корзину\n", shopAdd);
System.out.println("-----------------------------\n");
return shopAdd;
//}
//return enterName();
}
public static double enterPrice() { // ввод стоимости товара
Scanner scanner = new Scanner(System.in);
System.out.println("ВВедите стоимость товара:");
if (scanner.hasNextDouble()) {
double price = scanner.nextDouble();
if (price > 0) { // добавлено условие проверки отрицательных чисел и ноля в цене
System.out.printf("Стоимость товара: %.2f\n", price);
System.out.println("-----------------------------\n");
return price;
}
}
System.out.println("Вы ввели неверные данные, повторите ввод\n");
return enterPrice();
}
public static void shopCalc() { // калькулятор суммы выбранных товаров
int persons = pers();
String basket = ""; // корзина
String rubleySumma; // окончание в Сумме
String rubleyOtvet; // окончание в Ответе
double sum = 0;
while (true) {
String shopAdd = enterName();
double price = enterPrice();
sum = sum + price;
basket = (shopAdd.trim() + "\n" + basket.trim());
double otvet = sum / persons;
// никогда бы не подумал, что так сложно запилить окончания слова "рубль" -_-
// англоговорящим прогерам, оказывается, жить-то намного проще D:
// большое Вам спасибо за помощь в алгоритме, про 5-19 итд :) сам бы хрен догадался
// проверка окончания в Сумме
// начало
int rublSum = (int) Math.floor(sum);
String rublStr = String.valueOf(rublSum);
int rublSumTwoD = rublSum%100;
if ((rublSumTwoD >= 5) && (rublSumTwoD <= 19)) {
rubleySumma = "рублей";
} else if (rublStr.endsWith("0")) {
rubleySumma = "рублей";
} else if (rublStr.endsWith("1")) {
rubleySumma = "рубль";
} else if ((rublStr.endsWith("2")) || (rublStr.endsWith("3")) || (rublStr.endsWith("4"))) {
rubleySumma = "рубля";
} else {
rubleySumma = "рублей";
}
// конец
// проверка окончания в Ответе
// начало
int rublOtv = (int) Math.floor(otvet);
String otvetStr = String.valueOf(rublOtv);
int rublOtvTwoD = rublOtv%100;
if ((rublOtvTwoD >= 5) && (rublOtvTwoD <= 19)) {
rubleyOtvet = "рублей";
} else if (otvetStr.endsWith("0")) {
rubleyOtvet = "рублей";
} else if (otvetStr.endsWith("1")) {
rubleyOtvet = "рубль";
} else if ((otvetStr.endsWith("2")) || (otvetStr.endsWith("3")) || (otvetStr.endsWith("4"))) {
rubleyOtvet = "рубля";
} else {
rubleyOtvet = "рублей";
}
// конец
System.out.printf("\nИтого\n------------------------\nДобавлены товары:\n%s\nВсего человек: %d\nВсего товаров на сумму: %.2f %s\nКаждый из вас должен заплатить: %.2f %s\n------------------------\n", basket, persons, sum, rubleySumma, otvet, rubleyOtvet);
System.out.println("\nХотите приобрести что-нибудь еще?");
System.out.println("----------------------------------\n");
}
}
}