LLDs - AtAnkit https://atankit.dev Ankit Arora's Resume Sat, 24 Jun 2023 23:08:45 +0000 en-US hourly 1 https://atankit.dev/wp-content/uploads/2023/06/cropped-Atankit-2-32x32.png LLDs - AtAnkit https://atankit.dev 32 32 Design A Music Player https://atankit.dev/design-a-music-player/?utm_source=rss&utm_medium=rss&utm_campaign=design-a-music-player https://atankit.dev/design-a-music-player/#respond Sat, 24 Jun 2023 23:08:02 +0000 https://ryancv-demo.bslthemes.com/?p=94 Problem Statement Design a Music Player, with a shuffle feature to play random songs without repetition We will design a...

The post Design A Music Player first appeared on AtAnkit.

]]>

Problem Statement

Design a Music Player, with a shuffle feature to play random songs without repetition

We will design a Music player with the following features

  1. Add a song to the music player.
  2. Play a particular song.
  3. Play a random song from music player

First, we define a structure to store the songs. Each song will have attributes like Song ID, Name, Singer and Genre, etc. There can be other attributes as well that can be used to add other feature like: sort and search later.

public enum Genre {

    POP,
    HIPHOP,
    ROCK,
    JAZZ,
    FOLK
}

@Getter
@AllArgsConstructor
public class Song {

    private int songId;
    private String singer;
    private String songName;
    private Genre genre;
}

To implement the music player we will use two data structures, Linked List and a queue.

public class MusicPlayer {

    ArrayList<Song> musicList;
    Queue<Song> musicQ;
    Random random;
    int totalSongCount;
    HashMap<Integer,Integer> songMap;

    public MusicPlayer() {
        System.out.println("Music Player Initialized");
        this.musicList = new ArrayList<>();
        this.musicQ = new LinkedList<>();
        this.random = new Random();
        this.totalSongCount = 0;
        this.songMap=new HashMap<>();
    }

Music list will contain all the songs present in our music player.

A separate queue, musicQ will act as the song queue for our system.

The variable totalSongCount will represent the count of songs that can be considered for random song search.

Once a song is found by a random song search, it will be removed from the list and added at the end of the list, we will decrease the count of totalSongCount

A simple function will be used to add songs on our music list. 

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;
    }

The index totalSongCount bifurcates the music list into two groups. The first one contains song which will be considered for random shuffle. All new songs are added to the first group.

To play a random song, we will generate a random number between 0 to totalSongCount - 1 Then we remove the song at that index from the list and then add to the music queue. We will add the song at the ending of arraylist. Also, we will decrement the totalSongCount, so that the group containing songs for random shuffle music decreases.

To play a song of a particular song ID we will keep track whether that song belongs to the first group or second. If it is of the first group, we will remove it from the first group and add it to the second. For this, we can use a function playSong which has additional variable addtoQ, which gives the functionality to either add in musicQ or play at that moment.

A function to play from the queue can be used, which plays songs from the queue. This function also checks the content of the queue and starts new song whenever a song ends if the queue is not empty.

We can create a function to close the music player which will empty the musicQ and will dissolve the second group to the first group.

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());
        }
    }


}

Finally, the main function to call the function of the above-implemented functions from MusicPlayer class.

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();

    }
}

There can be multiple other features that can be added here – 

  1. Search, sort and manage songs according to the genre.
  2. Add AI to create a playlist according to user mood.

The post Design A Music Player first appeared on AtAnkit.

]]>
https://atankit.dev/design-a-music-player/feed/ 0