Repository navigation
Expand file tree
/
Copy pathCalculator.java
More file actions
102 lines (83 loc) · 3.75 KB
/
Copy pathCalculator.java
File metadata and controls
102 lines (83 loc) · 3.75 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
import java.util.ArrayList;
import java.util.Scanner;
public class Calculator {
ArrayList<Products> productList = new ArrayList<>();
Scanner scanner = new Scanner(System.in);
double sum;
int divide;
public void calcStart() {
while (!scanner.hasNextInt()) {
System.out.println("Введите, пожалуйста число");
scanner.next();
}
divide = scanner.nextInt();
while (true) {
if (divide == 1) {
System.out.println("Все оплатить придется самому");
break;
}
if (divide > 1) {
prodCount();
break;
}
else {
System.out.println("Вы ввели неправильное значение. Попробуйте снова!");
while (!scanner.hasNextInt()) {
System.out.println("Введите, пожалуйста число");
scanner.next();
}
divide = scanner.nextInt();
}
}
}
public void prodCount() {
while (true) {
System.out.println("Уточните название продукта: ");
String name = scanner.next();
System.out.println("Уточните стоимость продукта: ");
while (!scanner.hasNextDouble()) {
System.out.println("Введите, пожалуйста число формате 1,45");
scanner.next();
}
double price = scanner.nextDouble();
Products productNew = new Products(name, price);
productList.add(productNew);
sum += productNew.price;
System.out.println("Продукт " + productNew.name + " стоимостью "
+ (Math.round(productNew.price * 100.0)/100.0) +
" " + formatter((Math.round(productNew.price * 100.0)/100.0))
+ " добавлен в список.\nОбщая сумма составляет: "
+ (Math.round((sum) * 100.0)/100.0) +
" " + formatter((Math.round((sum) * 100.0)/100.0)));
System.out.println("\nЧтобы ввести новый товар введите любой символ.\n" +
"Чтобы закончить, введите 'Завершить'");
if (scanner.next().equalsIgnoreCase("Завершить")) {
finalSum();
break;
}
}
}
public void finalSum(){
System.out.println("\nДобавленные товары: ");
for (Products product : productList)
System.out.println (product.name + " " + (Math.round(product.price * 100.0)/100.0) +
" " + formatter ((Math.round(product.price * 100.0)/100.0)));
System.out.println("Итоговая сумма: " + (Math.round(sum * 100.0)/100.0) +
" " + formatter ((Math.round(sum * 100.0)/100.0)));
int name = divide;
double finalSum = sum / divide;
System.out.println("Каждый должен оплатить: " + (Math.round(finalSum * 100.0)/100.0) +
" " + formatter ((Math.round(finalSum * 100.0)/100.0)));
}
public String formatter (double number) {
if ((number % 10) > 1 && (number % 10) < 5)
return "рубля";
else if ((number % 10) > 4 && (number % 10) < 10)
return "рублей";
else if ((number % 10) > 0 && (number % 10) < 2)
return "рубль";
else if ((number % 10) == 0)
return "рублей";
return null;
}
}