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