Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

project3code.cpp #include <iostream> #include <fstream> #include <cstdlib> #incl

ID: 2246312 • Letter: P

Question

project3code.cpp

#include <iostream>

#include <fstream>

#include <cstdlib>

#include <ostream>

#include <fstream>

#include <cstdlib>

#include <cstring>

using namespace std;

class movie{

public:

int id;

char title[250];

int year;

char rating[6];

int totalCopies;

int rentedCopies;

};

class Catalog{

private:

movie movies[200];

int count = 0;

public:

int loadData(ifstream &infile, movie movies[]);

void printAll();

void printRated();

void printTitled();

void addMovie();

void returnMovie();

void rentMovie();

void saveToFile(char *filename);

void printMovie(movie &m);

int find(movie movies[], int count, int id);

};

int main(int argc, char *argv[])

{

Catalog obj; //declaring catalog object

//open file for catalog.txt

char filename[100] = "catalog.txt";

ifstream infile(filename);

movie movies[200];

int count = 0;

if(infile.is_open())

{

obj.loadData(infile, movies);

}

int choice = 0;

while(choice != 7)

{

cout << " ********** MAIN MENU ********** " << endl;

cout << "1 - Print Calalog" << endl;

cout << "2 - Search by Title" << endl;

cout << "3 - Search by Rating" << endl;

cout << "4 - Add Movie" << endl;

cout << "5 - Rent Movie" << endl;

cout << "6 - Return Movie" << endl;

cout << "7 - Quit" << endl;

cout << "Enter choice: ";

cin >> choice; //switch statements for different user choices

switch(choice)

{

case 1:

obj.printAll();

break;

case 2:

obj.printTitled();

break;

case 3:

obj.printRated();

break;

case 4:

obj.addMovie();

break;

case 5:

obj.rentMovie();

break;

case 6:

obj.returnMovie();

break;

case 7:

obj.saveToFile(filename);

break;

default:

cout << "Invalid choice. Please re-enter: " << endl;

}

}

return 0;

}

int Catalog::loadData(ifstream &infile, movie movies[])

{

int count = 0;

while(infile >> movies[count].id)

{

infile.ignore();

infile.getline(movies[count].title, 250);

infile >> movies[count].year;

infile >> movies[count].rating;

infile >> movies[count].totalCopies;

infile >> movies[count].rentedCopies;

count++;

}

infile.close();

return count;

}

void Catalog:: printMovie(movie &m)

{

cout << "---------- ID: " << m.id << " ----------" << endl;

cout << "Title: " << m.title << endl;

cout << "Year: " << m.year << endl;

cout << "Rating: " << m.rating << endl;

cout << "Number of copies: " << m.totalCopies << endl;

cout << "Number rented: " << m.rentedCopies << endl;

cout << "---------------------------------------" << endl;

}

void Catalog::printAll()

{

cout << "%%%%%%%%%% Movie Catalog %%%%%%%%%%" << endl;

for(int i = 0 ;i < count; i++)

{

printMovie(movies[i]);

}

cout << endl;

}

void Catalog::printRated()

{

int r = -1;

char rating[5] = "";

cout << "0 - NONE" << endl;

cout << "1 - G" << endl;

cout << "2 - PG" << endl;

cout << "3 - PG13" << endl;

cout << "4 - R" << endl;

cout << "5 - N17" << endl;

while(true)

{

cin >> r;

if(r < 0 || r > 5)

cout << "Invalid choice. Please re-enter: ";

else

break;

}

if(r == 0)

strcpy(rating, "NONE");

else if(r == 1)

strcpy(rating, "G");

else if(r == 2)

strcpy(rating, "PG");

else if(r == 3)

strcpy(rating, "PG13");

else if(r == 4)

strcpy(rating, "R");

else if(r == 5)

strcpy(rating, "N17");

for(int i = 0 ;i < count; i++)

{

if(strcmp(movies[i].rating, rating) == 0)

printMovie(movies[i]);

}

cout << endl;

}

void Catalog::printTitled()

{

char title[250];

cout << "Title of Movie? " << endl;

cin.ignore(); //flush newline

cin.getline(title, 250);

cout << endl;

for(int i = 0 ;i < count; i++)

{

if(strcmp(movies[i].title, title) == 0)

{

printMovie(movies[i]);

break;

}

}

cout << endl;

}

int Catalog::find(movie movies[], int count, int id)

{

for(int i = 0; i < count; i++)

{

if(movies[i].id == id)

return i;

}

return -1;

}

void Catalog::addMovie() //pass by ref

{

int id;

cout << "Moview ID? (must be unique) " ;

cin >> id;

while(find(movies, count, id) != -1)

{

cout << "That ID is already in use. Please enter another: ";

cin >> id;

}

movies[count].id = id;

cin.ignore();

cout << "Title? ";

cin.getline(movies[count].title, 250);

cout << "Year? " ;

cin >> movies[count].year;

cout << "Movie rating? " << endl;

int r = -1;

cout << "0 - NONE" << endl;

cout << "1 - G" << endl;

cout << "2 - PG" << endl;

cout << "3 - PG13" << endl;

cout << "4 - R" << endl;

cout << "5 - N17" << endl;

while(true)

{

cin >> r;

if(r < 0 || r > 5)

cout << "Invalid choice. Please re-enter: ";

else

break;

}

if(r == 0)

strcpy(movies[count].rating, "NONE");

else if(r == 1)

strcpy(movies[count].rating, "G");

else if(r == 2)

strcpy(movies[count].rating, "PG");

else if(r == 3)

strcpy(movies[count].rating, "PG13");

else if(r == 4)

strcpy(movies[count].rating, "R");

else if(r == 5)

strcpy(movies[count].rating, "N17");

cout << "Copies? " ;

while(true)

{

cin >> movies[count].totalCopies;

if(movies[count].totalCopies <= 0 )

cout << "Copies must not be negative. Please re-enter: " ;

else

break;

}

movies[count].rentedCopies = 0;

count++;

}

void Catalog::returnMovie()

{

char answer;

cout << "Do you want to see a list of movies first? (y or n) ";

cin >> answer;

if(answer == 'y' || answer == 'Y')

printAll();

int id;

cout << "Enter id of movie: ";

while(true)

{

cin >> id;

if(id == -1)

break;

int index = find(movies, count, id);

if(index == -1)

cout << "The movie does not exist. Please re-enter(or enter -1 to quit): " << endl;

else

{

if(movies[index].rentedCopies == 0)

cout << "No copies were rented." << endl;

else

{

movies[index].rentedCopies--;

cout << movies[index].title << " returned successfully. " << endl;

}

break;

}

}

}

void Catalog::rentMovie()

{

char answer;

cout << "Do you want to see a list of movies first? (y or n) ";

cin >> answer;

if(answer == 'y' || answer == 'Y')

printAll();

int id;

cout << "Enter id of movie: ";

while(true)

{

cin >> id;

if(id == -1)

break;

int index = find(movies, count, id);

if(index == -1)

cout << "The movie does not exist. Please re-enter(or enter -1 to quit): " << endl;

else

{

if(movies[index].rentedCopies == movies[index].totalCopies)

cout << "There are no copies to rent." << endl;

else

{

movies[index].rentedCopies++;

cout << "One copy of " << movies[index].title << " rented. " << endl;

}

break;

}

}

}

void Catalog::saveToFile(char *filename)

{

ofstream outfile(filename);

if(!outfile.is_open())

cout << "Error writing to output file " << filename << endl;

else

{

for(int i = 0; i < count; i++)

{

outfile << movies[i].id << endl;

outfile << movies[i].title << endl;

outfile << movies[i].year << endl;

outfile << movies[i].rating << endl;

outfile << movies[i].totalCopies << endl;

outfile << movies[i].rentedCopies << endl;

}

outfile.close();

}

}

The purpose of this program is to learn how to use a linear link list to expand or contract the size of list when needed Remember in Project 3 the movie rental app, the number of movies after being read in from the file was fixed at some value which is not very flexible. We still don't want to create more memory that we need but we want the number of movies to increase or decrease as we add movies while running our program. Also, we would like to delete movies as well and print out the titles in sorted order instead of the order they are read in from the file What To Code Take your project 3 code (not the project 4 code)and re-write the Catalog class to use a sorted linear linked list. Add functionality to the program to allow the user to add or delete movies from the catalog. Again, it should read in a list of movies and write out a list of movies and do all the other functionality from project 3 Keep in mind that a linked list doesn't have an index. So to make it more convenient, the find function should pass back a pointer to the object instead of an index. The list should stay sorted for add and delete. However, if the user decides to change the title of the movie then this will cause the list to become unsorted. One way to re-sort the list is to first delete the modified movie and then re-add it to the list. Requirement There should be no arrays (except C-strings) being created in your code anywhere All data is stored in a sorted linear link list. When reading in your file, you should read in one Movie object and call an addToList function to add it to the list; this way you're not duplicating any code as you will need to add to the list when the user adds a new movie. So have both the reading in of a movie and the user adding a new movie call the same addToList function The best way to assure that the list is in 'sorted' order is to always place a new movie in its 'right place' each time you add a movie to the list. Deletion shouldn't change the order of your list so only adding will be affected here. Also, it's okay to write the list out in sorted order even if it was originally unsorted You need to use C-strings, no 'string' type in your code. Also, all functions should less than 30 lines long and no global variables

Explanation / Answer

Given below is the code to use linked list for the Catalog.

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ostream>
#include <fstream>
#include <cstdlib>
#include <cstring>

using namespace std;

class movie{
  
public:
  
int id;
char title[250];
int year;
char rating[6];
int totalCopies;
int rentedCopies;
movie* next ;
  
};

class Catalog{
  
private:
movie *head;
  
  
public:
Catalog(){head = NULL;}
void loadData(ifstream &infile);
void printAll();
void printRated();
void printTitled();
void addMovie();//get user input and add movie
void addMovie(movie *m); //add a constructed movied object in sorted order
void modifyMovie();
void deleteMovie();
bool deleteMovie(char *title);
void returnMovie();
void rentMovie();
void saveToFile(char *filename);
void printMovie(movie *m);
movie* find(int id);
~Catalog();
};

int main(int argc, char *argv[])

{
Catalog obj; //declaring catalog object
//open file for catalog.txt
char filename[100] = "catalog.txt";
ifstream infile(filename);
  
  
if(infile.is_open())
{
obj.loadData(infile);
}
else
{
cout << "Could not open input file : " << filename << endl;
return 1;
}
  
int choice = 0;
  
while(choice != 9)
{
  
cout << " ********** MAIN MENU ********** " << endl;
cout << "1 - Print Calalog" << endl;
cout << "2 - Search by Title" << endl;
cout << "3 - Search by Rating" << endl;
cout << "4 - Add Movie" << endl;
cout << "5 - Modify Movie" << endl;
cout << "6 - Delete Movie" << endl;
cout << "7 - Rent Movie" << endl;
cout << "8 - Return Movie" << endl;
cout << "9 - Quit" << endl;
cout << "Enter choice: ";
cin >> choice; //switch statements for different user choices
  
switch(choice)
{
case 1:
obj.printAll();
break;
  
case 2:
obj.printTitled();
break;
  
case 3:
obj.printRated();
break;
  
case 4:
obj.addMovie();
break;
case 5:
obj.modifyMovie();
break;
case 6:
obj.deleteMovie();
break;
case 7:
obj.rentMovie();
break;
  
case 8:
obj.returnMovie();
break;
  
case 9:
obj.saveToFile(filename);
break;
  
default:
cout << "Invalid choice. Please re-enter: " << endl;
}
}
  
return 0;
}

void Catalog::loadData(ifstream &infile)
{
int id;
while(infile >> id)
{
movie *m = new movie;
m->id = id;
infile.ignore();
infile.getline(m->title, 250);
infile >> m->year;
infile >> m->rating;
infile >> m->totalCopies;
infile >> m->rentedCopies;
m->next = NULL;
addMovie(m);
}
infile.close();
  
}

void Catalog:: printMovie(movie *m)
{
  
cout << "---------- ID: " << m->id << " ----------" << endl;
cout << "Title: " << m->title << endl;
cout << "Year: " << m->year << endl;
cout << "Rating: " << m->rating << endl;
cout << "Number of copies: " << m->totalCopies << endl;
cout << "Number rented: " << m->rentedCopies << endl;
cout << "---------------------------------------" << endl;
}

void Catalog::printAll()
{
cout << "%%%%%%%%%% Movie Catalog %%%%%%%%%%" << endl;
  
for(movie *curr = head; curr != NULL; curr = curr->next)
{
printMovie(curr);
}
cout << endl;
}

void Catalog::printRated()
{
int r = -1;
char rating[5] = "";
cout << "0 - NONE" << endl;
cout << "1 - G" << endl;
cout << "2 - PG" << endl;
cout << "3 - PG13" << endl;
cout << "4 - R" << endl;
cout << "5 - N17" << endl;
  
while(true)
{
cin >> r;
  
if(r < 0 || r > 5)
cout << "Invalid choice. Please re-enter: ";
  
else
  
break;
}
  
if(r == 0)
strcpy(rating, "NONE");
  
else if(r == 1)
strcpy(rating, "G");
  
else if(r == 2)
strcpy(rating, "PG");
  
else if(r == 3)
strcpy(rating, "PG13");
  
else if(r == 4)
strcpy(rating, "R");
  
else if(r == 5)
strcpy(rating, "N17");
  
for(movie *curr = head; curr != NULL; curr = curr->next)
{
if(strcmp(curr->rating, rating) == 0)
printMovie(curr);
}
cout << endl;
}

void Catalog::printTitled()
{
char title[250];
cout << "Title of Movie? " << endl;
cin.ignore(); //flush newline
cin.getline(title, 250);
cout << endl;
for(movie *curr = head; curr != NULL; curr = curr->next)
{
if(strcmp(curr->title, title) == 0)
{
printMovie(curr);
}
}
cout << endl;
}

movie* Catalog::find( int id)
{
for(movie *curr = head; curr != NULL; curr = curr->next)
{
if(curr->id == id)
  
return curr;
}
return NULL;
}

void Catalog::addMovie() //pass by ref
{
movie *m = new movie;
m->next = NULL;

cout << "Moview ID? (must be unique) " ;
cin >> m->id;
  
while(find(m->id) != NULL)
{
cout << "That ID is already in use. Please enter another: ";
cin >> m->id;
}
  
cin.ignore();
cout << "Title? ";
cin.getline(m->title, 250);
cout << "Year? " ;
cin >> m->year;
cout << "Movie rating? " << endl;
  
int r = -1;
cout << "0 - NONE" << endl;
cout << "1 - G" << endl;
cout << "2 - PG" << endl;
cout << "3 - PG13" << endl;
cout << "4 - R" << endl;
cout << "5 - N17" << endl;
  
while(true)
{
cin >> r;
  
if(r < 0 || r > 5)
  
cout << "Invalid choice. Please re-enter: ";
  
else
break;
}
  
if(r == 0)
strcpy(m->rating, "NONE");
else if(r == 1)
strcpy(m->rating, "G");
  
else if(r == 2)
  
strcpy(m->rating, "PG");
  
else if(r == 3)
  
strcpy(m->rating, "PG13");
  
else if(r == 4)
  
strcpy(m->rating, "R");
  
else if(r == 5)
  
strcpy(m->rating, "N17");
  
cout << "Copies? " ;
  
while(true)
  
{
  
cin >> m->totalCopies;
  
if(m->totalCopies <= 0 )
  
cout << "Copies must not be negative. Please re-enter: " ;
  
else
  
break;
  
}

m->rentedCopies = 0;
  
addMovie(m);
}

void Catalog::addMovie(movie *m)
{
if(head == NULL)
head = m;
else
{
movie* curr = head;
movie* prev = NULL;
while(curr != NULL)
{
if(strcmp(m->title, curr->title) < 0)
break;
prev = curr;
curr = curr->next;
}
  
m->next = curr;
  
if(prev == NULL)
head = m;
else
prev->next = m;
  
}

}
void Catalog::deleteMovie()
{
  
char answer;
  
cout << "Do you want to see a list of movies first? (y or n) ";
cin >> answer;
cin.ignore();
  
if(answer == 'y' || answer == 'Y')
  
printAll();
  
  
char title[250];
cout << "Enter title of movie to be deleted: ";
cin.getline(title, 250);
if(deleteMovie(title))
cout << title << " deleted successfully." << endl;
else
cout << title << " could not be deleted." << endl;
  
}

bool Catalog::deleteMovie(char *title)
{
for(movie *curr = head, *prev =NULL; curr != NULL; prev = curr, curr = curr->next)
{
if(strcmp(curr->title, title) == 0)
{
if(prev == NULL)
head = head->next;
else
prev->next = curr->next;
delete curr;
return true;
}
}
return false;
}

void Catalog::modifyMovie()
{
int id;
cout << "Enter id of movie to modify: ";
cin >> id;
movie *old = find(id);
movie *m;
if(old == NULL)
{
cout << "Movie with id = " << id << " not found. " << endl;
return;
}
else
{
//copy some old values
m = new movie;
m->id = old->id;
m->totalCopies = old->totalCopies;
m->rentedCopies = old->rentedCopies;
m->next = NULL;
deleteMovie(old->title);
}
cin.ignore();
cout << "Title? ";
cin.getline(m->title, 250);
cout << "Year? " ;
cin >> m->year;
cout << "Movie rating? " << endl;
  
int r = -1;
cout << "0 - NONE" << endl;
cout << "1 - G" << endl;
cout << "2 - PG" << endl;
cout << "3 - PG13" << endl;
cout << "4 - R" << endl;
cout << "5 - N17" << endl;
  
while(true)
{
cin >> r;
  
if(r < 0 || r > 5)
  
cout << "Invalid choice. Please re-enter: ";
  
else
break;
}
  
if(r == 0)
strcpy(m->rating, "NONE");
else if(r == 1)
strcpy(m->rating, "G");
  
else if(r == 2)
  
strcpy(m->rating, "PG");
  
else if(r == 3)
  
strcpy(m->rating, "PG13");
  
else if(r == 4)
  
strcpy(m->rating, "R");
  
else if(r == 5)
  
strcpy(m->rating, "N17");
  
addMovie(m);
}

void Catalog::returnMovie()
{
  
char answer;
  
cout << "Do you want to see a list of movies first? (y or n) ";
cin >> answer;
  
if(answer == 'y' || answer == 'Y')
  
printAll();
  
int id;
  
cout << "Enter id of movie: ";
  
while(true)
{
cin >> id;
if(id == -1)
  
break;
  
movie *m = find (id);
if(m == NULL)
  
cout << "The movie does not exist. Please re-enter(or enter -1 to quit): " << endl;
  
else
{
if(m->rentedCopies == 0)
  
cout << "No copies were rented." << endl;
  
else
{
  
m->rentedCopies--;
cout << m->title << " returned successfully. " << endl;
  
}
break;
}
}
}

void Catalog::rentMovie()
{
  
char answer;
  
cout << "Do you want to see a list of movies first? (y or n) ";
cin >> answer;
  
if(answer == 'y' || answer == 'Y')
  
printAll();
  
int id;
  
cout << "Enter id of movie: ";
  
while(true)
{
cin >> id;
  
if(id == -1)
  
break;
  
movie *m= find(id);
  
if(m == NULL)
  
cout << "The movie does not exist. Please re-enter(or enter -1 to quit): " << endl;
  
else
{
  
if(m->rentedCopies == m->totalCopies)
  
cout << "There are no copies to rent." << endl;
  
else
{
  
m->rentedCopies++;
  
cout << "One copy of " << m->title << " rented. " << endl;
  
}
  
break;
  
}
  
}
  
}

void Catalog::saveToFile(char *filename)
{
ofstream outfile(filename);
  
if(!outfile.is_open())
  
cout << "Error writing to output file " << filename << endl;
  
else
{
  
for(movie *curr = head; curr != NULL; curr = curr->next)
{

outfile << curr->id << endl;
outfile << curr->title << endl;
outfile << curr->year << endl;
outfile << curr->rating << endl;
outfile << curr->totalCopies << endl;
outfile << curr->rentedCopies << endl;
}
outfile.close();
cout << "movies written to file " << filename << endl;
}
  
}

Catalog::~Catalog()
{
movie *curr = head;
movie *temp;
while(curr != NULL)
{
temp = curr->next;
delete curr;
curr = temp;
}
}