J.5. Class CashDispenser

Table of contents:

Class CashDispenser (Fig. J.4) represents the cash dispenser of the ATM. Line 6 declares constant INITIAL_COUNT, which indicates the number of $20 bills in the cash dispenser when the ATM starts (i.e., 500). Line 7 implements attribute billCount (modeled in Fig. 11.22), which keeps track of the number of bills remaining in the CashDispenser at any time. The constructor (lines 1013) sets billCount to the initial count. [Note: We assume that the process of adding more bills to the CashDispenser and updating the billCount occur outside the ATM system.] Class CashDispenser has two public methodsDispenseCash (lines 1621) and IsSufficientCashAvailable (lines 2431). The class trusts that a client (i.e., Withdrawal) calls method DispenseCash only after establishing that sufficient cash is available by calling method IsSufficientCashAvailable. Thus, DispenseCash simulates dispensing the requested amount of cash without checking whether sufficient cash is available.

Figure J.4. Class CashDispenser represents the ATM's cash dispenser.

 1 // CashDispenser.cs
 2 // Represents the cash dispenser of the ATM
 3 public class CashDispenser
 4 {
 5 // the default initial number of bills in the cash dispenser
 6 private const int INITIAL_COUNT = 500;
 7 private int billCount; // number of $20 bills remaining
 8
 9 // parameterless constructor initializes billCount to INITIAL_COUNT
10 public CashDispenser()
11 {
12 billCount = INITIAL_COUNT; // set billCount to INITIAL_COUNT
13 } // end constructor
14
15 // simulates dispensing the specified amount of cash
16 public void DispenseCash( decimal amount )
17 {
18 // number of $20 bills required
19 int billsRequired = ( ( int ) amount ) / 20;
20 billCount -= billsRequired;
21 } // end method DispenseCash
22
23 // indicates whether cash dispenser can dispense desired amount
24 public bool IsSufficientCashAvailable( decimal amount )
25 {
26 // number of $20 bills required
27 int billsRequired = ( ( int ) amount ) / 20;
28
29 // return whether there are enough bills available
30 return ( billCount >= billsRequired );
31 } // end method IsSufficientCashAvailable
32 } // end class CashDispenser

Method IsSufficientCashAvailable (lines 2431) has a parameter amount that specifies the amount of cash in question. Line 27 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 convert amount to an integer value and divide it by 20 to obtain the number of billsRequired. Line 30 returns TRue if the CashDispenser's billCount 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., billCount is 3), the method returns false.

Method DispenseCash (lines 1621) simulates cash dispensing. If our system were hooked up to a real hardware cash dispenser, this method would interact with the hardware device to physically dispense the cash. Our simulated version of the method simply decreases the billCount of bills remaining by the number required to dispense the specified amount (line 20). Note that it is the responsibility of the client of the class (i.e., Withdrawal) to inform the user that cash has been dispensedCashDispenser does not interact directly with Screen.

J 6 Class DepositSlot

Preface

Index

    Introduction to Computers, the Internet and Visual C#

    Introduction to the Visual C# 2005 Express Edition IDE

    Introduction to C# Applications

    Introduction to Classes and Objects

    Control Statements: Part 1

    Control Statements: Part 2

    Methods: A Deeper Look

    Arrays

    Classes and Objects: A Deeper Look

    Object-Oriented Programming: Inheritance

    Polymorphism, Interfaces & Operator Overloading

    Exception Handling

    Graphical User Interface Concepts: Part 1

    Graphical User Interface Concepts: Part 2

    Multithreading

    Strings, Characters and Regular Expressions

    Graphics and Multimedia

    Files and Streams

    Extensible Markup Language (XML)

    Database, SQL and ADO.NET

    ASP.NET 2.0, Web Forms and Web Controls

    Web Services

    Networking: Streams-Based Sockets and Datagrams

    Searching and Sorting

    Data Structures

    Generics

    Collections

    Appendix A. Operator Precedence Chart

    Appendix B. Number Systems

    Appendix C. Using the Visual Studio 2005 Debugger

    Appendix D. ASCII Character Set

    Appendix E. Unicode®

    Appendix F. Introduction to XHTML: Part 1

    Appendix G. Introduction to XHTML: Part 2

    Appendix H. HTML/XHTML Special Characters

    Appendix I. HTML/XHTML Colors

    Appendix J. ATM Case Study Code

    Appendix K. UML 2: Additional Diagram Types

    Appendix L. Simple Types

    Index



    Visual C# How to Program
    Visual C# 2005 How to Program (2nd Edition)
    ISBN: 0131525239
    EAN: 2147483647
    Year: 2004
    Pages: 600

    Similar book on Amazon

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