Programming and Problem Solving with Java

(やまだぃちぅ) #1

CASE STUDY


570


{

Address resultAddress;
resultEntry = resultList.getNextItem(); // Get an entry
outFile.writeObject(resultEntry); // Write the entry
resultAddress = resultEntry.knowAddress();
labelFile.println(resultEntry.knowName().full());
labelFile.println(resultAddress.knowStreet());
labelFile.println(resultAddress.knowCity() + ","
+ resultAddress.knowState() + " "+ resultAddress.knowZip());
labelFile.println();
}

// Close files and exit
firstFile.close();
secondFile.close();
outFile.close();
labelFile.close();
}

static voidgenerateLists(ObjectInputStream inFile, SortedList list)
throwsIOException, ClassNotFoundException
{
EntryPlus entry;
booleanmoreData = true;
while(moreData)
{
try
{
entry = (EntryPlus)inFile.readObject();
list.insert(entry);
}
catch(EOFException except)
{
moreData = false;
}
}
}

}

Because EntryPlusdoes not implement Comparable, we need to change our Listclass
to call the compareTomethod associated with the Namefield in an item. We can access this
field using the knowNamemethod for the EntryPlusobject. Of course,sortedListwas also
written to use Stringitems, and must be changed so that items are EntryPlusobjects.
Testing MergeBooks required that we write a small program to enable us to create
data files and another program to view the name fields in the files. When you work
with files of serialized objects it is often necessary to write such helper programs as

T


E


A


M


F


L


Y


Team-Fly®

Free download pdf