Java Reference
In-Depth Information
if (searchResult > 0)
System.out.printf("Duplicate part: %s ignored\n", part.partNum);
else { //this is a new part number; insert in location -searchResult
if (numRecords == MaxRecords) {
System.out.printf("Too many records: only %d allowed\n", MaxRecords);
System.exit(1);
}
//the index has room; shift entries to accommodate new part
for (int h = numRecords; h >= -searchResult; h--)
index[h + 1] = index[h];
index[-searchResult] = new Index(part.partNum, ++numRecords);
writePartToFile(part, f);
}
part = getPartData(in);
} //end while
index[0] = new Index("NOPART", numRecords);
in.close();
} //end createMasterIndex
public static Part getPartData(Scanner in) {
String pnum = in.next();
if (pnum.equals(EndOfData)) return null;
return new Part(pnum, in.next(), in.nextInt(), in.nextDouble());
} //end getPartData
public static void writePartToFile(Part part, RandomAccessFile f) throws IOException {
for (int h = 0; h < PartNumSize; h++) f.writeChar(part.partNum.charAt(h));
int n = Math.min(part.name.length(), StringFixedLength);
for (int h = 0; h < n; h++) f.writeChar(part.name.charAt(h));
for (int h = n; h < StringFixedLength; h++) f.writeChar(' ');
f.writeInt(part.amtInStock);
f.writeDouble(part.price);
} //end writePartToFile
public static void saveIndex(Index[] index) throws IOException {
RandomAccessFile f = new RandomAccessFile("index.bin", "rw");
int numRecords = index[0].recNum;
//fill the unused index positions with dummy entries
for (int h = numRecords+1; h <= MaxRecords; h++)
index[h] = new Index("NOPART", 0);
f.writeInt(MaxRecords);
for (int h = 0; h <= MaxRecords; h++) {
for (int i = 0; i < PartNumSize; i++)
f.writeChar(index[h].partNum.charAt(i));
f.writeInt(index[h].recNum);
}
f.close();
} //end saveIndex
Search WWH ::




Custom Search