01: package bank;
02:
03: import java.util.Collection;
04: import java.util.ArrayList;
05: import java.util.Iterator;
06:
07: /**
08: * @author S.Chassande-Barrioz
09: */
10: public class Client {
11: protected String lastName;
12: protected String firstName;
13: protected String address;
14: protected Collection accounts;
15: protected Agency agency;
16:
17: public Client() {
18: }
19:
20: public Client(String lastName, String firstName, String address,
21: Agency agency) {
22: this .lastName = lastName;
23: this .firstName = firstName;
24: this .address = address;
25: this .agency = agency; // coherence managed by Speedo
26: this .accounts = new ArrayList();
27: }
28:
29: private Account getAccount(String number) {
30: Iterator it = accounts.iterator();
31: while (it.hasNext()) {
32: Account a = (Account) it.next();
33: if (a.number.equals(number)) {
34: return a;
35: }
36: }
37: return null;
38: }
39:
40: public synchronized Account createAccount(String number) {
41: Account a = getAccount(number);
42: if (a == null) {
43: a = new Account(number, this , 0);
44: }
45: return a;
46: }
47:
48: public synchronized void removeAccount(Account a) {
49: if (accounts.remove(a)) {
50: a.client = null;
51: }
52: }
53:
54: public synchronized void removeAllAccounts() {
55: Iterator it = accounts.iterator();
56: while (it.hasNext()) {
57: Account a = (Account) it.next();
58: a.client = null;
59: }
60: accounts.clear();
61: }
62:
63: }
|