Java Reference
In-Depth Information
3 for Gum
4 for Cookies
9 to exit
The definition of the function showSelection is:
public static void showSelection()
{
System.out.println("*** Welcome to Shelly's "
+ "Candy Shop ***");
System.out.println("To select an item, enter ");
System.out.println("1 for Candy");
System.out.println("2 for Chips");
System.out.println("3 for Gum");
System.out.println("4 for Cookies");
System.out.println("9 to exit");
} //end showSelection
Next, we describe the method sellProduct .
Method
sellProduct
This method attempts to sell a particular product selected by the customer. The
candy machine contains four dispensers, which correspond to the four products. The
first thing this method does is check whether the dispenser holding the product is
empty. If the dispenser is empty, the method informs the customer that this product
is sold out. If the dispenser is not empty, it tells the user to deposit the necessary
amount to buy the product. For simplicity, we assume that this program does not
return the extra money deposited by the customer. Therefore, the cash register is
updated by adding the money entered by the user.
From this discussion, it follows that the method sellProduct must have access to
the dispenser holding the product (to decrement the number of items in the dispenser
by 1 and to show the cost of the item) as well as access to the cash register (to update
the cash). Therefore, this method has two parameters: one corresponding to the
dispenser and the other corresponding to the cash register.
In pseudocode, the algorithm for this method is:
1.
8
If the dispenser is not empty
a. Get the product cost.
b. Set the variable coinsRequired to the price of the product.
c. Set the variable coinsInserted to 0 .
d. While coinsRequired is greater than 0 :
i. Show and prompt the customer to enter the additional
amount.
ii. Calculate the total amount entered by the customer.
iii. Determine the amount needed.
Search WWH ::




Custom Search