/*Assignment3 Case study 13 
  Group 6 Section 8
  Teoh Wei Jian A20EC0229
  Lai Chee Yee A20EC0199
  Uh Guan Yong A20EC0230*/
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cctype>
#include <iomanip>
#include <cstring>
using namespace std;

//Functions prototype
int displayRebate(char[]);
double calculateItem(double,int);

int main()
{	
	//declaration variables
	char str [100];
	int close = 0;
	ifstream input;
	input.open("CS13.txt");
	
	//Checking if the file is open failed
	if (!input)
	{
		cout << "ERROR: Cannot open file" << endl;
		return 0;
	}
	
	//Display the output and it is align to left side
	cout << left;
	cout << setw(15) << "User" << setw(12) << "Full Tax" 
	     << setw(12) << "Item 1" << setw(10) << "Amount"
	     << setw(12) << "Item 2" << setw(10) << "Amount"
	     << setw(12) << "Item 3" << setw(10) << "Amount"
	     << setw(10) << "Total Rebate" << endl;
	
	//Get input from the txt file     
	while(!input.eof())
	{
		input.getline(str, 100);
		close = displayRebate(str);
		/*if the displayRebate function return a value -1, 
		  it means the input file format not correct and 
		  it will end the program.*/
		if(close == -1){
			cout << "Your input file format are not correct.";
			return 0;
		}
	}
	
	
	
	input.close();
	return 0;
	
}

/*This function will display name and amount of rebate 
  If the input file format not correct, 
  it will return a value -1 to the main function */
int displayRebate(char str[100]){
	char item [5][12] = {"Zakat","Life Choice","Books",
						 "Hospitality", "School Fees"}; 
	char name [20], salary[10];
	//i1 variable is functioned as storing the item number or the item name of the first item in data type char, 
	//same as i2 and i3 is for second item and third item respectively
	char i1[12], i2[12], i3[12];
	//a1 variable is functioned as storing the amount of money paid for first item in data type char,
	//same as a2 and a3 is for second item and third item respectively
	char a1[10], a2[10], a3[10];
	/*c is used to determine there is no alphabet in the section of rebate amount digit value part,
	  x is used as an index to determine what place of element of the str variable that we want operate,
	  y is used as an index to determine what place of element from the array of the amount of money variable that we want to operate,
	  z is used to determine the digit character is need to allocate to what section such item 1, amount of money of item 1 and so on,
	  s is used to determine the spacing character is the spacing between the user name or not.*/
	int c = 0, x = 0, y = 0, z = 1, s = 0;
	//in1 variable (item number 1 )is used to store the number which determine the item type of the first item,
	//same as in2 and in3 is for second item and third item respectively
	int in1, in2, in3;
	/*sa is used to store the salary value in float type,
	  am1 is used to store the the amount of money paid for first item in data type float,
	  same as am2 and am3 is for second item and third item respectively,
	  total is used for storing total rebate amount value*/
	float sa, am1, am2, am3, total;
	
	//To make sure all the element in the array are empty
	for(int i = 0; i <=20; i++){
		name[i] = '\0';
		salary[i] = '\0';
		i1[i] = '\0';
		a1[i] = '\0';
		i2[i] = '\0';
		a2[i] = '\0';
		i3[i] = '\0';
		a3[i] = '\0';
	}
	
	//Get input from the txt file and allocate the data 
	//to the correct variable	
	while (str[x] != '\0')
		{
			if((x == 0) && (!isalpha(str[x]))){
				return -1;
			}
			else if(((x >= 20) || (c == 1)) && (isalpha(str[x]))){
				return -1;
			}
			
			if (isalpha(str[x])){
				name[x] = str [x];
			}
			else if (isdigit(str[x]) && (z == 1)){
				salary[y] = str[x];
				y++;
				s = 1;
				c = 1;
			}
			else if (isdigit(str[x]) && (z == 2)){
				i1[0] = str[x];
				s = 1;
			}
			else if (isdigit(str[x]) && (z == 3)){
				a1[y] = str[x];
				y++;
				s = 1;
			}
			else if (isdigit(str[x]) && (z == 4)){
				i2[0] = str[x];
				s = 1;
			}
			else if (isdigit(str[x]) && (z == 5)){
				a2[y] = str[x];
				y++;
				s = 1;
			}
			else if (isdigit(str[x]) && (z == 6)){
				i3[0] = str[x];
				s = 1;
			}
			else if (isdigit(str[x]) && (z == 7)){
				a3[y] = str[x];
				y++;
				s = 1;
			}
			else if (isspace(str[x]) && (s == 1)){
				s = 0;
				y = 0;
				z += 1;
			}
			else if (isspace(str[x]) && (s == 0)){
				name[x] = ' ';
			}
			++x;
		}
		
		//Changing the content type from char to float or int
		sa  = atof(salary);
		in1 = atoi(i1);
		in2 = atoi(i2);
		in3 = atoi(i3);
		am1 = atof(a1);
		am2 = atof(a2);
		am3 = atof(a3);

		//Calculate the rebate amount for each section
		sa *= 8.0 / 100.0 * 12.0;
		//Copy the name of item to each variable 
		strcpy(i1, item[(in1 - 1)]);
		strcpy(i2, item[(in2 - 1)]);
		strcpy(i3, item[(in3 - 1)]);
		//Determine the type item and the method calculation of rebate amount
		am1 = calculateItem(am1,in1);
		am2 = calculateItem(am2,in2);
		am3 = calculateItem(am3,in3);
		
		//If the input file format not correct, 
		//it will return a value -1 to the main function
		if ((am1 == -1) || (am2 == -1) || (am3 == -1))
			return -1;
		
		/*Total up the rebate amount of each section
		  and make sure the total rebate dose not 
		  exceed 85% of salary*/
		total = am1 + am2 + am3;
		if (total > (sa * 85.0 /100.0))
			total = (sa * 85.0 /100.0);
		
		//Display name, the rebate amount of each section 
		//and also the total rebate amount
		cout << left;
		cout << setw(15) << name << "RM" << setw(10) << sa
	    	 << setw(12) << i1 << "RM" << setw(8) << am1
	    	 << setw(12) << i2 << "RM" << setw(8) << am2
	    	 << setw(12) << i3 << "RM" << setw(8) << am3
	     	 << "RM" << setw(8) << total << endl;
		
		return 0;	
}

/*This function will return the certain amount of rebate 
  which fulfill the category of the item.
  If the input file format not correct, 
  it will return a value -1 to the calculateRebate function*/
double calculateItem (double amount, int item){
	if (item == 1)
		amount = amount;
	
	else if (item == 2)
		amount *= 25.0 / 100.0;
	
	else if (item == 3)
		amount *= 5.0 / 100.0;
	
	else if (item == 4)
		amount *= 45.0 / 100.0;
	
	else if (item == 5)
		amount *= 20.0 / 100.0;
	
	else 
		amount = -1;

	return amount;
}
