-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibrary Management System.cpp
More file actions
430 lines (388 loc) · 8.42 KB
/
Library Management System.cpp
File metadata and controls
430 lines (388 loc) · 8.42 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
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
#include<fstream>
#include<iostream>
#include<iomanip>
#include<string.h>
using namespace std;
class Library
{
int BookId;
char Title[20];
char Author[20];
char Category[20];
int Pages;
float Price;
public:
//Accessor Functions
int getID(){return BookId;}
char* getTitle(){return Title;}
char* getAuthor(){return Author;}
char* getCategory(){return Category;}
float getPrice(){return Price;}
//Default constructor
Library()
{
BookId=0;
strcpy(Title,"");
strcpy(Author,"");
strcpy(Category,"");
Pages=0;
Price=0;
}
void getBook();
void listView();
void showBook();
void header();
}l;
void Library::getBook()
{
cout<<"\tEnter Book Id : ";
cin>>BookId;
cout<<"\tEnter Book Title : ";
cin.get();
cin.getline(Title,20);
cout<<"\tEnter Book Author: ";
cin.getline(Author,20);
cout<<"\tEnter Book Category: ";
cin.getline(Category,20);
cout<<"\tEnter No. of Pages : ";
cin>>Pages;
cout<<"\tEnter Price of Book: ";
cin>>Price;
cout<<endl;
}
//For displaying the book details in individual form
void Library::showBook()
{
cout<<endl;
cout<<"Book ID : "<<BookId<<endl;
cout<<"Book Title : "<<Title<<endl;
cout<<"Author Name : "<<Author<<endl;
cout<<"Category : "<<Category<<endl;
cout<<"No. of Pages : "<<Pages<<endl;
cout<<"Price of Book: "<<Price<<endl;
}
//For displaying header for Display all function
void Library::header()
{
cout.setf(ios::left);
cout<<setw(5)<<"I.D."
<<setw(20)<<"Book Title"
<<setw(20)<<"Book Author"
<<setw(15)<<"Category"
<<setw(6)<<"Pages"
<<setw(6)<<"Price"<<endl;
for(int i=1;i<=72;i++)
cout<<"=";
cout<<endl;
}
//For displaying in the tabular form used in Display all function
void Library::listView()
{
cout.setf(ios::left);
cout<<setw(5)<<BookId
<<setw(20)<<Title
<<setw(20)<<Author
<<setw(15)<<Category
<<setw(6)<<Pages
<<setw(6)<<Price<<endl;
}
//Function prototyping for project
void drawLine(char);//Function for drawing line on screen.
void heading(); //FFunction for heading to be displayed on each page.
void menu(); //Function for displaying program options.
void searchMenu(); //Function for displaying searching options.
void addBook(); //Function for writing data to file.
void displayBooks();//FFunction for reading data from file.
//Function for searching data from file.
void searchByID();
void searchByTitle();
void searchByCategory();
void searchByPrice();
void searchByAuthor();
void dispose(); //FFunction to shift books from main file to other file.
void modify(); //Function to modify the book details.
void displayDisposed();//FFunction to display the list of disposed off books
/////////////////////////////////// Global Functions Definitions ///////////////////////////////////
//Function for drawing line on screen.
void drawLine(char ch)
{
for(int i=1;i<80;i++)
cout<<ch;
cout<<endl;
}
void heading()
{
drawLine('+');
cout<<"\t\tL I B R A R Y M A N A G E M E N T S Y S T E M\n";
drawLine('+');
}
//Function for adding new book in file.
void addBook()
{
ofstream fout;
fout.open("books.dat",ios::app);
l.getBook();
fout.write((char*)&l,sizeof(l));
cout<<"Book data saved in file...\n";
fout.close();
}
//Function for reading data from file.
void displayBooks()
{
ifstream fin("books.dat");
int rec=0;
while(fin.read((char*)&l,sizeof(l)))
{
if(rec<1)
l.header();
l.listView();
rec++;
}
fin.close();
cout<<"\nTotal "<<rec<<" Records in file...\n";
}
//Function for searching data from file.
void searchByID()
{
int n,flag=0;
ifstream fin("books.dat");
cout<<"Enter Book ID : ";
cin>>n;
while(fin.read((char*)&l,sizeof(l)))
{
if(n==l.getID())
{
l.showBook();
flag++;
}
}
fin.close();
if(flag==0)
cout<<"Book Number with ID:"<<n<<" not available...\n";
}
//Function for searching data from file.
void searchByTitle()
{
int flag=0;
char title[20];
ifstream fin("books.dat");
cout<<"Enter Book Title : ";
cin.ignore();
cin.getline(title,20);
while(fin.read((char*)&l,sizeof(l)))
{
if(strcmp(title,l.getTitle())==0)
{
l.showBook();
flag++;
}
}
fin.close();
if(flag==0)
cout<<"Book with Title: "<<title<<" not available...\n";
}
//Function for searching data from file.
void searchByCategory()
{
int flag=0,rec=0;
char cat[20];
ifstream fin("books.dat");
cout<<"Enter Book Category : ";
cin.ignore();
cin.getline(cat,20);
while(fin.read((char*)&l,sizeof(l)))
{
if(strcmp(cat,l.getCategory())==0)
{
if(rec<1)
l.header();
l.listView();
flag++;
rec++;
}
}
fin.close();
if(flag==0)
cout<<"Book with Category: "<<cat<<" not available...\n";
}
//Function for searching data from file.
void searchByAuthor()
{
int flag=0,rec=0;
char aut[20];
ifstream fin("books.dat");
cout<<"Enter Book Author : ";
cin.ignore();
cin.getline(aut,20);
while(fin.read((char*)&l,sizeof(l)))
{
if(strcmp(aut,l.getAuthor())==0)
{
if(rec<1)
l.header();
l.listView();
flag++;
rec++;
}
}
fin.close();
if(flag==0)
cout<<"Book with Author name: "<<aut<<" not available...\n";
}
//Function for searching data from file.
void searchByPrice()
{
int flag=0,rec=0;
float minrate, maxrate;
ifstream fin("books.dat");
cout<<"Enter Minimum Price of Book : ";
cin>>minrate;
cout<<"Enter Maximum Price of Book : ";
cin>>maxrate;
while(fin.read((char*)&l,sizeof(l)))
{
if(l.getPrice()>=minrate && l.getPrice()<=maxrate)
{
if(rec<1)
l.header();
l.listView();
flag++;
rec++;
}
}
fin.close();
if(flag==0)
cout<<"Books between Price Range: "<<minrate
<<" and "<<maxrate<<" not available...\n";
}
//Function for shifting books from main file to dispose file.
void dispose()
{
int n,flag=0;
ifstream fin("books.dat");
ofstream fout("dispose.dat",ios::out);
cout<<"Enter Book ID : ";
cin>>n;
while(fin.read((char*)&l,sizeof(l)))
{
if(n==l.getID())
{
l.showBook();
flag++;
}
else
{
fout.write((char*)&l,sizeof(l));
}
}
fin.close();
fout.close();
if(flag==0)
cout<<"Book Number with ID:"<<n<<" not available...\n";
}
//Function for modifying data in file.
void modify()
{
int n,flag=0,pos;
fstream fin("books.dat",ios::in|ios::out);
cout<<"Enter Book ID : ";
cin>>n;
while(fin.read((char*)&l,sizeof(l)))
{
if(n==l.getID())
{
pos=fin.tellg();
cout<<"Following data will be edited...\n";
l.showBook();
flag++;
fin.seekg(pos-sizeof(l));
l.getBook();
fin.write((char*)&l,sizeof(l));
cout<<"\nData Modified successfully...\n";
}
}
fin.close();
if(flag==0)
cout<<"Book Number with ID:"<<n<<" not available...\n";
}
//Function to display the list of disposed off books
void displayDisposed()
{
ifstream fin("dispose.dat");
int rec=0;
while(fin.read((char*)&l,sizeof(l)))
{
if(rec<1)
l.header();
l.listView();
rec++;
}
fin.close();
cout<<"\nTotal "<<rec<<" Records in disposed off file...\n";
}
//Functionfor displaying program options.
void menu()
{
int ch;
do
{
system("cls");
heading();
cout<<"0. EXIT.\n";
cout<<"1. Add New Book\n";
cout<<"2. Display All Books\n";
cout<<"3. Search Books\n";
cout<<"4. Disposed Off Books\n";
cout<<"5. Modify Details\n";
cout<<"6. List of Disposed Books\n";
cout<<"Enter Your Choice : ";
cin>>ch;
system("cls");
heading();
switch(ch)
{
case 1: addBook(); break;
case 2: displayBooks(); break;
case 3: searchMenu(); break;
case 4: dispose(); break;
case 5: modify(); break;
case 6: displayDisposed(); break;
}
system("pause");
}while(ch!=0);
}
void searchMenu()
{
int ch;
do
{
system("cls");
heading();
cout<<"BOOK SEARCH OPTIONS\n";
cout<<"0. Back\n";
cout<<"1. By ID\n";
cout<<"2. By Title\n";
cout<<"3. By Category\n";
cout<<"4. By Author\n";
cout<<"5. By Price Range\n";
cout<<"Enter Your Choice : ";
cin>>ch;
system("cls");
heading();
switch(ch)
{
case 1: searchByID(); break;
case 2: searchByTitle(); break;
case 3: searchByCategory(); break;
case 4: ;searchByAuthor(); break;
case 5: searchByPrice();break;
default: cout<<"\a";
}
system("pause");
}while(ch!=0);
}
int main()
{
menu();
return 0;
}