1  // File: CreditInquiry.java (Fig. 15.7 in JHTP2)
  2  // This program reads a file sequentially and displays the
  3  // contents in a text area based on the type of account the
  4  // user requests (credit balance, debit balance or zero balance.
  5  
  6  import java.io.*;
  7  import java.awt.*;
  8  import java.awt.event.*;
  9  import java.text.DecimalFormat;
 10  
 11  public class CreditInquiry extends Frame
 12                             implements ActionListener {
 13  
 14     // application window components
 15     private TextArea recordDisplay;
 16     private Button done, credit, debit, zero;                    
 17     private Panel buttonPanel;         
 18  
 19     private RandomAccessFile input;
 20     private String accountType;
 21  
 22     public CreditInquiry()
 23     {
 24        super( "Credit Inquiry Program" );
 25  
 26        // Open the file
 27        try {
 28           input = new RandomAccessFile( "client.dat", "r" );
 29        }
 30        catch ( IOException e ) {
 31           System.err.println( e.toString() );
 32           System.exit( 1 );
 33        }
 34  
 35        setSize( 400, 150 );
 36  
 37        // create the components of the Frame
 38        buttonPanel = new Panel();
 39        credit = new Button( "Credit balances" );
 40        credit.addActionListener( this );
 41        buttonPanel.add( credit );
 42        debit = new Button( "Debit balances" );
 43        debit.addActionListener( this );
 44        buttonPanel.add( debit );
 45        zero = new Button( "Zero balances" );
 46        zero.addActionListener( this );
 47        buttonPanel.add( zero );
 48        done = new Button( "Done" );
 49        done.addActionListener( this );
 50        buttonPanel.add( done );
 51        recordDisplay = new TextArea( 4, 40 );
 52  
 53        // add the components to the Frame
 54        add( recordDisplay, BorderLayout.NORTH );
 55        add( buttonPanel, BorderLayout.SOUTH );
 56  
 57        setVisible( true );
 58     }
 59  
 60     public void actionPerformed( ActionEvent e )
 61     {
 62        if ( e.getSource() != done ) {
 63           accountType = e.getActionCommand();
 64           readRecords();
 65        }
 66        else {      // Close the file
 67           try {
 68              input.close();
 69              System.exit( 0 );
 70           }
 71           catch ( IOException ioe ) {
 72              System.err.println( "File not closed properly\n" +
 73                                  ioe.toString() );
 74              System.exit( 1 );
 75           }
 76        }
 77     }
 78  
 79     public void readRecords()
 80     {
 81        int account;
 82        String first, last;
 83        double balance;
 84        DecimalFormat twoDigits = new DecimalFormat( "0.00" );
 85  
 86        // input the values from the file
 87        try {      // to catch IOException
 88  
 89           try {   // to catch EOFException
 90              recordDisplay.setText( "The accounts are:\n" );
 91  
 92              while ( true ) {
 93                 account = input.readInt();
 94                 first = input.readUTF();
 95                 last = input.readUTF();
 96                 balance = input.readDouble();
 97  
 98                 if ( shouldDisplay( balance ) )
 99                    recordDisplay.append( account + "\t" +
100                       first + "\t" + last + "\t" +
101                       twoDigits.format( balance ) + "\n" );
102              }
103           }
104           catch ( EOFException eof ) {
105              input.seek( 0 );
106           }
107        }
108        catch ( IOException e ) {
109           System.err.println( "Error during read from file\n" +
110                               e.toString() );
111           System.exit( 1 );
112        }
113     }
114  
115     public boolean shouldDisplay( double balance )
116     {
117        if ( accountType.equals( "Credit balances" ) &&
118             balance < 0 )
119           return true;
120  
121        else if ( accountType.equals( "Debit balances" ) &&
122                  balance > 0 )
123           return true;
124  
125        else if ( accountType.equals( "Zero balances" ) &&
126                  balance == 0 )
127           return true;
128  
129        return false;
130     }
131  
132     // Instantiate a CreditInquiry object and start the program
133     public static void main( String args[] )
134     {
135        new CreditInquiry();
136     }
137  }
138