1 |
import java.io.BufferedReader; |
2 |
import java.io.FileReader; |
3 |
import java.io.IOException; |
4 |
import java.util.ArrayList; |
5 |
import java.util.StringTokenizer; |
6 |
|
7 |
/** |
8 |
A bank contains customers with bank accounts. |
9 |
*/ |
10 |
public class Bank |
11 |
{ |
12 |
/** |
13 |
Constructs a bank with no customers. |
14 |
*/ |
15 |
public Bank() |
16 |
{ |
17 |
customers = new ArrayList(); |
18 |
} |
19 |
|
20 |
/** |
21 |
Reads the customer numbers and pins |
22 |
and initializes the bank accounts. |
23 |
@param filename the name of the customer file |
24 |
*/ |
25 |
public void readCustomers(String filename) |
26 |
throws IOException |
27 |
{ |
28 |
BufferedReader in = new BufferedReader |
29 |
(new FileReader(filename)); |
30 |
boolean done = false; |
31 |
while (!done) |
32 |
{ |
33 |
String inputLine = in.readLine(); |
34 |
if (inputLine == null) done = true; |
35 |
else |
36 |
{ |
37 |
StringTokenizer tokenizer |
38 |
= new StringTokenizer(inputLine); |
39 |
int number |
40 |
= Integer.parseInt(tokenizer.nextToken()); |
41 |
int pin |
42 |
= Integer.parseInt(tokenizer.nextToken()); |
43 |
|
44 |
Customer c = new Customer(number, pin); |
45 |
addCustomer(c); |
46 |
} |
47 |
} |
48 |
in.close(); |
49 |
} |
50 |
|
51 |
/** |
52 |
Adds a customer to the bank. |
53 |
@param c the customer to add |
54 |
*/ |
55 |
public void addCustomer(Customer c) |
56 |
{ |
57 |
customers.add(c); |
58 |
} |
59 |
|
60 |
/** |
61 |
Finds a customer in the bank. |
62 |
@param aNumber a customer number |
63 |
@param aPin a personal identification number |
64 |
@return the matching customer, or null if no customer |
65 |
matches |
66 |
*/ |
67 |
public Customer findCustomer(int aNumber, int aPin) |
68 |
{ |
69 |
for (int i = 0; i < customers.size(); i++) |
70 |
{ |
71 |
Customer c = (Customer)customers.get(i); |
72 |
if (c.match(aNumber, aPin)) |
73 |
return c; |
74 |
} |
75 |
return null; |
76 |
} |
77 |
|
78 |
private ArrayList customers; |
79 |
} |
80 |
|
81 |
|