Source of FileEditor.java


  1: // Fig. 14.34: FileEditor.java
  2: // This class declares methods that manipulate bank account
  3: // records in a random access file.
  4: import java.io.EOFException;
  5: import java.io.File;
  6: import java.io.IOException;
  7: import java.io.RandomAccessFile;
  8: import java.util.Scanner;
  9: 
 10: import com.deitel.jhtp6.ch14.RandomAccessAccountRecord;
 11: 
 12: public class FileEditor
 13: {
 14:    RandomAccessFile file; // reference to the file
 15:    Scanner input = new Scanner( System.in );
 16:    
 17:    // open the file
 18:    public FileEditor( String fileName ) throws IOException
 19:    {
 20:       file = new RandomAccessFile( fileName, "rw" );
 21:    } // end FileEditor constructor
 22:    
 23:    // close the file
 24:    public void closeFile() throws IOException
 25:    {
 26:       if ( file != null )
 27:          file.close();
 28:    } // end method closeFile
 29:    
 30:    // get a record from the file
 31:    public RandomAccessAccountRecord getRecord( int accountNumber )
 32:       throws IllegalArgumentException, NumberFormatException, IOException
 33:    {
 34:       RandomAccessAccountRecord record = new RandomAccessAccountRecord();
 35: 
 36:       if ( accountNumber < 1 || accountNumber > 100 )
 37:          throw new IllegalArgumentException( "Out of range" );
 38: 
 39:       // seek appropriate record in file
 40:       file.seek( ( accountNumber - 1 ) * RandomAccessAccountRecord.SIZE );
 41:       
 42:       record.read( file );
 43: 
 44:       return record;
 45:    } // end method getRecord
 46:    
 47:    // update record in file
 48:    public void updateRecord( int accountNumber, double transaction )
 49:       throws IllegalArgumentException, IOException
 50:    {
 51:       RandomAccessAccountRecord record = getRecord( accountNumber );
 52: 
 53:       if ( record.getAccount() == 0 )
 54:          throw new IllegalArgumentException( "Account does not exist" );
 55: 
 56:       // seek appropriate record in file
 57:       file.seek( ( accountNumber - 1 ) * RandomAccessAccountRecord.SIZE );   
 58: 
 59:       record = new RandomAccessAccountRecord(
 60:          record.getAccount(), record.getFirstName(),
 61:          record.getLastName(), record.getBalance() + transaction );
 62:          
 63:       record.write( file ); // write updated record to file      
 64:    } // end method updateRecord
 65:    
 66:    // add record to file
 67:    public void newRecord( int accountNumber, String firstName, 
 68:       String lastName, double balance )
 69:       throws IllegalArgumentException, IOException
 70:    {
 71:       RandomAccessAccountRecord record = getRecord( accountNumber );
 72:       
 73:       if ( record.getAccount() != 0 )
 74:          throw new IllegalArgumentException( "Account already exists" );
 75: 
 76:       // seek appropriate record in file
 77:       file.seek( ( accountNumber - 1 ) * RandomAccessAccountRecord.SIZE );   
 78: 
 79:       record = new RandomAccessAccountRecord( accountNumber, 
 80:          firstName, lastName, balance );
 81:          
 82:       record.write( file ); // write record to file      
 83:    } // end method newRecord
 84:    
 85:    // delete record from file
 86:    public void deleteRecord( int accountNumber )
 87:       throws IllegalArgumentException, IOException
 88:    {
 89:       RandomAccessAccountRecord record = getRecord( accountNumber );
 90:       
 91:       if ( record.getAccount() == 0 )
 92:          throw new IllegalArgumentException( "Account does not exist" );
 93:       
 94:       // seek appropriate record in file
 95:       file.seek( ( accountNumber - 1 ) * RandomAccessAccountRecord.SIZE );
 96: 
 97:       // create a blank record to write to the file
 98:       record = new RandomAccessAccountRecord();
 99:       record.write( file );      
100:    } // end method deleteRecord
101: 
102:    // read and display records
103:    public void readRecords()
104:    {
105:       RandomAccessAccountRecord record = new RandomAccessAccountRecord();
106: 
107:       System.out.printf( "%-10s%-15s%-15s%10s\n", "Account",
108:          "First Name", "Last Name", "Balance" );
109:    
110:       try // read a record and display
111:       {
112:          file.seek( 0 );
113: 
114:          while ( true )
115:          {
116:             do
117:             {
118:                record.read( file );
119:             } while ( record.getAccount() == 0 );
120: 
121:             // display record contents
122:             System.out.printf( "%-10d%-15s%-15s%10.2f\n",
123:                record.getAccount(), record.getFirstName(),
124:                record.getLastName(), record.getBalance() );
125:          } // end while
126:       } // end try
127:       catch ( EOFException eofException ) // close file
128:       {
129:          return; // end of file was reached
130:       } // end catch
131:       catch ( IOException ioException )
132:       {
133:          System.err.println( "Error reading file." );
134:          System.exit( 1 );
135:       } // end catch
136:    } // end method readRecords  
137: } // end class FileEditor
138: 
139: /*************************************************************************
140: * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
141: * Pearson Education, Inc. All Rights Reserved.                           *
142: *                                                                        *
143: * DISCLAIMER: The authors and publisher of this book have used their     *
144: * best efforts in preparing the book. These efforts include the          *
145: * development, research, and testing of the theories and programs        *
146: * to determine their effectiveness. The authors and publisher make       *
147: * no warranty of any kind, expressed or implied, with regard to these    *
148: * programs or to the documentation contained in these books. The authors *
149: * and publisher shall not be liable in any event for incidental or       *
150: * consequential damages in connection with, or arising out of, the       *
151: * furnishing, performance, or use of these programs.                     *
152: *************************************************************************/