-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProfile.cpp
313 lines (283 loc) · 9.26 KB
/
Profile.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
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
#include "stdafx.h"
#include "Profile.h"
Profile::Profile()
{
}
Profile::Profile(const Profile & profile)
{
cout << "in copy!!\n";
SinglyLinkedList<Category *> catgs = profile.categories;
this->addCategories(&catgs);
}
Profile::Profile(string label, string firstName, string lastName, int age)
{
this->label = label;
this->firstName = firstName;
this->lastName = lastName;
this->age = age;
}
Profile::~Profile()
{
this->categories.clear();
}
void Profile::edit(string lbl, string fname, string lname, int ag)
{
label = lbl;
firstName = fname;
lastName = lname;
age = ag;
}
void Profile::operator<<(string category)
{
cout << "The movies in your Queue with Category " << category << " are displayed below.";
// fetch all movies
SinglyLinkedList<Movie> allMovies = SinglyLinkedList<Movie>();
for (int i = 0; i < this->categories.getSize(); i++) {
for (int j = 0; j < this->categories.get(i)->movies.getSize(); j++) {
if (this->categories.get(i)->movies.get(j).getCategory() != category)
continue;
Movie tempMovie = this->categories.get(i)->movies.get(j);
allMovies.add(Movie(tempMovie.getName(), tempMovie.getYear(), tempMovie.getCategory(), tempMovie.getRating(), tempMovie.getRanking()));
}
}
// display all movies
for (int i = 0; i < allMovies.getSize(); i++) {
Movie movie = allMovies[i];
cout << "\n----------------------------\n";
cout << movie.getName() << endl;
cout << "Year : " << movie.getYear() << endl;
cout << "Category : " << movie.getCategory() << endl;
cout << "Rated : " << movie.getRating() << endl;
cout << "Ranked : ";
for (int k = 0; k < movie.getRanking(); k++) {
cout << "*";
}cout << endl;
}
cout << "----------------------------\n";
}
string Profile::getLabel()
{
return this->label;
}
string Profile::getFirstName()
{
return this->firstName;
}
string Profile::getLastName()
{
return this->lastName;
}
int Profile::getAge()
{
return this->age;
}
SinglyLinkedList<Category*> Profile::getCategories()
{
return this->categories;
}
void Profile::addCategories(SinglyLinkedList<Category*> * categories)
{
int categoriesCount = categories->getSize();
for (int i = 0; i < categoriesCount; i++) {
int moviesCount = categories->get(i)->getNumberOfMovies();
for (int j = 0; j < moviesCount; j++) {
this->addMovieToQueue(&(categories->get(i)->movies.get(j)));
}
}
}
void Profile::displayMovieQueue(int option)
{
if (option == 1) {
// fetch all movies
SinglyLinkedList<Movie> allMovies = SinglyLinkedList<Movie>();
for (int i = 0; i < this->categories.getSize(); i++) {
for (int j = 0; j < this->categories.get(i)->movies.getSize(); j++) {
Movie tempMovie = this->categories.get(i)->movies.get(j);
allMovies.add(Movie(tempMovie.getName(), tempMovie.getYear(), tempMovie.getCategory(), tempMovie.getRating(), tempMovie.getRanking()));
}
}
// sort all movies by ranking from highest to lowest (insertion sort)
for (int i = 1; i < allMovies.getSize(); i++) {
Movie movie = allMovies[i];
int j = i - 1;
while (j >= 0 && allMovies[j].getRanking() < movie.getRanking()) {
Movie tempMovie = allMovies[j];
allMovies.remove(j + 1);
allMovies.insert(tempMovie, j + 1);
j--;
}
allMovies.remove(j + 1);
allMovies.insert(movie, j + 1);
}
// display all movies
for (int i = 0; i < allMovies.getSize(); i++) {
Movie movie = allMovies[i];
cout << "\n----------------------------\n";
cout << movie.getName() << endl;
cout << "Year : " << movie.getYear() << endl;
cout << "Category : " << movie.getCategory() << endl;
cout << "Rated : " << movie.getRating() << endl;
cout << "Ranked : ";
for (int k = 0; k < movie.getRanking(); k++) {
cout << "*";
}cout << endl;
}
cout << "----------------------------\n";
}
else if (option == 2) {
// fetch all movies
SinglyLinkedList<Movie> allMovies = SinglyLinkedList<Movie>();
for (int i = 0; i < this->categories.getSize(); i++) {
for (int j = 0; j < this->categories.get(i)->movies.getSize(); j++) {
Movie tempMovie = this->categories.get(i)->movies.get(j);
allMovies.add(Movie(tempMovie.getName(), tempMovie.getYear(), tempMovie.getCategory(), tempMovie.getRating(), tempMovie.getRanking()));
}
}
// sort all movies by ranking from lowest to highest (insertion sort)
for (int i = 1; i < allMovies.getSize(); i++) {
Movie movie = allMovies[i];
int j = i - 1;
while (j >= 0 && allMovies[j].getRanking() > movie.getRanking()) {
Movie tempMovie = allMovies[j];
allMovies.remove(j + 1);
allMovies.insert(tempMovie, j + 1);
j--;
}
allMovies.remove(j + 1);
allMovies.insert(movie, j + 1);
}
// display all movies
for (int i = 0; i < allMovies.getSize(); i++) {
Movie movie = allMovies[i];
cout << "\n----------------------------\n";
cout << movie.getName() << endl;
cout << "Year : " << movie.getYear() << endl;
cout << "Category : " << movie.getCategory() << endl;
cout << "Rated : " << movie.getRating() << endl;
cout << "Ranked : ";
for (int k = 0; k < movie.getRanking(); k++) {
cout << "*";
}cout << endl;
}
cout << "----------------------------\n";
}
else if (option == 5) {
// fetch all movies
SinglyLinkedList<Movie> allMovies = SinglyLinkedList<Movie>();
for (int i = 0; i < this->categories.getSize(); i++) {
for (int j = 0; j < this->categories.get(i)->movies.getSize(); j++) {
Movie tempMovie = this->categories.get(i)->movies.get(j);
allMovies.add(Movie(tempMovie.getName(), tempMovie.getYear(), tempMovie.getCategory(), tempMovie.getRating(), tempMovie.getRanking()));
}
}
// sort all movies in alphabetical order (insertion sort)
for (int i = 1; i < allMovies.getSize(); i++) {
Movie movie = allMovies[i];
int j = i - 1;
while (j >= 0 && allMovies[j].getName() > movie.getName()) {
Movie tempMovie = allMovies[j];
allMovies.remove(j + 1);
allMovies.insert(tempMovie, j + 1);
j--;
}
allMovies.remove(j + 1);
allMovies.insert(movie, j + 1);
}
// display all movies
for (int i = 0; i < allMovies.getSize(); i++) {
Movie movie = allMovies[i];
cout << "\n----------------------------\n";
cout << movie.getName() << endl;
cout << "Year : " << movie.getYear() << endl;
cout << "Category : " << movie.getCategory() << endl;
cout << "Rated : " << movie.getRating() << endl;
cout << "Ranked : ";
for (int k = 0; k < movie.getRanking(); k++) {
cout << "*";
}cout << endl;
}
cout << "----------------------------\n";
}
}
void Profile::displayMovieQueue(int option, string value)
{
if(option == 3)
cout << "The movies in your Queue with Rating "<< value <<" are displayed below.";
if(option == 4)
cout << "The movies in your Queue with Category " << value << " are displayed below.";
// fetch all movies
SinglyLinkedList<Movie> allMovies = SinglyLinkedList<Movie>();
for (int i = 0; i < this->categories.getSize(); i++) {
for (int j = 0; j < this->categories.get(i)->movies.getSize(); j++) {
if (option == 3 && this->categories.get(i)->movies.get(j).getRating() != value)
continue;
if (option == 4 && this->categories.get(i)->movies.get(j).getCategory() != value)
continue;
Movie tempMovie = this->categories.get(i)->movies.get(j);
allMovies.add(Movie(tempMovie.getName(), tempMovie.getYear(), tempMovie.getCategory(), tempMovie.getRating(), tempMovie.getRanking()));
}
}
// display all movies
for (int i = 0; i < allMovies.getSize(); i++) {
Movie movie = allMovies[i];
cout << "\n----------------------------\n";
cout << movie.getName() << endl;
cout << "Year : " << movie.getYear() << endl;
cout << "Category : " << movie.getCategory() << endl;
cout << "Rated : " << movie.getRating() << endl;
cout << "Ranked : ";
for (int k = 0; k < movie.getRanking(); k++) {
cout << "*";
}cout << endl;
}
cout << "----------------------------\n";
}
void Profile::addMovieToQueue(Movie * movie)
{
string category = movie->getCategory();
int categoryIndex = this->findCategory(category);
if (categoryIndex > -1) {
++*this->categories.get(categoryIndex);
this->categories.get(categoryIndex)->movies.add(*movie);
}
else {
Category * newCategory = new Category(category);
++*newCategory;
this->categories.add(newCategory);
this->categories.get(this->categories.getSize() - 1)->movies.add(*movie);
}
}
void Profile::editMovieInQueue(pair<int, int> pos, Movie * movie)
{
this->removeMovieFromQueue(pos); // remove the old one
this->addMovieToQueue(movie); // put the new one
}
void Profile::removeMovieFromQueue(pair<int, int> pos)
{
this->categories.get(pos.first)->movies.remove(pos.second);
(*this->categories.get(pos.first))--;
if (this->categories.get(pos.first)->movies.getSize() == 0) {
this->categories.remove(pos.first);
}
}
pair<int, int> Profile::searchForMovieInQueue(string name, int year)
{
for (int i = 0; i < this->categories.getSize(); i++) {
for (int j = 0; j < this->categories.get(i)->getNumberOfMovies(); j++) {
if (this->categories.get(i)->movies.get(j).getName() == name
&& this->categories.get(i)->movies.get(j).getYear() == year) {
return pair<int, int>(i,j);
}
}
}
return pair<int, int>(-1,-1);
}
int Profile::findCategory(string categoryName)
{
int catesCount = this->categories.getSize();
for (int i = 0; i < catesCount; i++) {
if (categoryName == this->categories.get(i)->getName())
return i;
}
return -1;
}