J.11. Class WithdrawalClass Withdrawal (Fig. J.10) extends transaction and represents an ATM withdrawal transaction. This class expands upon the "skeleton" code for this class developed in Fig. 11.22. Recall from the class diagram of Fig. 11.19 that class Withdrawal has one attribute, amount, which line 6 implements as a Decimal instance variable. Fig. 11.19 models associations between class Withdrawal and classes Keypad and CashDispenser, for which lines 78 implement reference attributes keypadHandle and dispenserHandle, respectively. Line 11 declares a constant corresponding to the cancel menu option. We will soon discuss how the class uses this constant. Figure J.10. Class Withdrawal represents an ATM withdrawal transaction.
Class Withdrawal's constructor (lines 1423) has five parameters. It uses MyBase.New to pass parameters userAccountNumber, atmScreen and atmBankDatabase to base class transaction's constructor to set the attributes that Withdrawal inherits from TRansaction. The constructor also takes references atmKeypad and atmCashDispenser as parameters and assigns them to variables keypadHandle and dispenserHandle. Overriding MustOverride Method ExecuteClass Withdrawal overrides transaction's MustOverride method Execute with a concrete implementation (lines 2674) that performs the steps involved in a withdrawal. Line 27 declares and initializes a local Boolean variable cashDispensed. This variable indicates whether cash has been dispensed (i.e., whether the transaction has completed successfully) and is initially False. Line 30 declares and initializes to False a Boolean variable TRansactionCanceled to indicate that the transaction has not yet been canceled by the user. Lines 3373 contain a Do…Loop While statement that executes its body until cash is dispensed (i.e., until cashDispensed becomes true) or until the user chooses to cancel (i.e., until transactionCanceled becomes true). We use this loop to continuously return the user to the start of the transaction if an error occurs (i.e., the requested withdrawal amount is greater than the user's available balance or greater than the amount of cash in the cash dispenser). Line 35 displays a menu of withdrawal amounts and obtains a user selection by calling Private utility method DisplayMenuOfAmounts (declared in lines 78118). This method displays the menu of amounts and returns either an Integer withdrawal amount or an Integer constant CANCELED to indicate that the user has chosen to cancel the transaction. Displaying Options With Private Utility Method DisplayMenuOfAmountsMethod DisplayMenuOfAmounts (lines 81121) first declares local variable userChoice (initially 0) to store the value that the method will return (line 82). Lines 8283 declare an integer array of withdrawal amounts that correspond to the amounts displayed in the withdrawal menu. We ignore the first element in the array (index 0) because the menu has no option 0. The While statement at lines 86115 repeats until userChoice takes on a value other than 0. We will see shortly that this occurs when the user makes a valid selection from the menu. Lines 8897 display the withdrawal menu on the screen and prompt the user to enter a choice. Line 100 obtains integer input tHRough the keypad. The Select Case statement at lines 103114 determines how to proceed based on the user's input. If the user selects a number between 1 and 5, line 108 sets userChoice to the value of the element in the amounts array at index input. For example, if the user enters 3 to withdraw $60, line 108 sets userChoice to the value of amounts(3) (i.e., 60). Variable userChoice no longer equals 0, so the While at lines 86115 terminates and line 117 returns userChoice. If the user selects the cancel menu option, line 110 executes, setting userChoice to CANCELED and causing the method to return this value. If the user does not enter a valid menu selection, lines 112113 display an error message and the user is returned to the withdrawal menu. The If statement at line 38 in method Execute determines whether the user has selected a withdrawal amount or chosen to cancel. If the user cancels, lines 6970 display an appropriate message to the user before returning control to the calling method (i.e., ATM method PerformTransactions). If the user has chosen a withdrawal amount, lines 39 assigns local variable selection to instance variable amount. Lines 4243 retrieve the available balance of the current user's Account and store it in a local Decimal variable availableBalance. Next, the If statement at line 46 determines whether the selected amount is less than or equal to the user's available balance. If it is not, lines 6466 display an error message. Control then continues to the end of the Do…Loop While, and the loop repeats because both cashDispensed and transactionCanceled are still False. If the user's balance is high enough, the If statement at line 48 determines whether the cash dispenser has enough money to satisfy the withdrawal request by invoking the cash dispenser's IsSufficientCashAvailable method. If this method returns False, lines 5961 display an error message and the Do…Loop While repeats. If sufficient cash is available, then the requirements for the withdrawal are satisfied, and line 50 debits the user's account in the database by amount. Lines 5253 then instruct the cash dispenser to dispense the cash to the user and set cashDispensed to true. Finally, lines 5657 display a message to the user to take the dispensed cash. Because cashDispensed is now TRue, control continues after the Do…Loop While. No additional statements appear below the loop, so the method returns control to class ATM. |