-
Notifications
You must be signed in to change notification settings - Fork 1
/
func_book.h
411 lines (345 loc) · 8.5 KB
/
func_book.h
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
void LIB::add_book()
{
cout<<"******************************************************"<<endl;
cout<<"Adding new Book to Library.\n";
booknode * tmp;
tmp = new booknode;
if(!tmp == 0)
{
tmp->borrowers=NULL;
cout<<"Enter Book Name: ";
getline(cin,tmp->name);
cout<<"Enter Author Name: ";
getline(cin,tmp->author);
cout<<"Enter Price of Book: ";
cin>>tmp->price;
cout<<"Enter Book Quantity: ";
cin>>tmp->quantity;
tmp->remaining=tmp->quantity;
cout<<"Enter UniqueID of Book:";
cin>>tmp->id;
cin.ignore(1,'\n');
//adding node to begining
tmp->next = book_start;
book_start=tmp;
book_count++;
}
else
{
cout<<"Memory limit exceded";
}
}
void LIB::remove_book()
{
if(book_start==NULL)
{
cout<<"No Book to delete\n";
return;
}
int id;
booknode * tmp, * prev_tmp;
cout<<"Enter Book ID to delete.";
cin>>id;
tmp = book_start;
do
{
prev_tmp=tmp;
if(tmp->next==NULL)
{
cout<<"Book not found.\n";
return;
}
else
tmp=tmp->next;
} while (tmp->id!=id && tmp != NULL);
if(tmp->remaining!=tmp->quantity)
{
cout<<"Cannot delete book, all books are not reterned.\n";
return;
}
// actual deleting part
prev_tmp->next = tmp->next;
delete tmp;
book_count--;
}
void LIB::edit_book()
{
cout<<"***********************************************"<<endl;
cout<<"Edit book from Library.\n";
booknode * tmp;
int id,ch_eb,add_quntity;
cout<<"Enter Book ID to be edited: "<<endl;
cin>>id;
tmp = book_start;
while (tmp!=NULL && tmp->id!=id)
{
tmp = tmp->next;
}
if (tmp==NULL)
{
cout<<"*Book not Found*";
return;
}
do
{
system("CLS");
cout<<"Book ID: "<<tmp->id<<endl;
cout<<"Book Name: "<<tmp->name<<"\n";
cout<<"Author Name: "<<tmp->author<<"\n";
cout<<"Book Price: "<<tmp->price<<"\n" ;
cout<<"Quantity of Books: "<<tmp->quantity<<"\n";
cout<<"Remaining Books: "<<tmp->remaining<<"\n" ;
cout<<"*****************************************************"<<endl;
cout<<"\t1.Edit Name of Book\n";
cout<<"\t2.Edit Author of Book\n";
cout<<"\t3.Edit Price of Book\n";
cout<<"\t4.Edit Quantity of Book\n";
cout<<"\t0.Exit\n";
cout<<"\n Enter your Choice : ";
cin>>ch_eb;
cin.ignore(1,'\n');
switch (ch_eb)
{
case 0:
cout<<"Exited Edit menu"<<endl;
break;
case 1:
cout<<"Enter Name of Book : ";
getline(cin,tmp->name);
break;
case 2:
cout<<"Enter Name of Author: ";
getline(cin,tmp->author);
break;
case 3:
cout<<"Enter Price of Book: ";
cin>>tmp->price;
break;
case 4:
cout<<"Enter quantity of Books to add(use -quantity to remove): ";
cin>>add_quntity;
tmp->quantity += add_quntity ;
break;
default:
cout<<"*Wrong Choice*\n";
break;
}
} while (ch_eb!=0);
}
void LIB::lend_book()
{
cout<<"*************************************************"<<endl;
cout<<"Lend a Book from Library.\n";
int bid,sid;
booknode * btmp;
studentnode * stmp;
btmp = book_start;
stmp = stud_start;
cout<<"Enter Book ID to lend: ";
cin>>bid;
while(btmp != NULL && btmp->id!=bid)
{
btmp=btmp->next;
}
if(btmp==NULL)
{
cout<<"Book not found";
return;
}
//check if books available
if(btmp->remaining==0)
{
cout<<"Copy of Book is not avilable.";
return;
}
//check if student have book quata remaining
cout<<"Enter student ID to lend: ";
cin>>sid;
while(stmp != NULL && stmp->id!=sid)
{
stmp=stmp->next;
}
if(stmp==NULL)
{
cout<<"Student not found\n";
return;
}
int count;
count = 0;
borrowed * count_node, * b_node;
count_node = stmp->borrowedbooks;
while(count_node!=NULL)
{
count++;
count_node=count_node->next;
}
if(count>2)
{
cout<<"Borrow limit reached, Student have already borrowed 3 Books.\n";
return;
}
//borrow book
b_node = new borrowed;
b_node->id = sid;
b_node->next = NULL;
if(btmp->borrowers == NULL)
{
btmp->borrowers = b_node;
}
else
{
b_node->next = btmp->borrowers;
btmp->borrowers = b_node;
}
count_node = new borrowed;
count_node->id = bid;
count_node->next = NULL;
if(stmp->borrowedbooks == NULL)
{
stmp->borrowedbooks = count_node;
}
else
{
count_node->next = stmp->borrowedbooks;
stmp->borrowedbooks = count_node;
}
btmp->remaining--;
cout<<"Book issued "<<endl;
cout<<"-------------------"<<endl;
}
void LIB::return_book()
{
cout<<"**************************************************"<<endl;
cout<<"Return Book to Library.\n";
int sid,bid;
cout<<"Enter Student ID: ";
cin>>sid;
studentnode * stmp;
stmp=stud_start;
while(stmp != NULL && stmp->id!=sid)
{
stmp=stmp->next;
}
if(stmp==NULL)
{
cout<<"Student not found";
return;
}
cout<<"Enter Book : ";
cout<<"Enter Book ID: ";
cin>>bid;
borrowed * count_node, * prev_node;
count_node = stmp->borrowedbooks;
prev_node = NULL;
while(count_node!=NULL && count_node->id!=bid)
{
prev_node=count_node;
count_node->next=count_node;
}
if(count_node==NULL)
{
cout<<"Student have not borrowed this book.";
return;
}
//actual return
if(prev_node==NULL)
{
delete stmp->borrowedbooks;
stmp->borrowedbooks=NULL;
}
else
{
prev_node->next = count_node->next;
delete count_node;
}
booknode * btmp;
btmp = book_start;
while(btmp!=NULL && btmp->id!=bid)
{
btmp= btmp->next;
}
count_node = btmp->borrowers;
prev_node = NULL;
while(count_node!=NULL && count_node->id!=sid)
{
prev_node=count_node;
count_node->next=count_node;
}
if(count_node==NULL)
{
cout<<"Error finding borrower";
return;
}
if(prev_node==NULL)
{
delete btmp->borrowers;
btmp->borrowers=NULL;
}
else
{
prev_node->next = count_node->next;
delete count_node;
}
btmp->remaining++;
cout<<"Book returned successfully \n";
cout<<"------------------------"<<endl;
}
void LIB::show_all_books()
{
if(book_start==NULL)
{
cout<<"No Book to display";
}
else
{
booknode * tmp;
tmp = book_start;
while(tmp!=NULL)
{
cout<<"Book ID: "<<tmp->id<<endl;
cout<<"Book Name: "<<tmp->name<<"\n";
cout<<"Author Name: "<<tmp->author<<"\n";
cout<<"Book Price: "<<tmp->price<<"\n" ;
cout<<"Quantity of Books: "<<tmp->quantity<<"\n";
cout<<"Remaining Books: "<<tmp->remaining<<"\n" ;
tmp=tmp->next;
cout<<"************************************************************"<<endl;
}
}
}
void LIB::book_details()
{
int id;
cout<<"Enter Book ID: ";
cin>>id;
booknode * tmp;
tmp = book_start;
while(tmp!=NULL && tmp->id!=id)
{
tmp=tmp->next;
}
if(tmp==NULL)
{
cout<<"Book not found";
return;
}
cout<<"Id of Book: "<<tmp->id<<endl;
cout<<"Name of Book: "<<tmp->name<<endl;
cout<<"Name of Author: "<<tmp->author<<endl;
cout<<"Price of Book: "<<tmp->author<<endl;
cout<<"Total Quantity of Book: "<<tmp->quantity<<endl;
cout<<"Remaining Quantity of Book: "<<tmp->remaining<<endl;
if(tmp->borrowers==NULL)
{
cout<<"No students have borrowed this book.";
return;
}
cout<<"Borrowers ID's : ";
borrowed * tmp1;
tmp1 = tmp->borrowers;
while(tmp1!=NULL)
{
cout<<tmp1->id<<" ";
tmp1=tmp1->next;
}
}