-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMovieDatabase.cpp
More file actions
169 lines (150 loc) · 4.69 KB
/
MovieDatabase.cpp
File metadata and controls
169 lines (150 loc) · 4.69 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
#include "MovieDatabase.h"
#include <string>
#include <vector>
#include <iostream>
#include <fstream>
#include "Movie.h"
using namespace std;
MovieDatabase::MovieDatabase()
{
}
bool MovieDatabase::load(const string& filename)
{
ifstream infile(filename); // infile is a name of our choosing
if (!infile) // Did opening the file fail?
{
cerr << "Error: Cannot open " << filename << endl;
return false;
}
string movieID;
string movieName;
int movieReleaseYear = 0;
vector<string> movieDirectors;
vector<string> movieActors;
vector<string> movieGenres;
float movieRating = 0.0f;
string s;
while(getline(infile, s))
{
if(s.empty()) // if the current line is empty, add the movie to the movie database
{
Movie newMovie(movieID, movieName, to_string(movieReleaseYear), movieDirectors, movieActors, movieGenres, movieRating);
// associate the movieID with the movie
m_movieID.insert(makeLower(movieID), newMovie);
// assosicate the movie director with the movies they've been in
for(int i = 0; i < movieDirectors.size(); i++)
{
m_movie_Directors.insert(makeLower(movieDirectors[i]), newMovie);
}
// associate the movie actor with the movies they've been in
for(int i = 0; i < movieActors.size(); i++)
{
m_movie_Actors.insert(makeLower(movieActors[i]), newMovie);
}
// associate the genre with the movies of that genre
for(int i = 0; i < movieGenres.size(); i++)
{
m_movie_Genres.insert(makeLower(movieGenres[i]), newMovie);
}
movieID.clear();
movieName.clear();
movieReleaseYear = 0;
movieDirectors.clear();
movieActors.clear();
movieGenres.clear();
movieRating = 0;
}
else if(movieID.empty())
{
movieID = s;
}
else if(movieName.empty())
{
movieName = s;
}
else if(movieReleaseYear == 0)
{
movieReleaseYear = stoi(s);
}
else if(movieDirectors.empty())
{
splitStringByCommas(movieDirectors, s);
}
else if(movieActors.empty())
{
splitStringByCommas(movieActors, s);
}
else if(movieGenres.empty())
{
splitStringByCommas(movieGenres, s);
}
else
{
movieRating = stof(s);
}
}
// Add the last movie to the database (if there was no empty line after it)
if(!movieID.empty())
{
Movie newMovie(movieID, movieName, to_string(movieReleaseYear), movieDirectors, movieActors, movieGenres, movieRating);
m_movieID.insert(makeLower(movieID), newMovie);
for(int i = 0; i < movieDirectors.size(); i++)
{
m_movie_Directors.insert(makeLower(movieDirectors[i]), newMovie);
}
for(int i = 0; i < movieActors.size(); i++)
{
m_movie_Actors.insert(makeLower(movieActors[i]), newMovie);
}
for(int i = 0; i < movieGenres.size(); i++)
{
m_movie_Genres.insert(makeLower(movieGenres[i]), newMovie);
}
}
return true;
}
Movie* MovieDatabase::get_movie_from_id(const string& id) const
{
TreeMultimap<string, Movie>::Iterator it = m_movieID.find(makeLower(id));
if(it.is_valid())
{
return &it.get_value();
}
else
{
return nullptr;
}
}
vector<Movie*> MovieDatabase::get_movies_with_director(const string& director) const
{
vector<Movie*> moviesWithDirectors;
TreeMultimap<string, Movie>::Iterator it = m_movie_Directors.find(makeLower(director));
while(it.is_valid())
{
moviesWithDirectors.push_back(&it.get_value());
it.advance();
}
return moviesWithDirectors;
}
vector<Movie*> MovieDatabase::get_movies_with_actor(const string& actor) const
{
vector<Movie*> movieWithActors;
TreeMultimap<string, Movie>::Iterator it = m_movie_Actors.find(makeLower(actor));
while(it.is_valid())
{
movieWithActors.push_back(&it.get_value());
it.advance();
}
return movieWithActors;
}
vector<Movie*> MovieDatabase::get_movies_with_genre(const string& genre) const
{
vector<Movie*> movieWithGenres;
TreeMultimap<string, Movie>::Iterator it = m_movie_Genres.find(makeLower(genre));
while(it.is_valid())
{
movieWithGenres.push_back(&it.get_value());
it.advance();
}
return movieWithGenres;
}