1 package bank; 2 3 import java.util.ArrayList ; 4 import java.util.Collection ; 5 import java.util.Iterator ; 6 7 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, 27 String ln, 28 String ad) { 29 Iterator it = clients.iterator(); 30 while(it.hasNext()) { 31 Client c = (Client) it.next(); 32 if (c.firstName.equals(fn) 33 && c.lastName.equals(ln) && c.address.equals(ad) ) { 34 return c; 35 } 36 } 37 return null; 38 } 39 40 public synchronized Client createClient(String fn, 41 String ln, 42 String ad) { 43 Client client = getClient(fn, ln, ad); 44 if (client == null) { 45 client = new Client(ln, fn, ad, this); 46 } 47 return client; 48 } 49 50 public void removeClient(Client c) { 51 if (clients.remove(c)) { 52 c.agency = null; 53 } 54 } 55 56 public void removeAllClients() { 57 Iterator it = clients.iterator(); 58 while (it.hasNext()) { 59 Client a = (Client) it.next(); 60 a.agency = null; 61 } 62 clients.clear(); 63 } 64 65 public synchronized String getNextAccountNumber() { 66 lastAccountNumber++; 67 return getStringAccountNumber(lastAccountNumber); 68 } 69 70 public static String getStringAccountNumber(int an) { 71 String str = String.valueOf(an); 72 for(int i=str.length(); i<11; i++) { 73 str = '0' + str; 74 } 75 return str; 76 } 77 } 78 | Popular Tags |