////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //PIMS: Personal Investment Management System //Class : dataRepository //This class is used as an access layer between the database (which) //in our case is a simple file system and the computational components. The //methods have been commented to give the full picture of the layer. //The present implementation supports only single user but the framework //has been built in a fashion so that it could be easily extended for multiuser. //This would require to make multiple directories for users instead of a single //directory ".pims". ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// import java.io.*; import java.util.*; public class dataRepository{ private static String PIMS_HOME; public static String DATA_HOME; private static int MAX_ALERTS; private static int MAX_PORTFOLIOS; private static int MAX_SECURITIES; private static int MAX_TRANSACTIONS; private static int MAX_SHARES; private BufferedReader BR; private FileWriter FW; public dataRepository(){ try{ //read the home directory specified by the user during the installation process. BR = new BufferedReader(new FileReader("./user_home")); PIMS_HOME = BR.readLine() + "/.pims/"; BR.close(); //set the directory of the data. DATA_HOME = PIMS_HOME + "/Investment/"; //set the maximum number of alerts allowed. MAX_ALERTS = 500; //Set the maximum number of portfolios allowed. MAX_PORTFOLIOS = 100; //Set the maximum number of securities allowed in a portfolio. MAX_SECURITIES = 100; //Set the maximum number of transactions allowed in a security MAX_TRANSACTIONS = 500; //Set the maximum number of shares in the database MAX_SHARES = 1500; }catch(Exception e){} } //Writes a line of string into the specified file public void writeLine(FileWriter fw, String Line)throws Exception{ int i=0; int length = Line.length(); char buffer[] = new char[length]; Line.getChars(0, length, buffer, 0); fw.write(buffer); fw.write('\n'); } //Reads and returns the list of portfolios in the Investment public String[] ReadPortfolioList()throws Exception{ int i=0; String name; String PortfolioList[] = new String[MAX_PORTFOLIOS]; BR = new BufferedReader(new FileReader(DATA_HOME+"portfolios")); while(true){ name = BR.readLine(); if(name == null)break; PortfolioList[i] = name; i++; } BR.close(); return(PortfolioList); } //Writes a list of portfolio in the "portfolios" file private boolean WritePortfolioList(String PortfolioList[]){ int i=0; try{ FW = new FileWriter(DATA_HOME+"portfolios"); while(PortfolioList[i] != null){ writeLine(FW, PortfolioList[i]); i++; } FW.close(); return(true); }catch(Exception e){return(false);} } //Adds a portfolio into the investment public boolean AddPortfolio(String PortfolioName){ int i=0; String PortfolioList[]; try{ //read portfolios PortfolioList = ReadPortfolioList(); //add portfolios while(PortfolioList[i] != null)i++; PortfolioList[i] = PortfolioName; //write portfolios WritePortfolioList(PortfolioList); //create the portfolio directory if(!(new File(DATA_HOME + PortfolioName)).mkdir())return(false); //Create securities file if(!(new File(DATA_HOME+PortfolioName+"/securities")).createNewFile())return(false); return(true); }catch(Exception e){return(false);} } //Deletes a portfolio public boolean DeletePortfolio(String PortfolioName){ int i=0; String PortfolioList[]; try{ //read portfolios PortfolioList = ReadPortfolioList(); //delete portfolio while(!PortfolioName.equals(PortfolioList[i]))i++; while(PortfolioList[i+1] != null){ PortfolioList[i] = PortfolioList[i+1]; i++; } PortfolioList[i] = null; //write portfolios WritePortfolioList(PortfolioList); //Delete the portfolio directory String Files[] = (new File(DATA_HOME+PortfolioName)).list(); //delete all the files in the portfolio directory for(i=0; icredit, 1=> debit String line; Transaction TransactionList[] = new Transaction[MAX_TRANSACTIONS]; BR = new BufferedReader(new FileReader(DATA_HOME+PortfolioName+"/"+SecurityName)); StringTokenizer ST; while(true){ line = BR.readLine(); if(line == null)break; ST = new StringTokenizer(line, "~"); Details = ST.nextToken(); Day = Integer.parseInt(ST.nextToken()); Month = Integer.parseInt(ST.nextToken()); Year = Integer.parseInt(ST.nextToken()); AmountTransacted = Double.parseDouble(ST.nextToken()); TransType = (ST.nextToken().equals("credit"))?(false):(true); TransactionList[i] = new Transaction(Details, Day, Month, Year, AmountTransacted, TransType); i++; } BR.close(); return(TransactionList); } //Return the transaction list for share private Transaction[] ReadTransactionListForShare(String PortfolioName, String SecurityName)throws Exception{ int i=0; boolean Bank; String Details; int Day; int Month; int Year; int NumShares; double CostOfShare; boolean TransType;//For shares 0=> buy 1=> sell String line; Transaction TransactionList[] = new Transaction[MAX_TRANSACTIONS]; BR = new BufferedReader(new FileReader(DATA_HOME+PortfolioName+"/"+SecurityName)); StringTokenizer ST; while(true){ line = BR.readLine(); if(line == null)break; ST = new StringTokenizer(line, "~"); Details = ST.nextToken(); Day = Integer.parseInt(ST.nextToken()); Month = Integer.parseInt(ST.nextToken()); Year = Integer.parseInt(ST.nextToken()); NumShares = Integer.parseInt(ST.nextToken()); CostOfShare = Double.parseDouble(ST.nextToken()); TransType = (ST.nextToken().equals("buy"))?(false):(true); TransactionList[i] = new Transaction(Details, Day, Month, Year, NumShares, CostOfShare, TransType); i++; } BR.close(); return(TransactionList); } //Write a transaction list private void WriteTransactionList(String PortfolioName, SecurityDS SecurityData, Transaction TransactionList[])throws Exception{ if(SecurityData.Bank) WriteTransactionListForBank(PortfolioName, SecurityData.SecurityName, TransactionList); else WriteTransactionListForShare(PortfolioName, SecurityData.SecurityName, TransactionList); } //Write the transaction list for bank private void WriteTransactionListForBank(String PortfolioName, String SecurityName, Transaction TransactionList[])throws Exception{ int i=0; String line; FW = new FileWriter(DATA_HOME+PortfolioName+"/"+SecurityName); while(TransactionList[i] != null){ line = TransactionList[i].Details; line += "~"+Integer.toString(TransactionList[i].Day); line += "~"+Integer.toString(TransactionList[i].Month); line += "~"+Integer.toString(TransactionList[i].Year); line += "~"+String.valueOf(TransactionList[i].AmountTransacted); line += "~"+((TransactionList[i].TransType)?("debit"):("credit")); writeLine(FW, line); i++; } FW.close(); } //Write the transaction list for share private void WriteTransactionListForShare(String PortfolioName, String SecurityName, Transaction TransactionList[])throws Exception{ int i=0; String line; FW = new FileWriter(DATA_HOME+PortfolioName+"/"+SecurityName); while(TransactionList[i] != null){ line = TransactionList[i].Details; line += "~"+Integer.toString(TransactionList[i].Day); line += "~"+Integer.toString(TransactionList[i].Month); line += "~"+Integer.toString(TransactionList[i].Year); line += "~"+Integer.toString(TransactionList[i].NumShares); line += "~"+String.valueOf(TransactionList[i].CostOfShare); line += "~"+((TransactionList[i].TransType)?("sell"):("buy")); writeLine(FW, line); i++; } FW.close(); } //Add a transaction public boolean AddTransaction(String PortfolioName, SecurityDS SecurityData, Transaction Trans)throws Exception{ int i=0; Transaction TransactionList[]; //Read the transaction list TransactionList = ReadTransactionList(PortfolioName, SecurityData); //Add the transaction while(TransactionList[i] != null)i++; TransactionList[i] = Trans; //write the transaction list WriteTransactionList(PortfolioName, SecurityData, TransactionList); return(true); } //Delete a transaction public boolean DeleteTransaction(String PortfolioName, SecurityDS SecurityData, int index)throws Exception{ int i=0; Transaction TransactionList[]; //read the transaction TransactionList = ReadTransactionList(PortfolioName, SecurityData); //delete the transaction for(i = index;TransactionList[i+1] != null;++i)TransactionList[i] = TransactionList[i+1]; TransactionList[i] = null; //write back the transaction list WriteTransactionList(PortfolioName, SecurityData, TransactionList); return(true); } //Edit a tranasction public boolean EditTransaction(String PortfolioName, SecurityDS SecurityData, int index, Transaction Trans)throws Exception{ Transaction TransactionList[]; //read the transaction TransactionList = ReadTransactionList(PortfolioName, SecurityData); //edit the transaction TransactionList[index] = Trans; //write back the transaction list WriteTransactionList(PortfolioName, SecurityData, TransactionList); return(true); } /////////////////////////////////////////////////File Operations Related to Alerts///////////////////////////////////////////////////////// //Read all the alerts in the alerts file. public String[][] readAlerts()throws Exception{ int i=0; String line; String alertList[][] = new String[MAX_ALERTS][2]; BR = new BufferedReader(new FileReader(PIMS_HOME+"alerts")); StringTokenizer ST; while(true){ line = BR.readLine(); if(line == null)break; ST = new StringTokenizer(line, ":"); alertList[i][0] = ST.nextToken(); alertList[i][1] = ST.nextToken(); i++; } BR.close(); return(alertList); } //Updates the Alerts file with the new alert list public void updateAlertsFile(String alertList[][])throws Exception{ int i = 0; FileWriter FW = new FileWriter(PIMS_HOME+"alerts"); String line=""; while(true){ if(alertList[i][0] == null)break; line = alertList[i][0]+":"+alertList[i][1]; writeLine(FW, line); i++; } FW.close(); } //Delete an alert from the alerts file public void deleteAlert(int index)throws Exception{ int i; String alertList[][] = readAlerts(); for(i=index;alertList[i+1][0] != null;i++){ alertList[i][0] = alertList[i+1][0]; alertList[i][1] = alertList[i+1][1]; } alertList[i][0] = null; updateAlertsFile(alertList); } ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////Methods Related to the Security manager///////////////////////////////////////////// //Returns : by reading from the login file public String GetAuthentication()throws Exception{ String res; BR = new BufferedReader(new FileReader(PIMS_HOME+"log_file")); res = BR.readLine(); BR.close(); return(res); } //Changes the password of the user public boolean changePassword(String NewPassword)throws Exception{ String line; BR = new BufferedReader(new FileReader(PIMS_HOME+"log_file")); line = BR.readLine(); BR.close(); StringTokenizer ST = new StringTokenizer(line, ":"); FW = new FileWriter(PIMS_HOME+"log_file"); writeLine(FW, (ST.nextToken()+":"+NewPassword)); FW.close(); return(true); } ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////Methods related to share prices////////////////////////////////////////////////////////// //Reads share prices from file public String[][] readSharePriceFromFile()throws Exception{ int i=0; String line; String Shares[][] = new String[MAX_SHARES][2]; BR = new BufferedReader(new FileReader(PIMS_HOME+"prices")); StringTokenizer ST; while(true){ line = BR.readLine(); if(line == null)break; ST = new StringTokenizer(line, ":"); Shares[i][0] = ST.nextToken(); Shares[i][1] = ST.nextToken(); i++; } return(Shares); } //Write share prices into the prices file public void writeSharePrices(int NumShares, String Shares[][])throws Exception{ int i=0; FW = new FileWriter(PIMS_HOME+"prices"); for(i=0;i