/** * A class containing list of records with insert/delete * and search funcionality. * * @author Vivek Rane */ public class DynamicRecordDB { private int maxRecords = 0; private int numRecords = 0; private Record recordList[]; public DynamicRecordDB (int maxRecords) throws Exception { if (maxRecords < 1) { throw new Exception ("Please supply a positive integer for the number of records"); } this.maxRecords = maxRecords; this.recordList = new Record[maxRecords]; } /** * Inserts a record into the ordered array in a sorted fashion. * @param rec - The record to be inserted. */ public void insert (Record rec) { // Check for overflow if (numRecords == maxRecords) { // Increase the size of the array System.out.println("Resizing the record list array..."); Record tempList[] = new Record[maxRecords*2]; for (int i=0; i rec.ssn) break; } // Shift all records one space down for (int j=numRecords-1; j>=i; j--) { recordList[j+1] = recordList[j]; } // Insert the new record recordList[i] = rec; // Increment current number of employees in the list numRecords++; printAll(); } /** * Deletes a record from the list based on the key (ssn). * @param ssn */ public void delete (long ssn) { if (numRecords==0) { System.out.println("No records present!"); return; } int i; for (i=0; i ssn) { System.out.println("Record not found: " + ssn); return; } // SSN found if (recordList[i].ssn == ssn) { break; } } for (int j=i; j ssn) { last = current-1; continue; } if (recordList[current].ssn < ssn) { first = current+1; continue; } } } /** * Prints the list of records to the standard output; */ private void printAll() { System.out.println("---------------------------"); for (int i=0; i