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.java26
-rw-r--r--src/main/java/edu/brown/cs/student/term/ProfitCalculation.java121
2 files changed, 139 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 c436b2e..e6584f1 100644
--- a/src/main/java/edu/brown/cs/student/term/Main.java
+++ b/src/main/java/edu/brown/cs/student/term/Main.java
@@ -5,11 +5,15 @@ import edu.brown.cs.student.term.repl.REPL;
import joptsimple.OptionParser;
import joptsimple.OptionSet;
+import java.sql.Connection;
+import java.sql.Date;
+import java.sql.DriverManager;
+import java.sql.SQLException;
+import java.sql.Statement;
import java.util.HashMap;
/**
* The Main class of our project. This is where execution begins.
- *
*/
public final class Main {
@@ -18,8 +22,7 @@ public final class Main {
/**
* The initial method called when execution begins.
*
- * @param args
- * An array of command line arguments
+ * @param args An array of command line arguments
*/
public static void main(String[] args) {
new Main(args).run();
@@ -36,7 +39,7 @@ public final class Main {
OptionParser parser = new OptionParser();
parser.accepts("gui");
parser.accepts("port").withRequiredArg().ofType(Integer.class)
- .defaultsTo(DEFAULT_PORT);
+ .defaultsTo(DEFAULT_PORT);
OptionSet options = parser.parse(args);
if (options.has("gui")) {
@@ -44,11 +47,18 @@ public final class Main {
//runSparkServer((int) options.valueOf("port"));
}
- HashMap<String, Command> commandHashMap = new HashMap<>();
- /** add commands to map here! */
- REPL repl = new REPL(commandHashMap);
- repl.runREPL();
+
+ ProfitCalculation person = new ProfitCalculation(null, "Vincent", new Date(1615629591000L), new Date(1615507898000L));
+ person.setConnection("./data/mock_tradeClarks.sqlite3");
+ person.calculateGains();
+
+// HashMap<String, Command> commandHashMap = new HashMap<>();
+// /** add commands to map here! */
+// REPL repl = new REPL(commandHashMap);
+// repl.runREPL();
// TODO: Process commands in a REPL
}
+
+
} \ No newline at end of file
diff --git a/src/main/java/edu/brown/cs/student/term/ProfitCalculation.java b/src/main/java/edu/brown/cs/student/term/ProfitCalculation.java
new file mode 100644
index 0000000..7c275d2
--- /dev/null
+++ b/src/main/java/edu/brown/cs/student/term/ProfitCalculation.java
@@ -0,0 +1,121 @@
+package edu.brown.cs.student.term;
+
+import java.sql.Connection;
+import java.sql.Date;
+import java.sql.DriverManager;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.HashMap;
+
+public class ProfitCalculation {
+ private Connection conn;
+ private String person;
+ Date startTime;
+ Date endTime;
+
+ /**
+ * constructor for ProfitCalculation.
+ *
+ * @param conn - database connection, with trade data.
+ * @param person - person of interest to calculate profit for.
+ * @param startTime - start of period to look at.
+ * @param endTime - end of period to look at.
+ */
+ public ProfitCalculation(Connection conn, String person, Date startTime, Date endTime) {
+ this.conn = conn;
+ this.person = person;
+ this.startTime = startTime;
+ this.endTime = endTime;
+ }
+
+ /**
+ * calculate the gains for a given person in a specified time.
+ *
+ * @return
+ */
+ public double calculateGains() {
+ HashMap<String, LinkedList<BuyTuple>> buyHistory = new HashMap<String, LinkedList<BuyTuple>>();
+ HashMap<String, Double> perStockGain = new HashMap<String, Double>();
+ double totalGain = 0;
+ //query for List<Trades> per person
+
+ try {
+ PreparedStatement prep;
+ prep = conn.prepareStatement("SELECT * FROM \'users\' WHERE user_id= ?;");
+ //prep.setString(1, id);
+ ResultSet rs = prep.executeQuery();
+ while (rs.next()) {
+ boolean buy = true;
+ String ticker = "STOCK";
+ //for buy orders, build up buy history
+ if (buy) {
+ int sharesBought = 99;
+ double cost = 420.0;
+
+ //add a buy to the end of a history for a given stock
+ if (buyHistory.containsKey(ticker)) {
+ buyHistory.get(ticker).addLast(new BuyTuple(sharesBought, cost));
+ } else {
+ LinkedList<BuyTuple> tickerHistory = new LinkedList<>();
+ tickerHistory.add(new BuyTuple(sharesBought, cost));
+ buyHistory.put(ticker, tickerHistory);
+ }
+ //for sell orders, calculate realized gains
+ } else {
+// int sharesSold = 99;
+// if (buyHistory.containsKey(ticker)) {
+// LinkedList<BuyTuple> =
+// }
+
+ }
+
+ }
+ prep.close();
+ } catch (SQLException e) {
+ System.out.println("ERROR: sql error getting trades");
+ }
+
+
+ //FOR EACH RESULT in ResultsSet...
+ //if buy: add to hashmap tuple entry (shares, cost) to hashmap
+ // - get cost of stock at time of purchase
+ //if sell: check if we hold this stock. If so use FIFO to sell
+
+ return totalGain;
+ }
+
+ private class BuyTuple {
+ private int shares;
+ private double cost;
+
+ private BuyTuple(int shares, double cost) {
+ this.shares = shares;
+ this.cost = cost;
+ }
+
+ public double getCost() {
+ return cost;
+ }
+
+ public int getShares() {
+ return shares;
+ }
+ }
+
+ 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;");
+ }
+
+
+}