Programming and Problem Solving with Java

(やまだぃちぅ) #1

CASE STUDY


562


MERGING ADDRESS BOOKS
Problem:In earlier chapters we created an electronic address book in which each entry
contains a name, an address, a phone number, and a birthday. We have stored the
entries on a file of objects. The electronic address book has been so successful that we
have shared it with others. Now we want to combine the entries from two address
books and print address labels in alphabetical order. If the same name appears in both
address books, the appropriate label should be printed only once. We also want to save
the combined address books.

Brainstorming:The processing seems very straightforward. We create a new address
book object made up of all the entries in both address books, removing duplicate
entries along the way. The combined address book must then be sorted and labels
printed on a text file. In addition to the classes involved in the class Entry, we have
three instances of an object file (two input and one output), a text file, and a driver:

Filtering:To this point, we have been working with files. The restriction that the list of
labels be alphabetized may lead to a different solution. We could read the two files of
entries and store them into two SortedListobjects. Then we could merge the two lists
and the output would be sorted. Let’s revise our list of classes:

Determining Responsibilities:This problem is somewhat different in that all of the
responsibilities are in the driver, and they are clearly spelled out in the problem
description. At this point, we switch from an object-oriented view and look at the tasks

ObjectInputStream
ObjectOutputStream
PrintWriter
SortedList
Driver

ObjectInputStream
ObjectOutputStream
PrintWriter
Driver
Free download pdf