Repository navigation
Expand file tree
/
Copy pathProduct.java
More file actions
108 lines (83 loc) · 4.19 KB
/
Copy pathProduct.java
File metadata and controls
108 lines (83 loc) · 4.19 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
import java.util.Locale;
import java.util.Scanner;
public class Product {
Scanner sc = new Scanner(System.in).useLocale(Locale.US);
static int numberOfPeople;
// подсчет количество человек
public void calculatePersons() {
System.out.println("На скольких гостей необходимо разделить счёт ?");
//количество человек
while (true) {
if (!sc.hasNextInt()) {
System.out.println("Ошибка. Введите 'ЧИСЛО' гостей !");
sc.next();
continue;
}
numberOfPeople = sc.nextInt();
if (numberOfPeople <= 1) {
System.out.println("Ошибка. Введите корректное 'ЧИСЛО' !");
} else {
System.out.println("Поздравляем вам есть с кем разделить счет !");
break;
}
}
}
// добавление товара и его стоимости в калькулятор
public void addProduct(Calculator calculator) {
System.out.println("Введите название товара:");
String product = sc.next();
System.out.println("Введите стоимость товара: 'рубли.копейки' [10.45, 11.40] ");
String addOne = "";
while (true) {
if (!sc.hasNextDouble()) {
System.out.println("Ошибка. Товар не добавлен. Введите корректное значение стоимости товара. ");
sc.next();
continue;
}
double price = sc.nextDouble();
if (price <= 0) {
System.out.println("Ошибка.Введите положительное значение. ");
}
if (price >= 0) {
calculator.setProducts(product);
calculator.setTotalPrice(price);
System.out.println("Товар успешно добавлен.\nДобавить ещё один товар? нажмите - 'Y'\nЕсли все товары добавлены нажмите - 'N' ! ");
while (true) {
addOne = sc.next();
if (addOne.equalsIgnoreCase("Y")) {
addProduct(calculator);
} if (addOne.equalsIgnoreCase("N")) {
outputResult(calculator);
}
else {
System.out.println("Ошибка. Введите корректное количество гостей!");
continue;
}
}
}
}
}
// вывод результатов
public void outputResult (Calculator calculator){
System.out.print("Добавленные товары:");
// вывод товаров
System.out.println(calculator.getProducts());
System.out.println("Общий счет: " + calculator.getTotalPrice() + " разделим его на " +numberOfPeople + " гостей. ");
// счет каждого посетителя
// склонение в зависимости от целочисленной части счета
double scoreVisitor = calculator.getTotalPrice() / numberOfPeople;
String currency = "";
// округление значения
int roundScore = (int) Math.floor(scoreVisitor);
if (roundScore == 1 || (roundScore % 10 == 1 && (roundScore / 10) % 2 == 0)) {
currency = "рубль";
} else if ((roundScore > 1 && roundScore <= 4) || ((roundScore % 10 > 1 && roundScore % 10 <= 4) && (roundScore / 10) % 2 == 0)) {
currency = "рубля";
} else if (roundScore > 4) {
currency = "рублей";
}
// вывод счета клиента
String formatAnswer = String.format("%.2f %s", scoreVisitor, currency);
System.out.println("Сумма к оплате каждого гостя : " + formatAnswer);
}
}