forked from Yandex-Practicum/Java-Module-Project-YP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOrderCalculator.java
More file actions
106 lines (90 loc) · 3.96 KB
/
OrderCalculator.java
File metadata and controls
106 lines (90 loc) · 3.96 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
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;
public class OrderCalculator {
Scanner scanner = new Scanner(System.in);
ArrayList<Product> products = new ArrayList<>();
double totalCost = 0;
public void start() {
System.out.print("Hа скольких человек необходимо разделить счёт: ");
int amountOfGuests = amountOfGuestsValidation();
while (true) {
String productName = nameOfProductValidation();
double productCost = costOfProductValidation();
Product product = new Product(productName, productCost);
products.add(product);
totalCost = totalCost + productCost;
System.out.println("Добавлен товар: " + productName + ", цена: " + productCost);
System.out.println("Добавить товар?");
String command = scanner.next();
if (command.equalsIgnoreCase("завершить")) {
printAllProducts();
printTotalPerGuest(amountOfGuests);
System.out.println("Выход из программы");
break;
}
}
}
private int amountOfGuestsValidation() {
int amountOfGuests = 0;
boolean isRightType = false;
while (!isRightType) {
try {
amountOfGuests = scanner.nextInt();
if (amountOfGuests <= 1) {
System.out.println(amountOfGuests + " - некорректное значение для подсчёта");
System.out.print("введите количество гостей: ");
} else {
isRightType = true;
}
} catch (InputMismatchException e) {
System.out.print("Неверно указано количество гостей, введите количество еще раз: ");
scanner.next();
}
}
return amountOfGuests;
}
private String nameOfProductValidation() {
System.out.print("Добавить название товара: ");
return scanner.next();
}
private double costOfProductValidation() {
double productCost = 0;
boolean isRightType = false;
System.out.print("Добавить цену товара: ");
while (!isRightType) {
try {
productCost = scanner.nextDouble();
if (productCost > 0) {
isRightType = true;
} else {
System.out.print("Цена товара не может быть отрицательным числом, введите цену еще раз: ");
}
} catch (InputMismatchException e) {
System.out.print("Неверно указана цена товара, введите цену еще раз: ");
scanner.next();
}
}
return productCost;
}
private void printAllProducts() {
System.out.println("Добавленные товары: ");
for (Product product : products) {
System.out.println(product.productName);
}
}
private void printTotalPerGuest(int amountOfQuests) {
String rubCase;
double totalPerGuest = totalCost / amountOfQuests;
int lastDigid = ((int) totalPerGuest) % 10;
if (lastDigid == 1 && !(totalPerGuest >= 10 && totalPerGuest <= 20)) {
rubCase = "рубль";
} else if ((lastDigid >= 2 && lastDigid < 5) && !(totalPerGuest >= 10 && totalPerGuest <= 20)) {
rubCase = "рубля";
} else {
rubCase = "рублей";
}
System.out.println("Сумма, которую должен заплатить каждый гость: "
+ String.format("%.2f", totalPerGuest) + " " + rubCase);
}
}