Repository navigation
Expand file tree
/
Copy pathCalculator.java
More file actions
66 lines (59 loc) · 2.17 KB
/
Copy pathCalculator.java
File metadata and controls
66 lines (59 loc) · 2.17 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
import java.util.ArrayList;
import java.util.Scanner;
public class Calculator {
ArrayList<Product> products = new ArrayList<Product>();
Scanner scanner = new Scanner(System.in);
double sum = 0;
int persons = 0;
public void calculate() {
while (true) {
System.out.println("Введите название товара");
String name = scanner.next();
if (name.equalsIgnoreCase("завершить")) {
break;
}
System.out.println("Введите стоимость товара");
double price;
try {
price = scanner.nextDouble();
} catch (Exception e) {
System.out.println("Вы ввели неверную цену. Давайте попробуем еще");
continue;
}
Product product = new Product(name, price);
products.add(product);
System.out.println("Товар успешно добавлен. Хотите добавить еще?");
}
System.out.println("fin");
this.computeSum();
this.printResults();
}
public void computeSum() {
for (Product product: this.products) {
this.sum += product.price;
}
}
public double computePay() {
return this.sum / this.persons;
}
public void printResults() {
System.out.println("Добавленные товары:");
for (Product product: this.products) {
System.out.println(product.name + " " + product.price);
}
if (this.sum == 0) {
this.computeSum();
}
double pay = this.computePay();
String rub = "рублей";
int last = (int)(Math.floor(pay)) % 10;
int decimal = (int)(Math.floor(pay) % 100);
if (last == 1 && decimal < 11 && decimal > 19 ) {
rub = "рубль";
}
if (last >= 2 && last <= 4 && decimal < 11 && decimal > 19) {
rub = "рубля";
}
System.out.println("Каждый должен заплатить: " + String.format("%.2f",pay) + " " + rub);
}
}