//This class models a collection of songs to be played
//in a particular order
public class Playlist{
private final int MAXIMUM_PLAYLIST_SIZE = 24;
private String playlistName;
private Song[] songs = new Song[MAXIMUM_PLAYLIST_SIZE]; //array to store song objects
private int numberOfSongs = 0; //the number of actual song objects in the collection
public Playlist(String listName){ //Constructor
playlistName = listName;
}
public int getNumberOfSongs(){
//answer the number of bank accounts in the bank
return numberOfSongs;
}
public void addSong(Song aSong){
//add a song if the playlist is not full
if(aSong == null) return; //don't add a null song
//check that there is room in the playlist and if so add the song
if(numberOfSongs < MAXIMUM_PLAYLIST_SIZE) {
//add aSong to the playlist
songs[numberOfSongs] = aSong;
numberOfSongs++;
}
}
public void exchangeSongsByPosition(int i, int j){
//This method exchanges the song in position i with the song in position j.
//positions count from 0. If either i or j is out of range the method
//does nothing (This method is complete)
if(i< 0 || i >= songs.length) return; //i is out of range
if(j< 0 || j >= songs.length) return; //j is out of range
//exchange the songs
Song temp = songs[i];
songs[i] = songs[j];
songs[j] = temp;
}
public void printPlaylist(){
//print the playlist and a summary of the playlist
//This method should list all the songs and their position (see sample output)
//The summary includes the number of songs and
//the total playing time of the playlist shown in HH:MM:SS format.
//NOTE: You don’t have to print "07" for 7, or "00" for 0 etc.
int totalSeconds = 0, totalMinutes = 0;
for(int i=0; i<numberOfSongs; i++){
	System.out.println("[" + i + "] " + songs[i].toString());
	totalSeconds+=songs[i].getSeconds();
	totalMinutes+=songs[i].getMinutes();
}
int sec = totalSeconds%60;
totalMinutes+=(totalSeconds/60);
int min = totalMinutes%60;
int hours = totalMinutes/ 60;
System.out.print("\nSUMMARY:  " + numberOfSongs + " songs. Total time: ");
if (hours<10)
	System.out.print("0");
System.out.print(hours + ":");
if (min<10)
	System.out.print("0");
System.out.print(min + ":");
if (sec<10)
	System.out.print("0");
System.out.print(sec);
}
public int findPositionOfSong(String aSongTitle){
//Return the position in the playlist (counting from 0) of the
//song with title: aSongTitle. If the song cannot be found return -1.
for(int i=0; i<numberOfSongs; i++){
	if(aSongTitle.equals(songs[i].getTitle()))
		return i;
}
return -1;
}
public void swap(String title1, String title2){
//Swap, or exchange, two songs in the playlist.
//Exchange the song with title title1 with the song with title title2
//in the playlist.
//If either song cannot be found then do nothing
exchangeSongsByPosition(findPositionOfSong(title1), findPositionOfSong(title2));
}
public void moveSongDown(String aSongTitle){
//Move the song with title aSongTitle down one position in the list.
//That is, exchange the song with the one below it.
//If song aSongTitle is the last one in list then the method should do nothing.
if(findPositionOfSong(aSongTitle)==numberOfSongs-1)
	return;
exchangeSongsByPosition(findPositionOfSong(aSongTitle), findPositionOfSong(aSongTitle) + 1);
}
public void moveSongUp(String aSongTitle){
//Move the song with title aSongTitle up one position in the list.
//That is, exchange the song with the one above it.
//If song aSongTitle is the first one in list then the method should do nothing.
if(findPositionOfSong(aSongTitle)==0)
	return;
exchangeSongsByPosition(findPositionOfSong(aSongTitle), findPositionOfSong(aSongTitle) -1);
}
} //end class Playlist