-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
475 lines (474 loc) · 13.8 KB
/
main.c
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
#include <stdio.h>
#include <time.h>
#include <string.h>
#define MAX_YR 2050
#define MIN_YR 1950
#define MAX_SIZE_USER_NAME 20
#define MAX_SIZE_PASSWORD 20
#define FILE_NAME "A7 Lib.bin"
#define MAX_BOOK_NAME 50
#define MAX_AUTHOR_NAME 50
#define MAX_STUDENT_NAME 50
#define FILE_HEADER_SIZE sizeof(sFileHeader)
//structure to store date
typedef struct
{
int yyyy;
int mm;
int dd;
} Date;
typedef struct
{
char username[MAX_SIZE_USER_NAME];
char password[MAX_SIZE_PASSWORD];
} sFileHeader;
typedef struct// to call in program
{
int books_id; // declare the integer data type
char bookName[MAX_BOOK_NAME];// declare the character data type
char authorName[MAX_AUTHOR_NAME];// declare the character data type
char studentName[MAX_STUDENT_NAME];// declare the character data type
Date bookIssueDate;// declare the integer data type
} s_BooksInfo;
void printMessageCenter(char* message)
{
//print message
printf("\t\t\t\t\t\t\t%s",message);
}
void headMessage(char *message)
{
system("cls");
printf("\t\t\t###########################################################################");
printf("\n\t\t\t############ ############");
printf("\n\t\t\t############ LIBARARY MANAGMENT SYSTEM ############");
printf("\n\t\t\t############ ############");
printf("\n\t\t\t###########################################################################");
printf("\n\t\t\t---------------------------------------------------------------------------\n");
printMessageCenter(message);
printf("\n\t\t\t----------------------------------------------------------------------------");
}
int isNameValid(char *name)
{
int validName = 1;
int len = 0;
int index = 0;
len = strlen(name);
for(index =0; index <len ; ++index)
{
if(!(isalpha(name[index])) && (name[index] != '\n') && (name[index] != ' '))
{
validName = 0;
break;
}
}
return validName;
}
// Function to check leap year.
//Function returns 1 if leap year
int IsLeapYear(int year)
{
return (year % 4 == 0);
}
// returns 1 if given date is valid.
int isValidDate(Date *validDate)
{
//check range of year,month and day
if (validDate->yyyy > MAX_YR || validDate->yyyy < MIN_YR)
return 0;
if (validDate->mm < 1 || validDate->mm > 12)
return 0;
if (validDate->dd < 1 || validDate->dd > 31)
return 0;
//Handle feb days in leap year
if (validDate->mm == 2)
{
if (IsLeapYear(validDate->yyyy))
return (validDate->dd <= 29);
else
return (validDate->dd <= 28);
}
//handle months which has only 30 days
if (validDate->mm == 4 || validDate->mm == 6 ||
validDate->mm == 9 || validDate->mm == 11)
return (validDate->dd <= 30);
return 1;
}
// Add books in list
void addBookInDataBase()
{
int days;
s_BooksInfo addBookInfoInDataBase = {0};
FILE *fp = NULL;
int status = 0;
fp = fopen(FILE_NAME,"ab+");
if(fp == NULL)
{
printf("File is not opened\n");
exit(1);
}
headMessage("ADD NEW BOOKS");
printf("\n\n\t\t\tENTER YOUR DETAILS BELOW:");
printf("\n\t\t\t---------------------------------------------------------------------------\n");
printf("\n\t\t\tBook ID NO = ");
fflush(stdin);
scanf("%d",&addBookInfoInDataBase.books_id);
do
{
printf("\n\t\t\tBook Name = ");
fflush(stdin);
fgets(addBookInfoInDataBase.bookName,MAX_BOOK_NAME,stdin);
status = isNameValid(addBookInfoInDataBase.bookName);
if (!status)
{
printf("\n\t\t\tName contain invalid character. Please enter again.");
}
}
while(!status);
do
{
printf("\n\t\t\tAuthor Name = ");
fflush(stdin);
fgets(addBookInfoInDataBase.authorName,MAX_AUTHOR_NAME,stdin);
status = isNameValid(addBookInfoInDataBase.authorName);
if (!status)
{
printf("\n\t\t\tName contain invalid character. Please enter again.");
}
}
while(!status);
do
{
printf("\n\t\t\tStudent Name = ");
fflush(stdin);
fgets(addBookInfoInDataBase.studentName,MAX_STUDENT_NAME,stdin);
status = isNameValid(addBookInfoInDataBase.studentName);
if (!status)
{
printf("\n\t\t\tName contain invalid character. Please enter again.");
}
}
while(!status);
do
{
//get date year,month and day from user
printf("\n\t\t\tEnter date in format (day/month/year): ");
scanf("%d/%d/%d",&addBookInfoInDataBase.bookIssueDate.dd,&addBookInfoInDataBase.bookIssueDate.mm,
&addBookInfoInDataBase.bookIssueDate.yyyy);
//check date validity
status = isValidDate(&addBookInfoInDataBase.bookIssueDate);
if (!status)
{
printf("\n\t\t\tPlease enter a valid date.\n");
}
}
while(!status);
fwrite(&addBookInfoInDataBase,sizeof(addBookInfoInDataBase), 1, fp);
fclose(fp);
}
// search books
void searchBooks()
{
int found = 0;
char bookName[MAX_BOOK_NAME] = {0};
s_BooksInfo addBookInfoInDataBase = {0};
FILE *fp = NULL;
int status = 0;
fp = fopen(FILE_NAME,"rb");
if(fp == NULL)
{
printf("\n\t\t\tFile is not opened\n");
exit(1);
}
headMessage("SEARCH BOOKS");
//put the control on books detail
if (fseek(fp,FILE_HEADER_SIZE,SEEK_SET) != 0)
{
fclose(fp);
printf("\n\t\t\tFacing issue while reading file\n");
exit(1);
}
printf("\n\n\t\t\tEnter Book Name to search:");
fflush(stdin);
fgets(bookName,MAX_BOOK_NAME,stdin);
while (fread (&addBookInfoInDataBase, sizeof(addBookInfoInDataBase), 1, fp))
{
if(!strcmp(addBookInfoInDataBase.bookName, bookName))
{
found = 1;
break;
}
}
if(found)
{
printf("\n\t\t\tBook id = %d\n",addBookInfoInDataBase.books_id);
printf("\t\t\tBook name = %s",addBookInfoInDataBase.bookName);
printf("\t\t\tBook authorName = %s",addBookInfoInDataBase.authorName);
printf("\t\t\tStudent Name = %s",addBookInfoInDataBase.studentName);
printf("\t\t\tBook issue date(day/month/year) = (%d/%d/%d)",addBookInfoInDataBase.bookIssueDate.dd,
addBookInfoInDataBase.bookIssueDate.mm, addBookInfoInDataBase.bookIssueDate.yyyy);
}
else{
printf("\n\t\t\tNo Record");
}
fclose(fp);
printf("\n\n\n\t\t\tPress any key to go to main menu.....");
getch();
}
// v books function
void viewBooks(){
int found = 0;
char bookName[MAX_BOOK_NAME] = {0};
s_BooksInfo addBookInfoInDataBase = {0};
FILE *fp = NULL;
int status = 0;
int countBook = 1;
headMessage("VIEW BOOKS DETAILS");
fp = fopen(FILE_NAME,"rb");
if(fp == NULL)
{
printf("\n\n\n\t\t\t\t\t\t\tFile is not opened\n\n\n");
exit(1);
}
if (fseek(fp,FILE_HEADER_SIZE,SEEK_SET) != 0)
{
fclose(fp);
printf("Facing issue while reading file\n");
exit(1);
}
while (fread (&addBookInfoInDataBase, sizeof(addBookInfoInDataBase), 1, fp))
{
printf("\n\t\t\tBook Count = %d\n\n",countBook);
printf("\t\t\tBook id = %d",addBookInfoInDataBase.books_id);
printf("\n\t\t\tBook name = %s",addBookInfoInDataBase.bookName);
printf("\t\t\tBook authorName = %s",addBookInfoInDataBase.authorName);
printf("\t\t\tStudent Name = %s",addBookInfoInDataBase.studentName);
printf("\t\t\tBook issue date(day/month/year) = (%d/%d/%d)\n\n",addBookInfoInDataBase.bookIssueDate.dd,
addBookInfoInDataBase.bookIssueDate.mm, addBookInfoInDataBase.bookIssueDate.yyyy);
found = 1;
++countBook;
}
fclose(fp);
if(!found)
{
printf("\n\t\t\tNo Record");
}
printf("\n\n\t\t\tPress any key to go to main menu.....");
fflush(stdin);
getch();
}
// delete function
void deleteBooks()
{
int found = 0;
int bookDelete = 0;
sFileHeader fileHeaderInfo = {0};
char bookName[MAX_BOOK_NAME] = {0};
s_BooksInfo addBookInfoInDataBase = {0};
FILE *fp = NULL;
FILE *tmpFp = NULL;
int status = 0;
headMessage("Delete Books Details");
fp = fopen(FILE_NAME,"rb");
if(fp == NULL)
{
printf("\n\n\n\t\t\t\t\t\t\tFile is not opened\n\n\n");
exit(1);
}
tmpFp = fopen("tmp.bin","wb");
if(tmpFp == NULL)
{
fclose(fp);
printf("\n\n\n\t\t\t\t\t\t\tFile is not opened\n\n\n");
exit(1);
}
fread (&fileHeaderInfo,FILE_HEADER_SIZE, 1, fp);
fwrite(&fileHeaderInfo,FILE_HEADER_SIZE, 1, tmpFp);
printf("\n\t\t\tEnter Book ID NO. for delete:");
scanf("%d",&bookDelete);
while (fread (&addBookInfoInDataBase, sizeof(addBookInfoInDataBase), 1, fp))
{
if(addBookInfoInDataBase.books_id != bookDelete)
{
fwrite(&addBookInfoInDataBase,sizeof(addBookInfoInDataBase), 1, tmpFp);
}
else
{
found = 1;
}
}
(found)? printf("\n\t\t\tRecord deleted successfully....."):printf("\n\t\t\tRecord not found");
getch();
fclose(fp);
fclose(tmpFp);
remove(FILE_NAME);
rename("tmp.bin",FILE_NAME);
}
void updateCredential()
{
sFileHeader fileHeaderInfo = {0};
FILE *fp = NULL;
char userName[MAX_SIZE_USER_NAME] = {0};
char password[MAX_SIZE_PASSWORD] = {0};
headMessage("Update Credential");
fp = fopen(FILE_NAME,"rb+");
if(fp == NULL)
{
printf("File is not opened\n");
exit(1);
}
printf("\n\n\t\t\tNew Username:");
fflush(stdin);
fgets(userName,MAX_SIZE_USER_NAME,stdin);
printf("\n\n\t\t\tNew Password:");
fflush(stdin);
fgets(password,MAX_SIZE_PASSWORD,stdin);
strncpy(fileHeaderInfo.username,userName,sizeof(userName));
strncpy(fileHeaderInfo.password,password,sizeof(password));
fwrite(&fileHeaderInfo,FILE_HEADER_SIZE, 1, fp);
fclose(fp);
printf("\n\t\t\tYour Password has been changed successfully");
printf("\n\t\t\t\tPress Enter to Login Again:");
getch();
login();
}
void about()
{
headMessage("About");
printf("\n\n\n\t\t\t\t\t--------------------------------\n");
printf("\t\t\t\t\t\t MABE BY \n\n");
printf("\t\t\t\t\t\tRAKSHIT GUPTA \n");;
printf("\t\t\t\t\t\t 2021a1r050 \n");;
printf("\t\t\t\t\t--------------------------------\n\n\n");
printf("Press Enter to Return to Main Menu.......");
getch();
menu();
}
void menu()
{
int choice = 0;
do
{
headMessage("MAIN MENU");
printf("\n\n\n\t\t\t1.Add Books");
printf("\n\t\t\t2.Search Books");
printf("\n\t\t\t3.View Books");
printf("\n\t\t\t4.Delete Book");
printf("\n\t\t\t5.Update Password");
printf("\n\t\t\t6.About");
printf("\n\t\t\t7.Exit");
printf("\n\n\n\t\t\tEnter choice => ");
scanf("%d",&choice);
switch(choice)
{
case 1:
addBookInDataBase();
break;
case 2:
searchBooks();
break;
case 3:
viewBooks();
break;
case 4:
deleteBooks();
break;
case 5:
updateCredential();
break;
case 6:
about();
exit(1);
break;
case 7:
printf("\n\n\n\t\t\t\t| Thank you!!! |\n\n\n\n\n");
exit(1);
break;
default:
printf("\n\n\n\t\t\tINVALID INPUT!!! Try again...");
} //Switch Ended
}
while(choice!=0); //Loop Ended
}
//login password
void login()
{
char userName[MAX_SIZE_USER_NAME] = {0};
char password[MAX_SIZE_PASSWORD] = {0};
int L=0,j;
sFileHeader fileHeaderInfo = {0};
FILE *fp = NULL;
headMessage("Login");
fp = fopen(FILE_NAME,"rb");
if(fp == NULL)
{
printf("File is not opened\n");
exit(1);
}
fread (&fileHeaderInfo,FILE_HEADER_SIZE, 1, fp);
fclose(fp);
do
{
printf("\n\n\n\t\t\t\tUsername:");
fgets(userName,MAX_SIZE_USER_NAME,stdin);
printf("\n\t\t\t\tPassword:");
fgets(password,MAX_SIZE_PASSWORD,stdin);
if((!strcmp(userName,fileHeaderInfo.username)) && (!strcmp(password,fileHeaderInfo.password)))
{
menu();
}
else
{
printf("\t\t\t\tLogin Failed Enter Again Username & Password\n\n");
L++;
}
}
while(L<=3);
if(L>3)
{
headMessage("Login Failed");
printf("\t\t\t\tSorry,Unknown User.");
getch();
}
}
int isFileExists(char *path)
{
// Try to open file
FILE *fp = fopen(path, "rb");
int status = 0;
// If file does not exists
if (fp != NULL)
{
status = 1;
// File exists hence close file
fclose(fp);
}
return status;
}
void init()
{
FILE *fp = NULL;
int status = 0;
char defaultUsername[] ="a7\n";
char defaultPassword[] ="a7\n";
sFileHeader fileHeaderInfo = {0};
status = isFileExists(FILE_NAME);
if(!status)
{
//create the binary file
fp = fopen(FILE_NAME,"wb");
if(fp != NULL)
{
//Copy default password
strncpy(fileHeaderInfo.password,defaultPassword,sizeof(defaultPassword));
strncpy(fileHeaderInfo.username,defaultUsername,sizeof(defaultUsername));
fwrite(&fileHeaderInfo,FILE_HEADER_SIZE, 1, fp);
fclose(fp);
}
}
}
int main()
{
init();
login();
return 0;
}