J.12. Class DepositClass 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.
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 ExecuteMethod 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 PromptForDepositAmountLines 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. |