1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
|
class Library{
public:
// Default constructor
Library(){};
// Destructor
~Library(){};
//Create a song with Song details
SongWrapper* createSong(string song, string artist, string album);
//removes the identifier
// Plays a song a number of times
unsigned int playSong(unsigned int numberOfPlays);
// Checks if the song exist
bool doesSongExist(string theSong);
// Checks if the ID of the song exist
bool doesIdentifierExist(int identifier);
// Gets the details of the song
string getSongDetails(SongWrapper* aSong);
// Adds song into the library
void addSongIntoLibrary(SongWrapper* aSong);
// Remove song given the song ID number
void removeSongFromLibrary(unsigned int identifier);
private:
// SongID = key,
unordered_map<int, SongWrapper*>librarySongs; // key = ID in the library
// SongID = value
unordered_map<string, int>libraryID; // key = Song name "Ex:Songname ArtistName AlbumName"
};
unsigned int Library::playSong(unsigned int numberOfPlays){
SongWrapper* aSong = new SongWrapper;
if(doesIdentifierExist(aSong->getID()) == false){
return 0;
}
aSong->setNumOfPlays(aSong->getNumOfPlays() + numberOfPlays);
return aSong->getNumOfPlays();
}
bool Library::doesIdentifierExist(int identifier){
if(librarySongs.find(identifier) == librarySongs.end()){
cout << "No song with identifier #" << identifier << " exist in your library." << endl;
return false;
}
return true;
}
bool Library::doesSongExist(string theSong){
if(libraryID.find(theSong) == libraryID.end()){
return false;
}
return true;
}
SongWrapper* Library::createSong(string song, string artist, string album){
SongWrapper* aSong = new SongWrapper;
aSong->setSongName(song);
aSong->setArtistName(artist);
aSong->setAlbum(album);
aSong->setNumOfPlays(0);
aSong->createID();
return aSong;
}
string Library::getSongDetails(SongWrapper* aSong){
string songString;
songString = aSong->getSongName() + aSong->getArtistName() + aSong->getAlbum();
return songString;
}
void Library::addSongIntoLibrary(SongWrapper* aSong){
string song,artist,album, theSong;
theSong = getSongDetails(aSong);
if(doesSongExist(theSong) == true){
return;
}
pair<int, SongWrapper*>songWrapperPointer (aSong->getID(), createSong(song,artist,album));
pair<string, int>songDetail (getSongDetails(aSong), aSong->getID());
librarySongs.insert(songWrapperPointer);
libraryID.insert(songDetail);
}
// This function takes a SongID and removes that song from the playlist.
// Since I have two maps I have to erase song in both maps given the songID
void Library::removeSongFromLibrary(unsigned int identifier){
librarySongs.erase(identifier);
libraryID.erase() // <--- Not sure how to implement this
}
|