test
Форк от lirfrnk/test
1package servlets;
2
3import java.io.IOException;
4import java.io.PrintWriter;
5import java.util.ArrayList;
6import java.util.List;
7
8import javax.servlet.RequestDispatcher;
9import javax.servlet.ServletException;
10import javax.servlet.http.HttpServlet;
11import javax.servlet.http.HttpServletRequest;
12import javax.servlet.http.HttpServletResponse;
13import javax.servlet.http.HttpSession;
14
15import com.bittercode.constant.BookStoreConstants;
16import com.bittercode.model.Book;
17import com.bittercode.model.Cart;
18import com.bittercode.model.UserRole;
19import com.bittercode.service.BookService;
20import com.bittercode.service.impl.BookServiceImpl;
21import com.bittercode.util.StoreUtil;
22
23public class CartServlet extends HttpServlet {
24
25BookService bookService = new BookServiceImpl();
26
27public void service(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
28PrintWriter pw = res.getWriter();
29res.setContentType(BookStoreConstants.CONTENT_TYPE_TEXT_HTML);
30
31// Check if Customer is logged In
32if (!StoreUtil.isLoggedIn(UserRole.CUSTOMER, req.getSession())) {
33RequestDispatcher rd = req.getRequestDispatcher("CustomerLogin.html");
34rd.include(req, res);
35pw.println("<table class=\"tab\"><tr><td>Please Login First to Continue!!</td></tr></table>");
36return;
37}
38try {
39// Add/Remove Item from the cart if requested
40// store the comma separated bookIds of cart in the session
41StoreUtil.updateCartItems(req);
42
43HttpSession session = req.getSession();
44String bookIds = "";
45if (session.getAttribute("items") != null)
46bookIds = (String) session.getAttribute("items");// read comma separated bookIds from session
47
48RequestDispatcher rd = req.getRequestDispatcher("CustomerHome.html");
49rd.include(req, res);
50
51// Set the active tab as cart
52StoreUtil.setActiveTab(pw, "cart");
53
54// Read the books from the database with the respective bookIds
55List<Book> books = bookService.getBooksByCommaSeperatedBookIds(bookIds);
56List<Cart> cartItems = new ArrayList<Cart>();
57pw.println("<div id='topmid' style='background-color:grey'>Shopping Cart</div>");
58pw.println("<table class=\"table table-hover\" style='background-color:white'>\r\n"
59+ " <thead>\r\n"
60+ " <tr style='background-color:black; color:white;'>\r\n"
61+ " <th scope=\"col\">BookId</th>\r\n"
62+ " <th scope=\"col\">Name</th>\r\n"
63+ " <th scope=\"col\">Author</th>\r\n"
64+ " <th scope=\"col\">Price/Item</th>\r\n"
65+ " <th scope=\"col\">Quantity</th>\r\n"
66+ " <th scope=\"col\">Amount</th>\r\n"
67+ " </tr>\r\n"
68+ " </thead>\r\n"
69+ " <tbody>\r\n");
70double amountToPay = 0;
71if (books == null || books.size() == 0) {
72pw.println(" <tr style='background-color:green'>\r\n"
73+ " <th scope=\"row\" colspan='6' style='color:yellow; text-align:center;'> No Items In the Cart </th>\r\n"
74+ " </tr>\r\n");
75}
76for (Book book : books) {
77int qty = (int) session.getAttribute("qty_" + book.getBarcode());
78Cart cart = new Cart(book, qty);
79cartItems.add(cart);
80amountToPay += (qty * book.getPrice());
81pw.println(getRowData(cart));
82}
83
84// set cartItems and amountToPay in the session
85session.setAttribute("cartItems", cartItems);
86session.setAttribute("amountToPay", amountToPay);
87
88if (amountToPay > 0) {
89pw.println(" <tr style='background-color:green'>\r\n"
90+ " <th scope=\"row\" colspan='5' style='color:yellow; text-align:center;'> Total Amount To Pay </th>\r\n"
91+ " <td colspan='1' style='color:white; font-weight:bold'><span>₹</span> "
92+ amountToPay
93+ "</td>\r\n"
94+ " </tr>\r\n");
95}
96pw.println(" </tbody>\r\n"
97+ "</table>");
98if (amountToPay > 0) {
99pw.println("<div style='text-align:right; margin-right:20px;'>\r\n"
100+ "<form action=\"checkout\" method=\"post\">"
101+ "<input type='submit' class=\"btn btn-primary\" name='pay' value='Proceed to Pay ₹ "
102+ amountToPay + "'/></form>"
103+ " </div>");
104}
105} catch (Exception e) {
106e.printStackTrace();
107}
108}
109
110public String getRowData(Cart cart) {
111Book book = cart.getBook();
112return " <tr>\r\n"
113+ " <th scope=\"row\">" + book.getBarcode() + "</th>\r\n"
114+ " <td>" + book.getName() + "</td>\r\n"
115+ " <td>" + book.getAuthor() + "</td>\r\n"
116+ " <td><span>₹</span> " + book.getPrice() + "</td>\r\n"
117+ " <td><form method='post' action='cart'><button type='submit' name='removeFromCart' class=\"glyphicon glyphicon-minus btn btn-danger\"></button> "
118+ "<input type='hidden' name='selectedBookId' value='" + book.getBarcode() + "'/>"
119+ cart.getQuantity()
120+ " <button type='submit' name='addToCart' class=\"glyphicon glyphicon-plus btn btn-success\"></button></form></td>\r\n"
121+ " <td><span>₹</span> " + (book.getPrice() * cart.getQuantity()) + "</td>\r\n"
122+ " </tr>\r\n";
123}
124
125}
126