Untitled

full final
 avatar
unknown
plain_text
17 days ago
22 kB
3
Indexable
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>


// Structure for Donor
typedef struct Donor
{
    char idNumber[20];
    char mobileNumber[15];
    char name[50];
    int age;
    char bloodGroup[5];
    char medicalCondition[50];
    char lastDonationDate[15];
    int totalDonationsLastYear;
    int isBlacklisted;
    int priority;
    char registrationLocation[50];
    int totalDonation;

    struct Donor *next;
} Donor;

// Structure for Request
typedef struct Request
{
    char requesterName[50];
    char bloodGroup[5];
    int unitsRequested;
    char location[50];
    char requestDate[15];
    int isFulfilled;
    int isCancelled;
    char donorId[20];

    struct Request *next;
} Request;

// Head pointers for linked lists
Donor *donorHead = NULL;
Request *requestHead = NULL;

void clearScreen()
{
#ifdef _WIN32
    system("cls");
#else
    system("clear");
#endif
}

int isValidMobile(char mobile[])
{
    if (strlen(mobile) != 11)
        return 0;
    if (mobile[0] != '0' || mobile[1] != '1')
        return 0;
    if (mobile[2] < '3' || mobile[2] > '9')
        return 0;
    for (int i = 0; i < 11; i++)
    {
        if (mobile[i] < '0' || mobile[i] > '9')
            return 0;
    }
    return 1;
}

int customStrptime(const char *date, struct tm *tm)
{
    if (sscanf(date, "%4d-%2d-%2d", &tm->tm_year, &tm->tm_mon, &tm->tm_mday) != 3)
        return -1;
    tm->tm_year -= 1900;
    tm->tm_mon -= 1;
    return 0;
}

int daysBetween(const char *date1, const char *date2)
{
    struct tm tm1 = {0}, tm2 = {0};
    time_t t1, t2;
    if (customStrptime(date1, &tm1) != 0 || customStrptime(date2, &tm2) != 0)
    {
        printf("Error parsing dates.\n");
        return -1;
    }

    t1 = mktime(&tm1);
    t2 = mktime(&tm2);

    return difftime(t2, t1) / (60 * 60 * 24);
}

int isRegistered(char id[], char mobile[])
{
    Donor *current = donorHead;
    while (current != NULL)
    {
        if (strcmp(current->idNumber, id) == 0 || strcmp(current->mobileNumber, mobile) == 0)
        {
            return 1; // Found a matching donor
        }
        current = current->next;
    }
    return 0; // Not found
}

void saveDataToFile() {
    // Save donors
    FILE *df = fopen("donors.dat", "wb");
    if (df == NULL) {
        printf("Error saving donors data!\n");
        return;
    }
    Donor *currDonor = donorHead;
    while (currDonor != NULL) {
        fwrite(currDonor, sizeof(Donor), 1, df);
        currDonor = currDonor->next;
    }
    fclose(df);
    // Save requests
    FILE *rf = fopen("requests.dat", "wb");
    if (rf == NULL) {
        printf("Error saving requests data!\n");
        return;
    }
    Request *currRequest = requestHead;
    while (currRequest != NULL) {
        fwrite(currRequest, sizeof(Request), 1, rf);
        currRequest = currRequest->next;
    }
    fclose(rf);
}


void loadDataFromFile()
{
// Load Donors
FILE *df = fopen("donors.txt", "r");
if (df)
{
    Donor *newDonor;
    while (1)
    {
        newDonor = (Donor *)malloc(sizeof(Donor));
        if (fscanf(df, "%[^;];%[^;];%[^;];%d;%[^;];%[^;];%[^;];%d;%d;%d;%[^;];%d\n",
                  newDonor->idNumber, newDonor->mobileNumber,
                  newDonor->name, &newDonor->age,
                  newDonor->bloodGroup, newDonor->medicalCondition,
                  newDonor->lastDonationDate, &newDonor->totalDonationsLastYear,
                  &newDonor->isBlacklisted, &newDonor->priority,
                  newDonor->registrationLocation, &newDonor->totalDonation) == 12)
        {
            newDonor->next = donorHead;
            donorHead = newDonor;
        }
        else
        {
            free(newDonor);
            break; // Exit the loop if fscanf fails
        }
    }
    fclose(df);
}
    // Load Requests
FILE *rf = fopen("requests.txt", "r");
if (rf)
{
    Request *newRequest;
    while (1)
    {
        newRequest = (Request *)malloc(sizeof(Request));
        if (fscanf(rf, "%[^;];%[^;];%d;%[^;];%[^;];%d;%d;%[^\n]\n",
                  newRequest->requesterName, newRequest->bloodGroup,
                  &newRequest->unitsRequested, newRequest->location,
                  newRequest->requestDate, &newRequest->isFulfilled,
                  &newRequest->isCancelled, newRequest->donorId) == 8)
        {
            newRequest->next = requestHead;
            requestHead = newRequest;
        }
        else
        {
            free(newRequest);
            break;
        }
    }
    fclose(rf);
 }
}
void registerDonor()
{
    Donor *newDonor = (Donor *)malloc(sizeof(Donor));

    printf("Enter ID Number: ");
    scanf("%s", newDonor->idNumber);

    printf("Enter Mobile Number: ");
    scanf("%s", newDonor->mobileNumber);
    if (!isValidMobile(newDonor->mobileNumber))
    {
        printf("Invalid mobile number format.\n");
        free(newDonor);
        return;
    }
    if (isRegistered(newDonor->idNumber, newDonor->mobileNumber))
    {
        printf("ID or mobile number already registered.\n");
        free(newDonor);
        return;
    }
    printf("Enter Name: ");
    getchar();
    fgets(newDonor->name, sizeof(newDonor->name), stdin);
    newDonor->name[strcspn(newDonor->name, "\n")] = 0;

    printf("Enter Age: ");
    scanf("%d", &newDonor->age);
    if (newDonor->age < 18)
    {
        printf("Donor must be at least 18 years old.\n");
        free(newDonor);
        return;
    }

    printf("Enter Blood Group: ");
    scanf("%s", newDonor->bloodGroup);

    printf("Enter Medical Condition: ");
    getchar();
    fgets(newDonor->medicalCondition, sizeof(newDonor->medicalCondition), stdin);
    newDonor->medicalCondition[strcspn(newDonor->medicalCondition, "\n")] = 0;

    printf("Enter Last Donation Date (YYYY-MM-DD): ");
    scanf("%s", newDonor->lastDonationDate);

    printf("Enter Registration Location: ");
    getchar();
    fgets(newDonor->registrationLocation, sizeof(newDonor->registrationLocation), stdin);
    newDonor->registrationLocation[strcspn(newDonor->registrationLocation, "\n")] = 0;

    newDonor->totalDonationsLastYear = 0;
    newDonor->isBlacklisted = 0;
    newDonor->priority = 0;
    newDonor->totalDonation = 0;
    newDonor->next = donorHead;
    donorHead = newDonor;

    saveDataToFile();

    printf("Donor registered successfully.\n");
    printf("Press Enter to continue...\n");
    getchar();
    clearScreen();
}

void createRequest()
{
    Request *newRequest = (Request *)malloc(sizeof(Request));
    time_t t = time(NULL);
    struct tm tm = *localtime(&t);

    strftime(newRequest->requestDate, sizeof(newRequest->requestDate), "%Y-%m-%d", &tm);

    printf("Enter Requester's Name: ");
    getchar();
    fgets(newRequest->requesterName, sizeof(newRequest->requesterName), stdin);
    newRequest->requesterName[strcspn(newRequest->requesterName, "\n")] = 0;

    printf("Enter Blood Group: ");
    scanf("%s", newRequest->bloodGroup);

    printf("Enter Units Requested: ");
    scanf("%d", &newRequest->unitsRequested);

    printf("Enter Location: ");
    getchar();
    fgets(newRequest->location, sizeof(newRequest->location), stdin);
    newRequest->location[strcspn(newRequest->location, "\n")] = 0;

    newRequest->isFulfilled = 0;
    newRequest->isCancelled = 0;
    strcpy(newRequest->donorId, ""); // No donor assigned yet

    newRequest->next = requestHead;
    requestHead = newRequest;

    saveDataToFile();

    printf("Request created successfully with Date: %s\n", newRequest->requestDate);
    printf("Press Enter to continue...\n");
    getchar();
    clearScreen();
}

void showRequests()
{
    printf("\n--- All Requests ---\n");
    Request *current = requestHead;
    int id = 0;

    while (current != NULL)
    {
        if (!current->isCancelled)
        {
            printf("Request ID: %d | Name: %s | Blood Group: %s | Units: %d | Location: %s | Status: %s\n",
                   id, current->requesterName, current->bloodGroup,
                   current->unitsRequested, current->location,
                   current->isFulfilled ? "Fulfilled" : "Pending");
        }
        current = current->next;
        id++;
    }

    printf("Press Enter to continue...\n");
    getchar();
    getchar();
    clearScreen();
}

void cancelRequest()
{
    int id;
    printf("Enter Request ID to cancel: ");
    scanf("%d", &id);

    Request *current = requestHead;
    int index = 0;

    while (current != NULL)
    {
        if (index == id)
            break;
        current = current->next;
        index++;
    }
    if (current == NULL || current->isCancelled)
    {
        printf("Invalid request ID or already cancelled.\n");
        return;
    }
    printf("Request Details:\n");
    printf("Name: %s\n", current->requesterName);
    printf("Blood Group: %s\n", current->bloodGroup);
    printf("Location: %s\n", current->location);

    current->isCancelled = 1;
    saveDataToFile();

    printf("Request cancelled successfully.\n");
    printf("Press Enter to continue...\n");
    getchar();
    getchar();
    clearScreen();
}

void blacklistDonor() {
    char searchID[20];
    printf("Enter Donor ID to blacklist: ");
    scanf(" %[^\n]", searchID);
    Donor *current = donorHead;
    int found = 0;

    while (current != NULL) {
        if (strcmp(current->idNumber, searchID) == 0) {
            current->isBlacklisted = 1;
            found = 1;
            printf("\n--- Donor Blacklisted ---\n");
            printf("ID Number:                %s\n", current->idNumber);
            printf("Name:                     %s\n", current->name);
            printf("Blood Group:              %s\n", current->bloodGroup);
            printf("Mobile Number:            %s\n", current->mobileNumber);
            printf("Status:                   Blacklisted.\n");
            break;
        }
        current = current->next;
    }
    if (!found) {
        printf("Donor ID %s not found in donor list.\n", searchID);
    } else {
        saveDataToFile();  // Ensure the updated blacklist status is saved
    }
    printf("Press Enter to continue...\n");
    getchar();
    getchar();
    clearScreen();
}

void showBlacklist()
 {
    Donor *current = donorHead;
    int count = 0;

    printf("--- Blacklisted Donors ---\n");
    printf("%-5s %-25s %-15s %-15s\n", "No", "Name", "Blood Group", "Mobile Number");
    printf("---------------------------------------------------------------\n");
    while (current != NULL) {
        if (current->isBlacklisted) {
            printf("%-5d %-25s %-15s %-15s\n", ++count, current->name, current->bloodGroup, current->mobileNumber);
        }
        current = current->next;
    }
    if (count == 0) {
        printf("No donors currently in blacklist.\n");
    }
    printf("Press Enter to continue...\n");
    getchar();
    getchar();
    clearScreen();
}

void removeFromBlacklist() {
    char searchID[20];
    printf("Enter Donor ID to remove from blacklist: ");
    scanf(" %[^\n]", searchID);

    Donor *current = donorHead;
    int found = 0;

    while (current != NULL) {
        if (strcmp(current->idNumber, searchID) == 0 && current->isBlacklisted) {
            current->isBlacklisted = 0;
            found = 1;

            printf("\n--- Donor Removed from Blacklist ---\n");
            printf("ID Number:                %s\n", current->idNumber);
            printf("Name:                     %s\n", current->name);
            printf("Age:                      %d\n", current->age);
            printf("Blood Group:              %s\n", current->bloodGroup);
            printf("Mobile Number:            %s\n", current->mobileNumber);
            printf("Address:                  %s\n", current->registrationLocation);
            printf("Medical Condition:        %s\n", current->medicalCondition);
            printf("Last Donation Date:       %s\n", current->lastDonationDate);
            printf("Total Donations (Last Yr): %d\n", current->totalDonationsLastYear);
            printf("Total Donation:           %d\n", current->totalDonation);
            printf("Priority:                 %d\n", current->priority);
            printf("Status:                   Restored to general donor list.\n");

            break;
        }
        current = current->next;
    }
    if (!found) {
        printf("Donor ID %s not found in blacklist.\n", searchID);
    } else {
        saveDataToFile();
    }

    printf("Press Enter to continue...\n");
    getchar();
    getchar();
    clearScreen();
}

void confirmDonation(int requestId, char requesterName[])
{
    Request *reqPtr = requestHead;
    Donor *donorPtr = donorHead;
    int currentIndex = 0;

    // Traverse to the request with given ID
    while (reqPtr != NULL && currentIndex < requestId)
    {
        reqPtr = reqPtr->next;
        currentIndex++;
    }

    if (reqPtr == NULL || reqPtr->isCancelled || reqPtr->isFulfilled)
    {
        printf("Invalid or already processed request.\n");
        return;
    }

    if (strcmp(reqPtr->requesterName, requesterName) != 0)
    {
        printf("Only the requester can confirm the blood donation.\n");
        return;
    }

    // Search for the donor by ID
    while (donorPtr != NULL)
    {
        if (strcmp(donorPtr->idNumber, reqPtr->donorId) == 0)
            break;
        donorPtr = donorPtr->next;
    }
    if (donorPtr == NULL)
    {
        printf("Donor not found.\n");
        return;
    }

    reqPtr->isFulfilled = 1;
    reqPtr->isCancelled = 1;

    donorPtr->totalDonationsLastYear++;

    time_t now = time(NULL);
    struct tm *t = localtime(&now);
    strftime(donorPtr->lastDonationDate, sizeof(donorPtr->lastDonationDate), "%Y-%m-%d", t);
    saveDataToFile();
    saveDataToFile();

    printf("Donation confirmed. Request ID %d fulfilled by Donor ID %s.\n", requestId, donorPtr->idNumber);
    printf("Request has been auto-deleted after fulfillment.\n");
}

void searchDonorByLocationBloodGroupEligibility()
{
    char location[50], bloodGroup[5];
    int found = 0;
    char today[] = "2025-04-10"; // Static date for demo

    // Input location
    printf("Enter Location: ");
    getchar();
    fgets(location, sizeof(location), stdin);
    location[strcspn(location, "\n")] = 0;

    // Input blood group
    printf("Enter Blood Group: ");
    scanf("%s", bloodGroup);

    printf("\n--- Eligible Donors ---\n");
    Donor *current = donorHead;
    while (current != NULL)
    {
        if (current->isBlacklisted)
        {
            current = current->next;
            continue;
        }

        // Check if location and blood group match and donor is eligible
        if (strcmp(current->bloodGroup, bloodGroup) == 0 &&
            strstr(current->registrationLocation, location) != NULL &&
            daysBetween(current->lastDonationDate, today) >= 120)
        {
            printf("ID: %s | Name: %s | Age: %d | Blood Group: %s | Mobile: %s | Location: %s\n",
                   current->idNumber, current->name, current->age,
                   current->bloodGroup, current->mobileNumber, current->registrationLocation);
            found = 1;
        }

        current = current->next;
    }

    if (!found)
    {
        printf("No eligible donors found.\n");
    }

    printf("Press Enter to continue...\n");
    getchar();
    getchar();
    clearScreen();
}

void showDonorList()
{
    printf("\n--- All Registered Donors ---\n");

    Donor *current = donorHead;
    if (!current) {
        printf("No donors registered.\n");
    }

    while (current != NULL)
    {
        printf("ID: %s | Name: %s | Age: %d | Blood Group: %s | Mobile: %s | Location: %s\n",
               current->idNumber, current->name, current->age,
               current->bloodGroup, current->mobileNumber, current->registrationLocation);
        current = current->next;
    }

    printf("Press Enter to continue...\n");
    getchar();
    getchar();
    clearScreen();
}

void editDonorInfo()
{
    char id[20];
    printf("Enter Donor ID to edit: ");
    scanf("%s", id);
    Donor *current = donorHead;
    while (current != NULL)
    {
        if (strcmp(current->idNumber, id) == 0)
        {
            printf("Editing Donor %s\n", current->name);

            printf("Enter New Name: ");
            getchar();
            fgets(current->name, sizeof(current->name), stdin);
            current->name[strcspn(current->name, "\n")] = 0;

            printf("Enter New Age: ");
            scanf("%d", &current->age);

            printf("Enter New Blood Group: ");
            scanf("%s", current->bloodGroup);

            printf("Enter New Mobile Number: ");
            scanf("%s", current->mobileNumber);
            if (!isValidMobile(current->mobileNumber))
            {
                printf("Invalid mobile number format. Changes aborted.\n");
                return;
            }

            printf("Enter New Medical Condition: ");
            getchar();
            fgets(current->medicalCondition, sizeof(current->medicalCondition), stdin);
            current->medicalCondition[strcspn(current->medicalCondition, "\n")] = 0;

            printf("Enter New Last Donation Date (YYYY-MM-DD): ");
            scanf("%s", current->lastDonationDate);

            printf("Enter New Registration Location: ");
            getchar();
            fgets(current->registrationLocation, sizeof(current->registrationLocation), stdin);
            current->registrationLocation[strcspn(current->registrationLocation, "\n")] = 0;

            saveDataToFile();
            printf("Donor information updated successfully.\n");
            break;
        }
        current = current->next;
    }
    if (current == NULL)
    {
        printf("Donor ID not found.\n");
    }

    printf("Press Enter to continue...\n");
    getchar();
    getchar();
    clearScreen();
}

void deleteDonor()
{
    char idToDelete[20];
    Donor *current = donorHead;
    Donor *prev = NULL;
    int found = 0;

    printf("Enter Donor ID to delete: ");
    scanf("%s", idToDelete);
    while (current != NULL)
    {
        if (strcmp(current->idNumber, idToDelete) == 0)
        {
            found = 1;

            // Display donor info before deletion
            printf("\nDonor Information:\n");
            printf("ID: %s\n", current->idNumber);
            printf("Name: %s\n", current->name);
            printf("Blood Group: %s\n", current->bloodGroup);
            printf("Age: %d\n", current->age);
            printf("Phone: %s\n", current->mobileNumber);
            printf("Location: %s\n", current->registrationLocation);

            // Confirm deletion
            char confirm;
            printf("\nAre you sure you want to delete this donor? (y/n): ");
            getchar(); // Clear newline
            scanf("%c", &confirm);

            if (confirm == 'y' || confirm == 'Y')
            {
                if (prev == NULL)
                {
                    // First node
                    donorHead = current->next;
                }
                else
                {
                    prev->next = current->next;
                }

                free(current);
                saveDataToFile(); // Write updated linked list to file
                printf("Donor with ID %s deleted successfully.\n", idToDelete);
            }
            else
            {
                printf("Deletion cancelled.\n");
            }

            break;
        }

        prev = current;
        current = current->next;
    }

    if (!found)
    {
        printf("No donor found with ID %s.\n", idToDelete);
    }

    printf("Press Enter to continue...\n");
    getchar();
    getchar();
    clearScreen();
}

int main()
{
    int choice;
    char donorId[20];
    int reqId;
    loadDataFromFile();
    do
    {
        printf("\n===== Blood Bank Management System =====\n");
        printf("1. Register Donor\n");
        printf("2. Create Blood Request\n");
        printf("3. Show All Requests\n");
        printf("4. Cancel Blood Request\n");
        printf("5. Show All Donors\n");
        printf("6. Blacklist Donor\n");
        printf("7. Show All Blacklist\n");
        printf("8. Remove From Black List\n");
        printf("9. Search Donor\n");
        printf("10. Delete Donor \n");
        printf("11. Confirm Blood Donation\n");
        printf("12. Edit Donor Info\n");
        printf("13. Exit\n");
        printf("Enter your choice: ");
        scanf("%d", &choice);
    switch (choice)
        {
        case 1:registerDonor();break;
        case 2:createRequest();break;
        case 3:showRequests();break;
        case 4:cancelRequest();break;
        case 5:showDonorList();break;
        case 6:blacklistDonor();break;
        case 7:showBlacklist();break;
        case 8:removeFromBlacklist();break;
        case 9:
            searchDonorByLocationBloodGroupEligibility();
            break;
        case 10:deleteDonor();break;
        case 11:
            printf("Enter Request ID: ");
            scanf("%d", &reqId);
            printf("Enter Donor ID: ");
            scanf("%s", donorId);
            confirmDonation(reqId, donorId);
            break;
        case 12:editDonorInfo();break;
        case 13:
            printf("Exiting program.\n");
            break;
        default:
            printf("Invalid choice.\n");
        }
    }while (choice != 13);  // Exit loop when choice is 13
    return 0;
}

Editor is loading...
Leave a Comment