Java Reference
In-Depth Information
The algorithm for the method
main
follows:
Method
main
1. Create the cash register—that is, create and initialize a
CashRegister
object.
2. Create four dispensers—that is, create and initialize four objects of
type
Dispenser
. For example, the statement:
Dispenser candy =
new
Dispenser(100, 50);
creates a dispenser object,
candy
, to hold the candies. The number
of items in the dispenser is
100
, and the cost of an item is
50
cents.
3. Declare additional variables as necessary.
4. Show the selection; call the method
showSelection
.
5. Get the selection.
6. While not done (a selection of
9
exits the program):
a. Sell the product; call the method
sellProduct
.
b. Show the selection; call the method
showSelection
.
c. Get the selection.
The definition of the method
main
follows:
8
public static void
main(String[] args)
{
CashRegister cashRegister =
new
CashRegister();
//Step 1
Dispenser candy =
new
Dispenser(100, 50);
//Step 2
Dispenser chips =
new
Dispenser(100, 65);
//Step 2
Dispenser gum =
new
Dispenser(75, 45);
//Step 2
Dispenser cookies =
new
Dispenser(100, 85);
//Step 2
int
choice;
//variable to hold the selection
//Step 3
showSelection();
//Step 4
choice = console.nextInt();
//Step 5
while
(choice != 9)
//Step 6
{
switch
(choice)
//Step 6a
{
case
1:
sellProduct(candy, cashRegister);
break
;
case
2:
sellProduct(chips, cashRegister);
break
;
Search WWH ::
Custom Search