In: Computer Science
Working with arrays in our class, reinforce constructors and setters and getters and implement dynamic variables.
Create a class called Movie that contains information about a movie.
The class has the following attributes (you choose the data type for the member variables):
■ The movie name
■ The MPAA rating (for example, G, PG, PG-13, R)
■ Array of size 5 called Ratings, each index will hold the following.
[0] The number of people that have rated this movie as a 1 (Terrible)
[1] The number of people that have rated this movie as a 2 (Bad)
[2] The number of people that have rated this movie as a 3 (OK)
[3]The number of people that have rated this movie as a 4 (Good)
[4]The number of people that have rated this movie as a 5 (Great)
Create setters and getters for the member variables:
- name
- MPAA Rating
Write a function addRating that takes an integer
as an input parameter (takes a number as an argument). The function
should verify that the parameter (argument) is a number between 1
and 5. The method (member function) will increment the number of
people rating (in the array) the movie that match the input
parameter.
For example, if 3 is the input parameter, then the number of
people that rated the movie as a 3 should be incremented by 1 in
index 2.
*Each index in the array represent a movie rating, Index 0 represents Terrible (1 star rating). Index 0 in the array holds the number of people who have given the movie a 1 star rating.
Write another function, getAverage , that
returns the average value of movie ratings for a particular
movie.
Finally, add a constructor that allows the programmer to create the object with a specified name and MPAA rating. The number of people rating the movie should be set to 0 in the constructor.
In main:
Test the class by creating at least two objects that represent a movie, adds at least five ratings for each movie, and output the movie name, MPAA rating, and average rating for each movie object. Both of these objects should be created as dynamic variables. Don't forget to use the delete operator to "clean up" memory.
Please find the requested program below. Also including the screenshot of sample output and screenshot of code to understand the indentation.
Please provide your feedback
Thanks and Happy learning!
#include <iostream>
#include <string>
#include <iomanip>
class Movie
{
private:
//The movie name
std::string m_strMovieName;
//The MPAA rating(for example, G, PG, PG-13, R)
std::string m_strMPAARating;
//Array of size 5 called Ratings, each index will hold the following.
//[0] The number of people that have rated this movie as a 1 (Terrible)
//[1] The number of people that have rated this movie as a 2 (Bad)
//[2] The number of people that have rated this movie as a 3 (OK)
//[3]The number of people that have rated this movie as a 4 (Good)
//[4]The number of people that have rated this movie as a 5 (Great)
int m_Ratings[5];
public:
//add a constructor that allows the programmer to create the object with a specified name and MPAA rating.
//The number of people rating the movie should be set to 0 in the constructor.
Movie(std::string movieName, std::string strMPAARating) :m_strMovieName(movieName), m_strMPAARating(strMPAARating), m_Ratings()
{}
//Create setters and getters for the member variables :
//-name
//-MPAA Rating
void setMovieName(const std::string& strMovieName)
{
m_strMovieName = strMovieName;
}
std::string getMovieName() const
{
return m_strMovieName;
}
void setMPAARating(const std::string& strMPAARating)
{
m_strMPAARating = strMPAARating;
}
std::string getMPAARating() const
{
return m_strMPAARating;
}
void addRating(int rating)
{
// verify that the rating is a number between 1 and 5.
if (rating < 1 || rating > 5)
{
std::cout << "WARNING: Rating should be between 1 and 5." << std::endl;
}
else
{
++m_Ratings[rating - 1];
}
return;
}
double getAverage()
{
double totalRating = 0.0;
double totalPeople = 0.0;
double avgRating = 0.0;
for (int i = 0; i < 5; i++)
{
totalRating += m_Ratings[i] * (i + 1);
totalPeople += m_Ratings[i];
}
avgRating = totalRating / totalPeople;
return avgRating;
}
};
int main()
{
//Create at least two objects( as dynamic variables) that represent a movie
Movie *m1 = new Movie("Test Movie Name 1", "R");
Movie *m2 = new Movie("Test Movie Name 2", "PG");
//Add at least five ratings for each movie
m1->addRating(1);
m1->addRating(2);
m1->addRating(3);
m1->addRating(4);
m1->addRating(4);
m1->addRating(4);
m1->addRating(5);
m2->addRating(1);
m2->addRating(2);
m2->addRating(3);
m2->addRating(3);
m2->addRating(4);
m2->addRating(4);
m2->addRating(5);
m2->addRating(5);
//output the movie name, MPAA rating, and average rating for each movie object.
std::cout << "Movie name: " << m1->getMovieName() << ", MPAA rating: " << m1->getMPAARating() << ", Average rating: " << std::fixed << std::setprecision(2) << m1->getAverage() << std::endl;
std::cout << "Movie name: " << m2->getMovieName() << ", MPAA rating: " << m2->getMPAARating() << ", Average rating: " << std::fixed << std::setprecision(2) << m2->getAverage() << std::endl;
//Use the delete operator to "clean up" memory.
delete m1;
delete m2;
return 0;
}