G.5. Class CashDispenser

Class CashDispenser (Figs. G.7G.8) represents the cash dispenser of the ATM. The class definition (Fig. G.7) contains the function prototype for a default constructor (line 9). Class CashDispenser declares two additional public member functionsdispenseCash (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. 13.29), which keeps track of the number of bills remaining in the CashDispenser at any time.

Figure G.7. CashDispenser class definition.

(This item is displayed on page 1296 in the print version)

 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 const static int INITIAL_COUNT = 500;
18 int count; // number of $20 bills remaining
19 }; // end class CashDispenser
20
21 #endif // CASH_DISPENSER_H

Figure G.8. CashDispenser class member-function definitions.

(This item is displayed on page 1296 in the print version)

 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

CashDispenser Class Member-Function Definitions

Figure G.8 contains the definitions of class CashDispenser's member functions. The constructor (lines 69) sets count to the initial count (i.e., 500). Member function dispenseCash (lines 1317) 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). Note that 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 note that it is the responsibility of the client of the class (i.e., Withdrawal) to inform the user that cash has been dispensedCashDispenser cannot interact directly with Screen.


Member function isSufficientCashAvailable (lines 2028) has a parameter amount that specifies the amount of cash in question. Lines 2427 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.

Introduction to Computers, the Internet and World Wide Web

Introduction to C++ Programming

Introduction to Classes and Objects

Control Statements: Part 1

Control Statements: Part 2

Functions and an Introduction to Recursion

Arrays and Vectors

Pointers and Pointer-Based Strings

Classes: A Deeper Look, Part 1

Classes: A Deeper Look, Part 2

Operator Overloading; String and Array Objects

Object-Oriented Programming: Inheritance

Object-Oriented Programming: Polymorphism

Templates

Stream Input/Output

Exception Handling

File Processing

Class string and String Stream Processing

Web Programming

Searching and Sorting

Data Structures

Bits, Characters, C-Strings and structs

Standard Template Library (STL)

Other Topics

Appendix A. Operator Precedence and Associativity Chart

Appendix B. ASCII Character Set

Appendix C. Fundamental Types

Appendix D. Number Systems

Appendix E. C Legacy Code Topics

Appendix F. Preprocessor

Appendix G. ATM Case Study Code

Appendix H. UML 2: Additional Diagram Types

Appendix I. C++ Internet and Web Resources

Appendix J. Introduction to XHTML

Appendix K. XHTML Special Characters

Appendix L. Using the Visual Studio .NET Debugger

Appendix M. Using the GNU C++ Debugger

Bibliography



C++ How to Program
C++ How to Program (5th Edition)
ISBN: 0131857576
EAN: 2147483647
Year: 2004
Pages: 627

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