Source of RandomAccessAccountRecord.java


  1: // Fig. 14.23: RandomAccessAccountRecord.java
  2: // Subclass of AccountRecord for random-access file programs.
  3: package com.deitel.jhtp6.ch14; // packaged for reuse
  4: 
  5: import java.io.RandomAccessFile;
  6: import java.io.IOException;
  7: 
  8: public class RandomAccessAccountRecord extends AccountRecord
  9: {  
 10:     public static final int SIZE = 72;
 11: 
 12:    // no-argument constructor calls other constructor with default values
 13:    public RandomAccessAccountRecord()
 14:    {
 15:       this( 0, "", "", 0.0 );
 16:    } // end no-argument RandomAccessAccountRecord constructor
 17: 
 18:    // initialize a RandomAccessAccountRecord
 19:    public RandomAccessAccountRecord( int account, String firstName, 
 20:       String lastName, double balance )
 21:    {
 22:       super( account, firstName, lastName, balance );
 23:    } // end four-argument RandomAccessAccountRecord constructor
 24: 
 25:    // read a record from specified RandomAccessFile
 26:    public void read( RandomAccessFile file ) throws IOException
 27:    {
 28:       setAccount( file.readInt() );
 29:       setFirstName( readName( file ) );
 30:       setLastName( readName( file ) );
 31:       setBalance( file.readDouble() );
 32:    } // end method read
 33: 
 34:    // ensure that name is proper length
 35:    private String readName( RandomAccessFile file ) throws IOException
 36:    {
 37:       char name[] = new char[ 15 ], temp;
 38: 
 39:       for ( int count = 0; count < name.length; count++ )
 40:       {
 41:          temp = file.readChar();
 42:          name[ count ] = temp;
 43:       } // end for     
 44:       
 45:       return new String( name ).replace( '\0', ' ' );
 46:    } // end method readName
 47: 
 48:    // write a record to specified RandomAccessFile
 49:    public void write( RandomAccessFile file ) throws IOException
 50:    {
 51:       file.writeInt( getAccount() );
 52:       writeName( file, getFirstName() );
 53:       writeName( file, getLastName() );
 54:       file.writeDouble( getBalance() );
 55:    } // end method write
 56: 
 57:    // write a name to file; maximum of 15 characters
 58:    private void writeName( RandomAccessFile file, String name )
 59:       throws IOException
 60:    {
 61:       StringBuffer buffer = null;
 62: 
 63:       if ( name != null ) 
 64:          buffer = new StringBuffer( name );
 65:       else 
 66:          buffer = new StringBuffer( 15 );
 67: 
 68:       buffer.setLength( 15 );
 69:       file.writeChars( buffer.toString() );
 70:    } // end method writeName
 71: } // end class RandomAccessAccountRecord
 72: 
 73: /*************************************************************************
 74: * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
 75: * Pearson Education, Inc. All Rights Reserved.                           *
 76: *                                                                        *
 77: * DISCLAIMER: The authors and publisher of this book have used their     *
 78: * best efforts in preparing the book. These efforts include the          *
 79: * development, research, and testing of the theories and programs        *
 80: * to determine their effectiveness. The authors and publisher make       *
 81: * no warranty of any kind, expressed or implied, with regard to these    *
 82: * programs or to the documentation contained in these books. The authors *
 83: * and publisher shall not be liable in any event for incidental or       *
 84: * consequential damages in connection with, or arising out of, the       *
 85: * furnishing, performance, or use of these programs.                     *
 86: *************************************************************************/