Data-Structures-and-Algorithms / Triage / Your_Main / main.cpp
main.cpp
Raw
/*  File Name: main.cpp
	Author: Essam Fahmy
	Date: 2023-11-01

	Main class that uses patient.h and priorityqueue.h to create the queue console program
*/

#include "Patient.h"
#include "PriorityQueue.h"
#include <string>
#include <iostream>
#include <fstream>
#include <sstream>

PriorityQueue<Patient> queue;
LinkedList<Patient> processedPatients;

using namespace std;

int readInteger(const string& prompt) {
	int value;
	cout << prompt;
	while (!(cin >> value)) {
		cout << "Invalid input. Please enter an integer number: ";
		cin.clear();
		cin.ignore(numeric_limits<streamsize>::max(), '\n');
	}
	cin.ignore(numeric_limits<streamsize>::max(), '\n');
	return value;
}

void add_patient()
{
	string input;

	cout << "Enter patient name: ";
	cin.ignore();
	getline(cin, input);

	Patient newPatient(input);

	cout << "\tEnter ailment (leave blank when done): ";
	while (getline(cin, input) && !input.empty())
	{
		int severity = readInteger("\tEnter severity: ");
		int timeCriticality = readInteger("\tEnter time criticality: ");
		int contagiousness = readInteger("\tEnter contagiousness: ");

		Ailment newAilment(input, severity, timeCriticality, contagiousness);
		newPatient.add_ailment(newAilment);

		cout << "\nEnter next ailment (leave blank when done): ";
	}

	queue.enqueue(newPatient);
}

void next_patient()
{
	if (queue.empty()) {
		cout << "No more patients in the queue." << endl;
		return;
	}

	Patient currentPatient = queue.dequeue();
	processedPatients.push_front(currentPatient);
	cout << currentPatient.get_name() << " moved to patient room!" << endl;

	if (!queue.empty()) {
		Patient nextPatient = queue.dequeue();
		cout << "Next in queue: " << nextPatient.get_name() << " with priority score " << nextPatient.get_score() << endl;
		queue.enqueue(nextPatient);
	}
	else {
		cout << "No more patients in the queue." << endl;
	}
}

void display_queue() {
	if (queue.empty()) {
		cout << "The queue is empty." << endl;
		return;
	}

	int position = 1;
	for (auto node = queue.begin(); node != nullptr; node = node->next) {
		Patient& patient = node->data;
		cout << "\nPosition " << position << ": " << patient.get_name()
			<< " - Priority Score: " << patient.get_score() << endl;

		auto ailmentNode = patient.get_ailments().begin();
		while (ailmentNode != nullptr) {
			const Ailment& ailment = ailmentNode->data;
			cout << "\tAilment: " << ailment.get_name()
				<< ", Severity: " << ailment.get_severity()
				<< ", Time Sensitivity: " << ailment.get_time_sensitivity()
				<< ", Contagiousness: " << ailment.get_contagiousness() << endl;
			ailmentNode = ailmentNode->next;
		}

		position++;
	}
}


void processed_patient_history() {
	if (processedPatients.empty()) {
		cout << "\nNo processed patients history available.\n" << endl;
		return;
	}

	for (auto node = processedPatients.begin(); node != nullptr; node = node->next) {
		Patient& patient = node->data;
		cout << "\nPatient Name: " << patient.get_name()
			<< " - Priority Score: " << patient.get_score() << endl;

		for (auto ailmentNode = patient.get_ailments().begin(); ailmentNode != nullptr; ailmentNode = ailmentNode->next) {
			const Ailment& ailment = ailmentNode->data;
			cout << "\tAilment: " << ailment.get_name()
				<< ", Severity: " << ailment.get_severity()
				<< ", Time Sensitivity: " << ailment.get_time_sensitivity()
				<< ", Contagiousness: " << ailment.get_contagiousness() << endl;
		}
	}
}

void load_queue() {
	string filePath;
	cout << "Enter path to the CSV file: ";
	cin >> filePath;

	ifstream file(filePath);
	if (!file.is_open()) {
		cout << "Unable to open file: " << filePath << endl;
		return;
	}

	// Skip the header line
	cin.clear();
	string header;
	if (!getline(file, header)) {
		cout << "Failed to read the header line or empty file." << endl;
		return;
	}

	string line;
	while (getline(file, line)) {
		stringstream ss(line);
		string name, numOfAilmentsStr, ailmentName, severityStr, timeCriticalityStr, contagiousnessStr;

		getline(ss, name, ',');
		getline(ss, numOfAilmentsStr, ',');

		try {
			int numOfAilments = stoi(numOfAilmentsStr);

			Patient patient(name);

			for (int i = 0; i < numOfAilments; ++i) {
				getline(ss, ailmentName, ',');
				getline(ss, severityStr, ',');
				getline(ss, timeCriticalityStr, ',');
				getline(ss, contagiousnessStr, ',');

				int severity = stoi(severityStr);
				int timeCriticality = stoi(timeCriticalityStr);
				int contagiousness = stoi(contagiousnessStr);

				Ailment ailment(ailmentName, severity, timeCriticality, contagiousness);
				patient.add_ailment(ailment);
			}

			queue.enqueue(patient);
		}
		catch (const std::invalid_argument& ia) {
			cerr << "Invalid argument: " << ia.what() << " in line: " << line << endl;
		}
	}

	cout << "\nPatients queue successfuly loaded from CSV file.\n" << endl;

	file.close();
}


void save_queue() {
	string filePath;
	cout << "Enter path to save the queue: ";
	cin >> filePath;

	ofstream file(filePath);
	if (!file.is_open()) {
		cout << "Unable to open file for writing: " << filePath << endl;
		return;
	}

	file << "Name,Number of Ailments,Name_1,Severity_1,Time_Criticality_1,Contagiousness_1,Name_2,Severity_2,Time_Criticality_2,Contagiousness_2\n";

	for (auto node = queue.begin(); node != nullptr; node = node->next) {
		Patient& patient = node->data;
		auto& ailments = patient.get_ailments();

		file << patient.get_name() << "," << ailments.size();

		for (auto ailmentNode = ailments.begin(); ailmentNode != nullptr; ailmentNode = ailmentNode->next) {
			const Ailment& ailment = ailmentNode->data;
			file << "," << ailment.get_name() << "," << ailment.get_severity()
				<< "," << ailment.get_time_sensitivity() << "," << ailment.get_contagiousness();
		}

		file << endl;
	}

	file.close();
	cout << "\nQueue saved to " << filePath << endl;
}



int main(int argc, char* argv[])
{
	int userInput = -1;
	while (true)
	{
		cout << "\n****************************************************" << endl;
		cout << "* Welcome to Fanshawe College Medical Center F2023 *" << endl;
		cout << "* Essam Fahmy                                      *" << endl;
		cout << "****************************************************\n" << endl;
		cout << "Please Make A Selection\n" << endl;
		cout << "\t1 - Add Patient\n"
			"\t2 - Process Next Patient In Queue\n"
			"\t3 - Display Queue\n"
			"\t4 - View Processed Patients History\n"
			"\t5 - Load Queue\n"
			"\t6 - Save Queue\n"
			"\t0 - Exit\n" << endl;

		cout << "Pease enter a number from 0 to 6: ";

		do {
			while (!(std::cin >> userInput)) {
				std::cout << "Invalid input. Please enter a number from 0 to 6: ";
				std::cin.clear();
				std::cin.ignore(10000, '\n');
			}
			if (userInput < 0 || userInput > 6)
			{
				cout << "Number Provided is not from 0-6, Try again: ";
			}
		} while (userInput < 0 || userInput > 6);

		switch (userInput) {
		case 0:
			exit(EXIT_SUCCESS);
		case 1:
			add_patient();
			break;
		case 2:
			next_patient();
			break;
		case 3:
			display_queue();
			break;
		case 4:
			processed_patient_history();
			break;
		case 5:
			load_queue();
			break;
		case 6:
			save_queue();
			break;
		}
	}
}