//Tunku Sharifah Ainun Nabila bt Tunku Syed Azhar
//Ang Ke Wei

#include<iostream>
#include<fstream>
#include<iomanip>
using namespace std;

const int ROW=5;
const int COL=12;
const int bRow=5;
const int mRow=12;

double calTotalSales(double salesData[][COL],int row)
{
	double totalS;
	for(int i=0; i<5; i++)
	{
		for(int j=0; j<12; j++)
		{
			totalS=totalS+salesData[i][j];
		}
	}
	return totalS;
}

double calBranchSales(double salesData[][COL], int ROW, double branchS[], int size)
{	
	for (int i=0; i<ROW; i++)
	{	
		branchS[i]= 0;
		for (int j= 0; j<COL; j++)
		{
			branchS[i]= branchS[i]+salesData[i][j];
		}
	}
}

double calMonthlySales(double salesData[][COL],int ROW, double monthlyS[],int size)
{
	for (int i= 0; i<COL; i++)
	{	
		monthlyS[i]= 0;	
		for (int j= 0; j<ROW; j++)
		{
			monthlyS[i]= monthlyS[i]+salesData[j][i];
		}
	}
}

int findHighest(double branch[], int size)
{
	int highest=branch[0];
	int h=0;
	for(int i=0; i<size; i++)
	{
		if(branch[i]>highest)
		{
			highest=branch[i];
			h=i+1;
		}
	}
	return h;
}

int findLowest(double branch[], int size)
{
	int lowest=branch[0];
	int l=0;
	for(int i=0; i<size; i++)
	{
		if(branch[i]<lowest)
		{
			lowest=branch[i];
			l=i+1;
		}
	}
	return l;
}

void getFileData(double salesData[][COL],int row)
{
	ifstream in;
	ofstream out("sales_report.txt");
	in.open("sales.txt");

	if(!in)
	{
		cout<<"ERROR opening file. "<<endl;
	}
	else {
		cout<<"Done process data. Output file generated! "<<endl;
	}

	for(int x=0; x<ROW; x++)
	{
		for(int y=0; y<COL; y++)
		{
			in>>salesData[x][y];
		}
	}
	in.close();
	out.close();
}

int main()
{
	ofstream out("sales_report.txt");
	double salesData[ROW][COL];
	double branchSales[bRow];
	double monthlySales[mRow];
	
	getFileData(salesData,ROW);
	calBranchSales(salesData,ROW,branchSales,bRow);
	calMonthlySales (salesData, ROW, monthlySales, mRow);
		
	
	//ni untuk output total sales 
	out<<fixed<<setprecision(2)<<"Total sales of the company throughout the year: RM "<<calTotalSales(salesData, ROW)<<endl;
	out<<"---------------------------------------------------------"<<endl;
	
	//ni output Branch sales punya words
	out<<setw(25)<<left<<"Branch Sales:"<<endl;
	out<<setw(25)<<left<<"============="<<endl;
	
	//ni output for each branches
	for (int i= 0; i<bRow; i++)
	{
		out<<"Branch "<<(i+1)<<": RM  "<<branchSales[i]<<endl;
	}
	
	//this one output for average, highest and lowest for branch sales
	double total= 0;
	double average;
	for (int i= 0; i<bRow; i++)
	{
		total= total+branchSales[i];
		average= total/bRow;
	}
	out<<fixed<<setprecision(2)<<"Average Sales per branch: RM "<<average<<endl;
	out<<"Highest sales branch: Branch "<<findHighest (branchSales, bRow)<<endl;
	out<<"Lowest sales branch: Branch "<<findLowest (branchSales, bRow)<<endl;
	out<<"-----------------------------------------------"<<endl;
	
	//output for monthly sales punya words
	out<<setw(25)<<left<<"Monthly Sales:"<<endl;
	out<<setw(25)<<left<<"============="<<endl;
	
	//yang ni output for each month (yang horizontal dulu, baru vertical)
	for(int i=0; i<3; i++)
	{
		out<<setw(1)<<left<<" "<<(i+1)<<" : RM"<<monthlySales[i]<<"  ";
	}
	out<<endl;
	for(int i=3; i<6; i++)
	{
		out<<setw(1)<<left<<" "<<(i+1)<<" : RM"<<monthlySales[i]<<"  ";
	}
	out<<endl;
	for(int i=6; i<9; i++)
	{
		out<<setw(1)<<left<<" "<<(i+1)<<" : RM"<<monthlySales[i]<<"  ";
	}
	out<<endl;
	out<<"10 : RM"<<monthlySales[9]<<"	  "<<"11 : RM"<<monthlySales[10]<<"  ";
	out<<"12 : RM"<<monthlySales[11]<<endl;
	
	//this one output for average, highest and lowest for monthly sales
	double totalMonth= 0;
	double averageMonth;
	for (int i= 0; i<mRow; i++)
	{
		totalMonth= totalMonth+monthlySales[i];
		averageMonth= totalMonth/mRow;
	}
	out<<"Average Sales per month: RM "<<averageMonth<<endl;
	out<<"Highest sales month: Month "<<findHighest (monthlySales, mRow)<<endl;
	out<<"Lowest sales month: Month "<<findLowest (monthlySales, mRow)<<endl;
	out<<"-----------------------------------------------"<<endl;
	
	out.close();
	
	return 0;
	
}

