Java Reference
In-Depth Information
cashRegister.acceptAmount(coinsInserted);
product.makeSale();
JOptionPane.showMessageDialog( null ,"Please pick up your "
+ productName + " and enjoy",
"Thank you, Come again!",
JOptionPane.PLAIN_MESSAGE);
}
else
//dispenser is empty
JOptionPane.showMessageDialog( null ,"Sorry "
+ productName
+ " is sold out\n" +
"Make another selection",
"Thank you, Come again!",
JOptionPane.PLAIN_MESSAGE);
} //end sellProduct
We have described the method sellProduct and the other necessary components,
so next we will write the Java application program for the candy machine.
The algorithm is as follows:
1. Create the cash register—that is, declare a reference variable of type
CashRegister and instantiate the object.
2. Create four dispensers—that is, declare four reference variables of
type Dispenser and instantiate the appropriate Dispenser objects.
For example, the statement:
Dispenser candy = new Dispenser(100, 50);
declares candy to be a reference variable of the Dispenser type and instantiates the
object candy to hold the candies. The number of items in the object candy is 100 ,
and the cost of a candy is 50 cents.
3. Create the other objects, such as labels and buttons, as previously
described.
4. Display the GUI showing the candy machine, as described at the
beginning of this programming example.
5. Get and process the selection.
The complete programming listing is available with the Additional Student Files at
www.cengagebrain.com.
 
Search WWH ::




Custom Search