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
129 lines (104 loc) · 4.34 KB
/
Calculator.java
File metadata and controls
129 lines (104 loc) · 4.34 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
125
126
127
128
129
import java.util.ArrayList;
import java.util.Scanner;
public class Calculator {
public double start() {
ArrayList<Bludo> feedList = new ArrayList<>();
System.out.println("Привет!");
double dolg = 0; // итог калькулятора
// ВВод числа плательщиков
int menCount = skolkoNaroduPlatit();
//System.out.println("принято. Число оплачивающих: " + menCount);
int i = 0;
Scanner scanner = new Scanner(System.in);
boolean vvod = true;
double fullPrice = 0;
while (vvod) {
Bludo bludo = new Bludo();
i++;
System.out.println("Введите название товара № " + i + " или введите 'Завершить': ");
String vvodStr = scanner.nextLine();
vvodStr.trim();
double price = 0;
boolean enteredPrice = false;
if (!vvodStr.toLowerCase().equals("завершить")) {
bludo.name = vvodStr;
// System.out.println("принято. товар введен ");
System.out.println("Введите цену [руб].[коп]:");
price=0;
while (true) {
try {
vvodStr = scanner.nextLine();
price = Double.parseDouble(vvodStr);
if (price > 0) {
bludo.price = price;
fullPrice = fullPrice + price;
System.out.println("Товар '"+bludo.name+"' успешно добавлен на сумму: " + fullPrice + " руб.");
enteredPrice = true;break;
}
else System.out.println("введите цену больше 0 ");
} catch (NumberFormatException e) {
System.out.println("неверный ввод, повторите ");
}
// System.out.println("неправильный ввод, повторите ");
// System.out.print(price);System.out.println("");
// добавление товара в список
}
feedList.add(bludo);
} else vvod = false;
}
int tovarov = feedList.size();
// Итог. Выводим список
if (fullPrice > 0) {
System.out.println(String.format("Итого: %.2f", fullPrice));
for (int t = 0; t < tovarov; t++) {
System.out.println("Добавленные товары: " + feedList.get(t).name);
}
// определим окончание для "рубль"
dolg = fullPrice /(double)menCount;
// String shablon = "Каждый должен заплатить по: %.2f "+pravilno;
// System.out.println(String.format(shablon, dolg));
}
return dolg;
}
public static int skolkoNaroduPlatit() {
boolean notOk = true;
int men = 0;
while (notOk) {
Scanner scanner = new Scanner(System.in);
System.out.println("Сколько человек расплачиваются(2-9)?");
String strMen = scanner.nextLine();
try {
men = Integer.parseInt(strMen.trim());
if ((men > 1) && (men < 10)) {
notOk = false;
} else System.out.println("неверное число " + men);
} catch (NumberFormatException nfe) {
System.out.println("неправильный ввод, повторите ");
}
}
return men;
}
public static String roubleEnd(double itog) {
int roubles = 0;
roubles = (int) itog;
int rouble = roubles - ((int) roubles / 10) * 10;
String pravilno = "";
if (rouble == 1) {
pravilno = "рубль";
}
;
if ((rouble == 0) || (rouble > 4)) {
pravilno = "рублей";
}
;
if ((rouble > 1) && (rouble < 5)) {
pravilno = "рубля";
}
;
return pravilno;
}
static class Bludo {
double price;
String name;
}
}