-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfaculty.cpp
More file actions
263 lines (233 loc) · 8.39 KB
/
faculty.cpp
File metadata and controls
263 lines (233 loc) · 8.39 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
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include "faculty.h"
#include "user.h"
#include "book.h"
#include "library.h"
#include <iomanip>
using namespace std;
// Constructor
Faculty::Faculty(int userId, int employeeId, const string &name, const string &password, const string &email)
: User(userId, name, password, "Faculty", email), employeeId(employeeId)
{
finePerDay = 0;
maxBooksAllowed = 5;
maxBorrowPeriod = 30;
maxLimitAfterBorrow = 60;
}
// Getters
int Faculty::getEmployeeId() const { return employeeId; }
void Faculty::setEmployeeId(int newEmployeeId) { employeeId = newEmployeeId; }
const Account &Faculty::getAccount() const { return account; }
Account &Faculty::getAccount() { return account; }
// Borrow a book
void Faculty::borrowBook(int bookId, Library &library)
{
// 1. Check if the faculty has reached maxBooksAllowed
if (account.getBorrowedBooks().size() >= maxBooksAllowed)
{
cout << "You have reached the maximum borrow limit of " << maxBooksAllowed << " books." << endl;
return;
}
// 2. Check if the faculty has overdue books(>60 days)
if (overDueCheck(library))
{
cout << "You have overdue books. Please return them before borrowing new books." << endl;
return;
}
// 3. Check if the book is Exists
Book *book = library.findBookById(bookId); // Use library object passed as parameter
if (!book)
{
cout << "Book ID " << bookId << " not found." << endl;
return;
}
// Check if the book is available
if (!book->isAvailable())
{
if (book->getStatus() == "Borrowed")
{
cout << "Book ID " << bookId << " is already borrowed." << endl;
}
else
{
cout << "Book ID " << bookId << " is Reserved" << endl;
}
return;
}
// Get current time
time_t now = time(0);
tm *ltm = localtime(&now);
stringstream ss;
ss << setw(4) << 1900 + ltm->tm_year << "-"
<< setfill('0') << setw(2) << 1 + ltm->tm_mon << "-"
<< setfill('0') << setw(2) << ltm->tm_mday << " "
<< setfill('0') << setw(2) << ltm->tm_hour << ":"
<< setfill('0') << setw(2) << ltm->tm_min << ":"
<< setfill('0') << setw(2) << ltm->tm_sec;
string borrowTime = ss.str();
// 4. Add the book to student's account
account.addToBorrowedBooks(bookId, borrowTime);
// 5. Mark the book as borrowed
book->setBorrowerId(this->getUserId());
book->setStatus("Borrowed");
cout << "Book ID " << bookId << " successfully borrowed on " << borrowTime << "." << endl;
}
// Check if the faculty has overdue books (>60 days)
bool Faculty::overDueCheck(Library &library)
{
vector<Account::BorrowedBook> borrowedBooks = getAccount().getBorrowedBooks();
time_t now = time(0); // Get current time
struct tm dueDate = {};
int totalAmount = 0;
bool hasOverDue = false;
for (const auto &bookEntry : borrowedBooks)
{
int bookId = bookEntry.bookId;
string borrowTime = bookEntry.borrowTimestamp;
// Convert borrowTime (string) to time_t
istringstream ss(borrowTime);
ss >> get_time(&dueDate, "%Y-%m-%d %H:%M:%S"); // Parse date format
time_t borrowTimestamp = mktime(&dueDate);
// Calculate days borrowed
int daysBorrowed = (now - borrowTimestamp) / (60 * 60 * 24);
// Change hasOverDue to true if daysBorrowed is greater than maxLimitAfterBorrow
if (daysBorrowed > maxLimitAfterBorrow)
{
hasOverDue = true;
}
}
return hasOverDue;
}
void Faculty::displayOverDueBooks(Library &library)
{
vector<Account::BorrowedBook> borrowedBooks = getAccount().getBorrowedBooks();
time_t now = time(0); // Get current time
struct tm dueDate = {};
int totalAmount = 0;
bool hasOverDue = false;
cout << "Overdue books:" << endl;
cout << "Book ID | Title | Due Date | Days Overdue " << endl;
cout << "--------|-------|----------|--------------" << endl;
for (const auto &bookEntry : borrowedBooks)
{
int bookId = bookEntry.bookId;
string borrowTime = bookEntry.borrowTimestamp;
// Convert borrowTime (string) to time_t
istringstream ss(borrowTime);
ss >> get_time(&dueDate, "%Y-%m-%d %H:%M:%S"); // Parse date format
time_t borrowTimestamp = mktime(&dueDate);
// Calculate days borrowed
int daysBorrowed = (now - borrowTimestamp) / (60 * 60 * 24);
// Change hasOverDue to true if daysBorrowed is greater than maxLimitAfterBorrow
if (daysBorrowed > maxBorrowPeriod)
{
Book *book = library.findBookById(bookId);
cout << bookId << " | " << book->getTitle() << " | " << put_time(&dueDate, "%Y-%m-%d") << " | " << daysBorrowed - maxBorrowPeriod << endl;
hasOverDue = true;
}
}
if (!hasOverDue)
{
cout << "No overdue books." << endl;
}
}
void Faculty::displayOverDueForBook(int bookID, Library &library)
{
Book *book = library.findBookById(bookID);
// Check if the book is borrowed by the faculty
if (!book)
{
cout << "Book ID " << bookID << " not found." << endl;
return;
}
if (book->getBorrowerId() != this->getUserId())
{
cout << "You have not borrowed Book ID " << bookID << "." << endl;
return;
}
time_t now = time(0); // Get current time
struct tm dueDate = {};
Account::BorrowedBook borrowedBook;
for (const auto &bookEntry : account.getBorrowedBooks())
{
if (bookEntry.bookId == bookID)
{
borrowedBook = bookEntry;
break;
}
}
string borrowTime = borrowedBook.borrowTimestamp;
// Convert borrowTime (string) to time_t
istringstream ss(borrowTime);
ss >> get_time(&dueDate, "%Y-%m-%d %H:%M:%S"); // Parse date format
time_t borrowTimestamp = mktime(&dueDate);
// Calculate days borrowed
int daysBorrowed = (now - borrowTimestamp) / (60 * 60 * 24);
// Get user-specific fine settings
if (daysBorrowed > maxBorrowPeriod)
{ // `maxBorrowPeriod` is inherited from `User`
int overdueDays = daysBorrowed - maxBorrowPeriod;
string bookTitle = book ? book->getTitle() : "Unknown";
cout << "Book ID: " << bookID << " | Title: " << bookTitle
<< " | Overdue by: " << overdueDays << " days"
<< " |Borrowed on: " << borrowTime << endl;
}
else
{
int overdueDays = 0;
string bookTitle = book ? book->getTitle() : "Unknown"; // If book is not found, display "Unknown"
cout << "Book ID: " << bookID << " | Title: " << bookTitle
<< " | Overdue by: " << overdueDays << " days"
<< " |Borrowed on: " << borrowTime << endl;
}
}
void Faculty::returnBook(int bookId, Library &library)
{
// 1. Check if the book is borrowed by the faculty
Book *book = library.findBookById(bookId);
if (!book)
{
cout << "Book ID " << bookId << " not found." << endl;
return;
}
if (book->getBorrowerId() != this->getUserId())
{
cout << "You have not borrowed Book ID " << bookId << "." << endl;
return;
}
// 2. Get current time
time_t now = time(0);
tm *ltm = localtime(&now);
stringstream ss;
ss << setw(4) << 1900 + ltm->tm_year << "-"
<< setfill('0') << setw(2) << 1 + ltm->tm_mon << "-"
<< setfill('0') << setw(2) << ltm->tm_mday << " "
<< setfill('0') << setw(2) << ltm->tm_hour << ":"
<< setfill('0') << setw(2) << ltm->tm_min << ":"
<< setfill('0') << setw(2) << ltm->tm_sec;
string returnTime = ss.str();
this->displayOverDueForBook(bookId, library);
account.addToHistory(bookId);
vector<Account::BorrowedBook> temp;
vector<Account::BorrowedBook> borrowedBooks = getAccount().getBorrowedBooks();
// Remove the book from the faculty's account
for (auto &borrowedBook : borrowedBooks)
{
if (borrowedBook.bookId != bookId)
{
temp.push_back(borrowedBook);
}
}
account.deleteBorrowedBooks();
for (auto &borrowedBook : temp)
{
account.addToBorrowedBooks(borrowedBook.bookId, borrowedBook.borrowTimestamp);
}
// 3. Mark the book as available
book->setBorrowerId(-1);
book->setStatus("Available");
cout << "Book ID " << bookId << " successfully returned on " << returnTime << "." << endl;
}