Question: CODE FROM PREVIOUS ASSIGNMENT (PART 1): import java.io.*; import java.util.*; public class DataBase { private DataBaseArray myDB; private IndexArray IDIA, fNameIA, lNameIA; private DeleteStack deleteStack;
CODE FROM PREVIOUS ASSIGNMENT (PART 1):
import java.io.*; import java.util.*;
public class DataBase { private DataBaseArray myDB; private IndexArray IDIA, fNameIA, lNameIA; private DeleteStack deleteStack; private Scanner input; private String nDn = " ---------------------------------------------------------- "; private String Dn = "---------------------------------------------------------- "; // Default constructor for size 100 public DataBase() { this.myDB = new DataBaseArray(100); this.IDIA = new IndexArray(100, false); this.fNameIA = new IndexArray(100, false); this.lNameIA = new IndexArray(100, true); this.deleteStack = new DeleteStack(100); readInDataFromFile(); input = new Scanner(System.in); } // Custom constructor for maximum size public DataBase(int maxSize) { this.myDB = new DataBaseArray(maxSize); this.IDIA = new IndexArray(maxSize, false); this.fNameIA = new IndexArray(maxSize, false); this.lNameIA = new IndexArray(maxSize, true); this.deleteStack = new DeleteStack(maxSize); readInDataFromFile(); input = new Scanner(System.in); } // Read in records public void readInDataFromFile() { File DBData = new File("P1Data.txt"); Scanner input = new Scanner(System.in); try { input = new Scanner(DBData); } catch (FileNotFoundException e) { e.printStackTrace(); } String ID = ""; String fName = ""; String lName = ""; String[] values;
while(input.hasNextLine()) { values = input.nextLine().split(" "); lName = values[0]; fName = values[1]; ID = values[2]; this.insertIt(ID, fName, lName); } } // Ask user for ID, delete that record // Error if ID is not in array public void deleteIt() { String ID = ""; System.out.println("Enter the ID of the record to be deleted: "); ID = this.input.nextLine(); if (this.IDIA.searchByKey(ID) == -1) { System.out.println(Dn + "ID not found" + nDn); return; }
this.delete(ID); System.out.println(Dn + "Record deleted" + nDn); } // Ask user for ID, find that record // Error if ID is not in array public void findIt() { String ID = ""; System.out.println("Enter the ID of the record to be found: "); ID = this.input.nextLine(); int iaIndexOfRecord = this.IDIA.searchByKey(ID);
if (iaIndexOfRecord == -1) { System.out.println(Dn + "ID not found" + nDn); return; }
System.out.println(this.myDB.getRecord(this.IDIA.getWhereByKey(ID))); System.out.println(Dn + "Record found" + nDn); }
// Ask user for ID, add new record // Error if ID is already in array public void addIt() { String ID = ""; String fName = ""; String lName = ""; System.out.println("Enter the ID of the record to be added: "); ID = this.input.nextLine(); if (this.IDIA.searchByKey(ID) != -1) { System.out.println(Dn + "ID already in use, please try again." + nDn); return; }
System.out.println("Enter the first name of the record: "); fName = this.input.nextLine();
System.out.println("Enter the last name of the record: "); lName = this.input.nextLine();
this.insertIt(ID, fName, lName); System.out.println(Dn + "Record added" + nDn); } // Insert record into DB public void insertIt(String ID, String fName, String lName) { if (IDIA.searchByKey(ID) == -1) { DataBaseRecord record = new DataBaseRecord(ID, fName, lName); int where = -1; if (this.deleteStack.isEmpty()) { this.myDB.addRecord(record); } else { where = this.deleteStack.pop(); this.myDB.setRecord(record, where); } if (where < 0) { where = this.myDB.getSize() - 1; } this.fNameIA.add(new IndexRecord
IDIA.deleteIndex(IDIA.searchByKey(key)); fNameIA.deleteIndex(fNameIA.searchByWhere(whereToDelete)); lNameIA.deleteIndex(lNameIA.searchByWhere(whereToDelete)); deleteStack.push(whereToDelete); } // List DB by passing desired IndexArray and order public void list(IndexArray IA, boolean ascending) { if (ascending) { IA.pointerToFront(); while(IA.hasNext()) { System.out.println(this.myDB.getRecord(IA.getNext())); } } else { IA.pointerToBack(); while(IA.hasPrevious()) { System.out.println(this.myDB.getRecord(IA.getPrevious())); } }
System.out.println(Dn + "Records successfully listed" + nDn); }
// Methods for listing public void ListByIDAscending() { this.list(IDIA, true); }
public void ListByFirstAscending() { this.list(fNameIA, true); } public void ListByLastAscending() { this.list(lNameIA, true); } public void ListByIDDescending() { this.list(IDIA, false); }
public void ListByFirstDescending() { this.list(fNameIA, false); }
public void ListByLastDescending() { this.list(lNameIA, false); } }
----------------------------------------------------------------------
public class DataBaseArray { private DataBaseRecord[] data; private int maxSize; private int currentSize; // Constructor to specify size of DB public DataBaseArray(int maxSize) { this.data = new DataBaseRecord[maxSize]; this.maxSize = maxSize; this.currentSize = 0; } // Inserts record into desired index public void setRecord(DataBaseRecord DBRec, int index) { this.data[index] = DBRec; }
// Inserts record at end of array public void addRecord(DataBaseRecord DBRec) { if (currentSize != maxSize) { this.data[currentSize] = DBRec; this.currentSize++; } } // Returns current size of array public int getSize() { return this.currentSize; }
//Returns record at specified index public DataBaseRecord getRecord(int index) { return data[index]; } }
----------------------------------------------------------------------
public class DataBaseRecord { private String ID; private String fName; private String lName; DataBaseRecord(String ID, String fName, String lName) { this.ID = ID; this.fName = fName; this.lName = lName; } public String getID() { return ID; }
public String getFirst() { return fName; }
public String getLast() { return lName; } @Override public String toString() { return this.ID + " " + this.fName + " " + this.lName; } }
----------------------------------------------------------------------
public class DeleteStack { private int[] stack; private int currentSize;
// Constructor to pass maximum size to public DeleteStack(int maxSize) { this.stack = new int[maxSize]; this.currentSize = 0; }
// Pushes DB index onto array public void push(int index) { this.stack[this.currentSize++] = index; }
// Returns DB index at end of array and decrements end-of-array pointer public int pop() { return this.stack[this.currentSize--]; }
// Returns if stack is empty public boolean isEmpty() { return this.currentSize == 0; } }
----------------------------------------------------------------------
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
