////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /* PIMS: Personal Invstment Management System Class : Security Descrption: This class correspond to a Security. Any access to Security (access/modification/edit transaction etc.) is done through the instance of this class. The attributes are: 1> A list of all transactions. 2> Net-worth of the security 3> ComputeNetWorth(): Computes the net worth for this security 4> ComputeROI(): Computes the ROI of this security 5> Add/Delete/Edit Transaction */ ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// import java.util.*; import java.text.*; public class security{ private dataRepository DR; private CurrentValueSystem CVS; private String PortfolioName;//Name of the parent portfolio private SecurityDS SecurityData;//Information about this security. private Transaction TransactionList[];//A list of all transactions private int NumTransactions;//Number of transactions private double NetWorth;//The net worth of the security private final static double EPSILON = 0.1; private final static double INITIAL_RATE = 0.0; private final static double STEP_RATE = 0.5; public security(dataRepository DR, CurrentValueSystem CVS, String PortfolioName, SecurityDS SecurityData)throws Exception{ this.DR = DR; this.CVS = CVS; this.PortfolioName = PortfolioName;; this.SecurityData = SecurityData; //Initialize the transactions list by reading from the //previously initialized file named by the security in the //HOME/.pims/Investment/ directory TransactionList = DR.ReadTransactionList(PortfolioName, SecurityData); //Count the number of securities while(TransactionList[NumTransactions] != null)NumTransactions++; } //Returns the transaction list public Transaction[] ReturnTransactionList()throws Exception{ return(TransactionList); } //Checks if transaction 1 happened before transaction 2 private boolean dateLessThan(Transaction Trans1, Transaction Trans2){ if(Trans1.Year < Trans2.Year)return(true); if(Trans1.Year > Trans2.Year)return(false); //transactions happened in the same year if(Trans1.Month < Trans2.Month)return(true); if(Trans1.Month > Trans2.Month)return(false); //transactions happened in the same year and same month if(Trans1.Day < Trans2.Day)return(true); if(Trans1.Day > Trans2.Day)return(false); //transactions happened on the same date return(false); } //Sorts the transaction list based on the transaction date private Transaction[] sortTransactions(){ int j,k; Transaction tempTransaction; Transaction trans[] = new Transaction[NumTransactions]; //Copy the transaction list for(k=0;k0) && (dateLessThan(tempTransaction, trans[j-1])) ; --j) trans[j] = trans[j-1]; trans[j] = tempTransaction; // put element in his new index. } return(trans); } //Computes the month difference private int calculateMonthDifference(int year2, int month2, int day2, int year1, int month1, int day1){ return(((year2 - year1)*365 + (month2 - month1)*30 + day2 - day1)/30); } //returns sign appropriately private double Sign(boolean TransType){ if(TransType)return(-1.0); else return(1.0); } //Computation of net worth for bank type private double NetWorthCalculationForBank()throws Exception{ double value = 0.0; double rate = SecurityData.RateOfInterest; int i, prevDay, prevMonth, prevYear, months; int Month, Year, Day; String line; if(NumTransactions == 0)return(0.0); //Sort the transactions based on the date Transaction trans[] = sortTransactions(); value = Sign(trans[0].TransType)*trans[0].AmountTransacted; prevYear = trans[0].Year; prevMonth = trans[0].Month; prevDay = trans[0].Day; for(i=1;i net_worth)prevGuess = true; else prevGuess = false; while(true){ net_worth_guessed = giveNetWorthValueForRate(rate); if(Math.abs(net_worth_guessed - net_worth) < EPSILON)break; if(net_worth_guessed > net_worth)thisGuess = true; else thisGuess = false; if(!prevGuess && thisGuess){ rate -= step_rate/2; step_rate = step_rate/2; } if(prevGuess && !thisGuess){ rate += step_rate/2; step_rate = step_rate/2; } if(!prevGuess && !thisGuess){ rate += step_rate; } if(prevGuess && thisGuess){ rate -= step_rate; } prevGuess = thisGuess; } return(rate); } }