In: Computer Science
C++ language:
Class Song
The class will have the following private attributes:
It will also have the following public member functions:
Playing *Title* by *Artist* *playtime* seconds
Implement the missing functions using the template provided.
Class MusicLibrary
MusicLibrary has the following private attributes
Note that mySongs is a pointer that will be used to initialize a dynamic array of songs. You need to handle this dynamic array correctly (e.g. shallow copies, memory deallocation etc.)
playList is a dynamic array of pointers to Songs in the mySongs array.
The class also includes the following public member functions:
This class is partially implemented. Complete the following:
If the Playlist is full, print the following error message
Could not add Song to PlayList. PlayList is full
If the integer denoting the position of a song in the MusicLibrary is invalid, print the following error message
Invalid song
A sample main file has been provided showing the utilization of the functions, as well as a sample input file.
IMPORTANT
main.cpp:
#include <iostream>
#include <string>
#include "Song.h"
#include "MusicLibrary.h"
using namespace std;
int main()
{
string filename;
int numsongs;
cout << "Enter number of Songs " << endl;
cin >> numsongs;
cout << "Enter filename with information about the songs" << endl;
cin >> filename;
MusicLibrary mylibrary(numsongs);
mylibrary.readSongsFromFile(filename);
mylibrary.playRandom();
for (int i = numsongs-1; i >= 0; i--) {
mylibrary.addSongToPlayList(i);
}
mylibrary.playPlaylist();
return 0;
}
musicLibrary.cpp:
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include "MusicLibrary.h"
MusicLibrary::MusicLibrary(int maxsongs)
{
// implement constructor
}
MusicLibrary::MusicLibrary(MusicLibrary& other)
{
// implement copy constructor
}
MusicLibrary::~MusicLibrary()
{
delete[] mySongs;
delete[] playList;
}
int MusicLibrary::getnumSongs()
{
return numSongs;
}
int MusicLibrary::getmaxSongs()
{
return maxSongs;
}
int MusicLibrary::getnumSongsPlayList()
{
return numSongsPlayList;
}
bool MusicLibrary::addSong(string title, string artist, string album, int year, int time)
{
if (numSongs == maxSongs) {
cout << "Could not add song to library. Library is full" << endl;
return false;
}
mySongs[numSongs].setTitle(title);
mySongs[numSongs].setArtist(artist);
mySongs[numSongs].setAlbum(album);
mySongs[numSongs].setYear(year);
mySongs[numSongs].setPlayTime(time);
numSongs++;
return true;
}
bool MusicLibrary::addSong(Song& song)
{
if (numSongs == maxSongs) {
cout << "Could not add Ssong to library. Library is full" << endl;
return false;
}
mySongs[numSongs] = song;
numSongs++;
return true;
}
void MusicLibrary::readSongsFromFile(string filename)
{
ifstream input;
input.open(filename);
bool cont = true;
if (input.is_open()) {
string line;
while ( getline(input, line) && cont ) {
string title, artist, album;
string s_year, s_time;
int year;
int time;
istringstream inSS(line);
getline(inSS, title, ',');
getline(inSS, artist, ',');
getline(inSS, album, ',');
getline(inSS, s_year, ',');
getline(inSS, s_time);
year = stoi(s_year);
time = stoi(s_time);
cont = addSong(title, artist, album, year, time);
};
}
else {
cout << "could not open file " << filename << endl;
}
}
void MusicLibrary::playRandom()
{
// implement this method
}
bool MusicLibrary::addSongToPlayList(int pos)
{
// implement this method
}
void MusicLibrary::playPlaylist()
{
// implement this method
}
musicLibrary.h:
#pragma once
#include <string>
#include "Song.h"
using namespace std;
class MusicLibrary
{
private:
int maxSongs;
int numSongs; // number of Songs in library
Song* mySongs; // dynamic array storing all Songs
int numSongsPlayList; // number of Songs in Playlist
Song** playList; // dynamic array of pointers to Songs
public:
MusicLibrary(int maxsongs);
MusicLibrary(MusicLibrary& other);
~MusicLibrary();
int getnumSongs();
int getmaxSongs();
int getnumSongsPlayList();
bool addSong(string title, string artist, string album, int year, int time);
bool addSong(Song& song);
void readSongsFromFile(string filename);
bool addSongToPlayList(int pos);
void playRandom();
void playPlaylist();
};
song.cpp:
#include <iostream>
#include "Song.h"
void Song::Play()
{
cout << "Playing "<< Title << " by " << Artist << " " << PlayTime << " seconds" << endl;
}
// Add code for constructors, accessors, mutators, and == operator.
song.h:
#pragma once
#include <string>
using namespace std;
class Song {
private:
string Title;
string Artist;
string Album;
int Year;
int PlayTime;
public:
// Add declaration for constructors, accessors and mutators
// Add declaration for overloading the == operator
void Play();
};
songs.txt:
Zoo Station, U2, Achtung Baby, 1991, 203
Youngblood, 5 Seconds of Summer, Youngblood, 2018, 311
Money for Nothing, Dire Straits, Brothers in Arms, 1986, 501
Summer of 69, Bryan Adams, Reckless, 1984, 178
Livin on a Prayer, Bon Jovi, Slippery when Wet, 1986, 241
Program code to copy
main.cpp
#include <iostream>
#include <string>
#include "Song.h"
#include "MusicLibrary.h"
using namespace std;
int main()
{
string filename;
int numsongs;
cout << "Enter number of Songs "
<< endl;
cin >> numsongs;
cout << "Enter filename with
information about the songs" << endl;
cin >> filename;
MusicLibrary mylibrary(numsongs);
mylibrary.readSongsFromFile(filename);
mylibrary.playRandom();
for (int i = numsongs-1; i >= 0;
i--)
{
mylibrary.addSongToPlayList(i);
}
mylibrary.playPlaylist();
return 0;
}
===================================================================================
MusicLibrary.cpp
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <cstring>
#include <cstdlib>
#include "MusicLibrary.h"
//Implement a constructor taking the number of songs as an
argument.
MusicLibrary::MusicLibrary(int maxsongs)
{
maxSongs = maxsongs;
numSongs = 0;
numSongsPlayList = 0;
mySongs = new Song[maxSongs];
playList = new Song*[maxSongs];
}
//Implement a copy constructor.
MusicLibrary::MusicLibrary(MusicLibrary& other)
{
this->maxSongs = other.maxSongs;
this->numSongs = other.numSongs;
this->numSongsPlayList =
other.numSongsPlayList;
mySongs = new Song[maxSongs];
playList = new Song*[maxSongs];
}
MusicLibrary::~MusicLibrary()
{
delete[] mySongs;
delete[] playList;
}
int MusicLibrary::getnumSongs()
{
return numSongs;
}
int MusicLibrary::getmaxSongs()
{
return maxSongs;
}
int MusicLibrary::getnumSongsPlayList()
{
return numSongsPlayList;
}
bool MusicLibrary::addSong(string title, string artist, string
album, int year, int time)
{
if (numSongs == maxSongs)
{
cout << "Could not add song
to library. Library is full" << endl;
return false;
}
mySongs[numSongs].setTitle(title);
mySongs[numSongs].setArtist(artist);
mySongs[numSongs].setAlbum(album);
mySongs[numSongs].setYear(year);
mySongs[numSongs].setPlayTime(time);
numSongs++;
return true;
}
bool MusicLibrary::addSong(Song& song)
{
if (numSongs == maxSongs)
{
cout << "Could not add Ssong
to library. Library is full" << endl;
return false;
}
mySongs[numSongs] = song;
numSongs++;
return true;
}
void MusicLibrary::readSongsFromFile(string filename)
{
ifstream input;
input.open(filename.c_str());
bool cont = true;
if (input.is_open())
{
string line;
while (getline(input, line)
&& cont)
{
string title,
artist, album;
string s_year,
s_time;
int year;
int time;
istringstream
inSS(line);
getline(inSS,
title, ',');
getline(inSS,
artist, ',');
getline(inSS,
album, ',');
getline(inSS,
s_year, ',');
getline(inSS,
s_time);
year =
atoi(s_year.c_str());
time =
atoi(s_time.c_str());
cont =
addSong(title, artist, album, year, time);
}
}
else
{
cout << "could not open file
" << filename << endl;
}
}
/*
*Implement the public function 'playRandom' which plays all songs
of the library
*/
void MusicLibrary::playRandom()
{
for (int i = 0; i < numSongs / 2; i++)
{
mySongs[i].Play();
mySongs[numSongs - 1 -
i].Play();
}
if ((numSongs % 2) != 0)
{
mySongs[numSongs / 2].Play();
}
}
/*
*Implement the public function addSongToPlayList which stores a
pointer to a song in the library in the array of Song
pointer.
*/
bool MusicLibrary::addSongToPlayList(int pos)
{
if (numSongsPlayList == maxSongs)
{
cout << "Could not add Song
to PlayList. PlayList is full" << endl;
return false;
}
if ((pos < 0) || (pos >= numSongs))
{
cout << "Invalid song"
<< endl;
return false;
}
playList[numSongsPlayList] =
&mySongs[pos];
numSongsPlayList++;
return true;
}
//Implement the public function 'playPlaylist' which plays the
songs in the playlist in the same order as the songs have been
added to the playlist.
void MusicLibrary::playPlaylist()
{
for (int i = 0; i < numSongsPlayList; i++)
{
playList[i]->Play();
}
}
===================================================================================
MusicLibrary.h
#pragma once
#include <string>
#include "Song.h"
using namespace std;
class MusicLibrary
{
private:
int maxSongs;
int
numSongs;
//number of Songs in library
Song*
mySongs; //dynamic
array storing all Songs
int numSongsPlayList; //number of Songs in
Playlist
Song** playList;
//dynamic array of pointers to Songs
public:
MusicLibrary(int maxsongs);
MusicLibrary(MusicLibrary& other);
~MusicLibrary();
int getnumSongs();
int getmaxSongs();
int getnumSongsPlayList();
bool addSong(string title, string artist, string
album, int year, int time);
bool addSong(Song& song);
void readSongsFromFile(string filename);
bool addSongToPlayList(int pos);
void playRandom();
void playPlaylist();
};
===================================================================================Song.cpp
#include <iostream>
#include "Song.h"
//Default constructor setting the playtime to 0 and year to
0.
Song::Song()
{
Title = "";
Artist = "";
Album = "";
Year = 0;
PlayTime = 0;
}
//A constructor using Title, Artist, Album, Year, and PlayTime
as arguments.
Song::Song(string Title, string Artist, string Album, int Year, int
PlayTime)
{
this->Title = Title;
this->Artist = Artist;
this->Album = Album;
this->Year = Year;
this->PlayTime = PlayTime;
}
//Accessors and mutators for all private attributes.
void Song::setTitle(string Title)
{
this->Title = Title;
}
void Song::setArtist(string Artist)
{
this->Artist = Artist;
}
void Song::setAlbum(string Album)
{
this->Album = Album;
}
void Song::setYear(int Year)
{
this->Year = Year;
}
void Song::setPlayTime(int PlayTime)
{
this->PlayTime = PlayTime;
}
string Song::getTitle()
{
return Title;
}
string Song::getArtist()
{
return Artist;
}
string Song::getAlbum()
{
return Album;
}
int Song::getYear()
{
return Year;
}
int Song::getPlayTime()
{
return PlayTime;
}
//A void function 'Play' that will print out the information
about the song in the following format (provided)
void Song::Play()
{
cout << "Playing "<< Title << " by "
<< Artist << " " << PlayTime << " seconds"
<< endl;
}
//A comparison operator == that uses title, artist and album
(but not year and playtime) for comparing two songs.
bool Song::operator==(const Song& other)
{
if (Title.compare(other.Title) == 0)
{
if (Artist.compare(other.Artist) ==
0)
{
if
(Album.compare(other.Album) == 0)
{
return true;
}
else
{
return false;
}
}
else
{
return
false;
}
}
else
{
return false;
}
}
===================================================================================
Song.h
#pragma once
#include <string>
using namespace std;
class Song {
private:
string Title;
string Artist;
string Album;
int Year;
int PlayTime;
public:
Song();
Song(string Title, string Artist, string Album, int
Year, int PlayTime);
void setTitle(string Title);
void setArtist(string Artist);
void setAlbum(string Album);
void setYear(int Year);
void setPlayTime(int PlayTime);
string getTitle();
string getArtist();
string getAlbum();
int getYear();
int getPlayTime();
void Play();
bool operator==(const Song& other);
};
===================================================================================songs.txt
Zoo Station, U2, Achtung Baby, 1991, 203
Youngblood, 5 Seconds of Summer, Youngblood, 2018, 311
Money for Nothing, Dire Straits, Brothers in Arms, 1986, 501
Summer of 69, Bryan Adams, Reckless, 1984, 178
Livin on a Prayer, Bon Jovi, Slippery when Wet, 1986, 241
===================================================================================Program Screenshot
Main.cpp
================================================================================
MusicLibrary.cpp
===================================================================================
MusicLibrary.h
===================================================================================
Songs.cpp
================================================================================
Song.h
==================================================================================
songs.txt
===================================================================================Sample output