-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOperation.java
More file actions
56 lines (44 loc) · 1.56 KB
/
Copy pathOperation.java
File metadata and controls
56 lines (44 loc) · 1.56 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
package Collections;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
public class Operation {
private final String accNo;
private final BigDecimal amount;
public Operation(String accNo, BigDecimal amount) {
this.accNo = accNo;
this.amount = amount;
}
public String getAccNo() {
return accNo;
}
public BigDecimal getAmount() {
return amount;
}
public static void main(String[] args) {
var operations = List.of(
new Operation("123", new BigDecimal("10")),
new Operation("456", new BigDecimal("1200")),
new Operation("123", new BigDecimal("-4")),
new Operation("123", new BigDecimal("8")),
new Operation("456", new BigDecimal("800")),
new Operation("456", new BigDecimal("-1500")),
new Operation("123", new BigDecimal("2")),
new Operation("123", new BigDecimal("-6.5")),
new Operation("456", new BigDecimal("-600"))
);
var balances = new HashMap<String, BigDecimal>();
/*
operations.forEach(op -> {
var key = op.getAccNo();
balances.putIfAbsent(key, BigDecimal.ZERO);
balances.computeIfPresent(key, (accNo, prev) -> prev.add(op.getAmount()));
});
*/
operations.forEach(op ->
balances.merge(op.getAccNo(), op.getAmount(),
BigDecimal::add)
);
balances.forEach((k, v) -> System.out.println(k + " " + v));
}
}