// The JukeBox should support all the features described in the problem statement. // It has a collection of PlayLists and a MediaPlayer. The main components // of the implementation are the following. // 1. Loading playlists from files. You can create playlists by reading them from // a text file. Implement the method loadPlayListFromFile as // described in greater detail later in this document. This method uses the // PlayListReader class to obtain an ArrayList of strings that it processes to // create and return a new play list. These playlists can then to added to the // collection of the JukeBox using the addPlayList method. // 2. Search for tracks. You can search for tracks by title and by the name of the // artist. these searches are very similar to the Approximate or SubString search as // described in PlayList.java. However, now you will have to search through all the // playlists that are present in the collection of playlists in the JukeBox. So // it will be easier to implement the search methods in the playlist first. // 3. Making requests. You can request for playlists to be played in the media // player (using the addPlayListToRequestQueue method). The juke box will have // to maintain a collection of requested playlists. // When the play() method of the JukeBox is invoked, a new playlist is // created that has all the tracks in all the requested playlists. // This playlist is sent to the play method of the Media Player and the tracks // are played on the media player (not really, only the track names are displayed :) // Calling the play method must empty the collections of requested playlists, since // they have already been played. // Typical interaction. // JukeBox j = new JukeBox(); // PlayList p1 = j.loadPlayListFromFile("..."); specify the file name here. // PlayList p2 = j.loadPlayListFromFile("..."); // ... // PlayList s1 = j.searchByTrackName("hotel cal"); // j.addPlayListToRequestQueue(s1); // PlayList s2 = j.searchByArtistName("eatles"); // j.addPlayListToRequestQueue(s2); // ... // j.play(); // The whole process of searching and making requests and playing will then repeat import java.util.ArrayList; public class JukeBox{ // The instance variables - the collections of playlists, the collection of // requested playlists. // The player that is accepting requests from this JukeBox private MediaPlayer mplayer; // Other instance variables... private ArrayList playlistCollection; // The collection of playlists that have // been added to the JukeBox private ArrayList requestedPlayLists; // The playlists that have been added to the // request queue // Constructor - initializes the JukeBox public JukeBox(){ // The media player mplayer = new MediaPlayer(); // Everything else that needs to get initialized must goes here } // Get/Set/Add properties // Add the playlist to the collection of playlists maintained by the // JukeBox. Return the number of playlists in the collection public int addPlayList(PlayList playlist){ return 0; } // Express the contents of the JukeBox in a single string. This string must // include all the play lists in the jukebox collection (with their tracks), // and also identify the playlists (with the tracks) that are in the requestQueue // Add newlines ("\n") to the return string, so that if it were printed, the // string will be readable. // NOTE: A string is computed and returned, NOT printed right here. public String toString(){ return ""; } // Search Queries // Search for the tracks in the JukeBox such that titleSubString is a // part of the title of the track. You should search through all the playlists // that are in the playlist collection of the JukeBox. // Return a NEWLY CREATED PlayList that has only // the tracks that satisfy this condition. The name and description of the // newly created PlayList are the same as described at the start of // PlayList.java. // 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){ // HINT: You can call the search on the individual playlists and then combine them // You will still need to iterate through ArrayLists return null; } // Search for the tracks such that artistSubString is a part of the artist name // for the track. Again the track might exist in any PlayList in playListCollection. // The rest of the description is similar to that of the searchByTitle method. public PlayList searchByArtist(String artistSubString){ return null; } // Search for the tracks such that titleSubString is a part of the track title // AND artistSubString is a part of the artist name for the track. // Again the track might exist in any PlayList in playListCollection. // The rest of the description is similar to that of the searchByTitle method. public PlayList searchByTitleAndArtist(String titleSubString, String artistSubString){ // Return a PlayList that has tracks that satisfy the conditions // 1. titleSubString appears in the title of the track // 2. artistSubString appears in the name of the artist of the track // HINT: Very similar to the earlier two searches return null; } // Making Requests and Playing songs in the Media Player // The whole purpose of a JukeBox - PlayLists that are the result of a search // or otherwise are added to the requested playlists queue, such that they are // played the next time play() is called. This is the simplest method public void addPlayListToRequestQueue(PlayList playlist){ } // Send the tracks that are present in the playlists in the requested ones to the // media player, such that they can be played. Note that the media player // accepts only a single playlist. So all the requested playlists have to be // combined into a single new one, before they can be sent to the media player. // Further, at the the end the collection of requests should be emptied since // the tracks have already been played in the media player. public void play() { // Combine the entries in the different requested playlists // Clear the requests // Invoke the play method of the media player } // Loading Playlists from files // Use the PlayListReader class that returns an ArrayList of Strings are they // were represented in the file specified. // The contents of the ArrayList (streamOfStrings) should be in this order // "playlist", its name, description, "track", its title, its artist, its length, // "track", ... public PlayList loadPlayListFromFile(String filename){ // Create the list of strings from the playlist file named filename PlayListReader plr = new PlayListReader(); ArrayList streamOfStrings = plr.readPlayList(filename); if (streamOfStrings == null) return null; // The result from the playlist reader must have at least three strings - // "playlist", the name of the playlist, its description if(streamOfStrings.size() < 3){ System.out.println("Insufficient tokens in the read stream"); return null; } // The first string must be "playlist" if (!((String) streamOfStrings.get(0)).equals("playlist")){ System.out.println("The first token is not \"playlist\""); return null; } // Get the name and the description of the playlist String plName = (String) streamOfStrings.get(1); String plDesc = (String) streamOfStrings.get(2); // FIX THIS!!!! Create your playlist using its name and description // Find the number of strings left - the number of tracks will be // a third of these int numStrings = streamOfStrings.size(); // Iterate over each track - each track has 4 strings // "track", the title of the track, the artist's name, // the playing time in seconds for(int tnum = 3; tnum < numStrings; tnum += 4){ // Check if the first string in this set is "track" if (!((String) streamOfStrings.get(tnum)).equals("track")) { System.out.println("The first token on line " + (tnum+1)/4 + " is not \"track\""); return null; } // Extract properties of the tracks and create a track object String trTitle = (String) streamOfStrings.get(tnum+1); String trArtist = (String) streamOfStrings.get(tnum+2); int trLength = Integer.parseInt((String) streamOfStrings.get(tnum+3)); // FIX THIS !!!! - Create your track and add it to the PlayList } // FIX THIS !!!! - Add the playlist created to the collection of the // JukeBox and return it too. return null; } }