-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAccount.cpp
More file actions
381 lines (376 loc) · 10.6 KB
/
Account.cpp
File metadata and controls
381 lines (376 loc) · 10.6 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
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#ifndef ACCOUNT_CPP
#define ACCOUNT_CPP
#include <iostream>
#include <string>
#include "Account.h"
using namespace std;
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//deallocate account book holding structures
Account::~Account()
{
//erase from users books and book histories
for (auto it = myBooks.begin();it!= myBooks.end(); ++it)
{
delete it->second;
myBooks.erase(it->second->getBookID());
}
for (auto it = GenreCount.begin();it!= GenreCount.end(); ++it)
{
delete it->second;
GenreCount.erase(it->second->getGenre());
}
for (auto it = AuthorCount.begin();it!= AuthorCount.end(); ++it)
{
delete it->second;
AuthorCount.erase(it->second->getAuthor());
}
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//initialize account with name and ID
Account::Account(string name, int ID){
UserID = ID;
Name = name;
checkouts=myBooks.size();
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//Constructor creates accounts given a vector of id, name, and checkout
//and their books
Account::Account(vector<string> AccountInfo, BookList myLib){
//first 3 eleemnts in vector are user id, name, #books
UserID = atoi((AccountInfo[0]).c_str()); //
Name = AccountInfo[1];
numBooks=atoi((AccountInfo[2]).c_str());
//For evry 3 peices of info(book id, due date,popularity) after that,
//create a book and add it to users list of checked out books
for (unsigned int i = 3; i <AccountInfo.size() ; i=i+3)
{
int id=atoi((AccountInfo[i]).c_str());
//Checks if book even exists.If it does checkout & set book info
if (myLib.hasBook(id))
{
int due=atoi((AccountInfo[i+1]).c_str());
int renew=atoi((AccountInfo[i+2]).c_str());
Book* myBook=new Book(id, due, renew);
myBook=myLib.findBook(id);
myBook->setDueDate(due);
myBook->setRenewTimes(renew);
CheckoutBook(myBook);
myBook->setCheckOutStatus(true);
myBook->setBorrowerID(UserID);
}
else
{
cout<<"Could not find library book with ID# "<<id<<endl;
}
}
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//Deletes book
void Account::Remove(int BookID)
{
myBooks.erase(BookID);//delete
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//Force returns all books to library
void Account::forceRemoveBooks()
{
//If user has any books, go through booklist. Print indication, and remove
if (myBooks.size()>0)
{
for (auto it = myBooks.begin();it!= myBooks.end(); ++it)
{
cout<<"\""<<it->second->getTitle()<<"\" by "<<it->second->getAuthor()
<<" (BookID# "<<it->second->getBookID()<<") force returned."<<endl;
it->second->setCheckOutStatus(false);
}
}
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//Checks out a book given a book
void Account::CheckoutBook(Book* someBook)
{
//Count to check if book exists in history, to see if need to add popularity
bool contains=false;
someBook->setBorrowerID(UserID);
for(auto it = GenreCount.begin();it!= GenreCount.end(); ++it)
{
if (it->second->getBookID()==someBook->getBookID())
{
contains=true;
}
}
//Adds book to booklist and history of books
myBooks.insert(std::pair<int,Book*>(someBook->getBookID(),someBook));
GenreCount.insert(std::pair<string,Book*>(someBook->getGenre(),someBook));
AuthorCount.insert(std::pair<string,Book*>(someBook->getAuthor(),someBook));
//If book was never checked out before, add to popularity score
if (!contains)
{
someBook->addPopularity();
}
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//Calculates 2 favorite genres and sets it
void Account::FaveGenre()
{
//Inserts pairs of genre and count of genre form gretatest to least
set<pair<string, int>, Comparator> counts;
int count;
vector<string> faves;
for(auto it = GenreCount.begin();it!= GenreCount.end(); ++it)
{
count=GenreCount.count(it->first);
counts.insert(std::pair<string,int>(it->second->getGenre(), count));
}
for(auto it = counts.begin();it!= counts.end(); ++it)
{
faves.push_back(it->first);
if(faves.size()==3){break;} //breaks after highest 2 added
}
MostReadGenre=faves[0];
SecondMostReadGenre=faves[1];
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//Finds and sets favorite author
void Account::FaveAuthor()
{
//Inserts pairs of author and counts of author form gretatest to least
set<pair<string, int>, Comparator> counts;
int count;
for(auto it = AuthorCount.begin();it!= AuthorCount.end(); ++it)
{
count=AuthorCount.count(it->first);
counts.insert(std::pair<string,int>(it->second->getAuthor(), count));
}
//Returns the first one or the max out of ordered set
FavoriteAuthor=counts.begin()->first;
cout<<FavoriteAuthor<<endl;
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
void Account::findRecommend(string criteria, string fave, BookList Library)
{
int counter=1;
vector<Book*> vect=Library.order("popularity");
for (unsigned int i = 0; i < vect.size(); ++i)
{
if (criteria=="genre")
{
if(vect[i]->getGenre()==fave)
{
cout<<counter<<". ";
vect[i]->PrintBookDataShort();
vect[i]->PrintStatus();
counter++;
if (counter==3){break;}
}
}
if (criteria=="author")
{
if (vect[i]->getAuthor()==fave)
{
cout<<counter<<". ";
vect[i]->PrintBookDataShort();
vect[i]->PrintStatus();
break;
}
}
}
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//Prints reccomendations of books from a given Library based on
//users favorite author and genres
void Account::Recommend(BookList Library)
{
//If book is already in reading history, dont include in recommendations
for (auto it = GenreCount.begin();it!= GenreCount.end(); ++it)
{
if (Library.hasBook(it->second->getBookID()))
{
Library.removeBook(it->second->getBookID());
}
}
//If no books ever read, no recommendations
if (GenreCount.size()==0)
{
cout<<"No available recommendations."<<endl;
}
//If one book read, recommend 3 books only
else if (GenreCount.size()==1)
{
MostReadGenre=GenreCount.begin()->second->getGenre();
FavoriteAuthor=AuthorCount.begin()->second->getAuthor();
cout<<"You love "<< MostReadGenre <<". We recommend:"<<endl;
findRecommend("genre", MostReadGenre, Library);
cout<<"You love "<< FavoriteAuthor <<". We recommend:"<<endl;
findRecommend("author", FavoriteAuthor, Library);
}
//Else recommend 5 books
else
{
FaveGenre();
FaveAuthor();
cout<<"You love "<< MostReadGenre <<". We recommend:"<<endl;
findRecommend("genre", MostReadGenre, Library);
cout<<"You love "<< SecondMostReadGenre <<". We recommend:"<<endl;
findRecommend("genre", SecondMostReadGenre, Library);
cout<<"You love "<< FavoriteAuthor <<". We recommend:"<<endl;
findRecommend("author", FavoriteAuthor, Library);
}
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//Renews all books if allowed
void Account::renewBooks()
{
int count=0;
for(auto it = myBooks.begin();it!= myBooks.end(); ++it)
{
if (it->second->getRenewed()>=2)
{
count++;
}
}
cout<<myBooks.size()-count<<" of "<<myBooks.size()<<
" books successfully renewed."<<endl;
//Renew books in the list of books.
for(auto it = myBooks.begin();it!= myBooks.end(); ++it)
{
if (it->second->getCheckedOutStatus()==true)
{
it->second->renew();
cout<<endl;
}
}
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//Prints short Account info
void Account::printAccount()
{
cout<<Name;
cout<<" (AccountID# "<<UserID<<"). ";
if (myBooks.size()==0)
{
cout<<"No books checked out."<<endl;
}
else
{
cout<<myBooks.size()<<" books checked out."<<endl;
}
int x=1;
for(auto it = myBooks.begin();it!= myBooks.end(); ++it)
{
cout<< "\t"<<x<<". ";
myBooks[it->first]->PrintBookDataShort();
cout<<endl;
x++;
}
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//Prints full account info
void Account::printAccountFull()
{
cout<<"Name: "<<Name<<endl;
cout<<"AccountID#: "<<UserID<<endl;
if (myBooks.size()==0)
{
cout<<"No books checked out."<<endl;
}
else
{
cout<<myBooks.size()<<" books checked out."<<endl;
int x=1;
for(auto it = myBooks.begin();it!= myBooks.end(); ++it)
{
cout<<"\t"<<x<<". "<<endl;
myBooks[it->first]->PrintBookDataFullIndent();
x++;
}
}
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//Returns a string of an account info and boos in file line format
string Account::outputAccount()
{ //collects all user info in file format
string a=to_string(UserID)+"|"+Name+"|"+to_string(myBooks.size())+"\n";
for (auto it = myBooks.begin(); it !=myBooks.end() ; ++it)
{
a+=it->second->outputCheckedoutBooks();
}
return a;
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//Prints partial account info
void Account::printPartialBook(int BookID)
{
cout<<"Borrower AccountID#: "<<UserID<<endl;
cout<<"Due Date: "<<myBooks[BookID]->getDueDate()<<endl;
cout<<"Times Renwed: "<<myBooks[BookID]->getRenewed()<<endl;
if (myBooks[BookID]->is_Overdue())
{
cout<<"OVERDUE"<<endl;
}
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//Return book back to library
void Account::returnBook(int BookID)
{
//If user has the book with given ID
//Remove it from Booklist, reset its info, and change its status to Available
//Print timing info
if (hasBook(BookID))
{
Book* returnBook=myBooks[BookID];
Remove(BookID);
cout<<"Book successfully returned by AccountID# "<<UserID;
if(returnBook->is_Overdue())
{
cout<<" (overdue by "<<Time::getTime()-returnBook->getDueDate()<<" days)."<<endl;
}
else
{
cout<<" (on time)."<<endl;
}
}
else
{
cout<<"Book is not currently checked out."<<endl;
}
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//returns true if any books are overdue
bool Account::hasOverdueBooks()
{
for(auto it = myBooks.begin();it!= myBooks.end(); ++it)
{
if(it->second->is_Overdue())
{
return true;
}
}
return false;
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//return true if use has book
bool Account::hasBook(int BookID)
{
return (myBooks.find(BookID)!=myBooks.end());
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//returns userid
int Account::getUserID() const
{
return UserID;
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//retruns name
string Account::getName() const
{
return Name;
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//return number of books
int Account::getCheckouts() const
{
return myBooks.size();
}
#endif