Java Reference
In-Depth Information
System.out.printf("\nEnter part number (E to end): ");
String pnum = in.next();
while (!pnum.equalsIgnoreCase("E")) {
updateRecord(pnum, index, numRecords);
System.out.printf("\nEnter part number (E to end): ");
pnum = in.next();
} //end while
} //end main
public static void updateRecord(String pnum, Index[] index, int max) throws IOException {
Scanner in = new Scanner(System.in);
RandomAccessFile fp = new RandomAccessFile("parts.bin", "rw");
int n = search(pnum, index, max);
if (n < 0) System.out.printf("Part not found\n");
else {
fp.seek(PartRecordSize * (index[n].recNum - 1));
Part part = readPartFromFile(fp);
System.out.printf("Enter amount sold: ");
int amtSold = in.nextInt();
if (amtSold > part.amtInStock)
System.out.printf("You have %d: cannot sell more, ignored\n",
part.amtInStock);
else {
part.amtInStock -= amtSold;
System.out.printf("Amount remaining: %d\n", part.amtInStock);
fp.seek(PartRecordSize * (index[n].recNum - 1));
writePartToFile(part, fp);
System.out.printf("%s %-11s %2d %5.2f\n", part.partNum, part.name,
part.amtInStock, part.price);
} //end if
} //end if
fp.close();
} //end updateRecord
public static Index[] retrieveIndex() throws IOException {
RandomAccessFile f = new RandomAccessFile("index.bin", "rw");
int MaxRecords = f.readInt();
Index[] index = new Index[MaxRecords + 1];
for (int j = 0; j <= MaxRecords; j++) {
String pnum = "";
for (int i = 0; i < PartNumSize; i++) pnum += f.readChar();
index[j] = new Index(pnum, f.readInt());
}
f.close();
return index;
} //end retrieveIndex
Search WWH ::




Custom Search