CMPE 261 - Lab worksheet - Week 5

Problem: Music library

In this exercise you are expected to write classes to represent a music library. A song in the library must provide methods to return the song's title, and artist's name, as dictated in the SongInterface below. The library itself must hold a list of songs, and must provide methods to add or remove a song, and list the songs, by supporting the MusicLibraryInterface below.

    interface SongInterface {
        public String getTitle();
        public String getArtist();
    }

    interface MusicLibraryInterface {
        public void addSong(String songName, String artistName);

        /** throws an exception if the song cannot be found */
        public void removeSong(String songName) throws Exception;

        /** throws and exception if the song cannot be found */
        public String getArtistNameForSongName(String songName) throws Exception;
        public ArrayList<SongInterface> getSongs();
    }

HINT: You can use ArrayList class from java.util library to store a mutable list of songs, for example:

    import java.util.ArrayList;
    class Song implements SongInterface { ... }
    class MusicLibrary implements MusicLibraryInterface {
        ArrayList<SongInterface> songs=new ArrayList<SongInterface>();
        ...
    }

The songs object then can be manipulated or used in for loops as below:

    songs.add(new Song("get lucky","daft punk");
    for(SongInterface s: songs)
        if (s.getTitle().equals("get lucky"))
            songs.remove(s);