SHARE
TWEET

Untitled

a guest Mar 25th, 2019 44 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class buyTicket extends BaseServlet {
  2.  
  3.     @Override
  4.     protected void doGet(HttpServletRequest request, HttpServletResponse response)
  5.             throws ServletException, IOException {
  6.         String username = request.getParameter("username");
  7.         String matchIDStr = request.getParameter("matchID");
  8.         UserAccount user = getUserDao().getUserbyName(username);
  9.         int matchID = 0;
  10.  
  11.         try {
  12.             matchID = Integer.parseInt(matchIDStr);
  13.         } catch (NumberFormatException e) {
  14.             e.printStackTrace();
  15.         }
  16.  
  17.         int number_of_ticket_availble = 0;
  18.         ArrayList<Ticket> ticket = getTicketDao().getTicketByMatchID(matchID);
  19.         for (Ticket t : ticket) {
  20.             if (t.getIsAvailable() == 1) {
  21.                 number_of_ticket_availble++;
  22.             }
  23.  
  24.         }
  25.         Match match = getMatchDao().getMatchByID(matchID);
  26.  
  27.         request.setAttribute("match", match);
  28.         request.setAttribute("user", user);
  29.         request.setAttribute("ticket", number_of_ticket_availble);
  30.         forward(request, response, "/WEB-INF/views/buyTicket.jsp");;
  31.     }
  32.  
  33.     @Override
  34.     protected void doPost(HttpServletRequest request, HttpServletResponse response)
  35.             throws ServletException, IOException {
  36.         String username = request.getParameter("username");
  37.         String matchIDStr = request.getParameter("matchID");
  38.  
  39.         String amountStr = request.getParameter("amount");
  40.         int matchID = 0, amount = 0;
  41.         String message = "";
  42.         String status = "";
  43.         try {
  44.             matchID = Integer.parseInt(matchIDStr);
  45.             amount = Integer.parseInt(amountStr);
  46.         } catch (NumberFormatException e) {
  47.             e.printStackTrace();
  48.         }
  49.  
  50.         UserAccount user = getUserDao().getUserbyName(username);
  51.         float credit = user.getCredit(); // kiểm tra tiền của user
  52.  
  53.         // kiểm tra số vé
  54.         int number_of_ticket_availble = 0;
  55.         ArrayList<Ticket> ticket = getTicketDao().getTicketByMatchID(matchID);
  56.         for (Ticket t : ticket) {
  57.             if (t.getIsAvailable() == 1) {
  58.                 number_of_ticket_availble++;
  59.             }
  60.  
  61.         }
  62.  
  63.         //
  64.         float totalprice = 0;
  65.         Receipt r = new Receipt(user.getUserID(), totalprice, 0, false);
  66.         if (ticket != null && number_of_ticket_availble >= amount) { // nếu còn vé
  67.             float price = ticket.get(0).getCost();
  68.             totalprice = amount * price;
  69.             r.setTotalprice(totalprice);
  70.             r.setTotalticket(amount);
  71.  
  72.             //  user.setCredit(credit - totalprice);
  73.             //getUserDao().editCredit(user);
  74.             getTicketDao().updateTicket(matchID, amount);
  75.             getMatchDao().updateMatchTicket(matchID, amount);
  76.  
  77.             getReceiptDao().createReceipt(r);
  78.  
  79.             message += "Thanh toán thành công";
  80.  
  81.         } else {
  82.             // hết vé hoặc số vé mua lớn hơn số vé còn lại
  83.             message = " Không đủ vé để mua";
  84.             amount = 0;
  85.  
  86.         }
  87.  
  88.         request.setAttribute("user", user);
  89.         request.setAttribute("receipt", r);
  90.  
  91.         request.setAttribute("message", message);
  92.         forward(request, response, "/WEB-INF/views/orderstatus.jsp");
  93.     }
  94.  
  95.     @Override
  96.     public String getServletInfo() {
  97.         return "Short description";
  98.     }// </editor-fold>
  99.  
  100. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top