package isip.java.bullyse.bullydb; // file: BullyDB.java // import java.io.*; import java.util.*; import java.sql.*; import java.text.*; import javax.servlet.*; import javax.servlet.http.*; import javax.mail.*; import javax.mail.internet.*; import javax.activation.*; //************************************* // // ISIP_BullySE_BullyDB // //************************************* public class BullyDB { //************************************ // // private data // //************************************ // These are needed to initialize the jdbc connection // Change MySQL to the address of your MySQL server private String MySQL = "isip003.isip.msstate.edu:3306"; // Change this to the name you gave your Exchange database private String dBase = "bullyse"; private String _URL = "jdbc:mysql://" + MySQL + "/" + dBase; private String _user = "bullyse"; private String _pWord = "BullySe"; // Database objects private Connection conn; private ResultSet result; // Initialize the Parameters object private Parameters ExchangeParam = new Parameters(); // The Users Hashtable is used to store User objects. Each user has // their own User object created when they log in and it is stored, // by SessionID, in the Hashtable. static Hashtable Users = new Hashtable(); // Parameter data String SystemURL = getURL(); String ServletURL = getServletURL(); String SystemEmail = getEmail(); String SystemPath = getPath(); String SystemName = getName(); String SystemShortName = getShortName(); //************************************ // // constructor methods // //************************************ // default constructor // public BullyDB() {} public void putUser() { // Create a generic User for nonMember User nonMember = new User(); String nonMId = "nonMember"; if (Users.containsKey(nonMId)) { Users.remove(nonMId); Users.put(nonMId, nonMember); } else { Users.put(nonMId, nonMember); } } public Statement connect() { Statement stmt = null; // Lets start a database connection try { Class.forName("org.gjt.mm.mysql.Driver").newInstance(); } catch(Exception e){ e.printStackTrace(); } // Statement shouldn't exist. If it does, close it. if (stmt != null) { try { stmt.close(); } catch(SQLException e) { e.printStackTrace(); } } // Connection shouldn't exist. If it does, close it. if (conn != null) { try { conn.close(); } catch(SQLException e) { e.printStackTrace(); } } // Open a connection to the database try { conn = DriverManager.getConnection(_URL, _user, _pWord); } catch(SQLException e) { e.printStackTrace(); } while (conn == null) { // Error. This isn't good. java.util.Date rightNow = new java.util.Date(); int dMin = rightNow.getMinutes(); rightNow = new java.util.Date(0, 0, 0, 0, dMin, 0); java.util.Date countIt = new java.util.Date(0, 0, 0, 0, (dMin + 5), 0); while (countIt != rightNow) { rightNow = new java.util.Date(); dMin = rightNow.getMinutes() ; rightNow = new java.util.Date(0, 0, 0, 0, dMin, 0); } try { conn = DriverManager.getConnection(_URL, _user, _pWord); } catch(SQLException e) { } } // Set up the statement object try { // initialize the statment object stmt = conn.createStatement(); } catch(SQLException e){ e.printStackTrace(); } return stmt; } public boolean doLogon(String username, String password, String sessionId) throws ServletException, IOException { String AccountId = (String)null; String Username = (String)null; String Fname = (String)null; // format query that verifies account information String query = "select * from Account where Username = '" + username + "' and Password = password('" + password + "')"; result = doQuery(query); // pull the accountId out of the ResultSet if it existed try { while(result.next()) { AccountId = result.getString("AccountId"); Username = result.getString("Username"); Fname = result.getString("Fname"); } } catch(SQLException e) { e.printStackTrace(); } if (AccountId == null) { // User login failed, return an error return false; } else { // User logged in successfully. Create a new user object // and insert it into the Hashtable. User Trader = new User(AccountId, Username, Fname); // First, make sure no duplicate SessionId's exist in the Hashtable if (Users.containsKey(sessionId)) { Users.remove(sessionId); Users.put(sessionId, Trader); } else { Users.put(sessionId, Trader); } return true; } } // Query method for users logged in to the system. SessionId // is given so that we can use the MySQL stream stored in the // user object. On Query's it's not too big an issue but it's // not a bad habit to get into. public ResultSet doQuery(String query, String SessionId) throws IOException { ResultSet toReturn = null; // Find users object and call the Query method within it if (Users.containsKey(SessionId)) { User thisUser = (User)Users.get(SessionId); toReturn = thisUser.doQuery(query); return toReturn; } else { return null; } } // Update method for users logged in to the system. SessionId // is given so that we can use the MySQL stream stored in the // user object. Individual streams have to be used here in order // to allow for table locking. public void doUpdate(String query, String SessionId) throws IOException { // Find users object and call the Update method within it if (Users.containsKey(SessionId)) { User thisUser = (User)Users.get(SessionId); thisUser.doUpdate(query); } } public boolean checkAccount(String SessionId) throws IOException { String LockedStat = (String)null; String query = "select Locked from Account where AccountId = " + getAccountId(SessionId); ResultSet result = doQuery(query, SessionId); try { while (result.next()) { LockedStat = result.getString("Locked"); } } catch (Exception e) { e.printStackTrace(); } if (LockedStat.equals("Y")) { return true; } else { return false; } } // Query method for users not logged in. This might be people // currently logging in or just looking through the system. public ResultSet doQuery(String query) throws IOException { Statement stmt = null; result = null; stmt = connect(); try { result = stmt.executeQuery(query); stmt.close(); } catch(Exception e) { e.printStackTrace(); } if(result != null) { return result; } else { return null; } } // Update method for users not logged in. This would be used // for people trying to create an account. public void doUpdate(String query) throws IOException { Statement stmt = null; stmt = connect(); try { stmt.executeUpdate(query); } catch(Exception e) { e.printStackTrace(); } } // The User object holds on to all the data about the current // user. public class User { // Initialize individual database connection Connection conn = null; Statement stmt = null; ResultSet result = null; // Holds user information private String AccountId; private String Username; private String Fname; public User () { Username = "Guest"; Fname = "Guest"; connect(); } public User (String AccountId, String Username, String Fname) { this.AccountId = AccountId; this.Username = Username; this.Fname = Fname; connect(); } private void connect() { // Lets start a database connection try { Class.forName("org.gjt.mm.mysql.Driver").newInstance(); } catch(Exception e){ e.printStackTrace(); } // Statement shouldn't exist. If it does, close it. if (stmt != null) { try { stmt.close(); } catch(SQLException e) { e.printStackTrace(); } } // Connection shouldn't exist. If it does, close it. if (conn != null) { try { conn.close(); } catch(SQLException e) { e.printStackTrace(); } } // Open a connection to the database try { conn = DriverManager.getConnection(_URL, _user, _pWord); } catch(SQLException e) { e.printStackTrace(); } while (conn == null) { // Error. This isn't good. java.util.Date rightNow = new java.util.Date(); int dMin = rightNow.getMinutes(); rightNow = new java.util.Date(0, 0, 0, 0, dMin, 0); java.util.Date countIt = new java.util.Date(0, 0, 0, 0, (dMin + 5), 0); while (countIt != rightNow) { rightNow = new java.util.Date(); dMin = rightNow.getMinutes() ; rightNow = new java.util.Date(0, 0, 0, 0, dMin, 0); } try { conn = DriverManager.getConnection(_URL, _user, _pWord); } catch(SQLException e) { } } // Set up the statement object try { // initialize the statment object stmt = conn.createStatement(); } catch(SQLException e){ e.printStackTrace(); } } // Query method for a logged in user public ResultSet doQuery(String query) throws IOException { ResultSet thisResult = null; try { thisResult = stmt.executeQuery(query); } catch(Exception e) { e.printStackTrace(); } if(thisResult != null) { return thisResult; } else { return null; } } // Update method for a logged in user public void doUpdate(String query) throws IOException { try { stmt.executeUpdate(query); } catch(Exception e) { e.printStackTrace(); } } // Return user details if requested public String getAccountId() { return AccountId; } public String getUsername() { return Username; } public String getFname() { return Fname; } // This object holds details on the users Holdings. Used // by the showHoldings method. public class Holdings { private String Symbol; private String Trans; private double boughtAt; private int numShares; private double Last; public Holdings (String Symbol, double boughtAt, String Trans, int numShares, double Last) { this.Symbol = Symbol; this.boughtAt = boughtAt; this.Trans = Trans; this.numShares = numShares; this.Last = Last; } // Return details on the users holdings when requested. public String getSymbol() { return Symbol.toUpperCase(); } public double getboughtAt() { return boughtAt; } public int getnumShares() { return numShares; } public double getLast() { return Last; } public String getTrans() { return Trans; } public String getTransL() { // Check Trans type, return a String based on this. if (Trans.equals("B")) { return "Pending Purchase"; } else if (Trans.equals("S")) { return "Pending Sale"; } else if (Trans.equals("FB")) { return "Frozen Purchase"; } else if (Trans.equals("FS")) { return "Frozen Sale"; } else if (Trans.equals("C")) { return " "; } else { return "Unknown"; } } } // Remove the user object from the Hashtable when they log out public void doLogout(String SessionId) { Users.remove(SessionId); } // Show all holdings of the user public void showHoldings(PrintWriter out, int vTag) throws IOException, ServletException { String query = (String)null; ResultSet rslt = null; double Balance = 0.00; String Symbol = (String)null; double boughtAt = 0.00; String Trans = (String)null; double Last = 0.00; double Profit = 0.00; int numShares = 0; int count = 0; double Total = 0.00; Vector dumpPlace = new Vector(); String fcolor = "000000"; // 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"); BullyDB Dbase = new BullyDB(); // Extract users Balance from database query = "select Balance from Portfolio where AccountId = '" + AccountId + "'"; rslt = Dbase.doQuery(query); try { while(rslt.next()) { Balance = rslt.getDouble("Balance"); } } catch(Exception e) { e.printStackTrace(); } // Extract users Holdings from database query = "select t1.Symbol, t1.NumShares, t1.Trans, t1.tradeAt, t2.Last from " + Username + " as t1, Companies as t2 where t1.Symbol = t2.Symbol"; rslt = Dbase.doQuery(query); try { while (rslt.next()) { count++; Symbol = rslt.getString("Symbol"); numShares = rslt.getInt("NumShares"); Trans = rslt.getString("Trans"); boughtAt = rslt.getDouble("tradeAt"); Last = rslt.getDouble("Last"); // Place users Holdings into a Holdings object. Each new // object is stored in a temporary Vector. dumpPlace.addElement(new Holdings(Symbol, boughtAt, Trans, numShares, Last)); } } catch(Exception e) { e.printStackTrace(); } // Display user holdings if(count == 0) { if(vTag == 1) { out.println(""); out.println(""); out.println(""); out.println(""); out.println("
No holdings found for your account.
Please email " + SystemEmail + " if this is in error.
"); out.println("
"); out.println("Current Balance: " + curFormat.format(Balance) + "
"); } else { out.println("Current Balance: " + curFormat.format(Balance) + "
"); } } else { out.println("Listed below are your current holdings and their value.
"); out.println("

"); out.println(""); out.println("
"); out.println(""); out.println(" "); out.println(" "); out.println(" "); out.println(" "); out.println(" "); out.println(" "); out.println(" "); out.println(" "); // Loop to go through each of the stocks in the storage // vector. for(int i=0; i"); Holdings hold = (Holdings) dumpPlace.elementAt(i); Trans = hold.getTrans(); if (Trans.equals("C")) { Profit = hold.getLast() * hold.getnumShares(); Total += Profit; if (AccountId.equals("000000000001")) { out.println(""); } else { out.println(""); } } else { Profit = hold.getboughtAt() * hold.getnumShares(); if (AccountId.equals("000000000001")) { out.println(""); } else { out.println(""); } } } Total += Balance; out.println(""); out.println(""); out.println(""); if (vTag == 1) { out.println(""); out.println(""); out.println(""); } out.println(""); out.println("
StatusSymbolNumber of SharesCurrent BidNet Worth
Fixed" + hold.getSymbol() + "" + hold.getnumShares() + "" + curFormat.format(hold.getLast()) + "" + curFormat.format(Profit) + "
Fixed" + hold.getSymbol() + "" + hold.getnumShares() + "" + curFormat.format(hold.getLast()) + "" + curFormat.format(Profit) + "
" + hold.getTransL() + "" + hold.getSymbol() + "" + hold.getnumShares() + "" + curFormat.format(hold.getboughtAt()) + "" + curFormat.format(Profit) + "
" + hold.getTransL() + "" + hold.getSymbol() + "" + hold.getnumShares() + "" + curFormat.format(hold.getboughtAt()) + "" + curFormat.format(Profit) + "
"); out.println("
Balance
"); out.println("
" + curFormat.format(Balance) + "
"); out.println("
Total Net Worth
"); out.println("
" + curFormat.format(Total) + "
"); out.println("
"); } } } // Calls the users Object to retrieve the AccountId public String getAccountId(String sessionId) { if (Users.containsKey(sessionId)) { User thisUser = (User)Users.get(sessionId); return thisUser.getAccountId(); } else { return (String)null; } } // Calls the users Object to retrieve the Username public String getUsername(String sessionId) { User thisUser = (User)Users.get(sessionId); return thisUser.getUsername(); } // Calls the users Object to retrieve the first name public String getFname(String sessionId) { User thisUser = (User)Users.get(sessionId); return thisUser.getFname(); } // Retrieves the users Username from the database. Similar to // getUsername except the AccountId is used rather than the // SessionId. public String getUser(String userId) throws IOException { String uName = (String)null; result = doQuery("select Username from Account where AccountId = " + userId); try { while(result.next()) { uName = result.getString("Username"); } } catch(Exception e) { e.printStackTrace(); } return uName; } // Call the user object showHoldings public void showHoldings(PrintWriter out, int vTag, String SessionId) throws IOException, ServletException { User thisUser = (User)Users.get(SessionId); thisUser.showHoldings(out, vTag); } // Call the user object Logout public void doLogout(String SessionId) { User thisUser = (User)Users.get(SessionId); thisUser.doLogout(SessionId); } // Handles sending email to a user. public void sendEmail(String subject, String to, String from, String body) throws IOException { // Insert variables into the Mail properties Properties props = new Properties(); props.put("mail.smtp.host", "isip.msstate.edu"); Session session = Session.getDefaultInstance(props, null); // Set up and send the message try { // Create a new Message Message msg = new MimeMessage(session); // Put the to and from address into the Message msg.setFrom(new InternetAddress(from)); InternetAddress[] address = {new InternetAddress(to)}; msg.setRecipients(Message.RecipientType.TO, address); // Set the message subject and date msg.setSubject(subject); msg.setSentDate(new java.util.Date()); // Insert the message body msg.setText(body); // Send the message Transport.send(msg); } catch(Exception e) { e.printStackTrace(); } } // Handles file output. Typically displays HTML templates. public void printFile(String fileName, PrintWriter out) throws IOException { try { // Open input file DataInputStream inFile = new DataInputStream(new FileInputStream(fileName)); // Loop through each line of the file while(inFile.available() != 0) { // Print contents of input file out.println(inFile.readLine()); } // Close input file inFile.close(); } catch(Exception e) { e.printStackTrace(); } } // This method displays the Quote Flash box on several BSE pages. // It simply picks the 5 highest valued stocks. public void QuoteFlash(PrintWriter out) throws IOException, ServletException { String query = (String)null; // Storage variables to hold the 5 values for display String Sym = (String)null; String Sym1 = (String)null; String Sym2 = (String)null; String Sym3 = (String)null; String Sym4 = (String)null; String Sym5 = (String)null; double Val = 0.00; double Val1 = 0.00; double Val2 = 0.00; double Val3 = 0.00; double Val4 = 0.00; double Val5 = 0.00; // Number format to force at least 0.00 DecimalFormat numFormat = new DecimalFormat("################0.00"); // Read in the Symbols and Last values query = "select Symbol, Last from Companies"; result = doQuery(query); try { while (result.next()) { Sym = result.getString("Symbol"); Val = result.getDouble("Last"); // Compare values and swap out the greater ones to // get the top 5. If a greater value is found, all // lesser values have to be moved down one. if (Val > Val1) { Val5 = Val4; Sym5 = Sym4; Val4 = Val3; Sym4 = Sym3; Val3 = Val2; Sym3 = Sym2; Val2 = Val1; Sym2 = Sym1; Val1 = Val; Sym1 = Sym; } else if (Val > Val2) { Val5 = Val4; Sym5 = Sym4; Val4 = Val3; Sym4 = Sym3; Val3 = Val2; Sym3 = Sym2; Val2 = Val; Sym2 = Sym; } else if (Val > Val3) { Val5 = Val4; Sym5 = Sym4; Val4 = Val3; Sym4 = Sym3; Val3 = Val; Sym3 = Sym; } else if (Val > Val4) { Val5 = Val4; Sym5 = Sym4; Val4 = Val; Sym4 = Sym; } else if (Val > Val5) { Val5 = Val; Sym5 = Sym; } } } catch (Exception e) { e.printStackTrace(); } if (Sym1 != null) { // Output Quote Flash table out.println(""); out.println("
"); out.println(""); out.println(""); out.println(""); out.println(""); } out.println("
"); out.println("Quote Flash
top stocks

"); if (Sym1 != null) { out.println("° " + Sym1.toUpperCase() + "
"); } if (Sym2 != null) { out.println("° " + Sym2.toUpperCase() + "
"); } if (Sym3 != null) { out.println("° " + Sym3.toUpperCase() + "
"); } if (Sym4 != null) { out.println("° " + Sym4.toUpperCase() + "
"); } if (Sym5 != null) { out.println("° " + Sym5.toUpperCase() + "

"); } out.println("

"); if (Sym1 != null) { out.println(numFormat.format(Val1) + "
"); } if (Sym2 != null) { out.println(numFormat.format(Val2) + "
"); } if (Sym3 != null) { out.println(numFormat.format(Val3) + "
"); } if (Sym4 != null) { out.println(numFormat.format(Val4) + "
"); } if (Sym5 != null) { out.println(numFormat.format(Val5) + "

"); out.println(""); out.println("MoreAll Quotes"); out.println("
"); } } // Reload any system parameters if changes are made public void loadParams() { ExchangeParam.reload(); } // This object holds all of the Parameters used in the Exchange. public class Parameters { private String URL; private String ServletURL; private String Path; private String Email; private String Name; private String ShortName; private String Frozen; private String Locked; public Parameters () { try { getParameters(); } catch (Exception e) { e.printStackTrace(); } } public void reload() { try { getParameters(); } catch (Exception e) { e.printStackTrace(); } // Load the individual objects Admin Admn = new Admin(); profile Prfl = new profile(); buysell BySl = new buysell(); Quotes Qts = new Quotes(); Portfolio Prtf = new Portfolio(); News Nws = new News(); nonMember Lgn = new nonMember(); // Reload each individual class Admn.reload(); Prfl.reload(); BySl.reload(); Qts.reload(); Prtf.reload(); Nws.reload(); Lgn.reload(); // Free the memory back up Admn = null; Prfl = null; BySl = null; Qts = null; Prtf = null; Nws = null; Lgn = null; } private void getParameters() throws IOException { String query = "select * from Parameters"; ResultSet result = null; result = doQuery(query); try { while (result.next()) { URL = result.getString("URL"); ServletURL = result.getString("ServletURL"); Path = result.getString("Path"); Email = result.getString("Email"); Name = result.getString("Name"); ShortName = result.getString("ShortName"); Frozen = result.getString("Frozen"); Locked = result.getString("Locked"); } } catch (Exception e) { e.printStackTrace(); } } public String getURL() { return URL; } public String getServletURL() { return ServletURL; } public String getPath() { return Path; } public String getEmail() { return Email; } public String getName() { return Name; } public String getShortName() { return ShortName; } public String getFreezeStatus() { return Frozen; } public String getLockStatus() { return Locked; } } public String getURL() { return ExchangeParam.getURL(); } public String getServletURL() { return ExchangeParam.getServletURL(); } public String getPath() { return ExchangeParam.getPath(); } public String getEmail() { return ExchangeParam.getEmail(); } public String getName() { return ExchangeParam.getName(); } public String getShortName() { return ExchangeParam.getShortName(); } public String getFreezeStatus() { return ExchangeParam.getFreezeStatus(); } public String getLockStatus() { return ExchangeParam.getLockStatus(); } public boolean checkRoot(String userName) throws IOException { String rootName = (String)null; // Retrieve the root user's name String query = "select Username from Account where AccountId = 1"; ResultSet result = doQuery(query); try { while (result.next()) { rootName = result.getString("Username"); } } catch (Exception e) { e.printStackTrace(); } rootName = rootName.toLowerCase(); userName = userName.toLowerCase(); if (rootName.equals(userName)) { return true; } else { return false; } } // These methods handle printing the top and bottom sections of BSE public void printTop(PrintWriter out, int On) throws IOException { // On values: // 1 - Home // 2 - Portfolio // 3 - Account // 4 - Buy & Sell // 5 - Quotes // 6 - News out.println(""); out.println(""); out.println("" + SystemName + ""); out.println(""); out.println(""); out.println(""); out.println(""); out.println(" "); out.println(" "); out.println(""); out.println(""); out.println(""); out.println(""); out.println(" "); out.println(""); out.println(""); out.println(" "); out.println(""); out.println(""); out.println(" "); out.println("
"); out.println("
"); out.println(""); out.println("Help
"); out.println("Logout
"); out.println("
"); out.println(""); out.println(""); out.println("
 
"); out.println(""); if (On == 1) { out.println("\"Home\"\"My\"My\"Buy\"Quotes\"News"); } else if (On == 2) { out.println("\"Home\"\"My\"My\"Buy\"Quotes\"News"); } else if (On == 3) { out.println("\"Home\"\"My\"My\"Buy\"Quotes\"News"); } else if (On == 4) { out.println("\"Home\"\"My\"My\"Buy\"Quotes\"News"); } else if (On == 5) { out.println("\"Home\"\"My\"My\"Buy\"Quotes\"News"); } else if (On == 6) { out.println("\"Home\"\"My\"My\"Buy\"Quotes\"News"); } out.println(""); out.println("
"); out.println(""); out.println(" "); out.println(""); out.println(""); out.println("
"); out.println(""); out.println(" "); out.println(" "); out.println(""); out.println(""); out.println(" "); out.println("
"); out.println(""); out.println(" "); out.println(" "); out.println(""); out.println(""); out.println(" "); out.println(""); out.println(""); out.println(" "); out.println("
"); out.println("Tools
"); out.println("
for research
"); out.println("
"); out.println("° View a quote: "); out.println("
"); out.println(""); out.println(" "); out.println(""); out.println(""); out.println(""); out.println("
"); out.println(""); out.println(" "); out.println(""); out.println("
"); out.println("
"); out.println("
"); out.println("
"); out.println("
"); out.println("
"); } public void printBot(PrintWriter out, String SessionId) throws IOException { Locale locale = new Locale("en","US"); DateFormat shortFormat = DateFormat.getDateInstance(DateFormat.SHORT, locale); String weekTitle = (String)null; String weekId = (String)null; java.util.Date weekDate = new java.util.Date(); java.util.Date weekDateO = new java.util.Date(); out.println(""); out.println(" "); out.println(""); out.println(""); out.println("
  "); out.println(""); out.println(" "); out.println(" "); out.println("
"); out.println(""); out.println(" "); out.println(" "); out.println(""); out.println("
"); out.println(" Market News
"); // We only want to get Headlines within 2 weeks of the current // date, set up the Date qualifier. String query = "select Title, Id, Date from News order by Date desc"; result = doQuery(query, SessionId); out.println(""); int reps = 0; try { while (result.next() && reps < 6) { weekDateO = weekDate; reps++; weekTitle = result.getString("Title"); weekId = result.getString("Id"); weekDate = result.getDate("Date"); if (!weekDate.equals(weekDateO)) { // Just a formatting thing, don't want to push down the links // if it is printing the top one. if (reps > 1) { out.println("
"); } out.println(""); out.println(""); } else { out.println(""); } } } catch (Exception e) { e.printStackTrace(); } out.println(""); // Check to see if this is a Company user. If so, give Add // News link. query = "select Type from Account where AccountId = " + getAccountId(SessionId); result = doQuery(query, SessionId); String Type = (String)null; try { while (result.next()) { Type = result.getString("Type"); } } catch (Exception e) { e.printStackTrace(); } if (Type.equals("C")) { out.println(""); } out.println(""); out.println("
" + shortFormat.format(weekDate) + "
°" + weekTitle + "
°" + weekTitle + "
MoreAdd News
MoreAll Headlines
"); out.println("
"); out.println(""); out.println(" "); out.println(""); out.println(""); out.println(""); out.println(""); out.println(""); out.println(""); out.println("


"); out.println(""); out.println(" "); out.println(" "); out.println(""); out.println(""); out.println(""); out.println(" "); out.println("
"); out.println("

"); out.println("Help |"); out.println("Home | "); out.println("Quotes & Research |"); out.println("Buy & Sell
"); out.println("My Portfolio |"); out.println(" My Account | "); out.println(" News Headlines |"); out.println("

Page-Top
"); out.println(""); out.println(""); } public boolean checkRules (String Symbol, int numShares, double Value, String Trans, int Action, String SessionId) throws IOException { double ChangeCap = 0; double ChangeValue = 0; double compValue = 0; int compShares = 0; double Cap = 0; ResultSet result = null; String query = (String)null; boolean pass = true; // The rules are coded as simple checks. The values for these rules can be // altered under the Administrative Parameters page so let's first retrieve // these values. // // Note, ChangeValue must be a decimal number. query = "select ChangeCap, ChangeValue from Parameters"; result = doQuery(query, SessionId); try { while (result.next()) { ChangeCap = result.getDouble("ChangeCap"); ChangeValue = result.getDouble("ChangeValue"); } } catch (Exception e) { e.printStackTrace(); } // Okay, general format for the rules: // // If the trade would change the Market Cap more than ChangeCap, freeze // the transaction. // // If the trade changes the Value more than ChangeValue, then freeze. // Retrieve Company info query = "select Last, Tshares from Companies where Symbol = '" + Symbol + "'"; result = doQuery(query, SessionId); try { while (result.next()) { compValue = result.getDouble("Last"); compShares = result.getInt("Tshares"); } } catch (Exception e) { e.printStackTrace(); } // Compute market campitalization Cap = compValue * (double)compShares; // Are we freezing the transaction or just returning if it passes the test? if (Action == 1) { // Simply reporting if the transaction passes. // Transaction type doesn't matter for simply checking the transaction // so we can ignore that here. // If ChangeCap = 0 then no check needs to be done if (ChangeCap != 0) { double checkCap = Value * (double)compShares; if ((checkCap - Cap) >= ChangeCap || (checkCap - Cap) <= -ChangeCap) { pass = false; } } // If ChangeValue = 0 then no check needs to be done if (ChangeValue != 0) { if ((compValue / Value) <= (1 - ChangeValue)) { pass = false; } } } else { // Freeze the transaction if it doesn't pass // If ChangeCap = 0 then no check needs to be done if (ChangeCap != 0) { double checkCap = Value * (double)compShares; if ((checkCap - Cap) >= 100000 || (checkCap - Cap) <= -100000) { pass = false; } } // If ChangeValue = 0 then no check needs to be done if (ChangeValue != 0) { if ((compValue / Value) <= (1 - ChangeValue)) { pass = false; } } if (!pass) { // Transaction must have failed, let's move it to frozen. // Tables should all be locked, let's move on with transfering the // transaction. // Delete the offer from the queue query = "delete from qt" + Symbol.toLowerCase() + " where AccountId = " + getAccountId(SessionId) + " and Trans = '" + Trans + "'"; doUpdate(query, SessionId); // Switch to Frozen in User Holdings query = "update " + getUsername(SessionId) + " set Trans = 'F" + Trans + "' where Symbol = '" + Symbol + "' and Trans = '" + Trans + "'"; doUpdate(query, SessionId); // Add to Frozen table query = "insert into Frozen (AccountId, Symbol, Trans, NumShares, tradeAt) values (" + getAccountId(SessionId) + ", '" + Symbol + "', '" + Trans + "', " + numShares + ", " + Value + ")"; doUpdate(query, SessionId); } } return pass; } public void displayGraph(PrintWriter out, String Type, String Symbol, int Links) { out.println("

"); out.println(""); out.println(""); out.println(""); out.println(""); out.println(""); out.println(""); out.println("</comment>No JDK 1.2 support for applet!"); out.println(""); if (Links == 1) { out.println("
"); if (Type.equals("1")) { out.println(""); out.println(""); out.println(""); } else if (Type.equals("2")) { out.println(""); out.println(""); out.println(""); } else if (Type.equals("3")) { out.println(""); out.println(""); out.println(""); } else if (Type.equals("4")) { out.println(""); out.println(""); out.println(""); } out.println("
This Week13 Week52 WeekToday13 Week52 WeekTodayThis Week52 WeekTodayThis Week13 Week
"); } else if (Links == 2) { out.println("
"); if (Type.equals("1")) { out.println(""); out.println(""); out.println(""); } else if (Type.equals("2")) { out.println(""); out.println(""); out.println(""); } else if (Type.equals("3")) { out.println(""); out.println(""); out.println(""); } else if (Type.equals("4")) { out.println(""); out.println(""); out.println(""); } out.println("
This Week13 Week52 WeekToday13 Week52 WeekTodayThis Week52 WeekTodayThis Week13 Week
"); } out.println("

"); } public boolean checkLock() throws IOException { String query = (String)null; ResultSet result = null; String Lock = (String)null; query = "select Locked from Parameters"; result = doQuery(query); try { while (result.next()) { Lock = result.getString("Locked"); } } catch (Exception e) { e.printStackTrace(); } if (Lock.equals("Y")) { return true; } else { return false; } } public boolean checkFreeze() throws IOException { String query = (String)null; ResultSet result = null; String Freeze = (String)null; query = "select Frozen from Parameters"; result = doQuery(query); try { while (result.next()) { Freeze = result.getString("Frozen"); } } catch (Exception e) { e.printStackTrace(); } if (Freeze.equals("Y")) { return true; } else { return false; } } }