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;
12
13import com.bittercode.constant.BookStoreConstants;
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 ReceiptServlet extends HttpServlet {
21BookService bookService = new BookServiceImpl();
22
23//NOT_IN_USED
24public void service(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
25PrintWriter pw = res.getWriter();
26res.setContentType(BookStoreConstants.CONTENT_TYPE_TEXT_HTML);
27if (!StoreUtil.isLoggedIn(UserRole.CUSTOMER, req.getSession())) {
28RequestDispatcher rd = req.getRequestDispatcher("CustomerLogin.html");
29rd.include(req, res);
30pw.println("<table class=\"tab\"><tr><td>Please Login First to Continue!!</td></tr></table>");
31return;
32}
33try {
34List<Book> books = bookService.getAllBooks();
35int i = 0;
36RequestDispatcher rd = req.getRequestDispatcher("CustomerHome.html");
37rd.include(req, res);
38StoreUtil.setActiveTab(pw, "cart");
39pw.println("<div class=\"tab\">Your order status is as below</div>");
40pw.println(
41"<div class=\"tab\">\r\n" + " <table>\r\n" + " <tr>\r\n" + " \r\n"
42+ " <th>Book Code</th>\r\n" + " <th>Book Name</th>\r\n"
43+ " <th>Book Author</th>\r\n" + " <th>Book Price</th>\r\n"
44+ " <th>Quantity</th><br/>\r\n" + " <th>Amount</th><br/>\r\n"
45+ " </tr>");
46double total = 0.0;
47for (Book book : books) {
48double bPrice = book.getPrice();
49String bCode = book.getBarcode();
50String bName = book.getName();
51String bAuthor = book.getAuthor();
52int bQty = book.getQuantity();
53i = i + 1;
54
55String qt = "qty" + Integer.toString(i);
56int quantity = Integer.parseInt(req.getParameter(qt));
57try {
58String check1 = "checked" + Integer.toString(i);
59String getChecked = req.getParameter(check1);
60if (bQty < quantity) {
61pw.println(
62"</table><div class=\"tab\" style='color:red;'>Please Select the Qty less than Available Books Quantity</div>");
63break;
64}
65
66if (getChecked.equals("pay")) {
67pw.println("<tr><td>" + bCode + "</td>");
68pw.println("<td>" + bName + "</td>");
69pw.println("<td>" + bAuthor + "</td>");
70pw.println("<td>" + bPrice + "</td>");
71pw.println("<td>" + quantity + "</td>");
72double amount = bPrice * quantity;
73total = total + amount;
74pw.println("<td>" + amount + "</td></tr>");
75bQty = bQty - quantity;
76System.out.println(bQty);
77bookService.updateBookQtyById(bCode, bQty);
78}
79} catch (Exception e) {
80}
81}
82pw.println("</table><br/><div class='tab'>Total Paid Amount: " + total + "</div>");
83// String fPay = req.getParameter("f_pay");
84} catch (Exception e) {
85e.printStackTrace();
86}
87}
88}
89