In: Computer Science
USE C++
Declare a Song struct to store the data for a single song (make it a private member), and you must have anarray of Song structures as a member variable.
Player(string name, float size) a constructor
for the class. ’name' is a name for it
'size' is a maximum capacity in Mb.
addSong(string band, string title, string length, float size) a function for adding a new song.
'band' is a name of the band
'title' is a name of the song
'length' is a time length of that song in a '1:23' format (“mm:ss")
'size' is a size of the song in Mb
Return true if the song was added and false if there was not enough space (memory) for it or there was a song with the same band and title already in that device or if the device already has 1000 songs in it.
// CODE
#include
#include
#include
using namespace std;
class Player {
public:
Player(string name, float size) {
this->name =
name;
this->size_allowed =
size; // this is the max Mb allowed to store
this->current_size =
0; // this is the Mb that is currently filled
this->current_songs_count = 0; // songs currently present
}
bool addSong(string band, string title, string
length, float size) {
Song s; // first create
a song structure for the data given
s.band = band;
s.title = title;
s.length = length;
s.size = size;
// now check if the
number of songs limit is reached
if(current_songs_count
> MAX_SONGS_ALLOWED) { // if the songs exceeds limit
return false; // return false
}
if(current_size+s.size
> size_allowed) { // if adding the song exceeds the memory
limit
return false; // return false
}
// check if there any
duplicat of the current song exists
for(int i = 0; i <
current_songs_count; i++) {
if((songs[i].title == s.title) && (songs[i].band ==
s.band)) {
return false;
}
}
songs[current_songs_count] = s; // add song to the player
current_songs_count++;
// update the number of songs
current_size += s.size;
// add the size of the song to the player's size
return true;
}
// additional method to print the stats
void printStats() {
cout<<"************************************"<
private:
typedef struct Song {
string band;
string title;
string length;
float size;
} Song;
static const int MAX_SONGS_ALLOWED = 1000;
int size_allowed;
int current_size;
int current_songs_count;
Song songs[MAX_SONGS_ALLOWED];
string name;
};
int main() {
Player p("My Music Player", 1000); // create a
music player with 1Gb space
p.printStats();
cout<
p.printStats();
return 0;
}
// OUTPUT