/
Library.cpp
347 lines (302 loc) · 9.67 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
340
341
342
343
344
#include "Library.h"
#include "Book.h"
#include <iomanip>
#include <iostream>
#include <string>
Library::Library()
{
holdings.reserve(100);
members.reserve(100);
currentDate = 0;
}
//Prompt the user for the idCode, title and author of the book
//If the book already exists, then exit the function.
//Else, add the book to the collection
void Library::addBook()
{
std::string idCode;
std::string title;
std::string author;
std::cin.ignore(1000, '\n');
std::cout << "Please enter the idCode of the book: ";
std::getline(std::cin, idCode);
std::cout << "Please enter the title of the book: ";
std::getline(std::cin, title);
std::cout << "Please enter the author of the book: ";
std::getline(std::cin, author);
if(GetBook(idCode) != NULL)
{
std::cout << "That book ID already exists!" << std::endl;
std::cout << "Returning to menu." << std::endl << std::endl;
return;
}
Book tmpBook(idCode, title, author);
holdings.push_back(tmpBook);
}
//Prompt the user for the id number of the patron and name
//If the patron already exists, say so and exit the function
//Else, add the patron
void Library::addMember()
{
std::cin.ignore(1000, '\n');
std::string idNumber;
std::string name;
std::cout << "Please enter the ID number of the patron: ";
std::getline(std::cin, idNumber);
std::cout << "Please enter the name of the patron: ";
std::getline(std::cin, name);
if(GetPatron(idNumber) != NULL)
{
std::cout << "That patron ID already exists!" << std::endl;
std::cout << "Returning to menu." << std::endl << std::endl;
return;
}
Patron tmpPatron(idNumber, name);
members.push_back(tmpPatron);
}
//Takes a patron and book ID
//Checks to make sure that the patron and book exist, returns if they dont
//If book is on hold, only check out to that patron
//If the book is on the shelf, it's fair game, set proper values.
void Library::checkOutBook(std::string patronID, std::string bookID)
{
Book* book = GetBook(bookID);
Patron* patron = GetPatron(patronID);
if(book == NULL || patron == NULL)
{
if(book == NULL)
{
std::cout << std::endl << "No such book in holdings." << std::endl;
}
if(patron == NULL)
{
std::cout << std::endl << "No such patron in records." << std::endl;
}
return;
}
if(book->getLocation() == CHECKED_OUT)
{
std::cout << "This book is already checked out." << std::endl << std::endl;
}
else if(book->getLocation() == ON_HOLD)
{
if(book->getRequestedBy()->getIdNum() == patron->getIdNum())
{
book->setLocation(CHECKED_OUT);
book->setCheckedOutBy(GetPatron(patronID));
book->setDateCheckedOut(currentDate);
book->setRequestedBy(NULL);
patron->addBook(book);
}
else
{
std::cout << "This book is on hold and can only be checked out to requestee." << std::endl <<std::endl;
}
}
else
{
book->setLocation(CHECKED_OUT);
book->setCheckedOutBy(patron);
book->setDateCheckedOut(currentDate);
patron->addBook(book);
}
}
int Library::GetCurrentDate()
{
return currentDate;
}
//The Next two functions are helpers that I found myself using a lot. It helped
//keep the code cleaner. Returns a nullptr if we don't find anything
Patron* Library::GetPatron(std::string idNum)
{
for(int i = 0; i < members.size(); i++)
{
if(members.at(i).getIdNum() == idNum)
{
return &members.at(i);
}
}
return NULL;
}
Book* Library::GetBook(std::string idNum)
{
for(int i = 0; i < holdings.size(); i++)
{
if(holdings.at(i).getIdCode() == idNum)
{
return &holdings.at(i);
}
}
return NULL;
}
//Increments the date AND computes fines
void Library::incrementCurrentDate()
{
currentDate++;
//Loop through and increase fines as needed
for(int i = 0; i < members.size(); i++)
{
for(int j = 0; j < members.at(i).getCheckedOutBooks().size(); j++)
{
if((members.at(i).getCheckedOutBooks().at(j)->getDateCheckedOut() + 21) < currentDate)
{
members.at(i).amendFine(DAILY_FINE);
}
}
}
}
//I left this able to take negative numbers, so the librarian could, in theory, use the
//pay fine menu option to add an additional fine (for lost book?)
void Library::payFine(std::string patronID, double payment)
{
Patron* patron = GetPatron(patronID);
if(patron == NULL)
{
std::cout << std::endl << "No such patron exists. No adjustment made." << std::endl;
return;
}
for(int i = 0; i < members.size(); i++)
{
if(members.at(i).getIdNum() == patronID)
{
members.at(i).amendFine(-(payment));
std::cout << "Fines for " << members.at(i).getName() << " are now $" <<
std::setprecision(2) << std::fixed << members.at(i).getFineAmount() << std::endl;
break;
}
}
}
//Puts a book on the hold shelf, unless it is already requested.
//If checked out, note that it should go to the hold shelf
void Library::requestBook(std::string patronID, std::string bookID)
{
Book* book = GetBook(bookID);
Patron* patron = GetPatron(patronID);
if(book == NULL || patron == NULL)
{
if(book == NULL)
{
std::cout << std::endl << "No such book in holdings." << std::endl;
}
if(patron == NULL)
{
std::cout << std::endl << "No such patron in records." << std::endl;
}
return;
}
if(book->getRequestedBy() == NULL)
{
book->setRequestedBy(patron);
if(book->getLocation() == ON_SHELF)
{
book->setLocation(ON_HOLD);
}
}
else
{
std::cout << "This book is already requested!";
}
}
//Return a book, if it is not already on the shelf.
void Library::returnBook(std::string bookID)
{
Book* book = GetBook(bookID);
if(book == NULL)
{
std::cout << std::endl << "No such book in holdings." << std::endl << std::endl;
return;
}
if(book->getLocation() == ON_SHELF)
{
std::cout << "This book is already on the shelf." << std::endl;
}
else if(book->getLocation() == ON_HOLD)
{
std::cout << "This book is on the hold shelf and cannot be returned." << std::endl;
}
else
{
Patron* patron = book->getCheckedOutBy();
if(book->getRequestedBy() == NULL)
{
book->setLocation(ON_SHELF);
book->setCheckedOutBy(NULL);
book->setDateCheckedOut(-1);
patron->removeBook(book);
}
else
{
book->setLocation(ON_HOLD);
book->setCheckedOutBy(NULL);
book->setDateCheckedOut(-1);
patron->removeBook(book);
}
}
}
//Goes through the public properties of a book and prints them out.
//This will print out if a book is checked out AND who requested it, if that situation is true
void Library::viewBookInfo(std::string bookID)
{
Book* book = GetBook(bookID);
if(book == NULL)
{
std::cout << "No such holding exists." << std::endl;
}
else
{
std::cout << std::endl << "ID: " << book->getIdCode() << std::endl;
std::cout << "Title: " << book->getTitle() << std::endl;
std::cout << "Author: " << book->getAuthor() << std::endl;
if(book->getLocation() == ON_SHELF)
{
std::cout << "Location: ON SHELF" << std::endl;
}
else if(book->getLocation() == ON_HOLD)
{
std::cout << "Location: ON HOLD" << std::endl;
std::cout << "Requested By: " << book->getRequestedBy()->getName() << std::endl;
std::cout << "Requested By ID: " << book->getRequestedBy()->getIdNum() << std::endl;
}
else
{
std::cout << "Location: CHECKED OUT" << std::endl;
std::cout << "Checked out by: " << book->getCheckedOutBy()->getName() << std::endl;
std::cout << "Checked out by ID: " << book->getCheckedOutBy()->getIdNum() << std::endl;
std::cout << "Due date: " << book->getDateCheckedOut() + 21 << std::endl;
if(book->getRequestedBy() != NULL)
{
std::cout << "This book is also requested by patron: " << book->getRequestedBy()->getIdNum()
<< std::endl;
std::cout << "It should be put on the hold shelf when checked back in." << std::endl;
}
}
}
}
//Same as above. Uses viewBookInfo to print books checked out to a patron.
void Library::viewPatronInfo(std::string patronID)
{
Patron* patron = GetPatron(patronID);
if(patron == NULL)
{
std::cout << std::endl << "That patron does not exist!" << std::endl;
}
else
{
std::cout << std::endl << "Patron ID: " << patron->getIdNum() << std::endl;
std::cout << "Name: " << patron->getName() << std::endl;
std::cout << "Fines: $" << std::setprecision(2) << std::fixed << patron->getFineAmount() << std::endl;
if(patron->getCheckedOutBooks().size() == 0)
{
std::cout << "No checked out books." << std::endl << std::endl;
}
else
{
std::cout << "Checked out books:" << std::endl << std::endl;
for(int i = 0; i < patron->getCheckedOutBooks().size(); i++)
{
viewBookInfo(patron->getCheckedOutBooks().at(i)->getIdCode());
}
std::cout << std::endl << std::endl;
}
}
}