import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ConfirmTrade 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");
String pricePerShare[] = request.getParameterValues("pricePerShare");;
double cash = 0.0;
double quant = 0.0;
double amount = 0.0;
double BuyAmount = 0.0;
double SellAmount = 0.0;
double commission = 0.0;
commission = (toDouble(pricePerShare[0]) * toDouble(quantity[0])) * 0.1;
amount = toDouble(pricePerShare[0]) * toDouble(quantity[0]);
BuyAmount = commission + amount;
SellAmount = amount - commission;
out.println("
Member Menu");
out.println(" ");
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 buyStock = "insert into transaction values ('" +
m_acctnum[0] + "', '" + symbol[0] +
"', sysdate, 'buy', " + quantity[0] + ", " +
pricePerShare[0] + ", " + commission + ", "
+ BuyAmount + ")";
String sellStock = "insert into transaction values('" +
m_acctnum[0] + "', '" + symbol[0] +
"', sysdate, 'sell', " +
quantity[0] + ", " + pricePerShare[0] +
", " + commission + ", " + SellAmount + ") ";
if (transType[0].equals("Buy")) {
try {
stmt.executeUpdate(buyStock);
out.println("Transaction successful!");
} catch (SQLException e) {
out.println(buyStock+"
");
out.println("updateQuery(Buy) " + e.getMessage());
return;
}
} else {
try {
stmt.executeUpdate(sellStock);
out.println("Transaction successful!");
} catch (SQLException e) {
out.println("updateQuery (Sell) " + e.getMessage());
return;
}
}
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 ";
}
}