-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMovieCollection.cpp
More file actions
316 lines (284 loc) · 9.92 KB
/
MovieCollection.cpp
File metadata and controls
316 lines (284 loc) · 9.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#include <string>
#include <vector>
#include <map>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <string>
#include <algorithm>
#include "movieTrie.cpp"
#include <set>
#include <stack>
#include "trie.cpp"
using namespace std;
// Container for all movies
class MovieCollection
{
private:
// data
OptimizedMovieTrie movies;
map<string, list<Movie*>> movieDirectors; // stores the references to all directors of the movies
map<int, list<Movie*>> moviesYearIndex; // stores the references to all movies year wise
map<float, list<Movie*>> moviesRatingIndex; // stores the references to all movies rating wise
map<string, list<Movie*>> moviesByGenre; // stores the references to all movies genre wise
map<string, list<Movie*>> moviesByActors; // stores the references to all movies actor wise
public:
//Contructor
MovieCollection()
{
importData("Data//newdataset.csv");
}
void importData(string path)
{
ifstream myFile;
myFile.open(path);
std::ostringstream out;
out << myFile.rdbuf();
string filestring = out.str();
// string stringinput = "Hello,,kesy,ho,main,to,theek,hoon,hehhe";
if(myFile.good())
{
string line;
// split the entire dataset into records
vector<string> data = split(filestring, '\n');
// for every row
for (auto row: data)
{
// split the record into attributes
vector<string> record = split(row, ',');
if (record.size() == 28)
{
if (record.at(0).compare("\"movie_title\"") == 0)
{
continue;
}
// create a movie node with the values
Movie* movieptr = new Movie(record.at(0), record.at(1), record.at(2), record.at(3), record.at(4), record.at(5),
record.at(6), record.at(7), record.at(8), record.at(9), record.at(10), record.at(11),
record.at(12), record.at(13), record.at(14), record.at(15), record.at(16), record.at(17),
record.at(18), record.at(19), record.at(20), record.at(21), record.at(22), record.at(23),
record.at(24), record.at(25), record.at(26), record.at(27));
// maintaining the trie of movies
movies.insert(removeQuotes(record.at(0)), *movieptr);
// pushing the list of actors to every actor's profile
moviesByActors[movieptr->actors[0]].push_back(movieptr);
moviesByActors[movieptr->actors[1]].push_back(movieptr);
moviesByActors[movieptr->actors[2]].push_back(movieptr);
// maintaing the trie of directors
movieDirectors[removeQuotes(movieptr->director_name)].push_back(movieptr);
// add the movie to its linked lists and hash maps
// maintaining year index list
moviesYearIndex[movieptr->getYear()].push_back(movieptr);
// maintaing year index list for ratings
moviesRatingIndex[movieptr->getRating()].push_back(movieptr);
// maintaining genre indexes
for (auto genre: movieptr->getGenre())
{
moviesByGenre[removeQuotes(genre)].push_back(movieptr);
}
}
}
}
}
// searches a movie in the loaded data and prints the information.
void SearchMovie(string movieName)
{
movies.printChildren(movieName);
}
// prints the movies released in a year
void moviesReleasedInYear(int releaseYear)
{
for (auto movie: moviesYearIndex[releaseYear])
{
movie->print();
}
}
// prints all the stored movies sorted by year in ascending order
void moviesListByYearAscending()
{
for (auto key_value: moviesYearIndex)
{
moviesReleasedInYear(key_value.first);
}
}
// prints all the stored movies sorted by year in descending order
void moviesListByYearDescending()
{
stack<int> stackYear;
for (auto key_value:moviesYearIndex)
{
stackYear.push(key_value.first);
}
while (!stackYear.empty())
{
moviesReleasedInYear(stackYear.top());
stackYear.pop();
}
}
// prints the movies released in a year
void moviesWithRating(float rating)
{
for (auto movie: moviesRatingIndex[rating])
{
movie->print();
}
}
// prints all the stored movies sorted by rating
void moviesListByRatingDescending()
{
stack<float> stackRating;
for (auto key_value:moviesRatingIndex)
{
stackRating.push(key_value.first);
}
while (!stackRating.empty())
{
moviesWithRating(stackRating.top());
stackRating.pop();
}
}
// lists all movies in the given genre
void moviesListByGenre(string genre)
{
for (auto movie : moviesByGenre[genre])
{
movie->print();
}
}
// prints the names of all movies directed by director
void moviesByDirector(string director)
{
for (auto movie: movieDirectors[director])
{
cout<<"\t"<<movie->movie_title<<endl;
}
}
// prints all directors who have directed movies of a genre
void getDirectorsOfGenre(string action)
{
set<string> directors;
for (auto movie: moviesByGenre[action])
{
directors.insert(movie->director_name);
}
for (auto director: directors)
{
cout<<director<<endl;
}
}
// prints the profile of an actor
void getActorProfile(string actor)
{
// display the actors info if he exists in the list
if (moviesByActors.find(actor) == moviesByActors.end())
{
cout<<"Actor does not exists"<<endl;
}
else
{
cout<<"\n"<<actor<<" acted in acted in "<<moviesByActors[actor].size()<<" movies"<<endl;
cout<<"Title of Movies: "<<endl;
for (auto movie: moviesByActors[actor])
{
cout<<"\t"<<movie->movie_title<<movie->title_year<<"\n";
}
}
}
// searches and prints the co actors of the actor with movie titles
void printCoActorsOfActor(string actor)
{
// display the actors info if he exists in the list
if (moviesByActors.find(actor) == moviesByActors.end())
{
cout<<"Actor does not exists"<<endl;
}
else
{
cout<<"\n"<<actor<<": "<<endl;
for (auto movie: moviesByActors[actor])
{
cout<<"\t"<<movie->movie_title<<movie->title_year<<"\n";
cout<<"\t\tCo-Actors:"<<endl<<"\t";
for (auto i : movie->actors)
{
if (i.compare(actor) != 0) // if the actor is not equal to 0 then print it
cout<<"\t"<<i;
}
cout<<endl;
}
}
}
void printUniqueCoActors(string actor)
{
set<string> co_actors;
// display the actors info if he exists in the list
if (moviesByActors.find(actor) == moviesByActors.end())
{
cout<<"Actor does not exists"<<endl;
}
else
{
cout<<"The unique co actors of "<<actor<<" are:"<<endl;
// save all actors name in a set
for (auto movie :moviesByActors[actor])
{
for (auto movieActor: movie->actors)
{
if (movieActor.compare(actor) != 0) // if the actor is not equal to movieactor then print it
co_actors.insert(movieActor);
}
}
// print all names in the set
for (auto coactor: co_actors)
{
cout<<"\t"<<coactor<<endl;
}
}
}
// prints all the co actors of co actors of the actor
void printCoActorsofCoActorsofActor(string actor)
{
// display the actors info if he exists in the list
if (moviesByActors.find(actor) == moviesByActors.end())
{
cout<<"Actor does not exists"<<endl;
}
else
{
cout<<"The unique co actors of co actors of "<<actor<<" are:"<<endl;
// save all actors name in a set
for (auto movie :moviesByActors[actor])
{
for (auto movieActor: movie->actors)
{
// if the actor is not equal to movieactor then print it
if (movieActor.compare(actor) != 0)
printUniqueCoActors(movieActor);
}
}
}
}
// returns true if the actors are co actors
bool checkCoActors(string actorA, string actorB)
{
// display the actors info if he exists in the list
if (moviesByActors.find(actorA) == moviesByActors.end())
{
cout<<"Actor does not exists"<<endl;
}
else
{
// save all actors name in a set
for (auto movie :moviesByActors[actorA])
{
for (auto movieActor: movie->actors)
{
// if the actor is not equal to movieactor then print it
if (movieActor.compare(actorB) != 0)
return true;
}
}
}
return false;
}
};