Java Reference
In-Depth Information
public static Part readPartFromFile(RandomAccessFile f) throws IOException {
String pname = "";
for (int h = 0; h < PartNumSize; h++) pname += f.readChar();
char[] name = new char[StringFixedLength];
for (int h = 0; h < StringFixedLength; h++) name[h] = f.readChar();
String hold = new String(name, 0, StringFixedLength);
return new Part(pname, hold.trim(), f.readInt(), f.readDouble());
} //end readPartFromFile
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 int search(String key, Index[] list, int n) {
//searches list[1..n] for key. If found, it returns the location; otherwise
//it returns the negative of the location in which key should be inserted.
int lo = 1, hi = n;
while (lo <= hi) { // as long as more elements remain to consider
int mid = (lo + hi) / 2;
int cmp = key.compareToIgnoreCase(list[mid].partNum);
if (cmp == 0) return mid; // search succeeds
if (cmp < 0) hi = mid - 1; // key is 'less than' list[mid].partNum
else lo = mid + 1; // key is 'greater than' list[mid].partNum
}
return -lo; // key not found; insert in location lo
} // end search
} //end class UpdateFile
// Part and Index classes go here
The following is a sample run of Program P7.10:
Enter part number (E to end): blj375
Enter amount sold: 2
Amount remaining: 10
BLJ375 Ball-Joint 10 11.95
Enter part number (E to end): blj375
Enter amount sold: 11
You have 10: cannot sell more, ignored
Enter part number (E to end): dkp080
Enter amount sold: 4
Amount remaining: 12
DKP080 Disc-Pads 12 9.99
Search WWH ::




Custom Search