Source of ATM.java


  1: // ATM.java
  2: // Represents an automated teller machine

  4: public class ATM 
  5: {
  6:    private boolean userAuthenticated; // whether user is authenticated
  7:    private int currentAccountNumber; // current user's account number
  8:    private Screen screen; // ATM's screen
  9:    private Keypad keypad; // ATM's keypad
 10:    private CashDispenser cashDispenser; // ATM's cash dispenser
 11:    private DepositSlot depositSlot; // ATM's deposit slot
 12:    private BankDatabase bankDatabase; // account information database

 14:    // constants corresponding to main menu options
 15:    private static final int BALANCE_INQUIRY = 1;
 16:    private static final int WITHDRAWAL = 2;
 17:    private static final int DEPOSIT = 3;
 18:    private static final int EXIT = 4;

 20:    // no-argument ATM constructor initializes instance variables
 21:    public ATM() 
 22:    {
 23:       userAuthenticated = false; // user is not authenticated to start
 24:       currentAccountNumber = 0; // no current account number to start
 25:       screen = new Screen(); // create screen
 26:       keypad = new Keypad(); // create keypad 
 27:       cashDispenser = new CashDispenser(); // create cash dispenser
 28:       depositSlot = new DepositSlot(); // create deposit slot
 29:       bankDatabase = new BankDatabase(); // create acct info database
 30:    } // end no-argument ATM constructor

 32:    // start ATM 
 33:    public void run()
 34:    {
 35:       // welcome and authenticate user; perform transactions
 36:       while ( true )
 37:       {
 38:          // loop while user is not yet authenticated
 39:          while ( !userAuthenticated ) 
 40:          {
 41:             screen.displayMessageLine( "\nWelcome!" );       
 42:             authenticateUser(); // authenticate user
 43:          } // end while
 44:          
 45:          performTransactions(); // user is now authenticated 
 46:          userAuthenticated = false; // reset before next ATM session
 47:          currentAccountNumber = 0; // reset before next ATM session 
 48:          screen.displayMessageLine( "\nThank you! Goodbye!" );
 49:       } // end while   
 50:    } // end method run

 52:    // attempts to authenticate user against database
 53:    private void authenticateUser() 
 54:    {
 55:       screen.displayMessage( "\nPlease enter your account number: " );
 56:       int accountNumber = keypad.getInput(); // input account number
 57:       screen.displayMessage( "\nEnter your PIN: " ); // prompt for PIN
 58:       int pin = keypad.getInput(); // input PIN
 59:       
 60:       // set userAuthenticated to boolean value returned by database
 61:       userAuthenticated = 
 62:          bankDatabase.authenticateUser( accountNumber, pin );
 63:       
 64:       // check whether authentication succeeded
 65:       if ( userAuthenticated )
 66:       {
 67:          currentAccountNumber = accountNumber; // save user's account #
 68:       } // end if
 69:       else
 70:          screen.displayMessageLine( 
 71:              "Invalid account number or PIN. Please try again." );
 72:    } // end method authenticateUser

 74:    // display the main menu and perform transactions
 75:    private void performTransactions() 
 76:    {
 77:       // local variable to store transaction currently being processed
 78:       Transaction currentTransaction = null;
 79:       
 80:       boolean userExited = false; // user has not chosen to exit

 82:       // loop while user has not chosen option to exit system
 83:       while ( !userExited )
 84:       {     
 85:          // show main menu and get user selection
 86:          int mainMenuSelection = displayMainMenu();

 88:          // decide how to proceed based on user's menu selection
 89:          switch ( mainMenuSelection )
 90:          {
 91:             // user chose to perform one of three transaction types
 92:             case BALANCE_INQUIRY: 
 93:             case WITHDRAWAL: 
 94:             case DEPOSIT:

 96:                // initialize as new object of chosen type
 97:                currentTransaction = 
 98:                   createTransaction( mainMenuSelection );

100:                currentTransaction.execute(); // execute transaction
101:                break; 
102:             case EXIT: // user chose to terminate session
103:                screen.displayMessageLine( "\nExiting the system..." );
104:                userExited = true; // this ATM session should end
105:                break;
106:             default: // user did not enter an integer from 1-4
107:                screen.displayMessageLine( 
108:                   "\nYou did not enter a valid selection. Try again." );
109:                break;
110:          } // end switch
111:       } // end while
112:    } // end method performTransactions
113:    
114:    // display the main menu and return an input selection
115:    private int displayMainMenu()
116:    {
117:       screen.displayMessageLine( "\nMain menu:" );
118:       screen.displayMessageLine( "1 - View my balance" );
119:       screen.displayMessageLine( "2 - Withdraw cash" );
120:       screen.displayMessageLine( "3 - Deposit funds" );
121:       screen.displayMessageLine( "4 - Exit\n" );
122:       screen.displayMessage( "Enter a choice: " );
123:       return keypad.getInput(); // return user's selection
124:    } // end method displayMainMenu
125:          
126:    // return object of specified Transaction subclass
127:    private Transaction createTransaction( int type )
128:    {
129:       Transaction temp = null; // temporary Transaction variable
130:       
131:       // determine which type of Transaction to create     
132:       switch ( type )
133:       {
134:          case BALANCE_INQUIRY: // create new BalanceInquiry transaction
135:             temp = new BalanceInquiry( 
136:                currentAccountNumber, screen, bankDatabase );
137:             break;
138:          case WITHDRAWAL: // create new Withdrawal transaction
139:             temp = new Withdrawal( currentAccountNumber, screen, 
140:                bankDatabase, keypad, cashDispenser );
141:             break; 
142:          case DEPOSIT: // create new Deposit transaction
143:             temp = new Deposit( currentAccountNumber, screen, 
144:                bankDatabase, keypad, depositSlot );
145:             break;
146:       } // end switch

148:       return temp; // return the newly created object
149:    } // end method createTransaction
150: } // end class ATM



154: /**************************************************************************
155:  * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
156:  * Pearson Education, Inc. All Rights Reserved.                           *
157:  *                                                                        *
158:  * DISCLAIMER: The authors and publisher of this book have used their     *
159:  * best efforts in preparing the book. These efforts include the          *
160:  * development, research, and testing of the theories and programs        *
161:  * to determine their effectiveness. The authors and publisher make       *
162:  * no warranty of any kind, expressed or implied, with regard to these    *
163:  * programs or to the documentation contained in these books. The authors *
164:  * and publisher shall not be liable in any event for incidental or       *
165:  * consequential damages in connection with, or arising out of, the       *
166:  * furnishing, performance, or use of these programs.                     *
167:  *************************************************************************/