Home > Net >  How to get a list of objects with common attributes?
How to get a list of objects with common attributes?

Time:10-17

Given a class Movie (id, title, ranking, release date, character number, ticket price, comment etc.), enum type: ACTION, COMEDY, DRAMA, FANTASY etc. and a class Cinema (name, location). I need to write a function calculateProfit ( Movie*, day) that would calculate cinema's profit based on some particular day. Also I need to write a method of choosing a movie based on some parameters and sort the movies based on release date. I've been thinking over this problem for a few days already, but it seems like I just can't write the proper code.

In order to be able to choose a movie based on parameters, I need to get a list of all objects of a Movie class that have the same particular attributes. How can I do this?

Here is the brief template for my classes:

using namespace std;

class Movie{
public:
    int ID;
    string Title;
    int Ranking;
    string ReleaseDate;
    int CharacterNumber;
    int TicketPrice;
    string Comment;
    //SortingByDate
    enum type{
        ACTION, COMEDY, DRAMA, FANTASY
    } Type;
    Movie(int, string, int, string, int, int, string, type);
    Movie();
};
Movie::Movie(int ID, string Title,int Ranking,string ReleaseDate,int CharacterNumber, int TicketPrice,string Comment, type Type){
    this->ID=ID;
    this->Title=Title;
    this->Ranking=Ranking;
    this->ReleaseDate=ReleaseDate;
    this->CharacterNumber=CharacterNumber;
    this->TicketPrice=TicketPrice;
    this->Comment=Comment;
    this->Type=Type;  
class Cinema{
private:
    int calculateProfit();
public:
    //Vector with objects of Movie class
    string name;
    string location;

};

CodePudding user response:

Given a std::vector<std::shared_ptr<Movie>>, you can find by title as follows:

using MovieCollection = std::vector<std::shared_ptr<Movie>>;
MovieCollection find_by_title(const MovieCollection& collection, const std::string& fragment) {
  MovieCollection ret;
  for (auto movie: collection) {
    if (movie->title.find(fragment) != std::string::npos) {
      ret.push_back(movie);
    }
  }
  return ret;
}
  • Related