test

Форк
0
Форк от lirfrnk/test
/
ProcessPaymentServlet.java 
103 строки · 4.9 Кб
1
package servlets;
2

3
import java.io.IOException;
4
import java.io.PrintWriter;
5
import java.util.List;
6

7
import javax.servlet.RequestDispatcher;
8
import javax.servlet.ServletException;
9
import javax.servlet.http.HttpServlet;
10
import javax.servlet.http.HttpServletRequest;
11
import javax.servlet.http.HttpServletResponse;
12
import javax.servlet.http.HttpSession;
13

14
import com.bittercode.constant.BookStoreConstants;
15
import com.bittercode.model.Book;
16
import com.bittercode.model.Cart;
17
import com.bittercode.model.UserRole;
18
import com.bittercode.service.BookService;
19
import com.bittercode.service.impl.BookServiceImpl;
20
import com.bittercode.util.StoreUtil;
21

22
public class ProcessPaymentServlet extends HttpServlet {
23

24
    BookService bookService = new BookServiceImpl();
25

26
    @SuppressWarnings("unchecked")
27
    public void service(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
28
        PrintWriter pw = res.getWriter();
29
        res.setContentType(BookStoreConstants.CONTENT_TYPE_TEXT_HTML);
30
        if (!StoreUtil.isLoggedIn(UserRole.CUSTOMER, req.getSession())) {
31
            RequestDispatcher rd = req.getRequestDispatcher("CustomerLogin.html");
32
            rd.include(req, res);
33
            pw.println("<table class=\"tab\"><tr><td>Please Login First to Continue!!</td></tr></table>");
34
            return;
35
        }
36
        try {
37

38
            RequestDispatcher rd = req.getRequestDispatcher("CustomerHome.html");
39
            rd.include(req, res);
40
            StoreUtil.setActiveTab(pw, "cart");
41
            pw.println("<div id='topmid' style='background-color:grey'>Your Orders</div>");
42
            pw.println("<div class=\"container\">\r\n"
43
                    + "        <div class=\"card-columns\">");
44
            HttpSession session = req.getSession();
45
            List<Cart> cartItems = null;
46
            if (session.getAttribute("cartItems") != null)
47
                cartItems = (List<Cart>) session.getAttribute("cartItems");
48
            for (Cart cart : cartItems) {
49
                Book book = cart.getBook();
50
                double bPrice = book.getPrice();
51
                String bCode = book.getBarcode();
52
                String bName = book.getName();
53
                String bAuthor = book.getAuthor();
54
                int availableQty = book.getQuantity();
55
                int qtToBuy = cart.getQuantity();
56
                availableQty = availableQty - qtToBuy;
57
                bookService.updateBookQtyById(bCode, availableQty);
58
                pw.println(this.addBookToCard(bCode, bName, bAuthor, bPrice, availableQty));
59
                session.removeAttribute("qty_" + bCode);
60
            }
61
            session.removeAttribute("amountToPay");
62
            session.removeAttribute("cartItems");
63
            session.removeAttribute("items");
64
            session.removeAttribute("selectedBookId");
65
            pw.println("</div>\r\n"
66
                    + "    </div>");
67
        } catch (Exception e) {
68
            e.printStackTrace();
69
        }
70
    }
71

72
    public String addBookToCard(String bCode, String bName, String bAuthor, double bPrice, int bQty) {
73
        String button = "<a href=\"#\" class=\"btn btn-info\">Order Placed</a>\r\n";
74
        return "<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\"> &#8377; " + bPrice
96
                + " </span>\r\n"
97
                + "                        </p>\r\n"
98
                + button
99
                + "                    </div>\r\n"
100
                + "                </div>\r\n"
101
                + "            </div>";
102
    }
103
}
104

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.