KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > bank > Client


1 package bank;
2
3 import java.util.Collection JavaDoc;
4 import java.util.ArrayList JavaDoc;
5 import java.util.Iterator JavaDoc;
6
7 /**
8  * @author S.Chassande-Barrioz
9  */

10 public class Client {
11     protected String JavaDoc lastName;
12     protected String JavaDoc firstName;
13     protected String JavaDoc address;
14     protected Collection JavaDoc accounts;
15     protected Agency agency;
16
17     public Client() {
18     }
19
20     public Client(String JavaDoc lastName,
21                   String JavaDoc firstName,
22                   String JavaDoc address,
23                   Agency agency) {
24         this.lastName = lastName;
25         this.firstName = firstName;
26         this.address = address;
27         this.agency = agency; // coherence managed by Speedo
28
this.accounts = new ArrayList JavaDoc();
29     }
30
31     private Account getAccount(String JavaDoc number) {
32         Iterator JavaDoc it = accounts.iterator();
33         while(it.hasNext()) {
34             Account a = (Account) it.next();
35             if (a.number.equals(number)) {
36                 return a;
37             }
38         }
39         return null;
40     }
41
42     public synchronized Account createAccount(String JavaDoc number) {
43         Account a = getAccount(number);
44         if (a == null) {
45             a = new Account(number, this, 0);
46         }
47         return a;
48     }
49
50     public synchronized void removeAccount(Account a) {
51         if (accounts.remove(a)) {
52             a.client = null;
53         }
54     }
55
56     public synchronized void removeAllAccounts() {
57         Iterator JavaDoc it = accounts.iterator();
58         while(it.hasNext()) {
59             Account a = (Account) it.next();
60             a.client = null;
61         }
62         accounts.clear();
63     }
64
65 }
66
Popular Tags