Java Reference
In-Depth Information
Next, we discuss the definitions of the methods to implement the operations of the
class
Dispenser
.
The method
getCount
returns the number of items of a particular product. Because
the number of items currently in the dispenser is stored in the instance variable
numberOfItems
, the method
getCount
returns the value of the instance variable
numberOfItems
. The definition of this method is:
public int
getCount()
{
return
numberOfItems;
}
The method
getProductCost
returns the cost of a product. Because the cost of a
product is stored in the instance variable
cost
, it returns the value of the instance
variable
cost
. The definition of this method is:
public int
getProductCost()
{
return
cost;
}
When a product is sold, the number of items in that dispenser is reduced by
1
.
Therefore, the method
makeSale
reduces the number of items in the dispenser by
1
.
That is, it decrements the value of the instance variable
numberOfItems
by
1
. The
definition of this method is:
public void
makeSale()
{
numberOfItems--;
}
The definition of the constructor checks for valid values of the parameters. If these
values are less than
0
, the default values are assigned to the instance variables. The
definition of the constructor is:
//constructor with parameters
public
Dispenser(
int
setNoOfItems,
int
setCost)
{
if
(setNoOfItems >= 0)
numberOfItems = setNoOfItems;
else
numberOfItems = 50;
if
(setCost >= 0)
cost = setCost;
else
cost = 50;
}
Search WWH ::
Custom Search