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

MOVIES CLASS MODIFICATION- ARRAY CHANGED TO LINKED LIST Change your Movies.cpp &

ID: 3730885 • Letter: M

Question

MOVIES CLASS MODIFICATION- ARRAY CHANGED TO LINKED LIST Change your Movies.cpp & Movies.h files so that you no longer create an array of Movie pointers. Instead, you will create a LinkedList of Movie pointers. You will only have one attribute - the linked list object which will hold Movie pointers . The constructor/destructor will need to change due to the change in attributes You will no longer need the following functions: getMaxMovies(), getMoviesArray(), getAMovieFromArray(), and resizeMovieArray() You should also rename any functions to say "List" instead of "Array" so that it makes more sense You will create two new functions . insertionSort- This function should sor thte LinkedLIst of Movies in ascending order by Movie title. This function will call a function called swap in the linkedList class to swap values in the linked list when necessary. Use the insertion sort algorithm to implement this function o o binarySearch This function should search for a particular movie title to see if it is in the list. It should return -1 if the Movie title could not be found. Remember that Movie titles are of data type Text*. Use the binary search algorithm to implement this function The addMovieToList function will need to be modified so that after this function gets the title of the movie from the user, it will check to see if this movie is already in the library (by calling binarySearch function) . If the title is not in the library, then the function will proceed to get the other movie information from the user and then add this movie to the linked list. Then, it will call the insertionSort function so that the movies are put in ascending order by title o If the title is in the library then no other information will be asked from the user and the program will print "l'm sorry! [movie title] is already in in your movie library so it will not be added again." o The readMoviesFromFile function will need to be modified so that after this function reads one movie's data from the file it will check to see if this movie is already in the library (by calling binarySearch function) If the title is not in the library, then the function will add this movie to the linked list. Then, it will call the insertionSort function so that the movies are put in ascending order by title o o If the title is in the library then the program will print "[movie title] was not added because it was already in the movie library." Then it will release the memory for the three Text objects that were unnecessarily created The getNumMovies function will need to be modified to return the number of nodes in the linked list .

Explanation / Answer

//main.cpp
#include "Movies.h"
#include "Movie.h"
#include "Text.h"
#include <iostream>
using namespace std;

int main()
{
   int menuChoice;
   int movieChoice;
  
   char filename[25];
  
  
   Movies* movieLibrary = new Movies();
  
   do
   {
       cout << " What would you like to do? ";
       cout << "1. Read movies from file. ";
       cout << "2. Save movies to a file. ";
       cout << "3. Add a movie. ";
       cout << "4. Delete a movie. ";
       cout << "5. Edit a movie. ";
       cout << "6. Print all movies. ";
       cout << "7. Delete ALL movies and end the program. ";
       cout << "CHOOSE 1-7: ";
       cin >> menuChoice;
       while(menuChoice < 1 || menuChoice > 7)
       {
           cout << "That is not a valid choice. ";
           cout << "CHOOSE 1-7: ";
           cin >> menuChoice;
       }
      
       switch(menuChoice)
       {
           case 1:   cout << " What is the name of the file? (example.txt): ";
                   cin >> filename;
                   movieLibrary->readMoviesFromFile(filename);
                   break;
                  
           case 2: cout << " What do you want to name the file? (example.txt): ";
                   cin >> filename;
                   movieLibrary->saveToFile(filename);
                   break;
                  
           case 3:   //add a movie
                   movieLibrary->addMovieToList();
                   break;
                  
           case 4:   //delete a movie if there is more than one movie in the library.
                   movieLibrary->removeMovieFromList();
                   break;
                  
           case 5: //edit a movie
                   movieLibrary->editMovieInList();
                   break;
                  
           case 6: //print all movies
                   movieLibrary->displayMovies();
                   break;
                  
           case 7: //delete all movies
                   delete movieLibrary;
                   break;
                  
       }
      
   } while(menuChoice != 7);
  
   cout << " GOODBYE! ";
  
   return 0;
}
---------------------------------------------------------------------------------------------------------------------------------
//Movie.cpp
#include "Movie.h"
#include "Text.h"

Movie::Movie(Text* title, int length)
{
  
   movieTitle = title;
   movieLength = length;
}

Movie::Movie(Text* title, int length, int year, Text* genre, Text* rating, int nom, float stars) //all info is know
{
  
   movieTitle = title;
   movieLength = length;
   movieYear = year;
   movieGenre = genre;
   movieRating = rating;
   movieOscars = nom;
   movieNumStars = stars;

}

Movie::~Movie()
{
   delete movieTitle;
   delete movieGenre;
   delete movieRating;
}

void Movie::printMovieDetails()
{
   cout << endl;
   cout << right << setw(30) << "Movie Title: " << left;
   movieTitle->displayText();
   cout << endl;
   cout << right << setw(30) << "Length (minutes): " << left << movieLength << endl;
   cout << right << setw(30) << "Year Released: " << left << movieYear << endl;
   cout << right << setw(30) << "Genre: " << left;
   movieGenre->displayText();
   cout << endl;
   cout << right << setw(30) << "Rating: " << left;
   movieRating->displayText();
   cout << endl;
   cout << right << setw(30) << "Number of Oscars Won: " << left << movieOscars << endl;
   cout << right << setw(30) << "Number of Stars: " << left << movieNumStars << endl << endl;
}

void Movie::printMovieDetailsToFile(ofstream &outFile)
{
   char temp[1000];
   strncpy(temp, movieTitle->getText(), 1000);
   outFile << temp << endl;
   outFile << movieLength << endl;
   outFile << movieYear << endl;
   strncpy(temp, movieGenre->getText(), 1000);
   outFile << temp << endl;
   strncpy(temp, movieRating->getText(), 1000);
   outFile << temp << endl;
   outFile << movieOscars << endl;
   outFile << movieNumStars << endl;
}

void Movie::editMovie()
{
   int choice;
   Text* tempText;
   char temp[100];
  
   do
   {
       cout << " Which detail do you wish to edit? ";
       cout << "1. Title ";
       cout << "2. Length ";
       cout << "3. Year ";
       cout << "4. Genre ";
       cout << "5. Rating ";
       cout << "6. Number of Oscars Won ";
       cout << "7. Number of Stars ";
       cout << "8. DONE EDITING ";
       cout << "CHOOSE 1-8: ";
       cin >> choice;
       while(choice < 1 || choice > 8)
       {
           cout << " OOPS! Enter choice 1 through 8: ";
           cin >> choice;
       }
       cin.ignore();
      
       switch(choice)
       {
           case 1: cout << " Current Title: ";
                   movieTitle->displayText();
                   cout << " NEW TITLE: ";
                   cin.getline(temp, 100);
                   movieTitle->editText(temp);
                   break;
          
           case 2:   cout << " Current Length: " << movieLength;
                   cout << " NEW LENGTH: ";
                   cin >> movieLength;
                   break;
                  
           case 3: cout << " Current Year: " << movieYear;
                   cout << " NEW LENGTH: ";
                   cin >> movieYear;
                   break;
                  
           case 4:   cout << " Current Genre: ";
                   movieGenre->displayText();
                   cout << " NEW GENRE: ";
                   cin.getline(temp, 100);
                   movieGenre->editText(temp);
                   break;
                  
           case 5: cout << " Current Rating: ";
                   movieRating->displayText();
                   cout << " NEW GENRE: ";
                   cin.getline(temp, 100);
                   movieRating->editText(temp);
                   break;
          
           case 6: cout << " Current Number of Oscars Won: " << movieOscars;
                   cout << " NEW NUMBER OF OSCARS: ";
                   cin >> movieOscars;
                   break;
                  
           case 7:   cout << " Current Star Rating from IMDB: " << movieNumStars;
                   cout << " NEW STAR RATING: ";
                   cin >> movieNumStars;
                   break;
       }
   }while(choice != 8);
}

Text* Movie::getMovieTitle() const
{
   return movieTitle;
}

int Movie::getMovieLength() const
{
   return movieLength;
}

int Movie::getMovieYear() const
{
   return movieYear;
}

Text* Movie::getMovieGenre() const
{
   return movieGenre;
}

Text* Movie::getMovieRating() const
{
   return movieRating;
}

int Movie::getMovieOscars() const
{
   return movieOscars;
}

float Movie::getMovieNumStars() const
{
   return movieNumStars;
}

void Movie::setMovieTitle(Text* title)
{
   movieTitle = title;
}

void Movie::setMovieLength(int length)
{
   movieLength = length;
}

void Movie::setMovieYear(int year)
{
   movieYear = year;
}

void Movie::setMovieGenre(Text* genre)
{
   movieGenre = genre;
}

void Movie::setMovieRating(Text* rating)
{
   movieRating = rating;
}

void Movie::setMovieOscars(int oscars)
{
   movieOscars = oscars;
}

void Movie::setMovieNumStars(int stars)
{
   movieNumStars = stars;
}
------------------------------------------------------------------------------------------------------
//Movie.h
#ifndef MOVIE_H
#define MOVIE_H

#include "Text.h"
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstring>
using namespace std;

class Movie
{
   private:
       Text* movieTitle;
       int movieLength;
       int movieYear;
       Text* movieGenre;
       Text* movieRating;
       int movieOscars;
       float movieNumStars;
      
   public:
       // Movie constructor(overloaded function)
      
       Movie(Text*, int);

       // Movie constructor (overloaded function)
      
       Movie(Text*, int, int, Text*, Text*, int, float);

      
       //~Movie destructor
      
       ~Movie();


      
       //editMovie
      
       void editMovie();

      

       //printMovieDetails
      
       void printMovieDetails();

      
      
       //printMovieDetailsToFile
      
       void printMovieDetailsToFile(ofstream &outFile);

      
      
      
       //accessor functions to get the attribute values
       Text* getMovieTitle() const;
       int getMovieLength() const;
       int getMovieYear() const;
       Text* getMovieGenre() const;
       Text* getMovieRating() const;
       int getMovieOscars() const;
       float getMovieNumStars() const;

       //mutator functions to change the attribute values to a new value, which is a parameter of each function
       void setMovieTitle(Text*);
       void setMovieLength(int);
       void setMovieYear(int);
       void setMovieGenre(Text*);
       void setMovieRating(Text*);
       void setMovieOscars(int);
       void setMovieNumStars(int);
};

#endif
-------------------------------------------------------------------------------------------------------
//Movies.cpp
#include "Movies.h"

Movies::Movies()
{
   linkedList = new LinkedList<Movie*>;
   }


Movies::~Movies()
{
  
for(int x = 0; x < linkedList->getLength();x++){
   delete linkedList->getNodeValue(x)->getMovieTitle();
   delete linkedList->getNodeValue(x)->getMovieGenre();
   delete linkedList->getNodeValue(x)->getMovieRating();
  
   }
  
}


void Movies::addMovieToList()
{
   char tempString[100];
   int length, year, numOscars;
   double numStars;
   Text* title;
   Text* genre;
   Text* rating;
  
   //add a movie
   cin.ignore(); //remove the from the keyboard buffer
   cout << " MOVIE TITLE: ";
   cin.getline(tempString, 100);
   title = new Text(tempString);
  
       cout << " MOVIE LENGTH (in minutes): ";
       cin >> length;
       cout << " MOVIE YEAR: ";
       cin >> year;
       cin.ignore();
       cout << " MOVIE GENRE: ";
       cin.getline(tempString, 100);
       genre = new Text(tempString);
       cout << " MOVIE RATING: ";
       cin.getline(tempString, 100);
       rating = new Text(tempString);
       cout << " NUMBER OF OSCARS WON: ";
       cin >> numOscars;
       cout << " STAR RATING (out of 10): ";
       cin >> numStars;
      
       //create the movie
       if(-1==binarySearch(title)){
       Movie* Movie(title, length, year, genre, rating, numOscars, numStars);
      
      
       linkedList->appendNode(oneMovie);
       insertionSort();
       }
      
  
   else{
       cout<<"I'm sorry ";
       title->displayText();
       cout<< " is already in your movie library so it will not be added again";  
       delete title;
       delete rating;
       delete genre;
   }
  
}  

void Movies::removeMovieFromList()
{
   int movieChoice;
  
   if(linkedList->getLength() <= 1)
   {
       cout << endl << "There must always be at least one movie in your library. You can't";
       cout << " remove any movies right now or you will have no movies in your library. ";
   }
   else
   {
       cout << " Choose from the following movies to remove: ";
       displayMovieTitles();
       cout << " Choose a movie to remove between 1 & " << linkedList->getLength() << ": ";
       cin >> movieChoice;
      
       while(movieChoice < 1 || movieChoice > linkedList->getLength())
       {
           cout << " Oops! You must enter a number between 1 & " << linkedList->getLength() << ": ";
           cin >> movieChoice;
       }

       int movieIndexToBeRemoved = movieChoice-1;
       Text* movieTitle;
       movieTitle = linkedList->getNodeValue(movieIndexToBeRemoved)->getMovieTitle();
      
       cout << " The movie "";
       movieTitle->displayText();
       cout << "" has been successfully deleted. ";  
      
       //destroy this movie
      
       int numElementsToMoveBack = (linkedList->getLength()) - 1;
      
       linkedList->deleteNode(movieIndexToBeRemoved);
      
   }
}

void Movies::editMovieInList()
{
   int movieChoice;
  
   cout << " Choose from the following movies to edit: ";
   displayMovieTitles();
  
   cout << " Choose a movie to edit between 1 & " << linkedList->getLength() << ": ";
   cin >> movieChoice;
  
   while(movieChoice < 1 || movieChoice > linkedList->getLength())
   {
       cout << " Oops! You must enter a number between 1 & " <<linkedList->getLength() << ": ";
       cin >> movieChoice;
   }
  
   Movie*>   
   oneMovie->editMovie();
}

void Movies::displayMovies()//start here!!
{
   if(linkedList->getLength() > 0)
   {
       for(int x=0; x < linkedList->getLength(); x++)
       {
           cout << endl << right << setw(50) << "----------MOVIE " << (x+1) << "----------";
           linkedList->getNodeValue(x)->printMovieDetails(); //function is in Movie.cpp
       }
   }
   else  
       cout << " There are no movies in your library yet.";
}

void Movies::displayMovieTitles()
{
   Text* movieTitle;
  
   for(int x=0; x<linkedList->getLength(); x++)
   {
       cout << " MOVIE " << (x+1) <<": ";
       movieTitle =linkedList->getNodeValue(x)->getMovieTitle();
       movieTitle->displayText();
   }
}

void Movies::readMoviesFromFile(char *filename)
{   
   int numMoviesReadFromFile = 0;
   ifstream inFile;
   char temp[100];
   Text* title;
   Text* genre;
   Text* rating;
   Movie* oneMovie;
   int movieLength; //length of movie in minutes
   int movieYear; //year released
   int movieOscars; //number of oscars won
   float movieNumStars; //from IMDB out of 10 stars
  
   inFile.open(filename);
   if(inFile.good())
   {
       inFile.getline(temp, 100);
       while(!inFile.eof())
       {  
          
           title = new Text(temp);//create a text for the movie title
          
           inFile >> movieLength;
           inFile >> movieYear;
           inFile.ignore(); //get rid of in the inFile buffer
           inFile.getline(temp, 100); //read in genre
           genre = new Text(temp); //create a text for genre
           inFile.getline(temp, 100); //read in rating
           rating = new Text(temp); //create a text for rating
           inFile >> movieOscars;
           inFile >> movieNumStars;
           inFile.ignore(); //get rid of in the inFile buffer
                   if(-1!=binarySearch(title)){
               title->displayText();
               cout<<" was not added because it was already in the movie library."<<endl;
               delete title;
               delete rating;
               delete genre;
                   }
           else{
           //one movie has been read from the file. Now create a movie object
           // title->displayText();
           cout<<" movie is being added";
           Movie(title, movieLength, movieYear, genre, rating, movieOscars, movieNumStars);
          
           //now add this movie to the library
          
           linkedList->appendNode(oneMovie);
          
          
          
           //confirm addition to user
           cout << endl;
           title->displayText();
           cout << " was added to the movie library! ";
           numMoviesReadFromFile++;
           }
                      
           inFile.getline(temp, 100); //read in the next movie title if there is one
          
          
       }
      
       cout << " " << numMoviesReadFromFile << " movies were read from the file and added to your movie library. ";
   }
   else
   {
       cout << " Sorry, I was unable to open the file. ";
   }
   insertionSort();
}

void Movies::saveToFile(char *filename)
{
   ofstream outFile;
  
   outFile.open(filename);
  
   for(int x=0; x<linkedList->getLength(); x++)
   {
       linkedList->getNodeValue(x)->printMovieDetailsToFile(outFile); //function in Movies.cpp
   }
   outFile.close();
  
   cout << " All movies in your library have been printed to " << filename << endl;
}


int Movies::getNumMovies() const
{
   return linkedList->getLength();
}


int Movies::binarySearch(Text* title){
   int found = -1;
   if(linkedList->getLength() == 0){return found;}
  
   for(int x = 0; x<= linkedList->getLength()-1;x++){
       if(0==strcmp(linkedList->getNodeValue(x)->getMovieTitle()->getText(),title->getText())){
           found = x;
       }
   }
  
   return found;
  
}

void Movies::insertionSort(){
   int j;
       for(int i =1 ; i< linkedList->getLength();i++){
           j = i;
               while(j>0 && 0<strcmp(linkedList->getNodeValue(j-1)->getMovieTitle()->getText(),linkedList->getNodeValue(j)->getMovieTitle()->getText()))
                   {
                       linkedList->swap(j-1,j);
                       j--;
                   }
   }
}


----------------------------------------------------------------------------------------------------------------
//movies.h
#ifndef MOVIES_H
#define MOVIES_H

#include "LinkedList.h"
#include "Movie.h"
#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

class Movies
{
   private:
       LinkedList <Movie*>*linkedList;      
      
      
   public:
       // Movies constructor
      
       Movies();
      
  
  
       // ~Movies destructor
      
       ~Movies();
      
      

       //addMovieToList
      
       void addMovieToList();

      

       //displayMovies
      
       void displayMovies();

      
      
       //displayMovieTitles
      
       void displayMovieTitles();

      
      
       //readMoviesFromFile
      
       void readMoviesFromFile(char* filename);

      
      
       //removeMovieFromList
      
       void removeMovieFromList();

      
      
       // editMovieInList
      
       void editMovieInList();
      
      
      
       //saveToFile
      
       void saveToFile(char *filename);

      
      
       //accessor functions to get the attribute values
       int getNumMovies() const;
      
       void insertionSort();
      
       int binarySearch(Text*);
  
};

#endif
--------------------------------------------------------------------------------------------------
//Text.cpp
#include "Text.h"
#include <iomanip>
#include <cstring>

using namespace std;

Text::Text(const char* text)
{  
   //get the length of the string passed to this function
   textLength = strlen(text);
  
   //dynamically allocate a new character array
   char* tempTextArray = new char[(textLength)+1];
  
   //Now put text inside of tempTextArray
   strcpy(tempTextArray, text);
  

   this->textArray = tempTextArray;  
}

Text::~Text()
{
   //release the dynamically allocated character array
   delete [] textArray;
}

void Text::editText(const char* newTextArray)
{
   delete [] textArray;
  
   //get the length of the string passed to this function
   textLength = strlen(newTextArray);
  
   //cout << " The length of " << newTextArray << " is " << textLength << endl << endl;
  
   //dynamically allocate a new character array
   char* tempTextArray = new char[(textLength)+1];
  
  
   strcpy(tempTextArray, newTextArray);
  

   this->textArray = tempTextArray;  
}

void Text::displayText() const
{
   cout << textArray;
}

const char* Text::getText() const
{
   return textArray;
}

int Text::getLength() const
{
   return textLength;
}
----------------------------------------------------------------------------------------------
//Text.h
#ifndef TEXT_H
#define TEXT_H

#include <iostream>
using namespace std;

class Text
{
   private:
       const char* textArray;
       int textLength;
      
   public:
      
       //Text (constructor)
      
       Text(const char*);

       // ~Text (destructor)
      
       ~Text();
      
       // displayText()
      
       void displayText() const;  
      
       //getText()
      
       const char* getText() const;

       // getLength()
      
       int getLength() const;
      
       // editText()
              
       void editText(const char*);
  
  
}; //end of Text class
#endif
--------------------------------------------------------------------------------------------------
//LinkedList.h
#include <cstddef>

#include <iostream>

using namespace std;

template <typename T>

class LinkedList{
   public :
      
       struct ListNode{
           T value;
           struct ListNode* next;  
       };
  
   private:
       ListNode* head;
       ListNode* tail;
       int numNodes;
  
   public:
      
       LinkedList();
       ~LinkedList();
  
       int getLength();
       T getNodeValue(int);
       void appendNode(T);
       void deleteNode(int);
       int getNumNodes(){return numNodes;}
       ListNode* getHead(){return head;};
       ListNode* getTail(){return tail;};
       void swap(int,int);
};

template <typename T>
LinkedList<T>::LinkedList(){
  
   head = NULL;
   tail = NULL;
   numNodes = 0;
}

template <typename T>
LinkedList<T>::~LinkedList(){
  
   ListNode* nodePtr = head;
   while (head!=NULL){
       head = nodePtr->next;
       delete nodePtr;
       nodePtr= nodePtr->next;      
   }
   head = NULL;
   tail = NULL;
}

template <typename T>
int LinkedList<T>::getLength(){
  
   int counter =0;
   if(!head){
       return 0;
   }
  
   ListNode* nodePtr = head;
   while(nodePtr){
       nodePtr = nodePtr->next;
       counter++;
   }
   return counter;
}

template <typename T>
T LinkedList<T>::getNodeValue(int position){
  
   int counter = 0;
   ListNode* nodePtr = head;
   while(counter != position){
       nodePtr = nodePtr->next;
       counter++;
   }
   return nodePtr->value;
}

template <typename T>
void LinkedList<T>::appendNode(T object){
  
   ListNode* newNode;
   newNode = new ListNode;
   newNode->value = object;
   newNode->next = NULL;

   if(!head){
       head =newNode;
       tail = newNode;  
       numNodes++;
   }
   else{
       tail->next = newNode;
       tail=newNode;
       numNodes++;
   }

}

template <typename T>
void LinkedList<T>::deleteNode(int position){
  
   int counter = 0;
   while(position >= numNodes){
       cout<<"please enter a valid position ";
       cin>>position;
       }
   ListNode* nodePtr = head;
   ListNode * nodePtr2 = head->next;
   if(position == 0){
       head = head->next;
       delete nodePtr;
       }
   if(position == numNodes-1){
       while(nodePtr->next != tail){
           nodePtr = nodePtr->next;
           }
       tail = nodePtr;
       nodePtr = nodePtr->next;  
       delete nodePtr;
       }
   while(counter != position){
       counter++;
       nodePtr = nodePtr->next;
       nodePtr2 = nodePtr2->next;
   }
   nodePtr->value = nodePtr2->value;
   nodePtr->next = nodePtr2->next;
   delete nodePtr2;
   numNodes--;
}

template <typename T>
void LinkedList<T>::swap (int objectGreater, int objectLesser){
   ListNode * nodePtr = head;
   ListNode* nodePtrPlus = nodePtr->next;
   int counter = 0;
   while(counter != objectGreater){
       nodePtr = nodePtr->next;
       nodePtrPlus = nodePtr->next;
       counter++;
   }
   T temp = nodePtr->value;
   nodePtr->value = nodePtrPlus->value;
   nodePtrPlus->value = temp;
  
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote