diff options
Diffstat (limited to 'src/main/java')
| -rw-r--r-- | src/main/java/edu/brown/cs/student/term/profit/ProfitCalculation.java | 31 | ||||
| -rw-r--r-- | src/main/java/edu/brown/cs/student/term/profit/StockHolding.java | 26 |
2 files changed, 42 insertions, 15 deletions
diff --git a/src/main/java/edu/brown/cs/student/term/profit/ProfitCalculation.java b/src/main/java/edu/brown/cs/student/term/profit/ProfitCalculation.java index 77c1c3a..18b79be 100644 --- a/src/main/java/edu/brown/cs/student/term/profit/ProfitCalculation.java +++ b/src/main/java/edu/brown/cs/student/term/profit/ProfitCalculation.java @@ -19,6 +19,7 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; +import java.text.SimpleDateFormat; import java.util.LinkedList; import java.util.HashMap; import java.util.List; @@ -228,11 +229,12 @@ public class ProfitCalculation { if (currentStockPrices.containsKey(ticker)) { return currentStockPrices.get(ticker); } else { + SimpleDateFormat localDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX"); String url = "https://data.alpaca.markets/v1/bars/" + "day?" + "symbols=" + ticker - + "&start=" + startTime - + "&end=" + endTime; + + "&start=" + localDateFormat.format(startTime) + + "&end=" + localDateFormat.format(endTime); HttpClient client = HttpClient.newHttpClient(); HttpRequest request = HttpRequest.newBuilder() @@ -278,6 +280,11 @@ public class ProfitCalculation { } public List<StockHolding> getHoldingsList() { + if (conn == null) { + System.out.println("ERROR: No database connection"); + return new LinkedList<>(); + } + if (!tablesFilled) { organizeOrders(); getRealizedGains(); @@ -310,11 +317,12 @@ public class ProfitCalculation { * return percent change in SPY (SP 500) over the time period. */ public double compareToSP500() { + SimpleDateFormat localDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX"); String url = "https://data.alpaca.markets/v1/bars/" + "day?" + "symbols=SPY" - + "&start=" + startTime.toString() + "T09:30:00-04:00" - + "&end=" + endTime.toString() + "T09:30:00-04:00"; + + "&start=" + localDateFormat.format(startTime) + + "&end=" + localDateFormat.format(endTime); HttpClient client = HttpClient.newHttpClient(); HttpRequest request = HttpRequest.newBuilder() @@ -350,6 +358,10 @@ public class ProfitCalculation { */ public Map<Integer, Double> getProfitMap() { Map<Integer, Double> profitMap = new HashMap<>(); + if (conn == null) { + System.out.println("ERROR: no database connection"); + return profitMap; + } try { PreparedStatement prep; prep = @@ -387,15 +399,4 @@ public class ProfitCalculation { tablesFilled = false; } - public void setConnection(String filename) throws SQLException, ClassNotFoundException { - - // Initialize the database connection, turn foreign keys on - Class.forName("org.sqlite.JDBC"); - String urlToDB = "jdbc:sqlite:" + filename; - conn = DriverManager.getConnection(urlToDB); - - Statement stat = conn.createStatement(); - stat.executeUpdate("PRAGMA foreign_keys=ON;"); - } - } diff --git a/src/main/java/edu/brown/cs/student/term/profit/StockHolding.java b/src/main/java/edu/brown/cs/student/term/profit/StockHolding.java index f7924f2..5edb5f7 100644 --- a/src/main/java/edu/brown/cs/student/term/profit/StockHolding.java +++ b/src/main/java/edu/brown/cs/student/term/profit/StockHolding.java @@ -1,11 +1,21 @@ package edu.brown.cs.student.term.profit; +/** + * class to map holding info for JSON. + */ public class StockHolding { private String ticker; private Double realizedGain; private Double unrealizedGain; private int shares; + /** + * constructor. + * @param ticker - stock. + * @param realizedGain realized gain. + * @param unrealizedGain unrealized gain. + * @param shares - number of shares + */ public StockHolding(String ticker, Double realizedGain, Double unrealizedGain, int shares) { this.ticker = ticker; this.realizedGain = realizedGain; @@ -13,11 +23,27 @@ public class StockHolding { this.shares = shares; } + /** + * getter method. + * @return realized gain. + */ public Double getRealizedGain() { return realizedGain; } + /** + * getter method. + * @return unrealized gain. + */ public Double getUnrealizedGain() { return unrealizedGain; } + + /** + * getter method for testing. + * @return shares. + */ + public int getShares() { + return shares; + } }
\ No newline at end of file |
