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.model.Book;
15import com.bittercode.model.UserRole;
16import com.bittercode.service.BookService;
17import com.bittercode.service.impl.BookServiceImpl;
18import com.bittercode.util.StoreUtil;
19
20public class ViewBookServlet extends HttpServlet {
21
22// book service for database operations and logics
23BookService bookService = new BookServiceImpl();
24
25public void service(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
26PrintWriter pw = res.getWriter();
27res.setContentType("text/html");
28
29// Check if the customer is logged in, or else return to login page
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
38// Read All available books from the database
39List<Book> books = bookService.getAllBooks();
40
41// Default Page to load data into
42RequestDispatcher rd = req.getRequestDispatcher("CustomerHome.html");
43rd.include(req, res);
44
45// Set Available Books tab as active
46StoreUtil.setActiveTab(pw, "books");
47
48// Show the heading for the page
49pw.println("<div id='topmid' style='background-color:grey'>Available Books"
50+ "<form action=\"cart\" method=\"post\" style='float:right; margin-right:20px'>"
51+ "<input type='submit' class=\"btn btn-primary\" name='cart' value='Proceed'/></form>"
52+ "</div>");
53pw.println("<div class=\"container\">\r\n"
54+ " <div class=\"card-columns\">");
55
56// Add or Remove items from the cart, if requested
57StoreUtil.updateCartItems(req);
58
59HttpSession session = req.getSession();
60for (Book book : books) {
61
62// Add each book to display as a card
63pw.println(this.addBookToCard(session, book));
64
65}
66
67// Checkout Button
68pw.println("</div>"
69+ "<div style='float:auto'><form action=\"cart\" method=\"post\">"
70+ "<input type='submit' class=\"btn btn-success\" name='cart' value='Proceed to Checkout'/></form>"
71+ " </div>");
72
73} catch (Exception e) {
74e.printStackTrace();
75}
76}
77
78public String addBookToCard(HttpSession session, Book book) {
79String bCode = book.getBarcode();
80int bQty = book.getQuantity();
81
82// Quantity of the current book added to the cart
83int cartItemQty = 0;
84if (session.getAttribute("qty_" + bCode) != null) {
85// Quantity of each book in the cart will be added in the session prefixed with
86// 'qty_' following with bookId
87cartItemQty = (int) session.getAttribute("qty_" + bCode);
88}
89
90// Button To Add/Remove item from the cart
91String button = "";
92if (bQty > 0) {
93// If no items in the cart, show add to cart button
94// If items is added to the cart, then show +, - button to add/remove more items
95button = "<form action=\"viewbook\" method=\"post\">"
96+ "<input type='hidden' name = 'selectedBookId' value = " + bCode + ">"
97+ "<input type='hidden' name='qty_" + bCode + "' value='1'/>"
98+ (cartItemQty == 0
99? "<input type='submit' class=\"btn btn-primary\" name='addToCart' value='Add To Cart'/></form>"
100: "<form method='post' action='cart'>"
101+ "<button type='submit' name='removeFromCart' class=\"glyphicon glyphicon-minus btn btn-danger\"></button> "
102+ "<input type='hidden' name='selectedBookId' value='" + bCode + "'/>"
103+ cartItemQty
104+ " <button type='submit' name='addToCart' class=\"glyphicon glyphicon-plus btn btn-success\"></button></form>")
105+ "";
106} else {
107// If available Quantity is zero, show out of stock button
108button = "<p class=\"btn btn-danger\">Out Of Stock</p>\r\n";
109}
110
111// Bootstrap card to show the book data
112return "<div class=\"card\">\r\n"
113+ " <div class=\"row card-body\">\r\n"
114+ " <img class=\"col-sm-6\" src=\"logo.png\" alt=\"Card image cap\">\r\n"
115+ " <div class=\"col-sm-6\">\r\n"
116+ " <h5 class=\"card-title text-success\">" + book.getName() + "</h5>\r\n"
117+ " <p class=\"card-text\">\r\n"
118+ " Author: <span class=\"text-primary\" style=\"font-weight:bold;\"> "
119+ book.getAuthor()
120+ "</span><br>\r\n"
121+ " </p>\r\n"
122+ " \r\n"
123+ " </div>\r\n"
124+ " </div>\r\n"
125+ " <div class=\"row card-body\">\r\n"
126+ " <div class=\"col-sm-6\">\r\n"
127+ " <p class=\"card-text\">\r\n"
128+ " <span>Id: " + bCode + "</span>\r\n"
129+ (bQty < 20 ? "<br><span class=\"text-danger\">Only " + bQty + " items left</span>\r\n"
130: "<br><span class=\"text-success\">Trending</span>\r\n")
131+ " </p>\r\n"
132+ " </div>\r\n"
133+ " <div class=\"col-sm-6\">\r\n"
134+ " <p class=\"card-text\">\r\n"
135+ " Price: <span style=\"font-weight:bold; color:green\"> ₹ "
136+ book.getPrice()
137+ " </span>\r\n"
138+ " </p>\r\n"
139+ button
140+ " </div>\r\n"
141+ " </div>\r\n"
142+ " </div>";
143}
144}
145