Skip to content

Instantly share code, notes, and snippets.

@prakhargurawa
Created January 19, 2020 08:14
Show Gist options
  • Save prakhargurawa/979c333104a3e98bda12bdedd0ac00dd to your computer and use it in GitHub Desktop.
Save prakhargurawa/979c333104a3e98bda12bdedd0ac00dd to your computer and use it in GitHub Desktop.
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Random;
import java.util.Queue;
enum Genre
{
POP,
HIPHOP,
JAZZ,
FOLK,
ROCK
}
class Song
{
int songID;
String singer;
String song_name;
Genre type;
Song(int ID,String singer,String songname,Genre type)
{
this.songID=ID;
this.singer=singer;
this.song_name=songname;
this.type=type;
}
}
class MusicPlayer {
ArrayList<Song> musiclist;
Queue<Song> musicQ;
int total_Song_Count;
Random rand;
MusicPlayer()
{
System.out.println("<<<<<< Initialize Music Player >>>>>>");
musiclist=new ArrayList<Song>();
rand = new Random();
musicQ=new LinkedList<Song>();
total_Song_Count=0;
}
void addSong(int ID,String singer,String songname,Genre type)
{
musiclist.add(total_Song_Count,new Song(ID,singer,songname,type));
total_Song_Count++;
}
void playRandomSong()
{
int rand_int = rand.nextInt(total_Song_Count);
Song playingSong=musiclist.remove(rand_int);
total_Song_Count--;
musicQ.add(playingSong);
System.out.println("The song : "+playingSong.song_name+" is been added to playing queue.");
musiclist.add(playingSong);
}
void playSong(int ID,Boolean addtoQ)
{
for(int i=0;i<total_Song_Count;i++)
{
Song x=(Song)musiclist.get(i);
if(x.songID==ID)
{
musiclist.remove(i);
total_Song_Count--;
if(addtoQ)
{
System.out.println("The song : "+x.song_name+" is been added to playing queue.");
musicQ.add(x);
}
else
{
System.out.println("The song : "+x.song_name+" is currently played.");
}
musiclist.add(x);
return;
}
}
for(int i=total_Song_Count;i<musiclist.size();i++)
{
Song x=(Song)musiclist.get(i);
if(x.songID==ID)
{
if(addtoQ)
{
System.out.println("The song : "+x.song_name+" is been added to playing queue.");
musicQ.add(x);
}
else
{
System.out.println("The song : "+x.song_name+" is currently played.");
}
return;
}
}
return;
}
void closeMusicPlayer()
{
System.out.println("<<<<<< Closing Music Player >>>>>>");
musicQ.clear();
total_Song_Count=musiclist.size();
}
void playMusicFromQueue()
{
//Play the song in the front of queue and remove it from the queue,
//if queue is empty wait till songs come in the queue
System.out.print("The current queue contains : [ ");
for(Song s:musicQ)
{
System.out.println(s.song_name+" ");
}
System.out.println("]");
}
}
public class MusicSystem {
public static void main(String[] args) {
// TODO Auto-generated method stub
MusicPlayer mp=new MusicPlayer();
mp.addSong(1,"Maroon5","Memories",Genre.POP);
mp.addSong(2,"Queen","Bohemian Rhapsody",Genre.ROCK);
mp.addSong(3,"Led Zeppelin","Stairway to Heaven",Genre.ROCK);
mp.addSong(4, "Lee Morgan", "Coera", Genre.JAZZ);
mp.addSong(5,"BennyGoodman","Sing Sing Sing",Genre.JAZZ);
mp.playMusicFromQueue();
mp.playRandomSong();
mp.playRandomSong();
mp.playRandomSong();
mp.playSong(3,true);
mp.playSong(5,false);
mp.playMusicFromQueue();
mp.closeMusicPlayer();
mp.playRandomSong();
mp.playRandomSong();
mp.playRandomSong();
mp.playRandomSong();
mp.playRandomSong();
mp.playMusicFromQueue();
}
}
@aprabakar
Copy link

Good Approach !!!

Few things to note:

To play every song ( Particular song ) , It will take O( PlayListSize).
To Play Every Random Song , It will take again O(PlaylistSize) , Because while removing , you are shifting the entire Array Elements.
when you say “Don’t Repeat” , You don’t have to repeat a song untill all the songs in the play list are played . Once you have played all the songs randomly from the playlist , You can start fresh. But In your Implementation , there is no support to Choose a random song again once all the songs in the list is played.

How can we Improve the Time Complexity?

To overcome this problem ( Choosing Particular Song ) you can have additional Storage HashMap ( key — Song Id , Value -> Song location ), But I understand it will introduce Memory Complexity , But Always Time Complexity should be given Importance.
To overcome this ( Choosing Random Song) . Instead of removing that song and insert it from the last ( Which requires shift the entire list), You can swap the last element[Total_Song_Count] with the current random element and update the same in the HashMap . 
For Ex: List → [1,2,3,4] , Map — [ 1,0] , [2,1],[3,2],[4,3]
- Now Random song is 2
- Swap the last element with 2 -> [1,4,3,2] , Decrease the total count -> 3
- Update the HashMap -> [1,0][2,3][3,2][4,1]
The above set of Operation will take O(1) Complexity for sure.
Note: You don’t need to do this operation when Total_Song_Count is one.
To Overcome this one , You can provide support by Re-updating Total_Song_Count in PlayRandom function to PlayList Size when it becomes zero.

Any thoughts on this???

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment