CASE STUDY
306
takes the values as arguments and stores the values into its fields. Have we forgotten
anything else? The entry is to be written to a file. Should the Entry class be responsible
for writing itself to a file or should the driver provide this function? Encapsulation dic-
tates that the Entry should bear this responsibility. Here, then, is our revised CRC card
for Entry:
The knowledge responsibilities give us at least a partial list of the instance variables
of the Entryclass: a Name, an Address, and a Phone. There may be other internal fields. If
so, we can add them later. We have a Nameclass and a Phoneclass. The only classes left to
write are Address,Entry, and a driver.
In Chapter 7, we will examine several different versions of addresses. For this appli-
cation, let’s just use the street, city, state, and ZIP code. We should have one constructor
that takes these attributes as parameters. Let’s include another constructor that reads
values into its fields. There should be four knowledge responsibilities, one for each of
these fields. The first three fields should be strings. What about the ZIP code? If the ZIP
code is just the standard five digits, we could use an integer. However, many ZIP codes
now use five digits, plus a hyphen and four more digits. To cover both cases, let’s use a
string to represent the ZIP code. Should this class provide the values in these fields in a
variety of formats like the Nameclass does? Not at this stage. We are only creating the
address book now, not processing the information in some way.
Class Name: Entry Superclass: Subclasses:
Responsibilities Collaborations
Create (name, address, phone) Name, Address, Phone
Create( ) Name, Address, Phone, BufferedReader
Know name None
return Name
Know address None
return Address
Know phone number None
return Phone
Write entry to a file (outfile) PrintWriter