Java Reference
In-Depth Information
The OOD solution to this problem proceeds as follows:
Step 1: Identify all the nouns.
Place, candy, candy machine, cafeteria, dispenser, items, cash register, chips,
gum, cookies, customer, products, cost (of the item), money, and change.
In this description of the problem, products stand for items such as candy, chips, gum, and
cookies. In fact, the actual product in the machine is not that important. What is
important is to note that there are four dispensers, each capable of dispensing one
product. Further, there is one cash register. Thus, the candy machine consists of four
dispensers and one cash register. Graphically, this can be represented as in Figure 6-14.
CandyMachine
4
1
Dispenser
CashRegister
FIGURE 6-14 Candy Machine and its components
In Figure 6-14, the number 4 on top of the box Dispenser indicates that there are four
dispensers
in the candy machine. Similarly,
the number 1 on top of
the box
CashRegister indicates that the candy machine has one cash register.
Step 2: Identify the class(es).
You can see that the program you are about to write is supposed to deal with dispensers
and cash registers. That is, the main objects are four dispensers and a cash register. Because
all the dispensers are of the same type, you need to create a class, say, Dispenser ,to
create the dispensers. Similarly, you need to create a class, say, CashRegister , to create a
cash register. We will create the class CandyMachine containing the four dispensers, a
cash register, and the application program.
Step 3: Identify the data members for each of the class(es).
Dispenser To make the sale, at least one item must be in the dispenser and the customer
must know the cost of the product. Therefore, the data members of a dispenser are:
￿ Product cost
￿ Number of items in the dispenser
Cash Register The cash register accepts money and returns change. Therefore, the cash
register has only one data member, which we call cashOnHand .
 
Search WWH ::




Custom Search