// A PlayList has a collection of Tracks. In addition, playlists also have a name // and a description, Eg. a playlist by the name "MJ All time hits" with the // description "The greatest tracks by the King of Pop" has a collection of Tracks // each a song by Michael Jackson. // An additional feature of your playlist will be that you can search for tracks // by title and by artist. These search commands can be one of two types (examples // below) - // 1. Exact Search: // searchByExactTitle("Dark Side of the Moon") will return the single Track // among all the tracks in this PlayList that have their title as "Dark Side of the // Moon". If no such track exists, then null is returned. // 2. Approximate (or Substring) Search // searchByTitle("moon") will return all the Tracks in this PlayList that have the // word "moon" in their title. Note that since there can be more than one such // Track, the return value is a new PlayList with only the correct tracks. For // example, PlayList p2 = p1.searchByTitle("moon") then p2 is a playlist // whose tracks are the tracks that were in p1 and had the word "moon" in their // name. Note that the chosen tracks will now be present both in playlist p1 and // p2. The name property of p2, should be the search expression ("moon" in this // case), and description should indicate that it was a search result ("search for // for title: moon" in this case). import java.util.ArrayList; import java.util.Iterator; public class PlayList{ // The instance variables of the PlayList - the name, description, and collection // of tracks in the playlist. // The name and description of this playlist private String name; private String description; // The collection of tracks on this playlist private ArrayList tracks; // The constructor for a playlist... create an empty playlist with the name // and description provided public PlayList(String playListName, String description){ name = playListName; this.description = description; tracks = new ArrayList(); } // Access methods - get the name, description, tracks, and the number of tracks public String getName() { return name; } public String getDescription() { return description; } public ArrayList getTracks() { return tracks; } public int getNumberOfTracks() { return tracks.size(); } // Express the contents of the playlist as a single string - the string should // include the name of the playlist, its description and the details of each of // the tracks in the playlist. Remember that your string can also include // newlines ("\n") so that the string is more readable. // Note: This method sould NOT print the string, but return the string. public String toString() { String string = "Playlist: Name: " + name + ", Description: " + description + "\n"; for(int tr = 0; tr < tracks.size(); tr++) string += " " + tracks.get(tr).toString() + "\n"; return string; } // Commands // Add a single track to the collection of tracks in this playlist. // Check if the parameter is not a null parameter. public void addTrack(Track track) { if (track != null) tracks.add(track); } // Merge two playlists. Add the tracks in the other playlist to the tracks in this // playlist. Retain the name and description of this playlist. // Note: You should NOT create a new PlayList. The tracks in the other playlist // will now be present in both this and the other playlist. public void combinePlayList(PlayList other) { // Get each track in the other playlist using an iterator ArrayList otherTracks = other.getTracks(); Iterator it = otherTracks.iterator(); while(it.hasNext()){ // Get the next track --- remember to cast Track track = (Track) it.next(); // Add to current playlist this.addTrack(track); } } // Search Methods // Search for a track in this PlayList whose title exactly matches the // title exactTitle. If there are multiple tracks with that name, return any // one. Return null if there is no such track. public Track searchByExactTitle(String exactTitle){ // Iterate through each track Iterator it = tracks.iterator(); while(it.hasNext()){ Track track = (Track) it.next(); // Check for the exact title String trackTitle = track.getTrackName(); if(exactTitle.equals(trackTitle)){ return track; } } return null; } // Search for the tracks in this PlayList such that titleSubString is a // part of the title of the track. Return a new PlayList that has only // the tracks that satisfy this condition. The name and description of the // newly created PlayList are as described at the top of this file. // Observe that there might not be any tracks which satisfy this search. // In that case the return playlist will have no tracks (but still has a // valid name and description). public PlayList searchByTitle(String titleSubString){ // Create the result playlist PlayList result = new PlayList(titleSubString, "search by title: " + titleSubString); // We are doing a case insensitive search by converting strings to lower case // before checking. This is not necessary for the homework String searchString = titleSubString.toLowerCase(); // Iterate through each track for(int i=0; i= 0){ result.addTrack(track); } } return result; } // Search for the tracks such that artistSubString is a part of the artist name // for the track. The rest of the description is similar to that of the // searchByTitle method. public PlayList searchByArtist(String artistSubString){ // Create the result playlist PlayList result = new PlayList(artistSubString, "search by artist: " + artistSubString); // We are doing a case insensitive search by converting strings to lower case // before checking. This is not necessary for the homework String searchString = artistSubString.toLowerCase(); // Iterate through each track for(int i=0; i= 0){ result.addTrack(track); } } return result; } }