DEV Community

NIYOMUNGELI Aline
NIYOMUNGELI Aline

Posted on

cpp-ne

#include <iostream>
#include <string>
#include <unordered_set>
#include <limits>
#include <ctime>

using namespace std;

// Structure of patients linked list
struct Patient {
    int patient_id;
    string name;
    string dob;
    string gender;
    Patient* next;
};

// Structure for doctors linked list
struct Doctor {
    int doctor_id;
    string name;
    string specialization;
    Doctor* next;
};

// Structure for appointments linked list
struct Appointment {
    int appointment_id;
    int patient_id;
    int doctor_id;
    string appointment_date;
    Appointment* next;
};

// Linked list heads
Patient* patientsHead = nullptr;
Doctor* doctorsHead = nullptr;
Appointment* appointmentsHead = nullptr;

// Set to keep track of existing IDs
unordered_set<int> patient_ids;
unordered_set<int> doctor_ids;
unordered_set<int> appointment_ids;

// Function prototypes
void registerPatient();
void registerDoctor();
void registerAppointment();
void displayPatients();
void displayDoctors();
void displayAppointments();
void displayMenu();

// Helper function to get the current date
string getCurrentDate() {
    time_t t = time(nullptr);
    tm* tm = localtime(&t);
    char buffer[11];
    strftime(buffer, sizeof(buffer), "%Y-%m-%d", tm);
    return string(buffer);
}

// Function to validate date formats (YYYY-MM-DD) and ensure the date is not in the future
bool isValidDateFormat(const string& date) {
    if (date.length() != 10)  // Check length
        return false;
    if (date[4] != '-' || date[7] != '-')  // Check if dashes are at correct positions
        return false;
    // Validate year, month, and day
    try {
        int year = stoi(date.substr(0, 4));
        int month = stoi(date.substr(5, 2));
        int day = stoi(date.substr(8, 2));
        if (month < 1 || month > 12 || day < 1 || day > 31)
            return false;
        // Additional checks for specific months can be added here
        string currentDate = getCurrentDate();
        if (date > currentDate)
            return false;
    } catch (const exception&) {
        return false;
    }
    return true;
}

// Function to validate name input (ensure it is a non-empty string)
bool isValidName(const string& name) {
    if (name.empty()) return false;
    for (char c : name) {
        if (!isalpha(c) && !isspace(c)) return false;
    }
    return true;
}

// Function to register a new patient
void registerPatient() {
    int id;
    string name, dob, gender;
    cout << "PATIENT REGISTRATION" << endl;
    cout << "--------------------" << endl;
    cout << "Enter patient ID: ";
    while (!(cin >> id)) {
        cout << "Invalid input. Please enter a valid integer for patient ID: ";
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
    }
    if (patient_ids.find(id) != patient_ids.end()) {
        cout << "Patient ID already exists!" << endl;
        return;
    }
    cout << "Enter patient name: ";
    cin.ignore();
    getline(cin, name);
    while (!isValidName(name)) {
        cout << "Invalid name. Please enter a valid name: ";
        getline(cin, name);
    }
    cout << "Enter patient DOB (YYYY-MM-DD): ";
    getline(cin, dob);
    while (!isValidDateFormat(dob)) {
        cout << "Invalid date format or future date. Please enter date in YYYY-MM-DD format: ";
        getline(cin, dob);
    }
    cout << "Enter patient gender: ";
    getline(cin, gender);
    // Create the new patient node
    Patient* newPatient = new Patient{id, name, dob, gender, patientsHead};
    // Update patientsHead to point to the new node
    patientsHead = newPatient;
    // Insert ID into set
    patient_ids.insert(id);
    cout << "Patient registered successfully." << endl;
}

// Function to register a new doctor
void registerDoctor() {
    int id;
    string name, specialization;
    cout << "DOCTOR REGISTRATION" << endl;
    cout << "-------------------" << endl;
    cout << "Enter doctor ID: ";
    while (!(cin >> id)) {
        cout << "Invalid input. Please enter a valid integer for doctor ID: ";
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
    }
    if (doctor_ids.find(id) != doctor_ids.end()) {
        cout << "Doctor ID already exists!" << endl;
        return;
    }
    cout << "Enter doctor name: ";
    cin.ignore();
    getline(cin, name);
    while (!isValidName(name)) {
        cout << "Invalid name. Please enter a valid name: ";
        getline(cin, name);
    }
    cout << "Enter doctor's specialization: ";
    getline(cin, specialization);
    // Create the new doctor node
    Doctor* newDoctor = new Doctor{id, name, specialization, doctorsHead};
    // Update doctorsHead to point to the new node
    doctorsHead = newDoctor;
    // Insert ID into set
    doctor_ids.insert(id);
    cout << "Doctor registered successfully." << endl;
}

// Function to register a new appointment
void registerAppointment() {
    int appointment_id, patient_id, doctor_id;
    string appointment_date;
    cout << "APPOINTMENT REGISTRATION" << endl;
    cout << "------------------------" << endl;
    cout << "Enter appointment ID: ";
    while (!(cin >> appointment_id)) {
        cout << "Invalid input. Please enter a valid integer for appointment ID: ";
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
    }
    if (appointment_ids.find(appointment_id) != appointment_ids.end()) {
        cout << "Appointment ID already exists!" << endl;
        return;
    }
    cout << "Enter patient ID: ";
    while (!(cin >> patient_id)) {
        cout << "Invalid input. Please enter a valid integer for patient ID: ";
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
    }
    if (patient_ids.find(patient_id) == patient_ids.end()) {
        cout << "Patient ID does not exist!" << endl;
        return;
    }
    cout << "Enter doctor ID: ";
    while (!(cin >> doctor_id)) {
        cout << "Invalid input. Please enter a valid integer for doctor ID: ";
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
    }
    if (doctor_ids.find(doctor_id) == doctor_ids.end()) {
        cout << "Doctor ID does not exist!" << endl;
        return;
    }
    cout << "Enter appointment date (YYYY-MM-DD): ";
    cin.ignore();
    getline(cin, appointment_date);
    while (!isValidDateFormat(appointment_date)) {
        cout << "Invalid date format or future date. Please enter date in YYYY-MM-DD format: ";
        getline(cin, appointment_date);
    }
    // Create the new appointment node
    Appointment* newAppointment = new Appointment{appointment_id, patient_id, doctor_id, appointment_date, appointmentsHead};
    // Update appointmentsHead to point to the new node
    appointmentsHead = newAppointment;
    // Insert ID into set
    appointment_ids.insert(appointment_id);
    cout << "Appointment registered successfully." << endl;
}

// Function to display all registered patients
void displayPatients() {
    Patient* current = patientsHead;
    if (!current) {
        cout << "No patients registered." << endl;
        return;
    }
    cout << "DISPLAYING PATIENTS" << endl;
    cout << "-------------------" << endl;
    while (current != nullptr) {
        cout << "ID: " << current->patient_id << ", Name: " << current->name
             << ", DOB: " << current->dob << ", Gender: " << current->gender << endl;
        current = current->next;
    }
}

// Function to display all registered doctors
void displayDoctors() {
    Doctor* current = doctorsHead;
    if (!current) {
        cout << "No doctors registered." << endl;
        return;
    }
    cout << "DISPLAYING DOCTORS" << endl;
    cout << "------------------" << endl;
    while (current != nullptr) {
        cout << "ID: " << current->doctor_id << ", Name: " << current->name
             << ", Specialization: " << current->specialization << endl;
        current = current->next;
    }
}

// Function to display all registered appointments
void displayAppointments() {
    Appointment* current = appointmentsHead;
    if (!current) {
        cout << "No appointments registered." << endl;
        return;
    }
    cout << "DISPLAYING APPOINTMENTS" << endl;
    cout << "-----------------------" << endl;
    while (current != nullptr) {
        cout << "Appointment ID: " << current->appointment_id << ", Patient ID: " << current->patient_id
             << ", Doctor ID: " << current->doctor_id << ", Date: " << current->appointment_date << endl;
        current = current->next;
    }
}

// Function to display the main menu
void displayMenu() {
    cout << "Menu: " << endl;
    cout << "1. Register a patient" << endl;
    cout << "2. Register a doctor" << endl;
    cout << "3. Register an appointment" << endl;
    cout << "4. Display Patients" << endl;
    cout << "5. Display Doctors" << endl;
    cout << "6. Display Appointments" << endl;
    cout << "7. Exit" << endl;
    cout << "Enter your choice: " << endl;
}

// Main function
int main() {
    cout << "WELCOME TO RUHENGELI REFERRAL HOSPITAL PLATFORM" << endl;
    cout << "----------------------------------------------" << endl;
    cout << endl;

    int choice;
    do {
        displayMenu();
        while (!(cin >> choice)) {
            cout << "Invalid input. Please enter a valid choice (1-7): ";
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
        }
        switch (choice) {
            case 1:
                registerPatient();
                break;
            case 2:
                registerDoctor();
                break;
            case 3:
                registerAppointment();
                break;
            case 4:
                displayPatients();
                break;
            case 5:
                displayDoctors();
                break;
            case 6:
                displayAppointments();
                break;
            case 7:
                cout << "Thank you for using Ruhengeli Referral Hospital Platform!" << endl;
                cout << "Have a nice day!" << endl;
                break;
            default:
                cout << "Invalid choice. Please try again." << endl;
        }
    } while (choice != 7);

    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)