#include<cmath>
#include<vector>
#include<iostream>
#include"cash_flow.h"

using namespace std;

cash_flow::cash_flow() { times.push_back(0.0); amounts.push_back(0.0);};

cash_flow::cash_flow(istream& is)
{
	double t,c;
	cout<<"Enter times and amounts:\n";
	while(is>>t>>c)
	{
		times.push_back(t);
		amounts.push_back(c);
	}

};

vector<double> cash_flow::get_times(){
	return times;
};
vector<double> cash_flow::get_amounts(){
	return amounts;
};


double cash_flow::principal_value_discrete(const double &r) {

	double PV=0.0;
	/*vector<double>::const_iterator t=times.begin();
	vector<double>::const_iterator a=amounts.begin();

		for(t=times.begin();t!=times.end();++t,++a)
		{
			PV+=*a/pow(1+r,*t);
		}
		return PV;
}*/
	for(size_t t=0;t<times.size();t++)
	{
		PV+=amounts[t]/pow((1+r),times[t]);
	}
	return PV;
}

double cash_flow::principal_value_cont(const double& r) {

	double PV=0.0;

/*	for(size_t t=0;t<times.size();t++)
	{
		PV+=exp(-r*times[t])*amounts[t];
	}
	return PV;
}*/
vector<double>::const_iterator t=times.begin();
	vector<double>::const_iterator a=amounts.begin();

		for(;t!=times.end();++t,++a)
		{
			PV+=*a*exp(-r*(*t));
		}
		return PV;
}
double cash_flow::IRR()
{
	double r1=0.0;
	double r2=1.0;
	double r;
	const double PREC=.000001;

	/* First we try to bracket the zero i.e. we find values 
		for r1 and r2 such that PV(r1) and PV(r2) have opposite
		signs i.e. PV(r1)*PV(r2)<0*/

	for(int i=0;i<=50;i++){
	
	double pv1=principal_value_cont(r1);
	double pv2=principal_value_cont(r2);

	if(pv1*pv2<0) break;
	 
	r1--;
	r2++;
	}
 return 0;
	
}



	


	
		




	

