Java Reference
In-Depth Information
e. Update the amount in the cash register.
f. Sell the product—that is, decrement the number of items in the
dispenser by 1 .
g. Display an appropriate message.
2. If the dispenser is empty, tell the user that this product is sold out.
The definition of the method sellProduct is:
public static void sellProduct(Dispenser product,
CashRegister cRegister)
{
int price;
//variable to hold the product price
int coinsInserted;
//variable to hold the amount entered
int coinsRequired;
//variable to show the extra amount
//needed
if (product.getCount() > 0)
//Step 1
{
price = product.getProductCost();
//Step 1a
coinsRequired = price;
//Step 1b
coinsInserted = 0;
//Step 1c
while (coinsRequired > 0)
//Step 1d
{
System.out.print("Please deposit "
+ coinsRequired
+ " cents: ");
//Step 1d.i
coinsInserted = coinsInserted
+ console.nextInt();
//Step 1d.ii
coinsRequired = price
- coinsInserted;
//Step 1d.iii
}
System.out.println();
cRegister.acceptAmount(coinsInserted);
//Step 1e
product.makeSale();
//Step 1f
System.out.println("Collect your item "
+ "at the bottom and "
+ "enjoy.\n");
//Step 1g
}
else
System.out.println("Sorry this item "
+ "is sold out.\n");
//Step 2
} //end sellProduct
 
Search WWH ::




Custom Search