-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMovieDatabase.cpp
210 lines (185 loc) · 5.38 KB
/
MovieDatabase.cpp
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
#include "MovieDatabase.h"
#include "Movie.h"
#include <string>
#include <vector>
#include <fstream>
#include <sstream>
using namespace std;
MovieDatabase::MovieDatabase()
{
// Replace this line with correct code.
}
bool MovieDatabase::load(const string& filename)
{
ifstream infile(filename);
if (!infile) {
return false;
}
string line;
while (getline(infile, line)) {
string id = line;
for (int i = 0; i < id.size(); i++)
{
if (isalpha(id[i]))
id[i] = tolower(id[i]);
}
//cout << "the movie ID: " << id << endl;
string title;
string releaseYear;
vector<string> directors;
vector<string> actors;
vector<string> genres;
float rating = 0;
getline(infile, title);
//cout << "the movie Title: " << title << endl;
getline(infile, releaseYear);
string director;
string directorLine;
getline(infile, directorLine);
for (int i = 0; i < directorLine.size(); i++)
{
char c = directorLine[i];
if (isalpha(c))
{
c = tolower(c);
}
if (c == ',')
{
directors.push_back(director);
director.clear();
}
else
director += c;
}
if (!director.empty())// add last entry since the above code only pushes the string when it sees a comma. if the line doesnt end in a comma there is a missed entry.
directors.push_back(director);
string actor;
string actorLine;
getline(infile, actorLine);
for (int i = 0; i < actorLine.size(); i++)
{
char c = actorLine[i];
if (isalpha(c))
{
c = tolower(c);
}
if (c == ',')
{
actors.push_back(actor);
actor.clear();
}
else
actor += c;
}
if (!actor.empty())// add last entry since the above code only pushes the string when it sees a comma. if the line doesnt end in a comma there is a missed entry.
actors.push_back(actor);
/*cout << "Actors: " << endl;
for (int i = 0; i < actors.size(); i++) {
cout <<"\t" << actors[i] << endl;
}*/
string genre;
string genreLine;
getline(infile, genreLine);
for (int i = 0; i < genreLine.size(); i++)
{
char c = genreLine[i];
if (isalpha(c))
{
c = tolower(c);
}
if (c == ',')
{
genres.push_back(genre);
genre.clear();
}
else
genre += c;
}
if (!genre.empty())// add last entry since the above code only pushes the string when it sees a comma. if the line doesnt end in a comma there is a missed entry.
genres.push_back(genre);
infile >> rating;
infile.ignore(10000, '\n');
Movie movie(id, title, releaseYear, directors, actors, genres, rating);
m_movies.insert(movie.get_id(), movie); // has O(LogM) time complexity
for (int i = 0; i < directors.size(); i++) {
m_directors.insert(directors[i],movie);
}
for (int i = 0; i < actors.size(); i++) {
m_actors.insert(actors[i], movie);
}
for (int i = 0; i < genres.size(); i++) {
m_genres.insert(genres[i], movie); //these loops are for adding movies to these things
}
getline(infile, line); // gotta account for empty line
}
return true;
}
Movie* MovieDatabase::get_movie_from_id(const string& id) const
{
string copy = id;
for (int i = 0; i < copy.size(); ++i)
{
if (isalpha(copy[i]))
{
copy[i] = tolower(copy[i]);
}
}
return &(m_movies.find(copy).get_value());
}
vector<Movie*> MovieDatabase::get_movies_with_director(const string& director) const
{
string copy = director;
for (int i = 0; i < copy.size(); ++i)
{
if (isalpha(copy[i]))
{
copy[i] = tolower(copy[i]);
}
}
vector<Movie*> result;
TreeMultimap<string, Movie>::Iterator it = m_directors.find(copy);
while (it.is_valid())
{
result.push_back(&(it.get_value()));
it.advance();
}
return result;
}
vector<Movie*> MovieDatabase::get_movies_with_actor(const string& actor) const
{
string copy = actor;
for (int i = 0; i < copy.size(); ++i)
{
if (isalpha(copy[i]))
{
copy[i] = tolower(copy[i]);
}
}
vector<Movie*> result;
TreeMultimap<string, Movie>::Iterator it = m_actors.find(copy);
while (it.is_valid())
{
result.push_back(&(it.get_value()));
it.advance();
}
return result;
}
vector<Movie*> MovieDatabase::get_movies_with_genre(const string& genre) const
{
string copy = genre;
for (int i = 0; i < copy.size(); ++i)
{
if (isalpha(copy[i]))
{
copy[i]=tolower(copy[i]);
}
}
vector<Movie*> result;
TreeMultimap<string, Movie>::Iterator it = m_genres.find(copy);
while (it.is_valid())
{
result.push_back(&(it.get_value()));
it.advance();
}
return result;
}