Java Reference
In-Depth Information
//Constructor with parameters to set the cash in
//the register to a specific amount
//Postcondition: cashOnHand = cashIn
public
CashRegister(
int
cashIn)
{
if
(cashIn >= 0)
cashOnHand = cashIn;
else
cashOnHand = 500;
}
//Method to show the current amount in the cash register
//Postcondition: The value of the instance variable
// cashOnHand is returned.
public int
currentBalance()
{
return
cashOnHand;
}
//Method to receive the amount deposited by
//the customer and update the amount in the register
//Postcondition: cashOnHand = cashOnHand + amountIn
public void
acceptAmount(
int
amountIn)
{
cashOnHand = cashOnHand + amountIn;
}
}
Dispenser
The dispenser releases the selected item if it is not empty. It should show the number
of items in the dispenser and the cost of the item. Let's call the class implementing a
dispenser
Dispenser
. The members necessary to implement the
class
Dispenser
are listed next and shown in Figure 8-20.
Instance
Variables
private int
numberOfItems;
//variable to store the number of
//items in the dispenser
private int
cost;
//variable to store the cost of an item
Constructors
and Methods
public
Dispenser()
//Default constructor to set the cost and number of
//items to the default values
//Postcondition: numberOfItems = 50; cost = 50;
public
Dispenser(
int
setNoOfItems,
int
setCost)
//Constructor with parameters to set the cost and number
//of items in the dispenser specified by the user
//Postcondition: numberOfItems = setNoOfItems;
//
cost = setCost;
Search WWH ::
Custom Search