C++ I would like to know if I can get some help on this assignment. I had alread
ID: 3570521 • Letter: C
Question
C++ I would like to know if I can get some help on this assignment. I had already built the program; however, I need to be able to incorporate a linked list which totally changes the game for me. My original program will be posted below with the Linked List I'm trying to incorporate. If you can please help me via the specs I posted for the assignment. If you have a better way of creating this via specs please let me see. Must be in C++ though. Thank you in advanced for your help.
CD/DVD Collection
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.
This is what I have that needs to be fixed:
//main:
#include "Collection.h"
#include "CD.h"
#include "DVD.h"
#include "CollectionList.h"
int DisplayMenu();
int main()
{
CollectionList coll;
Collection *collect;
//coll.Display();
int choice;
choice = DisplayMenu();
while (choice!=0)
{
switch (choice)
{
case 1://1. Add a CD"
{
collect = new CD();
coll.Add(collect);
}
break;
case 2://Add a DVD
{
collect = new DVD();
coll.Add(collect);
}
break;
case 3: // Remove a disk by ID
{
string id;
cout << "Enter id of disk to remove: ";
cin >> id;
coll.Remove(id);
}
break;
case 4://Update a disk
{
string id;
cout << "Enter id of disk to update: ";
cin >> id;
coll.Update(id);
}
break;
case 5://Display List
coll.Display1();
break;
}
choice = DisplayMenu();
}
//system ("pause");
return 0;
}
int DisplayMenu()
{
int ch;
do
{// Display Menu
cout << endl <<"********MENU********" << endl;
cout << "1. Add a CD" << endl;
cout << "2. Add a DVD" << endl;
cout << "3. Remove a disk by ID" << endl;
cout << "4. Update a disk" << endl;
cout << "5. Display List" << endl;
cout << "0. Exit" << endl;
cout << "Enter option: ";
cin >> ch;
if (ch>=0 && ch <=5) // so you don't use anything other then 0-5 number selections
{
return ch;
}
}
while (true);
}
//The DVD class
#include "Collection.h"
#ifndef DVD_H
#define DVD_H
class DVD : public Collection
{
private:
string MovieTitle;
double MovieLength;
int year;
string Actor1;
string Actor2;
public:
DVD() {
SetDVD();
}
void SetDVD() // Tells the user what the program needs to add
{
SetType("DVD");
string id;
cout << "Enter ID: ";
cin >> id;
SetID(id);
cout << "Enter movie title: ";
cin.ignore();
getline(cin, MovieTitle);
cout << "Enter year of the movie: ";
cin >> year;
cout << "Enter name of actor 1: ";
cin.ignore();
getline(cin, Actor1);
cout << "Enter name of actor 2: ";
getline(cin, Actor2);
cout << "Enter length of the movie: ";
cin >> MovieLength;
}
void Display(){ // what will be displayed; from what the user enters when asked
cout << "Disk type: DVD" << endl;
cout << "Movie Title: " << MovieTitle << endl;
cout << "Movie Year: " << year << endl;
cout << "Actor 1: " << Actor1 << endl;
cout << "Actor 2: " << Actor2 << endl;
cout << "Movie Length: " << MovieLength << endl << endl;
}
};
#endif
//Collection.h
#include
#include
#include
using namespace std;
#ifndef COLLECTION_H
#define COLLECTION_H
class Collection
{
private:
string id;
string type;
public:
void SetID(string i)
{
id = i;
}
string GetID()
{
return id;
}
void SetType(string t)
{
type = t;
}
string GetType()
{
return type;
}
virtual void Display() = 0;
};
#endif
//collectionlist.h
#include "Collection.h"
#include "CD.h"
#include "DVD.h"
#ifndef COLLECTION_LIST_H
#define COLLECTION_LIST_H
class CollectionList
{
private:
Collection *list[5];
int count;
public:
CollectionList()
{
count = 0;
}
void Add(Collection *obj)
{
if (count==5)
{
cout << endl << "No more disks can be added." << endl;
return;
}
list[count] = obj;
//list[count]->Display();
count++;
}
void Display1()
{
for (int i=0;i {
list[i]->Display();
}
}
void Remove(string id)
{
if (count==0)
{
cout << endl << "No disk can be removed" << endl;
return;
}
for (int i=0;i {
if (list[i]->GetID()==id)
{
for (int j=i; j {
list[j] = list[j+1];
}
}
}
count--;
}
void Update(string id)
{
for (int i=0;i {
if (list[i]->GetID()==id)
{
if(list[i]->GetType()=="CD")
{
Remove(id);
Collection *collect;
collect = new CD();
Add(collect);
}
else
{
Remove(id);
Collection *collect;
collect = new DVD();
Add(collect);
}
}
}
}
};
#endif
//CD.h
//*The CD class
#include "Collection.h"
#ifndef CD_H
#define CD_H
class CD : public Collection
{
private:
string SongTitles[5];//string set to five to add five titles
double SongLength[5];// string used to add 5 different song lengths
double TotalLength;
string Artist;
public:
CD()
{
TotalLength = 0;
SetCD();
}
void SetCD()
{//user inputs for CD
SetType("CD");
string id;
cout << "Enter ID: ";
cin >> id;
SetID(id);
cout << "Enter artist: ";
cin.ignore();
getline(cin, Artist);
//cout << "Artist = " << Artist << endl;
for (int i=0; i<5; i++)
{// user inputs form CD
cout << "Enter song " << (i+1) << " title: ";
getline(cin, SongTitles[i]);
//cout << "SongTitles[i] = " << SongTitles[i] << endl;
cout << "Enter length of song " << (i+1) << ": ";
cin >> SongLength[i];
cin.ignore();
TotalLength += SongLength[i];
}
}
void Display(){ //Displays the final result form what user added
cout << "Disk type: CD" << endl;
cout << "Artist Name: " << Artist << endl;
for (int i=0; i<5; i++)
{
cout << SongTitles[i] << "-" << SongLength[i] << endl;
}
cout << "Total length of CD: " << TotalLength << endl << endl;
}
};
#endif
//linklist.h
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include
using namespace std;
// LinkedList.h
template
class LinkedList
{
private:
// Declare a structure for the list
struct List
{
L value; // The value in the node
struct List *next; // point next node
};
List *head; // The head pointer
public:
// Constructor
LinkedList()
{
head = NULL; // Establishes empty linked list
}
// Destructor
~LinkedList();
// Linked list operations, append, insert, delete, search, display
void appendNode(L);
void insertNode(L);
void deleteNode(L);
L searchList(int);
void displayList() const;
};
// appendNode function
template
void LinkedList::appendNode(L num)
{
List *newNode; // point to new node
List *nodePtr; // move through list
// new node and store num
newNode = new List;
newNode->value = num;
newNode->next = NULL;
if (!head)
{
head = newNode;
}
else
{
// nodePtr to head of list
nodePtr = head;
// Find last node
while (nodePtr->next)
{
nodePtr = nodePtr->next;
}
nodePtr->next = newNode;
}
}
// insertNode function ***
template
void LinkedList::insertNode(L num)
{
List *newNode;
List *nodePtr;
List *previousNode = NULL;
newNode = new List;
newNode->value = num;
if (!head)
{
head = newNode;
newNode->next = NULL;
}
else
{
// head of list
nodePtr = head;
previousNode = NULL;
// Skip all nodes - less than num
while (nodePtr != NULL && nodePtr->value < num)
{
previousNode = nodePtr;
nodePtr = nodePtr->next;
}
if (previousNode == NULL)
{
head = newNode;
newNode->next = nodePtr;
}
else // Otherwise insert after the previous node
{
previousNode->next = newNode;
newNode->next = nodePtr;
}
}
}
// deleteNode function ***
template
void LinkedList::deleteNode(L num)
{
List *nodePtr; // To traverse the list
List *previousNode; // To point to the previous node
if (!head)
{
return;
}
if (head->value == num)
{
nodePtr = head->next;
delete head;
head = nodePtr;
}
else
{
// head of list
nodePtr = head;
// Skip all nodes whose value member is not
// equal to num
while (nodePtr != NULL && nodePtr->value != num)
{
previousNode = nodePtr;
nodePtr = nodePtr->next;
}
if (nodePtr)
{
previousNode->next = nodePtr->next;
delete nodePtr;
}
}
}
// displayList
template
void LinkedList::displayList() const
{
List *nodePtr; // To move through the list
nodePtr = head;
while (nodePtr)
{
// Display the value in this node
//cout << nodePtr->value << " ";
cout << "Display: " << endl;
cout <value <
// Move to next node
nodePtr = nodePtr->next;
}
}
// searchList function***
template
L LinkedList::searchList(int value)
{
List *nodePtr; // move through the list
int location = -1;
if (!head)
{
cout << "Element not found in list." << endl;
return -1;
}
// head of the list
nodePtr = head;
// nodePtr points to node / traverse the list
while (nodePtr)
{
location++;
if (nodePtr->value == value)
{
return location;
}
else
{
nodePtr = nodePtr->next;
}
}
return -1; // return -1
}
//deletes every node in the list ***
template
LinkedList::~LinkedList()
{
List *nodePtr; // traverse the list
List *nextNode; // point the next node
// head of the list
nodePtr = head;
while (nodePtr != NULL)
{
nextNode = nodePtr->next;
// Delete current node
delete nodePtr;
// Position next node
nodePtr = nextNode;
}
}
#endif
Explanation / Answer
#include "CD.h" template class LinkedList1 { private: // Declare a structure struct discList { T value; struct discList *next; // To point to the next node }; discList *head; // List head pointer public: // Default Constructor LinkedList1() { head = NULL; } // Destructor ~LinkedList1(); // Linked list operations void appendNode(T); void insertNode(T); void deleteNode(T); void displayList() const; }; //*********************************************** // appendNode function adds the node to the end * // list. It accepts 3 arguments. * //*********************************************** template void LinkedList1::appendNode(T newValue) { discList *newNode; // To point to a new node discList *nodePtr; // To move through the list // Allocate a new node and store num there newNode = new discList; newNode->value = newValue; newNode->next = NULL; // If there are no nodes in the list make // newNode the first node if (!head) { head = newNode; } else // Otherwise, insert newNode at end { // Intialize nodePtr to head of list nodePtr = head; // Find the last node in the list while (nodePtr->next) { nodePtr = nodePtr->next; } // Insert newNode as the last node nodePtr->next = newNode; } } //*********************************************** // insertNode function inserts the node in * // numerical order. It accepts 3 arguments. * //*********************************************** template void LinkedList1::insertNode(T newValue) { discList *newNode; // A new node discList *nodePtr; // To traverse the list discList *previousNode = NULL; // The previous node // Allocate a new node and store the title there newNode = new discList(newValue); // If there are no nodes in the list make // newNode the first node if (!head) { head = newNode; newNode->next = NULL; } else // Otherwise, insert newNode { // Position nodePtr at the head of list nodePtr = head; // Initialize previousNode to NULL previousNode = NULL; // Skip all nodes whose value is less than the title while (nodePtr != NULL && nodePtr->title next; } // If the new node is to be the 1st in the list // insert it before all other nodes if (previousNode == NULL) { head = newNode; newNode->next = nodePtr; } else // Otherwise insert after the previous node { previousNode->next = newNode; newNode->next = nodePtr; } } } //*********************************************** // deleteNode function removes the node from the* // list without breaking the links created by * // the next pointers and removes the node from * // memory. * //*********************************************** template void LinkedList1::deleteNode( T searchValue) { discList *nodePtr; // To traverse the list discList *previousNode; // To point to the previous node // If the list is empty, do nothing if (!head) { return; } // Determine if the first node is the one if (head->value==searchValue) { nodePtr = head->next; delete head; head = nodePtr; } else { // Intialize nodePtr to head of list nodePtr = head; // Skip all nodes whose value member is not // equal to num while (nodePtr != NULL && nodePtr->value != searchValue) { previousNode = nodePtr; nodePtr = nodePtr->next; } // If nodePtr is not at the end of the list, link // the previous node to the node after nodePtr, // the delete nodePtr if (nodePtr) { previousNode->next = nodePtr->next; delete nodePtr; } } } //*********************************************** // displayList shows the value stored in each * // node of the linked list pointed to by head * //*********************************************** template void LinkedList1::displayList() const { discList *nodePtr; // To move through the list // Postion nodePtr at the head of the list nodePtr = head; // While nodePtr points to a node, traverse the list while (nodePtr) { // Display the value in this node coutRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.