1. Engineering
  2. Computer Science
  3. background movie review sites collect reviews and will often provide...

Question: background movie review sites collect reviews and will often provide...

Question details

BACKGROUND

Movie Review sites collect reviews and will often provide some sort of average review to sort movies by their quality. In this assignment, you will collect a list of movies and then a list of reviews for each movie. You will then take a simple average (total of reviews divided by number of reviews) for each movie and output a sorted list of movies with their average review. Here you are provided the shell of a program and asked to finish the implementation. All the functions you need are already defined, you simply must implement them. Do not remove or rename existing functions, nor add any additional functions!


REQUIREMENTS

1. There will be 2 input files and 1 output file for the program.

2. The first input file will be in the following format: [movie id] [movie name] The value for the movie id will be in sequential order starting at 1. The value for the movie name will start after whitespace and continue to the rest of the line. Note that the movie name may have spaces and punctuation as part of the name.

3. The second input file will be in the following format: [movie id] [rating 1] [rating 2] … [rating n] Where the movie id will correspond to the id from the first input file. There may be 1 or more integer ratings for each movie and the number of ratings may not be the same for all movies (i.e. one movie may have 5 reviews, and another 25).

4. The output file will be in the following format [movie id] [average rating] [movie name] Where the movies are sorted in descending order by average rating (represented by a floatingpoint value truncated to 2 decimal places).

5. The program will not contain any memory leaks.

6. The program will not use any custom loops. For this assignment, you are to use std:: functions only (std::for_each, std::transform, std::copy, etc., from the <algorithm> library, as well as any member functions provided by the std::vector and std::list containers).

7. The only changes will be to the movieratings.h file. 8. The only changes will be where the TODO: // Implement lines appear

MAIN.cpp

#include <iostream>
#include <iterator>
#include <fstream>
#include <sstream>
#include <string>

#include "movieratings.h"


/// The main entry point for the application
/// argc - the number of command line parameters. This will always be at least 1
/// argv - the command line parameters. The first parameter is always the name of the application
int main(int argc, char** argv)
{
   // To run the program, it requires 4 command line parameters:
   // argv[0] - the application name
   // argv[1] - the input file for the movie list
   // argv[2] - the input file for the movie ratings
   // argv[3] - the output file for the results
   if (argc != 4)
   {
       std::cout << "Usage: assignment01 [movie input file] [ratings input file] [output file]" << std::endl;
       return 0;
   }

   // Create the input streams
   std::ifstream finMovies(argv[1]);
   std::ifstream finRatings(argv[2]);

   // Create the output stream
   std::ofstream fout(argv[3]);

   // create the begin and end iterators for the input files
   std::istream_iterator<Movie> beginMovies(finMovies);
   std::istream_iterator<Movie> endMovies;
   std::istream_iterator<Rating> beginRatings(finRatings);
   std::istream_iterator<Rating> endRatings;

   // create the Movie Ratings object
   // note that it takes the iterators previously created
   MovieRatings ratings(beginMovies, endMovies, beginRatings, endRatings);
   ratings.GenerateResults(fout);

   return 0;
}

MOVIE.h

#ifndef __MOVIE_H__

#define __MOVIE_H__

#include <iostream>
#include <string>

class Movie
{
private:
   int m_ID;
   std::string m_Name;
public:
   Movie() : m_ID(-1)
   {

   }

   int GetID() const
   {
       return m_ID;
   }

   void SetID(int id)
   {
       m_ID = id;
   }

   std::string GetName() const
   {
       return m_Name;
   }
  
   void SetName(const std::string& name)
   {
       m_Name = name;
   }

   friend std::istream& operator>>(std::istream& is, Movie& g);
   friend std::ostream& operator<<(std::ostream& os, const Movie& g);
};

std::ostream& operator<<(std::ostream& os, const Movie& g)
{
   os << g.m_ID << " " << g.m_Name << std::endl;
   return os;
}

#endif // __MOVIE_H__

RATING.h

#ifndef __RATING_H__
#define __RATING_H__

#include <algorithm>
#include <iostream>
#include <iterator>
#include <list>
#include <numeric>

class Rating
{
private:
   int m_ID;
   std::list<int> m_Ratings;
public:
   Rating()
   {
   }

   int GetID() const
   {
       return m_ID;
   }

   void SetID(int id)
   {
       m_ID = id;
   }

   std::list<int> GetRatings() const
   {
       return m_Ratings;
   }

   void SetRatings(const std::list<int>& ratings)
   {
       m_Ratings = ratings;
   }

   float GetAverageRating() const
   {
       int totalRatings = std::accumulate(m_Ratings.begin(), m_Ratings.end(), 0);
       int count = m_Ratings.size();
       float result = static_cast<float>(totalRatings) / static_cast<float>(count);
       return result;
   }

   bool operator> (const Rating& rhs) const
   {
       float l = GetAverageRating();
       float r = rhs.GetAverageRating();
       return l > r;
   }

   friend std::istream& operator>>(std::istream& is, Rating& r);
   friend std::ostream& operator<<(std::ostream& os, const Rating& r);
};

std::ostream& operator<<(std::ostream& os, const Rating& r)
{
   os << r.m_ID << " ";
   std::copy(r.m_Ratings.begin(), r.m_Ratings.end(), std::ostream_iterator<int>(os, " "));
   os << std::endl;
   return os;
}

#endif // __RATING_H__

MOVIERATINGS.h (Only file to edit)

#ifndef __MOVIE_RATINGS_H__
#define __MOVIE_RATINGS_H__

#include <algorithm>
#include <iomanip>
#include <iterator>
#include <list>
#include <vector>

#include "movie.h"
#include "rating.h"

class MovieRatings
{
private:
   std::vector<Movie> m_Movies;
   std::vector<Rating> m_Ratings;

public:
   template<class MovieInputIterator, class RatingInputIterator>
   MovieRatings(MovieInputIterator movieBegin, MovieInputIterator movieEnd, RatingInputIterator ratingBegin, RatingInputIterator ratingEnd)
   {
       // TODO: Implement the constructor to load the m_Movies and m_Ratings containers
   }

   void GenerateResults(std::ostream& os) const
   {
       // TODO: Implement the GenerateResults function to output the movie id, average rating, and movie name in descending order by average rating
       // NOTE: do not modify the m_Movies or m_Ratings objects in this function!
   }
};

std::istream& operator>>(std::istream& is, Movie& g)
{
   // TODO: Read in the movie id and movie name for a single movie
}
  
std::istream& operator>>(std::istream& is, Rating& r)
{
   // TODO: Read in the movie id and list of movie ratings for a single movie
}

Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution