Managing a Movie Collection

Problem

Part 1

  • You plan to build a program to manage a movie collection
  • All you care about are the title, the duration in hours, and your rating of each movie (out of 5 stars)
  • Your rating may change over time, but the title and duration are immutable
  • What data type(s) would we use to represent an entry in your collection?
  • Develop a class to represent a movie
  • What are the data members? member functions?
  • To test your class, write a program that creates your favorite movie and your least favorite movie and print them to the console

Part 2

  • To test your class further, write a program that creates 10 movies and print them to the console (using a static function)
  • For each movie, prompt for an updated rating

Solution

Source.cpp

This is the source file containing the main function to test our Movie class.

/*
There are no test cases for this example as it is simply a demonstration of using classes
At the point at which updated ratings are prompted for, one could potentially try different test cases
*/
 
#include <iostream>
#include <vector>
#include "Movie.h" // This is how our program knows what a Movie object is
 
using namespace std;
 
/*
	Function to print a list of movies to the console
	@param movies list of movies to print
*/
void print_movies(const vector<Movie> & movies)
{
	for (int i = 0; i < movies.size(); i++)
	{
		// Note that the to_string() function of the Movie class must be const
		// otherwise we cannot guarantee that the function parameter will be unchanged
		// by the function, as indicated by the const above
		cout << movies[i].to_string() << endl;
	}
}
 
int main()
{
	// This program simulates the creation of movie objects purely to demonstrate how classes and objects are used
 
	// Create a new Movie object using a constructor that takes a title, a duration, and a rating
	Movie favorite_movie("Jurassic Park 4",2.5,4.5);
 
	// Here are two accessor functions that allow us to access 
	// (but not modify) data memembers of objects of the Movie class
	cout << "Favorite movie is " << favorite_movie.get_title() << endl;
	cout << "It's rating is " << favorite_movie.get_my_rating() << endl;
 
	// Here is a mutator function to allow us to change the value of a data
	favorite_movie.update_my_rating(4.9);
 
	// Check that the value has actually changed
	cout << "It's new rating is " << favorite_movie.get_my_rating() << endl << endl;
 
	// It is always good to have a to_string function that returns a string representation of the object
	cout << favorite_movie.to_string() << endl << endl;
 
	// We can create multiple objects of the same class
	Movie worst_movie("The Last Airbender", 2.0, 0.1);
 
	cout << worst_movie.to_string() << endl;
 
	// We can create a vector of objects of a particular class
	vector<Movie> movies(10);
	movies[0] = Movie("Shrek", 1.8, 5.0);
	movies[1] = Movie("Count of Monte Cristo", 2.3, 4.9);
	movies[2] = Movie("Vertigo", 1.9, 4.5);
	movies[3] = Movie("Airplane", 1.7, 4.9);
	movies[4] = Movie("The Village", 1.6, 1.4);
	movies[5] = Movie("Eragon", 1.6, .3);
	movies[6] = Movie("Teenage Mutant Ninja Turtles", 1.4, .6);
	movies[7] = Movie("Man of Steel", 1.6, 3.6);
	movies[8] = Movie("The Majestic", 2.3, 4.85);
	movies[9] = Movie("Duck Soup", 1.6, 4.78);
 
	// This is a static function, in contrast to 
	// the member functions called (via dot-notation) on an object
	print_movies(movies);
 
	// Prompt for a new rating for each of our movies
	for (int i = 0; i < movies.size(); i++)
	{
		cout << movies[i].get_title() << " has current rating " 
			<< movies[i].get_my_rating() << endl;
		cout << "New rating: ";
		double new_rating;
		cin >> new_rating;
 
		movies[i].update_my_rating(new_rating);
	}
 
	// Use the same static function to view movies with updated ratings
	print_movies(movies);
 
	system("pause");
	return 0;
}

Movie.h

#include <string>
 
using namespace std;
 
// These lines make it so that if a header is circuitously included twice
// then the class is defined only once
#ifndef _MOVIE_H
#define _MOVIE_H
 
// class names should be capitalized and camel-case
class Movie
{
public:
	// CONSTRUCTORS
	/*
	Default constructor (called when no arguments are given)
	*/
	Movie();
 
	/*
	Constructor to create new movie with specified title, duration and rating
	@param _title the title that should be assigned to the newly created Movie object
	@param _duration the duration of the newly created Movie object
	@param _my_rating the rating to initially assign to the newly created Movie object
	*/
	Movie(string _title, double _duration, double _my_rating);
 
	// MEMBER FUNCTIONS
	// Accessors
 
	/*
	Getter for the title of this Movie object
	@return title of this Movie object
	*/
	string get_title() const; // const means this function won't change the data members
 
	/*
	Getter for the duration of this Movie object
	@return duration of this Movie object
	*/
	double get_duration() const;
 
	/*
	Getter for my rating of this Movie object
	@return my rating of this Movie object
	*/
	double get_my_rating() const;
 
	// Mutators
 
	/*
	Assigns a new value to my rating for this Movie object
	@param new_rating the rating to assign to this Movie object
	*/
	void update_my_rating(double new_rating);
 
	/*
	Returns a string representation of this Movie object
	@return string representation of this Movie object
	*/
	string to_string() const;
 
private:
 
	// DATA MEMBERS
	string title;
	double duration;
	double my_rating;
};
 
#endif

Movie.cpp

Usually, it is only the member functions that need implementing in the .cpp file.

#include <sstream>
// We have to include the class definition in order to know what we are implementing
#include "Movie.h" 
 
// Don't forget to specify which class we are defining implementations for with "ClassName::"
Movie::Movie()
{
	title = "";
	duration = 0.0;
	my_rating = 0.0;
}
 
Movie::Movie(string _title, double _duration, double _my_rating)
{
	title = _title;
	duration = _duration;
	my_rating = _my_rating;
}
 
string Movie::get_title() const
{
	return title;
}
 
double Movie::get_my_rating() const
{
	return my_rating;
}
 
void Movie::update_my_rating(double new_rating)
{
	my_rating = new_rating;
}
 
string Movie::to_string() const
{
	ostringstream strm;
	strm << "Title: " << title << "\n";
	strm << "Duration: " << duration << "\n";
	strm << "My rating: " << my_rating << "\n";
	return strm.str();
}
cs-142/managing-a-movie-collection.txt · Last modified: 2015/06/02 18:40 by cs142ta
Back to top
CC Attribution-Share Alike 4.0 International
chimeric.de = chi`s home Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0