Library Management System in C

Library Management System in C

Library Management System in C

Welcome to this tutorial on building a simple library management system using the C programming language. In this project, we will create a program that allows librarians to manage books, track borrowing and returning of books, and provide a user-friendly interface for library users. Let's get started!

Key Features

  • Book Entry: Librarians can add new books to the library by providing details such as the title, author, ISBN, publication year, and quantity available.
  • Book Search: Users can search for books based on criteria such as the title, author, or ISBN. The program will display search results with book details and availability status.
  • Book Borrowing and Returning: Users can borrow and return books. The program will enforce validation checks to ensure books are available for borrowing and update the availability status accordingly.
  • User Registration: Users can register for a library account by providing their name, email, and password.
  • User Authentication: The program includes a login system to authenticate users and provide access to borrowing and returning books, as well as other user-specific functionalities.
  • User Management: Librarians can manage user accounts, including creating, updating, and deleting user profiles.
  • Data Persistence: The program securely stores book and user data, ensuring data integrity and persistence between sessions.

The Code

      
	#include 
        #include 
        #include 

        #define MAX_BOOKS 100
        #define MAX_USERS 100

        // Structure to represent a book
        struct Book {
            char title[100];
            char author[100];
            char isbn[13];
            int publicationYear;
            int quantity;
        };

        // Structure to represent a user
        struct User {
            char name[100];
            char email[100];
            char password[100];
        };

        // Global arrays to store books and users
        struct Book books[MAX_BOOKS];
        struct User users[MAX_USERS];

        int bookCount = 0;
        int userCount = 0;

        // Function to add a book to the library
        void addBook() {
            if (bookCount >= MAX_BOOKS) {
                printf("Maximum book limit reached.\n");
                return;
            }

            struct Book newBook;

            printf("Enter Book Title: ");
            scanf(" %[^\n]s", newBook.title);

            printf("Enter Author Name: ");
            scanf(" %[^\n]s", newBook.author);

            printf("Enter ISBN: ");
            scanf("%s", newBook.isbn);

            printf("Enter Publication Year: ");
            scanf("%d", &newBook.publicationYear);

            printf("Enter Quantity: ");
            scanf("%d", &newBook.quantity);

            books[bookCount++] = newBook;

            printf("Book added successfully.\n");
        }

        // Function to search for a book by title
        void searchBook() {
            char searchTitle[100];
            printf("Enter Book Title to search: ");
            scanf(" %[^\n]s", searchTitle);

            int found = 0;

            for (int i = 0; i < bookCount; i++) {
                if (strcmp(books[i].title, searchTitle) == 0) {
                    printf("Book found:\n");
                    printf("Title: %s\n", books[i].title);
                    printf("Author: %s\n", books[i].author);
                    printf("ISBN: %s\n", books[i].isbn);
                    printf("Publication Year: %d\n", books[i].publicationYear);
                    printf("Quantity: %d\n", books[i].quantity);
                    found = 1;
                    break;
                }
            }

            if (!found) {
                printf("Book not found.\n");
            }
        }

        // Function to display all books in the library
        void displayBooks() {
            printf("Books in the Library:\n");

            for (int i = 0; i < bookCount; i++) {
                printf("Title: %s\n", books[i].title);
                printf("Author: %s\n", books[i].author);
                printf("ISBN: %s\n", books[i].isbn);
                printf("Publication Year: %d\n", books[i].publicationYear);
                printf("Quantity: %d\n", books[i].quantity);
                printf("---------------------------\n");
            }
        }

        // Function to add a user
        void addUser() {
            if (userCount >= MAX_USERS) {
                printf("Maximum user limit reached.\n");
                return;
            }

            struct User newUser;

            printf("Enter Name: ");
            scanf(" %[^\n]s", newUser.name);

            printf("Enter Email: ");
            scanf(" %[^\n]s", newUser.email);

            printf("Enter Password: ");
            scanf(" %[^\n]s", newUser.password);

            users[userCount++] = newUser;

            printf("User added successfully.\n");
        }

        // Function to authenticate a user
        int authenticateUser() {
            char email[100];
            char password[100];

            printf("Enter Email: ");
            scanf(" %[^\n]s", email);

            printf("Enter Password: ");
            scanf(" %[^\n]s", password);

            for (int i = 0; i < userCount; i++) {
                if (strcmp(users[i].email, email) == 0 && strcmp(users[i].password, password) == 0) {
                    return 1; // User authenticated
                }
            }

            return 0; // Authentication failed
        }

        int main() {
            int choice;
            int authenticated = 0;

            while (1) {
                printf("\n----- Library Management System -----\n");
                printf("1. Add Book\n");
                printf("2. Search Book\n");
                printf("3. Display All Books\n");
                printf("4. Add User\n");
                printf("5. Login\n");
                printf("6. Exit\n");
                printf("Enter your choice: ");
                scanf("%d", &choice);

                switch (choice) {
                    case 1:
                        addBook();
                        break;
                    case 2:
                        searchBook();
                        break;
                    case 3:
                        displayBooks();
                        break;
                    case 4:
                        addUser();
                        break;
                    case 5:
                        authenticated = authenticateUser();
                        if (authenticated) {
                            printf("User authenticated.\n");
                            // Additional functionality for authenticated users
                            // You can add borrowing, returning, and other user-specific operations here
                        } else {
                            printf("Authentication failed. Please try again.\n");
                        }
                        break;
                    case 6:
                        printf("Exiting...\n");
                        exit(0);
                    default:
                        printf("Invalid choice. Please try again.\n");
                }
            }

            return 0;
        }
      
    

Output

      
----- Library Management System -----
1. Add Book
2. Search Book
3. Display All Books
4. Add User
5. Login
6. Exit
Enter your choice: 1
Enter Book Title: Introduction to Programming
Enter Author Name: John Smith
Enter ISBN: 9781234567890
Enter Publication Year: 2021
Enter Quantity: 5
Book added successfully.

----- Library Management System -----
1. Add Book
2. Search Book
3. Display All Books
4. Add User
5. Login
6. Exit
Enter your choice: 3
Books in the Library:
Title: Introduction to Programming
Author: John Smith
ISBN: 9781234567890
Publication Year: 2021
Quantity: 5
---------------------------

----- Library Management System -----
1. Add Book
2. Search Book
3. Display All Books
4. Add User
5. Login
6. Exit
Enter your choice: 2
Enter Book Title to search: Introduction to Programming
Book found:
Title: Introduction to Programming
Author: John Smith
ISBN: 9781234567890
Publication Year: 2021
Quantity: 5

----- Library Management System -----
1. Add Book
2. Search Book
3. Display All Books
4. Add User
5. Login
6. Exit
Enter your choice: 4
Enter Name: Alice Johnson
Enter Email: alice@example.com
Enter Password: password
User added successfully.

----- Library Management System -----
1. Add Book
2. Search Book
3. Display All Books
4. Add User
5. Login
6. Exit
Enter your choice: 5
Enter Email: alice@example.com
Enter Password: password
User authenticated.

      
    

Running the Program

  1. Open a C compiler or IDE of your choice.
  2. Create a new project and add the provided code.
  3. Build and run the program.
  4. You can now interact with the library management system through the console interface.

Conclusion

Congratulations! You have successfully implemented a basic library management system in C. This project provides a solid foundation for further expansion and customization. Feel free to enhance the system with additional features such as fine calculation, book reservation, book recommendations, or reports and analytics.

I hope you found this tutorial helpful. Happy coding!

Comments

Popular posts from this blog

Installation Steps for Flutter and Android Studio

Exploring Flutter's Text and TextField Widgets