In: Computer Science
C++
Consider a class Movie that information about a movie. The class has the following attributes:
• 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)
Implement the C ++ class with accessor (getter) and mutator (setter) functions for the movie name and MPAA rating. Write a function addRating that takes an integer as input parameter. The function should verify that the parameter is number between 1 and 5, and if so, increment the number of people rating 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. Write another function, getAverage, that returns the average value for all of the movie ratings. Finally, add a constructor that allows the programmer to create the object with a specific name and MPAA rating. The number of people rating the movie should be set to 0 in the constructor. Test the class by writing a main function that creates at least two movie objects, add at least five ratings for each movie, and outputs the movie name, MPAA rating, and average rating for each movie object.
Program Plan:
Consider a class Movie that contains information about a movie.
The class has the following attributes The movie name The MPAA rating (for example, G, PG, PG-13, R).
An array to store the movie ratings .The number of people that have rated this movie as a 1 (Terrible) .The number of people that have rated this movie as a 2 (Bad) .The number of people that have rated this movie as a 3 (OK) .The number of people that have rated this movie as a 4 (Good) .The number of people that have rated this movie as a 5 (Great).
Implement the class with accessor(getter) and mutator(setter) functions for the movie name and MPAA rating.
Write a function addRating that takes an integer as an input parameter.
The function should verify that the parameter is a number between 1 and 5, and if so, increment the number of people rating the movie that match the input parameter.
Write another function, getAverage, that returns the average value for all of the movie ratings. 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. Test the class by writing a main function that creates at least two movie objects, adds at least five ratings for each movie, and outputs the movie name, MPAA rating, and average rating for each movie object.
c++ program:
#include<iostream>
using namespace std;
class Movie
{
private:
string name;
int rating[5], rate;
int terrible = 0, bad = 0, ok = 0, good = 0, great = 0;
string MPAA;
double average = 0;
public:
Movie()
{ }
Movie(string Name, string mpaa)
{
name = "The Adjustment Bureau";
MPAA = "PG-13";
rate = 0;
for(int i = 0; i < 5; i++)
{
rating[i] = 0;
}
}
void setname(string nam)
{
name = nam;
}
string getname()
{
return name;
}
void setrating(string mpaa)
{
MPAA = mpaa;
}
string getrating()
{
return MPAA;
}
void addRating(int rate)
{
if(rate >= 1 && rate <= 5)
{
rating[rate - 1]++;
}
switch(rate)
{
case 1:
terrible++;
break;
case 2:
bad++;
break;
case 3:
ok++;
break;
case 4:
good++;
break;
case 5:
great++;
break;
}
}
double getAverage()
{
int total;
total = 0; rate = 0;
for(int i = 0; i < 5; i++)
{
rate = rate + rating[i];
total = total + rating[i] * (i + 1);
}
if(rate > 0)
{
return (total / rate);
}
else
return 0;
}
int display()
{
cout<<"\n Name: "<<getname();
cout<<"\n MPAA: " <<getrating();
cout<<"\n Average Rating: " <<getAverage();
cout<<"\n The people have rated tihs movie as 1 (Terrible): "
<<terrible;
cout<<"\n The people have rated tihs movie as 2 (Bad): "
<<bad;
cout<<"\n The people have rated tihs movie as 3 (Ok): "
<<ok;
cout<<"\n The people have rated tihs movie as 4 (Good): "
<<good;
cout<<"\n The people have rated tihs movie as 5 (Great): "
<<great;
return 0;
}
};
int main()
{
Movie m, m1;
string name, rating;
m.setname("Criminal");
m.setrating("PG");
m.getAverage();
m.addRating(5);
m.addRating(5);
m.addRating(4);
m.addRating(5);
m.addRating(5);
m.display();
cout<<endl;
m1.addRating(3);
m1.addRating(2);
m1.addRating(2);
m1.addRating(4);
m1.addRating(1);
m1.setname("Mr India");
m1.setrating("PG-13");
m1.getAverage();
m1.display();
return 0;
}
Output:
Name: Criminal
MPAA: PG
Average Rating: 3
The people have rated tihs movie as 1 (Terrible): 0
The people have rated tihs movie as 2 (Bad): 0
The people have rated tihs movie as 3 (Ok): 0
The people have rated tihs movie as 4 (Good): 1
The people have rated tihs movie as 5 (Great): 4
Name: Mr India
MPAA: PG-13
Average Rating: 0
The people have rated tihs movie as 1 (Terrible): 1
The people have rated tihs movie as 2 (Bad): 2
The people have rated tihs movie as 3 (Ok): 1
The people have rated tihs movie as 4 (Good): 1
The people have rated tihs movie as 5 (Great): 0