This program will allow the user to keep track of a CD or DVD collection. This c
ID: 3559589 • Letter: T
Question
This program will allow the user to keep track of a CD or DVD collection. This can only work exclusively with either CDs or DVDs since some of the data is differentyour choice. The data will be stored in a file. The data from the file will be stored in a text file as records. Each CD/DVD in the collection will be represented as a class, so you will have one class that is the CD/DVD.
The CD class will be limited to 5 songs on that CD. So the class will need to keep an array of 5 strings for the song titles. It should also maintain the length of each song and the total length of the CD. The class will also have the artist name.
The DVD class will have data members for the title of the movie, the length of the movie, the year of the movie and the names of two of the main actors in the movie.
There will be a class that maintains the list of CD/DVDs. This list can be limited to just 5 CD/DVDs. The list provides methods to add a CD/DVD, remove a CD/DVD and update a CD/DVD.
The program should provide a menu for the user to be able to add, delete, update and display the information in a CD/DVD.
Explanation / Answer
Answer:
Program Code to copy:
// CDorDVDProgram.cpp :
#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
using namespace std;
// create a Song structure which holds
// a song Name and its length
struct Song
{
string songName;
double length;
};
// create a CD class
class CD
{
private:
// declare the songs array
struct Song *songs;
// totalLength variable
double totalLength;
// artistName for the whole cd
string artistName;
// size of the array
int sListsize;
public:
// default constructor
CD()
{
songs = new struct Song[5];
totalLength = 0;
artistName = "";
}
// single parameterised constructor
CD(int size)
{
sListsize = size;
songs = new struct Song[sListsize];
totalLength = 0;
artistName = "";
}
// Three parameter constructor
CD(struct Song *cdSongs, int size, string cdArtistName)
{
songs = cdSongs;
sListsize = size;
artistName = cdArtistName;
calTotalLength();
}
// to get the artistName
string getArtistName()
{
return artistName;
}
// to set the Artist Name
void setArtistName(string artName)
{
artistName = artName;
}
// to set the songs list
void setSongs(struct Song *cdsongs)
{
songs = cdsongs;
}
// to ge the songs list
struct Song *getSongs()
{
calTotalLength();
return songs;
}
// to calculate the total length of cd
void calTotalLength()
{
double total = 0;
for(int i = 0; i < sListsize; i++)
{
total += songs[i].length;
}
totalLength = total;
}
// to get the total length
double getTotalLength()
{
return totalLength;
}
// to get the songs list size
int getSongsListSize()
{
return sListsize;
}
// print the cd data
void printCDData()
{
cout << "The CD details are: " << endl;
cout << "CD Artist Name: " << artistName << endl;
cout << "Total Length of the CD is: " << totalLength << endl;
cout << "SNo Song Name Length" << endl;
cout << "-----------------------------------" << endl;
for(int i = 0; i < sListsize; i++)
{
cout << (i + 1) << " " << songs[i].songName << " " << songs[i].length << endl;
}
cout << endl;
}
// to modify the CD data
void modifyCD()
{
// declare the required variables
int choice, inchoice;
string sname, nsname;
string artName;
bool found = false;
double length;
// print the data
printCDData();
// print the menu
cout << "Select the option to update CD data: ";
cout << "1. To update songs name ";
cout << "2. To update songs length ";
cout << "3. To update cd artist name ";
cout << ">> ";
cin >> choice;
// depending on user choice, modify the respective
// cd data
switch(choice)
{
// logic to modify the song name
case 1:
// prompt the user for song name
cout << "Enter song name: ";
cin >> sname;
// loop to search for the song
for(int i = 0; i < sListsize; i++)
{
// match the song name
if(songs[i].songName.compare(sname) == 0)
{
// if matched, prompt the user for
// new song name and change accordingly
cout << "Enter new song name: ";
cin >> nsname;
songs[i].songName = nsname;
found = true;
break;
}
}
break;
// logic to modify the song length
case 2:
// prompt for the song name
cout << "Enter song name: ";
cin >> sname;
// search for the song name
for(int i = 0; i < sListsize; i++)
{
if(songs[i].songName.compare(sname) == 0)
{
// prompt the new length of the song
cout << "Enter new song length: ";
cin >> length;
// update the length
songs[i].length = length;
found = true;
break;
}
}
break;
// to update the artist name
case 3:
cout << "Enter new artist name: ";
cin >> artName;
artistName = artName;
found = true;
break;
}
// if found is true, print the update
if(found)
{
cout << "Updated successfully! ";
}
// otherwise print the error message
else
{
cout << "Sorry! Unable to update! ";
}
}
};
// create a DVD class
class DVD
{
private:
// declare the required variables
string movieTitle;
double movieLength;
int movieYear;
string actorName;
string actoressName;
public:
// default constructor
DVD()
{
movieTitle = "";
movieLength = 0;
movieYear = 0;
actorName = "";
actoressName = "";
}
// parameterised constructor
DVD(string dvdMovieTitle, double dvdMovieLength, int dvdMovieYear, string actor1, string actor2)
{
movieTitle = dvdMovieTitle;
movieLength = dvdMovieLength;
movieYear = dvdMovieYear;
actorName = actor1;
actoressName = actor2;
}
// get the movie name
string getMovieTitle()
{
return movieTitle;
}
// get the actor name
string getActorName()
{
return actorName;
}
// get the actress name
string getActoressName()
{
return actoressName;
}
// get the length of the movie
double getMovieLength()
{
return movieLength;
}
// get the year
int getMovieYear()
{
return movieYear;
}
// print the DVD data
void printDVDData()
{
cout << "The DVD details are: " << endl;
cout << "Movie Name: " << movieTitle << endl;
cout << "Movie Length: " << movieLength << endl;
cout << "Movie Year " << movieYear << endl;
cout << "Male Artist Name: " << actorName << endl;
cout << "Female Artist Name: " << actoressName << endl;
cout << endl;
}
// to modify the DVD data
void modifyDVD()
{
// declare the required varialbes
int choice, inchoice;
string mname, nsname;
string artName;
bool found = false;
int length;
int year, index;
// print the DVD data
printDVDData();
// print the menu
cout << " Select the option to update CD data: ";
cout << "1. To update movie name ";
cout << "2. To update movie length ";
cout << "3. To update movie year ";
cout << "4. To update male artist name ";
cout << "5. To update female artist name ";
cout << ">> ";
cin >> choice;
// depending on the user choice, update
// the required dvd data
switch(choice)
{
// to update the movie name
case 1:
cout << "Enter new movie name: ";
cin >> mname;
movieTitle = mname;
break;
// to update the movie length
case 2:
cout << "Enter new movie length name: ";
cin >> length;
movieLength = length;
break;
// to update the movie year
case 3:
cout << "Enter new movie year: ";
cin >> year;
movieYear = year;
break;
// to update the male artist
case 4:
cout << "Enter new male artist name: ";
cin >> artName;
actorName = artName;
found = true;
break;
// to update the female artist
case 5:
cout << "Enter new female artist name: ";
cin >> artName;
actoressName = artName;
found = true;
break;
}
// if found is true print the successful message
if(found)
{
cout << "Updated successfully! ";
}
// otherwise print the unsuccessful message
else
{
cout << "Sorry! unable to update! ";
}
}
};
// list class which holds either cdlist or dvdlist,
// depending on the user choice
class ListCDorDVD
{
private:
// declare the cd class list as vector
vector<CD> cdlist;
// declare the dvd class list as vector
vector<DVD> dvdlist;
public:
// default constructor
ListCDorDVD()
{}
// method to add CD to the list
void addCD(CD cdobj)
{
cdlist.push_back(cdobj);
}
// method to add DVD to the list
void addDVD(DVD dvdobj)
{
dvdlist.push_back(dvdobj);
}
// method to remove CD from the list
void removeCD()
{
string artName;
cout << "Enter artist name: ";
cin >> artName;
for(int i = 0; i < cdlist.size(); i++)
{
if(cdlist[i].getArtistName().compare(artName) == 0)
cdlist.erase(cdlist.begin() + i);
}
}
// method to remove DVD to the list
void removeDVD()
{
string movieName;
cout << "Enter movie name: ";
cin >> movieName;
for(int i = 0; i < dvdlist.size(); i++)
{
if(dvdlist[i].getMovieTitle().compare(movieName) == 0)
dvdlist.erase(dvdlist.begin() + i);
}
}
// method to uupdate CD list
void upDateCDData()
{
string name;
bool found = false;
cout << "Enter name of the artist name: ";
cin >> name;
for(int i = 0; i < cdlist.size(); i++)
{
if(cdlist[i].getArtistName().compare(name) == 0)
{
cdlist[i].modifyCD();
found = true;
break;
}
}
if(found == false)
{
cout << "Sorry! Could not find the artist. ";
}
}
// method to uupdate DVD list
void upDateDVDData()
{
string name;
bool found = false;
cout << "Enter name of the movie name: ";
cin >> name;
for(int i = 0; i < dvdlist.size(); i++)
{
if(dvdlist[i].getMovieTitle().compare(name) == 0)
{
dvdlist[i].modifyDVD();
found = true;
break;
}
}
if(found == false)
{
cout << "Sorry! Could not find the movie. ";
}
}
// method to print the cd list
void printCD()
{
for(int i = 0; i < cdlist.size(); i++)
cdlist[i].printCDData();
}
// method to print the dvd list
void printDVD()
{
for(int i = 0; i < dvdlist.size(); i++)
dvdlist[i].printDVDData();
}
void storeCDData()
{
ofstream outfile("CD_Data.txt");
for(int i = 0; i < cdlist.size(); i++)
{
outfile << cdlist[i].getArtistName() << " ";
outfile << cdlist[i].getTotalLength() << " ";
for(int j = 0; j < cdlist[i].getSongsListSize(); j++)
{
outfile << " " << cdlist[i].getSongs()[j].songName << " " << cdlist[i].getSongs()[j].length;
}
outfile << endl;
}
outfile.close();
}
void storeDVDData()
{
ofstream outfile("DVD_Data.txt");
for(int i = 0; i < dvdlist.size(); i++)
{
outfile << dvdlist[i].getMovieTitle() << " ";
outfile << dvdlist[i].getMovieLength() << " ";
outfile << dvdlist[i].getMovieYear() << " ";
outfile << dvdlist[i].getActorName() << " ";
outfile << dvdlist[i].getActoressName() << endl;
}
outfile.close();
}
};
// outer function prototypes
// function to work with CD list
void workWithCD(ListCDorDVD cdLists, int count);
// function to work with DVD list
void workWithDVD(ListCDorDVD dvdLists, int count);
// to read CD input from the user
CD inputCDData();
// to read DVD input from the user
DVD inputDVDData();
// main function
int main()
{
// define two ListCDorDVD class objects
ListCDorDVD cdLists;
ListCDorDVD dvdLists;
// declare and define other variables
int choice, cdCount = 0, dvdCount = 0;
int size;
// print the menu to select the option to work with
cout << " Select an option to deal with ";
cout << "1. CD ";
cout << "2. DVD ";
cout << "3. Exit ";
cout << ">> ";
cin >> choice;
// depending on user choice call the respective function
switch(choice)
{
// to work with CD list
case 1:
cout << "Enter the size of CD-List: ";
cin >> cdCount;
workWithCD(cdLists, cdCount);
break;
// to work with DVD list
case 2:
cout << "Enter the size of DVD-List: ";
cin >> dvdCount;
workWithDVD(dvdLists, dvdCount);
break;
// to exit the program
case 3:
return 0;
break;
}
system("pause");
return 0;
}
// function to work with CD list which contains the functionality of
// adding, removing, updating and printing the cd list
void workWithCD(ListCDorDVD cdLists, int cdCount)
{
int choice;
bool toexit = true;
CD cd;
do
{
cout << " Select an option: ";
cout << "1. Add CD ";
cout << "2. Remove CD ";
cout << "3. Update CD ";
cout << "4. Print CD and Store Data ";
cout << "5. Exit ";
cout << ">> ";
cin >> choice;
switch(choice)
{
case 1:
cd = inputCDData();
cdLists.addCD(cd);
break;
case 2:
cdLists.removeCD();
break;
case 3:
cdLists.upDateCDData();
break;
case 4:
cdLists.printCD();
cdLists.storeCDData();
break;
case 5:
toexit = false;
break;
}
} while(toexit);
}
// function to work with DVD list which contains the functionality of
// adding, removing, updating and printing the dvd list
void workWithDVD(ListCDorDVD dvdLists, int count)
{
int choice;
bool toexit = true;
DVD dvd;
do
{
cout << " Select an option: ";
cout << "1. Add CD ";
cout << "2. Remove CD ";
cout << "3. Update CD ";
cout << "4. Print CD ";
cout << "5. Exit ";
cout << ">> ";
cin >> choice;
switch(choice)
{
case 1:
dvd = inputDVDData();
dvdLists.addDVD(dvd);
break;
case 2:
dvdLists.removeDVD();
break;
case 3:
dvdLists.upDateDVDData();
break;
case 4:
dvdLists.printDVD();
dvdLists.storeDVDData();
break;
case 5:
toexit = false;
break;
}
} while(toexit);
}
// function to read the CD data from the user
// and return the CD object
CD inputCDData()
{
struct Song *songs;
int length;
int number = 5;
string artName;
songs = new struct Song[number];
for(int i = 0; i < number; i++)
{
cout << "Enter Song name(use Undescore eg(XXX_AAA_UUU)): ";
cin >> songs[i].songName;
cout << "Enter length of Song: ";
cin >> songs[i].length;
}
cout << "Enter artist name: ";
cin >> artName;
CD cd(songs, number, artName);
return cd;
}
// function to read the DVD data from the user
// and return the DVD object
DVD inputDVDData()
{
string movieName;
string actrName;
string actrsName;
int length;
int year;
cout << "Enter Movie Name: ";
cin >> movieName;
cout << "Enter Movie Length: ";
cin >> length;
cout << "Enter Movie Year: ";
cin >> year;
cout << "Enter Male Actor Name: ";
cin >> actrName;
cout << "Enter Female Actor Name: ";
cin >> actrsName;
DVD dvd(movieName, length, year, actrName, actrsName);
return dvd;
}
Sample Output:
Select an option to deal with
1. CD
2. DVD
3. Exit
>> 1
Enter the size of CD-List: 1
Select an option:
1. Add CD
2. Remove CD
3. Update CD
4. Print CD
5. Exit
>> 1
Enter Song name(use Undescore eg(XXX_AAA_UUU)): Stop_Stop
Enter length of Song: 3.16
Enter Song name(use Undescore eg(XXX_AAA_UUU)): Gimme
Enter length of Song: 3.42
Enter Song name(use Undescore eg(XXX_AAA_UUU)): Just_Tonight
Enter length of Song: 2.14
Enter Song name(use Undescore eg(XXX_AAA_UUU)): Moonlight
Enter length of Song: 3.57
Enter Song name(use Undescore eg(XXX_AAA_UUU)): Girls_Girls_Girls
Enter length of Song: 3.65
Enter artist name: Got7
Select an option:
1. Add CD
2. Remove CD
3. Update CD
4. Print CD
5. Exit
>> 1
Enter Song name(use Undescore eg(XXX_AAA_UUU)): Alarm_Clock
Enter length of Song: 2.31
Enter Song name(use Undescore eg(XXX_AAA_UUU)): Better_off
Enter length of Song: 2.14
Enter Song name(use Undescore eg(XXX_AAA_UUU)): Dazzling_Girl
Enter length of Song: 2.36
Enter Song name(use Undescore eg(XXX_AAA_UUU)): Last_Gift
Enter length of Song: 2.65
Enter Song name(use Undescore eg(XXX_AAA_UUU)): Perfect_10
Enter length of Song: 3.42
Enter artist name: Shinee
Select an option:
1. Add CD
2. Remove CD
3. Update CD
4. Print CD
5. Exit
>> 4
The CD details are:
CD Artist Name: Got7
Total Length of the CD is: 15.94
SNo Song Name Length
-----------------------------------
1 Stop_Stop 3.16
2 Gimme 3.42
3 Just_Tonight 2.14
4 Moonlight 3.57
5 Girls_Girls_Girls 3.65
The CD details are:
CD Artist Name: Shinee
Total Length of the CD is: 12.88
SNo Song Name Length
-----------------------------------
1 Alarm_Clock 2.31
2 Better_off 2.14
3 Dazzling_Girl 2.36
4 Last_Gift 2.65
5 Perfect_10 3.42
Select an option:
1. Add CD
2. Remove CD
3. Update CD
4. Print CD
5. Exit
>>
1
Enter Song name(use Undescore eg(XXX_AAA_UUU)): Cant_Stop
Enter length of Song: 2.36
Enter Song name(use Undescore eg(XXX_AAA_UUU)): Dont_Say_GoodBye
Enter length of Song: 3.54
Enter Song name(use Undescore eg(XXX_AAA_UUU)): You_Have_Fallen_For_Me
Enter length of Song: 3.48
Enter Song name(use Undescore eg(XXX_AAA_UUU)): Heart_Song
Enter length of Song: 3.47
Enter Song name(use Undescore eg(XXX_AAA_UUU)): Radio
Enter length of Song: 3.47
Enter artist name: CNBlue
Select an option:
1. Add CD
2. Remove CD
3. Update CD
4. Print CD
5. Exit
>> 4
The CD details are:
CD Artist Name: Got7
Total Length of the CD is: 15.94
SNo Song Name Length
-----------------------------------
1 Stop_Stop 3.16
2 Gimme 3.42
3 Just_Tonight 2.14
4 Moonlight 3.57
5 Girls_Girls_Girls 3.65
The CD details are:
CD Artist Name: Shinee
Total Length of the CD is: 12.88
SNo Song Name Length
-----------------------------------
1 Alarm_Clock 2.31
2 Better_off 2.14
3 Dazzling_Girl 2.36
4 Last_Gift 2.65
5 Perfect_10 3.42
The CD details are:
CD Artist Name: CNBlue
Total Length of the CD is: 16.32
SNo Song Name Length
-----------------------------------
1 Cant_Stop 2.36
2 Dont_Say_GoodBye 3.54
3 You_Have_Fallen_For_Me 3.48
4 Heart_Song 3.47
5 Radio 3.47
Select an option:
1. Add CD
2. Remove CD
3. Update CD
4. Print CD
5. Exit
>> 2
Enter artist name: Shinee
Select an option:
1. Add CD
2. Remove CD
3. Update CD
4. Print CD
5. Exit
>> 4
The CD details are:
CD Artist Name: Got7
Total Length of the CD is: 15.94
SNo Song Name Length
-----------------------------------
1 Stop_Stop 3.16
2 Gimme 3.42
3 Just_Tonight 2.14
4 Moonlight 3.57
5 Girls_Girls_Girls 3.65
The CD details are:
CD Artist Name: CNBlue
Total Length of the CD is: 16.32
SNo Song Name Length
-----------------------------------
1 Cant_Stop 2.36
2 Dont_Say_GoodBye 3.54
3 You_Have_Fallen_For_Me 3.48
4 Heart_Song 3.47
5 Radio 3.47
Select an option:
1. Add CD
2. Remove CD
3. Update CD
4. Print CD
5. Exit
>>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.