Section J.5. Class CashDispenser


J.5. Class CashDispenser

Class CashDispenser (Fig. J.4) represents the cash dispenser of the ATM. Line 5 declares constant INITIAL_COUNT, which indicates the initial count of bills in the cash dispenser when the ATM starts (i.e., 500). Line 6 implements attribute billCount (modeled in Fig. 11.20), which keeps track of the number of bills remaining in the CashDispenser at any time. The constructor (lines 911) 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 1418) and IsSufficientCashAvailable (lines 2131). 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 simply simulates dispensing the requested amount without checking whether sufficient cash is available.

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

  1  ' CashDispenser.vb  2  ' Represents the cash dispenser of the ATM  3  Public Class CashDispenser  4     ' the default initial number of bills in the case dispenser  5     Private Const INITIAL_COUNT As Integer = 500  6     Private billCount As Integer ' number of $20 bills remaining  7  8     ' parameterless constructor initializes billCount to INITIAL_COUNT  9     Public Sub New() 10        billCount = INITIAL_COUNT ' set billCount to INITIAL_COUNT 11     End Sub ' New 12 13     ' simulates dispensing of specified amount of cash 14     Public Sub DispenseCash(ByVal amount As Decimal) 15        ' number of $20 bills required 16        Dim billsRequired As Integer = (Convert.ToInt32(amount) \ 20) 17        billCount -= billsRequired 18     End Sub ' DispenseCash 19 20     ' indicates whether cash dispenser can dispense desired amount 21     Public Function IsSufficientCashAvailable(ByVal amount As Decimal) _ 22        As Boolean 23        ' number of $20 bills required 24        Dim billsRequired As Integer = (Convert.ToInt32(amount) \ 20) 25 26        If (billCount >= billsRequired) Then 27           Return True ' enough bills available 28        Else 29           Return False ' not enough bills available 30        End If 31     End Function ' IsSufficientCashAvailable 32  End Class ' CashDispenser 

Method IsSufficientCashAvailable (lines 2131) has a parameter amount that specifies the amount of cash in question. Line 24 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. Lines 2630 return 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 1418) 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 cash. Our simulated version of the method simply decreases the billCount of bills remaining by the number required to dispense the specified amount (line 17). 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.




Visual BasicR 2005 for Programmers. DeitelR Developer Series
Visual Basic 2005 for Programmers (2nd Edition)
ISBN: 013225140X
EAN: 2147483647
Year: 2004
Pages: 435

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