forked from fracz/java-invoice
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInvoice.java
More file actions
79 lines (62 loc) · 1.39 KB
/
Invoice.java
File metadata and controls
79 lines (62 loc) · 1.39 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
package pl.edu.agh.mwo.invoice;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
import pl.edu.agh.mwo.invoice.product.Product;
public class Invoice {
private Collection<Product> products;
private BigDecimal subTotal;
private BigDecimal total;
private BigDecimal tax;
Invoice() {
subTotal = new BigDecimal("0");
total = new BigDecimal("0");
tax = new BigDecimal("0");
products = new ArrayList<Product>();
}
public void addProduct(Product product) {
products.add(product);
update();
}
public void addProduct(Product product, Integer quantity) {
if (quantity <= 0) {
throw new IllegalArgumentException();
}
for (int i = 0; i < quantity; i++) {
products.add(product);
}
update();
}
public BigDecimal getSubtotal() {
return subTotal;
}
public void setSubtotal() {
subTotal = BigDecimal.ZERO;
for (Product product : products) {
subTotal = subTotal.add(product.getPrice());
}
}
public BigDecimal getTax() {
return tax;
}
public void setTax() {
tax = total.subtract(subTotal);
}
public BigDecimal getTotal() {
return total;
}
public void setTotal() {
total = BigDecimal.ZERO;
for (Product product : products) {
total = total.add(product.getPriceWithTax());
}
}
public static Invoice create() {
return new Invoice();
}
public void update() {
setSubtotal();
setTotal();
setTax();
}
}