01: package bank;
02:
03: import java.util.ArrayList;
04: import java.util.Collection;
05: import java.util.Iterator;
06:
07: /**
08: * @author S.Chassande-Barrioz
09: */
10: public class Agency {
11: protected String name;
12: protected Collection clients;
13: protected int lastAccountNumber;
14: protected Bank bank;
15:
16: public Agency() {
17: }
18:
19: public Agency(String name, Bank bank) {
20: this .name = name;
21: this .bank = bank;
22: lastAccountNumber = 0;
23: clients = new ArrayList();
24: }
25:
26: public Client getClient(String fn, String ln, String ad) {
27: Iterator it = clients.iterator();
28: while (it.hasNext()) {
29: Client c = (Client) it.next();
30: if (c.firstName.equals(fn) && c.lastName.equals(ln)
31: && c.address.equals(ad)) {
32: return c;
33: }
34: }
35: return null;
36: }
37:
38: public synchronized Client createClient(String fn, String ln,
39: String ad) {
40: Client client = getClient(fn, ln, ad);
41: if (client == null) {
42: client = new Client(ln, fn, ad, this );
43: }
44: return client;
45: }
46:
47: public void removeClient(Client c) {
48: if (clients.remove(c)) {
49: c.agency = null;
50: }
51: }
52:
53: public void removeAllClients() {
54: Iterator it = clients.iterator();
55: while (it.hasNext()) {
56: Client a = (Client) it.next();
57: a.agency = null;
58: }
59: clients.clear();
60: }
61:
62: public synchronized String getNextAccountNumber() {
63: lastAccountNumber++;
64: return getStringAccountNumber(lastAccountNumber);
65: }
66:
67: public static String getStringAccountNumber(int an) {
68: String str = String.valueOf(an);
69: for (int i = str.length(); i < 11; i++) {
70: str = '0' + str;
71: }
72: return str;
73: }
74: }
|