Q1. Design and implement a simple Book-Organizing Module of a Library Management System using C programming. (40 Marks)


Description:


Create a menu driven application that allows the user to manage the books in a library. The application should enable users to perform the following operations:

1. Add a new book record.
2. Display all book records.
3. Search a book by its ISBN.
4. Search a book by its Author.
5. Search a book by its Title.
6. Update a book record.
7. Delete a book record.
8. Save book records to a file.
9. Load book records from a file.
10.Exit.


Implement the following functions:

ï‚· void addBook(Book books[], int *count)

ï‚· void displayBooks(const Book books[], int count)

ï‚· int searchBookByISBN(const Book books[], int count, const char *isbn)

ï‚· void updateBook(Book books[], int count, const char *isbn)

ï‚· void deleteBook(Book books[], int *count, const char *isbn)

ï‚· void saveToFile(const Book books[], int count, const char *filename)

ï‚· void loadFromFile(Book books[], int *count, const char *filename)

Menus

ï‚·Implement a menu-driven system that allows the user to choose the operation they want to perform.

File Handling

ï‚·Use file handling to save the book records to a file and load them back when the program starts.

Note: You must execute the program and submit the program logic, sample input and output along with

the necessary documentation for this practical question. Assumptions can be made wherever necessary

Ans:-  Below is a simple implementation of a **Book-Organizing Module** for a **Library Management System** using C programming. The program performs the specified operations like adding, displaying, searching, updating, deleting, saving, and loading book records.

Struct Definition
We define a `Book` structure to represent a book record.

```c
#include <stdio.h>
#include <string.h>

#define MAX_BOOKS 100
#define ISBN_LENGTH 13

typedef struct {
    char isbn[ISBN_LENGTH];
    char title[100];
    char author[100];
    int year;
} Book;

void addBook(Book books[], int *count);
void displayBooks(const Book books[], int count);
int searchBookByISBN(const Book books[], int count, const char *isbn);
void updateBook(Book books[], int count, const char *isbn);
void deleteBook(Book books[], int *count, const char *isbn);
void saveToFile(const Book books[], int count, const char *filename);
void loadFromFile(Book books[], int *count, const char *filename);
```

### Menu-Driven System

```c
void menu() {
    printf("\nLibrary Management System\n");
    printf("1. Add a new book\n");
    printf("2. Display all books\n");
    printf("3. Search book by ISBN\n");
    printf("4. Search book by Author\n");
    printf("5. Search book by Title\n");
    printf("6. Update a book record\n");
    printf("7. Delete a book record\n");
    printf("8. Save book records to a file\n");
    printf("9. Load book records from a file\n");
    printf("10. Exit\n");
}

int main() {
    Book books[MAX_BOOKS];
    int count = 0;
    int choice;
    char isbn[ISBN_LENGTH];
    char author[100];
    char title[100];
    char filename[100] = "library.txt";

    loadFromFile(books, &count, filename);  // Load records from file at the start

    do {
        menu();
        printf("Enter your choice: ");
        scanf("%d", &choice);

        switch (choice) {
            case 1:
                addBook(books, &count);
                break;
            case 2:
                displayBooks(books, count);
                break;
            case 3:
                printf("Enter ISBN to search: ");
                scanf("%s", isbn);
                int index = searchBookByISBN(books, count, isbn);
                if (index != -1)
                    printf("Book found: %s by %s\n", books[index].title, books[index].author);
                else
                    printf("Book not found\n");
                break;
            case 4:
                printf("Enter Author name to search: ");
                scanf("%s", author);
                for (int i = 0; i < count; i++) {
                    if (strcmp(books[i].author, author) == 0)
                        printf("Found: %s by %s\n", books[i].title, books[i].author);
                }
                break;
            case 5:
                printf("Enter Title to search: ");
                scanf("%s", title);
                for (int i = 0; i < count; i++) {
                    if (strcmp(books[i].title, title) == 0)
                        printf("Found: %s by %s\n", books[i].title, books[i].author);
                }
                break;
            case 6:
                printf("Enter ISBN to update: ");
                scanf("%s", isbn);
                updateBook(books, count, isbn);
                break;
            case 7:
                printf("Enter ISBN to delete: ");
                scanf("%s", isbn);
                deleteBook(books, &count, isbn);
                break;
            case 8:
                saveToFile(books, count, filename);
                break;
            case 9:
                loadFromFile(books, &count, filename);
                break;
            case 10:
                printf("Exiting...\n");
                break;
            default:
                printf("Invalid choice! Try again.\n");
        }
    } while (choice != 10);

    return 0;
}
```

Function Implementations

1. **Add a new book**

```c
void addBook(Book books[], int *count) {
    if (*count < MAX_BOOKS) {
        printf("Enter ISBN: ");
        scanf("%s", books[*count].isbn);
        printf("Enter Title: ");
        scanf(" %[^\n]s", books[*count].title);
        printf("Enter Author: ");
        scanf(" %[^\n]s", books[*count].author);
        printf("Enter Year: ");
        scanf("%d", &books[*count].year);
        (*count)++;
        printf("Book added successfully!\n");
    } else {
        printf("Library is full!\n");
    }
}
```

2. **Display all books**

```c
void displayBooks(const Book books[], int count) {
    if (count == 0) {
        printf("No books to display!\n");
        return;
    }
    printf("\nList of books in the library:\n");
    for (int i = 0; i < count; i++) {
        printf("ISBN: %s, Title: %s, Author: %s, Year: %d\n", books[i].isbn, books[i].title, books[i].author, books[i].year);
    }
}
```

3. **Search book by ISBN**

```c
int searchBookByISBN(const Book books[], int count, const char *isbn) {
    for (int i = 0; i < count; i++) {
        if (strcmp(books[i].isbn, isbn) == 0) {
            return i;
        }
    }
    return -1;
}
```

4. **Update book record**

```c
void updateBook(Book books[], int count, const char *isbn) {
    int index = searchBookByISBN(books, count, isbn);
    if (index != -1) {
        printf("Updating record for ISBN: %s\n", isbn);
        printf("Enter new Title: ");
        scanf(" %[^\n]s", books[index].title);
        printf("Enter new Author: ");
        scanf(" %[^\n]s", books[index].author);
        printf("Enter new Year: ");
        scanf("%d", &books[index].year);
        printf("Book updated successfully!\n");
    } else {
        printf("Book not found!\n");
    }
}
```

5. **Delete book record**

```c
void deleteBook(Book books[], int *count, const char *isbn) {
    int index = searchBookByISBN(books, *count, isbn);
    if (index != -1) {
        for (int i = index; i < (*count) - 1; i++) {
            books[i] = books[i + 1];  // Shift all records after the deleted one
        }
        (*count)--;
        printf("Book deleted successfully!\n");
    } else {
        printf("Book not found!\n");
    }
}
```

6. **Save records to a file**

```c
void saveToFile(const Book books[], int count, const char *filename) {
    FILE *file = fopen(filename, "w");
    if (file == NULL) {
        printf("Error opening file for writing!\n");
        return;
    }
    fwrite(books, sizeof(Book), count, file);
    fclose(file);
    printf("Records saved to file.\n");
}
```

7. **Load records from a file**

```c
void loadFromFile(Book books[], int *count, const char *filename) {
    FILE *file = fopen(filename, "r");
    if (file == NULL) {
        printf("Error opening file for reading! Starting with empty library.\n");
        return;
    }
    *count = fread(books, sizeof(Book), MAX_BOOKS, file);
    fclose(file);
    printf("Records loaded from file.\n");
}
```

### Sample Input/Output

```plaintext
Library Management System
1. Add a new book
2. Display all books
3. Search book by ISBN
4. Search book by Author
5. Search book by Title
6. Update a book record
7. Delete a book record
8. Save book records to a file
9. Load book records from a file
10. Exit

Enter your choice: 1
Enter ISBN: 9781234567890
Enter Title: C Programming
Enter Author: John Doe
Enter Year: 2020
Book added successfully!

Enter your choice: 2
List of books in the library:
ISBN: 9781234567890, Title: C Programming, Author: John Doe, Year: 2020
```

No comments: