01: import java.io.IOException;
02: import java.io.RandomAccessFile;
03: import java.util.Scanner;
04: 
05: /**
06:    This program tests random access. You can access existing
07:    accounts and deposit money, or create new accounts. The
08:    accounts are saved in a random access file.
09: */
10: public class BankDataTester
11: {  
12:    public static void main(String[] args)
13:          throws IOException
14:    {  
15:       Scanner in = new Scanner(System.in);
16:       BankData data = new BankData();
17:       try
18:       {  
19:          data.open("bank.dat");
20: 
21:          boolean done = false;
22:          while (!done)
23:          {  
24:             System.out.print("Account number: ");
25:             int accountNumber = in.nextInt();
26:             System.out.print("Amount to deposit: ");
27:             double amount = in.nextDouble();
28: 
29:             int position = data.find(accountNumber);
30:             BankAccount account;
31:             if (position >= 0)
32:             {
33:                account = data.read(position);
34:                account.deposit(amount);
35:                System.out.println("new balance=" 
36:                      + account.getBalance());
37:             }
38:             else // Add account
39:             {  
40:                account = new BankAccount(accountNumber,
41:                      amount);
42:                position = data.size();
43:                System.out.println("adding new account");
44:             }
45:             data.write(position, account);
46: 
47:             System.out.print("Done? (Y/N) ");
48:             String input = in.next();
49:             if (input.equalsIgnoreCase("Y")) done = true;
50:          }      
51:       }
52:       finally
53:       {
54:          data.close();
55:       }
56:    }
57: }
58: 
59: 
60: 
61: 
62: 
63: 
64: 
65: 
66: 
67: 
68: 
69: