For a family or an individual a favorite place to go on week
For a family or an individual, a favorite place to go on weekends or holidays is to a DVD store to rent movies. A new DVD store in your neighborhood is about to open. However, it does not have a program to keep track of its DVDs and customers. The store managers want someone to write a program for their system so that the DVD store can function. The program should be able to perform the following operations:
Rent a DVD; that is, check out a DVD.
Return, or check in, a DVD.
Create a list of DVDs owned by the store.
Show the details of a particular DVD.
Print a list of all of the DVDs in the store.
Check whether a particular DVD is in the store.
Maintain a customer database.
Print a list of all of the DVDs rented by each customer.
Let us write a program for the DVD store. This example further illustrates the object-oriented design methodology and, in particular, inheritance and overloading.
The programming requirement tells us that the DVD store has two major components: DVDs and customers. We will describe these two components in detail. We also need to maintain the following lists:
A list of all of the DVDs in the store
A list of all of the store’s customers
Lists of the DVDs currently rented by the customers
We will develop the program in two parts. In Part 1, we design, implement, and test the DVD component. In Part 2, we design and implement the customer component, which is then added to the DVD component developed in Part 1. That is, after completing Parts 1 and 2, we can perform all of the operations listed previously.
Part 1: DVD Component
DVD Object
This is the first stage, wherein we discuss the DVD component. The common things associated with a DVD are:
Name of the movie
Names of the stars
Name of the producer
Name of the director
Name of the production company
Number of copies in the store
From this list, we see that some of the operations to be performed on a DVD object are:
Set the DVD information—that is, the title, stars, production company, and so on.
Show the details of a particular DVD.
Check the number of copies in the store.
Check out (that is, rent) the DVD. In other words, if the number of copies is greater than zero, decrement the number of copies by one.
Check in (that is, return) the DVD. To check in a DVD, first we must check whether the store owns such a DVD and, if it does, increment the number of copies by one.
Check whether a particular DVD is available—that is, check whether the number of copies currently in the store is greater than zero.
The deletion of a DVD from the DVD list requires that the list be searched for the DVD to be deleted. Thus, we need to check the title of a DVD to find out which DVD is to be deleted from the list. For simplicity, we assume that two DVDs are the same if they have the same title.
Part 2: Customer Component
Customer Object
The customer object stores information about a customer, such as the first name, last name, account number, and a list of DVDs rented by the customer.
Every customer is a person. We have already designed the class personType in Example 10-10 (Chapter 10) and described the necessary operations on the name of a person. Therefore, we can derive the class customerType from the classpersonType and add the additional members that we need. First, however, we must redefine the class personType to take advantage of the new features of object-oriented design that you have learned, such as operator overloading, and then derive the class customerType.
Recall that the basic operations on an object of type personType are:
Print the name.
Set the name.
Show the first name.
Show the last name.
Similarly, the basic operations on an object of type customerType are:
Print the name, account number, and the list of rented DVDs.
Set the name and the account number.
Rent a DVD; that is, add the rented DVD to the list.
Return a DVD; that is, delete the rented DVD from the list.
Show the account number.
The details of implementing the customer component are left as an exercise for you. (See Programming Exercise 14 at the end of this chapter.)
Main Program
We will now write the main program to test the DVD object. We assume that the necessary data for the DVDs are stored in a file. We will open the file and create the list of DVDs owned by the DVD store. The data in the input file is in the following form:
We will write a function, createDVDList, to read the data from the input file and create the list of DVDs. We will also write a function, displayMenu, to show the different choices—such as check in a movie or check out a movie—that the user can make. The algorithm of the function main is:
Open the input file.
If the input file does not exist, exit the program.
Create the list of DVDs (createDVDList).
Show the menu (displayMenu).
While not done
Perform various operations.
Opening the input file is straightforward. Let us describe Steps 2 and 3, which are accomplished by writing two separate functions: createDVDList and displayMenu.
createDVDList
This function reads the data from the input file and creates a linked list of DVDs. Because the data will be read from a file and the input file was opened in the function main, we pass the input file pointer to this function. We also pass the DVD list pointer, declared in the function main, to this function. Both parameters are reference parameters. Next, we read the data for each DVD and then insert the DVD in the list. The general algorithm is:
Read the data and store it in a DVD object.
Insert the DVD in the list.
Repeat steps a and b for each DVD’s data in the file.
displayMenu
This function informs the user what to do. It contains the following output statements:
Select one of the following:
1.
To check whether the store carries a particular DVD
2.
To check out a DVD
3.
To check in a DVD
4.
To check whether a particular DVD is in stock
5.
To print only the titles of all the DVDs
6.
To print a list of all the DVDs
9.
To exit
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Here\'s what I\'ve typed so far but its not seeming to work. Can you alter it for me so i can have a working code? thanks
#include <iostream>
#include <string>
using namespace std;
class dvdType
{
friend ostream& operator << (ostream&, const dvdType&);
public:
void setDVDInfo (string title, string star1, string star2, string producer, string director, string productionCo, int setInStock);
int getNoOfCopiesInStock() const;
void checkOut();
void checkIn();
void printTitle() const;
void printInfo() const;
bool checkTitle (string title);
void updateInStock (int num);
void setCopiesInStock (int num);
string getTitle () const;
dvdType (string title = \" \", string star1= \" \", string star2= \" \", string operator= \" \", string director= \" \", string productionCo= \" \", int setInStock =0);
bool operator == (const dvdType&) const;
bool operator != (const dvdType&) const;
private:
string dvdTitle;
string movieStar1;
string movieStar2;
string movieProducer;
string movieDirector;
string movieProductionCo;
int copiesInStock;
};
void dvdType::setDVDInfo(string title, string star1, string star2, string producer, string director, string productionCo, int setInStock)
{
dvdTitle = title;
movieStar1 = star1;
movieStar2 = star2;
movieProducer = producer;
movie Director = director;
movieProductionCo = productionCo;
copiesInStock = setInStock;
}
void dvdType::checkOut())
{
if (getNoOfCopiesInStock () > 0)
copiesInStock--;
else
cout<<\"Currently out of stock\"<<endl;
}
void dvdType::checkIn()
{
copiesInStock++;
}
int dvdType::getNoOfCopiesInStock() const
{
return copiesInStock;
}
void dvdType :: printTitle() const
{
cout<<\"DVD Title: \"<<dvdTitle<<endl;
}
void dvdType::printInfo() const
{
cout<<\"DVD Title:\"<<dvdTitle<<endl;
cout<<\"Stars: \" <<movieStar1<< \" and \"<<movieStar2<<endl;
cout<<\"Producer:\"<<movieProducer<<endl;
cout<<\"Director: \"<<movieDirector<<endl;
cout<<\"Production Company: \"<<movieProductionCo<<endl;
cout<<\"Copies in stock:\"<<copiesInStock<<endl;
}
bool dvdType::checkTitle(string title)
{
return (dvdTitle == title);
}
void dvdType::updateInStock (int num)
{
copiesInStock += num;
}
void dvdType::setCopiesInStock(int num)
{
copiesInStock = num;
}
string dvdType::getTitle() const
{
return dvdTitle;
}
dvdType::dvdType(string title, string star1, string star2, string producer, string director, string productionCo, int setInStock)
{
setDVDInfo(title, star1, star2, proucer, director, productionCo, setInStock);
}
bool dvdType::operator == const dvdType& other) const
{
return (dvdTitle==other.dvdTitle);
}
bool dvdType::operator != (const dvdType& other) const
{
return (dvdTitle != other.dvdTitle);
}
ostream& operator<<(ostream& osObject, const dvdType& dvd)
{
osObject<<endl;
osObject<<\"DVD Title: \"<<dvd.dvdTitle<<endl;
obObject<<\"Stars: \"<<dvd.movieStar1<<\"and\"<<dvd.movieStar2<<endl;
osObject<<\"Producer: \"<<dvd.movieProducer<<endl;
osObject<<\"Director: \"<<dvd.movieDirector<<endl;
osObject<<\"Production Company: \"<<dvd.movieProductionCo<<endl;
osObject<<\"Copies in stock: \" <<dvd.copiesInStock<<endl;
osObject<<\"_______________________________________________\"<<endl;
return osObject;
};
#include <string>
#include \"unorderedLinkedList.h\"
#include \"dvdType.h\"
using namespace std;
class dvdListType::public unorderedLinkedList<dvdType>
{
public:
bool dvdSearch (string title) const;
bool isDVDAvailable (string title) const;
void dvdCheckOut (string title);
void dvdCheckIn (string title);
bool dvdCheckTitle (string title) const;
void dvdUpdateInStock (string title, int num);
void dvdSetCopiesInStock (string title, int num);
void dvdPrintTitle () const;
private:
void searchDVDList (string title, bool& found, nodeType<dvdType>* ¤t) const;
};
void dvdListType::searchDVDList (string title, bool& found, nodeType<dvdType>* ¤t) const
{
found = false;
current = first;
while (current != nullptr && !found)
if (current -> info.checkTitle(title))
found=true;
else
current = current->link;
}
bool dvdListType::isDVDAvailable (string title) const
{
bool found;
nodedType<dvdType> *location;
searchDVDList (title, found, location);
if(found)
found=(location -> info.getNoOfCopiesInStock () >0);
else
found= false;
return found;
}
void dvdListType::dvdCheckIn(string title)
{
bool found=false;
nodeType<dvdType> *location;
searchDVDList (title, found, location);
if(found)
location ->info.checkIn();
else
cout<<\"The store does not carry\"<<title<<endl;
}
void dvdListType::dvdCheckOut(string title)
{
bool found=false;
nodeType<dvdType> *location;
seachDVDList (title, found, location);
if(found)
location -> info.chechOut();
else
cout<<\"The store does not carry\"<<title<<endl;
}
bool dvdListType::dvdCheckTitle(string title) const
{
bool found=false;
nodeType<dvdType> *location
searchDVDList (title, found, location);
return found;
}
void dvdListType::dvdUpdateInStock (string title, int num)
{
bool found=false;
nodetype<dvdType> *location;
searchDVDList (title, found, location);
if(found)
location -> info.updateInStock(num);
else
cout<<\"The store does not carry\"<<title<<endl;
}
void dvdListType::dvdSetCopiesInStock (string title, int num)
{
bool found = false;
nodeType<dvdType> *location;
seachDVDList (title, found, location);
if(found)
location -> info.setCopiesInStock(num);
else
cout<<\"The store does not carry\"<<title<<endl;
}
bool dvdListType::dvdSearch(string title) const
{
bool found = false;
nodeType<dvdType> *location;
searchDVDList (title, found, location);
return found;
}
void dvdListType::dvdPrintTitle() const
{
nodeType<dvdType>* current;
current = first;
while (current != nullptr)
{
current -> info.printTitle();
current = current -> link;
}
}
while (choice != 9)
{
switch (choice)
{
case 1:
a. get the movie name
b. search the DVD List
c. if found, report success
else report \"failure\"
break;
case 2:
a. get the movie name
b. seach the DVD list
c. if found, check out the DVD
else report \"failure\"
break;
case 3:
a. get the movie name
b. search the DVD list
c. if found, check in DVD
else report \"failure\"
break;
case 4:
a. get the movie name
b. search the DVD list
c. if found
if number of copies > 0
report \"success\"
else
report \"current out of stock\"
else
report \"failure\"
break;
case 5:
print the titles of the DVDs
break;
case 6:
print all the DVDs in the store
break;
default: invalid selection
}
displayMenu();
get choice;
}
#include <iostream>
#include <fstream>
#include <string>
#include \"dvdType.h\"
#include \"dvdListType.h\"
using namespace std;
void createDVDList (ifstream& infile, dvdListType& dvdList);
void displayMenu();
int main()
{
dvdListType dvdList;
int choice;
char ch;
string title;
ifstream infile;
infile.open(\"dvdDat.txt\");
if (!infile)
{
cout<<\"The input file does not exist.\"<<\"The program terminates!!!\"<<endl;
return 1;
}
createDVDList (infile, dvdList)
infile.close();
displayMenu();
cout<<\"Enter your choice: \";
cin>>choice;
cin.get(ch);
cout<<endl;
while (choice != 9)
{
switch (choice)
{
case 1:
cout<<\"Enter the title: \";
getline(cin, title);
cout<<endl;
if (dvdList.dvdSearch(title))
cout<<\"The store carries\"<<title<<endl;
else
cout<<\"The store does not carry\"<<title<<endl;
break;
case 2:
cout<<\"Enter the title: \";
getline(cin, title);
cout<<endl;
if(dvdeList.dvdSearch (title))
{
if (dvdList.isDVDAvailable (title))
{
dvdList.dvdCheckOut(title);
cout<<\"Enjoy your movie: \"<<title<<endl;
}
else
cout<<\"Currently\"<<title<<\"is out of stock.\"<<endl;
}
else
cout<<\"The store does not carry\"<<title<<endl;
break;
case 3:
cout<<\"Enter the title:\";
getline(cin, title);
cout<<endl;
if (dvdList.dvdSearch (title))
{
dvdList.dvdCheckIn(title);
cout<<\"Thanks for returning\"<<title<<endl;
}
else
cout<<\"The store does not carry\"<<title<<endl;
break;
}
case 4:
cout<<\"Enter the title:\";
getline(cin, title);
cout<<endl;
if (dvdList.dvdSearch(title))
{
if (dvdList.isDVDAvailable (title))
cout<<title<<\"is currently in\"<<\"stock.\"<<endl;
else
cout<<title<<\"is current out \"<<\"of stock.\"<<endl;
}
else
cout<<\"The store does not carry\"<<title<<endl;
break;
case 5:
dvdList.dvdPrintTitle();
break;
case 6:
dvdList.print();
break;
default:
cout<<\"Invalid selection.\"<<endl;
}
displayMenu();
cout<<\"Enter your choice: \";
cin>>choice;
cin.get(ch);
cout<<endl;
}
return 0;
}
void createDVDList (ifstream& infile, dvdListType& dvdList)
{
string title;
string star1;
string star2;
string producer;
string director;
string productionCo;
char ch;
int inStock;
dvdType newDVD;
getline (infile, title);
while (infile)
{
getline (infile, star1);
getline (infile, star2);
getline (infile, producer);
getline (infile, director);
getline (infile, productionCo);
infile>>inStock;
infile.get(ch);
newDVD.setDVDInfo (title, star1, star2, producer, director, productionCo, inStock);
dvdList.insertFirst (newDVD);
getline (infile, title);
}
}
void displayMenu()
{
cout<<\"Select one of the following: \"<<endl;
cout<<\"1: To check whether the store carries a\"<<\"particular DVD.\"<<endl;
cout<<\"2: To check out a DVD.\"<<endl;
cout<<\"3: To check in a DVD.\"<<endl;
cout<<\"4: To check whether a particular DVD is\"<<\"in stock.\"<<endl;
cout<<\"5: To print only the title of all the DVDs.\"<<endl;
cout<<\"6: To print a list of all the DVDs.\"<<endl;
cout<<\"9: To exit\"<<endl;
}
Solution
main.cpp
#include <iostream>
#include <fstream>
#include <string>
#include \"dvdType.h\"
#include \"dvdListType.h\"
using namespace std;
void createDVDList(ifstream& infile,
dvdListType& dvdList);
void displayMenu();
int main()
{
dvdListType dvdList;
int choice;
char ch;
string title;
ifstream infile;
//open the input file
infile.open(\"dvdDat.txt\");
if (!infile)
{
cout << \"The input file does not exist. \"
<< \"The program terminates!!!\" << endl;
return 1;
}
//create the DVD list
createDVDList(infile, dvdList);
infile.close();
//show the menu
displayMenu();
cout << \"Enter your choice: \";
cin >> choice; //get the request
cin.get(ch);
cout << endl;
//process the requests
while (choice != 9)
{
switch (choice)
{
case 1:
cout << \"Enter the title: \";
getline(cin, title);
cout << endl;
if (dvdList.dvdSearch(title))
cout << \"The store carries \" << title
<< endl;
else
cout << \"The store does not carry \"
<< title << endl;
break;
case 2:
cout << \"Enter the title: \";
getline(cin, title);
cout << endl;
if (dvdList.dvdSearch(title))
{
if (dvdList.isDVDAvailable(title))
{
dvdList.dvdCheckOut(title);
cout << \"Enjoy your movie: \"
<< title << endl;
}
else
cout << \"Currently \" << title
<< \" is out of stock.\" << endl;
}
else
cout << \"The store does not carry \"
<< title << endl;
break;
case 3:
cout << \"Enter the title: \";
getline(cin, title);
cout << endl;
if (dvdList.dvdSearch(title))
{
dvdList.dvdCheckIn(title);
cout << \"Thanks for returning \"
<< title << endl;
}
else
cout << \"The store does not carry \"
<< title << endl;
break;
case 4:
cout << \"Enter the title: \";
getline(cin, title);
cout << endl;
if (dvdList.dvdSearch(title))
{
if (dvdList.isDVDAvailable(title))
cout << title << \" is currently in \"
<< \"stock.\" << endl;
else
cout << title << \" is currently out \"
<< \"of stock.\" << endl;
}
else
cout << \"The store does not carry \"
<< title << endl;
break;
case 5:
dvdList.dvdPrintTitle();
break;
case 6:
dvdList.print();
break;
default:
cout << \"Invalid selection.\" << endl;
}//end switch
displayMenu(); //display menu
cout << \"Enter your choice: \";
cin >> choice; //get the next request
cin.get(ch);
cout << endl;
}//end while
return 0;
}
void createDVDList(ifstream& infile,
dvdListType& dvdList)
{
string title;
string star1;
string star2;
string producer;
string director;
string productionCo;
char ch;
int inStock;
dvdType newDVD;
getline(infile, title);
while (infile)
{
getline(infile, star1);
getline(infile, star2);
getline(infile, producer);
getline(infile, director);
getline(infile, productionCo);
infile >> inStock;
infile.get(ch);
newDVD.setDVDInfo(title, star1, star2, producer,
director, productionCo, inStock);
dvdList.insertFirst(newDVD);
getline(infile, title);
}//end while
}//end createDVDList
void displayMenu()
{
cout << \"Select one of the following:\" << endl;
cout << \"1: To check whether the store carries a \"
<< \"particular DVD.\" << endl;
cout << \"2: To check out a DVD.\" << endl;
cout << \"3: To check in a DVD.\" << endl;
cout << \"4: To check whether a particular DVD is \"
<< \"in stock.\" << endl;
cout << \"5: To print only the titles of all the DVDs.\"
<< endl;
cout << \"6: To print a list of all the DVDs.\" << endl;
cout << \"9: To exit\" << endl;
} //end displayMenu
dvdListType.h
#ifndef H_DVDLinkedListType
#define H_DVDLinkedListType
#include <string>
#include \"unorderedLinkedList.h\"
#include \"dvdType.h\"
using namespace std;
class dvdListType:public unorderedLinkedList<dvdType>
{
public:
bool dvdSearch(string title) const;
bool isDVDAvailable(string title) const;
void dvdCheckOut(string title);
void dvdCheckIn(string title);
bool dvdCheckTitle(string title) const;
void dvdUpdateInStock(string title, int num);
void dvdSetCopiesInStock(string title, int num);
void dvdPrintTitle() const;
private:
void searchDVDList(string title, bool& found,
nodeType<dvdType>* ¤t) const;
};
#endif
dvdListTypeImp.cpp
#include <iostream>
#include <string>
#include \"dvdListType.h\"
using namespace std;
void dvdListType::searchDVDList(string title, bool& found,
nodeType<dvdType>* ¤t) const
{
found = false; //set found to false
current = first; //set current to point to the first node
//in the list
while (current != nullptr && !found) //search the list
if (current->info.checkTitle(title)) //the item is found
found = true;
else
current = current->link; //advance current to
//the next node
}//end searchDVDList
bool dvdListType::isDVDAvailable(string title) const
{
bool found;
nodeType<dvdType> *location;
searchDVDList(title, found, location);
if (found)
found = (location->info.getNoOfCopiesInStock() > 0);
else
found = false;
return found;
}
void dvdListType::dvdCheckIn(string title)
{
bool found = false;
nodeType<dvdType> *location;
searchDVDList(title, found, location); //search the list
if (found)
location->info.checkIn();
else
cout << \"The store does not carry \" << title
<< endl;
}
void dvdListType::dvdCheckOut(string title)
{
bool found = false;
nodeType<dvdType> *location;
searchDVDList(title, found, location); //search the list
if (found)
location->info.checkOut();
else
cout << \"The store does not carry \" << title
<< endl;
}
bool dvdListType::dvdCheckTitle(string title) const
{
bool found = false;
nodeType<dvdType> *location;
searchDVDList(title, found, location); //search the list
return found;
}
void dvdListType::dvdUpdateInStock(string title, int num)
{
bool found = false;
nodeType<dvdType> *location;
searchDVDList(title, found, location); //search the list
if (found)
location->info.updateInStock(num);
else
cout << \"The store does not carry \" << title
<< endl;
}
void dvdListType::dvdSetCopiesInStock(string title, int num)
{
bool found = false;
nodeType<dvdType> *location;
searchDVDList(title, found, location);
if (found)
location->info.setCopiesInStock(num);
else
cout << \"The store does not carry \" << title
<< endl;
}
bool dvdListType::dvdSearch(string title) const
{
bool found = false;
nodeType<dvdType> *location;
searchDVDList(title, found, location);
return found;
}
void dvdListType::dvdPrintTitle() const
{
nodeType<dvdType>* current;
current = first;
while (current != nullptr)
{
current->info.printTitle();
current = current->link;
}
}
dvdType.h
#ifndef H_dvdType
#define H_dvdType
#include <iostream>
#include <string>
using namespace std;
class dvdType
{
friend ostream& operator<< (ostream&, const dvdType&);
public:
void setDVDInfo(string title, string star1,
string star2, string producer,
string director, string productionCo,
int setInStock);
int getNoOfCopiesInStock() const;
void checkOut();
void checkIn();
void printTitle() const;
void printInfo() const;
bool checkTitle(string title);
void updateInStock(int num);
void setCopiesInStock(int num);
string getTitle() const;
dvdType(string title = \"\", string star1 = \"\",
string star2 = \"\", string producer = \"\",
string director = \"\", string productionCo = \"\",
int setInStock = 0);
//Overload the relational operators.
bool operator==(const dvdType&) const;
bool operator!=(const dvdType&) const;
private:
string dvdTitle; //variable to store the name
//of the movie
string movieStar1; //variable to store the name
//of the star
string movieStar2; //variable to store the name
//of the star
string movieProducer; //variable to store the name
//of the producer
string movieDirector; //variable to store the name
//of the director
string movieProductionCo; //variable to store the name
//of the production company
int copiesInStock; //variable to store the number of
//copies in stock
};
#endif
dvdTypeImp.cpp
#include <iostream>
#include <string>
#include \"dvdType.h\"
using namespace std;
void dvdType::setDVDInfo(string title, string star1,
string star2, string producer,
string director,
string productionCo,
int setInStock)
{
dvdTitle = title;
movieStar1 = star1;
movieStar2 = star2;
movieProducer = producer;
movieDirector = director;
movieProductionCo = productionCo;
copiesInStock = setInStock;
}
void dvdType::checkOut()
{
if (getNoOfCopiesInStock() > 0)
copiesInStock--;
else
cout << \"Currently out of stock\" << endl;
}
void dvdType::checkIn()
{
copiesInStock++;
}
int dvdType::getNoOfCopiesInStock() const
{
return copiesInStock;
}
void dvdType::printTitle() const
{
cout << \"DVD Title: \" << dvdTitle << endl;
}
void dvdType::printInfo() const
{
cout << \"DVD Title: \" << dvdTitle << endl;
cout << \"Stars: \" << movieStar1 << \" and \"
<< movieStar2 << endl;
cout << \"Producer: \" << movieProducer << endl;
cout << \"Director: \" << movieDirector << endl;
cout << \"Production Company: \" << movieProductionCo
<< endl;
cout << \"Copies in stock: \" << copiesInStock
<< endl;
}
bool dvdType::checkTitle(string title)
{
return(dvdTitle == title);
}
void dvdType::updateInStock(int num)
{
copiesInStock += num;
}
void dvdType::setCopiesInStock(int num)
{
copiesInStock = num;
}
string dvdType::getTitle() const
{
return dvdTitle;
}
dvdType::dvdType(string title, string star1,
string star2, string producer,
string director,
string productionCo, int setInStock)
{
setDVDInfo(title, star1, star2, producer, director,
productionCo, setInStock);
}
bool dvdType::operator==(const dvdType& other) const
{
return (dvdTitle == other.dvdTitle);
}
bool dvdType::operator!=(const dvdType& other) const
{
return (dvdTitle != other.dvdTitle);
}
ostream& operator<< (ostream& osObject, const dvdType& dvd)
{
osObject << endl;
osObject << \"DVD Title: \" << dvd.dvdTitle << endl;
osObject << \"Stars: \" << dvd.movieStar1 << \" and \"
<< dvd.movieStar2 << endl;
osObject << \"Producer: \" << dvd.movieProducer << endl;
osObject << \"Director: \" << dvd.movieDirector << endl;
osObject << \"Production Company: \"
<< dvd.movieProductionCo << endl;
osObject << \"Copies in stock: \" << dvd.copiesInStock
<< endl;
osObject << \"_____________________________________\"
<< endl;
return osObject;
}
linkedList.h
#ifndef H_LinkedListType
#define H_LinkedListType
#include <iostream>
#include <cassert>
using namespace std;
//Definition of the node
template <class Type>
struct nodeType
{
Type info;
nodeType<Type> *link;
};
template <class Type>
class linkedListIterator
{
public:
linkedListIterator();
linkedListIterator(nodeType<Type> *ptr);
Type operator*();
linkedListIterator<Type> operator++();
bool operator==(const linkedListIterator<Type>& right) const;
bool operator!=(const linkedListIterator<Type>& right) const;
private:
nodeType<Type> *current; //pointer to point to the current
//node in the linked list
};
template <class Type>
linkedListIterator<Type>::linkedListIterator()
{
current = nullptr;
}
template <class Type>
linkedListIterator<Type>::
linkedListIterator(nodeType<Type> *ptr)
{
current = ptr;
}
template <class Type>
Type linkedListIterator<Type>::operator*()
{
return current->info;
}
template <class Type>
linkedListIterator<Type> linkedListIterator<Type>::operator++()
{
current = current->link;
return *this;
}
template <class Type>
bool linkedListIterator<Type>::operator==
(const linkedListIterator<Type>& right) const
{
return (current == right.current);
}
template <class Type>
bool linkedListIterator<Type>::operator!=
(const linkedListIterator<Type>& right) const
{ return (current != right.current);
}
template <class Type>
class linkedListType
{
public:
const linkedListType<Type>& operator=
(const linkedListType<Type>&);
void initializeList();
bool isEmptyList() const;
void print() const;
int length() const;
void destroyList();
Type front() const;
Type back() const;
virtual bool search(const Type& searchItem) const = 0;
virtual void insertFirst(const Type& newItem) = 0;
virtual void insertLast(const Type& newItem) = 0;
virtual void deleteNode(const Type& deleteItem) = 0;
linkedListIterator<Type> begin();
linkedListIterator<Type> end();
linkedListType();
linkedListType(const linkedListType<Type>& otherList);
//copy constructor
~linkedListType();
protected:
int count; //variable to store the number of
//elements in the list
nodeType<Type> *first; //pointer to the first node of the list
nodeType<Type> *last; //pointer to the last node of the list
private:
void copyList(const linkedListType<Type>& otherList);
};
template <class Type>
bool linkedListType<Type>::isEmptyList() const
{
return(first == nullptr);
}
template <class Type>
linkedListType<Type>::linkedListType() //default constructor
{
first = nullptr;
last = nullptr;
count = 0;
}
template <class Type>
void linkedListType<Type>::destroyList()
{
nodeType<Type> *temp; //pointer to deallocate the memory
//occupied by the node
while (first != nullptr) //while there are nodes in the list
{
temp = first; //set temp to the current node
first = first->link; //advance first to the next node
delete temp; //deallocate the memory occupied by temp
}
last = nullptr; //initialize last to nullptr; first has already
//been set to nullptr by the while loop
count = 0;
}
template <class Type>
void linkedListType<Type>::initializeList()
{
destroyList(); //if the list has any nodes, delete them
}
template <class Type>
void linkedListType<Type>::print() const
{
nodeType<Type> *current; //pointer to traverse the list
current = first; //set current so that it points to
//the first node
while (current != nullptr) //while more data to print
{
cout << current->info << \" \";
current = current->link;
}
}//end print
template <class Type>
int linkedListType<Type>::length() const
{
return count;
} //end length
template <class Type>
Type linkedListType<Type>::front() const
{
assert(first != nullptr);
return first->info; //return the info of the first node
}//end front
template <class Type>
Type linkedListType<Type>::back() const
{
assert(last != nullptr);
return last->info; //return the info of the last node
}//end back
template <class Type>
linkedListIterator<Type> linkedListType<Type>::begin()
{
linkedListIterator<Type> temp(first);
return temp;
}
template <class Type>
linkedListIterator<Type> linkedListType<Type>::end()
{
linkedListIterator<Type> temp(nullptr);
return temp;
}
template <class Type>
void linkedListType<Type>::copyList
(const linkedListType<Type>& otherList)
{
nodeType<Type> *newNode; //pointer to create a node
nodeType<Type> *current; //pointer to traverse the list
if (first != nullptr) //if the list is nonempty, make it empty
destroyList();
if (otherList.first == nullptr) //otherList is empty
{
first = nullptr;
last = nullptr;
count = 0;
}
else
{
current = otherList.first; //current points to the
//list to be copied
count = otherList.count;
//copy the first node
first = new nodeType<Type>; //create the node
first->info = current->info; //copy the info
first->link = nullptr; //set the link field of
//the node to nullptr
last = first; //make last point to the
//first node
current = current->link; //make current point to
//the next node
//copy the remaining list
while (current != nullptr)
{
newNode = new nodeType<Type>; //create a node
newNode->info = current->info; //copy the info
newNode->link = nullptr; //set the link of
//newNode to nullptr
last->link = newNode; //attach newNode after last
last = newNode; //make last point to
//the actual last node
current = current->link; //make current point
//to the next node
}//end while
}//end else
}//end copyList
template <class Type>
linkedListType<Type>::~linkedListType() //destructor
{
destroyList();
}//end destructor
template <class Type>
linkedListType<Type>::linkedListType
(const linkedListType<Type>& otherList)
{
first = nullptr;
copyList(otherList);
}//end copy constructor
//overload the assignment operator
template <class Type>
const linkedListType<Type>& linkedListType<Type>::operator=
(const linkedListType<Type>& otherList)
{
if (this != &otherList) //avoid self-copy
{
copyList(otherList);
}//end else
return *this;
}
#endif
unorderedLinkedList.h
#ifndef H_UnorderedLinkedList
#define H_UnorderedLinkedList
#include \"linkedList.h\"
using namespace std;
template <class Type>
class unorderedLinkedList: public linkedListType<Type>
{
public:
bool search(const Type& searchItem) const;
//Function to determine whether searchItem is in the list.
//Postcondition: Returns true if searchItem is in the
// list, otherwise the value false is
// returned.
void insertFirst(const Type& newItem);
void insertLast(const Type& newItem);
void deleteNode(const Type& deleteItem);
};
template <class Type>
bool unorderedLinkedList<Type>::
search(const Type& searchItem) const
{
nodeType<Type> *current; //pointer to traverse the list
bool found = false;
current = first; //set current to point to the first
//node in the list
while (current != nullptr && !found) //search the list
if (current->info == searchItem) //searchItem is found
found = true;
else
current = current->link; //make current point to
//the next node
return found;
}//end search
template <class Type>
void unorderedLinkedList<Type>::insertFirst(const Type& newItem)
{
nodeType<Type> *newNode; //pointer to create the new node
newNode = new nodeType<Type>; //create the new node
newNode->info = newItem; //store the new item in the node
newNode->link = first; //insert newNode before first
first = newNode; //make first point to the
//actual first node
count++; //increment count
if (last == nullptr) //if the list was empty, newNode is also
//the last node in the list
last = newNode;
}//end insertFirst
template <class Type>
void unorderedLinkedList<Type>::insertLast(const Type& newItem)
{
nodeType<Type> *newNode; //pointer to create the new node
newNode = new nodeType<Type>; //create the new node
newNode->info = newItem; //store the new item in the node
newNode->link = nullptr; //set the link field of newNode
//to nullptr
if (first == nullptr) //if the list is empty, newNode is
//both the first and last node
{
first = newNode;
last = newNode;
count++; //increment count
}
else //the list is not empty, insert newNode after last
{
last->link = newNode; //insert newNode after last
last = newNode; //make last point to the actual
//last node in the list
count++; //increment count
}
}//end insertLast
template <class Type>
void unorderedLinkedList<Type>::deleteNode(const Type& deleteItem)
{
nodeType<Type> *current; //pointer to traverse the list
nodeType<Type> *trailCurrent; //pointer just before current
bool found;
if (first == nullptr) //Case 1; the list is empty.
cout << \"Cannot delete from an empty list.\"
<< endl;
else
{
if (first->info == deleteItem) //Case 2
{
current = first;
first = first->link;
count--;
if (first == nullptr) //the list has only one node
last = nullptr;
delete current;
}
else //search the list for the node with the given info
{
found = false;
trailCurrent = first; //set trailCurrent to point
//to the first node
current = first->link; //set current to point to
//the second node
while (current != nullptr && !found)
{
if (current->info != deleteItem)
{
trailCurrent = current;
current = current-> link;
}
else
found = true;
}//end while
if (found) //Case 3; if found, delete the node
{
trailCurrent->link = current->link;
count--;
if (last == current) //node to be deleted
//was the last node
last = trailCurrent; //update the value
//of last
delete current; //delete the node from the list
}
else
cout << \"The item to be deleted is not in \"
<< \"the list.\" << endl;
}//end else
}//end else
}//end deleteNode
#endif
dvdDat.txt
Titanic
Kate Winslet
Leonardo DiCaprio
Cameron
Cameron
20th Century Fox
2
One Fine Day
George Clooney
Michelle Pfeiffer
Obset
Hoffman
20th Century Fox
19
Sister Act
Whoopi GoldBerg
Maggie Smith
Schwartz
Ardolino
Touch Stone Pictures
14

























