test
Форк от lirfrnk/test
1package servlets;
2
3import java.io.IOException;
4import java.io.PrintWriter;
5import java.util.List;
6
7import javax.servlet.RequestDispatcher;
8import javax.servlet.ServletException;
9import javax.servlet.http.HttpServlet;
10import javax.servlet.http.HttpServletRequest;
11import javax.servlet.http.HttpServletResponse;
12import javax.servlet.http.HttpSession;
13
14import com.bittercode.constant.BookStoreConstants;
15import com.bittercode.model.Book;
16import com.bittercode.model.Cart;
17import com.bittercode.model.UserRole;
18import com.bittercode.service.BookService;
19import com.bittercode.service.impl.BookServiceImpl;
20import com.bittercode.util.StoreUtil;
21
22public class ProcessPaymentServlet extends HttpServlet {
23
24BookService bookService = new BookServiceImpl();
25
26@SuppressWarnings("unchecked")
27public void service(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
28PrintWriter pw = res.getWriter();
29res.setContentType(BookStoreConstants.CONTENT_TYPE_TEXT_HTML);
30if (!StoreUtil.isLoggedIn(UserRole.CUSTOMER, req.getSession())) {
31RequestDispatcher rd = req.getRequestDispatcher("CustomerLogin.html");
32rd.include(req, res);
33pw.println("<table class=\"tab\"><tr><td>Please Login First to Continue!!</td></tr></table>");
34return;
35}
36try {
37
38RequestDispatcher rd = req.getRequestDispatcher("CustomerHome.html");
39rd.include(req, res);
40StoreUtil.setActiveTab(pw, "cart");
41pw.println("<div id='topmid' style='background-color:grey'>Your Orders</div>");
42pw.println("<div class=\"container\">\r\n"
43+ " <div class=\"card-columns\">");
44HttpSession session = req.getSession();
45List<Cart> cartItems = null;
46if (session.getAttribute("cartItems") != null)
47cartItems = (List<Cart>) session.getAttribute("cartItems");
48for (Cart cart : cartItems) {
49Book book = cart.getBook();
50double bPrice = book.getPrice();
51String bCode = book.getBarcode();
52String bName = book.getName();
53String bAuthor = book.getAuthor();
54int availableQty = book.getQuantity();
55int qtToBuy = cart.getQuantity();
56availableQty = availableQty - qtToBuy;
57bookService.updateBookQtyById(bCode, availableQty);
58pw.println(this.addBookToCard(bCode, bName, bAuthor, bPrice, availableQty));
59session.removeAttribute("qty_" + bCode);
60}
61session.removeAttribute("amountToPay");
62session.removeAttribute("cartItems");
63session.removeAttribute("items");
64session.removeAttribute("selectedBookId");
65pw.println("</div>\r\n"
66+ " </div>");
67} catch (Exception e) {
68e.printStackTrace();
69}
70}
71
72public String addBookToCard(String bCode, String bName, String bAuthor, double bPrice, int bQty) {
73String button = "<a href=\"#\" class=\"btn btn-info\">Order Placed</a>\r\n";
74return "<div class=\"card\">\r\n"
75+ " <div class=\"row card-body\">\r\n"
76+ " <img class=\"col-sm-6\" src=\"logo.png\" alt=\"Card image cap\">\r\n"
77+ " <div class=\"col-sm-6\">\r\n"
78+ " <h5 class=\"card-title text-success\">" + bName + "</h5>\r\n"
79+ " <p class=\"card-text\">\r\n"
80+ " Author: <span class=\"text-primary\" style=\"font-weight:bold;\"> " + bAuthor
81+ "</span><br>\r\n"
82+ " </p>\r\n"
83+ " \r\n"
84+ " </div>\r\n"
85+ " </div>\r\n"
86+ " <div class=\"row card-body\">\r\n"
87+ " <div class=\"col-sm-6\">\r\n"
88+ " <p class=\"card-text\">\r\n"
89+ " <span style='color:blue;'>Order Id: ORD" + bCode + "TM </span>\r\n"
90+ " <br><span class=\"text-danger\">Item Yet to be Delivered</span>\r\n"
91+ " </p>\r\n"
92+ " </div>\r\n"
93+ " <div class=\"col-sm-6\">\r\n"
94+ " <p class=\"card-text\">\r\n"
95+ " Amout Paid: <span style=\"font-weight:bold; color:green\"> ₹ " + bPrice
96+ " </span>\r\n"
97+ " </p>\r\n"
98+ button
99+ " </div>\r\n"
100+ " </div>\r\n"
101+ " </div>";
102}
103}
104