Background C++
I have a music player on my phone. I can buy songs, add them to playlists and play them. Obviously it would be redundant to store each song in each playlist; each playlist is just a list of pointers to the songs.
For this lab you will simulate this behavior. Your program will need to have options to: – add songs to the system (you will store only the first line of the song in text, not the audio), – add playlists to a list of playlists, – add songs to a playlist, – play a playlist, and – list all of the songs in the system with a count of how many times each song has been played.
Note that we will not be checking many error cases. In real programming this would be bad, you should usually try to recognize and respond to as many types of errors as you can. In the context of class we are trying to acquaint you with as many concepts as possible, so for the sake of educational efficiency we will not be checking most errors in this lab, you may assume that your user provides correct input.
Requirements:
Programming Language: C++
Part 1 – The Menu
Prompt the user to select one of several operations:
0 – Quit the program
1 – Add songs
2 – List all songs
3 – Add a playlist
4 – Add a song to a playlist
5 – Play a playlist
After any operation is complete (except for quitting), re-prompt the user to select another operation (return user to your menu)
For part 1, only the quit option needs to work.
Part 2 – Add songs
Add the operations for adding and listing the songs.
For this part you will need to create a song class and vector of pointers to songs
As songs are added use “new”
At the end of your program free the space for each song using “delete”.
Add songs
Prompt the user for the name of the song (you do not need to check for duplicate names)
Prompt the user for the first line of the song
Set the count of how many times this song has been played to 0 (without user input)
Continue to prompt for songs until the user responds with “STOP”
Display all songs – display a list of all of the songs currently in the system
Part 3 – Playlists
Add operations for creating playlists and adding songs to them.
For this part you will need to create a playlist class and a list of playlists
Add a playlist
Prompt the user for the name of the playlist (you do not need to check for duplicate names)
Add the new playlist to the list of playlists
Add a song to a playlist
List all of the playlists currently in the system and let the user select one of them.
List all of the songs currently in the system and let the user select one of them.
Add a pointer to the selected song to the selected playlist.
Part 4 – Play and count
Now add the operation to play playlists
Play playlist
List all of the playlist names and let the user select one of them.
“Play” the songs in the play list by printing their first lines.
Whenever a song is played from any playlist, add to the count of how many times it has been played. Note that this is the number of times it has been played, not the number of playlists it is on.
Assure that when you use the “list all songs” operation that the correct count is displayed.
Sample Output
Welcome to the Firstline Player! Please select one of the following options: 0 - Quit the program 1 - Add songs 2 - List all songs 3 - Add a playlist 4 - Add a song to a playlist 5 - Play a playlist Enter your selection now: 1 Read in Song names and first lines: Song Name (type "STOP" when done): Viva La Vida That song's first line: I used to rule the world Song Name (type "STOP" when done): Paradise That song's first line: When she was just a girl Song Name (type "STOP" when done): Yellow That song's first line: Look at the stars Song Name (type "STOP" when done): Ride That song's first line: I just wanna stay in the sun where I find Song Name (type "STOP" when done): Superheroes That song's first line: All her life she has seen Song Name (type "STOP" when done): My Generation That song's first line: People try to put us d-down (Talkin' 'bout my generation) Song Name (type "STOP" when done): Our House That song's first line: I'll light the fire, you place the flowers in the vase that you bought today Song Name (type "STOP" when done): Time That song's first line: Ticking away the moments that make up a dull day Song Name (type "STOP" when done): Love Stinks That song's first line: You love her But she loves him Song Name (type "STOP" when done): STOP Please select one of the following options: 0 - Quit the program 1 - Add songs 2 - List all songs 3 - Add a playlist 4 - Add a song to a playlist 5 - Play a playlist Enter your selection now: 2 0: Name: "Viva La Vida" FirstLine: "I used to rule the world" Played 0 times. 1: Name: "Paradise" FirstLine: "When she was just a girl" Played 0 times. 2: Name: "Yellow" FirstLine: "Look at the stars" Played 0 times. 3: Name: "Ride" FirstLine: "I just wanna stay in the sun where I find" Played 0 times. 4: Name: "Superheroes" FirstLine: "All her life she has seen" Played 0 times. 5: Name: "My Generation" FirstLine: "People try to put us d-down (Talkin' 'bout my generation)" Played 0 times. 6: Name: "Our House" FirstLine: "I'll light the fire, you place the flowers in the vase that you bought today" Played 0 times. 7: Name: "Time" FirstLine: "Ticking away the moments that make up a dull day" Played 0 times. 8: Name: "Love Stinks" FirstLine: "You love her But she loves him" Played 0 times. Please select one of the following options: 0 - Quit the program 1 - Add songs 2 - List all songs 3 - Add a playlist 4 - Add a song to a playlist 5 - Play a playlist Enter your selection now: 3 Playlist name: Went to Coldplay concert Please select one of the following options: 0 - Quit the program 1 - Add songs 2 - List all songs 3 - Add a playlist 4 - Add a song to a playlist 5 - Play a playlist Enter your selection now: 4 Add to a playlist: 0: Went to Coldplay concert Pick a playlist index number: 0 Find a song to add: 0: Viva La Vida 1: Paradise 2: Yellow 3: Ride 4: Superheroes 5: My Generation 6: Our House 7: Time 8: Love Stinks Pick a song index number: 0 Please select one of the following options: 0 - Quit the program 1 - Add songs 2 - List all songs 3 - Add a playlist 4 - Add a song to a playlist 5 - Play a playlist Enter your selection now: 4 Add to a playlist: 0: Went to Coldplay concert Pick a playlist index number: 0 Find a song to add: 0: Viva La Vida 1: Paradise 2: Yellow 3: Ride 4: Superheroes 5: My Generation 6: Our House 7: Time 8: Love Stinks Pick a song index number: 1 Please select one of the following options: 0 - Quit the program 1 - Add songs 2 - List all songs 3 - Add a playlist 4 - Add a song to a playlist 5 - Play a playlist Enter your selection now: 4 Add to a playlist: 0: Went to Coldplay concert Pick a playlist index number: 0 Find a song to add: 0: Viva La Vida 1: Paradise 2: Yellow 3: Ride 4: Superheroes 5: My Generation 6: Our House 7: Time 8: Love Stinks Pick a song index number: 2 Please select one of the following options: 0 - Quit the program 1 - Add songs 2 - List all songs 3 - Add a playlist 4 - Add a song to a playlist 5 - Play a playlist Enter your selection now: 3 Playlist name: Old Please select one of the following options: 0 - Quit the program 1 - Add songs 2 - List all songs 3 - Add a playlist 4 - Add a song to a playlist 5 - Play a playlist Enter your selection now: 4 Add to a playlist: 0: Went to Coldplay concert 1: Old Pick a playlist index number: 1 Find a song to add: 0: Viva La Vida 1: Paradise 2: Yellow 3: Ride 4: Superheroes 5: My Generation 6: Our House 7: Time 8: Love Stinks Pick a song index number: 8 Please select one of the following options: 0 - Quit the program 1 - Add songs 2 - List all songs 3 - Add a playlist 4 - Add a song to a playlist 5 - Play a playlist Enter your selection now: 4 Add to a playlist: 0: Went to Coldplay concert 1: Old Pick a playlist index number: 1 Find a song to add: 0: Viva La Vida 1: Paradise 2: Yellow 3: Ride 4: Superheroes 5: My Generation 6: Our House 7: Time 8: Love Stinks Pick a song index number: 7 Please select one of the following options: 0 - Quit the program 1 - Add songs 2 - List all songs 3 - Add a playlist 4 - Add a song to a playlist 5 - Play a playlist Enter your selection now: 4 Add to a playlist: 0: Went to Coldplay concert 1: Old Pick a playlist index number: 1 Find a song to add: 0: Viva La Vida 1: Paradise 2: Yellow 3: Ride 4: Superheroes 5: My Generation 6: Our House 7: Time 8: Love Stinks Pick a song index number: 6 Please select one of the following options: 0 - Quit the program 1 - Add songs 2 - List all songs 3 - Add a playlist 4 - Add a song to a playlist 5 - Play a playlist Enter your selection now: 4 Add to a playlist: 0: Went to Coldplay concert 1: Old Pick a playlist index number: 1 Find a song to add: 0: Viva La Vida 1: Paradise 2: Yellow 3: Ride 4: Superheroes 5: My Generation 6: Our House 7: Time 8: Love Stinks Pick a song index number: 5 Please select one of the following options: 0 - Quit the program 1 - Add songs 2 - List all songs 3 - Add a playlist 4 - Add a song to a playlist 5 - Play a playlist Enter your selection now: 3 Playlist name: mix Please select one of the following options: 0 - Quit the program 1 - Add songs 2 - List all songs 3 - Add a playlist 4 - Add a song to a playlist 5 - Play a playlist Enter your selection now: 4 Add to a playlist: 0: Went to Coldplay concert 1: Old 2: mix Pick a playlist index number: 2 Find a song to add: 0: Viva La Vida 1: Paradise 2: Yellow 3: Ride 4: Superheroes 5: My Generation 6: Our House 7: Time 8: Love Stinks Pick a song index number: 7 Please select one of the following options: 0 - Quit the program 1 - Add songs 2 - List all songs 3 - Add a playlist 4 - Add a song to a playlist 5 - Play a playlist Enter your selection now: 4 Add to a playlist: 0: Went to Coldplay concert 1: Old 2: mix Pick a playlist index number: 2 Find a song to add: 0: Viva La Vida 1: Paradise 2: Yellow 3: Ride 4: Superheroes 5: My Generation 6: Our House 7: Time 8: Love Stinks Pick a song index number: 0 Please select one of the following options: 0 - Quit the program 1 - Add songs 2 - List all songs 3 - Add a playlist 4 - Add a song to a playlist 5 - Play a playlist Enter your selection now: 5 Pick a playlist to play: 0: Went to Coldplay concert 1: Old 2: mix Pick a playlist index number: 1 Playing first lines: You love her But she loves him Ticking away the moments that make up a dull day I'll light the fire, you place the flowers in the vase that you bought today People try to put us d-down (Talkin' 'bout my generation) Please select one of the following options: 0 - Quit the program 1 - Add songs 2 - List all songs 3 - Add a playlist 4 - Add a song to a playlist 5 - Play a playlist Enter your selection now: 5 Pick a playlist to play: 0: Went to Coldplay concert 1: Old 2: mix Pick a playlist index number: 2 Playing first lines: Ticking away the moments that make up a dull day I used to rule the world Please select one of the following options: 0 - Quit the program 1 - Add songs 2 - List all songs 3 - Add a playlist 4 - Add a song to a playlist 5 - Play a playlist Enter your selection now: 5 Pick a playlist to play: 0: Went to Coldplay concert 1: Old 2: mix Pick a playlist index number: 0 Playing first lines: I used to rule the world When she was just a girl Look at the stars Please select one of the following options: 0 - Quit the program 1 - Add songs 2 - List all songs 3 - Add a playlist 4 - Add a song to a playlist 5 - Play a playlist Enter your selection now: 2 0: Name: "Viva La Vida" FirstLine: "I used to rule the world" Played 2 times. 1: Name: "Paradise" FirstLine: "When she was just a girl" Played 1 times. 2: Name: "Yellow" FirstLine: "Look at the stars" Played 1 times. 3: Name: "Ride" FirstLine: "I just wanna stay in the sun where I find" Played 0 times. 4: Name: "Superheroes" FirstLine: "All her life she has seen" Played 0 times. 5: Name: "My Generation" FirstLine: "People try to put us d-down (Talkin' 'bout my generation)" Played 1 times. 6: Name: "Our House" FirstLine: "I'll light the fire, you place the flowers in the vase that you bought today" Played 1 times. 7: Name: "Time" FirstLine: "Ticking away the moments that make up a dull day" Played 2 times. 8: Name: "Love Stinks" FirstLine: "You love her But she loves him" Played 1 times. Please select one of the following options: 0 - Quit the program 1 - Add songs 2 - List all songs 3 - Add a playlist 4 - Add a song to a playlist 5 - Play a playlist Enter your selection now: 0 GOODBYE!
Deliverables:
Song.h, Song.cpp, Playlist.h, main.cpp, and Playlist.cpp
We will expect the above file(s) to be submitted
Compile command:
g++ Song.cpp main.cpp Playlist.cpp -Wall -o a.out
We will use this command to compile your code
Expert Answer
//main.cpp
#include <iostream>
#include <string>
#include “Playlist.h”
#include “Song.h”
const int quitProgramOption = 0;
const int addSongOption = 1;
const int listSongsOption = 2;
const int addPlaylistOption = 3;
const int addSongToPlaylistOption = 4;
const int playPlaylistOption = 5;
int PrintMenu() {
int userOption = 0;
cout << endl << endl << “Please select one of the following options :” << endl << endl << “0 – Quit the program” << endl << “1 – Add songs”
<< endl << “2 – List all songs” << endl << “3 – Add a playlist” << endl << “4 – Add a song to a playlist” << endl
<< “5 – Play a playlist” << endl << “Enter your selection now : “;
cin >> userOption;
cin.ignore();
return userOption;
}
vector<Song*> AddSong() {
string name;
string firstLine;
vector<Song*> songs;
cout << endl << “Read in Song names and first lines: “;
while (true) {
cout << endl << endl << “Song Name (type “STOP” when done): “;
getline(cin, name);
if (name == “STOP”) {
break;
}
cout << endl << “That song’s first line: “;
getline(cin, firstLine);
Song* song = new Song(name, firstLine);
songs.push_back(song);
}
return songs;
}
void ListSongs(vector<Song*> songs) {
for (int i = 0; i < songs.size(); i++) {
cout << endl << i << “: ” << songs.at(i)->toString();
}
}
Playlist AddPlaylist() {
string playlistName;
Playlist playlist;
cout << endl << “Playlist name: “;
getline(cin, playlistName);
playlist.SetName(playlistName);
return playlist;
}
void AddSongToPlaylist(vector<Playlist> &playlists, vector<Song*> songs) {
int playlistIndex = 0;
int songIndex = 0;
cout << endl << endl << “Add to a playlist: “;
for (int i = 0; i < playlists.size(); i++) {
cout << endl << i << “: ” << playlists.at(i).GetName();
}
cout << endl << “Pick a playlist index number: “;
cin >> playlistIndex;
cout << endl << endl << “Find a song to add: “;
for (int i = 0; i < songs.size(); i++) {
cout << endl << i << “: ” << songs.at(i)->GetName();
}
cout << endl << “Pick a song index number: “;
cin >> songIndex;
playlists.at(playlistIndex).AddSong(songs.at(songIndex));
}
void PlayPlaylist(vector<Playlist> &playlists) {
int playlistIndex = 0;
cout << endl << “Pick a playlist to play: “;
for (int i = 0; i < playlists.size(); i++) {
cout << endl << i << “: ” << playlists.at(i).GetName();
}
cout << endl << “Pick a playlist index number: “;
cin >> playlistIndex;
cout << endl << endl << “Playing first lines:”;
playlists.at(playlistIndex).Play();
}
using namespace std;
int main() {
int userOption = 0;
vector<Playlist> playlists;
vector<Song*> songs;
cout << “Welcome to the Firstline Player!” << endl << endl;
do {
userOption = PrintMenu();
if (userOption == addSongOption) {
vector<Song*> songList = AddSong();
songs.insert(songs.end(), songList.begin(), songList.end());
}
else if (userOption == listSongsOption) {
ListSongs(songs);
}
else if (userOption == addPlaylistOption) {
playlists.push_back(AddPlaylist());
}
else if (userOption == addSongToPlaylistOption) {
AddSongToPlaylist(playlists, songs);
}
else if (userOption == playPlaylistOption) {
PlayPlaylist(playlists);
}
} while (userOption != quitProgramOption);
cout << endl << “GOODBYE!”;
for (int i = songs.size() – 1; i >= 0; i–) {
delete songs.at(i);
songs.erase(songs.begin() + i);
}
return 0;
}
===============================================================================
//Playlist.cpp
#include “Playlist.h”
Playlist::Playlist()
{
}
Playlist::~Playlist()
{
}
void Playlist::AddSong(Song* song)
{
songs.push_back(song);
}
void Playlist::SetName(string name)
{
this->name = name;
}
string Playlist::GetName()
{
return name;
}
void Playlist::Play()
{
for (int i = 0; i < songs.size(); i++) {
cout << endl << songs.at(i)->GetFirstLine();
songs.at(i)->IncrementCount();
}
}
=============================================================================
//Playlist.h
#pragma once
#include “Song.h”
#include <vector>
#include <string>
using namespace std;
class Playlist
{
private:
vector<Song*> songs;
string name;
public:
Playlist();
~Playlist();
void AddSong(Song* song);
void SetName(string name);
string GetName();
void Play();
};
==========================================================================
//Song.cpp
#include “Song.h”
Song::Song(string name, string firstLine)
{
this->name = name;
this->firstLine = firstLine;
count = 0;
}
Song::~Song()
{
}
string Song::toString()
{
stringstream ss;
ss << “Name: “” << name << “” FirstLine: “” << firstLine << “” Played ” << count << ” times.”;
return ss.str();
}
string Song::GetName()
{
return name;
}
string Song::GetFirstLine()
{
return firstLine;
}
void Song::IncrementCount()
{
count++;
}
===============================================================================
//Song.h
#pragma once
#include <string>
#include <sstream>
#include <iostream>
using namespace std;
class Song
{
private:
string name;
string firstLine;
int count;
public:
Song(string name, string firstLine);
~Song();
string toString();
string GetName();
string GetFirstLine();
void IncrementCount();
};
===========================================================================
sample output