C++ program visual studio program that does and includes the following: This pro
ID: 3772529 • Letter: C
Question
C++ program visual studio program that does and includes the following:
This program will allow the user to keep track of both a CD and a DVD collection. The program needs to work for both CDs and DVDs. There should be a base class that maintains the common information between a CD and a DVD. Each CD/DVD in the collection will be represented as a class, so you will have one class that will be a CD and another that will be a DVD. The program will then has a base class called Media. Both the CD and DVD classes will inherit from the same base class.
The base class will have one data member that will hold the CD Name or Movie Title and another data member for the length of the total CD or the length of the movie.
The CD class will use a linked list to keep track of the titles of the songs on the CD; this will allow each CD to have a different number of songs. It should also maintain the length of each song, thus the class will use a structure which will have the song title and its length. Each song will be an instance of this structure and will be stored in the linked list. The class will also have a data member for the artist name.
The DVD class will have data members for the year the movie was released. The class will have a linked list for storage of the name of the actors and actresses in the movie. The class will also maintain the character names that the actors/actresses played in the movie. The actors/ actresses and characters information will be stored in a structure which will be stored in the linked list.
The program will then maintain two lists, one for the CDs and another for DVDs. The program will use linked lists to maintain the two different lists. The program must provide methods (functions) to add a CD/DVD, remove a CD/DVD and update a CD/DVD. There should also be a function that displays the entire list of CDs/DVDs. The output must be a table format, with heading.
NOTE: There must be only one linked list class (template) that will be used for each of the lists needed in this program. Derived classes of the linked list can be added to expend the functionality of the linked list to be specific for each class.
Movie Title - Length of Movie - Year Released - Actors/Actresses - Characters
NOTE: The movie title, length of movie and year released should only appear once while the actors/actresses and characters will have several lines. So the other columns must be displayed with blanks.
Artist - CD Name - Length of CD - Song Title - Song Length
NOTE: The artist name, CD name and length of CD should only appear once while the song title and length will have several lines. So the other columns must be displayed with blanks.
Explanation / Answer
Complete Program:
// File: Media.h
#ifndef MEDIA_H
#define MEDIA_H
#include <iostream>
#include <string>
using namespace std;
class Media
{
public:
Media(string str, double len);
string getName();
int getLength();
private:
string name;
int length;
};
#endif
-----------------------------------------------------------------------------------------------
// File: Media.cpp
#include "Media.h"
Media::Media(string str, double len)
{
name = str;
length = len;
}
string Media::getName()
{
return name;
}
int Media::getLength()
{
return length;
}
-----------------------------------------------------------------------------------------------
// File: CD.h
#ifndef CD_H
#define CD_H
#include "Media.h"
#include <list>
#include <iterator>
struct Song
{
string title;
int size;
};
class CD : public Media
{
public:
CD(string str1, int len, string str2);
string getArtist();
void addSong(string ti, int si);
void removeSong(string ti, int si);
void updateSong(string ti, int newsize);
void showSongs();
private:
list<Song> songs;
string artistName;
};
#endif
-----------------------------------------------------------------------------------------------
// File: CD.cpp
#include "CD.h"
CD::CD(string str1, int len, string str2) : Media(str1, len)
{
artistName = str2;
}
string CD::getArtist()
{
return artistName;
}
void CD::addSong(string ti, int si)
{
Song s;
s.title = ti;
s.size = si;
songs.push_back(s);
}
void CD::removeSong(string ti, int si)
{
Song s;
s.title = ti;
s.size = si;
list<Song>::iterator itr;
for(itr = songs.begin(); itr != songs.end(); itr++)
{
if(strcmp(itr->title.c_str(), ti.c_str()) == 0)
{
songs.erase(itr);
break;
}
}
}
void CD::updateSong(string ti, int newsize)
{
list<Song>::iterator itr;
for(itr = songs.begin(); itr != songs.end(); itr++)
{
if(strcmp(itr->title.c_str(), ti.c_str()) == 0)
{
songs.erase(itr);
Song s;
s.title = ti;
s.size = newsize;
songs.push_back(s);
break;
}
}
}
void CD::showSongs()
{
list<Song>::iterator itr;
for(itr = songs.begin(); itr != songs.end(); itr++)
cout << itr->title << " " << itr->size << endl;
}
-----------------------------------------------------------------------------------------------
// File: DVD.h
#ifndef DVD_H
#define DVD_H
#include "Media.h"
#include <list>
#include <iterator>
struct Actor
{
string actorName;
string characterName;
};
class DVD : public Media
{
public:
DVD(string str1, double len, int yr);
int getYear();
void addActor(string str1, string str2);
void removeActor(string str1, string str2);
void updateActor(string str1, string newCharacter);
void showActors();
private:
list<Actor> actors;
int year;
};
#endif
-----------------------------------------------------------------------------------------------
// File: DVD.cpp
#include "DVD.h"
DVD::DVD(string str, double len, int yr) : Media(str, len)
{
year = yr;
}
int DVD::getYear()
{
return year;
}
void DVD::addActor(string str1, string str2)
{
Actor c;
c.actorName = str1;
c.characterName = str2;
actors.push_back(c);
}
void DVD::removeActor(string str1, string str2)
{
Actor c;
c.actorName = str1;
c.characterName = str2;
list<Actor>::iterator itr;
for(itr = actors.begin(); itr != actors.end(); itr++)
{
if(strcmp(itr->actorName.c_str(), str1.c_str()) == 0)
{
actors.erase(itr);
break;
}
}
}
void DVD::updateActor(string str1, string newCharacter)
{
list<Actor>::iterator itr;
for(itr = actors.begin(); itr != actors.end(); itr++)
{
if(strcmp(itr->actorName.c_str(), str1.c_str()) == 0)
{
actors.erase(itr);
Actor c;
c.actorName = str1;
c.characterName = newCharacter;
actors.push_back(c);
break;
}
}
}
void DVD::showActors()
{
list<Actor>::iterator itr;
for(itr = actors.begin(); itr != actors.end(); itr++)
cout << itr->actorName << " " << itr->characterName << endl;
}
-----------------------------------------------------------------------------------------------
// Main.cpp
#include "Media.h"
#include "CD.h"
#include "DVD.h"
#include <list>
#include <iterator>
void addCD(list<CD> cds);
void addDVD(list<DVD> dvds);
void removeCD(list<CD> cds);
void removeDVD(list<DVD> dvds);
void updateCD(list<CD> cds);
void updateDVD(list<DVD> dvds);
void showCDs(list<CD> cds);
void showDVDs(list<DVD> dvds);
int main()
{
list<CD> cds;
list<DVD> dvds;
int choice;
do
{
cout << "-----MENU-----" << endl;
cout << "1. Add CD" << endl;
cout << "2. Add DVD" << endl;
cout << "3. Remove CD" << endl;
cout << "4. Remove DVD" << endl;
cout << "5. Update CD" << endl;
cout << "6. Update DVD" << endl;
cout << "7. Show CDs" << endl;
cout << "8. Show DVDs" << endl;
cout << "9. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch(choice)
{
case 1:
addCD(cds);
break;
case 2:
addDVD(dvds);
break;
case 3:
removeCD(cds);
break;
case 4:
removeDVD(dvds);
break;
case 5:
updateCD(cds);
break;
case 6:
updateDVD(dvds);
break;
case 7:
showCDs(cds);
break;
case 8:
showDVDs(dvds);
break;
case 9:
cout << "Thank you." << endl;
break;
default:
cout << "Invalid Choice!" << endl;
}
cout << endl;
}while(choice != 9);
system("pause");
return 0;
}
void addCD(list<CD> cds)
{
string str, ti, art;
int len, n;
cout << " Enter CD name: ";
getline(cin, ti);
cout << "Enter CD length: ";
getline(cin, str);
len = atoi(str.c_str());
cout << "Enter artist name: ";
getline(cin, art);
CD cd(ti, len, art);
cout << "Enter number of songs: ";
getline(cin, str);
n = atoi(str.c_str());
for(int i = 0; i < n; i++)
{
cout << "Enter title for song #" << (i + 1) << ": ";
getline(cin, str);
cout << "Enter size for song #" << (i + 1) << ": ";
getline(cin, str);
len = atoi(str.c_str());
cd.addSong(str, len);
}
cds.push_back(cd);
}
void addDVD(list<DVD> dvds)
{
string str, name, str1, str2;
int len, yr, n;
cout << " Enter DVD name: ";
getline(cin, name);
cout << "Enter DVD length: ";
getline(cin, str);
len = atoi(str.c_str());
cout << "Enter DVD year: ";
getline(cin, str);
yr = atoi(str.c_str());
DVD dvd(name, len, yr);
cout << "Enter number of actors: ";
getline(cin, str);
n = atoi(str.c_str());
for(int i = 0; i < n; i++)
{
cout << "Enter name of actor #" << (i + 1) << ": ";
getline(cin, str1);
cout << "Enter character of actor #" << (i + 1) << ": ";
getline(cin, str2);
dvd.addActor(str1, str2);
}
dvds.push_back(dvd);
}
void removeCD(list<CD> cds)
{
string name;
cout << " Enter CD name: ";
getline(cin, name);
list<CD>::iterator itr;
for(itr = cds.begin(); itr != cds.end(); itr++)
{
if(strcmp(itr->getName().c_str(), name.c_str()) == 0)
{
cds.erase(itr);
break;
}
}
}
void removeDVD(list<DVD> dvds)
{
string name;
cout << " Enter DVD name: ";
getline(cin, name);
list<DVD>::iterator itr;
for(itr = dvds.begin(); itr != dvds.end(); itr++)
{
if(strcmp(itr->getName().c_str(), name.c_str()) == 0)
{
dvds.erase(itr);
break;
}
}
}
void updateCD(list<CD> cds)
{
string name, str;
int len;
cout << " Enter CD name: ";
getline(cin, name);
list<CD>::iterator itr;
for(itr = cds.begin(); itr != cds.end(); itr++)
{
if(strcmp(itr->getName().c_str(), name.c_str()) == 0)
{
cout << "Enter title for song: ";
getline(cin, str);
cout << "Enter new size for song: ";
getline(cin, str);
len = atoi(str.c_str());
itr->updateSong(str, len);
break;
}
}
}
void updateDVD(list<DVD> dvds)
{
string name, str1, str2;
cout << " Enter DVD name: ";
getline(cin, name);
list<DVD>::iterator itr;
for(itr = dvds.begin(); itr != dvds.end(); itr++)
{
if(strcmp(itr->getName().c_str(), name.c_str()) == 0)
{
cout << "Enter name of actor: ";
getline(cin, str1);
cout << "Enter character of actor: ";
getline(cin, str2);
itr->updateActor(str1, str2);
break;
}
}
}
void showCDs(list<CD> cds)
{
list<CD>::iterator itr;
for(itr = cds.begin(); itr != cds.end(); itr++)
{
cout << "CD name: " << itr->getName() << endl;
cout << "CD length: " << itr->getLength() << endl;
cout << "CD artist: " << itr->getArtist() << endl;
itr->showSongs();
cout << endl;
}
}
void showDVDs(list<DVD> dvds)
{
list<DVD>::iterator itr;
for(itr = dvds.begin(); itr != dvds.end(); itr++)
{
cout << "DVD name: " << itr->getName() << endl;
cout << "DVD length: " << itr->getLength() << endl;
cout << "DVD year: " << itr->getYear() << endl;
itr->showActors();
cout << endl;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.