import java.util.*; import java.io.*; public class DVD_Collection { private static final int MAX = 100; // constant private int capacity = MAX; // maximum size of the array private int size = 0; // actual size of the array private String sourceName = null; //file name storing DVD collection private boolean modified = false; private DVD_Entry[] theCollection = new DVD_Entry[capacity]; private int find(String title) { for (int i = 0; i < size; i++) if (theCollection[i].getTitle().equals(title)) return i; return -1; } //Add an entry or change an existing entry. public boolean addOrChangeEntry(DVD_Entry newEntry) { modified = true; int index = find(newEntry.getTitle()); if (index > -1) { theCollection[index].setCategory(newEntry.getCategory()); theCollection[index].setRunningTime(newEntry.getRunningTime()); return false; } // end if else { add(newEntry); return true; } // end else } //Add an entry to the collection. private void add(DVD_Entry entry) { theCollection[size] = entry; size++; } //Loads saved collection information public void loadData(String fileName) { sourceName = fileName; try { BufferedReader in = new BufferedReader(new FileReader(fileName)); String nextLine = null; String title = null; String runningTime = null; String category = null; StringTokenizer st = null; while ( (nextLine = in.readLine()) != null) { st = new StringTokenizer(nextLine, ","); title = st.nextToken(); category = st.nextToken(); runningTime = st.nextToken(); add(new DVD_Entry(title, category, runningTime)); } // end while in.close(); } // end try catch (FileNotFoundException ex) { return; } catch (IOException ex) { System.err.println("Load of directory failed."); ex.printStackTrace(); System.exit(1); } } //Look up an entry. public String[] lookupEntry(String title) { int index = find(title); if (index > -1) { String[] out = new String[2]; out[0] = theCollection[index].getCategory(); out[1] = theCollection[index].getRunningTime(); return out; } else return null; } //Remove an entry from the collection public boolean removeEntry(String notWanted) { int index = find(notWanted); if (index == -1) return false; else // this is really inefficient { DVD_Entry[] newCollection = new DVD_Entry[theCollection.length]; modified = true; for (int i = 0; i < index; i++) newCollection[i] = theCollection[i]; for (int i = index + 1; i < theCollection.length; i++) newCollection[i - 1] = theCollection[i]; theCollection = newCollection; size--; } return true; } // Method to save the collection. public void save() { if (modified) { try { PrintWriter out = new PrintWriter(new FileWriter(sourceName)); for (int i = 0; i < size; i++) { out.println(theCollection[i].getTitle() + "," + theCollection[i].getCategory() + "," + theCollection[i].getRunningTime()); } // end for out.close(); modified = false; } // end try block catch (Exception ex) { System.err.println("Save of dcollection failed"); ex.printStackTrace(); System.exit(1); }// end catch clause } // end if } // end method save // Retrieves all titles falling under the specified category public String[] getTitlesinCategory(String category) { int count = 0; String temp = ""; String[] out; for (int i = 0; i < size; i++) { if (theCollection[i].getCategory().equals(category)) { count++; temp += theCollection[i].getTitle() + ","; } //end if } //end for if (count == 0) return null; else { temp = temp.substring(0, temp.length()-1); StringTokenizer st = new StringTokenizer(temp, ","); out = new String[st.countTokens()]; for (int j = 0; j < out.length; j++) out[j] = st.nextToken(); } // end else return out; } //Sort the collection by title public void sortByTitle() { DVD_Entry minEntry; DVD_Entry temp; int minIndex; for (int i = 0; i < size; i++) { minEntry = theCollection[i]; minIndex = i; for (int j = i; j < size; j++) { if (theCollection[j].getTitle().compareTo(minEntry.getTitle()) < 0) { minEntry = theCollection[j]; minIndex = j; } // end if }// end inner for temp = theCollection[i]; theCollection[i] = minEntry; theCollection[minIndex] = temp; } // end outer for modified = true; } // end method sort by title public String toString() { String temp = ""; for(int i =0; i