package isip.java.bullyse.bullydb;
import java.io.*;
import java.util.*;
import java.text.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
// This file handles retrieving and displaying quotes for the
// different companies in the Bulldog Stock Exchange.
public class Quotes 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 Symbol = request.getParameter("Symbol");
String Type = request.getParameter("Type");
String task = request.getParameter("Task");
HttpSession session = request.getSession();
String SessionId = session.getId();
// Retrieve AccountId
String AccountId = sessionbase.getAccountId(SessionId);
if (AccountId == null && !task.equals("getQuotes") ) {
response.sendRedirect(SystemURL + "/html/errors/not_logged_in.html");
} else if (task != null && AccountId == null) {
if (task.equals("getQuotes")) {
// Outputs in html the info needed for the ticker
displayTicker(request, response);
}
} else {
if (task == null) {
// Simple quote display
if (Type == null) {
Type = "1";
}
displayQuote(Symbol, Type, request, response, SessionId);
} else if (task.equals("showAll")) {
// Show a brief summary of all symbols
displayAll(request, response, SessionId);
} else if (task.equals("Trans")) {
// Show the offers for a symbol
displayTrans(request, response, SessionId);
} else if (task.equals("allOffers")) {
// Show all offers for a symbol
displayAllTrans(request, response, SessionId);
} else if (task.equals("getQuotes")) {
// Outputs in html the info needed for the ticker
displayTicker(request, response);
}
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
HttpSession session = request.getSession();
String SessionId = session.getId();
// Retrieve AccountId
String AccountId = sessionbase.getAccountId(SessionId);
if (AccountId == null) {
response.sendRedirect(SystemURL + "/html/errors/not_logged_in.html");
} else {
// Display quote for a single symbol.
String Symbol = request.getParameter("Symbol");
String Type = "1";
displayQuote(Symbol, Type, request, response, SessionId);
}
}
public void displayTicker(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
String query = (String)null;
String Symbol = (String)null;
String Last = (String)null;
ResultSet result = null;
int newU = 0;
// Initialize the output stream
response.setContentType("text/html");
PrintWriter out=response.getWriter();
query = "select Symbol, Last from Companies order by Symbol";
result = sessionbase.doQuery(query);
try {
while (result.next()) {
Symbol = result.getString("Symbol");
Last = result.getString("Last");
out.println(Symbol);
out.println(Last);
}
} catch (Exception e) {
e.printStackTrace();
}
out.close();
}
public void displayQuote(String Symbol, String Type, HttpServletRequest request, HttpServletResponse response, String SessionId)
throws IOException, ServletException {
String query = (String)null;
String Name = (String)null;
int Tshares = 0;
double Ipo = 0.00;
double Open = 0.00;
double Last = 0.00;
int Volume = 0;
double Change = 0.00;
double WkHi = 0.00;
double WkLo = 0.00;
double DayHi = 0.00;
double DayLo = 0.00;
double Previous = 0.00;
String fLast = (String)null;
String fChange = (String)null;
String fLow = (String)null;
String fHigh = (String)null;
ResultSet result = null;
double Capitalization = 0.00;
boolean member = true;
String Locked = (String)null;
// Set up decimal formatting for digits. This will force at least 0.00.
DecimalFormat curFormat = new DecimalFormat("$##,###,###,###,###,##0.00");
DecimalFormat numFormat = new DecimalFormat("################0.00");
DecimalFormat intFormat = new DecimalFormat("################0");
// Initialize output stream
response.setContentType("text/html");
PrintWriter out=response.getWriter();
// Extract Company info from the database. A null ResultSet means the company
// must not be in the database.
query = "select * from Companies where Symbol = '" + Symbol + "'";
result = sessionbase.doQuery(query);
try {
while(result.next()) {
Name = result.getString("Name");
Tshares = result.getInt("Tshares");
Open = result.getDouble("Open");
Last = result.getDouble("Last");
Volume = result.getInt("Volume");
WkHi = result.getDouble("52WkHi");
WkLo = result.getDouble("52WkLo");
DayHi = result.getDouble("DayHi");
DayLo = result.getDouble("DayLo");
Previous = result.getDouble("Previous");
Locked = result.getString("Locked");
}
} catch(Exception e) {
e.printStackTrace();
}
Change = Last - Previous;
Capitalization = Tshares * Last;
if (Name == null) {
response.sendRedirect(SystemURL + "/html/errors/invalid_symbol.html");
out.close();
} else {
sessionbase.printTop(out, 5);
// Output the Quote Flash data
sessionbase.QuoteFlash(out);
// Fill in the blanks from the template
out.println("");
out.println("
");
out.println("");
out.println("");
out.println(" ");
out.println("");
out.println("");
out.println("");
out.println("");
out.println("");
out.println("Today with " + Name + " (" + Symbol + ") | ");
out.println(" ");
out.println(" Today's Open: | ");
out.println(" " + numFormat.format(Open) + " | ");
out.println(" Yesterday's Close: | ");
out.println(" " + numFormat.format(Previous) + " | ");
out.println(" ");
out.println(" ");
out.println(" Current Price: | ");
out.println(" " + numFormat.format(Last) + " | ");
out.println(" Change: | ");
out.println(" " + numFormat.format(Change) + " | ");
out.println(" ");
out.println(" ");
out.println(" Today's Low: | ");
out.println(" " + numFormat.format(DayLo) + " | ");
out.println(" Today's High: | ");
out.println(" " + numFormat.format(DayHi) + " | ");
out.println(" ");
out.println(" ");
out.println(" 52wk High: | ");
out.println(" " + numFormat.format(WkHi) + " | ");
out.println(" 52wk Low: | ");
out.println(" " + numFormat.format(WkLo) + " | ");
out.println(" ");
out.println(" ");
out.println(" Volume: | ");
out.println(" " + Volume + " | ");
out.println(" ");
out.println("Market Capitalization | " + curFormat.format(Capitalization) + " | ");
out.println("View all offers for this stock... | ");
if (Locked.equals("Y")) {
out.println(" ");
out.println("This company is currently in Locked status. ");
out.println("Buying and Selling cannot take place for this company until ");
out.println("it is unlocked. | ");
}
out.println(" | ");
// Rude and crude to push the graph down - but it works.
out.println("
");
// Display the Company graph
sessionbase.displayGraph(out, Type, Symbol, 1);
out.println(" | ");
out.println("");
out.println("");
sessionbase.printBot(out, SessionId);
out.close();
}
}
public void displayAll(HttpServletRequest request, HttpServletResponse response, String SessionId)
throws IOException, ServletException {
String query = (String)null;
String Name = (String)null;
int Tshares = 0;
double Ipo = 0.00;
double Open = 0.00;
double Last = 0.00;
int Volume = 0;
double Change = 0.00;
double WkHi = 0.00;
double WkLo = 0.00;
double DayHi = 0.00;
double DayLo = 0.00;
double Previous = 0.00;
String fLast = (String)null;
String fChange = (String)null;
String fLow = (String)null;
String fHigh = (String)null;
String Symbol = (String)null;
ResultSet result = null;
double Capitalization = 0.00;
boolean member = true;
Vector dumpPlace = new Vector();
int i = 0;
// Set up decimal formatting for digits. This will force at least 0.00.
DecimalFormat curFormat = new DecimalFormat("$##,###,###,###,###,##0.00");
DecimalFormat numFormat = new DecimalFormat("################0.00");
DecimalFormat intFormat = new DecimalFormat("################0");
// Initialize output stream
response.setContentType("text/html");
PrintWriter out=response.getWriter();
// Extract Company info from the database. A null ResultSet means the company
// must not be in the database.
query = "select * from Companies order by Symbol";
result = sessionbase.doQuery(query);
try {
while (result.next()) {
Symbol = result.getString("Symbol");
Last = result.getDouble("Last");
DayHi = result.getDouble("DayHi");
DayLo = result.getDouble("DayLo");
Volume = result.getInt("Volume");
dumpPlace.addElement(new Companies(Symbol, DayHi, DayLo, Last, Volume));
}
} catch (Exception e) {
e.printStackTrace();
}
sessionbase.printTop(out, 5);
sessionbase.QuoteFlash(out);
// Fill in the blanks from the template
out.println(" | ");
out.println("");
out.println("");
out.println("");
out.println(" ");
out.println("");
out.println("");
out.println("");
out.println("");
out.println("");
out.println("Symbol | High | Low | Current | Volume | Offers | Transaction | ");
Companies Company = new Companies();
int vSize = dumpPlace.size();
for (i = 0 ; i < vSize ; i++) {
Company = (Companies)dumpPlace.elementAt(i);
Symbol = Company.getSymbol();
DayHi = Company.getHigh();
DayLo = Company.getLow();
Last = Company.getCurrent();
Volume = Company.getVolume();
out.println(" ");
out.println(" " + Symbol + " | ");
out.println(" " + numFormat.format(DayHi) + " | ");
out.println(" " + numFormat.format(DayLo) + " | ");
out.println(" " + numFormat.format(Last) + " | ");
out.println(" " + Volume + " | ");
out.println("View | ");
out.println(" Buy | ");
out.println(" / | ");
out.println(" Sell | ");
out.println(" ");
}
out.println(" ");
out.println(" | ");
out.println(" | ");
out.println("");
sessionbase.printBot(out, SessionId);
out.close();
}
public void displayTrans(HttpServletRequest request, HttpServletResponse response, String SessionId)
throws IOException, ServletException {
String Trans = (String)null;
int numShares = 0;
double tradeAt = 0.00;
String AccountId = (String)null;
String query = (String)null;
String Symbol = request.getParameter("Symbol");
String Name = (String)null;
ResultSet result = null;
// Set up decimal formatting for digits. This will force at least 0.00.
DecimalFormat curFormat = new DecimalFormat("$##,###,###,###,###,##0.00");
DecimalFormat numFormat = new DecimalFormat("################0.00");
DecimalFormat intFormat = new DecimalFormat("################0");
HttpSession session = request.getSession();
// Retrieve AccountId
AccountId = sessionbase.getAccountId(SessionId);
if (AccountId == null) {
response.sendRedirect(SystemURL + "/html/errors/not_logged_in.html");
} else {
// Initialize output stream
response.setContentType("text/html");
PrintWriter out=response.getWriter();
// Get Company name
query = "select Name from Companies where Symbol = '" + Symbol + "'";
result = sessionbase.doQuery(query);
try {
while(result.next()) {
Name = result.getString("Name");
}
} catch (Exception e) {
e.printStackTrace();
}
// Make sure the company has offers available
query = "select Trans from qt" + Symbol.toLowerCase();
result = sessionbase.doQuery(query, SessionId);
try {
while (result.next()) {
Trans = result.getString("Trans");
}
} catch (Exception e) {
e.printStackTrace();
}
if (Trans == null) {
sessionbase.printTop(out, 5);
sessionbase.QuoteFlash(out);
// Fill in the blanks from the template
out.println(" | ");
out.println("");
out.println("");
out.println("");
out.println(" ");
out.println("No Data ");
out.println("No offers are currently pending for this stock. ");
; out.println(" | ");
out.println("");
sessionbase.printBot(out, SessionId);
out.close();
} else {
// Extract Company info from the database. A null ResultSet means
// the company must not be in the database.
query = "select * from qt" + Symbol.toLowerCase();
result = sessionbase.doQuery(query, SessionId);
sessionbase.printTop(out, 5);
sessionbase.QuoteFlash(out);
// Fill in the blanks from the template
out.println(" | ");
out.println("");
out.println("");
out.println("");
out.println(" ");
out.println("" + Name + " (" + Symbol.toUpperCase() + ")");
out.println("");
out.println("");
out.println("");
out.println("");
out.println("");
out.println("Offer Type | Number of Shares | Amount per Share | Transaction | ");
try {
while(result.next()) {
Trans = result.getString("Trans");
numShares = result.getInt("NumShares");
tradeAt = result.getDouble("tradeAt");
if (Trans.equals("S")) {
Trans = "Sell";
} else if (Trans.equals("B")) {
Trans = "Buy";
} else {
Trans = "Unknown";
}
out.println(" ");
out.println("" + Trans + " | ");
out.println("" + intFormat.format(numShares) + " | ");
out.println("" + curFormat.format(tradeAt) + " | ");
if (Trans.equals("Buy")) {
out.println(" Sell | ");
} else if (Trans.equals("Sell")) {
out.println(" Buy | ");
out.println(" ");
}
}
} catch(Exception e) {
e.printStackTrace();
}
}
out.println(" ");
out.println(" | ");
out.println(" | ");
out.println("");
sessionbase.printBot(out, SessionId);
out.close();
}
}
public void displayAllTrans(HttpServletRequest request, HttpServletResponse response, String SessionId)
throws IOException, ServletException {
String Trans = (String)null;
int numShares = 0;
double tradeAt = 0.00;
String AccountId = (String)null;
String query = (String)null;
String Symbol = request.getParameter("Symbol");
String Name = (String)null;
ResultSet Symbols = null;
ResultSet result = null;
Vector dumpPlace = new Vector();
Vector Buys = new Vector();
Vector Sells = new Vector();
int i = 0;
int j = 0;
int timeStamp = 0;
int buy = 0;
int sell = 0;
int check = 1;
// Set up decimal formatting for digits. This will force at least 0.00.
DecimalFormat curFormat = new DecimalFormat("$##,###,###,###,###,##0.00");
DecimalFormat numFormat = new DecimalFormat("################0.00");
DecimalFormat intFormat = new DecimalFormat("################0");
HttpSession session = request.getSession();
// Initialize output stream
response.setContentType("text/html");
PrintWriter out=response.getWriter();
// Retrieve AccountId
AccountId = sessionbase.getAccountId(SessionId);
if (AccountId == null) {
response.sendRedirect(SystemURL + "/html/errors/not_logged_in.html");
} else {
// Output the header information
sessionbase.printTop(out, 5);
sessionbase.QuoteFlash(out);
// Fill in the blanks from the template
out.println(" | ");
out.println("");
out.println("");
out.println("");
out.println(" ");
out.println("");
out.println("");
out.println("");
out.println("");
out.println("");
out.println("Symbol | Offer Type | Number of Shares | Amount per Share | Transaction | ");
// Get symbols to use. These will be plugged in to the next search.
query = "select Symbol from Companies order by Symbol";
Symbols = sessionbase.doQuery(query);
try {
while (Symbols.next()) {
Symbol = Symbols.getString("Symbol");
// Extract all offers for the symbols
query = "select * from qt" + Symbol.toLowerCase() + " order by Trans, Dtime";
result = sessionbase.doQuery(query, SessionId);
while (result.next()) {
Trans = result.getString("Trans");
numShares = result.getInt("NumShares");
tradeAt = result.getDouble("tradeAt");
timeStamp = result.getInt("Dtime");
dumpPlace.addElement(new Offers(Symbol, Trans, tradeAt, numShares, timeStamp));
}
}
} catch (Exception e) {
e.printStackTrace();
}
int vSize = dumpPlace.size();
for (i = 0 ; i < vSize ; i++) {
Offers thisOffer = (Offers)dumpPlace.elementAt(i);
Symbol = thisOffer.getSymbol();
numShares = thisOffer.getShares();
tradeAt = thisOffer.getValue();
Trans = thisOffer.getTrans();
out.println(" ");
out.println("" + Symbol.toUpperCase() + " | ");
if (Trans.equals("B")) {
out.println("Buy | ");
} else {
out.println("Sell | ");
}
out.println("" + intFormat.format(numShares) + " | ");
out.println("" + curFormat.format(tradeAt) + " | ");
if (Trans.equals("B")) {
out.println(" Sell | ");
} else {
out.println(" Buy | ");
}
out.println(" ");
}
out.println("");
out.println(" ");
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 int setAt;
private String Trans;
public Offers () {
this.Symbol = (String)null;
this.offerValue = 0.00;
this.numShares = 0;
this.setAt = 0;
this.Trans = "B";
}
public Offers (String Symbol, String Trans, double offerValue, int numShares, int setAt) {
this.Symbol = Symbol;
this.offerValue = offerValue;
this.numShares = numShares;
this.setAt = setAt;
this.Trans = Trans;
}
public String getSymbol() { return Symbol; }
public String getTrans() { return Trans; }
public double getValue() { return offerValue; }
public int getShares() { return numShares; }
public int getTimestamp() { return setAt; }
}
static public void reload() {
SystemURL = sessionbase.getURL();
ServletURL = sessionbase.getServletURL();
SystemEmail = sessionbase.getEmail();
SystemPath = sessionbase.getPath();
SystemName = sessionbase.getName();
SystemShortName = sessionbase.getShortName();
}
}
| | | | | |