Java Reference
In-Depth Information
PROGRAMMING EXAMPLE: Candy Machine
A new candy machine is bought for the cafeteria and a program is needed to make
the machine function properly. The machine sells candies, chips, gum, and cookies.
In this programming example, we write a program to create a Java application
program for the candy machine so that it can be put into operation.
We implement this program in two ways. First, we show how to design a non-GUI
application program. Then, we show how to design an application program that will
create a GUI to make the candy machine operational.
The non-GUI application program should do the following:
1.
Show the customer the different products
sold by the candy
machine.
2. Let the customer make the selection.
3. Show the customer the cost of the item selected.
4. Accept the money from the customer.
5. Release the item.
8
The item selection and the cost of the item
Input:
Output: The selected item
In the next section, we design the candy machine's basic components, which are
required by either type of application program—GUI or non-GUI. The difference
between the two types is evident when we write the main program to put the candy
machine into operation.
A candy machine has three main components: a built-in cash register, several
dispensers to hold and release the products, and the candy machine itself. Therefore,
we need to define a class to implement the cash register, a class to implement the
dispenser, and a class to implement the candy machine. First, we describe the classes
to implement the cash register and dispenser, and then we use these classes to describe
the candy machine.
PROBLEM
ANALYSIS
AND
ALGORITHM
DESIGN
Let's first discuss the properties of a cash register. The register has some cash on hand,
it accepts the amount from the customer, and if the amount entered is more than the
cost of the item, then—if possible—it returns the change. For simplicity, we assume
that the user enters the exact amount for the product. The cash register should also be
able to show the candy machine's owner the amount of money in the register at any
given time. Let's call the class implementing the cash register CashRegister .
Cash
Register
Search WWH ::




Custom Search