diff options
| author | clarkohw <66530369+clarkohw@users.noreply.github.com> | 2021-04-19 16:17:36 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-04-19 16:17:36 -0400 |
| commit | 58dbfe9d7e511e47c8b50078f060911de74b1d4b (patch) | |
| tree | e91feb27c711f65814753b0a41ad49da76cf62ea /src/main/java/edu/brown/cs | |
| parent | 534d0cc5070287b221fa77f5dd564dd4544b5780 (diff) | |
| parent | 3780077f257b973426577d6168936a3ce0a904e3 (diff) | |
Merge pull request #14 from cs0320-2021/profit-tests
more profit testing
Diffstat (limited to 'src/main/java/edu/brown/cs')
| -rw-r--r-- | src/main/java/edu/brown/cs/student/term/profit/ProfitCalculation.java | 35 | ||||
| -rw-r--r-- | src/main/java/edu/brown/cs/student/term/profit/StockHolding.java | 26 |
2 files changed, 46 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 0a19bcb..0ef87c3 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,11 @@ 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; +import java.util.Map; import java.util.*; public class ProfitCalculation { @@ -243,11 +248,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() @@ -293,6 +299,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(); @@ -325,11 +336,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() @@ -365,6 +377,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 = @@ -402,15 +418,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 |
