-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBank.java
More file actions
74 lines (64 loc) · 2.26 KB
/
Copy pathBank.java
File metadata and controls
74 lines (64 loc) · 2.26 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
package Phase3_ObjectOrientation.BankingApp;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
/**
* Bank - aggregate root that owns many Accounts.
* <p>
*
* Demonstrates:
* - ENCAPSULATION - accounts list is hidden; callers go through openAccount,
* find, applyMonthlyCycle.
* - POLYMORPHISM - applyMonthlyCycle iterates over Account references;
* each subclass's overridden calculateInterest runs.
* - SINGLE RESPONSIBILITY - the Bank schedules; each Account owns its rules.
*/
public class Bank {
private final String name;
private final List<Account> accounts = new ArrayList<>();
public Bank(String name) {
if (name == null || name.isBlank()) {
throw new IllegalArgumentException("bank name required");
}
this.name = name;
}
public String name() { return name; }
/** Add an already-constructed account to this bank. */
public <A extends Account> A openAccount(A account) {
accounts.add(account);
return account;
}
/** Look up by account number. */
public Optional<Account> find(String accountNumber) {
for (Account a : accounts) {
if (a.accountNumber().equals(accountNumber)) return Optional.of(a);
}
return Optional.empty();
}
/** Read-only view of all accounts. */
public List<Account> accounts() {
return Collections.unmodifiableList(accounts);
}
/**
* Runs the monthly cycle for every account. The runtime dispatches to
* each subclass's calculateInterest() - the textbook example of runtime
* polymorphism.
*/
public void applyMonthlyCycle() {
for (Account a : accounts) {
a.applyInterest();
}
}
/** Print a per-account summary - useful for a quick demo. */
public void printStatement() {
System.out.println("\n=== " + name + " - Account Statement ===");
for (Account a : accounts) {
System.out.println(a);
for (Transaction t : a.history()) {
System.out.printf(" %-9s %8.2f -> balance %8.2f%n",
t.type(), t.amount(), t.balanceAfter());
}
}
}
}