Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package servlet; import bean.User; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.*; import javax.servlet.annotation.WebServlet; import javax.servlet.http.*; import java.sql.*; import java.util.logging.Level; import java.util.logging.Logger; import org.json.*; /** * * @author user */ @WebServlet(name = "CustomerControl", urlPatterns = { "/CustomerControl" }) public class CustomerControl extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String action = request.getParameter("action"); if (action.equals("buyTicket")) { this.doPurchaseTicket(request, response); } else if (action.equals("listTickets")) { this.doListTickets(request, response); } else if (action.equals("refundTicket")) { this.doRefundTicket(request, response); } } protected void doPurchaseTicket(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); HttpSession session = request.getSession(); String req_msID = request.getParameter("msID"); String req_seatIDs = request.getParameter("seatID"); int msID = Integer.parseInt(req_msID); User user = (User) session.getAttribute("user"); int userID; int userCredit = 0; int userLP = 0; if (user != null) { userID = user.getUserID(); userCredit = user.getCredit(); userLP = user.getTradePoint(); } else { out.println("require login"); return; } ServletContext sc = getServletContext(); String db_driver = sc.getInitParameter("db_driver"), db_url = sc.getInitParameter("db_url"), db_user = sc.getInitParameter("db_user"), db_password = sc.getInitParameter("db_password"); String db_q_chk_credit = "SELECT SUM(s.surcharge + ms.price)" + " FROM Seat s, House h, MovieSession ms" + " WHERE s.houseID = h.houseID" + " AND h.houseID = ms.houseID" + " AND ms.msID = ?" + " AND s.seatID IN (" + req_seatIDs + ");"; String db_q_update_credit = "UPDATE \"User\" SET credit = credit - ?, tradePoint = tradePoint + ? WHERE userID = ?;"; String db_q_update_loyaltyPoints = "UPDATE \"User\" SET tradePoint = tradePoint - ? WHERE userID = ?;"; String db_q_check = "SELECT t.* FROM Ticket t WHERE t.userID = ? AND t.msID = ? AND t.seatID = ?;"; String db_q_insert = "INSERT INTO Ticket (\"userID\", \"msID\", \"seatID\", \"state\") VALUES (?, ?, ?, 'purchased');"; String db_q_insert_2 = "INSERT INTO Ticket (\"userID\", \"msID\", \"seatID\", \"state\") VALUES (?, ?, ?, 'redeemed');"; String db_q_update = "UPDATE Ticket SET state = 'refunded' WHERE ticketID = ?;"; String paymentMethod = request.getParameter("paymentMethod"); try { Class.forName(db_driver); Connection conn = DriverManager.getConnection(db_url, db_user, db_password); PreparedStatement statmt1 = conn.prepareStatement(db_q_chk_credit); statmt1.setInt(1, msID); if (statmt1.execute()) { ResultSet rs1 = statmt1.getResultSet(); if (rs1.next()) { int totalAmount = rs1.getInt(1); if (paymentMethod.equals("Cash")) { if (totalAmount < userCredit) { // purchase String[] seatIDs = req_seatIDs.split(","); for (int i = 0; i < seatIDs.length; i++) { PreparedStatement statmt2 = conn.prepareStatement(db_q_check); int seatID = Integer.parseInt(seatIDs[i]); statmt2.setInt(1, userID); statmt2.setInt(2, msID); statmt2.setInt(3, seatID); statmt2.execute(); ResultSet rs2 = statmt2.getResultSet(); boolean hasNext = rs2.next(); if (!hasNext || (hasNext && rs2.getString("state").equals("refunded"))) { PreparedStatement statmt3 = conn.prepareStatement(db_q_insert); statmt3.setInt(1, userID); statmt3.setInt(2, msID); statmt3.setInt(3, seatID); statmt3.executeUpdate(); } else { out.println("Seat is reserved"); } } PreparedStatement statmt4 = conn.prepareStatement(db_q_update_credit); statmt4.setInt(1, totalAmount); statmt4.setInt(2, totalAmount / 10); statmt4.setInt(3, userID); statmt4.executeUpdate(); out.println("true"); } else { out.println("Your credit is not enough!"); } } else if (paymentMethod.equals("Loyalty Point")) { // pay by loyalty points if (totalAmount < userLP) { // purchase String[] seatIDs = req_seatIDs.split(","); for (int i = 0; i < seatIDs.length; i++) { PreparedStatement statmt2 = conn.prepareStatement(db_q_check); int seatID = Integer.parseInt(seatIDs[i]); statmt2.setInt(1, userID); statmt2.setInt(2, msID); statmt2.setInt(3, seatID); statmt2.execute(); ResultSet rs2 = statmt2.getResultSet(); boolean hasNext = rs2.next(); if (!hasNext || (hasNext && rs2.getString("state").equals("refunded"))) { PreparedStatement statmt3 = conn.prepareStatement(db_q_insert_2); statmt3.setInt(1, userID); statmt3.setInt(2, msID); statmt3.setInt(3, seatID); statmt3.executeUpdate(); } else { out.println("Seat is reserved"); } } PreparedStatement statmt4 = conn.prepareStatement(db_q_update_loyaltyPoints); statmt4.setInt(1, totalAmount); statmt4.setInt(2, userID); statmt4.executeUpdate(); out.println("true"); } else { out.println("Your loyalty points is not enough!"); } } } } conn.close(); } catch (SQLException ex) { Logger.getLogger(CustomerControl.class.getName()).log(Level.SEVERE, null, ex); } catch (ClassNotFoundException ex) { Logger.getLogger(CustomerControl.class.getName()).log(Level.SEVERE, null, ex); } } protected void doListTickets(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); HttpSession session = request.getSession(); JSONObject jso0 = new JSONObject(); JSONArray jsa0 = new JSONArray(); try { jso0.put("tickets", jsa0); } catch (JSONException ex) { Logger.getLogger(CustomerControl.class.getName()).log(Level.SEVERE, null, ex); } User user = (User) session.getAttribute("user"); int userID; int userCredit = 0; int userLP = 0; if (user != null) { userID = user.getUserID(); userCredit = user.getCredit(); userLP = user.getTradePoint(); } else { out.println(jso0.toString()); return; } ServletContext sc = getServletContext(); String db_driver = sc.getInitParameter("db_driver"), db_url = sc.getInitParameter("db_url"), db_user = sc.getInitParameter("db_user"), db_password = sc.getInitParameter("db_password"), db_q_tickets = "SELECT DISTINCT t.ticketID, t.state, s.rowName, s.seatName," + " (ms.price + s.surcharge) AS 'price', ms.playtime, m.*, h.houseName," + " c.cinemaName, c.cinemaDistrict, c.cinemaAddress, c.tel, c.image1" + " FROM Ticket t, MovieSession ms, Movie m, Seat s, House h, Cinema c" + " WHERE t.msID = ms.msID" + " AND ms.houseID = h.houseID" + " AND h.cinemaID = c.cinemaID" + " AND h.houseID = s.houseID" + " AND t.seatID = s.seatID" + " AND ms.movieID = m.movieID" + " AND t.userID = ?;"; try { Class.forName(db_driver); Connection conn = DriverManager.getConnection(db_url, db_user, db_password); PreparedStatement statmt = conn.prepareStatement(db_q_tickets); statmt.setInt(1, userID); if (statmt.execute()) { ResultSet rs = statmt.getResultSet(); ResultSetMetaData rsmd = rs.getMetaData(); int numOfColumns = rsmd.getColumnCount(); while (rs.next()) { JSONObject jso1 = new JSONObject(); jsa0.put(jso1); for (int i = 1; i <= numOfColumns; i++) { jso1.put(rsmd.getColumnLabel(i), rs.getString(i)); } String playtime = jso1.getString("playtime"); jso1.put("date", playtime.substring(0, 10)); jso1.put("time", playtime.subSequence(11, 16)); } } conn.close(); out.println(jso0.toString()); } catch (ClassNotFoundException ex) { Logger.getLogger(CustomerControl.class.getName()).log(Level.SEVERE, null, ex); } catch (SQLException ex) { Logger.getLogger(CustomerControl.class.getName()).log(Level.SEVERE, null, ex); } catch (JSONException ex) { Logger.getLogger(CustomerControl.class.getName()).log(Level.SEVERE, null, ex); } } protected void doRefundTicket(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); HttpSession session = request.getSession(); User user = (User) session.getAttribute("user"); int userID; int userCredit = 0; int userLP = 0; if (user != null) { userID = user.getUserID(); userCredit = user.getCredit(); userLP = user.getTradePoint(); } else { userID = 3; userCredit = 3000; userLP = 500; } ServletContext sc = getServletContext(); String db_driver = sc.getInitParameter("db_driver"), db_url = sc.getInitParameter("db_url"), db_user = sc.getInitParameter("db_user"), db_password = sc.getInitParameter("db_password"), db_q_update = "UPDATE Ticket SET state = 'request refundment' WHERE ticketID = ? AND state = 'purchased';", db_q_update2 = "UPDATE \"User\" SET credit = credit + ? WHERE userID = ?"; int ticketID = Integer.parseInt(request.getParameter("ticketID")); try { Class.forName(db_driver); Connection conn = DriverManager.getConnection(db_url, db_user, db_password); PreparedStatement statmt1 = conn.prepareStatement(db_q_update); statmt1.setInt(1, ticketID); statmt1.executeUpdate(); PreparedStatement statmt2 = conn.prepareStatement( "SELECT ms.price FROM Ticket t, MovieSession ms WHERE t.msID = ms.msID AND ticketID = ?;"); statmt2.setInt(1, ticketID); statmt2.execute(); ResultSet rs2 = statmt2.getResultSet(); PreparedStatement statmt3 = conn.prepareStatement(db_q_update2); rs2.next(); statmt3.setInt(1, rs2.getInt("price")); statmt3.setInt(2, userID); statmt3.executeUpdate(); this.doListTickets(request, response); } catch (ClassNotFoundException ex) { Logger.getLogger(CustomerControl.class.getName()).log(Level.SEVERE, null, ex); } catch (SQLException ex) { Logger.getLogger(CustomerControl.class.getName()).log(Level.SEVERE, null, ex); } } }