forked from Yandex-Practicum/Java-Module-Project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInvoiceCalculator.java
More file actions
140 lines (121 loc) · 4.31 KB
/
InvoiceCalculator.java
File metadata and controls
140 lines (121 loc) · 4.31 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
130
131
132
133
134
135
136
137
138
139
140
import java.util.ArrayList;
import java.util.Scanner;
public class InvoiceCalculator {
protected ArrayList<Product> products = new ArrayList<>();
protected int countUsers;
public void start() {
this.requestCountUsers();
this.addProducts();
this.printProducts();
this.printResults();
}
public void requestCountUsers() {
Scanner scanner = new Scanner(System.in);
System.out.println("На скольких человек необходимо разделить счёт?");
String input;
while (true) {
input = scanner.nextLine();
if (isInt(input) && Integer.parseInt(input) > 1) {
break;
} else {
System.out.println("Кол-во должно быть больше 1");
System.out.println("Повторите ввод:");
}
}
this.countUsers = Integer.parseInt(input);
}
public void addProducts() {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("Добавьте новый товар или введите \"Завершить\"");
String productTitle;
double productPrice;
String input;
System.out.println("Введите название товара:");
input = scanner.nextLine();
if (checkEnd(input)) {
break;
}
productTitle = input;
while (true) {
System.out.println("Введите цену товара:");
input = scanner.nextLine();
if (isDouble(input)) {
productPrice = Double.parseDouble(input);
break;
}
System.out.println("Некорректное значение");
}
Product product = new Product(productTitle, productPrice);
this.products.add(product);
}
}
public void printProducts() {
System.out.printf("%n -=-=-=-=-=-=-=- %n");
if (this.products.size() == 0) {
System.out.println("Список товаров пуст...");
} else {
double sum = 0;
System.out.println("Добавленные товары:");
for (int i = 0; i < this.products.size(); i++) {
Product product = this.products.get(i);
System.out.printf("%d) %s%n", i + 1, product);
sum += product.price;
}
System.out.printf("Всего товаров: %s шт. на сумму %.2f%n", this.products.size(), sum);
}
}
public double getAvgSum() {
double sum = 0;
if (this.products.size() == 0) {
return 0;
} else {
for (int i = 0; i < this.products.size(); i++) {
sum += this.products.get(i).price;
}
return sum / this.countUsers;
}
}
public String getRubAddition(double sum) {
String rubAddition = "рублей";
int sumInt = (int) sum;
int preLastDigit = sumInt % 100 / 10;
if (preLastDigit == 1) {
return rubAddition;
}
switch (sumInt % 10) {
case 1:
return "рубль";
case 2:
case 3:
case 4:
return "рубля";
default:
return rubAddition;
}
}
public void printResults() {
double avgSum = this.getAvgSum();
String getRubAddition = this.getRubAddition(avgSum);
System.out.printf("Итого: %.2f %s на человека%n", avgSum, getRubAddition);
}
public static boolean isInt(String input) throws NumberFormatException {
try {
Integer.parseInt(input);
return true;
} catch (NumberFormatException e) {
return false;
}
}
public static boolean isDouble(String input) throws NumberFormatException {
try {
Double.parseDouble(input);
return true;
} catch (NumberFormatException e) {
return false;
}
}
public static boolean checkEnd(String input) {
return "завершить".equals(input.trim().toLowerCase());
}
}