Java Reference
In-Depth Information
The auction project contains the following classes: Auction , Bid , Lot , and Person . A close
look at the class diagram for this project (Figure 4.6) reveals that the relationships between the
various classes are a little more complicated than we have seen in previous projects, and this will
have a bearing on the way in which information is accessed during the auction activities. For in-
stance, the diagram shows that Auction objects know about all the other types of objects: Bid ,
Lot , and Person . Lot objects know about Bid objects, and Bid objects know about Person
objects. What the diagram cannot tell us is exactly how information stored in a Bid object, say,
is accessed by an Auction object. For that, we have to look at the code of the various classes.
4.14.1
Getting started with the project
At this stage, it would be worth opening the auction project and exploring the source code be-
fore reading further. As well as seeing familiar use of an ArrayList and loops, you will likely
encounter several things that you do not quite understand at first, but that is to be expected as
we move on to new ideas and new ways of doing things.
An Auction object is the starting point for the project. People wishing to sell items enter
them into the auction via the enterLot method, but they only supply a string description. The
Auction object then creates a Lot object for each entered lot. This models the way things
work in the real world: it is the auction site, rather than the sellers, for instance, that assigns lot
numbers or identification codes to items. So a Lot object is the auction site's representation of
an item for sale.
In order to bid for lots, people must register with the auction house. In our program, a poten-
tial bidder is represented by a Person object. These objects must be created independently on
BlueJ's object bench. In our project, a Person object simply contains the person's name. When
someone wants to bid for a lot, they call the bidFor method of the Auction object, entering
the lot number they are interested in and how much they want to bid for it. Notice that they pass
the lot number rather than the Lot object; Lot objects remain internal to the Auction object
and are always referenced externally by their lot number.
Just as the Auction object creates Lot objects, it also transforms a monetary bid amount into a
Bid object, which records the amount and the person who bid that amount. This is why we see a
link from the Bid class to the Person class on the class diagram. However, note that there is no
link from Bid to Lot ; the link on the diagram is the other way around, because a Lot records
which is currently the highest bid for that lot. This means that the Lot object will replace the
Bid object it stores each time a higher bid is made.
What we have described here reveals quite a nested chain of object references. Auction
objects store Lot objects; each Lot object can store a Bid object; each Bid object stores a
Person object. Such chains are very common in programs, so this project offers a good oppor-
tunity to explore how they work in practice.
Exercise 4.46 Create an auction with a few lots, persons, and bids. Then use the object
inspector to investigate the object structure. Start with the auction object, and continue by
inspecting any further object references you encounter in the objects' fields.
 
Search WWH ::




Custom Search