import java.io.RandomAccessFile; import java.io.IOException; public class PartsDB{ public final static int PART_NUM_LENGTH = 20; // char-based public final static int DESC_LENGTH = 30; // char-based public final static int QUANTITY_LENGTH = 1; // int-based public final static int COST_LENGTH = 1; // int-based; private final static int RECORD_LENGTH = PART_NUM_LENGTH*2 + DESC_LENGTH*2 + QUANTITY_LENGTH*4 + COST_LENGTH*4; // UNIT BYTE. private RandomAccessFile raf; public PartsDB(String pathname) throws IOException{ raf = new RandomAccessFile(pathname, "rw"); } public void append(String partNum, String partDesc, int quantity, int unitCost) throws IOException{ raf.seek(raf.length()); write(partNum, partDesc, quantity, unitCost); } public void close() throws IOException{ raf.close(); } public int numRecs() throws IOException{ return (int)raf.length()/RECORD_LENGTH; } public Part select(int recNo) throws IOException{ if(recNo<0 || recNo >= numRecs()){ throw new IllegalArgumentException(recNo + " out of range"); } raf.seek(recNo*RECORD_LENGTH); return read(); } public void update(int recNo, String partNum, String partDesc, int quantity, int unitCost) throws IOException{ if(recNo<0 || recNo>=numRecs()){ throw new IllegalArgumentException(recNo + " out of range"); } raf.seek(recNo*RECORD_LENGTH); write(partNum, partDesc, quantity, unitCost); } private Part read() throws IOException{ StringBuffer sb = new StringBuffer(); for(int i=0;i PART_NUM_LENGTH){ sb.setLength(PART_NUM_LENGTH); }else{ // add some padding int paddingLen = PART_NUM_LENGTH-sb.length(); for(int i=0;i< paddingLen;i++){ // understand the statements pattern; evaluate the last two expressions every iteration. sb.append(" "); } } System.out.print("WritePart: " + sb.toString() + " | "); raf.writeChars(sb.toString()); sb = new StringBuffer(partDesc); if(sb.length() > DESC_LENGTH){ sb.setLength(DESC_LENGTH); }else{ // add some padding int descLen = DESC_LENGTH-sb.length(); // understand the FOR statements pattern; evaluate the last two expressions every iteration. for(int i=0;i