-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmovie.cpp
More file actions
174 lines (136 loc) · 3.1 KB
/
movie.cpp
File metadata and controls
174 lines (136 loc) · 3.1 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
/*movie.cpp*/
/**
* @brief Implementation of Movie class.
*
* Implementation of Movie member functions for class Movie, which
* models one Movie object.
*
* Initial template:
* Prof. Hummel
* Northwestern University
*
* @note Modified by ...
* @note Northwestern University
* @note Database access using "SqliteModernCPP": https://github.com/SqliteModernCpp/sqlite_modern_cpp
*/
#include <iostream>
#include <string>
#include <vector>
#include "movie.h"
#include "sqlite_modern_cpp.h"
using namespace std;
using namespace sqlite;
/**
* @brief constructor to initialize Movie object from database.
*
* @parame DB SQLite database object representing movie database.
* @param Movie_ID ID (integer) of movie in database.
* @return nothing.
*/
Movie::Movie(database& db, int movie_id)
: Movie_ID(movie_id)
{
//
// First, let's retrieve the title and revenue from the database:
//
string sql = "SELECT Title, Revenue FROM Movies WHERE Movie_ID = ?;";
auto results = db << sql << movie_id;
for (auto row : results) {
row >> this->Title >> this->Revenue;
break; // there's only one row in the result:
}
//
// Next, let's retreive any ratings for this movie and
// store in the vector:
//
sql = "SELECT Rating FROM Ratings WHERE Movie_ID = ?;";
auto results2 = db << sql << movie_id;
for (auto row : results2) {
int rating;
row >> rating;
this->Ratings.push_back(rating);
}
//
// Finally, let's retrieve the genre(s) that this movie
// falls into, and store in vector:
//
sql = R""""(
SELECT Genre_Name
FROM Genres
JOIN Movie_Genres ON Genres.Genre_ID = Movie_Genres.Genre_ID
WHERE Movie_ID = ?
ORDER BY Genre_Name;
)"""";
auto results3 = db << sql << movie_id;
for (auto row : results3) {
string genre_name;
row >> genre_name;
this->Genres.push_back(genre_name);
}
return;
}
//
// getters:
//
int Movie::getMovieID()
{
return (this->Movie_ID);
}
string Movie::getTitle()
{
return (this->Title);
}
double Movie::getRevenue()
{
return (this->Revenue);
}
//
// getNumRatings:
//
int Movie::getNumRatings()
{
return (this->Ratings.size());
}
//
// getAverageRating:
//
double Movie::getAverageRating()
{
//
// TODO
if (this->Ratings.size() == 0) {
return 0.0;
}
double sum = 0.0;
for (const auto& rating : this->Ratings) {
sum += rating;
}
return sum / this->Ratings.size();
}
//
// print:
//
void Movie::print()
{
cout << this->Movie_ID << ": " << "'" << this->Title << "'" << endl;
cout << " Rating info: " << "# of ratings=" << this->getNumRatings()
<< ", avg rating=" << this->getAverageRating() << endl;
cout << " Genres: ";
//
// TODO
//
// print out the genres vector with ", " between each genre.
// Example: Genres: Action, Science Fiction, Thriller
//
for (size_t i = 0; i < this->Genres.size(); i++) {
cout << this->Genres[i];
if (i < this->Genres.size() - 1) {
cout << ", ";
}
}
cout << endl;
// HINT: use an index-based for loop, not foreach, and use
// index to decide if you should output ", " or not.
//
return;
}