Genre.java
public enum Genre {
POP,
HIPHOP,
ROCK,
JAZZ,
FOLK
}Song.java
public class Song {
private int songId;
private String singer;
private String songName;
private Genre genre;
//constructor
public Song(int songId, String singer, String songName, Genre genre) {
this.songId = songId;
this.singer = singer;
this.songName = songName;
this.genre = genre;
}
//getter and setter
}MusicPlayer.java (Main class)
public class MusicPlayer {
ArrayList<Song> musicList;
Queue<Song> musicQ;
Random random;
int totalSongCount;
HashMap<Integer,Integer> songMap;
public MusicPlayer() {
System.out.println("<<<<<<<<Initialize Music Player>>>>>>");
this.musicList = new ArrayList<>();
this.musicQ = new LinkedList<>();
this.random = new Random();
this.totalSongCount = 0;
this.songMap=new HashMap<>();
}
public Song addSong(int id, String singer, String songName,Genre genre)
{
if(songMap.containsKey(id))
{
throw new RuntimeException("Song is already present in the list");
}
Song song=new Song(id,singer,songName,genre);
musicList.add(song);
songMap.put(id,totalSongCount);
totalSongCount++;
return song;
}
public void playRandomSong()
{
if(totalSongCount==1)
{
totalSongCount=musicList.size();
}
int rand_int=random.nextInt(totalSongCount);
System.out.println("Total count "+totalSongCount);
System.out.println("random count "+rand_int);
Song playingSong=musicList.get(rand_int);
totalSongCount--;
swap(musicList.get(rand_int),musicList.get(totalSongCount));
musicQ.add(playingSong);
System.out.println("The song "+playingSong+" added to the queue");
songMap.put(musicList.get(totalSongCount).getSongId(),totalSongCount);
songMap.put(musicList.get(rand_int).getSongId(),rand_int);
}
void swap(Song a,Song b)
{
Song temp=a;
a=b;
b=temp;
}
public void playSong(int id,boolean addToQ)
{
int loc=songMap.get(id);
Song song=musicList.get(loc);
if(addToQ)
{
if(loc<totalSongCount)
{
totalSongCount--;
swap(musicList.get(loc),musicList.get(totalSongCount));
musicQ.add(song);
System.out.println("The song "+song+" added to the queue");
songMap.put(musicList.get(totalSongCount).getSongId(),totalSongCount);
songMap.put(musicList.get(loc).getSongId(),loc);
}else {
musicQ.add(song);
System.out.println("The song "+song+" added to the queue");
}
}else{
System.out.println("The song "+song+" is playing");
}
}
public void closePlayer()
{
System.out.println("<<<<<<<CLOSING PLAYER>>>>>>>");
musicQ.clear();
totalSongCount=musicList.size();
}
public void playMusicFromQueue()
{
System.out.println("Playing songs from queue ::->");
for(Song s:musicQ)
{
System.out.println("Playing song from queue ::-> "+s.getSongName());
}
}
}
MusicSystem.java
public class MusicSystem {
public static void main(String[] args) {
MusicPlayer musicPlayer=new MusicPlayer();
musicPlayer.addSong(1,"sia","thunderclouds",Genre.ROCK);
musicPlayer.addSong(2,"Praaaz","Arcade",Genre.POP);
musicPlayer.addSong(3,"billie","everything I wanted",Genre.JAZZ);
musicPlayer.addSong(4,"dua lipa","levitating",Genre.POP);
musicPlayer.addSong(5,"dua lipa","broke my heart",Genre.POP);
musicPlayer.addSong(6,"billie","therefore I am",Genre.JAZZ);
musicPlayer.addSong(8,"dua lipa","IDGAF",Genre.POP);
musicPlayer.addSong(9,"billie","sugar and brownies",Genre.JAZZ);
musicPlayer.playMusicFromQueue();
musicPlayer.playRandomSong();
musicPlayer.playRandomSong();
musicPlayer.playRandomSong();
musicPlayer.playSong(3,true);
musicPlayer.playSong(6,false);
musicPlayer.playMusicFromQueue();
musicPlayer.closePlayer();
musicPlayer.playRandomSong();
musicPlayer.playRandomSong();
musicPlayer.playRandomSong();
musicPlayer.playMusicFromQueue();
}
}