-
Notifications
You must be signed in to change notification settings - Fork 216
Expand file tree
/
Copy pathCalculate.java
More file actions
86 lines (68 loc) · 3.16 KB
/
Calculate.java
File metadata and controls
86 lines (68 loc) · 3.16 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
import java.util.Scanner;
public class Calculate {
private static final Scanner scanner = new Scanner(System.in);
int count;
String allProducts = "";
double totalPrice = 0.00f;
double productPrice;
String rubles = "";
public void initDataPersons() {
System.out.println("Здравствуйте!\nНа скольких гостей разделить счет?");
while (true) {
count = scanner.nextInt();
if (count > 1) {
break;
}
if (count == 1) {
System.out.println("Разделение счета рассчитывается на большее кол-во гостей. Пожалуйста, введите число заново.");
} else {
System.out.println("Неверное кол-во гостей. Пожалуйста, введите число заново.");
}
}
}
// Добавление товаров в калькулятор
public void addingProducts() {
while (true) {
System.out.println("Введите название товара или введите команду \"Завершить\".");
try {
String productName = scanner.next();
if (productName.equalsIgnoreCase("finish")) {
System.out.println("Добавленные товары: " + "\n" + allProducts);
break;
} else {
allProducts = allProducts + productName + "\n";
while (true) {
System.out.println("Введите стоимость товара в формате \"рубли,копейки\".");
productPrice = scanner.nextDouble();
if (productPrice <= 0) {
System.out.println("Некорректное значение.");
} else {
break;
}
}
this.totalPrice = totalPrice + productPrice;
System.out.println("Товар добавлен в чек.");
}
}
catch(Exception e){
System.out.println("Некорректное название товара. Попробуйте снова.");
}
}
}
//Подсчет рублей для каждого гостя
public void endingOfRubles() {
int lastNum = (int) totalPrice % 10;
if (totalPrice % 100 >= 11 && totalPrice % 100 <= 20) {
rubles = "рублей rublei";
} else if (lastNum > 1 && lastNum < 5) {
rubles = "рубля rublya";
} else if (totalPrice % 10 == 1) {
rubles = "рубль rubl";
}
else {
rubles = "рублей rublei";
}
double OnePersonSum = totalPrice / (double)count;
System.out.println("Сумма на каждого гостя составляет " + String.format("%.2f", OnePersonSum) + " " + rubles + ".\nВсего доброго!");
}
}