PortFolio.java
PortFolio.java
package toBeCompleted;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Arrays;
public class PortFolio {
public ArrayList stocksHeld = new ArrayList();
//DO NOT MODIFY
public PortFolio(String dir) throws FileNotFoundException {
File directory = new File(dir);
File[] files = directory.listFiles();
Arrays.sort(files);
for(File file: files) {
String baseName = file.getName().substring(0, file.getName().indexOf("."));
stocksHeld.add(new PortFolioEntry(dir, baseName));
}
}
//DO NOT MODIFY
public PortFolioEntry getStockDetailsByHandler(String handler) {
for(PortFolioEntry stock: stocksHeld) {
if(stock.handler.equals(handler)) {
return stock;
}
}
return null; //not found
}
/**
* sort the stocks in ascending order of returns
* note that returns are calculated using PortFolioEntry.compareTo
* we say,
* PortFolioEntry e1 has higher returns than PortFolioEntry e2, if e1.compareTo(e2) is 1
* PortFolioEntry e1 has lower returns than PortFolioEntry e2, if e1.compareTo(e2) is -1
* PortFolioEntry e1 has the same return as PortFolioEntry e2, if e1.compareTo(e2) is 0
*/
public void sort() {
stocksHeld.sort((PortFolioEntry e1, PortFolioEntry e2)->e1.compareTo(e2));
}
//DO NOT MODIFY
public String toString() {
String result = "";
double cost = 0;
double currentPrice = 0;
for(PortFolioEntry entry: stocksHeld) {
result = result + entry.toString() + "\n";
cost = cost + entry.getTotalPurchasePrice();
currentPrice = currentPrice + entry.getTotalCurrentPrice();
}
result = result+"Total Purchase Price: "+cost+"\n";
result = result+"Current Price: "+currentPrice+"\n";
result = result + "Change: "+(currentPrice - cost) + " ("+((currentPrice - cost)*100/cost)+"%)";
return result;
}
}
PortFolioEntry.java
PortFolioEntry.java
package toBeCompleted;
import doNotModify.DailyTrade;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Scanner;
public class PortFolioEntry {
public StockHistoricalData historicalData;
public int sharesHeld;
public double purchasePrice;
public String handler;
//DO NOT MODIFY
public PortFolioEntry(String dir, String filename) throws FileNotFoundException {
ArrayList
data = new ArrayList();
handler = filename;
FileReader reader = new FileReader(dir+"/"+filename+".csv");
@SuppressWarnings("resource")
Scanner scanner = new Scanner(reader);
String firstLine = scanner.nextLine();
int idx = firstLine.indexOf(',');
String str = firstLine.substring(0, idx);
sharesHeld = Integer.parseInt(str);
firstLine = firstLine.substring(idx+1);
idx = firstLine.indexOf(',');
str = firstLine.substring(0, idx);
purchasePrice = Double.parseDouble(str);
scanner.nextLine(); //ignore header line
while(scanner.hasNextLine()) {
String line = scanner.nextLine();
data.add(new DailyTrade(line));
}
historicalData = new StockHistoricalData(data);
}
/**
* DO NOT MODIFY
* @return the total price paid for acquiring the stock.
* so if you bought 2 stocks @ 1.523 per stock, the method should return 3.046
*/
public double getTotalPurchasePrice() {
return purchasePrice * sharesHeld;
}
/**
* DO NOT MODIFY
* @return the current market value of the stock
*/
public double getTotalCurrentPrice() {
return historicalData.getLastPrice() * sharesHeld;
}
/**
* DO NOT MODIFY
* @return overall change in value from the time of purchase to the current time
* in the overall value of the stock.
* IMPORTANT: use the outcome of getTotalPurchasePrice() as the purchase price and
* the outcome of getTotalCurrentPrice as current market price
*/
public double changeInPrice() {
return getTotalCurrentPrice() - getTotalPurchasePrice();
}
/**
* DO NOT MODIFY
* @return percentage change, rounded off to 2-decimal points, in the overall value of the stock
* IMPORTANT: use the outcome of getTotalPurchasePrice() as the purchase price and
* the outcome of getTotalCurrentPrice as current market price
*/
public double getPercentageChangeInPrice() {
return (getTotalCurrentPrice() - getTotalPurchasePrice())*100/getTotalPurchasePrice();
}
/**
*
* @param other
* @return 1 if calling object's percentage change in price is more than parameter object's percentage change in price
* @return -1 if calling object's percentage change in price is less than parameter object's percentage change in price
*
* in case the calling object and the parameter object have the same percentage change:
* @return 1 if calling object's overall change in price is more than parameter object's overall change in price
* @return -1 if calling object's overall change in price is less than parameter object's overall change in price
* @return 0 if calling object's overall change in price is same as parameter object's overall change in price
*/
public int compareTo(PortFolioEntry other) {
if(this.getPercentageChangeInPrice() > other.getPercentageChangeInPrice()){
return 1;
}
else if(this.getPercentageChangeInPrice() < other.getPercentageChangeInPrice()){
return -1;
}
else{
if(this.getTotalPurchasePrice() > other.getTotalPurchasePrice()){
return 1;
}
else if(this.getTotalPurchasePrice() < other.getTotalPurchasePrice()){
return -1;
}
else {
return 0;
}
}
}
...