// The Beginning.

//                           SUPER PAYROLL ADVANCED!!! (SPRA), (c)

//                                Written by Joseph Curtis..

//                          Advanced Programming Design and Concepts.

//                                     ----------------
//                                      C++ Assignment
//                                     ----------------

//                                     SEMESTER 2 2001

// This program is an advanced version of the payroll application in the semester 1
// assignment.  It uses Object Oriented Programming (OOP) to enhance the funtionality
// and expand on the usefullness of the previous attempt.

// FEARURES

// You can create new payrolls, view them or edit them if you made a mistake.  The payroll
// can then be saved to be accessed or modified later at your leisure.  The easy to use 
// graphical interface and simple yet elegant design make this program the perfect solution
// to your small buisiness payroll problems.  Super Payroll Advanced will make your nasty
// yet essential book-keeping hours be done in just minuites!








// Include files
#include <iostream.h>  // Basic input/output stream
#include <fstream.h>   // File saving and loading
#include <iomanip.h>   // Input/output stream manipulation
#include <stdio.h>     // C standard input/output
#include <windows.h>   // Windows console manipulation
#include <conio.h>     // Console input/output commands

// Global variables
const int max_employees = 100, max_menu_items = 10, max_menu_item_length = 20;
int total_employees = 15, screen_height = 40, screen_width = 100;

// Structures
struct details                 // Holds payroll details
{
	int grade;                 // Employee grade
	float hours_worked;        // Hours worked
	char payroll_name[20];     // Name of payroll
	char employee_number[20];  // Employee number
};
struct _details
{
	struct details d[max_employees];
};

// Main class
class employee      // This class holds all the payroll information
{
private:
	float gross_pay;          // Gross pay
	float super_deducted;     // Superanuation deducted
	float tax_deducted;       // Tax deducted
	float total_pay;          // Net pay
	int grade;                // Employee grade
	float hours_worked;       // Number of hours worked
	char payroll_name[20];    // Payroll name
	char employee_number[20]; // Employee number
public:
	employee();                           // Constructor
	~employee();                          // Destructor
	new_data(struct details);             // Function for entering data into class
	float calc_super();                   // Calculate superanuation deducted
	float calc_tax();                     // Calculate tax deducted
	float calc_total_pay();               // Calculate net pay
	float calc_gross_pay();               // Calculate gross pay
	float get_hours();                    // Returns hours worked
	int get_grade();                      // Returns grade
	struct details get_payroll_name();    // Returns payroll name
	struct details get_employee_number(); // Returns employee number
};

employee::employee() // Constructor
{

}

employee::~employee() // Destructor
{

}

struct details employee::get_employee_number()
{
	struct details temp;
	for(int i=0;i<20;i++)
	{
		temp.employee_number[i] = employee_number[i];
	}
	return temp;
}

struct details employee::get_payroll_name()
{
	struct details temp;
	for(int i=0;i<20;i++)
	{
		temp.payroll_name[i] = payroll_name[i];
	}
	return temp;
}

float employee::get_hours()
{
	return hours_worked;
}

int employee::get_grade()
{
	return grade;
}

employee::new_data(struct details temp)
{
	for(int i=0;i<20;i++)
	{
		payroll_name[i] = temp.payroll_name[i];
		employee_number[i] = temp.employee_number[i];
	}
	grade = temp.grade;
	hours_worked = temp.hours_worked;
	gross_pay = employee::calc_gross_pay();
	super_deducted = employee::calc_super();
	tax_deducted = employee::calc_tax();
	total_pay = employee::calc_total_pay();
return 0;
}

float employee::calc_gross_pay()
{
	float rate = 10.5;
	switch (grade)
	{
	case 2:
		rate = 15;
		break;
	case 3:
		rate = 20.5;
		break;
	default:
		break;
	};
	if (hours_worked > 38) gross_pay = float (((rate * 1.5) * (hours_worked-38))+(38*rate));
	else
	gross_pay = float (rate) * hours_worked;
	return gross_pay;
}

float employee::calc_super()
{
	super_deducted = float (0.05) * gross_pay;
	return super_deducted;
}

float employee::calc_tax()
{
	float net = gross_pay - super_deducted;
	float tax;
	bool a,b,c,d;
	a = net >= 0 && net < 201;
	b = net >= 201 && net < 301;
	c = net >= 301 && net < 450;
	d = net >= 451 && net < 600;
	if (a) tax = 0;
	else
		if (b) tax = float (.15);
		else
			if (c) tax = float (.25);
			else
				if (d) tax = float (.34);
				else
					tax = float (.46);
	tax_deducted = tax * net;
	return tax_deducted;
}
float employee::calc_total_pay()

{
	total_pay = gross_pay - super_deducted - tax_deducted;
	return total_pay;
}
// End of class

// Function Prototypes
void cls();                  // Clear the screen
void locate(int x, int y);   // Change the position of the cursor
void screensize();           // Change the screen size
void data(struct details temp[max_employees], class employee temp1[max_employees]); // Input data into the class
void write_file(class employee temp[max_employees]); // Write a payroll to disk
struct _details read_file(char name[20]);           // Read a payroll from disk
char menu(int noi, char title [max_menu_item_length], char temp [max_menu_items][max_menu_item_length]); //Display a menu to the screen
void border(int number, char title[]);   // Draw the border on the screen
void quit();                             //Exit the program
int view_detail(class employee temp[max_employees], int page);  // View a payroll in detail
void view(class employee temp[max_employees]);                  // View a payroll overview
void new_payroll(class employee temp[max_employees]);           // Create a new payroll
void save(class employee temp[max_employees]);                  // Save a payroll
void load(class employee temp[max_employees]);                  // Load a payroll
void edit(class employee temp[max_employees]);                  // Edir a payroll

// Functions

void cls()
{
	COORD const topleft = {0,0};
	HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
	DWORD len;FillConsoleOutputCharacter(console, ' ', 4000, topleft, & len);
	SetConsoleCursorPosition(console, topleft);
}

void locate(int x, int y)
{
	COORD const topleft = {x,y};
	HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleCursorPosition(console, topleft);
}

void screensize()
{
	COORD const size = {screen_width,screen_height};
	_CONSOLE_CURSOR_INFO ccinfo;
	ccinfo.dwSize = 99;
	ccinfo.bVisible = 0;
	HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleCursorInfo(console, &ccinfo);
	SetConsoleScreenBufferSize(console, size);
	SetConsoleTitle("Super PayRoll Advanced!!! (SPRA).");
}

void data(struct details temp[max_employees], class employee temp1[max_employees])
{
	for(int i=0;i<total_employees;i++)
	{
		temp1[i].new_data(temp[i]);
	}
}

void write_file(class employee temp[max_employees])
{
	ofstream fout (temp[0].get_payroll_name().payroll_name, ios::out | ios::binary);
	fout << total_employees << ' ';
	for(int i=0;i<total_employees;i++)
	{
		fout << temp[i].get_employee_number().employee_number << ' ' << temp[i].get_grade() << ' ' << temp[i].get_hours() << ' ';
	}
	fout.close();
}

struct _details read_file(char name[20])
{
	struct _details temp;
	ifstream fin (name, ios::in | ios::binary);
	fin >> total_employees ;
	for(int i=0;i<20;i++)
	{
	temp.d[0].payroll_name[i] = name[i];
	}
	for(i=0;i<total_employees;i++)
	{
		fin  >> temp.d[i].employee_number >> temp.d[i].grade >> temp.d[i].hours_worked;
	}
	fin.close();
	return temp;
}

char menu(int noi, char title [max_menu_item_length], char temp [max_menu_items][max_menu_item_length])
{
	char c=char(178);
	int s=6, choice=0, cursor_pos=1;
	for(int a=0;a<max_menu_item_length+s;a++)
	{
			locate((screen_width/2)-((max_menu_item_length+s)/2)+a, (screen_height/2)-((s+((2)*noi))/2));
			cout<<c<<endl;
			locate((screen_width/2)-((max_menu_item_length+s)/2)+a, (screen_height/2)+((s+((2)*noi))/2));
			cout<<c<<endl;
	}
	for(int b=0;b<(s+(2*noi)+1);b++)
	{
			locate((screen_width/2)-((max_menu_item_length+s)/2), (screen_height/2)-((s+((2)*noi))/2)+b);
			cout<<c<<endl;
			locate((screen_width/2)+((max_menu_item_length+s)/2), (screen_height/2)-((s+((2)*noi))/2)+b);
			cout<<c<<endl;
	}
	locate(((screen_width/2)+s)-((max_menu_item_length+s)/2)-1, -1+((screen_height/2)+(s/2))-((s+((2)*noi))/2));	
	cout << title << endl << endl;	
	for(int i=0;i<noi;i++)
	{
	locate(((screen_width/2)+s)-((max_menu_item_length+s)/2)-1, -1+((screen_height/2)+s)-((s+((2)*noi))/2)+i*2);	
	cout << i+1 << ".  " << temp[i]<<endl;
	}
		locate(0, screen_height-4);cout<<char(195)<<endl;
		locate(screen_width-1,screen_height-4);cout<<char(180)<<endl;
		locate(2, screen_height-3);cout<<"Use "<<char(30)<< " and "<<char(31)<<" keys to choose, and ENTER to accept."<<endl;
		for(i=1;i<screen_width-1;i++)
		{
			locate(i, screen_height-4);cout<<char(196)<<endl;
		}
		do
		{
			locate(((screen_width/2)+s)-((max_menu_item_length+s)/2)-4, -1+((screen_height/2)+s)-((s+((2)*noi))/2)+(cursor_pos-1)*2);	
			cout<<char(16)<<endl;	
			locate(((screen_width/2)+s)+((max_menu_item_length+s)/2)-8, -1+((screen_height/2)+s)-((s+((2)*noi))/2)+(cursor_pos-1)*2);	
			cout<<char(17)<<endl;			
			while( !_kbhit() )
			{
				locate(2, screen_height-3);
			}
			switch(_getch() )
			{
			case char(80):
				cursor_pos++;
				locate(((screen_width/2)+s)-((max_menu_item_length+s)/2)-4, -1+((screen_height/2)+s)-((s+((2)*noi))/2)+(cursor_pos-2)*2);	
				cout<<" "<<endl;	
				locate(((screen_width/2)+s)+((max_menu_item_length+s)/2)-8, -1+((screen_height/2)+s)-((s+((2)*noi))/2)+(cursor_pos-2)*2);	
				cout<<" "<<endl;			
				break;
			case char(72):
				cursor_pos--;
				locate(((screen_width/2)+s)-((max_menu_item_length+s)/2)-4, -1+((screen_height/2)+s)-((s+((2)*noi))/2)+(cursor_pos)*2);	
				cout<<" "<<endl;	
				locate(((screen_width/2)+s)+((max_menu_item_length+s)/2)-8, -1+((screen_height/2)+s)-((s+((2)*noi))/2)+(cursor_pos)*2);	
				cout<<" "<<endl;			
				break;
			case char(13):
				choice = cursor_pos;
				locate(((screen_width/2)+s)-((max_menu_item_length+s)/2)-4, -1+((screen_height/2)+s)-((s+((2)*noi))/2)+(cursor_pos-1)*2);	
				cout<<" "<<endl;	
				locate(((screen_width/2)+s)+((max_menu_item_length+s)/2)-8, -1+((screen_height/2)+s)-((s+((2)*noi))/2)+(cursor_pos-1)*2);	
				cout<<" "<<endl;			
				break;
			case char(27):
				choice = 27;
				break;
			}
			if(cursor_pos<1)
				cursor_pos = noi;
			if(cursor_pos>noi)
				cursor_pos = 1;
		}while(choice<1);
	return choice;
}

void border(int number, char title[])
{
	locate((screen_width/2)-(number/2), 3);
	cout<<title<<endl;
	locate(0,screen_height-1);cout<<char(192)<<endl;
	locate(0,0);cout<<char(218)<<endl;
	locate(screen_width-1,0);cout<<char(191)<<endl;
	for(int i=1;i<(screen_width-1);i++)
	{
		locate(i,0);cout<<char(196)<<endl;
		locate(i,screen_height-2);cout<<char(196)<<endl;
	}
	for(i=1;i<(screen_height-2);i++)
	{
		locate(0,i);cout<<char(179)<<endl;
		locate(screen_width-1, i);cout<<char(179)<<endl;
	}
	locate(0, 4);cout<<char(195)<<endl;
	locate(screen_width-1,4);cout<<char(180)<<endl;
	for(i=1;i<screen_width-1;i++)
	{
		locate(i, 4);cout<<char(196)<<endl;
	}
}

void quit()
{
	char temp[max_menu_items][max_menu_item_length] = {{"Yes"}, {"No"}};
	cls();
	border(30, "Exit Super PayRoll Advanced!!!");
	switch(menu(2, "Are you sure??", temp))
	{
	case 1:
		exit(0);
		break;
	case 2:
		cls();
		border(44, "Welcome to Super PayRoll Advanced!!! (SPRA)");			
		break;
	default:
		break;
	}
}

int view_detail(class employee temp[max_employees], int page)
{
	int i=0,num=10, pages = total_employees+1;
	bool done=0;
	float rate = 10.5, hours, overtime;
	char title[41] = "View current Payroll - Detailed, Page ";
	title[38] = page+48;
	cls();
	border(40, title);
	do
	{
		locate(68,2);cout<<"    "<<endl;
		locate(68,2);cout<<page<<endl;
		locate(0, screen_height-4);cout<<char(195)<<endl;
		locate(screen_width-1,screen_height-4);cout<<char(180)<<endl;
		for(i=1;i<screen_width-1;i++)
		{
			locate(i, screen_height-4);cout<<char(196)<<endl;
		}
		locate(2,screen_height-3);cout<<char(17)<<" - Previous Page, "<<char(16)<<" - Next Page, ESC to return to Overview."<<endl;
		locate(4,6);cout<<"Payroll name: ";
		i=0;
		do
		{
			cout<<temp[0].get_payroll_name().payroll_name[i];
			i++;
		}while(temp[0].get_payroll_name().payroll_name[i]!='.');
		cout<<endl;
		locate(21,8);cout<<"               "<<endl;
		locate(4,8);cout<<"Employee Number: "<<temp[page-1].get_employee_number().employee_number<<endl;
		switch(temp[page-1].get_grade())
		{
		case 1:
			rate=10.5;
			break;
		case 2:
			rate=15;
			break;
		case 3:
			rate=20.5;
			break;
		default:
			rate=10.5;
			break;
		}
		locate(4,10);cout<<"Grade: "<<temp[page-1].get_grade()<<endl;
		if(temp[page-1].get_hours()>38)
		{
			hours=38;
			overtime = temp[page-1].get_hours()-38;
			locate(4,14);cout<<"       "<<endl;
			locate(4,14);cout<<setprecision(0)<<hours<<" hours worked @ $"<<setprecision(2)<<rate<<" per hour."<<endl;
			locate(70,14);cout<<"      "<<endl;
			locate(70,14);cout<<"$ "<<38*rate<<endl;
			locate(82,14);cout<<"          "<<endl;
			locate(4,16);cout<<setprecision(0)<<overtime<<" hours overtime @ $"<<setprecision(2)<<rate*1.5<<" per hour."<<endl;
			locate(70,16);cout<<"      "<<endl;		
			locate(70,16);cout<<"$ "<<overtime*rate*1.5<<endl;
			locate(82,16);cout<<"      "<<endl;
			locate(82,16);cout<<"Overtime."<<endl;				
			locate(4,18);cout<<"Total gross pay"<<endl;
			locate(70,18);cout<<"$ "<<temp[page-1].calc_gross_pay()<<endl;
			locate(82,18);cout<<"Gross Pay."<<endl;
		}
		else
		{
			locate(4,14);cout<<"                                                                                          "<<endl;
			locate(4,16);cout<<"                                                                                          "<<endl;
			locate(4,18);cout<<"                                                                                          "<<endl;
			locate(4,14);cout<<setprecision(0)<<temp[page-1].get_hours()<<" hours worked @ $"<<setprecision(2)<<rate<<" per hour."<<endl;		
			locate(70,14);cout<<"$ "<<temp[page-1].calc_gross_pay()<<endl;
			locate(82,14);cout<<"Gross Pay"<<endl;
		}
		locate(4,21);cout<<"      "<<endl;
		locate(4,21);cout<<"5% of $"<<temp[page-1].calc_gross_pay()<<" (gross pay)."<<endl;
		locate(70,21);cout<<"      "<<endl;
		locate(70,21);cout<<"$ "<<temp[page-1].calc_super()<<endl;
		locate(82,21);cout<<"      "<<endl;
		locate(82,21);cout<<"Super Deducted."<<endl;
		float net = temp[page-1].calc_gross_pay() - temp[page-1].calc_super();
		float tax;
		bool a,b,c,d;
		a = net >= 0 && net < 201;
		b = net >= 201 && net < 301;
		c = net >= 301 && net < 450;
		d = net >= 451 && net < 600;
		if (a) tax = 0;
		else
			if (b) tax = float (.15);
			else
				if (c) tax = float (.25);
				else
					if (d) tax = float (.34);
					else
						tax = float (.46);
		locate(4,24);cout<<"      "<<endl;
		locate(4,24);cout<<tax*100<<"% tax of $"<<net<<" (gross pay - super)."<<endl;
		locate(70,24);cout<<"         "<<endl;
		locate(70,24);cout<<"$ "<<net*tax<<endl;
		locate(82,24);cout<<"      "<<endl;
		locate(82,24);cout<<"Tax Deducted."<<endl;
		for(i=4;i<97;i++)
		{
			locate(i,26);cout<<char(205)<<endl;
		}
		locate(4,28);cout<<"      "<<endl;
		locate(4,28);cout<<"Total Net Pay."<<endl;
		locate(70,28);cout<<"      "<<endl;
		locate(70,28);cout<<"$ "<<temp[page-1].calc_total_pay()<<endl;
		locate(82,28);cout<<"      "<<endl;
		locate(82,28);cout<<"Net Pay."<<endl;
		while( !_kbhit() ){}
		switch(_getch())
		{
		case char(75):
			page--;
			break;
		case char(77):
			page++;
			break;
		case char(27):
			done=true;
			break;
		default:
			break;
		}
		if(page<1)
			page=pages-1;
		if(page>pages-1)
			page=1;
	}while(!done);
	cls();
	border(40, "View current Payroll - OverView, Page ");
	return page;
}
/////////////////////////////////////////////////////////////////NICEIFICATION STOPS!!!!!!
void view(class employee temp[max_employees])
{
	int i=0, page=1,num=10, pages = 1, selected=1, n=10;
	bool done=0;

	if(total_employees>10)
	{
		if(total_employees<21)
			pages=2;
		else
			if(total_employees<31)
				pages=3;
			else
				if(total_employees<41)
					pages=4;
				else
					if(total_employees<51)
						pages=5;
					else
						pages=6;
	}else{pages=1;}


	
		char title[41] = "View current Payroll - OverView, Page ";
		title[38] = page+48;
		cls();
		border(40, title);
		
		do
		{
		locate(68,2);cout<<page<<endl;
	
		locate(0, screen_height-4);cout<<char(195)<<endl;
		locate(screen_width-1,screen_height-4);cout<<char(180)<<endl;
		for(i=1;i<screen_width-1;i++)
		{
			locate(i, screen_height-4);cout<<char(196)<<endl;
		}
		locate(2,screen_height-3);cout<<char(17)<<" - Previous Page, "<<char(16)<<" - Next Page, ESC to return to main menu, ENTER to view detailed payroll."<<endl;
		locate(4,6);cout<<"Payroll name: ";
		i=0;
		do{cout<<temp[0].get_payroll_name().payroll_name[i];i++;}while(temp[0].get_payroll_name().payroll_name[i]!='.');
		cout<<endl;
		locate(4,8);cout<<"Number of Employees: ";cout<<total_employees<<endl;
	
		locate(4,12);cout<<setw(15)<<setiosflags(ios::left)<<"Employee Number"<<endl;
		locate(24,12);cout<<"Grade"<<endl;
		locate(34,12);cout<<"Hours Worked"<<endl;
		locate(51,12);cout<<"Gross Pay"<<endl;
		locate(65,12);cout<<"Super"<<endl;
		locate(75,12);cout<<"Tax  "<<endl;
		locate(85,12);cout<<"Net Pay"<<endl;
	
		for(i=4;i<93;i++)
		{
			locate(i,13);cout<<char(205)<<endl;
		}
		if(total_employees<11)
			num=total_employees;
		else
			num=10;
	
		for(i=0;i<num;i++)
		{
		locate(4,15+i*2);cout<<setw(15)<<setiosflags(ios::left|ios::fixed)<<setprecision(2)<<temp[i+((page-1)*10)].get_employee_number().employee_number<<endl;
		if((temp[i+((page-1)*10)].get_grade())!=0)
		{
		locate(24,15+i*2);cout<<"          "<<endl;
		locate(34,15+i*2);cout<<"          "<<endl;
		locate(51,15+i*2);cout<<"          "<<endl;
		locate(65,15+i*2);cout<<"          "<<endl;
		locate(75,15+i*2);cout<<"          "<<endl;
		locate(85,15+i*2);cout<<"          "<<endl;
		
		locate(24,15+i*2);cout<<temp[i+((page-1)*10)].get_grade()<<endl;
		locate(34,15+i*2);cout<<setprecision(0)<<temp[i+((page-1)*10)].get_hours()<<setprecision(2)<<endl;
		locate(51,15+i*2);cout<<"$ "<<temp[i+((page-1)*10)].calc_gross_pay()<<endl;
		locate(65,15+i*2);cout<<"$ "<<temp[i+((page-1)*10)].calc_super()<<endl;
		locate(75,15+i*2);cout<<"$ "<<temp[i+((page-1)*10)].calc_tax()<<endl;
		locate(85,15+i*2);cout<<"$ "<<temp[i+((page-1)*10)].calc_total_pay()<<endl;
		}
		else
		{
		locate(24,15+i*2);cout<<"          "<<endl;
		locate(34,15+i*2);cout<<"          "<<endl;
		locate(51,15+i*2);cout<<"          "<<endl;
		locate(65,15+i*2);cout<<"          "<<endl;
		locate(75,15+i*2);cout<<"          "<<endl;
		locate(85,15+i*2);cout<<"          "<<endl;
		}
		}
		
		locate(2,15+(selected-1)*2);cout<<char(16)<<endl;
		locate(94,15+(selected-1)*2);cout<<char(17)<<endl;
		

		while( !_kbhit() ){}
		switch(_getch())
		{
		case char(75):
			page--;
			for(i=0;i<10;i++)
			{
			locate(2,15+i*2);cout<<" "<<endl;
			locate(94,15+i*2);cout<<" "<<endl;
			}
			break;
		case char(77):
			page++;
			for(i=0;i<10;i++)
			{
			locate(2,15+i*2);cout<<" "<<endl;
			locate(94,15+i*2);cout<<" "<<endl;
			}
			break;
		case char(27):
			done=true;
			break;
		case char(80):
			selected++;
		locate(2,15+(selected-2)*2);cout<<" "<<endl;
		locate(94,15+(selected-2)*2);cout<<" "<<endl;
			break;
		case char(72):
			selected--;
		locate(2,15+(selected)*2);cout<<" "<<endl;
		locate(94,15+(selected)*2);cout<<" "<<endl;
			break;
		case char(13):
			selected=view_detail(temp, selected+((page-1)*10));
			do{
			if(selected>(page*10))
				page++;
			
			if(selected<((page*10)-9))
				page--;

			}while(selected<((page*10)-19)||selected>(page*10));

				selected=selected-(page-1)*10;
		break;
		default:
			break;
		}	
		if(page<1)
			page=pages;

		if(page>pages)
			page=1;

		if(selected<1)
		{
			if(page==pages)
			selected=(total_employees-((pages-1)*10));
			else
				selected=10;
		}
		if(page==pages)
		{
			if(selected>(total_employees-((pages-1)*10)))
				selected=1;
		}else
		{
		if(selected>10)
		{
			selected=1;
		}
		}
	
	}while(!done);
	
	cls();
	border(44, "Welcome to Super PayRoll Advanced!!! (SPRA)");			

}

void new_payroll(class employee temp[max_employees])
{
	struct details pay[max_employees];
	int pages=1, page=1;		
	bool done=false;	
	char tmp[20], null;
			null=getchar();

	_CONSOLE_CURSOR_INFO ccinfo;
	ccinfo.dwSize = 99;
	ccinfo.bVisible = 1;
	HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleCursorInfo(console, &ccinfo);

	
	cls();
	border(21, "Create a new Payroll, Page ");
	locate(67,2);cout<<page<<endl;
	locate(0, screen_height-4);cout<<char(195)<<endl;
	locate(screen_width-1,screen_height-4);cout<<char(180)<<endl;
	
	for(int i=1;i<screen_width-1;i++)
	{
		locate(i, screen_height-4);cout<<char(196)<<endl;
	}
	locate(2,screen_height-3);cout<<"Do what it says!!!"<<endl;
	
//	for(int d=0;d<20;d++){pay[0].payroll_name[d]='`';}
	

	
	locate(4, 6);cout<<"Enter name of new payroll >";cin.getline(tmp,20);cout<<endl;
	locate(31,6);

	int d=0;
	while(tmp[d]!='\0'){pay[0].payroll_name[d]=tmp[d];d++;}
	pay[0].payroll_name[d]='.';
	pay[0].payroll_name[d+1]='d';
	pay[0].payroll_name[d+2]='a';
	pay[0].payroll_name[d+3]='t';
	for(d+=4;d<max_menu_item_length;d++)
	{
		pay[0].payroll_name[d]='\0';
	}
	for(d=d+4;d<20;d++){pay[0].payroll_name[d]=' ';}
		
	locate(4, 8);cout<<"Enter number of employees in new payroll >";
	cin>>total_employees;cout<<endl;


	locate(4,12);cout<<setw(15)<<setiosflags(ios::left)<<"Employee Number"<<endl;
		locate(24,12);cout<<"Grade"<<endl;
		locate(34,12);cout<<"Hours Worked"<<endl;
	
		for(i=4;i<46;i++)
		{
			locate(i,13);cout<<char(205)<<endl;
		}

	int b=0;
	for(i=0;i<total_employees;i++)
	{
	locate(4,15+(2*b));cin>>pay[i].employee_number;cout<<endl;
	do{
	locate(24,15+(2*b));cin>>pay[i].grade;cout<<endl;
	}while(pay[i].grade<1||pay[i].grade>3);
	do{
	locate(34,15+(2*b));cin>>pay[i].hours_worked;cout<<endl;
	}while(pay[i].hours_worked<0||pay[i].hours_worked>168);
	b++;
	if(b>=10)
	{
		b=0;
		page++;
		for(int z=0;z<10;z++)
		{
			locate(4,15+(2*z));cout<<"     "<<endl;
			locate(24,15+(2*z));cout<<"     "<<endl;
			locate(34,15+(2*z));cout<<"     "<<endl;
		}
	}
		locate(67,2);cout<<page<<endl;
	}

	data(pay, temp);	

	locate(2, screen_height-3);cout<<"Done!  Press ESC to return to main menu..."<<endl;
	locate(44, screen_height-3);
	do{
	while( !_kbhit() ){}
	switch(_getch())
	{
	case 27:
		done=true;
		break;
	default:
		break;
	}
	}while(!done);
	ccinfo.bVisible = 0;
	SetConsoleCursorInfo(console, &ccinfo);
    cls();
	border(44, "Welcome to Super PayRoll Advanced!!! (SPRA)");			

}
void save(class employee temp[max_employees])
{
	char slot[max_menu_items][max_menu_item_length];
	
	ifstream fin ("files.dat", ios::in|ios::binary);
	fin.getline(slot[0],20);
	fin.getline(slot[1],20);
	fin.getline(slot[2],20);
	fin.getline(slot[3],20);
	fin.getline(slot[4],20);
	fin.close();

	cls();
	border(21, "Save current Payroll.");
	
	int i=0;
	switch(menu(5, "Save in slot?", slot))
	{
	case 1:
		for(i=0;i<max_menu_item_length;i++){slot[0][i] = temp[0].get_payroll_name().payroll_name[i];}
		write_file(temp);
	break;
	case 2:
		for(i=0;i<max_menu_item_length;i++){slot[1][i] = temp[0].get_payroll_name().payroll_name[i];}
		write_file(temp);
		break;
	case 3:
		for(i=0;i<max_menu_item_length;i++){slot[2][i] = temp[0].get_payroll_name().payroll_name[i];}
		write_file(temp);
		break;
	case 4:
		for(i=0;i<max_menu_item_length;i++){slot[3][i] = temp[0].get_payroll_name().payroll_name[i];}
		write_file(temp);
		break;
	case 5:
		for(i=0;i<max_menu_item_length;i++){slot[4][i] = temp[0].get_payroll_name().payroll_name[i];}
		write_file(temp);
		break;
	case 27:
		break;
	default:
		break;
	}
	ofstream fout("files.dat", ios::out|ios::binary);	
    fout<<slot[0]<<endl;
	fout<<slot[1]<<endl;
	fout<<slot[2]<<endl;
	fout<<slot[3]<<endl;
	fout<<slot[4]<<endl;
	fout.close();
	cls();
	border(44, "Welcome to Super PayRoll Advanced!!! (SPRA)");			

}

void load(class employee temp[max_employees])
{
	char slot[max_menu_items][max_menu_item_length];
	bool go=false;

	ifstream fin ("files.dat", ios::in | ios::binary);
	fin.getline(slot[0],20);
	fin.getline(slot[1],20);
	fin.getline(slot[2],20);
	fin.getline(slot[3],20);
	fin.getline(slot[4],20);
	fin.close();

	cls();
	border(24, "Load a previous Payroll.");
	
	do
	{
	int temp_menu = menu(5, "Load which slot?", slot)-1;
	
	if(temp_menu!=27)
	{
	if(slot[temp_menu][0]!='-')
	{
			data(read_file(slot[temp_menu]).d, temp);
			go=false;
	}
	else
	{
		go=true;
	}
	}
	
	else
	{
		go=false;
	}
	}while(go);
	
    
	cls();
	border(44, "Welcome to Super PayRoll Advanced!!! (SPRA)");			

}
void edit(class employee temp[max_employees])
{
	int i=0, page=1,num=10, pages = 1, selected=1, n=10, item=1, y=0;
	bool done=0;
	char null;
	struct details tmp[max_employees];
		
		_CONSOLE_CURSOR_INFO ccinfo;
		HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);

	
	for (i=0;i<max_employees;i++)
	{
		tmp[i].grade = temp[i].get_grade();
		tmp[i].hours_worked = temp[i].get_hours();
		for(int b=0;b<20;b++)
		{
			tmp[i].payroll_name[b] = temp[i].get_payroll_name().payroll_name[b];
			tmp[i].employee_number[b] = temp[i].get_employee_number().employee_number[b];
		}
	}

	if(total_employees>10)
	{
		if(total_employees<21)
			pages=2;
		else
			if(total_employees<31)
				pages=3;
			else
				if(total_employees<41)
					pages=4;
				else
					if(total_employees<51)
						pages=5;
					else
						pages=6;
	}else{pages=1;}


	
	
		cls();
		border(27, "Edit current Payroll, Page ");
		
		do
		{
	if(total_employees>10)
	{
		if(total_employees<21)
			pages=2;
		else
			if(total_employees<31)
				pages=3;
			else
				if(total_employees<41)
					pages=4;
				else
					if(total_employees<51)
						pages=5;
					else
						pages=6;
	}else{pages=1;}

			locate(64,2);cout<<page<<endl;
	
		locate(0, screen_height-4);cout<<char(195)<<endl;
		locate(screen_width-1,screen_height-4);cout<<char(180)<<endl;
		for(i=1;i<screen_width-1;i++)
		{
			locate(i, screen_height-4);cout<<char(196)<<endl;
		}
		locate(2,screen_height-3);cout<<char(17)<<" - Previous Page, "<<char(16)<<" - Next Page, ESC to return to main menu, ENTER to edit selected field."<<endl;
		locate(4,6);cout<<"Payroll name: ";
		i=0;
		do{cout<<temp[0].get_payroll_name().payroll_name[i];i++;}while(temp[0].get_payroll_name().payroll_name[i]!='.');
		cout<<endl;
		locate(4,8);cout<<"Number of Employees: ";cout<<total_employees<<endl;
	
		locate(4,12);cout<<setw(15)<<setiosflags(ios::left)<<"Employee Number"<<endl;
		locate(24,12);cout<<"Grade"<<endl;
		locate(34,12);cout<<"Hours Worked"<<endl;
		locate(51,12);cout<<"Gross Pay"<<endl;
		locate(65,12);cout<<"Super"<<endl;
		locate(75,12);cout<<"Tax  "<<endl;
		locate(85,12);cout<<"Net Pay"<<endl;
	
		for(i=4;i<93;i++)
		{
			locate(i,13);cout<<char(205)<<endl;
		}
		if(total_employees<11)
			num=total_employees;
		else
			num=10;
	
		for(i=0;i<num;i++)
		{
		locate(4,15+i*2);cout<<"                   "<<endl;
		locate(4,15+i*2);cout<<setw(15)<<setiosflags(ios::left|ios::fixed)<<setprecision(2)<<temp[i+((page-1)*10)].get_employee_number().employee_number<<endl;
		if((temp[i+((page-1)*10)].get_grade())!=0)
		{
		locate(2,15+i*2);cout<<"  "<<endl;
		locate(24,15+i*2);cout<<"          "<<endl;
		locate(34,15+i*2);cout<<"              "<<endl;
		locate(51,15+i*2);cout<<"          "<<endl;
		locate(65,15+i*2);cout<<"          "<<endl;
		locate(75,15+i*2);cout<<"          "<<endl;
		locate(85,15+i*2);cout<<"          "<<endl;
		
		locate(24,15+i*2);cout<<temp[i+((page-1)*10)].get_grade()<<endl;
		locate(34,15+i*2);cout<<setprecision(0)<<temp[i+((page-1)*10)].get_hours()<<setprecision(2)<<endl;
		locate(51,15+i*2);cout<<"$ "<<temp[i+((page-1)*10)].calc_gross_pay()<<endl;
		locate(65,15+i*2);cout<<"$ "<<temp[i+((page-1)*10)].calc_super()<<endl;
		locate(75,15+i*2);cout<<"$ "<<temp[i+((page-1)*10)].calc_tax()<<endl;
		locate(85,15+i*2);cout<<"$ "<<temp[i+((page-1)*10)].calc_total_pay()<<endl;
		}
		else
		{
		locate(2,15+i*2);cout<<"  "<<endl;
		locate(24,15+i*2);cout<<"          "<<endl;
		locate(34,15+i*2);cout<<"              "<<endl;
		locate(51,15+i*2);cout<<"          "<<endl;
		locate(65,15+i*2);cout<<"          "<<endl;
		locate(75,15+i*2);cout<<"          "<<endl;
		locate(85,15+i*2);cout<<"          "<<endl;
		}
		}
		
		switch(item)
		{
		case 1:
			locate(2,15+(selected-1)*2);cout<<char(16)<<endl;
			locate(18,15+(selected-1)*2);cout<<char(17)<<endl;
			break;
		case 2:
			locate(20,15+(selected-1)*2);cout<<char(16)<<endl;
			locate(28,15+(selected-1)*2);cout<<char(17)<<endl;
			break;
		case 3:
			locate(30,15+(selected-1)*2);cout<<char(16)<<endl;
			locate(40,15+(selected-1)*2);cout<<char(17)<<endl;
			break;
		default:
			break;
		}
		

		while( !_kbhit() ){}
		switch(_getch())
		{
		case '+':
			total_employees++;
			tmp[total_employees].hours_worked=0;
			tmp[total_employees].grade=1;

//			locate(4,15+total_employees*2);cout<<tmp[total_employees].employee_number<<endl;
//			locate(24,15+total_employees*2);cout<<tmp[total_employees].grade<<endl;
//			locate(34,15+total_employees*2);cout<<tmp[total_employees].hours_worked<<endl;

			break;
		case '-':
			total_employees--;
			break;
		case char(75):
			page--;
			for(i=0;i<10;i++)
			{
			locate(2,15+i*2);cout<<" "<<endl;
			locate(94,15+i*2);cout<<" "<<endl;
			}
			break;
		case char(77):
			page++;
			for(i=0;i<10;i++)
			{
			locate(2,15+i*2);cout<<" "<<endl;
			locate(94,15+i*2);cout<<" "<<endl;
			}
			break;
		case char(27):
			done=true;
			break;
		case char(80):
			if(item>2)
			{
				item=1;
				selected++;
				locate(2,15+(selected-2)*2);cout<<" "<<endl;
			}
			else
			{
				item++;
				locate(2,15+(selected-2)*2);cout<<" "<<endl;
			}

		
			break;
		case char(72):
			if(item<2)
			{
				item=3;
				selected--;
			}
			else
			{
				item--;
			}

			break;
		case char(13):
			null=getchar();
		ccinfo.dwSize = 99;	
		ccinfo.bVisible = 1;
		SetConsoleCursorInfo(console, &ccinfo);

			switch(item)
			{
			case 1:
				locate(4,15+(selected-1)*2);cout<<"          "<<endl;
				locate(4,15+(selected-1)*2);cin>>tmp[(page-1)*10+selected-1].employee_number;
				break;
			case 2:
				do{locate(24,15+(selected-1)*2);cout<<"    "<<endl;
				locate(24,15+(selected-1)*2);cin>>tmp[(page-1)*10+selected-1].grade;}while(tmp[(page-1)*10+selected-1].grade<1||tmp[(page-1)*10+selected-1].grade>3);
				break;
			case 3:
				do{locate(34,15+(selected-1)*2);cout<<"     "<<endl;
				locate(34,15+(selected-1)*2);cin>>tmp[(page-1)*10+selected-1].hours_worked;}while(tmp[(page-1)*10+selected-1].hours_worked>168);
				break;
			default:
			break;
			}
				data(tmp, temp);				
				ccinfo.dwSize = 99;
	ccinfo.bVisible = 0;
	SetConsoleCursorInfo(console, &ccinfo);
		break;
		default:
			break;
		}	
		if(page<1)
			page=pages;

		if(page>pages)
			page=1;

		if(selected<1)
		{
			if(page==pages)
			selected=(total_employees-((pages-1)*10));
			else
				selected=10;
		}
		if(page==pages)
		{
			if(selected>(total_employees-((pages-1)*10)))
				selected=1;
		}else
		{
		if(selected>10)
		{
			selected=1;
		}
		}
	
	}while(!done);
	
	cls();
	border(44, "Welcome to Super PayRoll Advanced!!! (SPRA)");			
	ccinfo.dwSize = 99;
	ccinfo.bVisible = 0;
	SetConsoleCursorInfo(console, &ccinfo);				
}


//Functions up - main down//
main()
{
/*	struct details emp[max_employees] = {{1,37, "payroll.dat", "AX-191"},
										{2,42,"","AX-192"},{1,27,"","AX-193"},
										{3,35,"","AX-194"},{3,17,"","AX-195"},
										{2,40,"","AX-196"},{1,29,"","AX-197"},
										{3,34,"","AX-198"},{1,39,"","AX-199"},
										{3,23,"","AX-20A"},{2,36,"","AX-20B"},
										{1,49,"","B2-426"},{2,31,"","B2-42J"},
										{1,11,"","B2-42K"},{3,40,"","B2-42Z"}};
*/
	class employee emp2[max_employees];
	
	float rate = 10.5;
	char menu_bits[max_menu_items][max_menu_item_length] = {{"New Payroll"}, {"View Payroll"}, {"Edit Payroll"}, {"Save Payroll"}, {"Load Payroll"}, {"Exit SPRA"}};
	
	screensize();

	data(read_file("payroll.dat").d, emp2);
//	data(emp, emp2);
	
	border(44, "Welcome to Super PayRoll Advanced!!! (SPRA)");			

	for(;;)
	{

	switch(menu(6, "Main Menu.", menu_bits))
	{
	case 1:
		new_payroll(emp2);
		break;
	case 2:
		view(emp2);
		break;
	case 3:
		edit(emp2);
		break;
	case 4:
		save(emp2);
		break;
	case 5:
		load(emp2);
		break;
	case 6:
		quit();	  
		break;
	default:
		break;
	}
	}

	
	return 0;
}

// The End.
