1  import java.io.IOException;
  2  import java.io.RandomAccessFile;
  3  
  4  /**
  5     This class is a conduit to a random access file
  6     containing bank account records.
  7  */
  8  public class BankData
  9  {
 10     private RandomAccessFile file;
 11  
 12     public static final int INT_SIZE = 4;  
 13     public static final int DOUBLE_SIZE = 8;  
 14     public static final int RECORD_SIZE = INT_SIZE + DOUBLE_SIZE;
 15  
 16     /**
 17        Constructs a BankData object that is not associated with a file.
 18     */
 19     public BankData()
 20     {
 21        file = null;
 22     }
 23  
 24     /**
 25        Opens the data file.
 26        @param filename the name of the file containing bank
 27        account information
 28     */
 29     public void open(String filename)
 30           throws IOException
 31     {
 32        if (file != null) { file.close(); }
 33        file = new RandomAccessFile(filename, "rw");
 34     }
 35  
 36     /**
 37        Gets the number of accounts in the file.
 38        @return the number of accounts
 39     */
 40     public int size()
 41           throws IOException
 42     {
 43        return (int) (file.length() / RECORD_SIZE);
 44     }
 45  
 46     /**
 47        Closes the data file.
 48     */
 49     public void close()
 50           throws IOException
 51     {
 52        if (file != null) { file.close(); }
 53        file = null;
 54     }
 55  
 56     /**
 57        Reads a bank account record.
 58        @param n the index of the account in the data file
 59        @return a bank account object initialized with the file data
 60     */
 61     public BankAccount read(int n)
 62           throws IOException
 63     {  
 64        file.seek(n * RECORD_SIZE);      
 65        int accountNumber = file.readInt();
 66        double balance = file.readDouble();
 67        return new BankAccount(accountNumber, balance);
 68     }
 69  
 70     /**
 71        Finds the position of a bank account with a given number.
 72        @param accountNumber the number to find
 73        @return the position of the account with the given number, 
 74        or -1 if there is no such account
 75     */
 76     public int find(int accountNumber)
 77           throws IOException
 78     {
 79        for (int i = 0; i < size(); i++)
 80        {
 81           file.seek(i * RECORD_SIZE);
 82           int a = file.readInt();         
 83           if (a == accountNumber) { return i; }
 84              // Found a match            
 85        } 
 86        return -1; // No match in the entire file
 87     }
 88  
 89     /**
 90        Writes a bank account record to the data file.
 91        @param n the index of the account in the data file
 92        @param account the account to write
 93     */
 94     public void write(int n, BankAccount account)
 95           throws IOException
 96     {  
 97        file.seek(n * RECORD_SIZE);
 98        file.writeInt(account.getAccountNumber());
 99        file.writeDouble(account.getBalance());
100     }
101  }