23.4.4. Class CashDispenser

Class CashDispenser (Figs. 23.2023.21) represents the cash dispenser. Figure 23.20 contains the function prototype for a default constructor (line 9). Class CashDispenser declares two additional public member functions—dispenseCash (line 12) and isSufficientCashAvailable (line 15). The class trusts that a client (i.e., Withdrawal) calls dispenseCash only after establishing that sufficient cash is available by calling isSufficientCashAvailable. Thus, dispenseCash simply simulates dispensing the requested amount without checking whether sufficient cash is available. Line 17 declares private constant INITIAL_COUNT, which indicates the initial count of bills in the cash dispenser when the ATM starts (i.e., 500). Line 18 implements attribute count (modeled in Fig. 23.11), which keeps track of the number of bills remaining in the CashDispenser at any time.


 1   // CashDispenser.h
 2   // CashDispenser class definition. Represents the ATM's cash dispenser.
 3   #ifndef CASH_DISPENSER_H
 4   #define CASH_DISPENSER_H
 5
 6   class CashDispenser
 7   {
 8   public:
 9      CashDispenser(); // constructor initializes bill count to 500
10
11      // simulates dispensing of specified amount of cash
12      void dispenseCash( int );
13
14      // indicates whether cash dispenser can dispense desired amount
15      bool isSufficientCashAvailable( int ) const;
16   private:
17      static const int INITIAL_COUNT = 500;
18      int count; // number of $20 bills remaining
19   }; // end class CashDispenser
20
21   #endif // CASH_DISPENSER_H


Fig. 23.20. CashDispenser class definition.

CashDispenser Class Member-Function Definitions

Figure 23.21 contains the definitions of class CashDispenser’s member functions. The constructor (lines 6–9) sets count to the initial count (i.e., 500). Member function dispenseCash (lines 13–17) simulates cash dispensing. If our system were hooked up to a real hardware cash dispenser, this member function would interact with the hardware device to physically dispense cash. Our simulated version of the member function simply decreases the count of bills remaining by the number required to dispense the specified amount (line 16). Line 15 calculates the number of $20 bills required to dispense the specified amount. The ATM allows the user to choose only withdrawal amounts that are multiples of $20, so we divide amount by 20 to obtain the number of billsRequired. Also, it’s the responsibility of the class’s client (i.e., Withdrawal) to inform the user that cash has been dispensed—CashDispenser cannot interact directly with Screen.


 1   // CashDispenser.cpp
 2   // Member-function definitions for class CashDispenser.
 3   #include "CashDispenser.h" // CashDispenser class definition
 4
 5   // CashDispenser default constructor initializes count to default
 6   CashDispenser::CashDispenser()
 7   {
 8      count = INITIAL_COUNT; // set count attribute to default
 9   } // end CashDispenser default constructor
10
11   // simulates dispensing of specified amount of cash; assumes enough cash
12   // is available (previous call to isSufficientCashAvailable returned true)
13   void CashDispenser::dispenseCash( int amount )
14   {
15      int billsRequired = amount / 20; // number of $20 bills required
16      count -= billsRequired; // update the count of bills
17   } // end function dispenseCash
18
19   // indicates whether cash dispenser can dispense desired amount
20   bool CashDispenser::isSufficientCashAvailable( int amount ) const
21   {
22      int billsRequired = amount / 20; // number of $20 bills required
23
24      if ( count >= billsRequired )
25         return true; // enough bills are available
26      else
27         return false; // not enough bills are available
28   } // end function isSufficientCashAvailable


Fig. 23.21. CashDispenser class member-function definitions.

Member function isSufficientCashAvailable (lines 20–28) has a parameter amount that specifies the amount of cash in question. Lines 24–27 return true if the CashDispenser’s count is greater than or equal to billsRequired (i.e., enough bills are available) and false otherwise (i.e., not enough bills). For example, if a user wishes to withdraw $80 (i.e., billsRequired is 4), but only three bills remain (i.e., count is 3), the member function returns false.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.161.180