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

The DVD Collection written in C++ The point of this program is to make a class t

ID: 3916164 • Letter: T

Question

The DVD Collection written in C++

The point of this program is to make a class that will hold a collection of DVD movies. It will utilize an array of objects contained within an object.

First, begin by making a class that is capable of holding the information about a single DVD. This information should include the title, the year, running_time, movie_type, and rating (G, PG-13, R, or X). You will need a full set of accessor functions and mutator functions, and you should overload the input and output stream operators. Test this class before moving on to the next phase by writing a very short main that declares a movie object and allows you to enter the data about a movie and see it. I would also be good to see if you can write your one movie out to a file and reload the data from the file.

Now, declare another class which holds a collection of movies. Its principal private variable will be an array of DVD objects, capable of holding at least 200 movies. This class should offer the ability to:

insert an additional movie

remove a movie that has become lost or destroyed

display all the movies

display all the movies of a certain rating

search for a movie by title

sort and display all movies alphabetically by title

sort and display all movies by the year made

Your application should offer a menu of the above options to the user that runs in a continuous loop until the user is ready to quit your program. Also, since you will want to preserve the list of DVD's from one running of the program to the next, the program should write the DVD list out to a file when shutting down and then reload from the same file during start-up. (If no datafile is present the program will assume that this is the first time it has been run.) The file interaction does not need to be visible to the user. They will simply start their program and it will “magically” remember the previous state of their DVD library.

Explanation / Answer

//main.cpp
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include "dvdlibrary.h"
using namespace std;

int main()
{
   int x=1;
   string name, score, type, choice;
   int date, length;
   dvd_library collection; //input from file
   while(x==1)
   {
       cout << "------------------------" << endl;
       cout << "a - insert an additional movie." << endl;
       cout << "b - remove a movie that has become lost or destroyed." << endl;
       cout << "c - display all movies." << endl;
       cout << "d - display movies organized by type." << endl;
       cout << "e - display all the movies of a certain rating." << endl;
       cout << "f - search for movie by title." << endl;
       cout << "g - sort and disaply all movies alphabetically by title." << endl;
       cout << "h - sort and display all movies by the year made." << endl;
       cout << "x - enter x to exit." << endl;
       cout << "------------------------" << endl;
       cin >> choice;
       cout << endl;
       if (choice=="a") //insert an additional movie
       {
           cout << "Enter the movie's title with underscores for spaces: ";
           cin >> name;
           cout << "Enter the movie's rating: ";
           cin >> score;
           cout << "Enter the movie's genre: ";
           cin >> type;
           cout << "Enter the movie's release date: ";
           cin >> date;
           cout << "Enter the movie's length in minutes: ";
           cin >> length;
           collection.insert_movie(name, score, type, date, length);
       }
       if (choice=="b") //remove a movie that has become lost or destroyed
       {
           cout << "Enter the title of the movie you would like to remove: ";
           cin >> name;
           collection.remove_movie(name);
       }
       if (choice=="c") //display all movies
       {
           collection.display_movies();
       }
       if (choice=="d") //display movies organized by type
       {
           collection.sort_movie_types();
           collection.display_movies();
       }
       if (choice=="e") //display all movies of a certain rating
       {
           cout << "Enter a rating to search for: ";
           cin >> score;
           collection.display_ratings(score);
       }
       if (choice=="f") //search for a movie by title
       {
           cout << "Enter a movie title to search for: ";
           cin >> name;
           collection.search_movies(name);
       }
       if (choice=="g")//sort and display all movies alphabetically by title
       {
           collection.alphabetize_movies();
           collection.display_movies();
       }
       if (choice=="h")//search and display all movies by the year made
       {
           collection.sort_years();
           collection.display_movies();
       }
       if (choice=="x") //output to file
       {
           collection.output_movies();
           return 0;
       }
   }
}
-------------------------------------------------------------------------------------------------------------
//dvdlibrary.cpp
#include <iostream>
#include <string>
#include <iomanip>
#include "dvdlibrary.h"
#include <fstream>
using namespace std;

//Constructors

dvd_library::dvd_library()
{
    //dvd library[MAX_LIBRARY_SIZE];
    used=0;
    dvd temp;
    ifstream in_stream;
    in_stream.open("data.txt");
    in_stream >> temp;
    while (! in_stream.eof())
    {
        //in_stream >> temp; //dvd object
        library[used].set_title(temp.get_title());
        library[used].set_rating(temp.get_rating());
        library[used].set_movie_type(temp.get_movie_type());
        library[used].set_year(temp.get_year());
        library[used].set_run_time(temp.get_run_time());
        used++;
        in_stream >> temp; //dvd object
    }
    in_stream.close();
    cout << "Loaded " << used << " movies from data.txt." << endl;
}

void dvd_library::insert_movie(string name, string score, string type, int date, int length)
{
    dvd tmp(name, score, type, date, length);
    library[used]=tmp;
    used++;
}

void dvd_library::display_movies()
{
    int i=0;
    while (i<used)
    {
        cout << library[i];
        i++;
    }
}

void dvd_library::display_ratings(string rating)
{
    int i=0;
    while (i<used)
    {
        if (library[i].get_rating()==rating)
        {
            cout << library[i];
        }
        i++;
    }
}

void dvd_library::search_movies(string name)
{
    int x=0, i=0;
    while (i<used && x==0)
    {
        if (library[i].get_title()==name)
        {
            cout << "Movie found." << endl;
            cout << library[i];
            x=1;
        }
        i++;
    }
    if (x==0)
    {
        cout << "Movie not found." << endl;
    }
}

void dvd_library::remove_movie(string name)
{
//Takes the last movie and inserts it where the movie to be deleted is.
//decrements used so that the movie at the end will be overwritten.
    int x=0, i=0;
    while (i<used && x==0)
    {
        if (library[i].get_title()==name)
        {
            library[i].set_title(library[used-1].get_title());
            library[i].set_rating(library[used-1].get_rating());
            library[i].set_movie_type(library[used-1].get_movie_type());
            library[i].set_year(library[used-1].get_year());
            library[i].set_run_time(library[used-1].get_run_time());
            used--;
            cout << "Movie removed" << endl;
            x=1;
        }
        i++;
    }
    if (x==0)
    {
        cout << "Movie not found" << endl;
    }
}

void dvd_library::output_movies()
{
    int i=0;
    ofstream out_stream;
    out_stream.open("data.txt");
    while (i<used)
    {
        out_stream << library[i];
        i++;
    }
    out_stream.close();
    cout << used << " movies written to data.txt." << endl;
}

void dvd_library::alphabetize_movies()
{
    int n=0, loc=0, i=0;
    string tmp;
    dvd temp;
    while (i < used)
    {
        tmp=library[i].get_title(); //tmp gets i's title
        loc=i;
        n=i+1;
        while (n < used)
        {
            if (library[n].get_title()<tmp) // lower than candidate tmp
            {
                tmp=library[n].get_title();
                loc=n; //stores the location of the dvd object with the
            }           //lowest alphabetical title
            n++;
        }
        //cout << tmp << " is lowest, set to " << i << endl;
        //who needs an overloaded =
        //cout << "temp gets " << i << endl;
        temp.set_title(library[i].get_title());
        temp.set_rating(library[i].get_rating());
        temp.set_movie_type(library[i].get_movie_type());
        temp.set_year(library[i].get_year());
        temp.set_run_time(library[i].get_run_time());
        //cout << i << " gets " << loc << endl;
        library[i].set_title(library[loc].get_title());
        library[i].set_rating(library[loc].get_rating());
        library[i].set_movie_type(library[loc].get_movie_type());
        library[i].set_year(library[loc].get_year());
        library[i].set_run_time(library[loc].get_run_time());
        //cout << loc << " gets temp" << endl;
        library[loc].set_title(temp.get_title());
        library[loc].set_rating(temp.get_rating());
        library[loc].set_movie_type(temp.get_movie_type());
        library[loc].set_year(temp.get_year());
        library[loc].set_run_time(temp.get_run_time());
        i++; //we assume i now has the dvd obj. with the lowest movie title
    }

}

void dvd_library::sort_years()           //quick and dirty, but I only had
{                                       //to change two calls.
    int n, tmp, loc=0, i=0;               //wonderfully inefficient
    dvd temp;
    while (i < used)
    {
        tmp=library[i].get_year();
        loc=i;
        n=i+1;
        while (n < used)
        {
            if (library[n].get_year()<=tmp)
            {
                tmp=library[n].get_year();
                loc=n; //stores the location of the dvd object with the
            }           //lowest alphabetical genre
            n++;
        }
        //cout << tmp << " is lowest, set to " << i << endl;
        //who needs an overloaded =
        //cout << "temp gets " << i << endl;
        temp.set_title(library[i].get_title());
        temp.set_rating(library[i].get_rating());
        temp.set_movie_type(library[i].get_movie_type());
        temp.set_year(library[i].get_year());
        temp.set_run_time(library[i].get_run_time());
        //cout << i << " gets " << loc << endl;
        library[i].set_title(library[loc].get_title());
        library[i].set_rating(library[loc].get_rating());
        library[i].set_movie_type(library[loc].get_movie_type());
        library[i].set_year(library[loc].get_year());
        library[i].set_run_time(library[loc].get_run_time());
        //cout << loc << " gets temp" << endl;
        library[loc].set_title(temp.get_title());
        library[loc].set_rating(temp.get_rating());
        library[loc].set_movie_type(temp.get_movie_type());
        library[loc].set_year(temp.get_year());
        library[loc].set_run_time(temp.get_run_time());
        i++; //we assume i now has the dvd obj. with the lowest release date
    }
}

void dvd_library::sort_movie_types()       //quick and dirty, but I only had
{                                           //to change two calls.
    int n, loc=0, i=0;                       //wonderfully inefficient
    string tmp;
    dvd temp;
    while (i < used)
    {
        tmp=library[i].get_movie_type();
        loc=i;
        n=i+1;
        while (n < used)
        {
            if (library[n].get_movie_type()<tmp)
            {
                tmp=library[n].get_movie_type();
                loc=n; //stores the location of the dvd object with the
            }           //lowest alphabetical genre
            n++;
        }
        //cout << tmp << " is lowest, set to " << i << endl;
        //who needs an overloaded =
        //cout << "temp gets " << i << endl;
        temp.set_title(library[i].get_title());
        temp.set_rating(library[i].get_rating());
        temp.set_movie_type(library[i].get_movie_type());
        temp.set_year(library[i].get_year());
        temp.set_run_time(library[i].get_run_time());
        //cout << i << " gets " << loc << endl;
        library[i].set_title(library[loc].get_title());
        library[i].set_rating(library[loc].get_rating());
        library[i].set_movie_type(library[loc].get_movie_type());
        library[i].set_year(library[loc].get_year());
        library[i].set_run_time(library[loc].get_run_time());
        //cout << loc << " gets temp" << endl;
        library[loc].set_title(temp.get_title());
        library[loc].set_rating(temp.get_rating());
        library[loc].set_movie_type(temp.get_movie_type());
        library[loc].set_year(temp.get_year());
        library[loc].set_run_time(temp.get_run_time());
        i++; //we assume i now has the dvd obj. with the lowest movie rating
    }

}
------------------------------------------------------------------------------------------------------------
//dvdlibrary.h
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include "dvd.h"
using namespace std;

class dvd_library
{
public:
    dvd_library();
    void insert_movie(string name, string score, string type, int date, int length);
    void display_movies();
    void display_ratings(string rating);
    void search_movies(string name);
    void remove_movie(string name);
    void output_movies();
    void alphabetize_movies();
    void sort_movie_types();
    void sort_years();
private:
    dvd library[200]; //an array called library of type dvd
    int used;
  
};
---------------------------------------------------------------------------------------------------
//dvd.cpp
#include <iostream>
#include <string>
#include <iomanip>
#include "dvd.h"
#include <fstream>
using namespace std;

//Contsructors

dvd::dvd(): title(""), rating(""), movie_type(""), year(0), run_time(0) {}

dvd::dvd(string name, string score, string type, int date, int length)
{
    title=name;
    rating=score;
    movie_type=type;
    year=date;
    run_time=length;
}

//Mutator functions

void dvd::set_title(string name)
{
    title=name;
}

void dvd::set_rating(string score)
{
    rating=score;
}

void dvd::set_movie_type(string type)
{
    movie_type=type;
}

void dvd::set_year(int date)
{
    year=date;
}

void dvd::set_run_time(int length)
{
    run_time=length;
}

//Accessor functions

string dvd::get_title()
{
    return title;
}

string dvd::get_rating()
{
    return rating;
}

string dvd::get_movie_type()
{
    return movie_type;
}

int dvd::get_year()
{
    return year;
}

int dvd::get_run_time()
{
    return run_time;
}

//Overloaded input/output functions

istream& operator >> (istream& fin, dvd& dvd)
{
    fin >> dvd.title >> dvd.rating >> dvd.movie_type >> dvd.year >> dvd.run_time;
    return fin;
}

ostream& operator << (ostream& fout, const dvd& dvd)
{
    if (fout==cout)
    {
        fout << "Title: " << dvd.title << endl;
        fout << "Rating: " << dvd.rating << endl;
        fout << "Genre: " << dvd.movie_type << endl;
        fout << "Release Date: " << dvd.year << endl;
        fout << "Length: " << dvd.run_time << " minutes" << endl;
        fout << endl;
        return fout;
    }
    else
    {
        fout << dvd.title << " " << dvd.rating << " " << dvd.movie_type << " ";
        fout << dvd.year << " " << dvd.run_time << endl;
        return fout;
    }
}
------------------------------------------------------------------------------------------------------
//dvd.h
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;

class dvd
{
public:
    dvd();
    dvd(string name, string score, string type, int date, int length);
    void set_title(string name);
    void set_rating(string score);
    void set_movie_type(string type);
    void set_year(int date);
    void set_run_time(int length);
    string get_title();
    string get_rating();
    string get_movie_type();
    int get_year();
    int get_run_time();
    friend istream& operator >> (istream& fin, dvd& dvd);
    friend ostream& operator << (ostream& fout, const dvd& dvd);
private:
    string title;
    string rating;
    string movie_type;
    int year;
    int run_time;
};

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