001: import java.io.IOException;
002: import java.io.RandomAccessFile;
003: 
004: /**
005:    This class is a conduit to a random access file
006:    containing savings account data.
007: */
008: public class BankData
009: {
010:    /**
011:       Constructs a BankData object that is not associated
012:       with a file.
013:    */
014:    public BankData()
015:    {
016:       file = null;
017:    }
018: 
019:    /**
020:       Opens the data file.
021:       @param filename the name of the file containing savings
022:       account information
023:    */
024:    public void open(String filename)
025:          throws IOException
026:    {
027:       if (file != null) file.close();
028:       file = new RandomAccessFile(filename, "rw");
029:    }
030: 
031:    /**
032:       Gets the number of accounts in the file.
033:       @return the number of accounts
034:    */
035:    public int size()
036:          throws IOException
037:    {
038:       return (int) (file.length() / RECORD_SIZE);
039:    }
040: 
041:    /**
042:       Closes the data file.
043:    */
044:    public void close()
045:          throws IOException
046:    {
047:       if (file != null) file.close();
048:       file = null;
049:    }
050: 
051:    /**
052:       Reads a savings account record.
053:       @param n the index of the account in the data file
054:       @return a savings account object initialized with the file data
055:    */
056:    public BankAccount read(int n)
057:          throws IOException
058:    {  
059:       file.seek(n * RECORD_SIZE);      
060:       int accountNumber = file.readInt();
061:       double balance = file.readDouble();
062:       return new BankAccount(accountNumber, balance);
063:    }
064: 
065:    /**
066:       Finds the position of a bank account with a given number
067:       @param accountNumber the number to find
068:       @return the position of the account with the given number, 
069:       or -1 if there is no such account
070:    */
071:    public int find(int accountNumber)
072:          throws IOException
073:    {
074:       for (int i = 0; i < size(); i++)
075:       {
076:          file.seek(i * RECORD_SIZE);
077:          int a = file.readInt();         
078:          if (a == accountNumber) // Found a match
079:             return i;
080:       } 
081:       return -1; // No match in the entire file
082:    }
083: 
084:    /**
085:       Writes a savings account record to the data file
086:       @param n the index of the account in the data file
087:       @param account the account to write
088:    */
089:    public void write(int n, BankAccount account)
090:          throws IOException
091:    {  
092:       file.seek(n * RECORD_SIZE);
093:       file.writeInt(account.getAccountNumber());
094:       file.writeDouble(account.getBalance());
095:    }
096: 
097:    private RandomAccessFile file;
098: 
099:    public static final int INT_SIZE = 4;  
100:    public static final int DOUBLE_SIZE = 8;  
101:    public static final int RECORD_SIZE 
102:          = INT_SIZE + DOUBLE_SIZE;
103: }