Source of CreditInquiry.java


  1: // Fig. 14.14: CreditInquiry.java
  2: // This program reads a file sequentially and displays the
  3: // contents based on the type of account the user requests 
  4: // (credit balance, debit balance or zero balance).
  5: import java.io.File;
  6: import java.io.FileNotFoundException;
  7: import java.lang.IllegalStateException;
  8: import java.util.NoSuchElementException;
  9: import java.util.Scanner;
 10: 
 11: import com.deitel.jhtp6.ch14.AccountRecord;
 12: 
 13: public class CreditInquiry
 14: {
 15:    private MenuOption accountType;
 16:    private Scanner input;
 17:    private MenuOption choices[] = { MenuOption.ZERO_BALANCE,
 18:       MenuOption.CREDIT_BALANCE, MenuOption.DEBIT_BALANCE,
 19:       MenuOption.END };
 20: 
 21:    // read records from file and display only records of appropriate type
 22:    private void readRecords()
 23:    {
 24:       // object to be written to file
 25:       AccountRecord record = new AccountRecord(); 
 26: 
 27:       try // read records
 28:       {     
 29:          // open file to read from beginning
 30:          input = new Scanner( new File( "clients.txt" ) );
 31: 
 32:          while ( input.hasNext() ) // input the values from the file
 33:          {
 34:             record.setAccount( input.nextInt() ); // read account number
 35:             record.setFirstName( input.next() ); // read first name
 36:             record.setLastName( input.next() ); // read last name
 37:             record.setBalance( input.nextDouble() ); // read balance
 38: 
 39:             // if proper acount type, display record
 40:             if ( shouldDisplay( record.getBalance() ) )
 41:                System.out.printf( "%-10d%-12s%-12s%10.2f\n",
 42:                   record.getAccount(), record.getFirstName(),
 43:                   record.getLastName(), record.getBalance() );
 44:          } // end while
 45:       } // end try
 46:       catch ( NoSuchElementException elementException )
 47:       {
 48:          System.err.println( "File improperly formed." );
 49:          input.close();
 50:          System.exit( 1 );
 51:       } // end catch
 52:       catch ( IllegalStateException stateException )
 53:       {
 54:          System.err.println( "Error reading from file." );
 55:          System.exit( 1 );
 56:       } // end catch
 57:       catch ( FileNotFoundException fileNotFoundException )
 58:       {
 59:          System.err.println( "File cannot be found." );
 60:          System.exit( 1 );
 61:       } // end catch
 62:       finally
 63:       {
 64:          if ( input != null )
 65:             input.close(); // close the Scanner and the file
 66:       } // end finally
 67:    } // end method readRecords
 68: 
 69:    // use record type to determine if record should be displayed
 70:    private boolean shouldDisplay( double balance )
 71:    {
 72:       if ( ( accountType == MenuOption.CREDIT_BALANCE )
 73:          && ( balance < 0 ) )
 74:          return true;
 75: 
 76:       else if ( ( accountType == MenuOption.DEBIT_BALANCE )
 77:          && ( balance > 0 ) )
 78:          return true;
 79: 
 80:       else if ( ( accountType == MenuOption.ZERO_BALANCE )
 81:          && ( balance == 0 ) )
 82:          return true;
 83: 
 84:       return false;
 85:    } // end method shouldDisplay
 86: 
 87:    // obtain request from user
 88:    private MenuOption getRequest()
 89:    {
 90:       Scanner textIn = new Scanner( System.in );
 91:       int request = 1;
 92: 
 93:       // display request options
 94:       System.out.printf( "\n%s\n%s\n%s\n%s\n%s\n",
 95:          "Enter request", " 1 - List accounts with zero balances",
 96:          " 2 - List accounts with credit balances",
 97:          " 3 - List accounts with debit balances", " 4 - End of run" );
 98: 
 99:       try // attempt to input menu choice
100:       {
101:          do // input user request
102:          {
103:             System.out.print( "\n? " );
104:             request = textIn.nextInt();
105:          } while ( ( request < 1 ) || ( request > 4 ) );
106:       } // end try
107:       catch ( NoSuchElementException elementException )
108:       {
109:          System.err.println( "Invalid input." );
110:          System.exit( 1 );
111:       } // end catch
112: 
113:       return choices[ request - 1 ]; // return enum value for option
114:    } // end method getRequest
115: 
116:    public void processRequests()
117:    {
118:       // get user's request (e.g., zero, credit or debit balance)
119:       accountType = getRequest();
120: 
121:       while ( accountType != MenuOption.END )
122:       {
123:          switch ( accountType )
124:          {
125:             case ZERO_BALANCE:
126:                System.out.println( "\nAccounts with zero balances:\n" );
127:                break;
128:             case CREDIT_BALANCE:
129:                System.out.println( "\nAccounts with credit balances:\n" );
130:                break;
131:             case DEBIT_BALANCE:
132:                System.out.println( "\nAccounts with debit balances:\n" );
133:                break;
134:          } // end switch
135: 
136:          readRecords();
137:          accountType = getRequest();
138:       } // end while
139:    } // end method processRequests
140: } // end class CreditInquiry
141: 
142: /*************************************************************************
143: * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
144: * Pearson Education, Inc. All Rights Reserved.                           *
145: *                                                                        *
146: * DISCLAIMER: The authors and publisher of this book have used their     *
147: * best efforts in preparing the book. These efforts include the          *
148: * development, research, and testing of the theories and programs        *
149: * to determine their effectiveness. The authors and publisher make       *
150: * no warranty of any kind, expressed or implied, with regard to these    *
151: * programs or to the documentation contained in these books. The authors *
152: * and publisher shall not be liable in any event for incidental or       *
153: * consequential damages in connection with, or arising out of, the       *
154: * furnishing, performance, or use of these programs.                     *
155: *************************************************************************/