Learn-dsa..in 30 days!



























CC-19 : Simulate a Song Playlist using CircularDoubleLinkedList.

Description:

Write and test code to simulate a Songs playlist. The Playlist should initially have 5 songs. User can add a song at the end of the playlist. User can also move to next or previous song in the play list.

Test cases and expected outputs:

Input Parameters Expected outputs
Music Playlist Simulation menu :
1: Add a new song to playlist
2: Move to next song in playlist
3: Move to previous song in playlist
4: Show current music playlist
5: Exit Music Playlist Simulation program

Choose an option:
1
Current Music Playlist: Song1 (last node points to first node)
Choose an option:
1
Current Music Playlist: Song1 <-> Song2 (last node points to first node)
Choose an option:
1
Current Music Playlist: Song1 <-> Song2 <-> Song3 (last node points to first node)
Choose an option:
2
Current Music Playlist: Song1(Current Song) <-> Song2 <-> Song3 (last node points to first node)
Choose an option:
2
Current Music Playlist: Song1 <-> Song2(Current Song) <-> Song3 (last node points to first node)
Choose an option:
2
Current Music Playlist: Song1 <-> Song2 <-> Song3(Current Song) (last node points to first node)
Choose an option:
3
Current Music Playlist: Song1 <-> Song2(Current Song) <-> Song3 (last node points to first node)

Pseudocode:

Create a CircularDoubleLinkedListNode class with String Data (instead of integer data that was used in above examples, song names are stored as node data values).
Initialize a CircularDoubleLinkedList with list of 5 songs initially.
To allow user to add a new Song node into playlist use CircularDoubleLinkedList “addNode()” method.
To give user Option to delete current Song node from playlist, use CircularDoubleLinkedList method “removeNode()”.
Use CircularDoubleLinkedListNode methods “getNextNode()” and “getPreviousNode()” to go to previous or next nodes.

Code:

public  void circularDoubleLinkedListMusicPlaylist() throws Exception{
	
	DoubleLinkedListNode currentSongInPlaylist=null;
	int newSongCounter=0;
	while (true) {
	try {
		System.out.println("\n\n Music Playlist Simulation menu :");
		System.out.println();
		System.out.println("1: Add a new song to playlist");
		System.out.println("2: Move to next song in playlist");
		System.out.println("3: Move to previous song in playlist");
		System.out.println("4: Show current music playlist");
		System.out.println("5: Exit Music Playlist Simulation program");
		System.out.println();
		System.out.println("Choose an option:\n");
		int selectedMenuOption=readIntegerFromConsole();
		switch(selectedMenuOption) {
		case 1://Add a new song
			newSongCounter++;
			// Handle case of new song being first or last song in the Music Playlist
			musicPlaylist.addLast(newSongCounter);
			printMusicPlaylist(musicPlaylist,currentSongInPlaylist);
			break;		
		case 2: //Play next song in playlist
			if (musicPlaylist.getFirstNode()==null) 
			{System.out.print("\n\n Cannot move to next song; playlist is empty \n"); 
			continue;}
			if (currentSongInPlaylist==null) {
				currentSongInPlaylist=musicPlaylist.getFirstNode();
				printMusicPlaylist(musicPlaylist,currentSongInPlaylist);
				continue;
			}else {
				currentSongInPlaylist=currentSongInPlaylist.getNextNode();		
				printMusicPlaylist(musicPlaylist,currentSongInPlaylist);
			}
		    break;
		  case 3: //Move To previous song in playlist
			if (musicPlaylist.getFirstNode()==null) {
				System.out.print("\n\n Cannot move to previoius song; playlist is empty \n"); 
				continue;}
			if (currentSongInPlaylist==null) {
				currentSongInPlaylist=musicPlaylist.getFirstNode();
				printMusicPlaylist(musicPlaylist,currentSongInPlaylist);
				continue;
			}else {
				currentSongInPlaylist=currentSongInPlaylist.getPreviousNode();		
				printMusicPlaylist(musicPlaylist,currentSongInPlaylist);
			}
		    break;
		  case 4: //Show current music playlist
			printMusicPlaylist(musicPlaylist,currentSongInPlaylist);
		    break;
		 case 5: //Exit the program
			System.out.println("\nExiting program....\n");
			return;
		default: //user has input an option that is not available
			System.out.print("\n Invalid option selected; please use one of the options given above: ");
			break;}
		}catch (Exception exception) {
			System.out.print("Exception: "+ exception);
			continue;
		}
		}
}
}

Click here to download and run code and test cases !