forked from Immortal-ibr/Library_System
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoan.cpp
More file actions
135 lines (112 loc) · 3.56 KB
/
Loan.cpp
File metadata and controls
135 lines (112 loc) · 3.56 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
//
// Created by tarek on 4/5/2024.
//
#include "Loan.h"
// Static list of all loans
std::vector<Loan> Loan::Loans_List={};
// Default constructor
Loan::Loan(): memberID(0),bookID(""), dueDate(0,0,0), borrowDate(0,0,0){}
// Constructor with member ID, book ID, due date, and borrow date
Loan::Loan(int m, const Custom_String_Class& b, Date due, Date borrow): memberID(m), bookID(b), dueDate(due), borrowDate(borrow) {}
// Constructor with member ID, book ID, and due date. Borrow date is set to current date.
Loan::Loan(int m, const Custom_String_Class& b, Date d): memberID(m), bookID(b), dueDate(d) {
borrowDate=Date::getCrrentDate();
}
// Constructor with member ID and book ID. Due date and borrow date are set to 0.
Loan::Loan(int m, const Custom_String_Class& b): memberID(m), bookID(b), dueDate(0,0,0), borrowDate(0,0,0) {}
// Getter for book ID
Custom_String_Class Loan::getBookID() const{
return bookID;
}
// Getter for member ID
int Loan::getMemberID() const{
return memberID;
}
// Getter for due date
Date Loan::getDueDate() const{
return dueDate;
}
// Getter for borrow date
Date Loan::getBorrowDate() const{
return borrowDate;
}
// Getter for list of loans
vector<Loan>& Loan::getLoans_List(){
return Loans_List;
}
// Setter for member ID
void Loan::setMemberID(int m) {
memberID = m;
}
// Setter for book ID
void Loan::setBookID(const Custom_String_Class& b) {
bookID = b;
}
// Setter for due date
void Loan::setDueDate(Date d) {
dueDate = d;
}
// Setter for borrow date
void Loan::setBorrowDate(Date r) {
borrowDate = r;
}
// Function to calculate fines
int Loan::calculateFines(){
if(dueDate<Date::getCrrentDate()){
int days=Date::getCrrentDate()-dueDate;
return days*2;
}
else {
return 0;
}
}
// Function to display details of the loan
void Loan::displayDetails(){
std::cout << "memberID " << memberID <<endl;
std::cout << "bookID " << bookID <<endl;
std::cout << "due date " <<dueDate.getDate() <<endl;
std::cout << "borrow date " <<borrowDate.getDate() <<endl;
}
// Function to load loans from a file
void Loan::loadLoans() {
Loans_List.clear();
std::ifstream file("loaned.json");
if (!file.is_open()) {
std::cerr << "Failed to open file." << std::endl;
return;
}
json j;
file >> j;
for (const auto& loan_json : j) {
Loan ln(loan_json["memberID"], loan_json["bookID"].get<string>(), Custom_String_Class(loan_json["dueDate"].get<string>()));
ln.setBorrowDate(Custom_String_Class(loan_json["borrowDate"].get<string>()));
Loan::Loans_List.push_back(ln);
}
file.close();
}
// Function to save loans to a file
void Loan::saveLoans() {
std::ofstream file("loaned.json");
if (!file.is_open()) {
std::cerr << "Failed to open file." << endl;
return;
}
json OUTPUT;
for(const auto& loan : Loan::Loans_List)
{
json loanJSON;
loanJSON["memberID"]=loan.memberID;
loanJSON["bookID"] =loan.bookID.getSTR();
loanJSON["dueDate"] =loan.dueDate.getDate().getSTR();
loanJSON["borrowDate"] =loan.borrowDate.getDate().getSTR();
OUTPUT.push_back(loanJSON);
}
file<<std::setw(4)<<OUTPUT<<std::endl; // setw(4) sets the width of the output field to 4
file.close();
}
// Function to display the list of loans
void Loan::displaylist() {
for (const auto &it: (Loan::Loans_List)) {
cout<<it.bookID<<endl;
}
}