Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
6.2 kB
2
Indexable
Never
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>

// Define constants
#define MAX_BOOK_NAME 50
#define MAX_AUTHOR_NAME 50
#define FILE_NAME "LibraryDatabase.bin"
#define FILE_HEADER_SIZE sizeof(struct FileHeader)

// Structure to store date
typedef struct {
    int yyyy;
    int mm;
    int dd;
} Date;

// Structure to store book information
typedef struct {
    unsigned int book_id;
    char bookName[MAX_BOOK_NAME];
    char authorName[MAX_AUTHOR_NAME];
    char studentName[MAX_AUTHOR_NAME];
    Date bookIssueDate;
} Book;

// Structure to store file header information
struct FileHeader {
    char username[30];
    char password[20];
};

// Function prototypes
void printMenu();
void addBook();
void searchBook();
void viewBooks();
void deleteBook();
void updatePassword();
bool isLeapYear(int year);
bool isValidDate(Date date);
bool isNameValid(const char *name);
bool isFileExists(const char *file);

// Global variable for file header
struct FileHeader fileHeader;

int main() {
    FILE *fp;

    // Check if the file exists and create one if not
    if (!isFileExists(FILE_NAME)) {
        fp = fopen(FILE_NAME, "wb");
        strcpy(fileHeader.username, "admin");
        strcpy(fileHeader.password, "admin123");
        fwrite(&fileHeader, FILE_HEADER_SIZE, 1, fp);
        fclose(fp);
    }

    // Show login screen
    char username[30], password[20];
    printf("Library Management System Login\n");
    printf("Username: ");
    fgets(username, sizeof(username), stdin);
    printf("Password: ");
    fgets(password, sizeof(password), stdin);
    username[strcspn(username, "\n")] = '\0'; // Remove newline character
    password[strcspn(password, "\n")] = '\0'; // Remove newline character

    // Check if the entered username and password are correct
    fp = fopen(FILE_NAME, "rb");
    fread(&fileHeader, FILE_HEADER_SIZE, 1, fp);
    fclose(fp);
    if (strcmp(username, fileHeader.username) != 0 || strcmp(password, fileHeader.password) != 0) {
        printf("Invalid credentials. Exiting...\n");
        return 0;
    }

    int choice;
    do {
        printMenu();
        printf("Enter choice: ");
        scanf("%d", &choice);

        switch (choice) {
            case 1:
                addBook();
                break;
            case 2:
                searchBook();
                break;
            case 3:
                viewBooks();
                break;
            case 4:
                deleteBook();
                break;
            case 5:
                updatePassword();
                break;
            case 6:
                printf("Exiting Library Management System...\n");
                break;
            default:
                printf("Invalid choice. Please try again.\n");
        }

    } while (choice != 6);

    return 0;
}

// Function to print the menu
void printMenu() {
    printf("\nLibrary Management System Menu:\n");
    printf("1. Add Book\n");
    printf("2. Search Book\n");
    printf("3. View Books\n");
    printf("4. Delete Book\n");
    printf("5. Update Password\n");
    printf("6. Exit\n");
}

// Function to add a new book
void addBook() {
    FILE *fp;
    Book newBook;

    fp = fopen(FILE_NAME, "ab+");
    if (fp == NULL) {
        printf("Error opening file.\n");
        return;
    }

    printf("Enter Book ID: ");
    scanf("%u", &newBook.book_id);
    printf("Enter Book Name: ");
    fflush(stdin);
    fgets(newBook.bookName, MAX_BOOK_NAME, stdin);
    newBook.bookName[strcspn(newBook.bookName, "\n")] = '\0'; // Remove newline character
    printf("Enter Author Name: ");
    fflush(stdin);
    fgets(newBook.authorName, MAX_AUTHOR_NAME, stdin);
    newBook.authorName[strcspn(newBook.authorName, "\n")] = '\0'; // Remove newline character

    // Set student name to empty initially
    newBook.studentName[0] = '\0';

    printf("Enter Book Issue Date (dd/mm/yyyy): ");
    scanf("%d/%d/%d", &newBook.bookIssueDate.dd, &newBook.bookIssueDate.mm, &newBook.bookIssueDate.yyyy);

    // Validate the book issue date
    if (!isValidDate(newBook.bookIssueDate)) {
        printf("Invalid date. Book not added.\n");
        fclose(fp);
        return;
    }

    fwrite(&newBook, sizeof(Book), 1, fp);
    printf("Book added successfully.\n");
    fclose(fp);
}

// Function to search for a book by name
void searchBook() {
    FILE *fp;
    char bookName[MAX_BOOK_NAME];
    Book book;

    fp = fopen(FILE_NAME, "rb");
    if (fp == NULL) {
        printf("Error opening file.\n");
        return;
    }

    printf("Enter Book Name to search: ");
    fflush(stdin);
    fgets(bookName, MAX_BOOK_NAME, stdin);
    bookName[strcspn(bookName, "\n")] = '\0'; // Remove newline character

    bool found = false;
    while (fread(&book, sizeof(Book), 1, fp)) {
        if (strcmp(book.bookName, bookName) == 0) {
            printf("Book ID: %u\n", book.book_id);
            printf("Book Name: %s\n", book.bookName);
            printf("Author Name: %s\n", book.authorName);
            printf("Student Name: %s\n", book.studentName);
            printf("Book Issue Date: %d/%d/%d\n", book.bookIssueDate.dd, book.bookIssueDate.mm, book.bookIssueDate.yyyy);
            found = true;
            break;
        }
    }

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

    fclose(fp);
}

// Function to view all books in the library
void viewBooks() {
    FILE *fp;
    Book book;

    fp = fopen(FILE_NAME, "rb");
    if (fp == NULL) {
        printf("Error opening file.\n");
        return;
    }

    while (fread(&book, sizeof(Book), 1, fp)) {
        printf("Book ID: %u\n", book.book_id);
        printf("Book Name: %s\n", book.bookName);
        printf("Author Name: %s\n", book.authorName);
        printf("Student Name: %s\n", book.studentName);
        printf("Book Issue Date: %d/%d/%d\n", book.bookIssueDate.dd, book.bookIssueDate.mm, book.bookIssueDate.yyyy);
        printf("-------------------------\n");
    }

    fclose(fp);
}

// Function to delete a book from the library
void deleteBook() {
    FILE *fp, *tempFp;
    char bookName[MAX_BOOK_NAME];
    Book book;

    fp = fopen(FILE_NAME, "rb");
    if (fp ==