G.12. Class Deposit

Class Deposit (Figs. G.21G.22) derives from TRansaction and represents a deposit ATM transaction. Figure G.21 contains the Deposit class definition. Like derived classes BalanceInquiry and Withdrawal, Deposit declares a constructor (line 13) and member function execute (line 14)we discuss these momentarily. Recall from the class diagram of Fig. 13.29 that class Deposit has one attribute amount, which line 16 implements as an int data member. Lines 1718 create reference data members keypad and depositSlot that implement the associations between class Deposit and classes Keypad and DepositSlot modeled in Fig. 13.28. Line 19 contains the function prototype for a private utility function promptForDepositAmount that we will discuss shortly.

Figure G.21. Deposit class definition.

1 // Deposit.h 2 // Deposit class definition. Represents a deposit transaction. 3 #ifndef DEPOSIT_H 4 #define DEPOSIT_H 5 6 #include "Transaction.h" // Transaction class definition 7 class Keypad; // forward declaration of class Keypad 8 class DepositSlot; // forward declaration of class DepositSlot 9 10 class Deposit : public Transaction 11 { 12 public: 13 Deposit( int, Screen &, BankDatabase &, Keypad &, DepositSlot & ); 14 virtual void execute(); // perform the transaction 15 private: 16 double amount; // amount to deposit 17 Keypad &keypad; // reference to ATM's keypad 18 DepositSlot &depositSlot; // reference to ATM's deposit slot 19 double promptForDepositAmount() const; // get deposit amount from user 20 }; // end class Deposit 21 22 #endif // DEPOSIT_H

Figure G.22. Deposit class member-function definitions.

(This item is displayed on pages 1314 - 1315 in the print version)

1 // Deposit.cpp 2 // Member-function definitions for class Deposit. 3 #include "Deposit.h" // Deposit class definition 4 #include "Screen.h" // Screen class definition 5 #include "BankDatabase.h" // BankDatabase class definition 6 #include "Keypad.h" // Keypad class definition 7 #include "DepositSlot.h" // DepositSlot class definition 8 9 const static int CANCELED = 0; // constant representing cancel option 10 11 // Deposit constructor initializes class's data members 12 Deposit::Deposit( int userAccountNumber, Screen &atmScreen, 13 BankDatabase &atmBankDatabase, Keypad &atmKeypad, 14 DepositSlot &atmDepositSlot ) 15 : Transaction( userAccountNumber, atmScreen, atmBankDatabase ), 16 keypad( atmKeypad ), depositSlot( atmDepositSlot ) 17 { 18 // empty body 19 } // end Deposit constructor 20 21 // performs transaction; overrides Transaction's pure virtual function 22 void Deposit::execute() 23 { 24 BankDatabase &bankDatabase = getBankDatabase(); // get reference 25 Screen &screen = getScreen(); // get reference 26 27 amount = promptForDepositAmount(); // get deposit amount from user 28 29 // check whether user entered a deposit amount or canceled 30 if ( amount != CANCELED ) 31 { 32 // request deposit envelope containing specified amount 33 screen.displayMessage( 34 " Please insert a deposit envelope containing " ); 35 screen.displayDollarAmount( amount ); 36 screen.displayMessageLine( " in the deposit slot." ); 37 38 // receive deposit envelope 39 bool envelopeReceived = depositSlot.isEnvelopeReceived(); 40 41 // check whether deposit envelope was received 42 if ( envelopeReceived ) 43 { 44 screen.displayMessageLine( " Your envelope has been received." 45 " NOTE: The money just will not be available until we" 46 " verify the amount of any enclosed cash, and any enclosed " 47 "checks clear." ); 48 49 // credit account to reflect the deposit 50 bankDatabase.credit( getAccountNumber(), amount ); 51 } // end if 52 else // deposit envelope not received 53 { 54 screen.displayMessageLine( " You did not insert an " 55 "envelope, so the ATM has canceled your transaction." ); 56 } // end else 57 } // end if 58 else // user canceled instead of entering amount 59 { 60 screen.displayMessageLine( " Canceling transaction..." ); 61 } // end else 62 } // end function execute 63 64 // prompt user to enter a deposit amount in cents 65 double Deposit::promptForDepositAmount() const 66 { 67 Screen &screen = getScreen(); // get reference to screen 68 69 // display the prompt and receive input 70 screen.displayMessage( " Please enter a deposit amount in " 71 "CENTS (or 0 to cancel): " ); 72 int input = keypad.getInput(); // receive input of deposit amount 73 74 // check whether the user canceled or entered a valid amount 75 if ( input == CANCELED ) 76 return CANCELED; 77 else 78 { 79 return static_cast< double >( input ) / 100; // return dollar amount 80 } // end else 81 } // end function promptForDepositAmount


Deposit Class Member-Function Definitions

Figure G.22 presents the implementation file for class Deposit. Line 3 #includes the Deposit class definition, and lines 47 #include the class definitions of the other classes used in Deposit's member functions. Line 9 declares a constant CANCELED that corresponds to the value a user enters to cancel a deposit. We will soon discuss how the class uses this constant.

Like class Withdrawal, class Deposit contains a constructor (lines 1219) that passes three parameters to base class transaction's constructor using a base-class initializer (line 15). The constructor also has parameters atmKeypad and atmDepositSlot, which it assigns to its corresponding data members (line 16).

Member function execute (lines 2262) overrides pure virtual function execute in base class transaction with a concrete implementation that performs the steps required in a deposit transaction. Lines 2425 get references to the database and the screen. Line 27 prompts the user to enter a deposit amount by invoking private utility function promptForDepositAmount (defined in lines 6581) and sets data member amount to the value returned. Member function 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 double value representing the dollar amount to be deposited.



Line 67 in member function promptForDepositAmount gets a reference to the ATM's screen. Lines 7071 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 72 receives the user's input from the keypad. The if statement at lines 7580 determines whether the user has entered a real deposit amount or chosen to cancel. If the user chooses to cancel, line 76 returns the constant CANCELED. Otherwise, line 79 returns the deposit amount after converting from the number of cents to a dollar amount by casting input to a double, then dividing by 100. For example, if the user enters 125 as the number of cents, line 79 returns 125.0 divided by 100, or 1.25125 cents is $1.25.

The if statement at lines 3061 in member function execute determines whether the user has chosen to cancel the transaction instead of entering a deposit amount. If the user cancels, line 60 displays an appropriate message, and the member function returns. If the user enters a deposit amount, lines 3336 instruct the user to insert a deposit envelope with the correct amount. Recall that Screen member function displayDollarAmount outputs a double formatted as a dollar amount.

Line 39 sets a local bool variable to the value returned by depositSlot's isEnvelopeReceived member function, indicating whether a deposit envelope has been received. Recall that we coded member function isEnvelopeReceived (lines 710 of Fig. G.10) 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 member function 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 isEnvelopeReceived that could return false. Lines 4450 execute if the deposit slot receives an envelope. Lines 4447 display an appropriate message to the user. Line 50 then credits the deposit amount to the user's account in the database. Lines 5455 will 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 member function then returns without modifying the user's account.

Категории