Java Reference
In-Depth Information
//Method to show the cost of the item
//Postcondition: The value of the instance
// variable cost is returned.
public int getProductCost()
{
return cost;
}
//Method to reduce the number of items by 1
//Postcondition: numberOfItems = numberOfItems - 1
public void makeSale()
{
numberOfItems--;
}
}
Main Program When the program executes, it must do the following:
1. Show the different products sold by the candy machine.
2. Show how to select a particular product.
3. Show how to terminate the program.
Furthermore, these instructions must be displayed after processing each selection
(except when exiting the program), so that the user need not remember what to
do if he or she wants to buy additional items. Once the user makes the appropriate
selection, the candy machine must act accordingly. If the user opts to buy an available
product, the candy machine should show the cost of the product and ask the user to
deposit the money. If the money deposited is at least the cost of the item, the candy
machine should sell the item and display an appropriate message.
This discussion translates into the following algorithm:
1. Show the selection to the customer.
2. Get the selection.
3. If the selection is valid and the dispenser corresponding to the
selection is not empty, sell the product.
We divide this program into three functions— showSelection , sellProduct , and
main .
This method displays the necessary information to help the user select and buy a
product. Essentially, it contains the following output statements (we assume that the
candy machine sells four types of products):
Method
showSelection
*** Welcome to Shelly's Candy Shop ***
To select an item, enter
1 for Candy
2 for Chips
 
Search WWH ::




Custom Search