-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoans.cpp
More file actions
650 lines (568 loc) · 17.3 KB
/
Loans.cpp
File metadata and controls
650 lines (568 loc) · 17.3 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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
/*
Lisa Lin (lisalin@my.unt.edu)
Homework 3 Assignment - Library Management System
Started: 3/12/2021
Loans storage cpp file
*/
#include "Loans.h"
#include "Loan.h"
#include "Books.h"
#include "Book.h"
#include "Patrons.h"
#include "Patron.h"
#include <string>
#include <map>
#include <iostream>
#include <fstream>
#include <time.h>
#include <iterator>
using namespace std;
Loan* Loans::findLoan(int lID) {
map<int,Loan*>::iterator it = loans.find(lID);
if(it != loans.end())
{
return it->second;
}
return NULL;
}
void Loans::searchLoan() {
Loan* loan;
int loanID;
cout << "Enter loan ID: ";
cin >> loanID;
loan = findLoan(loanID);
if (loan != NULL)
{
cout << "Loan #" << loanID << " exists in the system." << endl;
}
else
{
cout << "Loan #" << loanID << " does not exist in the system." << endl;
}
}
/*
* menu functions
*/
void Loans::checkOutBook(Books* books, Patrons* patrons){
Loan* loan;
Book* book;
Patron* patron;
int loanID, bookID, patronID, tempPatronID;
int numBook = 1;
time_t checkoutDate, checkinDate;
string status;
char c; //choice
cout << endl << "<<< CREATE LOAN >>>" << endl << endl;
do {
cout << "Enter loan ID: ";
cin >> loanID;
loan = findLoan(loanID);
if (loan != NULL) { //checks if loan with entered Loan ID exists
cout << "Loan with this loan ID already exists in the system." << endl;
do {
cout << "Would you like to enter a different loan ID? (y/n): ";
cin >> c;
if ((c != 'y') && (c != 'n'))
{
cout << "Invalid option." << endl;
}
else
{
if (c == 'n')
{
return;
}
else
{
break;
}
}
} while (true);
}
} while (loan != NULL);
loan = new Loan();
loan->setLoanID(loanID);
do {
cout << "Enter patron ID: ";
cin >> patronID;
patron = patrons->findPatron(patronID);
if (patron == NULL) {
cout << "Entered patron ID does not exist." << endl;
do {
cout << "Would you like to enter a different patron ID? (y/n): ";
cin >> c;
if ((c != 'n') && (c != 'y'))
{
cout << "Invalid option." << endl;
}
else
{
if (c == 'n')
{
return;
}
else
{
break;
}
}
} while (true);
}
} while (patron == NULL);
loan->setPatronID(patronID);
//check to see if patron has more than 6 books checked out / if they would exceed 6 books upon checkout
patron = patrons->findPatron(patronID);
int currentBooksChecked = patron->getBooksChecked();
patron->setBooksChecked(currentBooksChecked);
if (currentBooksChecked >= 6) { //stops user from checking out > 6 books
cout << "You can only check out a maximum of 6 books. Please return a book first." << endl;
cout << "You have " << currentBooksChecked << " books checked out." << endl; //debuggin purposes
return;
}
//check to see if patron has any overdue books
/*
string tempStatus = "OVERDUE";
map<int, Loan*>::iterator it;
if (it->second->getStatus() == tempStatus){
cout << "Patron has overdue book. Please check in book first." << endl;
return;
}
*/
do {
cout << "Enter book ID: ";
cin >> bookID;
book = books->findBook(bookID);
if (book == NULL) { //checks if book with entered book ID exists
cout << "Book with entered book ID does not exist." << endl;
do {
cout << "Would you like to enter a different book ID? (y/n): ";
cin >> c;
if ((c != 'n') && (c != 'y'))
{
cout << "Invalid option." << endl;
}
else
{
if (c == 'n')
{
return;
}
else
{
break;
}
}
} while (true);
}
} while (book == NULL);
string statusString = book->getStatus();
if (statusString == "IN") {
cout << "Book found." << endl;;
loan->setBookID(bookID);
}
else if (statusString == "OUT") {
cout << "Book is already checked out."<< endl;
book = NULL;
return;
}
else if (statusString == "LOST") {
cout << "Book is lost. It cannot be checked out." << endl;
book = NULL;
return;
}
else if (statusString == "REPAIR") {
cout << "Book is out for repair. It cannot be checked out." << endl;
book = NULL;
return;
}
/*
* time
*/
checkoutDate = time(0);
time(&checkoutDate); //current time
checkinDate = checkoutDate + 864000; //sets check in date to 10 days after checkout date
loan->setDueDate(checkinDate);
loan->setCheckoutDate(checkoutDate);
/*
set recheck to false
*/
//loan->setRecheck(false);
//changes values in other objects
loans[loanID] = loan; // makes a loan
book->setStatus("OUT");
patron->setBooksChecked(currentBooksChecked + 1);
time_t dueDate = loan->getDueDate();
cout << book->getTitle() << " has been successfully loaned. Enjoy your book! It is due on ";
loan->dueDateToMonth(dueDate);
cout << "Returning to main menu..." << endl << endl;
}
void Loans::checkInBook(Books* books, Patrons* patrons){
Loan* loan;
Book* book;
Patron* patron;
int loanID, bookID, patronID;
char c;
cout << "<<< CHECK IN BOOK >>>" << endl << endl;
do {
cout << "Enter loan ID: ";
cin >> loanID;
loan = findLoan(loanID);
if (loan == NULL) {
cout << "Loan with loan ID #" << loanID << " does not exist." << endl;
do {
cout << "Would you like to enter a ifferent loan ID? (y/n): ";
cin >> c;
if ((c != 'y') && (c != 'n'))
{
cout << "Invalid option." << endl;
}
else
{
if (c == 'n')
{
return;
}
else
{
break;
}
}
} while (true);
}
} while (loan == NULL);
bookID = loan->getBookID();
patronID = loan->getPatronID();
book = books->findBook(bookID);
patron = patrons->findPatron(patronID);
//delete from other functions
int numBooksChecked = patron->getBooksChecked();
string bookStatus = "IN";
book->setStatus(bookStatus); //sets status to IN
patron->setBooksChecked(numBooksChecked - 1);
//erases entities
loans.erase(loanID);
delete loan;
cout << book->getTitle() << " has been checked in. Thank you!" << endl;
}
void Loans::recheckBook() {
int loanID;
char c;
Loan* loan;
cout << "<<< RECHECK BOOK >>>" << endl << endl;
do {
cout << "Enter loan ID: ";
cin >> loanID;
loan = findLoan(loanID);
if (loan == NULL) {
cout << "Loan with loan ID #" << loanID << " does not exist." << endl;
do {
cout << "Would you like to enter a ifferent loan ID? (y/n): ";
cin >> c;
if ((c != 'y') && (c != 'n'))
{
cout << "Invalid option." << endl;
}
else
{
if (c == 'n')
{
return;
}
else
{
break;
}
}
} while (true);
}
} while (loan == NULL);
//checks if book has been rechecked before
loan = findLoan(loanID);
bool recheck = loan->getRecheck(); //this line is having problems
if (recheck == true) {
cout << "Books can only be rechecked once." << endl;
return;
}
/*
time_t currentTime = time(0);
time(¤tTime);
*/
time_t currentDate = time(0);
time(¤tDate);
time_t newDueDate = currentDate + 864000;
loan->setDueDate(newDueDate); //sets new due date
loan->setRecheck(true); // sets recheck bool to be true in loan
//confirmation message
cout << "Book has been rechecked and is due ten days from today. The new due date is ";
loan->dueDateToMonth(newDueDate);
}
void Loans::editLoan(Books* books, Patrons* patrons){
int o;
char c;
Patron* patron;
Loan* loan;
Book* book;
int loanID;
if (loans.empty())
{
cout << "No loans have been created." << endl;
return;
}
do {
cout << "Enter the loan ID: ";
cin >> loanID;
loan = findLoan(loanID);
if (loan == NULL) {
cout << "Loan not found." << endl;
do {
cout << "Would you like to enter a new loan ID? (y/n) ";
cin >> c;
if ((c != 'y') && (c != 'n'))
{
cout << "Choice not recongized, please try again." << endl;
}
else
{
if (c == 'n')
{
return;
}
else
{
break;
}
}
} while (true);
}
} while (loan == NULL);
cout << endl;
loan = findLoan(loanID);
int patronID = loan->getPatronID(), bookID = loan->getBookID();
do {
cout << "<<< EDITING MENU FOR LOAN #" << loanID << " >>>" << endl;
cout << endl << "----------" << endl;
cout << "0 - Exit editing menu" << endl;
cout << "1 - Edit loan ID" << endl;
cout << "2 - Edit book status to repair and delete loan." << endl;
cout << "Select an option: ";
cin >> o;
switch(o)
{
case 0:
break;
case 1:
{
cout << "Enter new loan ID: ";
cin >> loanID;
loan->setLoanID(loanID);
cout << "Loan ID successfully updated." << endl;
break;
}
case 2:
{
cout << "<<< CHANGE BOOK STATUS TO REPAIR >>> " << endl << endl;
book->setStatus("REPAIR"); //sets status to repair
//delete from other functions
int numBooks = patron->getBooksChecked();
patron->setBooksChecked(numBooks - 1);
//erases entities
loans.erase(loanID); //deletes loan
delete loan;
break;
}
default:
cout << "Invalid option." << endl;
break;
}
} while (o != 0);
}
void Loans::reportLost(Books* books, Patrons* patrons){
cout << endl << "<<< REPORT LOST BOOK >>>" << endl << endl;
if (loans.empty()) { //checks to see if any loans are there
cout << "No loans have been created." << endl;
return;
}
string status;
int loanID, bookID, patronID;
float fine, oldfine;
char c;
Book* book;
Patron* patron;
Loan* loan;
do {
cout << "Enter loan ID: ";
cin >> loanID;
loan = findLoan(loanID);
if (loan == NULL) {
cout << "Loan with loan ID #" << loanID << " does not exist." << endl;
do {
cout << "Would you like to enter a ifferent loan ID? (y/n): ";
cin >> c;
if ((c != 'y') && (c != 'n'))
{
cout << "Invalid option." << endl;
}
else
{
if (c == 'n')
{
return;
}
else
{
break;
}
}
} while (true);
}
} while (loan == NULL);
loan = findLoan(loanID); //find loan
bookID = loan->getBookID(); //get book id from loan
patronID = loan->getPatronID(); //get patron id from loan
book = books->findBook(bookID); //find book from book id
patron = patrons->findPatron(patronID); //find patron from patron id
/*
changing patron and book class variables
*/
oldfine = patron->getFine(); //find patron old fine
fine = book->getCost(); //set fine to book cost
cout << "Fine of $" << fine << " has been added to the patron." << endl;
fine += oldfine; // finds overall fine
cout << "Loan has been deleted." << endl;
patron->setFine(fine); // sets patron fine to sum of both
//delete from other functions
int numBooks = patron->getBooksChecked();
book->setStatus("LOST"); //sets status to IN
patron->setBooksChecked(numBooks - 1);
//erases entities
loans.erase(loanID); //deletes loan
delete loan;
}
void Loans::updateLoan(Books* books, Patrons* patrons){
cout << "<<< UPDATE LOANS >>>" << endl;
if (loans.empty()) {
cout << "No loans have been created." << endl;
return;
}
map<int, Loan*>::iterator it;
string status = "OVERDUE";
for (it = loans.begin(); it != loans.end(); it++)
{
(it->second)->markLoanOverdue(); //updates loan status
}
cout << "Loans successfully updated." << endl;
}
void Loans::listAllOverdue(){
cout << "<<< PRINT ALL OVERDUE >>>" << endl << endl;
if (loans.empty())
{
cout << "No loans have been created." << endl;
return;
}
string status = "OVERDUE";
do {
if ((status != "IN") && (status != "OVERDUE")) {
cout << "Status could not be found." << endl;
} //debugging
} while ((status != "IN") && (status != "OVERDUE"));
map<int, Loan*>::iterator it;
if (it->second->getStatus() == status){
it->second->printLoanInfo();
}
cout << "All overdue books:" << endl;
for (it = loans.begin(); it != loans.end(); it++) {
((*it).second)->printLoanInfo();
}
}
void Loans::listSpecificOverdue(){
cout << "<<< LIST SPECIFIC PATRON'S OVERDUE BOOKS >>>" << endl << endl;
if (loans.empty()) {
cout << "No loans have been created." << endl;
return;
}
map<int, Loan*>::iterator it;
string status = "OVERDUE";
for (it = loans.begin(); it != loans.end(); it++)
{
(it->second)->markLoanOverdue(); //updates loan status
if (it->second->getStatus() == status)
{
it->second->printLoanInfo();
}
}
}
void Loans::printAllLoans() {
cout << "<<< PRINT ALL BOOKS >>>" << endl << endl;
if (loans.empty())
{
cout << "No loans have been created." << endl;
return;
}
map<int, Loan*>::iterator it;
cout << "Information for All Books" << endl;
for (it = loans.begin(); it != loans.end(); it++) {
((*it).second)->printLoanInfo();
}
}
/*
store and load
*/
/*
int Loans::numOfLoans(){
} //for use in storing
*/
void Loans::store(){
ofstream fout;
fout.open("loans.dat");
Loan* loan;
map<int, Loan*>::iterator it;
fout << numOfLoans() << endl;
for (it = loans.begin(); it != loans.end(); it++)
{
cout << (*it).first << endl;
loan = ((*it).second);
fout << loan->getLoanID() << endl;
fout << loan->getPatronID() << endl;
fout << loan->getBookID() << endl;
fout << loan->getStatus() << endl;
fout << loan->getCheckoutDate() << endl;
fout << loan->getDueDate() << endl;
fout << loan->getRecheck() << endl;
}
fout.close();
}
void Loans::load(){
ifstream fin;
int numLoans;
fin.open("books.dat"); // u should probably check to see if it actually opened
/*
* variable declarations
*/
int loanID, patronID, bookID;
string status;
bool recheck;
time_t checkoutDate, checkinDate;
fin >> numLoans;
for (int i = 0; i < numLoans; i++)
{
fin >> loanID;
fin >> patronID;
fin >> bookID;
fin.ignore();
getline(fin, status);
fin >> checkoutDate;
fin >> checkinDate;
loans[loanID] = new Loan(loanID, bookID, patronID, status, checkoutDate, checkinDate, recheck);
}
fin.close();
}
int Loans::numOfLoans() {
Loan* loan;
map<int, Loan*>::iterator it;
int n = 0;
for (it = loans.begin(); it != loans.end(); it++)
{
n++;
}
return n; //returns number of books
}