Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
14 kB
0
Indexable
Never
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <windows.h>
#include <stdbool.h>

struct Member
{
    char name[100];
    int age;
    char email[100];
    char username[100];
    char password[100];
    struct Member *next;
};

void printColoredText(char *text, int color, bool animated)
{
    HANDLE hConsole;
    hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(hConsole, color);

    if (animated)
    {
        int i;
        for (i = 0; i < strlen(text); i++)
        {
            printf("%c", text[i]);
            Sleep(50);
        }
    }
    else
    {
        printf("%s", text);
    }

    SetConsoleTextAttribute(hConsole, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
}

void getPassword(char *password, int maxLength)
{
    int i = 0;
    char c;

    while (1)
    {
        c = _getch();
        if (c == 13)
        {
            break;
        }
        else if (c == 8 && i > 0)
        {
            printf("\b \b");
            i--;
        }
        else if (c != 8 && i < maxLength - 1)
        {
            password[i++] = c;
            printf("*");
        }
    }

    password[i] = '\0';
}

void loadMembersFromFile(struct Member **head)
{
    FILE *file = fopen("members.csv", "r");
    if (file == NULL)
    {
        printf("Could not open file for reading.\n");
        exit(1);
    }

    char buffer[200];
    while (fgets(buffer, sizeof(buffer), file))
    {
        struct Member *newMember = (struct Member *)malloc(sizeof(struct Member));
        sscanf(buffer, "%*[^,],%*d,%*[^,],%[^,],%s", newMember->username, newMember->password);
        newMember->next = *head;
        *head = newMember;
    }

    fclose(file);
}

int authenticateUser(struct Member *head, const char *username, const char *password)
{
    while (head != NULL)
    {
        if (strcmp(username, head->username) == 0 && strcmp(password, head->password) == 0)
        {
            return 1;
        }
        head = head->next;
    }
    return 0;
}

void addMember(struct Member **head, char name[], int age, char email[], char username[], char password[])
{
    struct Member *newMember = (struct Member *)malloc(sizeof(struct Member));

    strcpy(newMember->name, name);

    newMember->age = age;

    strcpy(newMember->email, email);
    strcpy(newMember->username, username);
    strcpy(newMember->password, password);

    newMember->next = *head;
    *head = newMember;
}

void saveMembersToFile(struct Member *head)
{
    FILE *file = fopen("members.csv", "w");
    if (file == NULL)
    {
        printf("Could not open file for writing.\n");
        return;
    }

    fprintf(file, "Name,Age,Email,Username,Password\n");
    while (head != NULL)
    {
        fprintf(file, "%s,%d,%s,%s,%s\n", head->name, head->age, head->email, head->username, head->password);
        head = head->next;
    }

    fclose(file);
}

int mainMember()
{
    system("cls");

    struct Member *head = NULL;

    char name[100],
        email[100],
        username[100],
        password[100];
    int age;

    printColoredText("==================== [Register Guest] ====================\n\n", FOREGROUND_GREEN, false);
    printf("Enter your Name : ");
    scanf("%s", name);

    printf("Enter your Age: ");
    scanf("%d", &age);

    printf("Enter your Email: ");
    scanf("%s", email);

    printf("Enter your Username: ");
    scanf("%s", username);

    printf("Enter your Password: ");
    scanf("%s", password);

    addMember(&head, name, age, email, username, password);
    saveMembersToFile(head);

    printColoredText("==========================================================\n\n", FOREGROUND_GREEN, false);

    printColoredText("[!] Wait written to members.csv\n", FOREGROUND_RED, true);
    printColoredText("[!] Data has been written to members.csv\n", FOREGROUND_GREEN, true);

    return 0;
}

void UserSelectClass(int choice)
{
    if (choice == 1)
    {
        printColoredText("-> Login\n", (choice == 1) ? FOREGROUND_BLUE : FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE, false);
        printColoredText("2. Register\n", (choice == 2) ? FOREGROUND_BLUE : FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE, false);
    }
    else
    {
        printColoredText("1. Login\n", (choice == 1) ? FOREGROUND_BLUE : FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE, false);
        printColoredText("-> Register\n", (choice == 2) ? FOREGROUND_BLUE : FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE, false);
    }
}

void AdminSelectMainMenu(int choice)
{
    if (choice == 1)
    {
        printColoredText("-> Manage with books\n", (choice == 1) ? FOREGROUND_BLUE : FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE, false);
        printColoredText("2. Manage with users\n", (choice == 2) ? FOREGROUND_BLUE : FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE, false);
        printColoredText("3. Manage book status\n", (choice == 3) ? FOREGROUND_BLUE : FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE, false);
    }
    else if (choice == 2)
    {
        printColoredText("1. Manage with books\n", (choice == 1) ? FOREGROUND_BLUE : FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE, false);
        printColoredText("-> Manage with users\n", (choice == 2) ? FOREGROUND_BLUE : FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE, false);
        printColoredText("3. Manage book status\n", (choice == 3) ? FOREGROUND_BLUE : FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE, false);
    }
    else
    {
        printColoredText("1. Manage with books\n", (choice == 1) ? FOREGROUND_BLUE : FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE, false);
        printColoredText("2. Manage with users\n", (choice == 2) ? FOREGROUND_BLUE : FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE, false);
        printColoredText("-> Manage book status\n", (choice == 3) ? FOREGROUND_BLUE : FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE, false);
    }
}

void UserSelectMainMenu(int choice)
{
    if (choice == 1)
    {
        printColoredText("-> Borrow a book\n", (choice == 1) ? FOREGROUND_BLUE : FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE, false);
        printColoredText("2. Search for books\n", (choice == 2) ? FOREGROUND_BLUE : FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE, false);
        printColoredText("3. View my borrowing\n", (choice == 3) ? FOREGROUND_BLUE : FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE, false);
        printColoredText("4. Manage my profile\n", (choice == 4) ? FOREGROUND_BLUE : FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE, false);
        printColoredText("5. Exit\n", (choice == 5) ? FOREGROUND_BLUE : FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE, false);
    }
    else if (choice == 2)
    {
        printColoredText("1. Borrow a book\n", (choice == 1) ? FOREGROUND_BLUE : FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE, false);
        printColoredText("-> Search for books\n", (choice == 2) ? FOREGROUND_BLUE : FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE, false);
        printColoredText("3. View my borrowing\n", (choice == 3) ? FOREGROUND_BLUE : FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE, false);
        printColoredText("4. Manage my profile\n", (choice == 4) ? FOREGROUND_BLUE : FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE, false);
        printColoredText("5. Exit\n", (choice == 5) ? FOREGROUND_BLUE : FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE, false);
    }
    else if (choice == 3)
    {
        printColoredText("1. Borrow a book\n", (choice == 1) ? FOREGROUND_BLUE : FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE, false);
        printColoredText("2. Search for books\n", (choice == 2) ? FOREGROUND_BLUE : FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE, false);
        printColoredText("-> View my borrowing\n", (choice == 3) ? FOREGROUND_BLUE : FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE, false);
        printColoredText("4. Manage my profile\n", (choice == 4) ? FOREGROUND_BLUE : FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE, false);
        printColoredText("5. Exit\n", (choice == 5) ? FOREGROUND_BLUE : FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE, false);
    }
    else if (choice == 4)
    {
        printColoredText("1. Borrow a book\n", (choice == 1) ? FOREGROUND_BLUE : FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE, false);
        printColoredText("2. Search for books\n", (choice == 2) ? FOREGROUND_BLUE : FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE, false);
        printColoredText("3. View my borrowing\n", (choice == 3) ? FOREGROUND_BLUE : FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE, false);
        printColoredText("-> Manage my profile\n", (choice == 4) ? FOREGROUND_BLUE : FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE, false);
        printColoredText("5. Exit\n", (choice == 5) ? FOREGROUND_BLUE : FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE, false);
    }
    else
    {
        printColoredText("1. Borrow a book\n", (choice == 1) ? FOREGROUND_BLUE : FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE, false);
        printColoredText("2. Search for books\n", (choice == 2) ? FOREGROUND_BLUE : FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE, false);
        printColoredText("3. View my borrowing\n", (choice == 3) ? FOREGROUND_BLUE : FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE, false);
        printColoredText("4. Manage my profile\n", (choice == 4) ? FOREGROUND_BLUE : FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE, false);
        printColoredText("-> Exit\n", (choice == 5) ? FOREGROUND_BLUE : FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE, false);
    }
}

int main()
{
    int choice = 1;
    int keyPressed;

    do
    {
        system("cls");
        printColoredText("==================== [Your Online Library] ====================\n\n", FOREGROUND_GREEN, false);
        UserSelectClass(choice);
        printColoredText("\n===============================================================\n\n", FOREGROUND_GREEN, false);

        keyPressed = _getch();

        if (keyPressed == 224)
        {
            keyPressed = _getch();
            switch (keyPressed)
            {
            case 72:
                choice = (choice > 1) ? choice - 1 : choice;
                break;
            case 80:
                choice = (choice < 2) ? choice + 1 : choice;
                break;
            }
        }

    } while (keyPressed != 13);

    switch (choice)
    {
    case 1:
        system("cls");

        struct Member *members = NULL;
        loadMembersFromFile(&members);

        char inputUsername[100];
        char inputPassword[100];

        printf("Please enter username : ");
        scanf("%s", inputUsername);

        printf("Please enter password : ");
        getPassword(inputPassword, sizeof(inputPassword));

        if (authenticateUser(members, inputUsername, inputPassword))
        {
            printColoredText("\n[!] Wait for checking password.\n", FOREGROUND_RED, true);
            Sleep(1000);
            printColoredText("[!] Checking password successful.\n", FOREGROUND_GREEN, true);
            Sleep(1000);
            printColoredText("[!] Login successful.\n", FOREGROUND_GREEN, true);
            Sleep(1000);

            int choice = 1;
            int keyPressed;

            do
            {
                system("cls");
                printf("\nPlease select a topic:\n");

                AdminSelectMainMenu(choice);

                keyPressed = _getch();

                if (keyPressed == 224)
                {
                    keyPressed = _getch();
                    switch (keyPressed)
                    {
                    case 72:
                        choice = (choice > 1) ? choice - 1 : choice;
                        break;
                    case 80:
                        choice = (choice < 3) ? choice + 1 : choice;
                        break;
                    }
                }

            } while (keyPressed != 13);

            switch (choice)
            {
            case 1:
                system("cls");
                printColoredText("You choose to manage with the book.\n", FOREGROUND_GREEN, false);
            case 2:
                system("cls");
                printColoredText("You choose to manage with the user.\n", FOREGROUND_GREEN, false);
            case 3:
                system("cls");
                printColoredText("You choose to manage with the book status.\n", FOREGROUND_GREEN, false);
            }
        }
        else
        {
            printColoredText("\n[!] Login failed. Invalid Username or Password.\n", FOREGROUND_RED, true);
        }
        break;
    case 2:
        mainMember();
        break;
    }

    return 0;
}