import java.io.*; import java.util.*; import java.lang.*; public class collection { private Vector CDS; // Vector for holding collection of cd objects private Vector theOutput; private static String default_file = "cd_url.txt"; public collection() { // constructor. CDS = new Vector(); } public Vector processInput(String theInput) { String command = null; String restOfLine = null; theOutput = new Vector(); // packaging Vector if (CDS.size() < 1) { // automatically loads default collection on start-up try { loadCollectionText(default_file); } catch (FileNotFoundException e) { // to handle wrong file name. System.out.println("\n++ERROR - File not found."); System.exit(0); } catch (IOException e) { // to handle IO exceptions. System.out.println("\n++ERROR - Failure to load specified file."); System.exit(0); } catch (NullPointerException e) { // handle file without markers '*'. System.out.println("++ERROR - Default collection may be corrupted."); System.exit(0); } } else { // client requests processed here try { StringTokenizer st = new StringTokenizer(theInput); command = st.nextToken(); restOfLine = theInput.substring(theInput.indexOf(' ')+1, theInput.length()); System.out.println("Heard: " + command + " " + restOfLine); // displays client request if (command.equalsIgnoreCase("LIST")) List(); else if (command.equalsIgnoreCase("ARTIST")) Search(1, restOfLine); else if (command.equalsIgnoreCase("COMPOSER")) Search(2, restOfLine); else if (command.equalsIgnoreCase("TITLE")) Search(3, restOfLine); else if (command.equalsIgnoreCase("TRACK")) Search(4, restOfLine); else if (command.equalsIgnoreCase("LOAD")) loadCollection(restOfLine); else if (command.equalsIgnoreCase("SAVE")) saveCollection(restOfLine); else if (command.equalsIgnoreCase("REMOVE")) Search(5, restOfLine); } catch (IOException e) { // to hanlde IO exceptions. System.out.println("IOException"); } catch (NoSuchElementException e) { // to handle empty Strings. System.out.println("NoSuchElementException"); } } return theOutput; // packaging Vector used to return cd objects (if any) } //======================================= addCD() ============================================= public void addCD(cd inputCD) { System.out.println("Adding CD."); CDS.addElement(inputCD); // cd object added to CDS (collection) Vector } //================================ Search() and Remove() ======================================= private void Search(int command, String r_o_l) { boolean found = false; for (int i = 0; i < CDS.size(); ++i) { found = false; // resets boolean each loop. cd element = (cd)CDS.elementAt(i); switch (command) { case 1: if (element.getArtist().equalsIgnoreCase(r_o_l)) found = true; break; case 2: if (element.getComposer().equalsIgnoreCase(r_o_l)) found = true; break; case 3: if (element.getTitle().equalsIgnoreCase(r_o_l)) found = true; break; case 4: { Vector TR = new Vector(); TR = element.getTracks(); for (int j = 0; j < TR.size(); ++j) { String track = (String)TR.elementAt(j); if (track.equalsIgnoreCase(r_o_l)) found = true; } break; } case 5: if (element.getTitle().equalsIgnoreCase(r_o_l)) { CDS.removeElementAt(i); } } if (found) { theOutput.addElement(element); // adds the cd object containing the found } // object to packaging Vector (theOutput) } } //==================================== List() ============================================== private void List() { theOutput = CDS; // fills packaging Vector with current collection } //=============================== loadCollection() ========================================== private void loadCollection(String f) throws IOException { FileInputStream in = new FileInputStream(f); ObjectInputStream s = new ObjectInputStream(in); CDS.removeAllElements(); // clears old collection from the CDS Vector try { CDS = (Vector)s.readObject(); // fills CDS vector with new collection } catch (ClassNotFoundException e) { System.out.println("Error - ClassNotFoundException !"); } theOutput = CDS; // fills packaging Vector with current collection in.close(); } //=============================== loadCollectionText() ==================================== // This method reads the default text file and constructs CD objects adding them to the Vector CDS. private void loadCollectionText(String f) throws IOException { BufferedReader in = new BufferedReader(new FileReader(f)); String track; while (in.ready()) { Vector TR = new Vector(); String A = in.readLine(); // 1st 5 lines of text file are read into tempory Strings. String C = in.readLine(); String T = in.readLine(); String U = in.readLine(); while (!(track = in.readLine()).equals("*")) { // rest of data set is read into tempory Vector containing Tracks. TR.addElement(track); } cd element = new cd(A, C, T, TR, U); // CD instance constructed using the tempory objects above. CDS.addElement(element); // CD instance added to CD Vector (CDS). } theOutput = CDS; // fills packaging Vector with current collection System.out.println("\n++OK - FILE " + f + " LOADED SUCCESSFULLY."); // confirm to the user the method is finished. } //=============================== saveCollection() ==================================== private void saveCollection (String f) throws IOException { FileOutputStream out = new FileOutputStream(f); ObjectOutputStream s = new ObjectOutputStream(out); s.writeObject(CDS); s.flush(); out.close(); s.close(); } }