PROGRAM 1
// Filename: Application.html Author: Anthony F. Ortiz // This is the JavaScript to the program, Stock Trader. HTML is not // included. // There are two global variables in this program. They are loginOk and // account. var loginOK = false; var account = ""; // This function is used many times in this program in order to determine // whether the html text fields are blank or not. function isblank (str) { for (var i = 0; i < str.length; i++) { var ch = str.charAt (i); if ((ch != ' ') && (ch != '\n') && (ch != '\t')) { return false; } } return true; } // This function is used many times in the program to determine whether // a user is logged into the program. function isLoggedIn () { if (! loginOK) { alert ("Please, login to the system."); } return loginOK; } // Get account (in other words login). function getAccount () { var stockform = document.STOCKFORM; var txtarea = stockform.LISTING; var txtaccount = stockform.ACCOUNT.value; var txtpassword = stockform.IPASSWORD.value; var db = ""; var rsaccount = ""; var rsinvestors = ""; var ssn = ""; var iname = ""; var email = ""; if (loginOK == true) { alert ("Please logout before logining in"); return false; } if (isblank (txtaccount)) { alert ("Please, enter Account#."); } else if (isblank (txtpassword)) { alert ("Please, enter Password."); } else { db = new Packages.DataBase ("cs742_18", "cs742_18", "qnYFtEUA"); // Can throw. rsaccount = db.execute ("select * from Accounts where AcctN = \"" + txtaccount + "\" and Password = \"" + txtpassword + "\""); if (! rsaccount.next ()) { alert ("Please type in correct Account# and Password."); } else { account = txtaccount; txtarea.value = "Login is ok: " + account + "."; loginOK = true; ssn = rsaccount.getString ("SSN"); rsinvestors = db.execute ("select * from Investors where SSN = \"" + ssn + "\""); if (rsinvestors.next ()) { iname = rsinvestors.getString ("Name"); email = rsinvestors.getString ("Email"); stockform.INAME.value = iname; stockform.SSN.value = ssn; stockform.EMAIL.value = email; } } db.close (); } return true; } // Get orders. function getOrders () { var stockform = document.STOCKFORM; var txtarea = stockform.LISTING; var db = ""; var ordersrs = ""; var ordersmd = ""; var SQLutil = ""; if (isLoggedIn ()) { db = new Packages.DataBase ("cs742_18", "cs742_18", "qnYFtEUA"); // Can throw. ordersrs = db.execute ("select * from Orders where AcctN = " + account); ordersmd = ordersrs.getMetaData (); SQLutil = Packages.SQLutil; // Like an import. txtarea.value = SQLutil.getRow (ordersrs, ordersmd, true) + "\n"; while (ordersrs.next ()) { txtarea.value += SQLutil.getRow (ordersrs, ordersmd, false) + "\n"; } db.close (); } return true; } // Get quote. function getQuote () { var stockform = document.STOCKFORM; var txtarea = stockform.LISTING; var txtstock = stockform.STOCK.value; var db = ""; var stocksrs = ""; var stocksmd var SQLutil = ""; if (isblank (txtstock)) { alert ("Please, enter stock symbol."); } else { db = new Packages.DataBase ("cs742_18", "cs742_18", "qnYFtEUA"); // Can throw. stocksrs = db.execute ("select * from Stocks where Symb = \"" + txtstock + "\""); stocksmd = stocksrs.getMetaData (); SQLutil = Packages.SQLutil; // Like an import. txtarea.value = SQLutil.getRow (stocksrs, stocksmd, true) + "\n"; while (stocksrs.next ()) { txtarea.value += SQLutil.getRow (stocksrs, stocksmd, false) + "\n"; } db.close (); } return true; } // Get holdings (in other words get portfolio). function getHoldings () { var stockform = document.STOCKFORM; var txtarea = stockform.LISTING; var db = ""; var holdingsrs = ""; var holdingsmd = ""; var SQLutil = ""; if (isLoggedIn ()) { db = new Packages.DataBase ("cs742_18", "cs742_18", "qnYFtEUA"); // Can throw. holdingsrs = db.execute ("select * from Holdings where AcctN = " + account); holdingsmd = holdingsrs.getMetaData (); SQLutil = Packages.SQLutil; // Like an import. txtarea.value = SQLutil.getRow (holdingsrs, holdingsmd, true) + "\n"; while (holdingsrs.next ()) { txtarea.value += SQLutil.getRow (holdingsrs, holdingsmd, false) + "\n"; } db.close (); } return true; } // Get transactions. function getTransactions () { var stockform = document.STOCKFORM; var txtarea = stockform.LISTING; var db = ""; var transactionsrs = ""; var transactionsmd = ""; var SQLutil = ""; if (isLoggedIn ()) { db = new Packages.DataBase ("cs742_18", "cs742_18", "qnYFtEUA"); // Can throw. transactionsrs = db.execute ("select * from Transactions where AcctN = " + account); transactionsmd = transactionsrs.getMetaData (); SQLutil = Packages.SQLutil; // Like an import. txtarea.value = SQLutil.getRow (transactionsrs, transactionsmd, true) + "\n"; while (transactionsrs.next ()) { txtarea.value += SQLutil.getRow (transactionsrs, transactionsmd, false) + "\n"; } db.close (); } return true; } // Deposit cash. function depositCash () { var stockform = document.STOCKFORM; var txtarea = stockform.LISTING; var txtdeposit = stockform.CASH.value; var db = ""; var accountsrs = ""; var balance = ""; var SQLutil = ""; var deposit = ""; if (isLoggedIn ()) { if (isblank (txtdeposit)) { alert ("Please, enter the amount of cash to deposit."); } else { db = new Packages.DataBase ("cs742_18", "cs742_18", "qnYFtEUA"); // Can throw. accountsrs = db.execute ("select Cash from Accounts where AcctN = " + account); if (accountsrs.next ()) { balance = accountsrs.getString ("Cash"); SQLutil = Packages.SQLutil; // Like an import. balance = SQLutil.toDouble (balance); deposit = SQLutil.toDouble (txtdeposit); balance = balance + deposit; db.execute ("update Accounts set Cash = " + balance + " where AcctN = " + account); db.execute ("insert into Transactions values (NULL, " + account + ", \'~~~\', \'DEPOSIT\', " + 0 + ", " + deposit + ")"); txtarea.value = "Deposit = " + deposit + " New Balance = " + balance + "\n"; } db.close (); } } return true; } // Withdrawl cash. function withdrawCash () { var stockform = document.STOCKFORM; var txtarea = stockform.LISTING; var txtwithdrawl = stockform.CASH.value; var db = ""; var accountsrs = ""; var balance = ""; var SQLutil = ""; var withdrawl = ""; if (isLoggedIn ()) { if (isblank (txtwithdrawl)) { alert ("Please, enter the amount of cash to withdrawl."); } else { db = new Packages.DataBase ("cs742_18", "cs742_18", "qnYFtEUA"); // Can throw. accountsrs = db.execute ("select Cash from Accounts where AcctN = " + account); if (accountsrs.next ()) { balance = accountsrs.getString ("Cash"); SQLutil = Packages.SQLutil; // Like an import. balance = SQLutil.toDouble (balance); withdrawl = SQLutil.toDouble (txtwithdrawl); balance = balance - withdrawl; if (balance < 0) { alert ("Please withdrawl less money."); } else { db.execute ("update Accounts set Cash = " + balance + " where AcctN = " + account); db.execute ("insert into Transactions values (NULL, " + account + ", \'~~~\', \'WITHDRAW\', " + 0 + ", " + withdrawl + ")"); txtarea.value = "Withdrawl = " + withdrawl + " New Balance = " + balance + "\n"; } } db.close (); } } return true; } // Leave account (in other words logout). function leaveAccount () { var stockform = document.STOCKFORM; var txtarea = stockform.LISTING; if (isLoggedIn ()) { txtarea.value = "Logout is ok: " + account + "."; loginOK = false; account = ""; stockform.ACCOUNT.value = ""; stockform.IPASSWORD.value = ""; stockform.INAME.value = ""; stockform.SSN.value = ""; stockform.EMAIL.value = ""; stockform.CASH.value = ""; stockform.SHARES.value = ""; stockform.PRICE.value = ""; stockform.STOCK.value = ""; } return true; } // Register investor. function registerInvestor () { var stockform = document.STOCKFORM; var txtarea = stockform.LISTING; var txtaccount = stockform.ACCOUNT.value; var txtpassword = stockform.IPASSWORD.value; var txtname = stockform.INAME.value; var txtssn = stockform.SSN.value; var txtemail = stockform.EMAIL.value; var flag = 0; var db = ""; var investorsrs = ""; var ssn = ""; var accountsrs = ""; if (loginOK == true) { alert ("Please logout before registering investor."); return false; } if (isblank (txtpassword)) { alert ("Please enter password."); } else if (isblank (txtname)) { alert ("Please enter name."); } else if (isblank (txtssn)) { alert ("Please enter ssn."); } else if (isblank (txtemail)) { alert ("Please enter e-mail"); } else { db = new Packages.DataBase ("cs742_18", "cs742_18", "qnYFtEUA"); // Can throw. investorsrs = db.execute ("select SSN from Investors"); while (investorsrs.next ()) { ssn = investorsrs.getString ("SSN"); if (ssn == txtssn) { flag = 1; } } if (flag == 1) { alert ("Please enter a new social security number."); } else { investorsrs = db.execute ("insert into Investors values (\'" + txtssn + "\'," + "\'" + txtname + "\'," + "\'" + txtemail + "\'" + ")"); accountsrs = db.execute ("insert into Accounts values (NULL, \'" + txtssn + "\', " + "\'" + txtpassword + "\', " + 0 + ")"); accountsrs = db.execute ("select AcctN from Accounts where Password = \"" + txtpassword + "\" and SSN = \"" + txtssn + "\""); if (accountsrs.next ()) { account = accountsrs.getString ("AcctN"); txtarea.value = "NInvestor is ok: " + account + "."; stockform.ACCOUNT.value = account; } } db.close (); } return true; } // Register account. function registerAccount () { var stockform = document.STOCKFORM; var txtarea = stockform.LISTING; var txtpassword = stockform.IPASSWORD.value; var ssn = ""; var db = ""; var accountsrs = ""; if (isLoggedIn ()) { if (isblank (txtpassword)) { alert ("Please enter a password."); } else { db = new Packages.DataBase ("cs742_18", "cs742_18", "qnYFtEUA"); // Can throw. accountsrs = db.execute ("select * from Accounts where AcctN = " + account); if (accountsrs.next ()) { ssn = accountsrs.getString ("SSN"); accountsrs = db.execute ("insert into Accounts values (NULL, \'" + ssn + "\', " + "\'" + txtpassword + "\', " + 0 + ")"); accountsrs = db.execute ("select AcctN from Accounts where Password = \"" + txtpassword + "\" and SSN = \"" + ssn + "\""); if (accountsrs.next ()) { account = accountsrs.getString ("AcctN"); txtarea.value = "NAccount is ok: " + account + "."; stockform.ACCOUNT.value = account; } } db.close (); } } return true; } // Buy stock (total, no partial). function buyStock () { var stockform = document.STOCKFORM; var txtarea = stockform.LISTING; var txtstock = stockform.STOCK.value; var txtprice = stockform.PRICE.value; var txtshares = stockform.SHARES.value; var db = ""; var accountsrs = ""; var stocksrs = ""; var ordersrs = ""; var transactions = ""; var holdingsrs = ""; var SQLutil = ""; var stock = ""; var price = ""; var vol = ""; var total = ""; var balance = ""; var newvol = ""; var shares = ""; var existingholdingshares = ""; var newholdingshares = ""; if (isLoggedIn ()) { if (isblank (txtstock)) { alert ("Please, enter a stock symbol."); } else if (isblank (txtprice)) { alert ("Please, enter price."); } else if (isblank (txtshares)) { alert ("Please enter shares."); } else { db = new Packages.DataBase ("cs742_18", "cs742_18", "qnYFtEUA"); // Can throw. SQLutil = Packages.SQLutil; // Like an import. stocksrs = db.execute ("select * from Stocks where Symb = \"" + txtstock + "\""); if (stocksrs.next ()) { stock = stocksrs.getString ("Symb"); price = stocksrs.getString ("Price"); vol = stocksrs.getString ("Vol"); price = SQLutil.toDouble (price); shares = SQLutil.toDouble (shares); buystock = txtstock; buyprice = SQLutil.toDouble (txtprice); buyshares = SQLutil.toDouble (txtshares); if (price > buyprice) { alert ("Please enter a higher bid -> " + price + " or more."); } if (vol < buyshares) { alert ("Please enter less shares."); } } if (price <= buyprice && vol >= buyshares) { accountsrs = db.execute ("select Cash from Accounts where AcctN = " + account); if (accountsrs.next ()) { balance = accountsrs.getString ("Cash"); balance = SQLutil.toDouble (balance); total = SQLutil.toDouble (total); total = price * buyshares; if (balance < total) { alert ("Please deposit more money into your account or order fewer shares."); } else { vol = SQLutil.toDouble (vol); newvol = SQLutil.toDouble (newvol); balance = balance - total; newvol = vol - buyshares; accountsrs = db.execute ("update Accounts set Cash = " + balance + " where AcctN = " + account); ordersrs = db.execute ("insert into Orders values (NULL, " + account + ", \'" + stock + "\', NULL, \'BUY\', " + buyshares + ", " + price + ")"); stocksrs = db.execute ("update Stocks set Vol = " + newvol + " where Symb = \'" + stock + "\'"); holdingsrs = db.execute ("select * from Holdings where AcctN = " + account + " and Symb = \'" + stock + "'"); transactionsrs = db.execute ("insert into Transactions values (NULL, " + account + ", \'" + stock + "\', \'BOUGHT\', " + buyshares + ", " + price + ")"); if (holdingsrs.next ()) { existingholdingshares = holdingsrs.getString ("Qty"); existingholdingshares = SQLutil.toDouble (existingholdingshares); newholdingshares = SQLutil.toDouble (newholdingshares); newholdingshares = existingholdingshares + buyshares; holdingsrs = db.execute ("update Holdings set Qty = " + newholdingshares + " where AcctN = " + account + " and Symb = \'" + stock + "\'"); } else { holdingsrs = db.execute ("insert into Holdings values (" + account + ", \'" + stock + "\', " + buyshares + ")"); } txtarea.value = "Stock = " + stock + " Price = " + price + " Quantity = " + buyshares + " Total = " + total + " Balance = " + balance; } } } db.close (); } } return true; } // Sell stock. function sellStock () { var stockform = document.STOCKFORM; var txtarea = stockform.LISTING; var txtstock = stockform.STOCK.value; var txtprice = stockform.PRICE.value; var txtshares = stockform.SHARES.value; var db = ""; var accountsrs = ""; var stocksrs = ""; var ordersrs = ""; var transactions = ""; var holdingsrs = ""; var SQLutil = ""; var stock = ""; var price = ""; var vol = ""; var total = ""; var balance = ""; var newvol = ""; var shares = ""; var existingholdingshares = ""; var newholdingshares = ""; if (isLoggedIn ()) { if (isblank (txtstock)) { alert ("Please, enter a stock symbol."); } else if (isblank (txtprice)) { alert ("Please, enter price."); } else if (isblank (txtshares)) { alert ("Please enter shares."); } else { db = new Packages.DataBase ("cs742_18", "cs742_18", "qnYFtEUA"); // Can throw. SQLutil = Packages.SQLutil; // Like an import. holdingsrs = db.execute ("select * from Holdings where AcctN = " + account + " and Symb = \"" + txtstock + "\""); if (holdingsrs.next ()) { stock = holdingsrs.getString ("Symb"); qty = holdingsrs.getString ("Qty"); qty = SQLutil.toDouble (qty); sellstock = txtstock; sellprice = SQLutil.toDouble (txtprice); sellshares = SQLutil.toDouble (txtshares); if (qty < sellshares) { alert ("Please enter less shares or buy some more shares."); } else { stocksrs = db.execute ("select * from Stocks where Symb = \"" + stock + "\""); if (stocksrs.next ()) { price = stocksrs.getString ("Price"); price = SQLutil.toDouble (price); vol = stocksrs.getString ("Vol"); vol = SQLutil.toDouble (vol); if (price < sellprice) { alert ("Please enter a lower bid -> " + price + " or less."); } else { accountsrs = db.execute ("select Cash from Accounts where AcctN = " + account); if (accountsrs.next ()) { balance = accountsrs.getString ("Cash"); balance = SQLutil.toDouble (balance); total = SQLutil.toDouble (total); total = price * sellshares; vol = SQLutil.toDouble (vol); newvol = SQLutil.toDouble (newvol); balance = balance + total; newvol = vol + sellshares; accountsrs = db.execute ("update Accounts set Cash = " + balance + " where AcctN = " + account); ordersrs = db.execute ("insert into Orders values (NULL, " + account + ", \'" + stock + "\', NULL, \'SELL\', " + sellshares + ", " + price + ")"); stocksrs = db.execute ("update Stocks set Vol = " + newvol + " where Symb = \'" + stock + "\'"); holdingsrs = db.execute ("select * from Holdings where AcctN = " + account + " and Symb = \'" + stock + "'"); transactionsrs = db.execute ("insert into Transactions values (NULL, " + account + ", \'" + stock + "\', '\SOLD\', " + sellshares + ", " + price + ")"); if (holdingsrs.next ()) { existingholdingshares = holdingsrs.getString ("Qty"); existingholdingshares = SQLutil.toDouble (existingholdingshares); newholdingshares = SQLutil.toDouble (newholdingshares); newholdingshares = existingholdingshares - sellshares; holdingsrs = db.execute ("update Holdings set Qty = " + newholdingshares + " where AcctN = " + account + " and Symb = \'" + stock + "\'"); } txtarea.value = "Stock = " + stock + " Price = " + price + " Quantity = " + sellshares + " Total = " + total + " Balance = " + balance; } } } } } db.close (); } } return true; } // Infile, Outfile: prog1.out Initial Input from File: insert into Stocks values ('IBM', 10.00, 100000); insert into Stocks values ('APPLE', 20.00, 100000); insert into Stocks values ('LINUX', 30.00, 100000); Input and Output from Screen: Input: Add Investors ('PACMAN', 'Anthony F. Ortiz', 'AFO2871@AOL.COM'). Output: NInvestor is ok: 1. Input: Login with 1, 'PACMAN'. Output: Login is ok: 1. Input: Logout. Output: Logout is ok: 1. Input: Holdings, Orders, Transactions Output: AcctN Symb Qty ----------- ----- ----------- OrderN AcctN Symb TStamp Action Qty Price ----------- ----------- ----- -------------- -------- ----------- ---------- TransN AcctN Symb Action Qty Price ----------- ----------- ----- -------- ----------- ---------- Input: IBM Output: Symb Price Vol ----- ---------- ----------- IBM 10.00 100000 Input: Deposit 1000 Output: Deposit = 1000 New Balance = 1000 Input: Withdrawl 1 Output: Withdrawl = 1 New Balance = 999 Input: Buy (IBM, 10.00, 10) Output: Stock = IBM Price = 10 Quantity = 10 Total = 100 Balance = 899 Input: Sell (IBM, 10.00, 5) Output: Stock = IBM Price = 10 Quantity = 5 Total = 50 Balance = 949 Input: Holdings, Orders, Transactions, Stocks Output: AcctN Symb Qty ----------- ----- ----------- 1 IBM 5 OrderN AcctN Symb TStamp Action Qty Price ----------- ----------- ----- -------------- -------- ----------- ---------- 1 1 IBM 20000302014509 BUY 10 10.00 2 1 IBM 20000302014544 SELL 5 10.00 TransN AcctN Symb Action Qty Price ----------- ----------- ----- -------- ----------- ---------- 1 1 ~~~ DEPOSIT 0 1000.00 2 1 ~~~ WITHDRAW 0 1.00 3 1 IBM BOUGHT 10 10.00 4 1 IBM SOLD 5 10.00 Symb Price Vol ----- ---------- ----------- IBM 10.00 99995
BACK TO CS6660 PAGE.