test

Форк
0
Форк от lirfrnk/test
/
ReceiptServlet.java 
88 строк · 4.0 Кб
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

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

20
public class ReceiptServlet extends HttpServlet {
21
    BookService bookService = new BookServiceImpl();
22

23
    //NOT_IN_USED
24
    public void service(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
25
        PrintWriter pw = res.getWriter();
26
        res.setContentType(BookStoreConstants.CONTENT_TYPE_TEXT_HTML);
27
        if (!StoreUtil.isLoggedIn(UserRole.CUSTOMER, req.getSession())) {
28
            RequestDispatcher rd = req.getRequestDispatcher("CustomerLogin.html");
29
            rd.include(req, res);
30
            pw.println("<table class=\"tab\"><tr><td>Please Login First to Continue!!</td></tr></table>");
31
            return;
32
        }
33
        try {
34
            List<Book> books = bookService.getAllBooks();
35
            int i = 0;
36
            RequestDispatcher rd = req.getRequestDispatcher("CustomerHome.html");
37
            rd.include(req, res);
38
            StoreUtil.setActiveTab(pw, "cart");
39
            pw.println("<div class=\"tab\">Your order status is as below</div>");
40
            pw.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>");
46
            double total = 0.0;
47
            for (Book book : books) {
48
                double bPrice = book.getPrice();
49
                String bCode = book.getBarcode();
50
                String bName = book.getName();
51
                String bAuthor = book.getAuthor();
52
                int bQty = book.getQuantity();
53
                i = i + 1;
54

55
                String qt = "qty" + Integer.toString(i);
56
                int quantity = Integer.parseInt(req.getParameter(qt));
57
                try {
58
                    String check1 = "checked" + Integer.toString(i);
59
                    String getChecked = req.getParameter(check1);
60
                    if (bQty < quantity) {
61
                        pw.println(
62
                                "</table><div class=\"tab\" style='color:red;'>Please Select the Qty less than Available Books Quantity</div>");
63
                        break;
64
                    }
65

66
                    if (getChecked.equals("pay")) {
67
                        pw.println("<tr><td>" + bCode + "</td>");
68
                        pw.println("<td>" + bName + "</td>");
69
                        pw.println("<td>" + bAuthor + "</td>");
70
                        pw.println("<td>" + bPrice + "</td>");
71
                        pw.println("<td>" + quantity + "</td>");
72
                        double amount = bPrice * quantity;
73
                        total = total + amount;
74
                        pw.println("<td>" + amount + "</td></tr>");
75
                        bQty = bQty - quantity;
76
                        System.out.println(bQty);
77
                        bookService.updateBookQtyById(bCode, bQty);
78
                    }
79
                } catch (Exception e) {
80
                }
81
            }
82
            pw.println("</table><br/><div class='tab'>Total Paid Amount: " + total + "</div>");
83
//            String fPay = req.getParameter("f_pay");
84
        } catch (Exception e) {
85
            e.printStackTrace();
86
        }
87
    }
88
}
89

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

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

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

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