import java.io.*; import java.util.*; import java.lang.*; public class cd implements Serializable { // the private data fields. private String Title; private String Artist; private String Composer; private Vector Tracks; private String cover_URL; // constructor. public cd(String A, String C, String T, Vector Tr, String c_url) { Artist = A; Composer = C; Title = T; Tracks = Tr; cover_URL = c_url; } // public methods - accessing and returning the private fields. public String getArtist() { return Artist; } public String getComposer() { return Composer; } public String getTitle() { return Title; } public Vector getTracks() { return Tracks; } public String getcover_URL() { return cover_URL; } // public method for printing the contents of the CD object. public void printAll() { System.out.println("----------------------------------------------------------------------"); System.out.println("TITLE: " + Title); System.out.println("ARTIST: " + Artist); System.out.println("COMPOSER: " + Composer); System.out.println("URL: " + cover_URL); System.out.println("\nTRACKS:\n"); for (int i = 0; i < Tracks.size(); ++i) { String track = (String)Tracks.elementAt(i); if (track.equals("*")) break; else System.out.println(track); } System.out.println("----------------------------------------------------------------------"); } // ******************************* MAIN **************************************** // uses the same test file as collection.java (containing 6 data sets). However, the // program will only read in the 1st data set and construct and print 1 CD instance. // (see documentation for the design algorithm). public static void main(String args []) throws IOException { // sets up a file reading object to the test file 'cd.txt' - which must be in the local // directory. try { BufferedReader in = new BufferedReader(new FileReader("cd.txt")); // constructs new Vector to hold the track Strings. Vector TR = new Vector(); String track; // Assigns the 1st 3 lines of the file to corresponding tempory Strings. String A = in.readLine(); String C = in.readLine(); String T = in.readLine(); String U = in.readLine(); // reads each line of the file into the tempory Vector TR until the end-of-data-set // marker is located. while (!(track = in.readLine()).equals("*")) { TR.addElement(track); } // constructs CD instance called 'test' by passing tempory objects to constructor. cd test = new cd(A, C, T, TR, U); // tests the successfull reading/assigning/constructing by printing contents to screen. test.printAll(); System.exit(0); } catch (FileNotFoundException e) { // to handle the absence of the test file cd.txt. System.out.println("\n\n++ERROR - File not found."); } catch (IOException e) { // to handle IO exceptions. System.out.println("\n\n++ERROR - IO error."); } catch (NullPointerException e) { // to handle absence of end-of-data-set marker in file. System.out.println("\n\n++ERROR - The test file contains an illegal format."); } } }