// FYLLADIO 5 - ASKHSH 15, C++2019-2020
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <time.h>
#define N 120
using namespace std;

class prod {
private :
	int code;
	int month;
	int month_total;
	int demand_total;
public:

//default constructor
	prod()
	{code=0;month=0; month_total=0; demand_total=0;}

//constructor with parameters' list
	prod(int in_code, int in_month, int in_month_total, int in_demand_total)
	{
		code=in_code;
		month=in_month;
		month_total=in_month_total;
		demand_total=in_demand_total;
	};

	int get_code() {return code;};
	int get_month() {return month;};
	int get_month_total() {return month_total;};
	int get_demand_total() {return demand_total;};

	void print()
	{ cout<<"base class printing.....";
	cout<<code<<"   "<<month<<"   "<<month_total <<"   "<<demand_total<<endl; 	}

};// end class prod definition
 
//function press any key
void pak()
{ printf("Press ENTER key to Continue\n");  getchar();   }


// DERIVED CLASS DEFINITION !!!!!
class new_prod : public prod
{
private:
	 double production_cost[10];
	
public :
	//default constructor
	new_prod() :   prod() { 
		 for (int jj=0;jj<10;jj++) 
	 	{
	  	   production_cost[jj]=0.0; 
	 	}	
	};

	//constructor with parameters' list
	new_prod(int in_code, int in_month, int in_month_total, int in_demand_total, double *in_production_cost) :
	prod(in_code, in_month, in_month_total, in_demand_total)
	{for (int ii=0;ii<10;ii++) production_cost[ii]=in_production_cost[ii]; 	}
	
	
	void new_print()
	{ 	prod:print(); 	cout<<"new values are :";
		for (int ii=0;ii<10;ii++) cout<<production_cost[ii]<<"  ";  cout<<endl;
	};
	
}; //end derived class definition

int main()
{ 
prod a[N]; 
new_prod b[N];
int product_number, demand_quantity, p_code;
int z[10][2]={0};
double in_production_cost[10];

int i,ii, k,j,w,v, m,in_date, in_pp, in_total_q, in_day[10], in_couples[20], sum=0, count=0, metr=0; 
int production_total[10][12]={0}, demand_total[10][12]={0}, *y1, flag=0,  year_total[10]={0}, order_total[10]={0};
int ypol[12]={0};

srand((unsigned int)time(0));

	// initialize array a
cout<<"INITIALIZE Object array a"<<endl;
for(i=0;i<N;i++) { 
		a[i]=prod(); 
		 //a[i].print();
}
//pak();

for(i=0;i<N;i++) { 
		b[i]=new_prod(); 
		 //b[i].print();
}

ifstream fg;
fg.open("prod2018.txt");
	if(!fg) {
				cout<<"file does not open"<<endl;
				exit(1);
			}
	k=0;
	 cout<<"--------------------------------------------"<<endl;
	 cout<<"DISPLAY DATA FROM FILE"<<endl;
	 //pak();
	while(!fg.eof())
	{  // read from file
		//sum=0;
		fg>>in_date;  for (i=0;i<10;i++)        { fg>>in_day[i]; }
		fg>> in_pp;   for (i=0;i<2*in_pp;i++)   { fg>>in_couples[i];	}
			k++;
		// display data from txt file
			cout<<in_date<<" ";
			for (i=0;i<10;i++) cout<<in_day[i]<<" ";
			cout<<endl;
			cout<<in_pp<<"  couples :";
			for (i=0;i<2*in_pp;i++) cout <<in_couples[i]<<"  ";
			cout<<endl<<endl; 
	
//	cout<<"lines from text file = "<<k-1<<endl;
	
	//pre-processing  phase 
	
	// fill array production_total[10][12] !!!  lines=products - columns=months
			m=in_date%100 - 1; // month number
			for (j=0;j<10;j++) 
			    production_total[j][m]+=in_day[j];   
			    
	// fill array demand_total[10][12] !!
		for (j=0;j<in_pp*2;j=j+2)
		{	product_number=in_couples[j]-1; demand_quantity=in_couples[j+1];
			demand_total[product_number][m]+=demand_quantity;
		}

	// ERWTHMA 2 - FILL DYNAMIC Array
	// initialize array z
	for(w=0;w<10;w++) {z[w][0]=0; z[w][1]=0;}
	
	if (flag==0) {y1=new int; flag=1;}
	for (int w=0;w<10;w++) z[w][0]=in_day[w];
	for (int w=0;w<2*in_pp;w+=2) z[in_couples[w]-1][1]+=in_couples[w+1];

	// check each product
	for (int w=0;w<10;w++)
		if (z[w][0]-z[w][1]<0)
		{
		// triplets
			y1[count]=m;
			y1[count+1]=w;
			y1[count+2]=-(z[w][0]-z[w][1]);
			count=count+3;
		};

	}; // end while not eof()	
cout<<"count="<<count<<endl;
//display arrays totals
cout<<"production TOTALS"<<endl;
for (i=0;i<10;i++)
{
	for (j=0;j<12;j++) cout<<production_total[i][j]<<" ";
	cout<<endl;
}
cout<<"demand TOTALS"<<endl;
for (i=0;i<10;i++)
{
	for (j=0;j<12;j++) cout<<demand_total[i][j]<<" ";
	cout<<endl;
}
//pak();
//insert 120 objects into array a and display objects in array a
cout<<"insert AND display objects - ARRAY a"<<endl;
for (i=0;i<10;i++)
{
		for (j=0;j<12;j++)
	{
		a[i*10+j]=prod(i+1,j+1,production_total[i][j], demand_total[i][j]);
		a[i*10+j].print();
	};
//	pak();
};
//pak(); 

// ERWTHMA 3 - years totals
for (i=0;i<10;i++)
	for (j=0;j<12;j++)
	{
			year_total[i]+=a[i*10+j].get_month_total();
			order_total[i]+=a[i*10+j].get_demand_total();
	}
	
	//display year_total array
for( int zz=0;zz<10;zz++) 
	{	cout<<"product ="<<zz+1<<"   year total =";
		cout<<year_total[zz]<<"    orders total= "<<order_total[zz]<<endl;
	};
//pak();

// 	ERWTHMA 4 - DISPLAY FROM DYNAMIC ARRAY DATA
// get product code 
cout<<"product code="; cin>>p_code;
for (w=0;w<count*3;w+=3)
if (y1[w+1]==p_code)
	{	ypol[y1[w]]+=y1[w+2];	}	
	
	//display month ypol totals
	for (w=0;w<12;w++) 
	{		cout<<"month="<<w<<"     ypol="<<ypol[w]<<endl;  }
//pak();


// ERWTHMA 5 - INHERITANCE 
//initialize and display derived class new_prod - initialization  problems in compilers not support C++ 11 !!!!!!
cout<<endl<<endl<<" class new_prod - array of objects : initialization ! "<<endl;
	for (ii=0;ii<N;ii++) 
		{	
			b[ii]=new_prod();
		//	b[ii].new_print();
		}
	
		//pak();
		//copy 
		cout<<endl<<endl<<" class new_prod - copying objects..... "<<endl;
		count=0;
		for (i=0;i<10;i++)
		{      
           	for (j=0;j<12;j++)
			{			
				// create random values 
				for (v=0;v<10;v++) 
				{
				in_production_cost[v]=(rand()%30)*1.0+2.0;
				cout<<in_production_cost[v]<<"  ";
				}
				cout<<endl<<endl;
												
				b[i*10+j]=new_prod(a[i*10+j].get_code(), a[i*10+j].get_month(),a[i*10+j].get_month_total(),a[i*10+j].get_demand_total(), in_production_cost);
				b[i*10+j].new_print();
				metr++;
			}
		cout<<"metr = "<<metr<<endl;
		}
return 0;
} // end main

