01: import java.io.Serializable;
02: import java.util.ArrayList;
03: 
04: /**
05:    This bank contains a collection of bank accounts.
06: */
07: public class Bank implements Serializable
08: {   
09:    /**
10:       Constructs a bank with no bank accounts.
11:    */
12:    public Bank()
13:    {
14:       accounts = new ArrayList<BankAccount>();
15:    }
16: 
17:    /**
18:       Adds an account to this bank.
19:       @param a the account to add
20:    */
21:    public void addAccount(BankAccount a)
22:    {
23:       accounts.add(a);
24:    }
25:    
26:    /**
27:       Gets the sum of the balances of all accounts in this bank.
28:       @return the sum of the balances
29:    */
30:    public double getTotalBalance()
31:    {
32:       double total = 0;
33:       for (BankAccount a : accounts)
34:       {
35:          total = total + a.getBalance();
36:       }
37:       return total;
38:    }
39: 
40:    /**
41:       Counts the number of bank accounts whose balance is at
42:       least a given value.
43:       @param atLeast the balance required to count an account
44:       @return the number of accounts having least the given balance
45:    */
46:    public int count(double atLeast)
47:    {
48:       int matches = 0;
49:       for (BankAccount a : accounts)
50:       {
51:          if (a.getBalance() >= atLeast) matches++; // Found a match
52:       }
53:       return matches;
54:    }
55: 
56:    /**
57:       Finds a bank account with a given number.
58:       @param accountNumber the number to find
59:       @return the account with the given number, or null if there
60:       is no such account
61:    */
62:    public BankAccount find(int accountNumber)
63:    {
64:       for (BankAccount a : accounts)
65:       {
66:          if (a.getAccountNumber() == accountNumber) // Found a match
67:             return a;
68:       } 
69:       return null; // No match in the entire array list
70:    }
71: 
72:    /**
73:       Gets the bank account with the largest balance
74:       @return the account with the largest balance, or null if the
75:       bank has no accounts
76:    */
77:    public BankAccount maximum()
78:    {
79:       if (accounts.size() == 0) return null;
80:       BankAccount largestYet = accounts.get(0);
81:       for (int i = 1; i < accounts.size(); i++) 
82:       {
83:          BankAccount a = accounts.get(i);
84:          if (a.getBalance() > largestYet.getBalance())
85:             largestYet = a;
86:       }
87:       return largestYet;
88:    }
89: 
90:    private ArrayList<BankAccount> accounts;
91: }