package isip.java.bullyse.bullydb;
import java.io.*;
import java.util.*;
import java.sql.*;
import java.text.*;
import javax.servlet.*;
import javax.servlet.http.*;
// This file handles the buying and selling of stocks in the
// " + sessionbase.getName() + "
public class buysell extends HttpServlet {
// Create a new BullyDB object
static BullyDB sessionbase = new BullyDB();
// System parameters are stored in the database, retrieve the values
// through the BullyDB object.
static String SystemURL = sessionbase.getURL();
static String ServletURL = sessionbase.getServletURL();
static String SystemEmail = sessionbase.getEmail();
static String SystemPath = sessionbase.getPath();
static String SystemName = sessionbase.getName();
static String SystemShortName = sessionbase.getShortName();
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
// Initialize variables
String AccountId = (String)null;
String task = request.getParameter("task");
HttpSession session = request.getSession();
String SessionId = session.getId();
// Retrieve AccountId
AccountId = sessionbase.getAccountId(SessionId);
if (AccountId == null) {
response.sendRedirect(SystemURL + "/html/errors/not_logged_in.html");
} else {
// Make sure user isn't locked
if (sessionbase.checkAccount(SessionId)) {
task = "Locked";
}
if(task == null) {
// Set up output stream
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String Symbol = (String)null;
String Trans = (String)null;
String NumS = (String)null;
String Price = (String)null;
Symbol = request.getParameter("Symbol");
Trans = request.getParameter("Trans");
NumS = request.getParameter("NumShares");
Price = request.getParameter("Price");
// Print top template for Buy and Sell
sessionbase.printTop(out, 4);
sessionbase.QuoteFlash(out);
// Fill in the blanks from the template
out.println("");
out.println("
");
out.println("");
out.println("");
out.println(" ");
// Print link to view all current offers before Buy and Sell form
out.println("View pending offers on all stock.
");
// Output form for user to make selections on
out.println("");
// Print user holdings
sessionbase.showHoldings(out, 1, SessionId);
// Output bottom template for Buy and Sell
out.println(" | ");
out.println("");
sessionbase.printBot(out, SessionId);
out.close();
} else if (task.equals("Locked")) {
response.sendRedirect(ServletURL + ".Portfolio?task=Locked");
} else if(task.equals("Status")) {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String Trans = request.getParameter("Trans");
String Symbol = request.getParameter("Symbol");
// Give user the option of canceling their trade offer
sessionbase.printTop(out, 4);
sessionbase.QuoteFlash(out);
// Fill in the blanks from the template
out.println(" | ");
out.println("");
out.println("");
out.println("");
out.println(" ");
if (Trans.equals("FB") || Trans.equals("FS")) {
out.println("This transaction has been frozen for evaluation by " + SystemName + " advisors. You can delete the transaction or send an email to " + SystemEmail + " for information why the task has been frozen. ");
}
out.println("Do you wish to delete the queued task? ");
out.println("Note: This will permanentely delete the offer from the");
out.println("queue. ");
out.println("");
out.println("");
out.println("");
out.println(" | ");
out.println("");
sessionbase.printBot(out, SessionId);
out.close();
} else if(task.equals("confirmDel")) {
String Trans = request.getParameter("Trans");
String Symbol = request.getParameter("Symbol");
Symbol = Symbol.toLowerCase();
ResultSet result = null;
int numShares = 0;
int oldShares = 0;
double buyAt = 0.0;
String query = (String)null;
DecimalFormat numFormat = new DecimalFormat("################0.00");
DecimalFormat intFormat = new DecimalFormat("################0");
if (Trans.equals("FS") || Trans.equals("FB")) {
sessionbase.doUpdate("lock tables " + sessionbase.getUsername(SessionId) + " write, qt" + Symbol.toLowerCase() + " write, Portfolio write, Frozen write", SessionId);
} else {
sessionbase.doUpdate("lock tables " + sessionbase.getUsername(SessionId) + " write, qt" + Symbol.toLowerCase() + " write, Portfolio write", SessionId);
}
query = "select NumShares, tradeAt from " + sessionbase.getUsername(SessionId) + " where Symbol = '" + Symbol + "' and Trans = '" + Trans + "'";
result = sessionbase.doQuery(query, SessionId);
try {
while (result.next()) {
numShares = result.getInt("NumShares");
buyAt = result.getDouble("tradeAt");
}
} catch(Exception e) {
e.printStackTrace();
}
if (numShares < 1) {
sessionbase.doUpdate("unlock tables", SessionId);
response.sendRedirect(SystemURL + "/html/members/errors/stale.html");
} else {
if (Trans.equals("S")) {
query = "delete from " + sessionbase.getUsername(SessionId) + " where Symbol = '" + Symbol + "' and Trans = 'S'";
sessionbase.doUpdate(query, SessionId);
query = "delete from qt" + Symbol.toLowerCase() + " where AccountId = " + sessionbase.getAccountId(SessionId) + " and Trans = 'S'";
sessionbase.doUpdate(query, SessionId);
// Check to see if user has already existing shares in
// Fixed status.
query = "select NumShares from " + sessionbase.getUsername(SessionId) + " where Symbol = '" + Symbol + "' and Trans = 'C'";
result = sessionbase.doQuery(query, SessionId);
try {
while(result.next()) {
oldShares = result.getInt("NumShares");
}
} catch(Exception e) {
e.printStackTrace();
}
if (oldShares > 0) {
int adSh = oldShares + numShares;
query = "update " + sessionbase.getUsername(SessionId) + " set NumShares = " + adSh + " where Symbol = '" + Symbol + "' and Trans = 'C'";
sessionbase.doUpdate(query, SessionId);
} else {
query = "insert into " + sessionbase.getUsername(SessionId) + " (Symbol, NumShares, Trans, Dtime) values ('" + Symbol + "', " + intFormat.format(numShares) + ", 'C', NOW())";
sessionbase.doUpdate(query, SessionId);
}
sessionbase.doUpdate("unlock tables", SessionId);
response.sendRedirect(ServletURL + ".buysell");
} else if (Trans.equals("B")) {
double balance = 0.00;
query = "select Balance from Portfolio where AccountId = " + sessionbase.getAccountId(SessionId);
result = sessionbase.doQuery(query, SessionId);
try {
while (result.next()) {
balance = result.getDouble("Balance");
}
} catch(Exception e) {
e.printStackTrace();
}
balance += buyAt * numShares;
query = "update Portfolio set Balance = " + numFormat.format(balance) + " where AccountId = " + sessionbase.getAccountId(SessionId);
sessionbase.doUpdate(query, SessionId);
query = "delete from " + sessionbase.getUsername(SessionId) + " where Symbol = '" + Symbol + "' and Trans = 'B'";
sessionbase.doUpdate(query, SessionId);
query = "delete from qt" + Symbol.toLowerCase() + " where AccountId = " + sessionbase.getAccountId(SessionId) + " and Trans = 'B'";
sessionbase.doUpdate(query, SessionId);
sessionbase.doUpdate("unlock tables", SessionId);
response.sendRedirect(ServletURL + ".buysell");
} else if (Trans.equals("FB")) {
query = "delete from " + sessionbase.getUsername(SessionId) + " where Symbol = '" + Symbol + "' and Trans = 'FB'";
sessionbase.doUpdate(query, SessionId);
query = "select * from Frozen where AccountId = " + sessionbase.getAccountId(SessionId) + " and Trans = 'B'";
result = sessionbase.doQuery(query, SessionId);
try {
while (result.next()) {
numShares = result.getInt("NumShares");
buyAt = result.getDouble("tradeAt");
}
} catch (Exception e) {
e.printStackTrace();
}
query = "select Balance from Portfolio where AccountId = " + sessionbase.getAccountId(SessionId);
result = sessionbase.doQuery(query, SessionId);
double Balance = 0.00;
try {
while (result.next()) {
Balance = result.getDouble("Balance");
}
} catch (Exception e) {
e.printStackTrace();
}
Balance += (numShares * buyAt);
query = "update Portfolio set Balance = " + Balance + " where AccountId = " + sessionbase.getAccountId(SessionId);
sessionbase.doUpdate(query, SessionId);
query = "delete from Frozen where AccountId = " + sessionbase.getAccountId(SessionId) + " and trans = 'B' and Symbol = '" + Symbol + "'";
sessionbase.doUpdate(query, SessionId);
sessionbase.doUpdate("unlock tables", SessionId);
response.sendRedirect(ServletURL + ".buysell");
} else if (Trans.equals("FS")) {
query = "delete from " + sessionbase.getUsername(SessionId) + " where Symbol = '" + Symbol + "' and Trans = 'FS'";
sessionbase.doUpdate(query, SessionId);
query = "select * from Frozen where AccountId = " + sessionbase.getAccountId(SessionId) + " and Trans = 'S'";
result = sessionbase.doQuery(query, SessionId);
try {
while (result.next()) {
numShares = result.getInt("NumShares");
buyAt = result.getDouble("tradeAt");
}
} catch (Exception e) {
e.printStackTrace();
}
// Check to see if user already has Fixed shares of this
// type and just update that. Otherwise, insert the new
// amount.
query = "select NumShares from " + sessionbase.getUsername(SessionId) + " where Symbol = '" + Symbol + "' and Trans = 'C'";
result = sessionbase.doQuery(query, SessionId);
try {
while (result.next()) {
oldShares = result.getInt("NumShares");
}
} catch (Exception e) {
e.printStackTrace();
}
if (oldShares > 0) {
oldShares += numShares;
query = "update " + sessionbase.getUsername(SessionId) + " set NumShares = " + oldShares + " where Symbol = '" + Symbol + "' and Trans = 'C'";
sessionbase.doUpdate(query, SessionId);
} else {
query = "insert into " + sessionbase.getUsername(SessionId) + " (Symbol, NumShares, tradeAt, Trans, Dtime) values ('" + Symbol + "', " + numShares + ", " + buyAt + ", 'C', NOW())";
sessionbase.doUpdate(query, SessionId);
}
query = "delete from Frozen where AccountId = " + sessionbase.getAccountId(SessionId) + " and trans = 'S' and Symbol = '" + Symbol + "'";
sessionbase.doUpdate(query, SessionId);
sessionbase.doUpdate("unlock tables", SessionId);
response.sendRedirect(ServletURL + ".buysell");
}
}
} else if(task.equals("confirmBuy")) {
// Initialize variables
String Symbol = request.getParameter("Symbol");
String nums = request.getParameter("numShares");
String buyVal = request.getParameter("askPrice");
Timestamp now = null;
Double buyTemp = Double.valueOf(buyVal);
double buyAt = buyTemp.doubleValue();
Integer NumS = Integer.valueOf(nums);
int offerShares = NumS.intValue();
Offers currentOffer = new Offers(Symbol, buyAt, offerShares, now, sessionbase.getAccountId(SessionId));
doBuy(currentOffer, SessionId, response);
response.sendRedirect(ServletURL + ".buysell");
} else if(task.equals("confirmSell")) {
// Initialize variables
String Symbol = request.getParameter("Symbol");
String nums = request.getParameter("numShares");
String buyVal = request.getParameter("askPrice");
Timestamp now = null;
Double buyTemp = Double.valueOf(buyVal);
double buyAt = buyTemp.doubleValue();
Integer NumS = Integer.valueOf(nums);
int offerShares = NumS.intValue();
Offers currentOffer = new Offers(Symbol, buyAt, offerShares, now, sessionbase.getAccountId(SessionId));
doSell(currentOffer, SessionId, response);
response.sendRedirect(ServletURL + ".buysell");
}
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException, NumberFormatException
{
// Initialize variables
String task = request.getParameter("task");
String AccountId = (String)null;
String query = (String)null;
// Initialize output stream
response.setContentType("text/html");
PrintWriter out = response.getWriter();
HttpSession session = request.getSession();
String SessionId = session.getId();
// Retrieve AccountId
AccountId = sessionbase.getAccountId(SessionId);
DecimalFormat numFormat = new DecimalFormat("################0.00");
DecimalFormat intFormat = new DecimalFormat("################0");
if (AccountId == null) {
response.sendRedirect(SystemURL + "/html/errors/not_logged_in.html");
} else {
// Make sure user isn't locked
if (sessionbase.checkAccount(SessionId)) {
task = "Locked";
}
if (task == null) {
sessionbase.printTop(out, 4);
sessionbase.QuoteFlash(out);
// Fill in the blanks from the template
out.println(" | ");
out.println("");
out.println("");
out.println("");
out.println(" ");
out.println("Error: ");
out.println("You must select whether to \"buy\" or \"sell\".");
out.println("Please go back and correct the problem. ");
out.println(" | ");
out.println("");
sessionbase.printBot(out, SessionId);
out.close();
} else if (task.equals("Locked")) {
response.sendRedirect(ServletURL + ".Portfolio?task=Locked");
} else if (task.equals("buy")) {
// Initialize variables
String Symbol = request.getParameter("symbol");
String nums = request.getParameter("numShares");
String askP = request.getParameter("askPrice");
Integer convInt;
int numShares = 0;
// Cost per share container
double shareCost = 0.00;
// Current balance container
double Balance = 0.00;
// Total cost of purchase container
double tCost = 0.00;
// End balance container
double endBalance = 0.00;
// Available shares container
int availShares = 0;
// Remaining shares available
int endShares = 0;
ResultSet result = null;
int error = 0;
Double contP;
double askPrice = 0.00;
String numCheck = "";
char Checkit;
int lenCheck = 0;
String Locked = "";
DecimalFormat curFormat = new DecimalFormat("$##,###,###,###,###,##0.00");
// Is trading currently frozen?
sessionbase.loadParams();
if (sessionbase.checkFreeze()) {
sessionbase.printTop(out, 4);
sessionbase.QuoteFlash(out);
// Fill in the blanks from the template
out.println(" | ");
out.println("");
out.println("");
out.println("");
out.println(" ");
out.println(" | ");
out.println(" Notice: ");
out.println("Trading is currently frozen on " + SystemName + ". ");
out.println("Please try your transaction again later. ");
out.println(" | ");
out.println("");
out.println("");
sessionbase.printBot(out, SessionId);
out.close();
error++;
}
if (error == 0) {
// Everything is type String, convert everything to its proper type.
askP = askP.trim();
lenCheck = askP.length();
// Filter out $ and , from the Cost
for (int i = 0 ; i < lenCheck ; i++) {
Checkit = askP.charAt(i);
if (Checkit != '$' && Checkit != ',') {
numCheck += Checkit;
}
}
askP = numCheck;
try {
convInt = new Integer(nums);
numShares = convInt.intValue();
contP = new Double(askP);
askPrice = contP.doubleValue();
} catch(NumberFormatException e) {
// User must have entered something else wrong
sessionbase.printTop(out, 4);
sessionbase.QuoteFlash(out);
// Fill in the blanks from the template
out.println(" | ");
out.println("");
out.println("");
out.println("");
out.println(" ");
out.println("Error: ");
out.println("Invalid characters entered.");
out.println("Please go back and correct the problem. ");
out.println(" | ");
out.println("");
sessionbase.printBot(out, SessionId);
out.close();
error++;
}
}
// Make sure this isn't a Company account trying to buy something
// other than itself
query = "select Type from Account where AccountId = " + AccountId;
result = sessionbase.doQuery(query, SessionId);
String Type = (String)null;
try {
while (result.next()) {
Type = result.getString("Type");
}
} catch (Exception e) {
e.printStackTrace();
}
if (Type.equals("C") && error == 0) {
String Sym1 = Symbol.toLowerCase();
String Sym2 = sessionbase.getUsername(SessionId);
Sym2 = Sym2.toLowerCase();
if (!Sym1.equals(Sym2)) {
sessionbase.printTop(out, 4);
sessionbase.QuoteFlash(out);
// Fill in the blanks from the template
out.println(" | ");
out.println("");
out.println("");
out.println("");
out.println(" ");
out.println("Error: ");
out.println("Company accounts are only permitted to buy and sell their own stock.");
out.println(" | ");
out.println("");
sessionbase.printBot(out, SessionId);
out.close();
error++;
}
}
// Make sure user isn't trying to sell 0 shares or < $0.01
if ((numShares < 1 || askPrice < 0.01) && error == 0) {
sessionbase.printTop(out, 4);
sessionbase.QuoteFlash(out);
// Fill in the blanks from the template
out.println(" | ");
out.println("");
out.println("");
out.println("");
out.println(" ");
out.println("Error: ");
out.println("0 and negative values not allowed for Shares or Amount.");
out.println("Go back and enter a different number. ");
out.println(" | ");
out.println("");
sessionbase.printBot(out, SessionId);
out.close();
error++;
}
// Make sure they entered a valid company and that it isn't locked
query = "Select Last, Locked from Companies where Symbol = '" + Symbol + "'";
result = sessionbase.doQuery(query, SessionId);
try {
while(result.next()) {
shareCost = result.getDouble("Last");
Locked = result.getString("Locked");
}
} catch(Exception e) {
e.printStackTrace();
}
if(shareCost == 0.00 && error == 0) {
sessionbase.printTop(out, 4);
sessionbase.QuoteFlash(out);
// Fill in the blanks from the template
out.println(" | ");
out.println("");
out.println("");
out.println("");
out.println(" ");
out.println("Error: ");
out.println("You have entered an invalid symbol.");
out.println("Please go back and enter a different one. ");
out.println(" | ");
out.println("");
sessionbase.printBot(out, SessionId);
out.close();
error++;
}
if (Locked.equals("Y") && error == 0) {
sessionbase.printTop(out, 4);
sessionbase.QuoteFlash(out);
// Fill in the blanks from the template
out.println(" | ");
out.println("");
out.println("");
out.println("");
out.println(" ");
out.println("Error: ");
out.println("The stock you are trying to buy is currently frozen, no buy or sell transactions can take place until this is lifted.");
out.println(" | ");
out.println("");
sessionbase.printBot(out, SessionId);
out.close();
error++;
}
// Get user balance and make sure they have enough money
query = "select Balance from Portfolio where AccountId = " + AccountId;
result = sessionbase.doQuery(query, SessionId);
try {
while(result.next()) {
Balance = result.getDouble("Balance");
}
} catch(Exception e) {
e.printStackTrace();
}
tCost = 20 + (askPrice * numShares);
endBalance = Balance - tCost;
if(endBalance < 0 && error == 0) {
sessionbase.printTop(out, 4);
sessionbase.QuoteFlash(out);
// Fill in the blanks from the template
out.println(" | ");
out.println("");
out.println("");
out.println("");
out.println(" ");
out.println("Error: ");
out.println("You cannot afford the number of stocks requested. Keep in mind there is a $20 commission on each transaction.");
out.println("Please go back and enter a different one. ");
out.println(" | ");
out.println("");
sessionbase.printBot(out, SessionId);
out.close();
error++;
}
// Check to see if user already has an offer out for this symbol
query = "select Symbol from " + sessionbase.getUsername(SessionId) + " where Symbol = '" + Symbol + "' and Trans = 'B'";
result = sessionbase.doQuery(query, SessionId);
String Sym = (String)null;
try {
while(result.next()) {
Sym = result.getString("Symbol");
}
} catch(Exception e) {
e.printStackTrace();
}
if (Sym != null) {
response.sendRedirect(SystemURL + "/html/members/errors/offer_exists.html");
}
if (error == 0) {
// Everything must be ok. Prompt user to ensure they
// really want to do the trade.
sessionbase.printTop(out, 4);
sessionbase.QuoteFlash(out);
// Fill in the blanks from the template
out.println(" | ");
out.println("");
out.println("");
out.println("");
out.println(" ");
out.println("Confirm ");
out.println("Please confirm your purchase bid: ");
// Make sure the transaction doesn't go outside the boundaries
// of whatever filters are in place.
// All the real checking for this, for buy or sell transactions,
// is done by a class in BullyDB. Any changes to the actual rules
// for the filters will need to be done there. Variables for the
// rules are currently stored in the Database and can be modified
// from the Parameters section of the Administrative pages.
if (!sessionbase.checkRules(Symbol, numShares, askPrice, "B", 1, SessionId)) {
out.println(" Notice: ");
out.println("As listed, your transaction will be frozen for review by an Exchange Administrator. Checks are put into place to prevent transactions done simply to manipulate the market value. You can either cancel the transaction and modify it or send it through and let an Administrator decide on its approval. Email " + SystemEmail + " with any questions.
");
}
out.println("" + numShares + " shares of " + Symbol.toUpperCase() + " at " + curFormat.format(askPrice) + " per share. ");
out.println("Transaction Cost: " + curFormat.format(tCost) + " ");
out.println("Commission Cost: $20 ");
out.println("Ending Balance: " + curFormat.format(endBalance) + " ");
out.println(" ");
out.println(" | ");
out.println("");
sessionbase.printBot(out, SessionId);
out.close();
}
} else if(task.equals("sell")) {
// Initialize variables
String Symbol = request.getParameter("symbol");
String nums = request.getParameter("numShares");
String askP = request.getParameter("askPrice");
int userShares = 0;
double Balance = 0.00;
double shareCost = 0.00;
double endBalance = 0.00;
double Profit = 0.00;
int error = 0;
Integer convInt;
int numShares = 0;
String numCheck = "";
int lenCheck = 0;
char Checkit;
Double contP;
double askPrice = 0.00;
String Locked = "";
// Is trading currently frozen?
sessionbase.loadParams();
if (sessionbase.checkFreeze()) {
sessionbase.printTop(out, 4);
sessionbase.QuoteFlash(out);
// Fill in the blanks from the template
out.println(" | ");
out.println("");
out.println("");
out.println("");
out.println(" ");
out.println(" | ");
out.println(" Notice: ");
out.println("Trading is currently frozen on " + SystemName + ". ");
out.println("Please try your transaction again later. ");
out.println(" | ");
out.println("");
out.println("");
sessionbase.printBot(out, SessionId);
out.close();
error++;
}
if (error == 0) {
askP = askP.trim();
lenCheck = askP.length();
for (int i = 0 ; i < lenCheck ; i++) {
Checkit = askP.charAt(i);
if (Checkit != '$' && Checkit != ',') {
numCheck += Checkit;
}
}
askP = numCheck;
try {
convInt = new Integer(nums);
numShares = convInt.intValue();
contP = new Double(askP);
askPrice = contP.doubleValue();
} catch(NumberFormatException e) {
sessionbase.printTop(out, 4);
sessionbase.QuoteFlash(out);
// Fill in the blanks from the template
out.println(" | ");
out.println("");
out.println("");
out.println("");
out.println(" ");
out.println("Error: ");
out.println("Invalid characters entered.");
out.println("Please go back and correct the problem. ");
out.println(" | ");
out.println("");
sessionbase.printBot(out, SessionId);
out.close();
error++;
}
}
ResultSet result = null;
DecimalFormat curFormat = new DecimalFormat("$##,###,###,###,###,###,##0.00");
// Make sure they entered a valid company
query = "select Last, Locked from Companies where Symbol = '" + Symbol + "'";
result = sessionbase.doQuery(query, SessionId);
try {
while(result.next()) {
shareCost = result.getDouble("Last");
Locked = result.getString("Locked");
}
} catch(Exception e) {
e.printStackTrace();
}
if (shareCost == 0.00 && error == 0) {
sessionbase.printTop(out, 4);
sessionbase.QuoteFlash(out);
// Fill in the blanks from the template
out.println(" | ");
out.println("");
out.println("");
out.println("");
out.println(" ");
out.println("Error: ");
out.println("The Symbol you have entered doesn't exist.");
out.println("Go back and enter a valid symbol. ");
out.println(" | ");
out.println("");
sessionbase.printBot(out, SessionId);
error++;
out.close();
}
// Make sure user isn't trying to sell 0 shares or < $0.01
if ((numShares < 1 || askPrice < 0.01) && error == 0) {
sessionbase.printTop(out, 4);
sessionbase.QuoteFlash(out);
// Fill in the blanks from the template
out.println(" | ");
out.println("");
out.println("");
out.println("");
out.println(" ");
out.println("Error: ");
out.println("0 and negative values not allowed for Shares or Amount.");
out.println("Go back and enter a different number. ");
out.println(" | ");
out.println("");
sessionbase.printBot(out, SessionId);
out.close();
error++;
}
// Make sure user has the number of shares they try to sell
query = "Select NumShares from " + sessionbase.getUsername(SessionId) + " where Symbol = '" + Symbol + "' and Trans = 'C'";
result = sessionbase.doQuery(query, SessionId);
try {
while(result.next()) {
userShares = result.getInt("NumShares");
}
} catch(Exception e) {
e.printStackTrace();
}
if ((userShares - numShares) < 0 && error == 0) {
sessionbase.printTop(out, 4);
sessionbase.QuoteFlash(out);
// Fill in the blanks from the template
out.println(" | ");
out.println("");
out.println("");
out.println("");
out.println(" ");
out.println("Error: ");
out.println("You don't have that many shares to sell.");
out.println("Go back and enter a different number. ");
out.println(" | ");
out.println("");
sessionbase.printBot(out, SessionId);
out.close();
error++;
}
if (Locked.equals("Y") && error == 0) {
sessionbase.printTop(out, 4);
sessionbase.QuoteFlash(out);
// Fill in the blanks from the template
out.println(" | ");
out.println("");
out.println("");
out.println("");
out.println(" ");
out.println("Error: ");
out.println("The stock you are trying to buy is currently frozen, no buy or sell transactions can take place until this is lifted.");
out.println(" | ");
out.println("");
sessionbase.printBot(out, SessionId);
out.close();
error++;
}
// Check to see if user already has an offer out for this symbol
query = "select Symbol from " + sessionbase.getUsername(SessionId) + " where Symbol = '" + Symbol + "' and Trans = 'S'";
result = sessionbase.doQuery(query, SessionId);
String Sym = (String)null;
try {
while(result.next()) {
Sym = result.getString("Symbol");
}
} catch(Exception e) {
e.printStackTrace();
}
if (Sym != null && error == 0) {
response.sendRedirect(SystemURL + "/html/members/errors/offer_exists.html");
error++;
}
if (error == 0) {
// Everything seems ok, confirm the sale
// Get user's balance
query = "select Balance from Portfolio where AccountId = " + AccountId;
result = sessionbase.doQuery(query, SessionId);
try {
while(result.next()) {
Balance = result.getDouble("Balance");
}
} catch(Exception e) {
e.printStackTrace();
}
Profit = (numShares * askPrice) - 20;
endBalance = Balance + Profit;
sessionbase.printTop(out, 4);
sessionbase.QuoteFlash(out);
// Fill in the blanks from the template
out.println(" | ");
out.println("");
out.println("");
out.println("");
out.println(" ");
out.println("Confirm ");
out.println("Please confirm your sale bid: ");
// Make sure the transaction doesn't go outside the boundaries
// of whatever filters are in place.
// All the real checking for this, for buy or sell transactions,
// is done by a class in BullyDB. Any changes to the actual rules
// for the filters will need to be done there. Variables for the
// rules are currently stored in the Database and can be modified
// from the Parameters section of the Administrative pages.
if (!sessionbase.checkRules(Symbol, numShares, askPrice, "S", 1, SessionId)) {
out.println(" Notice: ");
out.println("As listed, your transaction will be frozen for review by an Exchange Administrator. Checks are put into place to prevent transactions done simply to manipulate the market value. You can either cancel the transaction and modify it or send it through and let an Administrator decide on its approval. Email " + SystemEmail + " with any questions.
");
}
out.println("" + numShares + " shares of " + Symbol.toUpperCase() + " at " + curFormat.format(askPrice) + " per share. ");
out.println("Sale Value: " + curFormat.format(Profit + 20) + " ");
out.println("Commission Cost: $20 ");
out.println("Ending Balance: " + curFormat.format(endBalance) + " ");
out.println(" ");
out.println(" | ");
out.println("");
sessionbase.printBot(out, SessionId);
out.close();
}
}
}
}
public class Offers {
private String Symbol;
private double offerValue;
private int numShares;
private Timestamp setAt;
private String AcId;
public Offers () {
this.Symbol = (String)null;
this.offerValue = 0.00;
this.numShares = 0;
this.setAt = null;
this.AcId = (String)null;
}
public Offers (String Symbol, double offerValue, int numShares, Timestamp setAt, String ActId) {
this.Symbol = Symbol;
this.offerValue = offerValue;
this.numShares = numShares;
this.setAt = setAt;
this.AcId = ActId;
}
public String getSymbol() { return Symbol; }
public double getValue() { return offerValue; }
public int getShares() { return numShares; }
public Timestamp getTimestamp() { return setAt; }
public String getAccountId() { return AcId; }
public void setSymbol(String Sym) {
Symbol = Sym;
}
public void setValue(double Value) {
offerValue = Value;
}
public void setShares(int Shares) {
numShares = Shares;
}
public void setTimestamp(Timestamp When) {
setAt = When;
}
public void setAccountId(String acId) {
AcId = acId;
}
public boolean compareOffers(Offers Previous) {
Timestamp prevTime = Previous.getTimestamp();
if (setAt.before(prevTime) || setAt.equals(prevTime)) {
return true;
} else {
return false;
}
}
}
private Vector getQueue(Offers Offer, String Task, String SessionId)
throws IOException {
String query = (String)null;
ResultSet result = null;
int numShares = 0;
double tradeAt = 0.00;
Timestamp tradeSet = null;
Vector Queue = new Vector();
String AccountId = (String)null;
String Symbol = Offer.getSymbol();
query = "Select * from qt" + Symbol.toLowerCase() + " where Trans = '" + Task + "' and AccountId != " + sessionbase.getAccountId(SessionId);
result = sessionbase.doQuery(query, SessionId);
try {
while(result.next()) {
AccountId = result.getString("AccountId");
numShares = result.getInt("NumShares");
tradeAt = result.getDouble("tradeAt");
tradeSet = result.getTimestamp("Dtime");
if (Task.equals("S")) {
if (tradeAt <= Offer.getValue()) {
Queue.addElement(new Offers(Offer.getSymbol(), tradeAt, numShares, tradeSet, AccountId));
}
} else if(Task.equals("B")) {
if (tradeAt >= Offer.getValue()) {
Queue.addElement(new Offers(Offer.getSymbol(), tradeAt, numShares, tradeSet, AccountId));
}
}
}
} catch(Exception e) {
e.printStackTrace();
}
return Queue;
}
private void doBuy(Offers current, String SessionId, HttpServletResponse response)
throws IOException {
String Symbol = (String)null;
Offers curOffer = new Offers();
Offers compareOffer = new Offers();
int count = 0;
int numShares = 0;
int indexPoint = 0;
int prevOwn = 0;
String SellerId = (String)null;
double Charge = 0.00;
double Cost = 0.00;
double Balance = 0.00;
double sellerBalance = 0.00;
String query = (String)null;
ResultSet result = null;
String from = (String)null;
String Subject = (String)null;
String Text = (String)null;
Vector Queue = new Vector();
String email = (String)null;
// Initialize currency format
DecimalFormat curFormat = new DecimalFormat("$##,######,###,###,##0.00");
DecimalFormat numFormat = new DecimalFormat("################0.00");
DecimalFormat intFormat = new DecimalFormat("################0");
Symbol = current.getSymbol();
// Lock the tables before anything happens
sessionbase.doUpdate("lock tables " + sessionbase.getUsername(SessionId) + " write, qt" + Symbol.toLowerCase() + " write, Portfolio write, Frozen write, Parameters write, Companies write", SessionId);
// Check to see if user already has an offer out for this symbol
query = "select Symbol from " + sessionbase.getUsername(SessionId) + " where Symbol = '" + Symbol + "' and Trans = 'B'";
result = sessionbase.doQuery(query, SessionId);
String Sym = (String)null;
try {
while(result.next()) {
Sym = result.getString("Symbol");
}
} catch(Exception e) {
e.printStackTrace();
}
if (Sym != null) {
response.sendRedirect(SystemURL + "/html/members/errors/offer_exists.html");
}
// Add this transaction to the queue.
query = "insert into qt" + Symbol.toLowerCase() + " (AccountId, Trans, NumShares, tradeAt, Dtime) VALUES (" + sessionbase.getAccountId(SessionId) + ", 'B', " + intFormat.format(current.getShares()) + ", " + numFormat.format(current.getValue()) + ", NOW())";
sessionbase.doUpdate(query, SessionId);
// Add transaction to user's Holdings as a pending Purchase.
query = "insert into " + sessionbase.getUsername(SessionId) + " (Symbol, NumShares, tradeAt, Trans, Dtime) values ('" + Symbol.toUpperCase() + "', " + intFormat.format(current.getShares()) + ", " + numFormat.format(current.getValue()) + ", 'B', NOW())";
sessionbase.doUpdate(query, SessionId);
// Retrieve user's balance and modify to reflect the pending trade
query = "select Balance from Portfolio where AccountId = " + sessionbase.getAccountId(SessionId);
result = sessionbase.doQuery(query, SessionId);
try {
while(result.next()) {
Balance = result.getDouble("Balance");
}
} catch(Exception e) {
e.printStackTrace();
}
// Calculate cost of the transaction
Cost = 20 + (current.getValue() * current.getShares());
Balance = Balance - Cost;
query = "update Portfolio set Balance = " + numFormat.format(Balance) + " where AccountId = " + sessionbase.getAccountId(SessionId);
sessionbase.doUpdate(query, SessionId);
// Check to see if this transaction passes the rules. If not, freeze it.
if (!sessionbase.checkRules(Symbol.toUpperCase(), current.getShares(), current.getValue(), "B", 2, SessionId)) {
} else {
// Check stuff, Do stuff
// Load the queue table for this symbol into the vector
Queue = getQueue(current, "S", SessionId);
while (!Queue.isEmpty()) {
// Queue should hold any offers that fit the price. Now find which one
// was first.
count = Queue.size() - 1;
compareOffer = (Offers) Queue.elementAt(0);
for (int i = 0 ; i < count ; i++) {
curOffer = (Offers) Queue.elementAt(i);
if (curOffer.compareOffers(compareOffer)) {
compareOffer = curOffer;
}
}
SellerId = compareOffer.getAccountId();
sessionbase.doUpdate("lock tables " + sessionbase.getUsername(SessionId) + " write, qt" + Symbol.toLowerCase() + " write, Portfolio write, Account read, Companies write, " + sessionbase.getUser(SellerId) + " write, ht" + Symbol.toLowerCase() + " write", SessionId);
// We should have the best matching offer. Execute the trade.
Charge = compareOffer.getValue();
// Determine which has more shares
if (compareOffer.getShares() < current.getShares()) {
numShares = compareOffer.getShares();
int modShares = current.getShares() - numShares;
current.setShares(modShares);
// Modify Holdings and Portfolio of SELLER
// Drop the offer from Queue
query = "delete from qt" + Symbol.toLowerCase() + " where Trans = 'S' and AccountId = " + SellerId;
sessionbase.doUpdate(query, SessionId);
query = "delete from " + sessionbase.getUser(SellerId) + " where Symbol = '" + Symbol + "' and Trans = 'S'";
sessionbase.doUpdate(query, SessionId);
// Modify query
query = "update qt" + Symbol.toLowerCase() + " set NumShares = " + intFormat.format(modShares) + " where AccountId = " + sessionbase.getAccountId(SessionId) + " and Trans = 'B'";
sessionbase.doUpdate(query, SessionId);
Cost = compareOffer.getValue() * numShares;
query = "select Balance from Portfolio where AccountId = " + SellerId;
result = sessionbase.doQuery(query, SessionId);
try {
while (result.next()) {
sellerBalance = result.getDouble("Balance");
}
} catch(Exception e) {
e.printStackTrace();
}
sellerBalance = sellerBalance + Cost;
query = "update Portfolio set Balance = " + numFormat.format(sellerBalance) + " where AccountId = " + SellerId;
sessionbase.doUpdate(query, SessionId);
// Modify Holdings and Portfolio of BUYER
query = "select Balance from Portfolio where AccountId = " + sessionbase.getAccountId(SessionId);
result = sessionbase.doQuery(query, SessionId);
try {
while(result.next()) {
Balance = result.getDouble("Balance");
}
} catch(Exception e) {
e.printStackTrace();
}
// Set user's balance. Since the value of the purchase could be less
// than what they specified, add back in their original balance then
// subtract the value. Also, since they won't be totally completing
// their purchase, subtract the remaining shares from the amount they
// originally specified.
Balance = Balance + (current.getValue() * current.getShares()) - Cost - (current.getValue() * (current.getShares() - numShares));
query = "update Portfolio set Balance = " + numFormat.format(Balance) + " where AccountId = " + sessionbase.getAccountId(SessionId);
sessionbase.doUpdate(query, SessionId);
// Modify user's sell to reflect new shares
query = "update " + sessionbase.getUsername(SessionId) + " set NumShares = " + intFormat.format(modShares) + " where Symbol = '" + Symbol + "' and Trans = 'B'";
sessionbase.doUpdate(query, SessionId);
// Check to see if user owns some of the stock already. If so simply
// add to that.
query = "select NumShares from " + sessionbase.getUsername(SessionId) + " where Symbol = '" + Symbol + "' and Trans = 'C'";
result = sessionbase.doQuery(query, SessionId);
try {
while (result.next()) {
prevOwn = result.getInt("NumShares");
}
} catch(Exception e) {
e.printStackTrace();
}
if (prevOwn > 0) {
prevOwn += numShares;
query = "update " + sessionbase.getUsername(SessionId) + " set NumShares = " + intFormat.format(prevOwn) + " where Symbol = '" + Symbol + "' and Trans = 'C'";
sessionbase.doUpdate(query, SessionId);
} else {
query = "insert into " + sessionbase.getUsername(SessionId) + " (Symbol, NumShares, tradeAt, Trans, Dtime) values ('" + Symbol.toUpperCase() + "', " + intFormat.format(numShares) + ", " + numFormat.format(Charge) + ", 'C', NOW())";
sessionbase.doUpdate(query, SessionId);
}
// Done with this offer, remove it from the queue.
Queue.removeElementAt(indexPoint);
// Update Company and History tables
double Last = 0.00;
int Volume = 0;
double Hi = 0.00;
double Lo = 0.00;
double dayHi = 0.00;
double dayLo = 0.00;
query = "select * from Companies where Symbol = '" + Symbol + "'";
result = sessionbase.doQuery(query, SessionId);
try {
while(result.next()) {
Last = result.getDouble("Last");
Volume = result.getInt("Volume");
Hi = result.getDouble("52WkHi");
Lo = result.getDouble("52WkLo");
dayHi = result.getDouble("DayHi");
dayLo = result.getDouble("DayLo");
}
} catch(Exception e) {
e.printStackTrace();
}
if (Charge > dayHi) {
dayHi = Charge;
} else if (Charge < dayLo) {
dayLo = Charge;
}
if (Charge > Hi) {
Hi = Charge;
} else if (Charge < Lo) {
Lo = Charge;
}
Volume += numShares;
query = "update Companies set Last = " + numFormat.format(Charge) + ", Volume = " + intFormat.format(Volume) + ", 52WkHi = " + numFormat.format(Hi) + ", 52WkLo = " + numFormat.format(Lo) + ", DayHi = " + numFormat.format(dayHi) + ", DayLo = " + numFormat.format(dayLo) + ", Previous = " + numFormat.format(Last) + " where Symbol = '" + Symbol + "'";
sessionbase.doUpdate(query, SessionId);
// Insert into log for future graphing purposes
// First get total shares of the company
query = "select Tshares from Companies where Symbol = '" + Symbol + "'";
result = sessionbase.doQuery(query, SessionId);
int tShares = 0;
try {
while (result.next()) {
tShares = result.getInt("Tshares");
}
} catch (Exception e) {
e.printStackTrace();
}
query = "insert into ht" + Symbol.toLowerCase() + " (Dtime, TradeAt, NumShares, TShares) values (NOW(), " + numFormat.format(Charge) + ", " + intFormat.format(numShares) + ", " + tShares + ")";
sessionbase.doUpdate(query, SessionId);
// Email buyer and seller
query = "select Email from Account where AccountId = " + sessionbase.getAccountId(SessionId);
result = sessionbase.doQuery(query, SessionId);
try {
while(result.next()) {
email = result.getString("Email");
}
} catch(Exception e) {
e.printStackTrace();
}
from = SystemEmail;
Subject = "" + SystemShortName + " - Successful Trade Notice";
Text = "You have successfully purchased " + intFormat.format(numShares) + " shares of " + Symbol.toUpperCase() + " at " + curFormat.format(Charge) + " per share.\nTotal cost: " + curFormat.format(Cost) + "\nEnding balance: " + curFormat.format(Balance) + ".";
sessionbase.sendEmail(Subject, email, from, Text);
query = "select Email from Account where AccountId = " + SellerId;
result = sessionbase.doQuery(query, SessionId);
try {
while(result.next()) {
email = result.getString("Email");
}
} catch(Exception e) {
e.printStackTrace();
}
from = SystemEmail;
Subject = "" + SystemShortName + " - Successful Trade Notice";
Text = "You have successfully sold " + intFormat.format(numShares) + " shares of " + Symbol.toUpperCase() + " at " + curFormat.format(Charge) + " per share.\nTotal value: " + curFormat.format(Cost) + "\nEnding balance: " + curFormat.format(sellerBalance) + ".";
sessionbase.sendEmail(Subject, email, from, Text);
} else if (compareOffer.getShares() == current.getShares()) {
numShares = current.getShares();
// Modify Portfolio of SELLER
SellerId = compareOffer.getAccountId();
// Drop the offer from Queue
query = "delete from qt" + Symbol.toLowerCase() + " where Trans = 'S' and AccountId = " + SellerId;
sessionbase.doUpdate(query, SessionId);
// Delete query
query = "delete from qt" + Symbol.toLowerCase() + " where AccountId = " + sessionbase.getAccountId(SessionId) + " and Trans = 'B'";
sessionbase.doUpdate(query, SessionId);
query = "delete from " + sessionbase.getUser(SellerId) + " where Symbol = '" + Symbol + "' and Trans = 'S'";
sessionbase.doUpdate(query, SessionId);
Cost = compareOffer.getValue() * numShares;
query = "select Balance from Portfolio where AccountId = " + SellerId;
result = sessionbase.doQuery(query, SessionId);
try {
while (result.next()) {
sellerBalance = result.getDouble("Balance");
}
} catch(Exception e) {
e.printStackTrace();
}
sellerBalance = sellerBalance + Cost;
query = "update Portfolio set Balance = " + numFormat.format(sellerBalance) + " where AccountId = " + SellerId;
sessionbase.doUpdate(query, SessionId);
// Modify Holdings and Portfolio of BUYER
query = "select Balance from Portfolio where AccountId = " + sessionbase.getAccountId(SessionId);
result = sessionbase.doQuery(query, SessionId);
try {
while(result.next()) {
Balance = result.getDouble("Balance");
}
} catch(Exception e) {
e.printStackTrace();
}
Balance = Balance + (current.getValue() * current.getShares()) - Cost;
query = "update Portfolio set Balance = " + numFormat.format(Balance) + " where AccountId = " + sessionbase.getAccountId(SessionId);
sessionbase.doUpdate(query, SessionId);
query = "delete from " + sessionbase.getUsername(SessionId) + " where Symbol = '" + Symbol + "' and Trans = 'B'";
sessionbase.doUpdate(query, SessionId);
// Check to see if user owns some of the stock already. If so simply
// add to that.
query = "select NumShares from " + sessionbase.getUsername(SessionId) + " where Symbol = '" + Symbol + "' and Trans = 'C'";
result = sessionbase.doQuery(query, SessionId);
try {
while (result.next()) {
prevOwn = result.getInt("NumShares");
}
} catch(Exception e) {
e.printStackTrace();
}
if (prevOwn > 0) {
prevOwn += numShares;
query = "update " + sessionbase.getUsername(SessionId) + " set NumShares = " + intFormat.format(prevOwn) + " where Symbol = '" + Symbol + "' and Trans = 'C'";
sessionbase.doUpdate(query, SessionId);
} else {
query = "insert into " + sessionbase.getUsername(SessionId) + " (Symbol, NumShares, tradeAt, Trans, Dtime) values ('" + Symbol.toUpperCase() + "', " + intFormat.format(numShares) + ", " + numFormat.format(Charge) + ", 'C', NOW())";
sessionbase.doUpdate(query, SessionId);
}
// Update Company and History tables
double Last = 0.00;
int Volume = 0;
double Hi = 0.00;
double Lo = 0.00;
double dayHi = 0.00;
double dayLo = 0.00;
query = "select * from Companies where Symbol = '" + Symbol + "'";
result = sessionbase.doQuery(query, SessionId);
try {
while(result.next()) {
Last = result.getDouble("Last");
Volume = result.getInt("Volume");
Hi = result.getDouble("52WkHi");
Lo = result.getDouble("52WkLo");
dayHi = result.getDouble("DayHi");
dayLo = result.getDouble("DayLo");
}
} catch(Exception e) {
e.printStackTrace();
}
if (Charge > dayHi) {
dayHi = Charge;
} else if (Charge < dayLo) {
dayLo = Charge;
}
if (Charge > Hi) {
Hi = Charge;
} else if (Charge < Lo) {
Lo = Charge;
}
Volume += numShares;
query = "update Companies set Last = " + numFormat.format(Charge) + ", Volume = " + intFormat.format(Volume) + ", 52WkHi = " + numFormat.format(Hi) + ", 52WkLo = " + numFormat.format(Lo) + ", DayHi = " + numFormat.format(dayHi) + ", DayLo = " + numFormat.format(dayLo) + ", Previous = " + numFormat.format(Last) + " where Symbol = '" + Symbol + "'";
sessionbase.doUpdate(query, SessionId);
// Insert into log for future graphing purposes
// First get total shares of the company
query = "select Tshares from Companies where Symbol = '" + Symbol + "'";
result = sessionbase.doQuery(query, SessionId);
int tShares = 0;
try {
while (result.next()) {
tShares = result.getInt("Tshares");
}
} catch (Exception e) {
e.printStackTrace();
}
query = "insert into ht" + Symbol.toLowerCase() + " (Dtime, TradeAt, NumShares, TShares) values (NOW(), " + numFormat.format(Charge) + ", " + intFormat.format(numShares) + ", " + tShares + ")";
sessionbase.doUpdate(query, SessionId);
// Email buyer and seller
query = "select Email from Account where AccountId = " + sessionbase.getAccountId(SessionId);
result = sessionbase.doQuery(query, SessionId);
try {
while(result.next()) {
email = result.getString("Email");
}
} catch(Exception e) {
e.printStackTrace();
}
from = SystemEmail;
Subject = "" + SystemShortName + " - Successful Trade Notice";
Text = "You have successfully purchased " + intFormat.format(numShares) + " shares of " + Symbol.toUpperCase() + " at " + curFormat.format(Charge) + " per share.\nTotal cost: " + curFormat.format(Cost) + "\nEnding balance: " + curFormat.format(Balance) + ".";
sessionbase.sendEmail(Subject, email, from, Text);
query = "select Email from Account where AccountId = " + SellerId;
result = sessionbase.doQuery(query, SessionId);
try {
while(result.next()) {
email = result.getString("Email");
}
} catch(Exception e) {
e.printStackTrace();
}
from = SystemEmail;
Subject = "" + SystemShortName + " - Successful Trade Notice";
Text = "You have successfully sold " + intFormat.format(numShares) + " shares of " + Symbol.toUpperCase() + " at " + curFormat.format(Charge) + " per share.\nTotal value: " + curFormat.format(Cost) + "\nEnding balance: " + curFormat.format(sellerBalance) + ".";
sessionbase.sendEmail(Subject, email, from, Text);
Queue = new Vector();
} else if (compareOffer.getShares() > current.getShares()) {
numShares = current.getShares();
int modShares = compareOffer.getShares() - numShares;
// Modify Portfolio of SELLER
SellerId = compareOffer.getAccountId();
// Drop the offer from Queue and add modified offer
query = "update qt" + Symbol.toLowerCase() + " set NumShares = " + intFormat.format(modShares) + " where Trans = 'S' and AccountId = " + SellerId;
sessionbase.doUpdate(query, SessionId);
// Delete query
query = "delete from qt" + Symbol.toLowerCase() + " where AccountId = " + sessionbase.getAccountId(SessionId) + " and Trans = 'B'";
sessionbase.doUpdate(query, SessionId);
query = "update " + sessionbase.getUser(SellerId) + " set NumShares = " + intFormat.format(modShares) + " where Symbol = '" + Symbol + "' and Trans = 'S'";
sessionbase.doUpdate(query, SessionId);
Cost = compareOffer.getValue() * numShares;
query = "select Balance from Portfolio where AccountId = " + SellerId;
result = sessionbase.doQuery(query, SessionId);
try {
while (result.next()) {
sellerBalance = result.getDouble("Balance");
}
} catch(Exception e) {
e.printStackTrace();
}
sellerBalance = sellerBalance + Cost;
query = "update Portfolio set Balance = " + numFormat.format(sellerBalance) + " where AccountId = " + SellerId;
sessionbase.doUpdate(query, SessionId);
// Modify Holdings and Portfolio of BUYER
query = "select Balance from Portfolio where AccountId = " + sessionbase.getAccountId(SessionId);
result = sessionbase.doQuery(query, SessionId);
try {
while(result.next()) {
Balance = result.getDouble("Balance");
}
} catch(Exception e) {
e.printStackTrace();
}
Balance = Balance + (current.getValue() * current.getShares()) - Cost;
query = "update Portfolio set Balance = " + numFormat.format(Balance) + " where AccountId = " + sessionbase.getAccountId(SessionId);
sessionbase.doUpdate(query, SessionId);
query = "delete from " + sessionbase.getUsername(SessionId) + " where Symbol = '" + Symbol + "' and Trans = 'B'";
sessionbase.doUpdate(query, SessionId);
// Check to see if user owns some of the stock already. If so simply
// add to that.
query = "select NumShares from " + sessionbase.getUsername(SessionId) + " where Symbol = '" + Symbol + "' and Trans = 'C'";
result = sessionbase.doQuery(query, SessionId);
try {
while (result.next()) {
prevOwn = result.getInt("NumShares");
}
} catch(Exception e) {
e.printStackTrace();
}
if (prevOwn > 0) {
prevOwn += numShares;
query = "update " + sessionbase.getUsername(SessionId) + " set NumShares = " + intFormat.format(prevOwn) + " where Symbol = '" + Symbol + "' and Trans = 'C'";
sessionbase.doUpdate(query, SessionId);
} else {
query = "insert into " + sessionbase.getUsername(SessionId) + " (Symbol, NumShares, tradeAt, Trans, Dtime) values ('" + Symbol.toUpperCase() + "', " + intFormat.format(numShares) + ", " + numFormat.format(Charge) + ", 'C', NOW())";
sessionbase.doUpdate(query, SessionId);
}
// Update Company and History tables
double Last = 0.00;
int Volume = 0;
double Hi = 0.00;
double Lo = 0.00;
double dayHi = 0.00;
double dayLo = 0.00;
query = "select * from Companies where Symbol = '" + Symbol + "'";
result = sessionbase.doQuery(query, SessionId);
try {
while(result.next()) {
Last = result.getDouble("Last");
Volume = result.getInt("Volume");
Hi = result.getDouble("52WkHi");
Lo = result.getDouble("52WkLo");
dayHi = result.getDouble("DayHi");
dayLo = result.getDouble("DayLo");
}
} catch(Exception e) {
e.printStackTrace();
}
if (Charge > dayHi) {
dayHi = Charge;
} else if (Charge < dayLo) {
dayLo = Charge;
}
if (Charge > Hi) {
Hi = Charge;
} else if (Charge < Lo) {
Lo = Charge;
}
Volume += numShares;
query = "update Companies set Last = " + numFormat.format(Charge) + ", Volume = " + intFormat.format(Volume) + ", 52WkHi = " + numFormat.format(Hi) + ", 52WkLo = " + numFormat.format(Lo) + ", DayHi = " + numFormat.format(dayHi) + ", DayLo = " + numFormat.format(dayLo) + ", Previous = " + numFormat.format(Last) + " where Symbol = '" + Symbol + "'";
sessionbase.doUpdate(query, SessionId);
// Insert into log for future graphing purposes
// First get total shares of the company
query = "select Tshares from Companies where Symbol = '" + Symbol + "'";
result = sessionbase.doQuery(query, SessionId);
int tShares = 0;
try {
while (result.next()) {
tShares = result.getInt("Tshares");
}
} catch (Exception e) {
e.printStackTrace();
}
query = "insert into ht" + Symbol.toLowerCase() + " (Dtime, TradeAt, NumShares, TShares) values (NOW(), " + numFormat.format(Charge) + ", " + intFormat.format(numShares) + ", " + tShares + ")";
sessionbase.doUpdate(query, SessionId);
// Email buyer and seller
query = "select Email from Account where AccountId = " + sessionbase.getAccountId(SessionId);
result = sessionbase.doQuery(query, SessionId);
try {
while(result.next()) {
email = result.getString("Email");
}
} catch(Exception e) {
e.printStackTrace();
}
from = SystemEmail;
Subject = "" + SystemShortName + " - Successful Trade Notice";
Text = "You have successfully purchased " + intFormat.format(numShares) + " shares of " + Symbol.toUpperCase() + " at " + curFormat.format(Charge) + " per share.\nTotal cost: " + curFormat.format(Cost) + "\nEnding balance: " + curFormat.format(Balance) + ".";
sessionbase.sendEmail(Subject, email, from, Text);
query = "select Email from Account where AccountId = " + SellerId;
result = sessionbase.doQuery(query, SessionId);
try {
while(result.next()) {
email = result.getString("Email");
}
} catch(Exception e) {
e.printStackTrace();
}
from = SystemEmail;
Subject = "" + SystemShortName + " - Successful Trade Notice";
Text = "You have successfully sold " + intFormat.format(numShares) + " shares of " + Symbol.toUpperCase() + " at " + curFormat.format(Charge) + " per share.\nTotal value: " + curFormat.format(Cost) + "\nEnding balance: " + curFormat.format(sellerBalance) + ".";
sessionbase.sendEmail(Subject, email, from, Text);
Queue = new Vector();
}
}
}
sessionbase.doUpdate("unlock tables", SessionId);
}
private void doSell(Offers current, String SessionId, HttpServletResponse response)
throws IOException {
Offers compareOffer = new Offers();
Offers curOffer = new Offers();
int numShares = 0;
int count = 0;
int ownedShares = 0;
int indexPoint = 0;
int prevOwn = 0;
Vector Queue = new Vector();
String BuyerId = (String)null;
double Charge = 0.00;
double Cost = 0.00;
double Balance = 0.00;
double buyerBalance = 0.00;
String query = (String)null;
ResultSet result = null;
String from = (String)null;
String Subject = (String)null;
String Text = (String)null;
String email = (String)null;
String Symbol = (String)null;
// Initialize currency format
DecimalFormat curFormat = new DecimalFormat("$##,###,###,###,###,##0.00");
DecimalFormat numFormat = new DecimalFormat("################0.00");
DecimalFormat intFormat = new DecimalFormat("################0");
Symbol = current.getSymbol();
// Check to see if user already has an offer out for this symbol
// First, lock the table
sessionbase.doUpdate("lock tables " + sessionbase.getUsername(SessionId) + " write, qt" + Symbol.toLowerCase() + " write, Portfolio write, Frozen write, Parameters write, Companies write", SessionId);
query = "select Symbol from " + sessionbase.getUsername(SessionId) + " where Symbol = '" + Symbol + "' and Trans = 'S'";
result = sessionbase.doQuery(query, SessionId);
String Sym = (String)null;
try {
while(result.next()) {
Sym = result.getString("Symbol");
}
} catch(Exception e) {
e.printStackTrace();
}
if (Sym != null) {
response.sendRedirect(SystemURL + "/html/members/errors/offer_exists.html");
}
// Add this transaction to the queue.
query = "insert into qt" + Symbol.toLowerCase() + " (AccountId, Trans, NumShares, tradeAt, Dtime) VALUES (" + sessionbase.getAccountId(SessionId) + ", 'S', " + intFormat.format(current.getShares()) + ", " + numFormat.format(current.getValue()) + ", NOW())";
sessionbase.doUpdate(query, SessionId);
// Remove shares from Completed user's Holdings
int tNumShares = 0;
query = "select NumShares from " + sessionbase.getUsername(SessionId) + " where Symbol = '" + Symbol + "' and Trans = 'C'";
result = sessionbase.doQuery(query, SessionId);
try {
while (result.next()) {
tNumShares = result.getInt("NumShares");
}
} catch(Exception e) {
e.printStackTrace();
}
tNumShares -= current.getShares();
// If remaining shares = 0 don't bother putting it back in
if (tNumShares > 0) {
query = "update " + sessionbase.getUsername(SessionId) + " set NumShares = " + intFormat.format(tNumShares) + " where Symbol = '" + Symbol + "' and Trans = 'C'";
sessionbase.doUpdate(query, SessionId);
} else {
query = "delete from " + sessionbase.getUsername(SessionId) + " where Trans = 'C' and Symbol = '" + Symbol + "'";
sessionbase.doUpdate(query, SessionId);
}
// Add transaction to user's Holdings as a pending Sale.
query = "insert into " + sessionbase.getUsername(SessionId) + " (Symbol, NumShares, tradeAt, Trans, Dtime) values ('" + Symbol.toUpperCase() + "', " + intFormat.format(current.getShares()) + ", " + numFormat.format(current.getValue()) + ", 'S', NOW())";
sessionbase.doUpdate(query, SessionId);
// Check to see if this transaction passes the rules. If not, freeze it.
if (!sessionbase.checkRules(Symbol, current.getShares(), current.getValue(), "S", 2, SessionId)) {
} else {
// Load the queue table for this symbol into the vector
Queue = getQueue(current, "B", SessionId);
while (!Queue.isEmpty()) {
// Queue should hold any offers that fit the price. Now find which one
// was first.
count = Queue.size() - 1;
compareOffer = (Offers) Queue.elementAt(0);
for (int i = 0 ; i < count ; i++) {
curOffer = (Offers) Queue.elementAt(i);
if (curOffer.compareOffers(compareOffer)) {
compareOffer = curOffer;
}
}
BuyerId = compareOffer.getAccountId();
sessionbase.doUpdate("lock tables " + sessionbase.getUsername(SessionId) + " write, qt" + Symbol.toLowerCase() + " write, Portfolio write, Account read, Companies write, " + sessionbase.getUser(BuyerId) + " write, ht" + Symbol.toLowerCase() + " write", SessionId);
// We should have the best matching offer. Execute the trade.
Charge = current.getValue();
// Determine which has more shares
if (compareOffer.getShares() < current.getShares()) {
numShares = compareOffer.getShares();
int modShares = current.getShares() - compareOffer.getShares();
current.setShares(modShares);
// Modify Holdings and Portfolio of BUYER
BuyerId = compareOffer.getAccountId();
// Drop the offer from Queue
query = "delete from qt" + Symbol.toLowerCase() + " where Trans = 'B' and AccountId = " + BuyerId;
sessionbase.doUpdate(query, SessionId);
// Modify query
query = "update qt" + Symbol.toLowerCase() + " set NumShares = " + intFormat.format(modShares) + " where AccountId = " + sessionbase.getAccountId(SessionId) + " and Trans = 'S'";
sessionbase.doUpdate(query, SessionId);
query = "delete from " + sessionbase.getUser(BuyerId) + " where Symbol = '" + Symbol + "' and Trans = 'B'";
sessionbase.doUpdate(query, SessionId);
Cost = current.getValue() * numShares;
query = "select Balance from Portfolio where AccountId = " + BuyerId;
result = sessionbase.doQuery(query, SessionId);
try {
while (result.next()) {
buyerBalance = result.getDouble("Balance");
}
} catch(Exception e) {
e.printStackTrace();
}
// Add original funds back in then subtract actual cost
buyerBalance = buyerBalance + (compareOffer.getValue() * compareOffer.getShares()) - Cost;
query = "update Portfolio set Balance = " + numFormat.format(buyerBalance) + " where AccountId = " + BuyerId;
sessionbase.doUpdate(query, SessionId);
// Check to see if user owns some of the stock already. If so simply
// add to that.
query = "select NumShares from " + sessionbase.getUser(BuyerId) + " where Symbol = '" + Symbol + "' and Trans = 'C'";
result = sessionbase.doQuery(query, SessionId);
try {
while (result.next()) {
prevOwn = result.getInt("NumShares");
}
} catch(Exception e) {
e.printStackTrace();
}
if (prevOwn > 0) {
prevOwn += numShares;
query = "update " + sessionbase.getUser(BuyerId) + " set NumShares = " + intFormat.format(prevOwn) + " where Symbol = '" + Symbol + "' and Trans = 'C'";
sessionbase.doUpdate(query, SessionId);
} else {
query = "insert into " + sessionbase.getUser(BuyerId) + " (Symbol, NumShares, tradeAt, Trans, Dtime) values ('" + Symbol.toUpperCase() + "', " + intFormat.format(numShares) + ", " + numFormat.format(Charge) + ", 'C', NOW())";
sessionbase.doUpdate(query, SessionId);
}
// Modify Holdings and Portfolio of SELLER
query = "select Balance from Portfolio where AccountId = " + sessionbase.getAccountId(SessionId);
result = sessionbase.doQuery(query, SessionId);
try {
while(result.next()) {
Balance = result.getDouble("Balance");
}
} catch(Exception e) {
e.printStackTrace();
}
Balance = Balance + (current.getValue() * numShares) - 20;
query = "update Portfolio set Balance = " + numFormat.format(Balance) + " where AccountId = " + sessionbase.getAccountId(SessionId);
sessionbase.doUpdate(query, SessionId);
// Modify Pending sell
query = "update " + sessionbase.getUsername(SessionId) + " set NumShares = " + intFormat.format(modShares) + " where Symbol = '" + Symbol + "' and Trans = 'S'";
sessionbase.doUpdate(query, SessionId);
// Update Company and History tables
double Last = 0.00;
int Volume = 0;
double Hi = 0.00;
double Lo = 0.00;
double dayHi = 0.00;
double dayLo = 0.00;
query = "select * from Companies where Symbol = '" + Symbol + "'";
result = sessionbase.doQuery(query, SessionId);
try {
while(result.next()) {
Last = result.getDouble("Last");
Volume = result.getInt("Volume");
Hi = result.getDouble("52WkHi");
Lo = result.getDouble("52WkLo");
dayHi = result.getDouble("DayHi");
dayLo = result.getDouble("DayLo");
}
} catch(Exception e) {
e.printStackTrace();
}
if (Charge > dayHi) {
dayHi = Charge;
} else if (Charge < dayLo) {
dayLo = Charge;
}
if (Charge > Hi) {
Hi = Charge;
} else if (Charge < Lo) {
Lo = Charge;
}
Volume += numShares;
query = "update Companies set Last = " + numFormat.format(Charge) + ", Volume = " + intFormat.format(Volume) + ", 52WkHi = " + numFormat.format(Hi) + ", 52WkLo = " + numFormat.format(Lo) + ", DayHi = " + numFormat.format(dayHi) + ", DayLo = " + numFormat.format(dayLo) + ", Previous = " + numFormat.format(Last) + " where Symbol = '" + Symbol + "'";
sessionbase.doUpdate(query, SessionId);
// Insert into log for future graphing purposes
// First get total shares of the company
query = "select Tshares from Companies where Symbol = '" + Symbol + "'";
result = sessionbase.doQuery(query, SessionId);
int tShares = 0;
try {
while (result.next()) {
tShares = result.getInt("Tshares");
}
} catch (Exception e) {
e.printStackTrace();
}
query = "insert into ht" + Symbol.toLowerCase() + " (Dtime, TradeAt, NumShares, TShares) values (NOW(), " + numFormat.format(Charge) + ", " + intFormat.format(numShares) + ", " + tShares + ")";
sessionbase.doUpdate(query, SessionId);
// Email buyer and seller
query = "select Email from Account where AccountId = " + sessionbase.getAccountId(SessionId);
result = sessionbase.doQuery(query, SessionId);
try {
while(result.next()) {
email = result.getString("Email");
}
} catch(Exception e) {
e.printStackTrace();
}
from = SystemEmail;
Subject = "" + SystemShortName + " - Successful Trade Notice";
Text = "You have successfully sold " + intFormat.format(numShares) + " shares of " + Symbol.toUpperCase() + " at " + curFormat.format(Charge) + " per share.\nTotal value: " + curFormat.format(Cost) + "\nEnding balance: " + curFormat.format(Balance) + ".";
sessionbase.sendEmail(Subject, email, from, Text);
query = "select Email from Account where AccountId = " + BuyerId;
result = sessionbase.doQuery(query, SessionId);
try {
while(result.next()) {
email = result.getString("Email");
}
} catch(Exception e) {
e.printStackTrace();
}
from = SystemEmail;
Subject = "" + SystemShortName + " - Successful Trade Notice";
Text = "You have successfully purchased " + intFormat.format(numShares) + " shares of " + Symbol.toUpperCase() + " at " + curFormat.format(Charge) + " per share.\nTotal cost: " + curFormat.format(Cost) + "\nEnding balance: " + curFormat.format(buyerBalance) + ".";
sessionbase.sendEmail(Subject, email, from, Text);
// Remove this offer from the queue
Queue.removeElementAt(indexPoint);
} else if (compareOffer.getShares() == current.getShares()) {
numShares = current.getShares();
// Modify Holdings and Portfolio of BUYER
BuyerId = compareOffer.getAccountId();
// Drop the offer from Queue
query = "delete from qt" + Symbol.toLowerCase() + " where Trans = 'B' and AccountId = " + BuyerId;
sessionbase.doUpdate(query, SessionId);
// Delete query
query = "delete from qt" + Symbol.toLowerCase() + " where AccountId = " + sessionbase.getAccountId(SessionId) + " and Trans = 'S'";
sessionbase.doUpdate(query, SessionId);
query = "delete from " + sessionbase.getUser(BuyerId) + " where Symbol = '" + Symbol + "' and Trans = 'B'";
sessionbase.doUpdate(query, SessionId);
Cost = current.getValue() * numShares;
query = "select Balance from Portfolio where AccountId = " + BuyerId;
result = sessionbase.doQuery(query, SessionId);
try {
while (result.next()) {
buyerBalance = result.getDouble("Balance");
}
} catch(Exception e) {
e.printStackTrace();
}
buyerBalance = buyerBalance + (compareOffer.getValue() * compareOffer.getShares()) - Cost;
query = "update Portfolio set Balance = " + numFormat.format(buyerBalance) + " where AccountId = " + BuyerId;
sessionbase.doUpdate(query, SessionId);
// Check to see if user owns some of the stock already. If so simply
// add to that.
query = "select NumShares from " + sessionbase.getUser(BuyerId) + " where Symbol = '" + Symbol + "' and Trans = 'C'";
result = sessionbase.doQuery(query, SessionId);
try {
while (result.next()) {
prevOwn = result.getInt("NumShares");
}
} catch(Exception e) {
e.printStackTrace();
}
if (prevOwn > 0) {
prevOwn += numShares;
query = "update " + sessionbase.getUser(BuyerId) + " set NumShares = " + intFormat.format(prevOwn) + " where Symbol = '" + Symbol + "' and Trans = 'C'";
sessionbase.doUpdate(query, SessionId);
} else {
query = "insert into " + sessionbase.getUser(BuyerId) + " (Symbol, NumShares, tradeAt, Trans, Dtime) values ('" + Symbol.toUpperCase() + "', " + intFormat.format(numShares) + ", " + numFormat.format(Charge) + ", 'C', NOW())";
sessionbase.doUpdate(query, SessionId);
}
// Modify Holdings and Portfolio of SELLER
query = "select Balance from Portfolio where AccountId = " + sessionbase.getAccountId(SessionId);
result = sessionbase.doQuery(query, SessionId);
try {
while(result.next()) {
Balance = result.getDouble("Balance");
}
} catch(Exception e) {
e.printStackTrace();
}
Balance = Balance + Cost - 20;
query = "update Portfolio set Balance = " + numFormat.format(Balance) + " where AccountId = " + sessionbase.getAccountId(SessionId);
sessionbase.doUpdate(query, SessionId);
query = "delete from " + sessionbase.getUsername(SessionId) + " where Symbol = '" + Symbol + "' and Trans = 'S'";
sessionbase.doUpdate(query, SessionId);
// Update Company and History tables
double Last = 0.00;
int Volume = 0;
double Hi = 0.00;
double Lo = 0.00;
double dayHi = 0.00;
double dayLo = 0.00;
query = "select * from Companies where Symbol = '" + Symbol + "'";
result = sessionbase.doQuery(query, SessionId);
try {
while(result.next()) {
Last = result.getDouble("Last");
Volume = result.getInt("Volume");
Hi = result.getDouble("52WkHi");
Lo = result.getDouble("52WkLo");
dayHi = result.getDouble("DayHi");
dayLo = result.getDouble("DayLo");
}
} catch(Exception e) {
e.printStackTrace();
}
if (Charge > dayHi) {
dayHi = Charge;
} else if (Charge < dayLo) {
dayLo = Charge;
}
if (Charge > Hi) {
Hi = Charge;
} else if (Charge < Lo) {
Lo = Charge;
}
Volume += numShares;
query = "update Companies set Last = " + numFormat.format(Charge) + ", Volume = " + intFormat.format(Volume) + ", 52WkHi = " + numFormat.format(Hi) + ", 52WkLo = " + numFormat.format(Lo) + ", DayHi = " + numFormat.format(dayHi) + ", DayLo = " + numFormat.format(dayLo) + ", Previous = " + numFormat.format(Last) + " where Symbol = '" + Symbol + "'";
sessionbase.doUpdate(query, SessionId);
// Insert into log for future graphing purposes
// First get total shares of the company
query = "select Tshares from Companies where Symbol = '" + Symbol + "'";
result = sessionbase.doQuery(query, SessionId);
int tShares = 0;
try {
while (result.next()) {
tShares = result.getInt("Tshares");
}
} catch (Exception e) {
e.printStackTrace();
}
query = "insert into ht" + Symbol.toLowerCase() + " (Dtime, TradeAt, NumShares, TShares) values (NOW(), " + numFormat.format(Charge) + ", " + intFormat.format(numShares) + ", " + tShares + ")";
sessionbase.doUpdate(query, SessionId);
// Email buyer and seller
query = "select Email from Account where AccountId = " + sessionbase.getAccountId(SessionId);
result = sessionbase.doQuery(query, SessionId);
try {
while(result.next()) {
email = result.getString("Email");
}
} catch(Exception e) {
e.printStackTrace();
}
from = SystemEmail;
Subject = "" + SystemShortName + " - Successful Trade Notice";
Text = "You have successfully sold " + intFormat.format(numShares) + " shares of " + Symbol.toUpperCase() + " at " + curFormat.format(Charge) + " per share.\nTotal value: " + curFormat.format(Cost) + "\nEnding balance: " + curFormat.format(Balance) + ".";
sessionbase.sendEmail(Subject, email, from, Text);
query = "select Email from Account where AccountId = " + BuyerId;
result = sessionbase.doQuery(query, SessionId);
try {
while(result.next()) {
email = result.getString("Email");
}
} catch(Exception e) {
e.printStackTrace();
}
from = SystemEmail;
Subject = "" + SystemShortName + " - Successful Trade Notice";
Text = "You have successfully purchased " + intFormat.format(numShares) + " shares of " + Symbol.toUpperCase() + " at " + curFormat.format(Charge) + " per share.\nTotal cost: " + curFormat.format(Cost) + "\nEnding balance: " + curFormat.format(buyerBalance) + ".";
sessionbase.sendEmail(Subject, email, from, Text);
Queue = new Vector();
} else if (compareOffer.getShares() > current.getShares()) {
numShares = current.getShares();
int modShares = compareOffer.getShares() - numShares;
// Modify Holdings and Portfolio of BUYER
BuyerId = compareOffer.getAccountId();
// Modify the offer
query = "update qt" + Symbol.toLowerCase() + " set NumShares = " + intFormat.format(modShares) + " where Trans = 'B' and AccountId = " + BuyerId;
sessionbase.doUpdate(query, SessionId);
// Delete query
query = "delete from qt" + Symbol.toLowerCase() + " where AccountId = " + sessionbase.getAccountId(SessionId) + " and Trans = 'S'";
sessionbase.doUpdate(query, SessionId);
query = "update " + sessionbase.getUser(BuyerId) + " set NumShares = " + intFormat.format(modShares) + " where Symbol = '" + Symbol + "' and Trans = 'B'";
sessionbase.doUpdate(query, SessionId);
Cost = current.getValue() * numShares;
query = "select Balance from Portfolio where AccountId = " + BuyerId;
result = sessionbase.doQuery(query, SessionId);
try {
while (result.next()) {
buyerBalance = result.getDouble("Balance");
}
} catch(Exception e) {
e.printStackTrace();
}
buyerBalance = buyerBalance + (compareOffer.getShares() * compareOffer.getValue()) - (compareOffer.getValue() * modShares) - Cost;
query = "update Portfolio set Balance = " + numFormat.format(buyerBalance) + " where AccountId = " + BuyerId;
sessionbase.doUpdate(query, SessionId);
// Check to see if user owns some of the stock already. If so simply
// add to that.
query = "select NumShares from " + sessionbase.getUser(BuyerId) + " where Symbol = '" + Symbol + "' and Trans = 'C'";
result = sessionbase.doQuery(query, SessionId);
try {
while (result.next()) {
prevOwn = result.getInt("NumShares");
}
} catch(Exception e) {
e.printStackTrace();
}
if (prevOwn > 0) {
prevOwn += numShares;
query = "update " + sessionbase.getUser(BuyerId) + " set NumShares = " + intFormat.format(prevOwn) + " where Symbol = '" + Symbol + "' and Trans = 'C'";
sessionbase.doUpdate(query, SessionId);
} else {
query = "insert into " + sessionbase.getUser(BuyerId) + " (Symbol, NumShares, tradeAt, Trans, Dtime) values ('" + Symbol.toUpperCase() + "', " + intFormat.format(numShares) + ", " + numFormat.format(Charge) + ", 'C', NOW())";
sessionbase.doUpdate(query, SessionId);
}
// Modify Holdings and Portfolio of SELLER
query = "select Balance from Portfolio where AccountId = " + sessionbase.getAccountId(SessionId);
result = sessionbase.doQuery(query, SessionId);
try {
while(result.next()) {
Balance = result.getDouble("Balance");
}
} catch(Exception e) {
e.printStackTrace();
}
Balance = Balance + Cost - 20;
query = "update Portfolio set Balance = " + numFormat.format(Balance) + " where AccountId = " + sessionbase.getAccountId(SessionId);
sessionbase.doUpdate(query, SessionId);
query = "delete from " + sessionbase.getUsername(SessionId) + " where Symbol = '" + Symbol + "' and Trans = 'S'";
sessionbase.doUpdate(query, SessionId);
// Update Company and History tables
double Last = 0.00;
int Volume = 0;
double Hi = 0.00;
double Lo = 0.00;
double dayHi = 0.00;
double dayLo = 0.00;
query = "select * from Companies where Symbol = '" + Symbol + "'";
result = sessionbase.doQuery(query, SessionId);
try {
while(result.next()) {
Last = result.getDouble("Last");
Volume = result.getInt("Volume");
Hi = result.getDouble("52WkHi");
Lo = result.getDouble("52WkLo");
dayHi = result.getDouble("DayHi");
dayLo = result.getDouble("DayLo");
}
} catch(Exception e) {
e.printStackTrace();
}
if (Charge > dayHi) {
dayHi = Charge;
} else if (Charge < dayLo) {
dayLo = Charge;
}
if (Charge > Hi) {
Hi = Charge;
} else if (Charge < Lo) {
Lo = Charge;
}
Volume += numShares;
query = "update Companies set Last = " + numFormat.format(Charge) + ", Volume = " + intFormat.format(Volume) + ", 52WkHi = " + numFormat.format(Hi) + ", 52WkLo = " + numFormat.format(Lo) + ", DayHi = " + numFormat.format(dayHi) + ", DayLo = " + numFormat.format(dayLo) + ", Previous = " + numFormat.format(Last) + " where Symbol = '" + Symbol + "'";
sessionbase.doUpdate(query, SessionId);
// Insert into log for future graphing purposes
// First get total shares of the company
query = "select Tshares from Companies where Symbol = '" + Symbol + "'";
result = sessionbase.doQuery(query, SessionId);
int tShares = 0;
try {
while (result.next()) {
tShares = result.getInt("Tshares");
}
} catch (Exception e) {
e.printStackTrace();
}
query = "insert into ht" + Symbol.toLowerCase() + " (Dtime, TradeAt, NumShares, TShares) values (NOW(), " + numFormat.format(Charge) + ", " + intFormat.format(numShares) + ", " + tShares + ")";
sessionbase.doUpdate(query, SessionId);
// Email buyer and seller
query = "select Email from Account where AccountId = " + sessionbase.getAccountId(SessionId);
result = sessionbase.doQuery(query, SessionId);
try {
while(result.next()) {
email = result.getString("Email");
}
} catch(Exception e) {
e.printStackTrace();
}
from = SystemEmail;
Subject = "" + SystemShortName + " - Successful Trade Notice";
Text = "You have successfully sold " + intFormat.format(numShares) + " shares of " + Symbol.toUpperCase() + " at " + curFormat.format(Charge) + " per share.\nTotal value: " + curFormat.format(Cost) + "\nEnding balance: " + curFormat.format(Balance) + ".";
sessionbase.sendEmail(Subject, email, from, Text);
query = "select Email from Account where AccountId = " + BuyerId;
result = sessionbase.doQuery(query, SessionId);
try {
while(result.next()) {
email = result.getString("Email");
}
} catch(Exception e) {
e.printStackTrace();
}
from = SystemEmail;
Subject = "" + SystemShortName + " - Successful Trade Notice";
Text = "You have successfully purchased " + intFormat.format(numShares) + " shares of " + Symbol.toUpperCase() + " at " + curFormat.format(Charge) + " per share.\nTotal cost: " + curFormat.format(Cost) + "\nEnding balance: " + curFormat.format(buyerBalance) + ".";
sessionbase.sendEmail(Subject, email, from, Text);
Queue = new Vector();
}
}
}
sessionbase.doUpdate("unlock tables", SessionId);
}
static public void reload() {
SystemURL = sessionbase.getURL();
ServletURL = sessionbase.getServletURL();
SystemEmail = sessionbase.getEmail();
SystemPath = sessionbase.getPath();
SystemName = sessionbase.getName();
SystemShortName = sessionbase.getShortName();
}
}
| | | | | | | | | | | | | | | | | | |