This repository was archived by the owner on Dec 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibrary.cpp
More file actions
374 lines (357 loc) · 12.2 KB
/
Library.cpp
File metadata and controls
374 lines (357 loc) · 12.2 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
#ifndef LIBRARY_CPP
#define LIBRARY_CPP
#include "Library.h"
#include "Time.h"
void Library::PrintLibraryBook(int id) const{
if (idLib.find(id) != idLib.end()){
auto it = idLib.find(id);
it->second->PrintBook();
}
else{
cout << "BookID# " << id << " not found." << endl;
}
}
//Adds a Book to the map using the
//Books unique ID, also stores the Books
//ID so it cannot be reused
void Library::addBook(Book *aBook){
idLib[aBook->getID()] = aBook;
usedBookIDs.insert(aBook->getID());
numBooks++;
}
//Adds a User to the map using the
//User unique ID, also stores the User
//ID so it cannot be reused
void Library::addUser(User *aUser){
if ((userLib.find(aUser->getID())) == userLib.end()){
userLib[aUser->getID()] = aUser;
usedUserIDs.insert(aUser->getID());
numUsers++;
}
}
//Precondition vector will be greater than 3
//Converts the numeric data regarding a Book to int
//So it can be checkout while reading File
vector<int> Library::convertToInt(vector<string> input){
vector<int> intVersion;
for (unsigned int i = 3; i < input.size(); i++){
intVersion.push_back(stoi(input[i]));
}
return intVersion;
}
//Precondition: vector<string> size is > 3
//Finds the Book, and then checks the Book out using Book
//Functions
//The bookInfo vector has ALL the Books the User has upon File input
//So collectBookInfo only stores the data for each Book. At each point that
//collectBookInfo is empty, it adds the User ID, because each Book needs
//To be updated with UserID
void Library::checkout(vector<string> stringInfo){
vector<int> bookInfo = convertToInt(stringInfo);
if (idLib.find(bookInfo[1]) == idLib.end()){//not sure if i need this..
cout << "Could not find library book with ID# " << bookInfo[1] << "." << endl;
return;
}
int userID = stoi(stringInfo[0]);
vector<int> collectBookInfo;
for (unsigned int i = 0; i < bookInfo.size(); i++){
if (collectBookInfo.size()==0){
collectBookInfo.push_back(userID);
}
collectBookInfo.push_back(bookInfo[i]);
//Need 4 things to change book status
//bookID, userID, dueDate, renewCount
//bookID will always be collectBookInfo[1]
if (collectBookInfo.size()==4){
idLib[collectBookInfo[1]]->changeBookStatus(collectBookInfo);
userLib[userID]->addBook(idLib[collectBookInfo[1]]);
collectBookInfo.clear();
}
}
}
//Checkout Book from Command line.
//Finds Account and Book, then if the Account
//Does not have any overdue books, has fewer than 10 Books
//And the book is Available
//It is allowed to check it out
void Library::checkout(int userID, int bookID){
if (userLib.find(userID)== userLib.end()){
cout << "AccountID# " << userID << " not found." << endl;
return;
}
if (idLib.find(bookID) == idLib.end()){
cout << "BookID# " << bookID << " not found." << endl;
}
else{
auto bookIT = idLib.find(bookID);
auto userIT = userLib.find(userID);
if (userIT->second->getBooksOverDueStatus()==true){
cout << "Account has books overdue.\n\n";
}
else if (bookIT->second->getAvailability() == false){
cout << "Book is already checked out.\n\n";
}
else if (userIT->second->numBook()){
cout << "Account already has 10 books checked out.\n\n";
}
else{
cout << "Book successfully checked out." << endl;
userIT->second->addBook(bookIT->second);
bookIT->second->PrintBook();
}
}
}
//Given a Book ID, it will remove it from the Library
//If the Book is checkedout, it will force return it
//Then it will delete it.
void Library::removeBook(int id){
if (idLib.find(id) != idLib.end()){
auto it = idLib.find(id);
//Change Books status to True by returning it from the User
//Then it can enter following IF statement to properly delete book
if (it->second->getAvailability() == false){
cout << "Force returing book from AccountID# " << it->second->getAccountID() << ".\n";
userLib[it->second->getAccountID()]->returnBookUser(id);
}
if (it->second->getAvailability()){
cout << "\"" << it->second->getTitle() << "\" by " << it->second->getAuthor() << " successfully removed.\n\n";
Book* temp = it->second;
idLib.erase(it);
delete temp;
}
}
else{
cout << "BookID# " << id << " not found.";
}
}
//Given a User ID, it will remove it from the Library
//If the User has checkedout Books, it will force return all of them
//Then it will delete the User.
void Library::removeUser(int id){
if (userLib.find(id) != userLib.end()){
auto it = userLib.find(id);
it->second->returnAllBooks();
cout << it->second->getName() << "'s account successfully removed.\n\n";
User* temp = it->second;
userLib.erase(it);
delete temp;
}
else{
cout << "UserID# " << id << " not found.\n\n";
}
}
//Print information about the User
void Library::PrintLibraryUser(int id) const{
if (userLib.find(id) != userLib.end()){
auto it = userLib.find(id);
it->second->PrintUser();
}
else{
cout << "AccountID# " << id << " not found.\n\n";
}
}
//The following 6 boolean functions are used with the
//"sort" function from <algorithm> each of them allows for Books
//Or Users to be sorted according to their specific data
bool sortByTitle(const Book* b1, const Book* b2){return b1->getTitle() < b2->getTitle();}
bool sortByAuthor(const Book* b1, const Book* b2){return b1->getAuthor() < b2->getAuthor();}
bool sortByGenre(const Book* b1, const Book* b2){return b1->getGenre() < b2->getGenre();}
bool sortByPop(const Book* b1, const Book* b2){return b1->getScore() < b2->getScore();}
bool sortByUserName(const User* u1, const User* u2){return u1->getName() < u2->getName();}
bool sortByCheckout(const User* u1, const User* u2){return u1->getNumBooksCheckedOut() > u2->getNumBooksCheckedOut();}
//Given the method to sort data by, it determine if a
//vector<Book*> or vector<User*> needs to be created.
//After creating the vector, it sorts it according to the method
//provided in the parameter
void Library::sortDataBy(string sortBy){
//Book Related Sort
//First creates vector of the necessary items (Books or Users)
//Then using sort functions that I have created above, it will sort the
//vector in the accoring way.
if (sortBy == "TITLE" || sortBy == "AUTHOR" || sortBy == "GENRE" || sortBy == "POPULARITY"){
vector<Book*> sortedBooks;
for (auto it = idLib.begin(); it != idLib.end(); ++it){
sortedBooks.push_back(it->second);
}
if (sortBy == "TITLE"){
sort(sortedBooks.begin(), sortedBooks.end(), sortByTitle);
}
else if (sortBy == "AUTHOR"){
sort(sortedBooks.begin(), sortedBooks.end(), sortByAuthor);
}
else if (sortBy == "GENRE"){
sort(sortedBooks.begin(), sortedBooks.end(), sortByGenre);
}
else{//"POPULARITY"
sort(sortedBooks.begin(), sortedBooks.end(), sortByPop);
}
PrintSortedBook(sortedBooks);
}
//ACCOUNT RELATED SORTING
else{//"NAME" "CHECKOUTS"
vector<User*> sortedUsers;
for (auto it = userLib.begin(); it != userLib.end(); ++it){
sortedUsers.push_back(it->second);
}
if (sortBy == "NAME"){
sort(sortedUsers.begin(), sortedUsers.end(), sortByUserName);
// cout << "not yet";
}
else{ // Refers to CHECKOUTS
sort(sortedUsers.begin(), sortedUsers.end(), sortByCheckout);
}
PrintSortedUser(sortedUsers);
}
}
//Provided a sorted vector, it prints all the data accordingly
void Library::PrintSortedBook(vector<Book*>& sortedBooks) const {
for (unsigned int i = 0; i < sortedBooks.size(); ++i){
cout << i+1 << ". ";
sortedBooks[i]->PrintBookFromBrowse();
}
cout << endl;
}
//Provided a sorted vector, it prints all the data accordingly
void Library::PrintSortedUser(vector<User*>& sortedUsers) const{
for (unsigned int i = 0; i < sortedUsers.size(); ++i){
cout << i+1 << ". ";
sortedUsers[i]->PrintByUsersInfo();
}
cout << endl;
}
//Given a word or a substring, it will search through
//Titles of all the Books to determine if that substring
//Exists, if it does, it prints it to the screen
void Library::searchTitles(const string searchWord){
int count = 1;
for (auto it = idLib.begin(); it != idLib.end(); ++it){
if (it->second->getTitle().find(searchWord) != std::string::npos){
cout << count << ". ";
count++;
it->second->PrintBookFromBrowse();
}
}
cout << endl;
}
//Given a word or a substring, it will search through
//Authors of all the Books to determine if that substring
//Exists, if it does, it prints it to the screen
void Library::searchAuthors(string searchWord){
int count = 1;
for (auto it = idLib.begin(); it != idLib.end(); ++it){
if (it->second->getAuthor().find(searchWord) != std::string::npos){
cout << count << ". ";
count++;
it->second->PrintBookFromBrowse();
}
}
cout << endl;
}
//Account ID is unique, so it does not need to be
//Created into a vector. It is able to simply be printed
void Library::sortByAccountID(){
int count = 1;
for (auto it = userLib.begin(); it != userLib.end(); ++it){
cout << count << ". ";
count++;
it->second->PrintByUsersInfo();
}
cout << endl;
}
//Books ID is unique, so it does not need to be
//Created into a vector. It is able to simply be printed
void Library::sortByBookID(){
int count = 1;
for (auto it = idLib.begin(); it != idLib.end(); ++it){
cout << count << ". ";
count++;
it->second->PrintBookFromBrowse();
}
cout << endl;
}
//Prints an update of the system
void Library::PrintSystem() const{
cout << "System time: " << Time::getTime() << endl;
cout << "Number of books: " << idLib.size() << endl;
cout << "Number of overdue books: " << numOverDueBooks() << endl;
cout << "Number of accounts: " << userLib.size() << endl;
cout << "Number of overdue accounts: " << numOverDueAccounts() << "\n\n";
}
//Calculates the number of overdue Books
int Library::numOverDueBooks() const {
int count = 0;
for (auto it = idLib.begin(); it != idLib.end(); ++it){
//only increments count if book is checkout AND time > duedate
if ((it->second->getDueDate() < Time::getTime()) && (!it->second->getAvailability())){
count++;
}
}
return count;
}
//Calculates the number of overdue accounts
int Library::numOverDueAccounts() const {
int count = 0;
for (auto it = userLib.begin(); it != userLib.end(); ++it){
if (it->second->getBooksOverDueStatus() == true){
count++;
}
}
return count;
}
//Returns a Book using the Book and User functions
//Prints the completion of the return with a "receipt"
//Including number of days after due that it was returned
void Library::returnBook(int id){
auto bookIT = idLib.find(id);
if (idLib.find(id) == idLib.end()){
cout << "BookID# " << id << " not found.\n\n";
}
else if (bookIT->second->getAvailability()==true){
cout << "Book is currently not checked out.\n\n";
}
else{
auto userIT = userLib.find(bookIT->second->getAccountID());
cout << "Book successfully returned from AccountID# " << bookIT->second->getAccountID();
if (bookIT->second->getDueDate() < Time::getTime()){
cout << " (overdue by " << Time::getTime() - bookIT->second->getDueDate() << " days).\n\n";
}
else{
cout << " (on time).\n\n";
}
userIT->second->returnBookUser(id);
}
}
//Renews all eligible Books in User
void Library::renewUsersBook(int userID){
if (userLib.find(userID)== userLib.end()){
cout << "AccountID# " << userID << " not found." << endl;
return;
}
auto it = userLib.find(userID);
it->second->renewAllBooks();
}
void Library::exportLib(string bookFile, string accountFile){
cout << "Couldnt get to function on time :( \n\n";
}
//Provided with a potential books title and author
//Determines if a book with that title and author
//Already exists, if yes, returns true and book will not be created
//Otherwise returns false and book can be created
bool Library::bookTitleAuthorExists(string title, string author){
for (auto it = idLib.begin(); it != idLib.end(); ++it){
if ((it->second->getTitle()==title) && (it->second->getAuthor()== author)){
return true;
}
}
return false;
}
void Library::makeRecs(int id){
if (userLib.find(id) == userLib.end()){
cout << "AccountID# " << id << " not found." << endl;
return;
}
else{
cout << "Couldnt get to function on time :( \n\n";
}
}
#endif