-
Notifications
You must be signed in to change notification settings - Fork 0
/
LibrarySystem.cpp
262 lines (231 loc) · 7.47 KB
/
LibrarySystem.cpp
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
#include <bits/stdc++.h>
using namespace std;
struct Book {
int id;
string name;
int quantity;
int borrowed_quantity;
Book() {
id = 0;
quantity = 0;
borrowed_quantity = 0;
}
};
struct User {
int id;
string name;
User() {
id = 0;
}
};
struct Library {
Book books[100];
int book_index = 0;
User users[100];
int user_index = 0;
string result[100];
string users_borrowed[100][100]{ "\0" };
int borrowed_book[100]{ 0 };
int B_inx = 0;
int U_inx = 0;
bool static compare_name(Book& a, Book& b) {
return a.name < b.name;
}
bool static compare_id(Book& a, Book& b) {
return a.id < b.id;
}
void add_book() {
int id;
string name;
int quantity;
cout << "Enter Book info: ID & Name & Total quantity: ";
cin >> id >> name >> quantity;
books[book_index].id = id;
books[book_index].name = name;
books[book_index].quantity = quantity;
book_index++;
}
void add_user() {
int id;
string name;
cout << "Enter User Name & National id: ";
cin >> name >> id;
users[user_index].id = id;
users[user_index].name = name;
user_index++;
}
void prefix_search() {
string prefix;
cout << "Enter Book name prefix: ";
cin >> prefix;
for (int i = 0; i < book_index; i++) {
if (books[i].name.find(prefix) == 0) {
cout << books[i].name << endl;
}
}
}
void print_library_by_id() {
sort(books, books + book_index, compare_id);
int j = 0;
for (int i = 0; i < book_index; i++) {
cout << "id = " << books[i].id << " , name : " << books[i].name
<< " , total quantity = " << books[i].quantity << " , total borrowed = " << books[i].borrowed_quantity << endl;
}
}
void sort_library_by_name() {
sort(books, books + book_index, compare_name);
int j = 0;
for (int i = 0; i < book_index; i++) {
cout << "id = " << books[i].id << " , name : " << books[i].name
<< " , total quantity = " << books[i].quantity << " , total borrowed = " << books[i].borrowed_quantity << endl;
}
}
void borrow_book() {
string _name;
string _book;
cout << "Enter user name and book name : ";
cin >> _name >> _book;
for (int i = 0; i < book_index; i++) {
if (books[i].name == _book && books[i].quantity == 0) {
cout << "There isn't no book for now!\n";
break;
}
else if (books[i].name == _book && books[i].quantity > 0) {
borrowed_book[B_inx] = i;
B_inx++;
books[i].borrowed_quantity++;
books[i].quantity--;
for (int j = 0; j < user_index; j++) {
if (users[j].name == _name) {
users_borrowed[borrowed_book[U_inx]][U_inx] = _name;
U_inx++;
}
}
}
}
}
void print_who_borrowed_book() {
string book;
cout << "Enter Book name : ";
cin >> book;
for (int i = 0; i < book_index; i++) {
if (books[i].name == book) {
for (int j = 0; j < U_inx; j++) {
if (users_borrowed[i][j] != users_borrowed[i][j + 1])
cout << users_borrowed[i][j] << endl;
}
break;
}
}
}
void return_book() {
string _name;
string _book;
cout << "Enter user name and book name : ";
cin >> _name >> _book;
int rb = 0;
for (; rb < book_index; rb++) {
if (books[rb].name == _book) {
B_inx--;
books[rb].quantity++;
books[rb].borrowed_quantity--;
}
}
string temp[100][100];
int inx = 0;
for (int j = 0; j < borrowed_book[B_inx]; j++) {
if (j != rb) {
for (int k = 0; k < U_inx; k++) {
temp[inx][j] = users_borrowed[j][k];
}
++inx;
}
}
for (int i = 0; i < borrowed_book[B_inx] - 1; ++i) {
for (int j = 0; j < U_inx; ++j) {
users_borrowed[i][j] = temp[i][j];
}
}
}
void print_users() {
for (int i = 0; i < user_index; i++) {
cout << "user : " << users[i].name << " has id : " << users[i].id << " and he borrowed books ids : ";
for (int j = 0; j < user_index; j++) {
for (int k = 0; k < U_inx; k++) {
if (users_borrowed[j][k] == users[i].name) {
cout << books[j].id << " ";
}
}
}
cout << endl;
}
}
};
struct LibrarySystem {
void menu() {
cout << "\n\n";
cout << setw(37) << left << "" << "==============================================\n";
cout << setw(37) << left << "" << "\t\tLibrary menu : \n\n";
cout << setw(37) << left << "" << "\t\t1) Add Book.\n";
cout << setw(37) << left << "" << "\t\t2) Search Book by predix.\n";
cout << setw(37) << left << "" << "\t\t3) Print who borrowed book by name.\n";
cout << setw(37) << left << "" << "\t\t4) Print libray by id.\n";
cout << setw(37) << left << "" << "\t\t5) Print libray by name.\n";
cout << setw(37) << left << "" << "\t\t6) Add User.\n";
cout << setw(37) << left << "" << "\t\t7) User borrow book.\n";
cout << setw(37) << left << "" << "\t\t8) User retunr book.\n";
cout << setw(37) << left << "" << "\t\t9) Print Users.\n";
cout << setw(37) << left << "" << "\t\t10) Exit.\n";
cout << setw(37) << left << "" << "==============================================\n";
}
void run() {
Library library;
while (true) {
menu();
int op;
cout << "\nEnter your menu choice [1 : 10]: ";
cin >> op;
switch (op) {
case 1:
library.add_book();
break;
case 2:
library.prefix_search();
break;
case 3:
library.print_who_borrowed_book();
break;
case 4:
library.print_library_by_id();
break;
case 5:
library.sort_library_by_name();
break;
case 6:
library.add_user();
break;
case 7:
library.borrow_book();
break;
case 8:
library.return_book();
break;
case 9:
library.print_users();
break;
case 10:
exit(0);
break;
default:
cout << "Invalid Choice! Try again.\n";
break;
break;
}
}
}
};
int main()
{
LibrarySystem library;
library.run();
}