import java.io.*; import java.sql.*; import javax.servlet.*; import javax.servlet.http.*; public class ProcessTrade extends HttpServlet { public static Connection conn; public static Statement stmt; public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String m_acctnum[] = request.getParameterValues("memberId"); String transType[] = request.getParameterValues("transType"); String quantity[] = request.getParameterValues("quantity"); String symbol[] = request.getParameterValues("symbol"); double pricePerShare = 0.0; double cash = 0.0; double quant = 0.0; double amount = 0.0; double commission = 0.0; out.println("Member Menu"); out.println(" "); out.println("
"); out.println("Please confirm Transaction requested

"); try { Class.forName("oracle.jdbc.driver.OracleDriver"); } catch(ClassNotFoundException e){ out.println("Error loading the Driver:"+e.getMessage()); return; } Connection conn = null; try { conn = DriverManager.getConnection ( "jdbc:oracle:thin:@tinman.cs.gsu.edu:1521:sid8i","book","book"); } catch (SQLException e1) { out.println("Error connecting to Oracle:"+e1.getMessage()); return; } if (conn == null) { out.println("Null Connection"); return; } Statement stmt = null; try { stmt = conn.createStatement (); } catch (SQLException e) { out.println("createStatement " + e.getMessage()); try {conn.close();} catch (SQLException e2) {}; return; } ResultSet rset = null; String cashQuery = "select cash_balance from member " + "where mid = '" + m_acctnum[0] + "'"; String priceQuery = "select current_price from security " + "where symbol = '" + symbol[0] + "'"; String quantityQuery = "select quantity from transaction " + "where symbol = '" + symbol[0] + "'" + "and mid = '" + m_acctnum[0] + "'"; try { rset = stmt.executeQuery(cashQuery); } catch (SQLException e) { out.println("executeQuery1 " + e.getMessage()); try {stmt.close(); conn.close();} catch (SQLException e2) {}; return; } try { rset.next(); cash = rset.getDouble(1); } catch (SQLException e) { } try { rset = stmt.executeQuery(priceQuery); } catch (SQLException e) { out.println("executeQuery2 " + e.getMessage()); try {stmt.close(); conn.close();} catch (SQLException e2) {}; return; } try { rset.next(); pricePerShare = rset.getDouble(1); } catch (SQLException e) { try {stmt.close(); conn.close();} catch (SQLException e2) {}; return; } commission = (pricePerShare * toDouble(quantity[0])) * 0.1; amount = pricePerShare * toDouble(quantity[0]); if (transType[0].equals("Buy")) { if (cash >= amount + commission) { out.println("Buy " + quantity[0] + " of " + symbol[0] + "at " + pricePerShare); } else { out.println("Current cash balance is not enough to buy amount specified!"); out.println("\n"); try {stmt.close(); conn.close();} catch (SQLException e2) {}; return; } } else { try { rset = stmt.executeQuery(quantityQuery); } catch (SQLException e) { out.println("executeQuery " + e.getMessage()); try {stmt.close(); conn.close();} catch (SQLException e2) {}; return; } try { rset.next(); quant = rset.getDouble(1); } catch (SQLException e) { out.println("You do not own any shares of " + symbol[0]); out.println("\n"); try {stmt.close(); conn.close();} catch (SQLException e2) {}; return; } if (quant >= toDouble(quantity[0])) { out.println("Sell " + quantity[0] + " of " + symbol[0]); } else { out.println("Quantity specified exceeds the amount in Portfolio"); out.println("\n"); try {stmt.close(); conn.close();} catch (SQLException e2) {}; return; } } out.println("

"); out.println(""); // out.println(""); out.println(""); out.println(""); out.println(""); out.println(""); out.println(""); out.println(""); out.println("
"); out.println("\n"); try { stmt.close(); conn.close(); } catch (SQLException e) { out.println("executeQuery " + e.getMessage()); return; } out.close(); } static String twoDigit(double f) { boolean neg = false; if (f < 0.0) { neg = true; f = -f; } long dollars = (long) f; int cents = (int) ((f - dollars) * 100); String result; if (cents <= 9) result = dollars + ".0" + cents; else result = dollars + "." + cents; if (neg) return "-" + result; else return result; } private static double toDouble(String s) { double d = 0.0d; try { d = Double.valueOf(s).doubleValue(); } catch (NumberFormatException e) { System.err.println("Error in Convert.toDouble: " + "argument is not a number"); System.exit(1); } return d; } public String getServletInfo() { return "This Servlet processes view Portfolio "; } }