Source of Withdrawal.java


  1: // Withdrawal.java
  2: // Represents a withdrawal ATM transaction
  3: 
  4: public class Withdrawal extends Transaction
  5: {
  6:    private int amount; // amount to withdraw
  7:    private Keypad keypad; // reference to keypad
  8:    private CashDispenser cashDispenser; // reference to cash dispenser
  9: 
 10:    // constant corresponding to menu option to cancel
 11:    private final static int CANCELED = 6;
 12: 
 13:    // Withdrawal constructor
 14:    public Withdrawal( int userAccountNumber, Screen atmScreen, 
 15:       BankDatabase atmBankDatabase, Keypad atmKeypad, 
 16:       CashDispenser atmCashDispenser )
 17:    {
 18:       // initialize superclass variables
 19:       super( userAccountNumber, atmScreen, atmBankDatabase );
 20:       
 21:       // initialize references to keypad and cash dispenser
 22:       keypad = atmKeypad;
 23:       cashDispenser = atmCashDispenser;
 24:    } // end Withdrawal constructor
 25: 
 26:    // perform transaction
 27:    public void execute()
 28:    {
 29:       boolean cashDispensed = false; // cash was not dispensed yet
 30:       double availableBalance; // amount available for withdrawal
 31: 
 32:       // get references to bank database and screen
 33:       BankDatabase bankDatabase = getBankDatabase(); 
 34:       Screen screen = getScreen();
 35: 
 36:       // loop until cash is dispensed or the user cancels
 37:       do
 38:       {
 39:          // obtain a chosen withdrawal amount from the user 
 40:          amount = displayMenuOfAmounts();
 41:          
 42:          // check whether user chose a withdrawal amount or canceled
 43:          if ( amount != CANCELED )
 44:          {
 45:             // get available balance of account involved
 46:             availableBalance = 
 47:                bankDatabase.getAvailableBalance( getAccountNumber() );
 48:       
 49:             // check whether the user has enough money in the account 
 50:             if ( amount <= availableBalance )
 51:             {   
 52:                // check whether the cash dispenser has enough money
 53:                if ( cashDispenser.isSufficientCashAvailable( amount ) )
 54:                {
 55:                   // update the account involved to reflect withdrawal
 56:                   bankDatabase.debit( getAccountNumber(), amount );
 57:                   
 58:                   cashDispenser.dispenseCash( amount ); // dispense cash
 59:                   cashDispensed = true; // cash was dispensed
 60: 
 61:                   // instruct user to take cash
 62:                   screen.displayMessageLine( 
 63:                      "\nPlease take your cash now." );
 64:                } // end if
 65:                else // cash dispenser does not have enough cash
 66:                   screen.displayMessageLine( 
 67:                      "\nInsufficient cash available in the ATM." +
 68:                      "\n\nPlease choose a smaller amount." );
 69:             } // end if
 70:             else // not enough money available in user's account
 71:             {
 72:                screen.displayMessageLine( 
 73:                   "\nInsufficient funds in your account." +
 74:                   "\n\nPlease choose a smaller amount." );
 75:             } // end else
 76:          } // end if
 77:          else // user chose cancel menu option 
 78:          {
 79:             screen.displayMessageLine( "\nCanceling transaction..." );
 80:             return; // return to main menu because user canceled
 81:          } // end else
 82:       } while ( !cashDispensed );
 83: 
 84:    } // end method execute
 85: 
 86:    // display a menu of withdrawal amounts and the option to cancel;
 87:    // return the chosen amount or 0 if the user chooses to cancel
 88:    private int displayMenuOfAmounts()
 89:    {
 90:       int userChoice = 0; // local variable to store return value
 91: 
 92:       Screen screen = getScreen(); // get screen reference
 93:       
 94:       // array of amounts to correspond to menu numbers
 95:       int amounts[] = { 0, 20, 40, 60, 100, 200 };
 96: 
 97:       // loop while no valid choice has been made
 98:       while ( userChoice == 0 )
 99:       {
100:          // display the menu
101:          screen.displayMessageLine( "\nWithdrawal Menu:" );
102:          screen.displayMessageLine( "1 - $20" );
103:          screen.displayMessageLine( "2 - $40" );
104:          screen.displayMessageLine( "3 - $60" );
105:          screen.displayMessageLine( "4 - $100" );
106:          screen.displayMessageLine( "5 - $200" );
107:          screen.displayMessageLine( "6 - Cancel transaction" );
108:          screen.displayMessage( "\nChoose a withdrawal amount: " );
109: 
110:          int input = keypad.getInput(); // get user input through keypad
111: 
112:          // determine how to proceed based on the input value
113:          switch ( input )
114:          {
115:             case 1: // if the user chose a withdrawal amount 
116:             case 2: // (i.e., chose option 1, 2, 3, 4 or 5), return the
117:             case 3: // corresponding amount from amounts array
118:             case 4:
119:             case 5:
120:                userChoice = amounts[ input ]; // save user's choice
121:                break;       
122:             case CANCELED: // the user chose to cancel
123:                userChoice = CANCELED; // save user's choice
124:                break;
125:             default: // the user did not enter a value from 1-6
126:                screen.displayMessageLine( 
127:                   "\nIvalid selection. Try again." );
128:          } // end switch
129:       } // end while
130: 
131:       return userChoice; // return withdrawal amount or CANCELED
132:    } // end method displayMenuOfAmounts
133: } // end class Withdrawal
134: 
135: 
136: 
137: /**************************************************************************
138:  * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
139:  * Pearson Education, Inc. All Rights Reserved.                           *
140:  *                                                                        *
141:  * DISCLAIMER: The authors and publisher of this book have used their     *
142:  * best efforts in preparing the book. These efforts include the          *
143:  * development, research, and testing of the theories and programs        *
144:  * to determine their effectiveness. The authors and publisher make       *
145:  * no warranty of any kind, expressed or implied, with regard to these    *
146:  * programs or to the documentation contained in these books. The authors *
147:  * and publisher shall not be liable in any event for incidental or       *
148:  * consequential damages in connection with, or arising out of, the       *
149:  * furnishing, performance, or use of these programs.                     *
150:  *************************************************************************/