//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*
PIMS: Personal Invstment Management System
Class : Portfolio
Descrption: This class correspond to a Portfolio. Any
access to Portfolio (access/modification/edit portfolios/securities/
transaction etc.) is done through the instance of this class.
The attributes are:
1> A list of all Security Names with attributes.
2> Net-worth of the portfolio
3> ComputeNetWorth(): Returns the value of Net-Worth. Each time the
       Investment is accessed, the value of the Net-Worth is modified.
4> ComputeROI(): Computes the ROI of a particular security
5> Add/Delete/Rename/Edit    Security/Transaction
*/
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

public class Portfolio{

	private dataRepository DR;
	private CurrentValueSystem CVS;
	private String PortfolioName;//Name of this portfolio
	private SecurityDS SecurityList[];//A list of all Securities
	private int NumSecurities;//Number of securities
	private double NetWorth;//The networth of the Portfolio
	
	public Portfolio(dataRepository DR, CurrentValueSystem CVS, String PortfolioName)throws Exception{
	
		this.DR = DR;
		this.CVS = CVS;
		this.PortfolioName = PortfolioName;
		
		//Initialize the Security list  by reading from the 
		//previously initialized file "securities" in the 
		//HOME/.pims/Investment directory		
		SecurityList = DR.ReadSecurityList(PortfolioName);
		//Count the number of securities
		while(SecurityList[NumSecurities] != null)NumSecurities++;
	}
	
	public boolean ReturnTypeOfSecurity(String SecurityName)throws Exception{
		
		int i;
		for(i=0;i<NumSecurities;++i){
			if(SecurityName.equals(SecurityList[i].SecurityName))return(SecurityList[i].Bank);
		}
		return(true);
	}
	
	//Returns the number of shares in a share type security.
	public int numShares(String SecurityName, int index)throws Exception{
		
		int i;
		for(i=0;i<NumSecurities;++i)
			if(SecurityName.equals(SecurityList[i].SecurityName))break;
			
		security sec = new security(DR, CVS, PortfolioName, SecurityList[i]);
		
		return(sec.numShares(index));
	}
	
	//Returns the list of securities
	public SecurityDS[] ReturnSecurityList()throws Exception{
		return(SecurityList);
	}
	
	//Returns the trasaction list
	public Transaction[] ReturnTransactionList(String SecurityName)throws Exception{
	
		int i;
		for(i=0;i<NumSecurities;++i)
			if(SecurityName.equals(SecurityList[i].SecurityName))break;
			
		security sec = new security(DR, CVS, PortfolioName, SecurityList[i]);
		
		return(sec.ReturnTransactionList());
	}
	
	//Returns the net worth of this portfolio
	public double ComputeNetWorth()throws Exception{

		//Compute the net worth of the portfolio
		NetWorth = 0.0;
		for(int i=0;i<NumSecurities;++i){
			NetWorth += (new security(DR, CVS, PortfolioName, SecurityList[i])).ComputeNetWorth();
		}
		
		return(NetWorth);
	}
	
	//Returns the net worth of the specified security
	public double ComputeNetWorth(String SecurityName)throws Exception{
	
		int i;
		for(i=0;i<NumSecurities;++i)
			if(SecurityName.equals(SecurityList[i].SecurityName))break;
		
			
		security sec = new security(DR, CVS, PortfolioName, SecurityList[i]);
		
		return(sec.ComputeNetWorth());
	}
	
	//Checks if a security by the name SecurityName already exists
	public boolean CheckSecurityName(String SecurityName)throws Exception{
	
		for(int i=0;i<NumSecurities;++i)
			if(SecurityName.equals(SecurityList[i].SecurityName))return(true);
			
		return(false);		
	}
	
	//Adds a security to this portfolio
	public boolean AddSecurity(SecurityDS SecurityData)throws Exception{
	
		//Do the file opertaions related to adding security
		if(!DR.AddSecurity(PortfolioName, SecurityData))return(false);
		
		//Add the security in the Security List
		SecurityList[NumSecurities++] = SecurityData;
		return(true);
	}
	
	//Deletes a security from this portfolio
	public boolean DeleteSecurity(String SecurityName)throws Exception{
	
		int i;
		for(i=0;i<NumSecurities;++i)
			if(SecurityName.equals(SecurityList[i].SecurityName))break;
			
		security sec = new security(DR, CVS, PortfolioName, SecurityList[i]);
		
		//Do the file opertaions related to deleting a security
		if(!DR.DeleteSecurity(PortfolioName, SecurityName))return(false);
		
		//Remove the security from the security list
		for(i=0;i<NumSecurities;++i)
			if(SecurityName.equals(SecurityList[i].SecurityName))break;
		for(;i<NumSecurities;++i)
			SecurityList[i] = SecurityList[i+1];
		NumSecurities--;
		
		//Compute the new net worth
		NetWorth -= sec.ComputeNetWorth();
		
		return(true);
	}
	
	public boolean RenameSecurity(String OldSecurityName, String NewSecurityName)throws Exception{
	
		//Do the related file operations
		if(!DR.RenameSecurity(PortfolioName, OldSecurityName, NewSecurityName));
		
		//Rename the security in the security list
		for(int i=0;i < NumSecurities;++i)
			if(OldSecurityName.equals(SecurityList[i].SecurityName)){
				SecurityList[i].SecurityName = NewSecurityName;
				break;
			}
			
		return(true);
	}
	
	//Adds a transaction into a specified security of this portfolio
	public boolean AddTransaction(String SecurityName, Transaction Trans)throws Exception{
	
		int i;
		for(i=0;i<NumSecurities;++i)
			if(SecurityName.equals(SecurityList[i].SecurityName))break;
			
		security sec = new security(DR, CVS, PortfolioName, SecurityList[i]);
		
		NetWorth -= sec.ComputeNetWorth();
		
		if(!sec.AddTransaction(Trans))return(false);
		
		//Modify the net worth
		NetWorth += sec.ComputeNetWorth();
		
		return(true);
	}
	
	//Deletes a transaction from a security of this portfolio
	public boolean DeleteTransaction(String SecurityName, int index)throws Exception{
	
		int i;
		for(i=0;i<NumSecurities;++i)
			if(SecurityName.equals(SecurityList[i].SecurityName))break;
			
		security sec = new security(DR, CVS, PortfolioName, SecurityList[i]);
		
		NetWorth -= sec.ComputeNetWorth();
		
		if(!sec.DeleteTransaction(index))return(false);
		
		//Modify the net worth of this portfolio
		NetWorth += sec.ComputeNetWorth();
		
		return(true);
	}
	
	public boolean EditTransaction(String SecurityName, int index, Transaction Trans)throws Exception{
		int i;
		for(i=0;i<NumSecurities;++i)
			if(SecurityName.equals(SecurityList[i].SecurityName))break;
			
		security sec = new security(DR, CVS, PortfolioName, SecurityList[i]);
		
		NetWorth -= sec.ComputeNetWorth();
		
		if(!sec.EditTransaction(index, Trans))return(false);
		
		//Modify the net worth of this portfolio
		NetWorth += sec.ComputeNetWorth();
		
		return(true);	
	}
	//Edits a transaction of a security in this portfolio
	public boolean EditSecurity(String SecurityName, int index, Transaction trans)throws Exception{
	
		int i;
		for(i=0;i<NumSecurities;++i)
			if(SecurityName.equals(SecurityList[i].SecurityName))break;
			
		security sec = new security(DR, CVS, PortfolioName, SecurityList[i]);
		
		NetWorth -= sec.ComputeNetWorth();
		
		if(!sec.EditTransaction(index, trans))return(false);
		
		//Modify the net worth of this portfolio
		NetWorth += sec.ComputeNetWorth();
		
		return(true);
	}
	
	//Computes the ROI of a security in this portfolio
	public double ComputeROI(String SecurityName)throws Exception{
		
		int i;
		for(i=0;i<NumSecurities;++i)
			if(SecurityName.equals(SecurityList[i].SecurityName))break;
			
		security sec = new security(DR, CVS, PortfolioName, SecurityList[i]);
		
		return(sec.ComputeROI());
	}
}
