Java Reference
In-Depth Information
return -lo; // key not found; insert in location lo
} // end search
} //end class UseIndex
// Part and Index classes go here
The following is a sample run of Program P7.9:
Enter a part number (E to end): dkp080
Part number: DKP080
Part name: Disc-Pads
Amount in stock: 16
Price: $9.99
Enter a part number (E to end): GsF555
Part number: GSF555
Part name: Gas-Filter
Amount in stock: 9
Price: $4.50
Enter a part number (E to end): PKL060
Part not found
Enter a part number (E to end): pkl070
Part number: PKL070
Part name: Park-Lens
Amount in stock: 8
Price: $6.50
Enter a part number (E to end): e
Observe that the part numbers could be entered using any combination of uppercase and lowercase.
If required, we could use the index to print the records in order by part number. We simply print the records in
the order in which they appear in the index. For example, using our sample data, we have the index as follows:
BLJ375 2
DKP080 4
FLT015 3
GSF555 5
PKL070 1
If we print record 2, followed by record 4, followed by record 3, followed by record 5, followed by record 1, we
would have printed them in ascending order by part number. This can be done with the following function:
public static void printFileInOrder(Index[] index, RandomAccessFile f) throws IOException {
System.out.printf("\nFile sorted by part number: \n\n");
int numRecords = index[0].recNum;
for (int h = 1; h <= numRecords; h++) {
f.seek(PartRecordSize * (index[h].recNum - 1));
Search WWH ::




Custom Search