/* Created By: Noor Hannani Syamimi Binti Mohd Suffian
   Created On: 28/12/2021
   Program Name: TEST 1 - PRACTICAL 2017/2018 
*/

/* LIBRARY */
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

/*FUNCTION PROTOTYPE*/
int getProblem();
void getRateDropFactor(int &); //function prototype with parameter is a reference variable 
void KgRateConc(int &); //function prototype with parameter is a reference variable 
double figDropsMin(double, double);
double byWeight(double, double, double);

//**************************************************************
//* Definition of function getProblem.                         *
//* This function display user menu and return user input      *
//**************************************************************
int getProblem()
{
	int number; //variable to hold user choice
	
    // Displaying the menu to user     
	cout << "\t\tINTRAVENOUS RATE ASSISTANT " << endl;
	cout << "\n\n";
	
	cout << "\t\tEnter the number of the problem you wish to solve." << endl;
	cout << "\n";
	cout << "\t\t     GIVEN A MEDICAL ORDER IN            CALCULATE RATE IN" << endl;
	cout << "\t\t (1) ml/hr & tubing drop factor              drops/min" << endl;
	cout << "\t\t (2) mg/kg/hr & concentration in mg/ml         ml/hr" << endl;
	cout << "\t\t (3) QUIT" << endl;
	
	//ask user to enter input (number of choice)
	cout << "\nPlease enter your choice: ";
	cin >> number;
	return number; //returning the 'number' value to main function
}

int main()
{
	//variable 
	int choice, dropmin, mlhr;
	
	//using post-test loop to validate user input (choice)
	do
	{
		choice = getProblem(); //get the choice and display the menu at the same time
		
		cout << "\nProblem => " << choice << endl; //displaying the user's choice
		
		//validate user choice if input out of choice range
		if (choice < 1 || choice > 3)
		{
			//informing the user that the input is out of range
			cout << "\nPlease run the system again and choose a problem number between 1 and 3.";
		    exit(0); //terminates the execution of the program immediately
		}
		
		//validate the user's input
		else if (choice != 3) // if the input is not 3 then one of the selection below will be executed
		{
			if (choice == 1) //selection 1
		    {
				getRateDropFactor(dropmin); //calling function getRateDropFactor
				cout << "The drop rate per minute is " << dropmin << " drops/min" << endl << endl; //displaying the output of getRateDropFactor
		    }
		    
		    else if (choice == 2) //selection 2
		    {
				KgRateConc(mlhr); //calling function getRateDropFactor
				cout << "The rate in millilitres per hour is " << mlhr << " ml/hr" << endl << endl; //displaying the output of KgRateConc
		    } 
		}
		
	}while (choice != 3); //if result is true, then the  program will perform the process in the loop again
	
	//once the program exit from loop it will display these messages.
	cout << "You have chosen to quit the program." << endl; 
	cout << "Thank you for using our system.";
	
	return 0;
}

//**************************************************************
//* Definition of function getRateDRopFactor.                  *
//* This function ask user to enter rate and drop              *
//* Call function figDropsMin to calculate drop rate           *
//**************************************************************
void getRateDropFactor(int &droprate)
{
	double rate, drop; 
	
	cout << "Enter rate in ml/hr => ";
	cin >> rate;

	cout << "Enter tubing's drop factor (drop/ml) => ";
	cin >> drop;
		
	droprate = figDropsMin(rate, drop);
}

//***********************************************************************
//* Definition of function figDropsMin.                                 *
//* This function receive rate and drop from getRateDropFactor function *
//* Calculate total rate and round it up                                *
//***********************************************************************
double figDropsMin (double ra,double dr)
{
	double t_rate, t_drra;
	
	t_rate = 60 / ra;
	t_drra = dr / t_rate;
	
	return round(t_drra);
}

//**************************************************************
//* Definition of function KgRateConc.                         *
//* This function display user menu and return user input      *
//**************************************************************
void KgRateConc(int &t_concentration)
{
	double rate, weight, concentration;
	
	cout << "Enter rate in mg/hr => ";
	cin >> rate;
	
	cout << "Enter patient weight in kg => ";
	cin >> weight;
	
	cout << "Enter concentration in mg/ml => ";
	cin >> concentration;
	
	t_concentration = byWeight(rate, weight, concentration);
}

//**************************************************************
//* Definition of function getProblem.                         *
//* This function display user menu and return user input      *
//**************************************************************
double byWeight(double mghr, double kg, double mgml)
{
	double t_weight;
	
	t_weight = mghr * kg * mgml;
	
	return round(t_weight);
}



