C++ While I am providing the whole context. I only need help with writing the cl
ID: 3748174 • Letter: C
Question
C++
While I am providing the whole context. I only need help with writing the class PLaylist( Playlist.h and playlist.cpp. )
It is important that you please show me the code for the .h file and .cpp file for the playlist classs..
Here is the whole context just in case you need it.
THank you
Design a simple PlayList Program
Class Set
You are given an interface: SetInterface.h
This is a public interface, and completely specifies what the Set class operations must be.
SetInterface is an abstract class (it has no implementation), so your Set class must inherit from SetInterface and implement all of its methods.
Set does not allow duplicates. This means that it does not have a getFrequencyOf()method because an item can be in the Set only once. This also means that add()must check that an element is not in Set already, if it is the item will not be added.
Here is the SetInterface:
__________________________________________________________________________________
#ifndef SET_INTERFACE_H_
#define SET_INTERFACE_H_
#include <vector>
template<class ItemType>
class SetInterface
{
public:
/** Gets the current number of entries in this set.
@return The integer number of entries currently in the set. */
virtual int getCurrentSize() const = 0;
/** Checks whether this set is empty.
@return True if the set is empty, or false if not. */
virtual bool isEmpty() const = 0;
/** Adds a new entry to this set.
@post If successful, newEntry is stored in the set and
the count of items in the set has increased by 1.
@param newEntry The object to be added as a new entry.
@return True if addition was successful, or false if not. */
virtual bool add(const ItemType& newEntry) = 0;
/** Removes a given entry from this set,if possible.
@post If successful, anEntry has been removed from the set
and the count of items in the set has decreased by 1.
@param anEntry The entry to be removed.
@return True if removal was successful, or false if not. */
virtual bool remove(const ItemType& anEntry) = 0;
/** Removes all entries from this set.
@post set contains no items, and the count of items is 0. */
virtual void clear() = 0;
/** Tests whether this set contains a given entry.
@param anEntry The entry to locate.
@return True if set contains anEntry, or false otherwise. */
virtual bool contains(const ItemType& anEntry) const = 0;
/** Fills a vector with all entries that are in this set.
@return A vector containing all the entries in the set. */
virtual std::vector<ItemType> toVector() const = 0;
}; // end SetfInterface
#endif /* SET_INTERFACE_H_ */
______________________________________________________________________________________________________
Other than the methods specified by the SetInterface, your Set class, similarly to the Bag class, will also have some private data members:
static const int DEFAULT_SET_SIZE = 4; // for testing purposes we will keep the set small
ItemType items_[DEFAULT_SET_SIZE]; // array of set items
int item_count_; // current count of set items
int max_items_; // max capacity of the set
Set also has a private member function (a helper function) other than the public methods specified by SetInterface.h
It is used by some of its' public methods to find out where a particular item is
// post: Either returns the index of target in the array items_
// or -1 if the array does not contain the target
int getIndexOf(const ItemType& target) const;
Class Song
You will also code your own Song class, so you can add Songs to your playlist. A Song stores the following information (private member variables):
std::string title_;
std::string author_;
std::string album_;
And the following operations (public member functions)
Song();
Song(const std::string& title, const std::string& author = "", const std::string& album = "");
void setTitle(std::string title); //"set" in setTitle here means "give a value" and has nothing
// to do with the Set class. Similarly for setAuthor and setAlbum
void setAuthor(std::string author);
void setAlbum(std::string album);
std::string getTitle() const;
std::string getAuthor() const;
std::string getAlbum() const;
friend bool operator==(const Song& lhs, const Song& rhs);
Since you are the best programmer in town I trust you will do a great job at commenting these thoroughly!!!
Class PlayList
Your PlayList class will have a single data member (private member variable), a Set object called playlist_ which will store your Songs:
Set<Song> playlist_;
Your PlayList will have the following operations (public member functions)
PlayList();
PlayList(const Song& a_song);
int getNumberOfSongs() const;
bool isEmpty() const;
bool addSong(const Song& new_song);
bool removeSong(const Song& a_song);
void clearPlayList();
void displayPlayList() const;
You will notice that the Set ADT did serve you well. Most of the things you want to do to your PlayList are functionalities your Set ADT provides, so all of these functions will essentially be making calls to the corresponding Set public members... good design goes a long way!!!
Once again, since you are the best programmer in town I trust you will do a great job at commenting these thoroughly!!!
A few things to notice:
The parameterized constructor needs to add a_song to playlist_
Depending on your design/implementation, when removing an item, Set may or may not have preserved an order for the remaining items because in a Set THERE IS NO ORDER. In a PlayList, on the other hand, you may like to have Songs play in a certain order. Don't worry about that for now, although it may not be the most desirable behavior, you can simply remove a Song from the PlayList by calling the Set::remove() member function no matter how you implemented it. We will take care of this in Project 3.
displayPlayList() will take advantage of Set::toVector() to access and display to the console (cout) data for all the songs in playlist_ . For each song it will display: * Title: title * Author: author * Album: album * on a new line, where the text in red should be replaced by the value of the corresponding Song member variable. After displaying data for all the songs it will display: End of playlist on a new line.
Usage:
Make sure your code compiles and runs correctly with this main() function:
int main() {
//**********Test Song************//
//instantiate 5 songs
Song song1;
song1.setTitle("title 1");
song1.setAuthor("author 1");
song1.setAlbum("album 1");
Song song2("title 2", "author 2", "album 2");
Song song3("title 3", "author 3", "album 3");
Song song4("title 4", "author 4", "album 4");
Song song5("title 5", "author 5", "album 5");
//output song information
cout << "The first song is: " << song1.getTitle() << ", " << song1.getAuthor() << ", " << song1.getAlbum() << endl;
cout << "The second song is: " << song2.getTitle() << ", " << song2.getAuthor() << ", " << song2.getAlbum() << endl;
cout << "The third song is: " << song3.getTitle() << ", " << song3.getAuthor() << ", " << song3.getAlbum() << endl;
cout << "The fourth song is: " << song4.getTitle() << ", " << song4.getAuthor() << ", " << song4.getAlbum() << endl << endl;
//************* Test PlayList*************//
//instantiate PlayList and add Songs to it
PlayList myPlayList(song1);
myPlayList.addSong(song2);
myPlayList.addSong(song3);
myPlayList.displayPlayList();
cout << "Playlist now holds " << myPlayList.getNumberOfSongs() << " songs ";
myPlayList.addSong(song1);
myPlayList.displayPlayList();
cout << endl;
myPlayList.addSong(song4);
myPlayList.displayPlayList();
cout << endl;
myPlayList.addSong(song5);
myPlayList.displayPlayList();
cout << endl;
myPlayList.removeSong(song2);
myPlayList.displayPlayList();
cout << endl;
myPlayList.removeSong(song3);
myPlayList.displayPlayList();
cout << endl;
myPlayList.clearPlayList();
myPlayList.displayPlayList();
cout << myPlayList.isEmpty() << endl;
return 0;
}
The task:
For this project you are given the full class interface SetInterface.h that your Set class will inherit from. You are also given as a sample usage main() function. This is the function that will be used to test your code. You need to write and submit 3 classes (6 files: Set.h, Set.cpp, Song.h, Song.cpp, PlayList.h, PlayList.cpp)
Explanation / Answer
Song.h
#ifndef MY_SONG_CLASS
#define MY_SONG_CLASS
#include <string>
class Song {
private:
public:
//member variables
std::string songTitle;
std::string artistName;
std::string songGenre;
double songLength {0.0};
// functions
Song();
Song(const std::string& title, const std::string& artist,
const std::string& genre, double length);
// Sets a name for the song
void setSongTitle(const std::string& st);
// Sets the artist name of the song
void setArtistName(const std::string& ar);
// Sets the album name of the song
void setSongGenre(const std::string& sg);
//Sets the song length of the song
void setSongLength(double sl);
// Gets the name of the song
std::string getSongName();
// Gets the name of the artist
std::string getArtistName();
// Gets the album name of the song
std::string getSongGenre();
//Gets the song length of the song
double getSongLength();
};
#endif // MY_SONG_CLASS
Song.cpp
#include "Song.h"
// Default constructor
/* --------------------------------------------------------------------------
* Note: you don't need to initialize std::strings.
* std::string is an object which is initialized as an empty string by its
* own constructor.
* -------------------------------------------------------------------------- */
Song::Song()
{}
Song::Song(const std::string& title, const std::string& artist,
const std::string& genre, double length)
{
songTitle = title;
artistName = artist;
songGenre = genre;
songLength = length;
}
void Song::setSongTitle(const std::string& st)
{
songTitle = st;
}
void Song::setArtistName(const std::string& ar)
{
artistName = ar;
}
void Song::setSongGenre(const std::string& sg)
{
songGenre = sg;
}
void Song::setSongLength(double sl)
{
songLength = sl;
}
std::string Song::getSongName()
{
return songTitle;
}
std::string Song::getArtistName()
{
return artistName;
}
std::string Song::getSongGenre()
{
return songGenre;
}
double Song::getSongLength()
{
return songLength;
}
Playlist.h
#ifndef MY_PLAYLIST
#define MY_PLAYLIST
#include <string>
#include <vector>
#include "Song.h"
class Playlist {
public:
Playlist(const std::string& name_arg = "default");
void addSong(Song&& song);
friend std::ostream& operator<<(std::ostream& os, const Playlist& value);
private:
std::string name;
double length {0.0};
std::vector<Song> song_list;
};
#endif // MY_PLAYLIST
Playlist.cpp
#include <iostream>
#include "Playlist.h"
Playlist::Playlist(const std::string& name_arg)
: name {name_arg}
{}
void Playlist::addSong(Song&& song)
{
length += song.songLength;
song_list.push_back(song);
}
/* --------------------------------------------------------------------------
* FRIEND FUNCTIONS
* -------------------------------------------------------------------------- */
std::ostream& operator<<(std::ostream& os, const Playlist& value)
{
os << "Playlist '" << value.name << "': ";
for(const auto& s : value.song_list) {
os << "title: " << s.songTitle << "; artist: " << s.artistName
<< "; genre: " << s.songGenre << "; duration " << s.songLength
<< ' ';
}
return os;
}
main.cpp
#include <cctype>
#include <iomanip>
#include <iostream>
#include <limits>
#include <string>
#include <vector>
#include "Playlist.h"
#include "Song.h"
Playlist& askForSongs(Playlist& play);
std::vector<Playlist>& addNewPlaylist(std::vector<Playlist>& play);
void displayAllPlaylists(const std::vector<Playlist>& plays);
int exitMenu();
void waitForEnter();
// Main Menu
int main()
{
std::vector<Playlist> to_play(1); // at least 1 default element
int choice = 0;
do {
std::cout << " *************************************************** "
"* * "
"* Playlist Manager * "
"* * "
"*************************************************** "
"Where would you like to start? "
"1. Add Songs "
"2. Add New Playlist "
"3. View All Playlists "
"4. Exit "
"Enter Your Choice: ";
std::cin >> choice;
std::cin.ignore(1);
// Set the numeric output formatting.
std::cout << std::fixed << std::showpoint << std::setprecision(2);
// Respond to the user's menu selection.
switch(choice) {
case 1: // Add songs to a playlist
askForSongs(to_play.at(0));
break;
case 2:
addNewPlaylist(to_play);
break;
case 3:
displayAllPlaylists(to_play);
break;
case 4:
std::cout << "Happy Listening! ";
choice = exitMenu();
break;
default:
break;
}
} while (0 < choice && choice < 4);
waitForEnter();
return 0;
}
Playlist& askForSongs(Playlist& play)
{
std::string title;
do {
int i = 1;
std::cout << " Insert new song into playlist"
" - leave title empty to end insertion."
" Enter title of song# " << i << ": ";
std::getline(std::cin, title);
if(title.empty()) { break; }
std::cout << "Enter artist for this song: ";
std::string artist;
std::getline(std::cin, artist);
std::cout << "Enter genre for this song: ";
std::string genre;
std::getline(std::cin, genre);
std::cout << "Enter duration (in mins) for this song: ";
double song_duration = 0.0;
std::cin >> song_duration;
std::cin.ignore(1);
std::cout << " Adding " << genre << " song "" << title
<< "" by " << artist << ". Running time: "
<< song_duration << " mins ";
i++;
Song song(title, artist, genre, song_duration);
play.addSong(std::move(song));
} while (not title.empty());
return play;
}
std::vector<Playlist>& addNewPlaylist(std::vector<Playlist>& play)
{
std::cout << " Enter Playlist Name: ";
std::string n_playlist;
std::getline(std::cin, n_playlist);
Playlist just_created(n_playlist);
std::cout << " Your new playlist "<< n_playlist << " has been added! ";
char option = 'n';
do {
std::cout << "Would you like to add songs to your new playlist "
<< n_playlist <<"? (Y/N) ";
std::cin >> option;
std::cin.ignore(1);
option = std::toupper(option);
} while (option not_eq 'Y' and option not_eq 'N');
if(option == 'Y') { askForSongs(just_created); }
play.push_back(just_created);
return play;
}
void displayAllPlaylists(const std::vector<Playlist>& plays)
{ for(const auto& a : plays) { std::cout << a; } }
//Exit Menu
int exitMenu()
{
std::cout << " ";
std::cout << "Would you like to exit the program? (Y/N) ";
char answ = 'N';
std::cin >> answ;
std::cin.ignore(1);
if(answ == 'Y' || answ == 'y') {
return 4;
}
return 3; // == show playlists
}
void waitForEnter()
{
std::cout << " Press ENTER to continue... ";
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), ' ');
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.