-
Notifications
You must be signed in to change notification settings - Fork 0
/
Library.cpp
339 lines (256 loc) · 7.74 KB
/
Library.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#include "Library.h"
Library::Library()
{
}
Library::Library(int numBooks, int numUsers) {
this->numBooks = numBooks;
this->numUsers = numUsers;
}
int Library::getNumBooks() {
return this->books.size();
}
int Library::getNumUsers() {
return this->users.size();
}
int Library::readBooks(string filename) {
ifstream fileStream;
fileStream.open(filename);
// Verify the file exists:
if (fileStream.fail()) return -1;
string tempLine;
while (getline(fileStream, tempLine)) {
// Filter empty lines:
if (tempLine == "") continue;
auto splitData = Utilities::split(tempLine, ',');
// Verify that the split data is valid:
if (splitData.size() < 2) continue;
// Verify the vector has not surpassed 50 books (although this can easily be surpassed):
if (this->books.size() >= 50) break;
Book book;
book.setAuthor(splitData[0]);
book.setTitle(splitData[1]);
this->books.push_back(book);
this->numBooks++;
}
// Close the file stream:
fileStream.close();
return this->books.size();
}
int Library::readRatings(string filename) {
ifstream fileStream;
fileStream.open(filename);
// Verify the file exists:
if (fileStream.fail()) return -1;
int linePos = 0;
string tempLine;
User currentUser;
while (getline(fileStream, tempLine)) {
// Filter empty lines:
if (tempLine == "") continue;
if (this->users.size() >= 100) break;
// If the line is even, we're looking at numbers otherwise its likely a name:
if (linePos % 2 != 0) {
auto splitRatings = Utilities::splitIntegers(tempLine, ',');
// Sort the ratings:
for (int i = 0; i < splitRatings.size(); i++)
currentUser.setRatingAt(i, splitRatings[i]);
this->users.push_back(currentUser);
currentUser = User();
} else {
currentUser.setUsername(Utilities::toLower(tempLine));
}
this->numUsers++;
linePos++;
}
// Close the file stream:
fileStream.close();
return this->users.size();
}
void Library::viewRatings(string username, int minRating) {
if (this->users.size() == 0 || this->books.size() == 0) {
printf("The library has not been fully initialized.\n");
return;
}
// Determine if the user exists:
User requestedUser;
bool wasUserFound = false;
for (User user : this->users) {
if (user.getUsername() == Utilities::toLower(username)) {
requestedUser = user;
wasUserFound = true;
}
}
if (!wasUserFound) {
printf("%s does not exist.\n", username.data());
return;
}
// Find all valid ratings:
int validRatingsFound = 0;
for (int i = 0; i < this->books.size(); i++) {
if (requestedUser.getRatingAt(i) >= minRating) {
validRatingsFound++;
}
}
if (validRatingsFound == 0) {
printf("%s has not rated any books with %i or higher.\n", username.data(), minRating);
return;
}
// Print all rated books above minRating:
printf("Here are the books that %s rated", username.data());
for (int i = 0; i < this->books.size(); i++) {
int rating = requestedUser.getRatingAt(i);
if (rating >= minRating) {
printf("\nTitle : %s \nRating : %i\n-----", this->books[i].getTitle().data(), rating);
}
}
printf("\n");
}
void Library::printAllBooks() {
if (this->users.size() == 0 || this->books.size() == 0) {
printf("The library has not been fully initialized.\n");
return;
}
for (int i = 0; i < this->books.size(); i++) {
Book book = this->books[i];
// Calculate average rating for this book:
float total = 0.0;
int usersRated = 0;
for (User user : this->users) {
float rating = (float)user.getRatingAt(i);
if (rating != 0) {
usersRated++;
total += rating;
}
}
printf("(%.2f) %s by %s\n", total / (float)usersRated, book.getTitle().data(), book.getAuthor().data());
}
}
void Library::addUser(string username) {
if (this->users.size() >= 100) {
printf("The library is already full. %s was not added.\n", username.data());
return;
}
// Check if the user already exists:
for (auto user : this->users) {
if (user.getUsername() == Utilities::toLower(username)) {
printf("%s already exists in the library.\n", username.data());
return;
}
}
// User doesn't exist and list isn't full, add them:
printf("Welcome to the library %s\n", username.data());
User user;
user.setUsername(Utilities::toLower(username));
this->users.push_back(user);
}
void Library::updateRating(string username, string title, int newRating) {
if (this->users.size() == 0 || this->books.size() == 0) {
printf("The library has not been fully initialized.\n");
return;
}
// Find the requested user:
int userIndex = -1;
for (int i = 0; i < this->users.size(); i++) {
if (Utilities::toLower(this->users[i].getUsername()) == Utilities::toLower(username)) {
userIndex = i;
break;
}
}
if (userIndex == -1) {
printf("%s does not exist.\n", username.data());
return;
}
// Verify the rating value is valid:
if (newRating < 0 || newRating > 5) {
printf("%i is not valid.\n", newRating);
return;
}
// Find the requested book:
int bookIndex = -1;
for (int i = 0; i < this->books.size(); i++) {
if (Utilities::toLower(this->books[i].getTitle()) == Utilities::toLower(title)) {
bookIndex = i;
break;
}
}
if (bookIndex == -1) {
printf("%s does not exist.\n", title.data());
return;
}
this->users[userIndex].setRatingAt(bookIndex, newRating);
printf("The rating has been updated.\n");
}
void Library::getRecommendations(string username) {
if (this->users.size() == 0 || this->books.size() == 0) {
printf("The library has not been fully initialized.\n");
return;
}
// Find the requested user:
int userIndex = -1;
for (int i = 0; i < this->users.size(); i++) {
if (this->users[i].getUsername() == Utilities::toLower(username)) {
userIndex = i;
break;
}
}
if (userIndex == -1) {
printf("%s does not exist.\n", username.data());
return;
}
// Determine this users most similar user:
if (this->users.size() > 1) {
User requestedUser = this->users[userIndex];
vector<int> recommendationScores;
for (int i = 0; i < this->users.size(); i++) {
User user = this->users[i];
int totalScore = 0;
// Loop through each book to determine the individual score:
for (int l = 0; l < this->books.size(); l++) {
totalScore += (int)pow(requestedUser.getRatingAt(l) - user.getRatingAt(l), 2);
}
recommendationScores.push_back(totalScore);
}
// Determine the lowest recommendation score:
int lowestScore = recommendationScores[userIndex == 0 ? 1 : 0];
int lowestIndex = userIndex == 0 ? 1 : 0;
for (int i = 0; i < recommendationScores.size(); i++) {
if (recommendationScores[i] < lowestScore && recommendationScores[i] != 0) {
// Check if this user has atleast 1 recommended book with 3 stars:
bool foundThreeStars = false;
for (int l = 0; l < this->books.size(); l++) {
if (this->users[i].getRatingAt(l) >= 3) {
foundThreeStars = true;
break;
}
}
if (foundThreeStars) {
lowestScore = recommendationScores[i];
lowestIndex = i;
}
}
}
// Determine the books which are rated 3+:
User similarUser = this->users[lowestIndex];
bool didFindRecommendation = false;
bool didPrint = false;
int recommendationCount = 0;
for (int i = 0; i < this->books.size(); i++) {
if (similarUser.getRatingAt(i) >= 3 && requestedUser.getRatingAt(i) == 0) {
Book book = this->books[i];
if (!didPrint) {
didPrint = true;
printf("Here is the list of recommendations\n");
}
printf("%s by %s\n", book.getTitle().data(), book.getAuthor().data());
didFindRecommendation = true;
recommendationCount++;
}
// Stop after 5 recommendations:
if (recommendationCount == 5) break;
}
if (!didFindRecommendation)
printf("There are no recommendations for %s at present.\n", username.data());
} else {
printf("There are no recommendations for %s at present.\n", username.data());
}
}