Section J.11. Class Withdrawal


J.11. Class Withdrawal

Class 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.

  1  ' Withdrawal.vb  2  ' Class Withdrawal represents an ATM withdrawal transaction.  3  Public Class Withdrawal  4     Inherits Transaction  5  6     Private amount As Decimal ' amount to withdraw  7     Private keypadHandle As Keypad ' reference to Keypad  8     Private dispenserHandle As CashDispenser ' reference to cash dispenser  9 10     ' constant that corresponds to menu option to cancel 11     Private Const CANCELED As Integer = 6 12 13     ' Withdrawal constructor 14     Public Sub New(ByVal userAccountNumber As Integer, _ 15        ByVal atmScreen As Screen, ByVal atmBankDatabase As BankDatabase, _ 16        ByVal atmKeypad As Keypad, ByVal atmCashDispenser As CashDispenser) 17        ' initialize base class variables 18        MyBase.New(userAccountNumber, atmScreen, atmBankDatabase) 19 20        ' initialize references to keypad and cash dispenser 21        keypadHandle = atmKeypad 22        dispenserHandle = atmCashDispenser 23     End Sub ' New 24 25     ' perform transaction 26     Public Overrides Sub Execute() 27        Dim cashDispensed As Boolean = False ' cash was not dispensed yet 28 29        ' transaction was not canceled yet 30        Dim transactionCanceled As Boolean = False 31 32        ' loop until cash is dispensed or the user cancels 33        Do 34           ' obtain the chosen withdrawal amount from the user 35           Dim selection As Integer = DisplayMenuOfAmounts() 36 37           ' check whether user chose a withdrawal amount or canceled 38           If (selection <> CANCELED) Then 39              amount = selection ' set amount to the selected dollar amount 40 41              ' get available balance of account involved 42              Dim availableBalance As Decimal = _ 43                 BankDatabaseReference.GetAvailableBalance(AccountNumber) 44 45              ' check whether the user has enough money in the account 46              If (amount <= availableBalance) Then 47                 ' check whether the cash dispenser has enough money 48                 If (dispenserHandle.IsSufficientCashAvailable(amount)) Then 49                    ' update the account involved to reflect withdrawal 50                    BankDatabaseReference.Debit(AccountNumber, amount) 51 52                    dispenserHandle.DispenseCash(amount) ' dispense cash 53                    cashDispensed = True ' cash was dispensed 54 55                    ' instruct user to take cash 56                    ScreenReference.DisplayMessageLine(vbCrLf & _ 57                       "Please take your cash from the cash dispenser.") 58                 Else ' cash dispenser does not have enough cash 59                    ScreenReference.DisplayMessageLine(vbCrLf & _ 60                      "Insufficient cash available in the ATM." & _ 61                      vbCrLf & vbCrLf & "Please choose a smaller amount.") 62                End If 63             Else ' not enough money available in user's account 64                ScreenReference.DisplayMessageLine(vbCrLf & _ 65                   "Insufficient cash available in your account." & _ 66                   vbCrLf & vbCrLf & "Please choose a smaller amount.") 67             End If 68          Else 69             ScreenReference.DisplayMessageLine( _ 70                vbCrLf & "Canceling transaction...") 71             transactionCanceled = True ' user canceled the transaction 72          End If 73       Loop While ((Not cashDispensed) And (Not transactionCanceled)) 74    End Sub ' Execute 75 76    ' display a menu of withdrawal amounts and the option to cancel; 77    ' return the chosen amount or 0 if the user chooses to cancel 78    Private Function DisplayMenuOfAmounts() As Integer 79       Dim userChoice As Integer = 0 ' variable to store return value 80 81       ' array of amounts to correspond to menu numbers 82       Dim amounts As Integer () = New Integer () { _ 83          0, 20, 40, 60, 100, 200} 84 85       ' loop while no valid choice has been made 86       While (userChoice = 0) 87          ' display the menu 88          ScreenReference.DisplayMessageLine( _ 89             vbCrLf & "Withdrawal options:" ) 90          ScreenReference.DisplayMessageLine("1 - $20") 91          ScreenReference.DisplayMessageLine("2 - $40") 92          ScreenReference.DisplayMessageLine("3 - $60") 93          ScreenReference.DisplayMessageLine("4 - $100") 94          ScreenReference.DisplayMessageLine("5 - $200") 95          ScreenReference.DisplayMessageLine("6 - Cancel transaction") 96          ScreenReference.DisplayMessage( _ 97             vbCrLf & "Choose a withdrawal option (1-6): ") 98 99          ' get user input through keypad 100         Dim input As Integer = keypadHandle.GetInput() 101 102         ' determine how to proceed based on the input value 103         Select Case (input) 104            ' if the user chose a withdrawal amount (i.e., option 105            ' 1, 2, 3, 4, or 5), return the corresponding amount 106            ' from the amounts array 107            Case 1 To 5 108               userChoice = amounts(input) ' save user's choice 109            Case CANCELED ' the user chose to cancel 110               userChoice = CANCELED ' save user's choice 111            Case Else 112               ScreenReference.DisplayMessageLine( _ 113                  vbCrLf & "Invalid selection. Try again.") 114          End Select 115       End While 116 117       Return userChoice 118    End Function ' DisplayMenuOfAmounts 119  End Class ' Withdrawal 

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 Execute

Class 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 DoLoop 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 DisplayMenuOfAmounts

Method 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 DoLoop 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 DoLoop 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 DoLoop While. No additional statements appear below the loop, so the method returns control to class ATM.



Visual BasicR 2005 for Programmers. DeitelR Developer Series
Visual Basic 2005 for Programmers (2nd Edition)
ISBN: 013225140X
EAN: 2147483647
Year: 2004
Pages: 435

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net