Advertisement
GSculerlor

Withdrawal.java

Oct 26th, 2017
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.21 KB | None | 0 0
  1. /**
  2.  * Represents a withdrawal ATM transaction
  3.  */
  4. public class Withdrawal extends Transaction {
  5.     private int amount; // amount to withdraw
  6.     private Keypad keypad; // reference to keypad
  7.     private CashDispenser cashDispenser; // reference to cash dispenser
  8.  
  9.     // constant corresponding to menu option to cancel
  10.     private final static int CANCELED = 6;
  11.  
  12.     // Withdrawal constructor
  13.     public Withdrawal(int userAccountNumber, Screen atmScreen, BankDatabase atmBankDatabase, Keypad atmKeypad,
  14.             CashDispenser atmCashDispenser) {
  15.         // initialize superclass variables
  16.         super(userAccountNumber, atmScreen, atmBankDatabase);
  17.  
  18.         // initialize references to keypad and cash dispenser
  19.         keypad = atmKeypad;
  20.         cashDispenser = atmCashDispenser;
  21.     } // end Withdrawal constructor
  22.  
  23.     // perform transaction
  24.     @Override
  25.     public void execute() {
  26.         boolean cashDispensed = false; // cash was not dispensed yet
  27.         double availableBalance; // amount available for withdrawal
  28.  
  29.         // get references to bank database and screen
  30.         BankDatabase bankDatabase = getBankDatabase();
  31.         Screen screen = getScreen();
  32.  
  33.         // loop until cash is dispensed or the user cancels
  34.         do {
  35.             // obtain a chosen withdrawal amount from the user
  36.             amount = displayMenuOfAmounts();
  37.  
  38.             // check whether user chose a withdrawal amount or canceled
  39.             if (amount != CANCELED) {
  40.                 // get available balance of account involved
  41.                 availableBalance = bankDatabase.getAvailableBalance(getAccountNumber());
  42.  
  43.                 // check whether the user has enough money in the account
  44.                 if (amount <= availableBalance) {
  45.                     // check whether the cash dispenser has enough money
  46.                     if (cashDispenser.isSufficientCashAvailable(amount)) {
  47.                         // update the account involved to reflect the withdrawal
  48.                         bankDatabase.debit(getAccountNumber(), amount);
  49.  
  50.                         cashDispenser.dispenseCash(amount); // dispense cash
  51.                         cashDispensed = true; // cash was dispensed
  52.  
  53.                         // instruct user to take cash
  54.                         screen.displayMessageLine("\nYour cash has been" + " dispensed. Please take your cash now.");
  55.                     } // end if
  56.                     else // cash dispenser does not have enough cash
  57.                         screen.displayMessageLine(
  58.                                 "\nInsufficient cash available in the ATM." + "\n\nPlease choose a smaller amount.");
  59.                 } // end if
  60.                 else // not enough money available in user's account
  61.                 {
  62.                     screen.displayMessageLine(
  63.                             "\nInsufficient funds in your account." + "\n\nPlease choose a smaller amount.");
  64.                 } // end else
  65.             } // end if
  66.             else // user chose cancel menu option
  67.             {
  68.                 screen.displayMessageLine("\nCanceling transaction...");
  69.                 return; // return to main menu because user canceled
  70.             } // end else
  71.         } while (!cashDispensed);
  72.  
  73.     } // end method execute
  74.  
  75.     // display a menu of withdrawal amounts and the option to cancel;
  76.     // return the chosen amount or 0 if the user chooses to cancel
  77.     private int displayMenuOfAmounts() {
  78.         int userChoice = 0; // local variable to store return value
  79.  
  80.         Screen screen = getScreen(); // get screen reference
  81.  
  82.         // array of amounts to correspond to menu numbers
  83.         int[] amounts = { 0, 20, 40, 60, 100, 200 };
  84.  
  85.         // loop while no valid choice has been made
  86.         while (userChoice == 0) {
  87.             // display the withdrawal menu
  88.             screen.displayMessageLine("\nWithdrawal Menu:");
  89.             screen.displayMessageLine("1 - $20");
  90.             screen.displayMessageLine("2 - $40");
  91.             screen.displayMessageLine("3 - $60");
  92.             screen.displayMessageLine("4 - $100");
  93.             screen.displayMessageLine("5 - $200");
  94.             screen.displayMessageLine("6 - Cancel transaction");
  95.             screen.displayMessage("\nChoose a withdrawal amount: ");
  96.  
  97.             int input = keypad.getInput(); // get user input through keypad
  98.  
  99.             // determine how to proceed based on the input value
  100.             switch (input) {
  101.             case 1: // if the user chose a withdrawal amount
  102.             case 2: // (i.e., chose option 1, 2, 3, 4 or 5), return the
  103.             case 3: // corresponding amount from amounts array
  104.             case 4:
  105.             case 5:
  106.                 userChoice = amounts[input]; // save user's choice
  107.                 break;
  108.             case CANCELED: // the user chose to cancel
  109.                 userChoice = CANCELED; // save user's choice
  110.                 break;
  111.             default: // the user did not enter a value from 1-6
  112.                 screen.displayMessageLine("\nInvalid selection. Try again.");
  113.             } // end switch
  114.         } // end while
  115.  
  116.         return userChoice; // return withdrawal amount or CANCELED
  117.     } // end method displayMenuOfAmounts
  118. } // end class Withdrawal
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement