J.5. Class CashDispenser

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

Категории