-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibrary Management System
More file actions
218 lines (218 loc) · 5.92 KB
/
Library Management System
File metadata and controls
218 lines (218 loc) · 5.92 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
#include <bits/stdc++.h>
using namespace std;
class book
{
string title, author;
int id;
bool is_borrowed;
public:
book(string t, string a, int i) : title(t), author(a), id(i), is_borrowed(0) {}
int getId() { return id; }
bool getStatus() { return is_borrowed; }
void borrowBook() { is_borrowed = true; }
void returnBook() { is_borrowed = false; }
void print()
{
cout << "=============================\n";
cout << "Book ID : " << id << "\n";
cout << "Title : " << title << "\n";
cout << "Author : " << author << "\n";
cout << "Status : " << (is_borrowed ? "Borrowed" : "Available") << "\n";
cout << "=============================\n\n";
}
};
// --------------------------------------------------------
class member
{
string name;
int id;
vector<int> borrowed_books;
public:
member(string s, int i) : name(s), id(i) {}
int getId() { return id; }
void borrowByMember(int bid)
{
borrowed_books.push_back(bid);
cout << name << " borrowed book (ID: " << bid << ")\n";
}
void returnBook(int bid)
{
for (int i = 0; i < borrowed_books.size(); i++)
{
if (borrowed_books[i] == bid)
{
borrowed_books.erase(borrowed_books.begin() + i);
cout << name << " returned book (ID: " << bid << ")\n";
return;
}
}
cout << name << " did not borrow book (ID: " << bid << ")\n";
}
void print()
{
cout << "=============================\n";
cout << "Member ID : " << id << "\n";
cout << "Name : " << name << "\n";
cout << "Borrowed : ";
if (borrowed_books.empty())
cout << "None";
else
{
for (auto b : borrowed_books)
cout << b << " ";
}
cout << "\n=============================\n\n";
}
};
// --------------------------------------------------------
class library
{
vector<book> books;
vector<member> members;
public:
void add_book(int i, string s, string a)
{
books.push_back(book(s, a, i));
cout << "Book \"" << s << "\" was added successfully.\n";
}
void add_member(int i, string s)
{
members.push_back(member(s, i));
cout << "Member \"" << s << "\" was added successfully.\n";
}
void borrow_book(int mem, int bo)
{
for (auto &boo : books)
{
if (boo.getId() == bo)
{
if (boo.getStatus())
{
cout << "Book (ID: " << bo << ") is already borrowed.\n";
return;
}
for (auto &memo : members)
{
if (memo.getId() == mem)
{
memo.borrowByMember(bo);
boo.borrowBook();
return;
}
}
cout << "Member not found.\n";
return;
}
}
cout << "Book not found.\n";
}
void ret_book(int mem, int bo)
{
for (auto &boo : books)
{
if (boo.getId() == bo)
{
for (auto &memo : members)
{
if (memo.getId() == mem)
{
memo.returnBook(bo);
boo.returnBook();
return;
}
}
cout << "Member not found.\n";
return;
}
}
cout << "Book not found.\n";
}
void printBooks()
{
cout << "\n===== All Books =====\n";
for (auto &b : books) b.print();
}
void printMembers()
{
cout << "\n===== All Members =====\n";
for (auto &m : members) m.print();
}
};
// --------------------------------------------------------
signed main()
{
library lib;
int choice;
while (true)
{
cout << "\n========= Library Menu =========\n";
cout << "1) Add Book\n";
cout << "2) Add Member\n";
cout << "3) Borrow Book\n";
cout << "4) Return Book\n";
cout << "5) Print All Books\n";
cout << "6) Print All Members\n";
cout << "7) Exit\n";
cout << "Please choose an option: ";
cin >> choice;
if (choice == 1)
{
int id;
string title, author;
cout << "Enter Book ID: ";
cin >> id;
cin.ignore();
cout << "Enter Book Title: ";
getline(cin, title);
cout << "Enter Book Author: ";
getline(cin, author);
lib.add_book(id, title, author);
}
else if (choice == 2)
{
int id;
string name;
cin.ignore();
cout << "Enter Member Name: ";
getline(cin, name);
cout << "Enter Member ID: ";
cin >> id;
lib.add_member(id, name);
}
else if (choice == 3)
{
int memid, boid;
cout << "Enter Member ID: ";
cin >> memid;
cout << "Enter Book ID: ";
cin >> boid;
lib.borrow_book(memid, boid);
}
else if (choice == 4)
{
int memid, boid;
cout << "Enter Member ID: ";
cin >> memid;
cout << "Enter Book ID: ";
cin >> boid;
lib.ret_book(memid, boid);
}
else if (choice == 5)
{
lib.printBooks();
}
else if (choice == 6)
{
lib.printMembers();
}
else if (choice == 7)
{
cout << "Exiting program...\n";
break;
}
else
{
cout << "Invalid choice, please try again.\n";
}
}
}