Untitled

 avatar
unknown
plain_text
11 days ago
12 kB
1
Indexable
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#define MAX_DONORS 100
#define MAX_REQUESTS 100
#define FILE_NAME "request_stats.txt"

// Structure for Donor
typedef struct {
    char idNumber[20];
    char mobileNumber[15];
    char name[50];
    int age;
    char bloodGroup[5];
    char medicalCondition[50]; // e.g., "Diabetes", "None"
    char lastDonationDate[15]; // YYYY-MM-DD
    int totalDonationsLastYear;
    int isBlacklisted; // 0 = No, 1 = Yes
    int priority; // 0 = No, 1 = Yes (for old donors)
    char registrationLocation[50]; // Donor's registration location
} Donor;

// Structure for Request
typedef struct {
    char requesterName[50];
    char bloodGroup[5];
    int unitsRequested; // Number of blood bags
    char location[50];
    int isFulfilled; // 0 = Not Fulfilled, 1 = Fulfilled
    int isCancelled; // 0 = Not Cancelled, 1 = Cancelled
} Request;

Donor donors[MAX_DONORS];
Request requests[MAX_REQUESTS];

int totalDonors = 0;
int totalRequests = 0;
int totalFulfilledRequests = 0;

// Function to clear screen based on the operating system
void clearScreen() {
    #ifdef _WIN32
        system("cls"); // Windows এর জন্য
    #else
        system("clear"); // Linux/Mac এর জন্য
    #endif
}

//  Helper: Validate mobile number format
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;
}

// Custom function to parse date strings (YYYY-MM-DD) into a tm structure
int customStrptime(const char *date, struct tm *tm) {
    // Parse date string (YYYY-MM-DD)
    if (sscanf(date, "%4d-%2d-%2d", &tm->tm_year, &tm->tm_mon, &tm->tm_mday) != 3) {
        return -1; // Error parsing the date string
    }

    // Adjust year and month for tm structure
    tm->tm_year -= 1900; // tm_year is years since 1900
    tm->tm_mon -= 1; // tm_mon is months since January (0-11)

    return 0; // Success
}

// Function to calculate the days between two dates (YYYY-MM-DD)
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; // Invalid date
    }

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

    if (t1 == -1 || t2 == -1) {
        printf("Error converting time.\n");
        return -1;
    }

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

// 🔁 Check if ID or Mobile is already registered
int isRegistered(char id[], char mobile[]) {
    for (int i = 0; i < totalDonors; i++) {
        if (strcmp(donors[i].idNumber, id) == 0 || strcmp(donors[i].mobileNumber, mobile) == 0) {
            return 1;
        }
    }
    return 0;
}

// Register a donor
void registerDonor() {
    Donor newDonor;
    printf("Enter ID Number: ");
    scanf("%s", newDonor.idNumber);

    printf("Enter Mobile Number: ");
    scanf("%s", newDonor.mobileNumber);

    if (!isValidMobile(newDonor.mobileNumber)) {
        printf("Invalid Mobile Number! Must be 11 digits and start with 01...\n");
        return;
    }

    if (isRegistered(newDonor.idNumber, newDonor.mobileNumber)) {
        printf("ID or Mobile number already registered.\n");
        return;
    }

    printf("Enter Name: ");
    getchar(); // To clear newline character
    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");
        return;
    }

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

    printf("Enter Medical Condition (None if not applicable): ");
    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;

    donors[totalDonors++] = newDonor;
    printf("Donor Registered Successfully!\n");

    printf("Press any key to continue...\n");
    getchar();  // To catch the newline character after pressing enter
    clearScreen();  // Clear the screen
}

// Create a blood request
void createRequest() {
    Request newRequest;
    printf("Enter Requester's Name: ");
    getchar(); // To clear newline character
    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(); // To clear newline character
    fgets(newRequest.location, sizeof(newRequest.location), stdin);
    newRequest.location[strcspn(newRequest.location, "\n")] = 0;

    newRequest.isFulfilled = 0;
    newRequest.isCancelled = 0;

    requests[totalRequests++] = newRequest;
    printf("Request Created Successfully!\n");

    printf("Press any key to continue...\n");
    getchar();  // To catch the newline character after pressing enter
    clearScreen();  // Clear the screen
}

// Show requests
void showRequests() {
    printf("\nAll Requests:\n");
    for (int i = 0; i < totalRequests; i++) {
        if (requests[i].isCancelled) continue;
        printf("Request ID: %d | Name: %s | Blood: %s | Units: %d | Location: %s | Status: %s\n",
            i, requests[i].requesterName, requests[i].bloodGroup,
            requests[i].unitsRequested, requests[i].location,
            requests[i].isFulfilled ? "Fulfilled" : "Pending");
    }

    printf("Press any key to continue...\n");
    getchar();  // To catch the newline character after pressing enter
    clearScreen();  // Clear the screen
}

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

    if (requestId < 0 || requestId >= totalRequests || requests[requestId].isCancelled) {
        printf("Invalid Request ID or already cancelled.\n");
        return;
    }

    requests[requestId].isCancelled = 1;
    printf("Request Cancelled!\n");

    printf("Press any key to continue...\n");
    getchar();  // To catch the newline character after pressing enter
    clearScreen();  // Clear the screen
}

//  Blacklist Donor
void blacklistDonor() {
    char donorId[20];
    printf("Enter Donor ID to blacklist: ");
    scanf("%s", donorId);

    for (int i = 0; i < totalDonors; i++) {
        if (strcmp(donors[i].idNumber, donorId) == 0) {
            donors[i].isBlacklisted = 1;
            printf("Donor blacklisted successfully!\n");
            return;
        }
    }

    printf("Donor not found.\n");
}

//  Search Donor by Location, Blood Group, and Eligibility (120 Days)
void searchDonorByLocationBloodGroupEligibility() {
    char searchLocation[50], searchBloodGroup[5];
    int found = 0;

    // Prompt for location and blood group
    printf("Enter Donor Location to search: ");
    getchar(); // To clear any leftover newline character
    fgets(searchLocation, sizeof(searchLocation), stdin);
    searchLocation[strcspn(searchLocation, "\n")] = 0; // Remove newline character

    printf("Enter Blood Group: ");
    scanf("%s", searchBloodGroup);

    printf("\n--- Search Results ---\n");

    // Search donors by location and blood group, then check eligibility
    for (int i = 0; i < totalDonors; i++) {
        // Check if the donor matches the location and blood group
        if (strstr(donors[i].registrationLocation, searchLocation) != NULL && strcmp(donors[i].bloodGroup, searchBloodGroup) == 0) {
            // Check if the donor is eligible (120 days since last donation)
            if (daysBetween(donors[i].lastDonationDate, "2025-04-10") >= 120) {
                // Print donor details
                printf("ID: %s\n", donors[i].idNumber);
                printf("Mobile: %s\n", donors[i].mobileNumber);
                printf("Name: %s\n", donors[i].name);
                printf("Age: %d\n", donors[i].age);
                printf("Blood Group: %s\n", donors[i].bloodGroup);
                printf("Medical Condition: %s\n", donors[i].medicalCondition);
                printf("Last Donation Date: %s\n", donors[i].lastDonationDate);
                printf("Registration Location: %s\n", donors[i].registrationLocation);
                printf("Total Donations Last Year: %d\n", donors[i].totalDonationsLastYear);
                printf("Priority: %s\n", donors[i].priority == 1 ? "Yes" : "No");
                printf("\n---\n");
                found = 1;
            }
        }
    }

    if (!found) {
        printf("No donors found for the specified location, blood group, and eligibility (120 days).\n");
    }

    printf("Press any key to continue...\n");
    getchar();  // To catch the newline character after pressing enter
    clearScreen();  // Clear the screen
}


//  Show Donor List
void showDonorList() {
    printf("\nAll Registered Donors:\n");
    for (int i = 0; i < totalDonors; i++) {
        printf("ID: %s | Name: %s | Age: %d | Blood: %s | Mobile: %s | Location: %s\n",
            donors[i].idNumber, donors[i].name, donors[i].age,
            donors[i].bloodGroup, donors[i].mobileNumber, donors[i].registrationLocation);
    }
}

int main() {
    int choice;

    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. Blacklist Donor\n");
        printf("6. Search Donor\n");
        printf("7. Show All Donors\n");
        printf("8. 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:
                blacklistDonor();
                break;
            case 6:
                searchDonorByLocationBloodGroupEligibility();
                break;
            case 7:
                showDonorList();
                break;
            case 8:
                printf("Exiting program...\n");
                break;
            default:
                printf("Invalid choice, try again.\n");
        }
    } while (choice != 8);

    return 0;
}
Editor is loading...
Leave a Comment