Repository navigation
Expand file tree
/
Copy pathCalculator.java
More file actions
59 lines (52 loc) · 2.05 KB
/
Copy pathCalculator.java
File metadata and controls
59 lines (52 loc) · 2.05 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
import core.Communicator;
import exceptions.PersonsAboveZeroException;
import exceptions.PersonsOnlyException;
import exceptions.PersonsZeroException;
import models.Good;
import services.GoodsReadIterator;
import utils.Consts;
import java.util.ArrayList;
public class Calculator {
private final Communicator communicator;
private final GoodsReadIterator iterator;
private final ArrayList<Good> goods;
private final int personsQuantity;
private Calculator(Communicator communicator, int quantity){
this.communicator = communicator;
this.iterator = new GoodsReadIterator(this.communicator);
this.personsQuantity = quantity;
goods = new ArrayList<>();
}
public static Calculator createInstance(int quantity, Communicator communicator) {
if(quantity == 1) {
throw new PersonsOnlyException(Consts.ONLYPERSON_MESSAGE);
} else if (quantity == 0) {
throw new PersonsZeroException(Consts.ZEROPERSONS_MESSAGE);
} else if (quantity < 0) {
throw new PersonsAboveZeroException(Consts.LOWPERSONS_MESSAGE);
}
return new Calculator(communicator, quantity);
}
public void readGoods() {
do {
var good = iterator.next();
goods.add(good);
communicator.showNotification(Consts.ADD_SUCCESS_PROMT);
} while(iterator.hasNext());
}
public ArrayList<Good> getGoods() {
return goods;
}
public double totalPrice() {
return getGoods().stream().mapToDouble(Good::getPrice).sum();
}
public void printGoods() {
communicator.showNotification(Consts.ENTER_COMPLETE_PROMT);
for(var good: getGoods()) {
communicator.showNotification(good.toString());
}
var sharedPrice = totalPrice() / personsQuantity;
communicator.showNotification(String.format("Общий счет %.2f", totalPrice()));
communicator.showNotification(String.format("На каждого из %d человек по %.2f", personsQuantity, sharedPrice));
}
}