Repository navigation
Expand file tree
/
Copy pathCalculator.java
More file actions
69 lines (58 loc) · 2.2 KB
/
Copy pathCalculator.java
File metadata and controls
69 lines (58 loc) · 2.2 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
import java.util.ArrayList;
import java.util.Scanner;
public class Calculator {
private int numberOfPeople;
private double totalCost = 0;
private final ArrayList<String> listOfProducts;
private final ArrayList<Double> costOfProducts;
private final Formater format = new Formater();
private final Scanner scanner = new Scanner(System.in);
Calculator(){
listOfProducts = new ArrayList<>();
costOfProducts = new ArrayList<>();
}
public void start(){
System.out.print("Сколько человек будет делить счет: ");
this.numberOfPeople = format.scanInt();
addProduct();
while(true){
System.out.print("Желаете добавить ещё один товар? (Да/Завершить): ");
String str = scanner.next();
if(str.equalsIgnoreCase("Завершить")){
show();
break;
}
if(str.equalsIgnoreCase("Да")) {
addProduct();
}
}
}
public void addProduct(){
while(true){
System.out.print("Введите название продукта: ");
String str = scanner.next();
if(!str.isEmpty()){
this.listOfProducts.add(str);
addProductCost();
System.out.println("Товар успешно добавлен!");
break;
}
}
}
public void addProductCost(){
System.out.print("Введите цену товара: ");
double cost = format.scanDouble();
this.costOfProducts.add(cost);
this.totalCost += cost;
}
public void show(){
double result = totalCost/numberOfPeople;
String rubl = format.checkRes(result);
System.out.println("Добавленные товары: ");
for(int i = 0; i < listOfProducts.size(); i++){
System.out.printf("%-10s %20.2f \n", listOfProducts.get(i), costOfProducts.get(i));
}
System.out.printf("\nИтого с человека:%10.2f %-2s\n(Поделено на %d человек)\n",
result, rubl, numberOfPeople);
}
}