Section J.12. Class Deposit


J.12. Class Deposit

Class Deposit (Fig. J.11) inherits from transaction and represents an ATM deposit transaction. Recall from the class diagram of Fig. 11.20 that class Deposit has one attribute, amount, which line 6 implements as a Decimal instance variable. Lines 78 create reference attributes keypadHandle and depositSlotHandle that implement the associations between class Deposit and classes Keypad and DepositSlot modeled in Fig. 11.19. Line 11 declares a constant CANCELED that corresponds to the value a user enters to cancel. We will soon discuss how the class uses this constant.

Figure J.11. Class Deposit represents an ATM deposit transaction.

  1  ' Deposit.vb  2  ' Represents a deposit ATM transaction.  3  Public Class Deposit  4     Inherits Transaction  5  6     Private amount As Decimal ' amount to deposit  7     Private keypadHandle As Keypad ' reference to Keypad  8     Private depositSlotHandle As DepositSlot ' reference to deposit slot  9 10     ' constant representing cancel option 11     Private Const CANCELED As Integer = 0 12 13     ' Deposit constructor initializes class's instance variables 14     Public Sub New(ByVal userAccountNumber As Integer, _ 15        ByVal atmScreen As Screen, ByVal atmBankDatabase As BankDatabase, _ 16        ByVal atmKeypad As Keypad, ByVal atmDepositSlot As DepositSlot) 17        ' initialize base class variables 18        MyBase.New(userAccountNumber, atmScreen, atmBankDatabase) 19 20        ' initialize references to keypad and deposit slot 21        keypadHandle = atmKeypad 22        depositSlotHandle = atmDepositSlot 23     End Sub ' New 24 25     ' perform transaction; overrides Transaction's MustOverride method 26     Public Overrides Sub Execute() 27        amount = PromptForDepositAmount() ' get deposit amount from user 28 29        ' check whether user entered a deposit amount or canceled 30        If (amount <> CANCELED) Then 31           ' request deposit envelope containing specified amount 32           ScreenReference.DisplayMessage(vbCrLf & _ 33              "Please insert a deposit envelope containing ") 34           ScreenReference.DisplayDollarAmount(amount) 35           ScreenReference.DisplayMessageLine(" in the deposit slot.") 36 37           ' retrieve deposit envelope 38           Dim envelopeReceived As Boolean = _ 39              depositSlotHandle.IsDepositEnvelopeReceived() 40 41           ' check whether deposit envelope was received 42           If envelopeReceived Then 43              ScreenReference.DisplayMessageLine(vbCrLf & _ 44                 "Your envelope has been received." & vbCrLf & _ 45                 "The money just deposited will not be available " & _ 46                 "until we" & vbCrLf & "verify the amount of any " & _ 47                 "enclosed cash, and any enclosed checks clear.") 48 49              ' credit account to reflect the deposit 50              BankDatabaseReference.Credit(AccountNumber, amount) 51           Else 52              ScreenReference.DisplayMessageLine(vbCrLf & _ 53                 "You did not insert an envelope, so the ATM has " & _ 54                 "canceled your transaction.") 55           End If 56        Else 57           ScreenReference.DisplayMessageLine( _ 58              vbCrLf & "Canceling transaction...") 59        End If 60     End Sub ' Execute 61 62     ' prompt user to enter a deposit amount to credit 63     Private Function PromptForDepositAmount() As Decimal 64        ' display the prompt and receive input 65        ScreenReference.DisplayMessage(vbCrLf & _ 66           "Please input a deposit amount in CENTS (or 0 to cancel): ") 67        Dim input As Integer = Convert.ToInt32(keypadHandle.GetInput()) 68 69        ' check whether the user canceled or entered a valid amount 70        If (input = CANCELED) Then 71           Return CANCELED 72        Else 73           Return Convert.ToDecimal(input / 100) 74        End If 75     End Function ' PromptForDepositAmount 76  End Class ' Deposit 

Like class Withdrawal, class Deposit contains a constructor (lines 1423) that passes three parameters to base class transaction's constructor using MyBase.New. The constructor also has parameters atmKeypad and atmDepositSlot, which it assigns to corresponding reference attributes (lines 2122).

Overriding MustOverride Method Execute

Method Execute (lines 2660) overrides MustOverride method Execute in base class transaction with a concrete implementation that performs the steps required in a deposit transaction. Line 27 prompts the user to enter a deposit amount by invoking Private utility method PromptForDepositAmount (declared in lines 6375) and sets attribute amount to the value returned. Method PromptForDepositAmount asks the user to enter a deposit amount as an integer number of cents (because the ATM's keypad does not contain a decimal point; this is consistent with many real ATMs) and returns the Decimal value representing the dollar amount to be deposited.

Getting Deposit Amount With Private Utility Method PromptForDepositAmount

Lines 6566 in method PromptForDepositAmount display a message on the screen asking the user to input a deposit amount as a number of cents or "0" to cancel the transaction. Line 67 receives the user's input from the keypad. The If statement at lines 7074 determines whether the user has entered a real deposit amount or chosen to cancel. If the user chooses to cancel, line 71 returns the constant CANCELED. Otherwise, line 73 returns the deposit amount after converting from the number of cents to a dollar amount by dividing input by 100, then converting the result to a Decimal. For example, if the user enters 125 as the number of cents, line 73 returns 125 divided by 100, or 1.25125 cents is $1.25.

Lines 3059 in method Execute determine whether the user chose to cancel the transaction rather than enter a deposit amount. If the user cancels, lines 5758 display an appropriate message and the method returns. If the user enters a deposit amount, lines 3235 instruct the user to insert a deposit envelope with the correct amount. Recall that Screen method DisplayDollarAmount outputs a Decimal value formatted as a dollar amount.

Lines 3839 sets a local Boolean variable to the value returned by the deposit slot's IsDepositEnvelopeReceived method, indicating whether a deposit envelope has been received. Recall that we coded method IsDepositEnvelopeReceived (lines 68 of Fig. J.5) to always return true, because we are simulating the functionality of the deposit slot and assume that the user always inserts an envelope. However, we code method Execute of class Deposit to test for the possibility that the user does not insert an envelopegood software engineering demands that programs account for all possible return values. Thus, class Deposit is prepared for future versions of IsDepositEnvelopeReceived that could return False. Lines 4350 execute if the deposit slot receives an envelope. Lines 4347 display an appropriate message to the user. Line 50 credits the user's account in the database with the deposit amount. Lines 5254 execute if the deposit slot does not receive a deposit envelope. In this case, we display a message to the user stating that the ATM has canceled the transaction. The method then returns without modifying the user's account.



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