1 |
import java.io.IOException; |
2 |
import java.io.RandomAccessFile; |
3 |
import javax.swing.JOptionPane; |
4 |
|
5 |
/** |
6 |
This program tests random access. You can access existing |
7 |
accounts and add interest, or create a new accounts. The |
8 |
accounts are saved in a random access file. |
9 |
*/ |
10 |
public class BankDataTest |
11 |
{ |
12 |
public static void main(String[] args) |
13 |
throws IOException |
14 |
{ |
15 |
BankData data = new BankData(); |
16 |
try |
17 |
{ |
18 |
data.open("bank.dat"); |
19 |
|
20 |
boolean done = false; |
21 |
while (!done) |
22 |
{ |
23 |
String input = JOptionPane.showInputDialog( |
24 |
"Account number or " + data.size() |
25 |
+ " for new account"); |
26 |
if (input == null) done = true; |
27 |
else |
28 |
{ |
29 |
int pos = Integer.parseInt(input); |
30 |
|
31 |
if (0 <= POs && POs < data.size()) // add interest |
32 |
{ |
33 |
SavingsAccount account = data.read(POs); |
34 |
System.out.println("balance=" |
35 |
+ account.getBalance() + ",interest rate=" |
36 |
+ account.getInterestRate()); |
37 |
account.addInterest(); |
38 |
data.write(POs, account); |
39 |
} |
40 |
else // add account |
41 |
{ |
42 |
input = JOptionPane.showInputDialog("Balance"); |
43 |
double balance = Double.parseDouble(input); |
44 |
input = JOptionPane.showInputDialog("Interest Rate"); |
45 |
double interestRate = Double.parseDouble(input); |
46 |
SavingsAccount account |
47 |
= new SavingsAccount(interestRate); |
48 |
account.deposit(balance); |
49 |
data.write(data.size(), account); |
50 |
} |
51 |
} |
52 |
} |
53 |
} |
54 |
finally |
55 |
{ |
56 |
data.close(); |
57 |
System.exit(0); |
58 |
} |
59 |
} |
60 |
} |