aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/main/java/edu/brown/cs/student/term/Main.java4
-rw-r--r--src/main/java/edu/brown/cs/student/term/ProfitCalculation.java75
2 files changed, 71 insertions, 8 deletions
diff --git a/src/main/java/edu/brown/cs/student/term/Main.java b/src/main/java/edu/brown/cs/student/term/Main.java
index 3800837..7838dd3 100644
--- a/src/main/java/edu/brown/cs/student/term/Main.java
+++ b/src/main/java/edu/brown/cs/student/term/Main.java
@@ -55,9 +55,7 @@ public final class Main {
e.printStackTrace();
}
- person.organizeOrders();
-
- System.out.println("hello");
+ person.calculateGains();
// HashMap<String, Command> commandHashMap = new HashMap<>();
// /** add commands to map here! */
diff --git a/src/main/java/edu/brown/cs/student/term/ProfitCalculation.java b/src/main/java/edu/brown/cs/student/term/ProfitCalculation.java
index 66891ce..998ea26 100644
--- a/src/main/java/edu/brown/cs/student/term/ProfitCalculation.java
+++ b/src/main/java/edu/brown/cs/student/term/ProfitCalculation.java
@@ -1,5 +1,20 @@
package edu.brown.cs.student.term;
+
+import org.json.JSONObject;
+
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.net.HttpURLConnection;
+import java.net.MalformedURLException;
+import java.net.ProtocolException;
+import java.net.URI;
+import java.net.URL;
+import java.net.http.HttpClient;
+import java.net.http.HttpRequest;
+import java.net.http.HttpResponse;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
@@ -54,7 +69,7 @@ public class ProfitCalculation {
/**
* This method fills the maps of sell and buy orders with lists of oldest - new trades
*/
- public void organizeOrders() {
+ private void organizeOrders() {
//get a list of trades for a person to consider
try {
PreparedStatement prep;
@@ -107,7 +122,7 @@ public class ProfitCalculation {
/**
* This method processes the sell orders in the sellHistoryMap to get realized gains
*/
- public void getRealizedGains() {
+ private void getRealizedGains() {
for (String ticker : sellHistoryMap.keySet()) {
//use FIFO selling
LinkedList<OrderTuple> sells = sellHistoryMap.get(ticker);
@@ -151,7 +166,7 @@ public class ProfitCalculation {
/**
* get the change in value of stocks which are still held
*/
- public void getUnrealizedGains() {
+ private void getUnrealizedGains() {
//calculate change in value of holdings
for (String ticker : buyHistoryMap.keySet()) {
@@ -167,7 +182,6 @@ public class ProfitCalculation {
}
}
-
private class OrderTuple {
private int shares;
private double cost;
@@ -197,9 +211,60 @@ public class ProfitCalculation {
}
public double getCurrentPrice(String ticker) {
- return 100.0;
+ //get request for body string
+ //convert body to JSONObject
+ //object.getDouble("price")
+
+ String API_KEY = "NW169NGWKDG9UGXO";
+ String url = "https://www.alphavantage.co/query?function=GLOBAL_QUOTE&symbol="
+ + ticker
+ + "&apikey="
+ + API_KEY;
+
+ HttpClient client = HttpClient.newHttpClient();
+ HttpRequest request = HttpRequest.newBuilder()
+ .uri(URI.create(url))
+ .build();
+
+ HttpResponse<String> response = null;
+ try {
+ response = client.send(request,
+ HttpResponse.BodyHandlers.ofString());
+ } catch (IOException e) {
+ e.printStackTrace();
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+
+ System.out.println(response.body());
+ JSONObject object = new JSONObject(response.body());
+ return object.getJSONObject("Global Quote").getDouble("05. price");
+ }
+
+ public void calculateGains() {
+ organizeOrders();
+ getRealizedGains();
+ getUnrealizedGains();
+ double totalGains = 0;
+ double realizedGains = 0;
+ double unrealizedGains = 0;
+
+ for (double value : realizedGainsMap.values()) {
+ realizedGains += value;
+ }
+
+ for (double value : unrealizedGainsMap.values()) {
+ unrealizedGains += value;
+ }
+
+ totalGains = unrealizedGains + realizedGains;
+
+ System.out.println(
+ "Total: " + totalGains + "| unrealized: " + unrealizedGains + " | realized: " +
+ realizedGains);
}
+
public void setConnection(String filename) throws SQLException, ClassNotFoundException {
// Initialize the database connection, turn foreign keys on