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