-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoan.cpp
More file actions
281 lines (210 loc) · 6.47 KB
/
Loan.cpp
File metadata and controls
281 lines (210 loc) · 6.47 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
/*
File:
Loan.cpp
*/
#include "Loan.h"
#include <iostream>
#include <ctime>
using namespace std;
//Default Constructor
Loan::Loan()
{
int loanID = 0;
int itemID = 0;
int patronID = 0;
string itemCategory = "Undefined";
string dueDate = "Undefined";
string loanDate = "Undefined";
string status = "Normal";
}
//Parametized Constructor
Loan::Loan(int loanID, int itemID, int patronID, string itemCategory, string dueDate, string loanDate, string status)
{
loanID = loanID;
itemID = itemID;
patronID = patronID;
itemCategory = itemCategory;
dueDate = dueDate;
loanDate = loanDate;
status = status;
}
//stringToDate function to convert string date to struct time
tm stringToDate(const string& dateStr) {
tm date = {};
int month, day, year;
//Date string in "MM-DD-YYYY" format
if (sscanf(dateStr.c_str(), "%d-%d-%d", &month, &day, &year) != 3) {
cerr << "Error: Invalid date format. Expected MM-DD-YYYY." << endl;
} else {
date.tm_mon = month - 1; //tm_mon is 0-based
date.tm_mday = day;
date.tm_year = year - 1900; //tm_year is years since 1900
}
return date;
}
//Check if the loan is overdue
bool Loan::isOverdue() const
{
time_t now = time(nullptr); //Get current time
tm dueDateTm = stringToDate(dueDate); //Convert due date to struct tm
if (difftime(mktime(&dueDateTm), now) < 0) //If current time is past the due date
{
return true;
}
return false;
}
int dateDifference(const string& date1, const string& date2) {
tm dateTm1 = stringToDate(date1);
tm dateTm2 = stringToDate(date2);
time_t time1 = mktime(&dateTm1);
time_t time2 = mktime(&dateTm2);
double secondsDifference = difftime(time2, time1);
int daysDifference = secondsDifference / (60 * 60 * 24); //Convert seconds to days
return daysDifference;
}
//Borrow a library item and create a loan
void Loan::borrowItem()
{
//Prompt user for Loan ID Number, Library Item ID Number. And Patron ID Number
cout << "\nEnter Loan ID Number: ";
cin >> loanID;
cout << "Enter Library Item ID Number: ";
cin >> itemID;
cout << "Enter Patron ID Number: ";
cin >> patronID;
cout << "Select Library Item Category (Book, AudioCD, DVD): ";
cin.ignore();
getline(cin, itemCategory);
cout << "Enter due date(MM-DD-YYYY): "; //Prompt user for Date using (MM-DD-YYYY) format
cin.ignore();
getline(cin, dueDate);
//Set loan date to the current date
time_t now = time(0);
tm *ltm = localtime(&now);
loanDate = to_string(1 + ltm->tm_mon) + "-" + to_string(ltm->tm_mday) + "-" + to_string(1900 + ltm->tm_year);
status = "Normal"; //Search for the Library Item by ID and check if it's available (Status = "Normal").
cout << "Loan has been added." << endl; //Add the Loan to the Loan collection
}
//Return a library item and update loan status
void Loan::returnItem()
{
//Prompted user for Loan ID Number
cout << "Returning Library Item with Loan ID " << loanID << "...\n";
status = "Returned";
cout << "Library Item returned successfully.\n"; //Remove the Loan from the Loan collection
}
//List overdue loans
void Loan::listOverdue() const
{
if (status == "Overdue" || isOverdue())
{
//Print Loan details with Library Item and Patron number if Loan status is "Overdue"
cout << "\nLoan Details(Overdue):" << endl;
cout << "Loan ID Number: " << loanID << endl;
cout << "Library Item ID Number: " << itemID << endl;
cout << "Patron ID Number: " << patronID << endl;
cout << "Library Item Category: " << itemCategory << endl;
//Include the Due Date
cout << "Due Date: " << dueDate << endl;
//Calculate how long the loan has been active
time_t now = time(nullptr);
tm nowTm = *localtime(&now);
string currentDate = to_string(nowTm.tm_mon + 1) + "-" + to_string(nowTm.tm_mday) + "-" + to_string(nowTm.tm_year + 1900);
int overdueDays = dateDifference(dueDate, currentDate);
//Include the Loan Active
cout << "Loan Active for: " << overdueDays << " days" << endl;
}
}
//Update the loan status based on the due date
void Loan::updateLoanStatus()
{
cout << "\nUpdating loan status for Loan ID " << loanID << endl;
//Check the current date against the Due Date
time_t now = time(nullptr);
tm dueDateTm = stringToDate(dueDate);
//If the current date exceeds the Due Date, update the current status to "Overdue."
if (difftime(mktime(&dueDateTm), now) < 0)
{
status = "Overdue";
cout << "Loan is overdue." << endl;
}
else
{
status = "Normal";
cout << "Loan status is normal." << endl;
}
}
//Extend the due date of a loan (recheck)
void Loan::recheckItem()
{
//Prompted user for Loan ID Number
cout << "Extending loan for Loan ID Number: " << loanID << endl;
cout << "Enter new due date(MM-DD-YYYY): "; //Prompt user for Date using (MM-DD-YYYY) format
cin.ignore();
getline(cin, dueDate);
cout << "Library Item has been rechecked." << endl;
}
//Edit an existing loan
void Loan::editLoan()
{
//Prompted user for Loan ID Number
cout << "\nEnter the new details for loan with Loan ID Number: " << loanID << endl;
cout << "Enter new due date(MM-DD-YYYY): "; //Prompt user for Date using (MM-DD-YYYY) format
cin.ignore();
getline(cin, dueDate);
cout << "Enter new status (Normal, Overdue, etc.): "; //Prompt user for new status (Due Date, Status, etc.)
getline(cin, status);
cout << "Loan has been edited and rechecked." << endl;
}
//Report a library item as lost
void Loan::reportLost()
{
cout << "Reporting Library Item with Loan ID " << loanID << " as lost...\n"; //Search for the Loan by ID Number
status = "Lost"; //If found, prompt the Library Item status as "Lost."
cout << "Library Item reported lost successfully.\n";
}
//Getters and Setters
int Loan::getLoanID() const
{
return loanID;
}
int Loan::getItemID() const
{
return itemID;
}
int Loan::getPatronID() const
{
return patronID;
}
string Loan::getItemCategory() const
{
return itemCategory;
}
string Loan::getDueDate() const
{
return dueDate;
}
string Loan::getStatus() const
{
return status;
}
void Loan::setLoanID(int loanID)
{
this->loanID = loanID;
}
void Loan::setItemID(int itemID)
{
this->itemID = itemID;
}
void Loan::setPatronID(int patronID)
{
this->patronID = patronID;
}
void Loan::setDueDate(string dueDate)
{
this->dueDate = dueDate;
}
void Loan::setStatus(string status)
{
this->status = status;
}