Pages

Labels

Library Management System

C Program of Library Management System

#include<stdio.h>
#include<ctype.h>
#include<string.h>
#include<stdlib.h>
#include <conio.h>

struct record
{
int serialNumber;
int ISBN;
char bookName[31];
int edition;
char authorName[31];
int isResevered;
};

void AddBook();
void DeleteBook();
void SearchBook();
void UpdateRecord();
void DisplayRecord();
void IssueBook();
void SearchByName();
void PrintLine(struct record Book);

int main()
{
int option = 0;
printf("\t\t\t Welcome to Library System \n");
printf("_________________________________________________________\n\n");
do{
printf("==========  Menu  ==========\n\n");
printf("1. Add a New Book\n");
printf("2. Delete a Book\n");
printf("3. Search a Book\n");
printf("4. Display All The Books in Library\n");
printf("5. Issue Book to an Other\n");
printf("6. Exit the Program");              
                                             
printf("\n\nEnter Your Choise: ");
scanf("%d",&option);
printf("\n\n");

switch (option)
{
case 1:
AddBook();
break;
case 2:
DeleteBook();
break;
case 3:
SearchBook();
break;
case 4:
DisplayRecord();
break;
case 5:
IssueBook();
break;
case 6:
printf("Exiting Program!\nGoodBye!\n");
exit(0);
return 0;
default:
printf("Invalid Option! Enter Right Choice\n\n");
break;
}
} while (1);
return 0;
}

void AddBook()
{
char ch;
int i;
struct record Book;
FILE *FileRecord;

if(fopen("Library.txt", "r"))
{
FileRecord = fopen("Library.txt", "a+");
fprintf(FileRecord, "\n");
}
else
FileRecord = fopen("Library.txt", "a+");
Book.isResevered = 0;
printf("===Enter Required information to Add New Book in Record!===\n");
printf("Enter Serial Number of Book: ");
scanf(" %d", &Book.serialNumber);

printf("Enter Book ISBN: ");
scanf(" %d", &Book.ISBN);

printf("Enter Book Name of Maximum 30 Character: ");
fflush(stdin);
for(i = 0; ;i++)
{
scanf("%c", &ch);
if(ch == '\n')
{
Book.bookName[i] = '\0';
break;

}
else if(ch == ' ')
Book.bookName[i] = '_';
else
Book.bookName[i] = ch;
}

printf("Enter Book Edition: ");
scanf(" %d", &Book.edition);

printf("Enter Author Name of maximum 30 characters: ");
fflush(stdin);
for(i = 0; ;i++)
{
scanf("%c", &ch);
if(ch == '\n')
{
Book.authorName[i] = '\0';
break;
}
else if(ch == ' ')
Book.authorName[i] = '_';
else
Book.authorName[i] = ch;
}

fprintf(FileRecord, "%d  %d  %s  %d  %s  %d",Book.serialNumber,Book.ISBN,Book.bookName,Book.edition,Book.authorName,Book.isResevered);
fclose(FileRecord);
printf("\nNew Book with Serial Number << %d >> Added in Library Record\n\n", Book.serialNumber);
printf("\n\nPress Any Key to Continue");
getch();
system("cls");
}

void DeleteBook()
{
int del = 0;
int serial;
int i = 0;
FILE *FileRecord, *temp;
struct record Book;
printf("Enter Serial Number of Book to delete: ");
scanf("%d", &serial);
FileRecord = fopen("Library.txt", "r+");
temp = fopen("Temp.txt", "w+");

while(fscanf(FileRecord, "%d %d %s %d %s %d",&Book.serialNumber,&Book.ISBN,Book.bookName,&Book.edition,Book.authorName,&Book.isResevered )>0)
{
if (i == 1)
fprintf(temp, "\n");
if (serial == Book.serialNumber)
{
del = 1;
i = 0;
continue;
}
fprintf(temp, "%d  %d  %s  %d  %s  %d",Book.serialNumber,Book.ISBN,Book.bookName,Book.edition,Book.authorName,Book.isResevered);
i = 1;
}
fclose(temp);
fclose(FileRecord);
remove("Library.txt");
rename("Temp.txt", "Library.txt");

if (del == 1)
printf("Book Having Serial Number=%d has been deleted from record\n\n", serial);
else
printf("Book having serial number=%d, not found in available record!!!Wrong serial number\n\n", serial);
printf("\n\nPress any key to continue");
getch();
system("cls");
}

void SearchBook()
{
char opt;
printf("\nSearch by Book Name\n: ");
scanf(" %c", &opt);
switch (opt) {
case 'a':
case 'A':
SearchByName();
break;
default:
printf("Invalid Option Entered!!!\n");
break;
}
}

void DisplayRecord()
{
struct record Book;
FILE *FileRecord = fopen("Library.txt", "r");
if (FileRecord == NULL)
{
printf("No Record Found!!!\n");
return;
}
printf("%-9s %-11s %-20s %-3s %-20s %s\n", "Sr. No.", "ISBN", "Book Name", "Ed.", "Author name", "Rsrvd/Isud?\n");
while(fscanf(FileRecord, "%d %d %s %d %s %d", &Book.serialNumber, &Book.ISBN, Book.bookName, &Book.edition, Book.authorName, &Book.isResevered ) > 0)
{
PrintLine(Book);
}
printf("\n");
fclose(FileRecord);
printf("\n\nPress any key to continue");
getch();
system("cls");
}


void IssueBook()
{
int flag = 0;
int serial;
int i = 0;
FILE *FileRecord, *temp;
struct record Book;
printf("Enter Serial Number of Book to Issue: ");
scanf("%d", &serial);
FileRecord = fopen("Library.txt", "r+");
temp = fopen("Temp.txt", "w+");

while(fscanf(FileRecord, "%d %d %s %d %s %d", &Book.serialNumber, &Book.ISBN, Book.bookName, &Book.edition, Book.authorName, &Book.isResevered ) > 0)
{
if (i == 1)
fprintf(temp, "\n");
if (serial == Book.serialNumber)
{
if(Book.isResevered == 0)
{
flag = 1;
Book.isResevered = 1;
}
else
flag = 2;
}
fprintf(temp, "%d  %d  %s  %d  %s  %d", Book.serialNumber, Book.ISBN, Book.bookName, Book.edition, Book.authorName, Book.isResevered);
i = 1;
}
fclose(temp);
fclose(FileRecord);
remove("Library.txt");
rename("Temp.txt", "Library.txt");

if (flag == 1)
printf("Book having serial number= %d has been Issued\n", serial);
else if (flag == 0)
printf("Book having serial number= %d, not found in available record!!!Wrong serial number\n", serial);
else
printf("Book having serial number= %d has already been Issued\n", serial);
printf("\n\nPress any key to continue");
getch();
system("cls");
}

void SearchByName()
{
int i, flag = 0;
char ch;
struct record Book;
char Name[30];
FILE *FileRecord = fopen("Library.txt", "r");
if (FileRecord == NULL)
{
printf("No Record Found!!!\n");
return;
}
printf("Enter \"Book Name\" to Search: ");
fflush(stdin);
for(i = 0; ;i++)
{
scanf("%c", &ch);
if(ch == '\n')
{
Name[i] = '\0';
break;
}
else if(ch == ' ')
Name[i] = '_';
else
Name[i] = ch;
}
while(fscanf(FileRecord, "%d %d %s %d %s %d", &Book.serialNumber, &Book.ISBN, Book.bookName, &Book.edition, Book.authorName, &Book.isResevered ) > 0)
{
if(strcmp(Book.bookName, Name) == 0)
{
if(flag == 0)
{
flag = 1;
printf("%-9s %-11s %-20s %-3s %-20s %s\n", "Sr. No.", "ISBN", "Book Name", "Ed.", "Author name", "Rsrvd/Isud?");
}
PrintLine(Book);
}
}
fclose(FileRecord);
if(flag == 0)
printf("Required Book Not Found\n");
printf("\n");
printf("\n\nPress any key to continue");
getch();
system("cls");
}

void PrintLine(struct record Book)
{
int i;
printf("%-9d %-11d ", Book.serialNumber, Book.ISBN);
i = 0;
while(Book.bookName[i] != '\0')
{
if(Book.bookName[i] == '_')
printf(" ");
else
printf("%c",Book.bookName[i]);
i++;
if(i>19)
break; //To keep list consistent, characters after 18 are not displayed
}
while(i <= 19)
{
printf(" ");
i++;
}
printf(" %-3d ",Book.edition);
i = 0;
while(Book.authorName[i] != '\0')
{
if(Book.authorName[i] == '_')
printf(" ");
else
printf("%c",Book.authorName[i]);
i++;
if(i > 19)
break;
}
while(i <= 19)
{
printf(" ");
i++;
}
if(Book.isResevered==0)
printf(" No\n");
else
printf(" Yes\n");
}