Repository navigation
Expand file tree
/
Copy pathInvoice.java
More file actions
73 lines (58 loc) · 2.07 KB
/
Copy pathInvoice.java
File metadata and controls
73 lines (58 loc) · 2.07 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
package de.codecentric.java8examples;
import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;
public class Invoice {
private String sender;
private String recipient;
private List<InvoiceItem> items;
public String getSender() {
return sender;
}
public String getRecipient() {
return recipient;
}
public List<InvoiceItem> getItems() {
return items;
}
public BigDecimal getTotal() {
return getItems().stream()
.<BigDecimal>map(invoice -> invoice
.getPricePerUnit()
.multiply(BigDecimal.valueOf(invoice.getQuantity())))
.collect(Collectors.reducing(
BigDecimal.ZERO,
(sum, elem) -> sum.add(elem)));
}
public Invoice(String sender, String recipient, List<InvoiceItem> items) {
this.sender = sender;
this.recipient = recipient;
this.items = items;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Invoice invoice = (Invoice) o;
if (items != null ? !items.equals(invoice.items) : invoice.items != null) return false;
if (recipient != null ? !recipient.equals(invoice.recipient) : invoice.recipient != null) return false;
if (sender != null ? !sender.equals(invoice.sender) : invoice.sender != null) return false;
return true;
}
@Override
public int hashCode() {
int result = sender != null ? sender.hashCode() : 0;
result = 31 * result + (recipient != null ? recipient.hashCode() : 0);
result = 31 * result + (items != null ? items.hashCode() : 0);
return result;
}
@Override
public String toString() {
return "Invoice{" +
"sender='" + sender + '\'' +
", recipient='" + recipient + '\'' +
", items=" + items +
'}';
}
}