与不从订单清单中删除书籍和更新订单有关的订单处理问题

时间:2019-06-14 16:13:11

标签: java html forms jsp servlets

我在计算有序列表及其副本的书号时遇到问题。

当我从书单中添加任何书的数量时,由于将书号和价格添加到具有其功能的现有书中,我得到了正确的总价结果。但是,删除过程无法完成。

当我从订购清单中删除任何一本书并编辑订单时,我得到的书号和计算总价的结果是错误的。

我检查了订单明细,删除的书在订单清单中有效,但是我无法将其价格加到总价中。

我的订单清单

1数量-10 $

2数量-30 $(我的附加费)

状态处理

总计40 $,总计书籍3的所有副本 在删除过程和编辑过程之后,

1数量-10 $

2数量-30 $(我的附加费)

状态已交付

总计10 $,总计书籍3的所有副本。 当我将交货状态更新为任何选定的项目时,

1数量-10 $

2数量-30 $(我的附加费)

状态处理

总计40 $,总计书籍3的所有副本 我认为从BookOrder删除图书项目并更新BookOrder会有问题。

我该如何解决?

<c:if test="${bookOrder != null}">
                <h2 class="sub-header">
                    Sipariş #
                    <c:out value="${bookOrder.id}" />
                </h2>

                <div class="table-responsive">
                    <h2 class="sub-header">Sipariş Bilgisi</h2>
                    <form action="order_process" action="GET" class="form-inline">

                        <input id="orderId" name="orderId" type=hidden
                            value="${bookOrder.id}">

                        <table class="table table-striped">
                            <tr>
                                <th>Sipariş Veren</th>
                                <td><c:out value="${bookOrder.customerOrders.name}" /> <c:out
                                        value="${bookOrder.customerOrders.surname}" /></td>
                            <tr>
                                <th>Kitap Adeti</th>
                                <td><c:out value="${bookOrder.allBookCopies}" /></td>
                            <tr>
                                <th>Toplam Fiyat</th>
                                <td><i class="fa fa-turkish-lira"></i> <fmt:setLocale
                                        value="tr_TR" /> <fmt:formatNumber
                                        value="${bookOrder.orderTotal}" minFractionDigits="2" /></td>
                            </tr>
                            <tr>
                                <th>Alıcı (Ad Soyad)</th>
                                <td><input type="text" class="form-control"
                                    id="recipientNameText" name="recipientName"
                                    value="${bookOrder.recipentName}" size="45" /></td>
                            </tr>
                            <tr>
                                <th>Alıcı Tel No</th>
                                <td><input type="text" class="form-control"
                                    id="recipientPhoneText" name="recipientPhone"
                                    value="${bookOrder.recipentPhone}" size="45" /></td>
                            </tr>
                            <tr>
                                <th>Teslimat Adres</th>
                                <td><input type="text" class="form-control"
                                    id="shippingAddressText" name="shippingAddress"
                                    value="${bookOrder.shippingAddress}" size="45" /></td>
                            </tr>
                            <tr>
                                <th>Ödeme Durumu</th>
                                <td><select name="paymentMethod" class="form-control">
                                        <c:if test="${bookOrder.paymentMethod !=null}">
                                            <option value="${bookOrder.paymentMethod}">${bookOrder.paymentMethod}</option>
                                        </c:if>
                                </select></td>
                            </tr>
                            <tr>
                                <th>Sipariş Durum</th>
                                <td><select name="orderStatus" class="form-control">
                                        <option value="İşleniyor"
                                            <c:if test="${bookOrder.orderStatus eq 'İşleniyor' }">selected='selected'</c:if>>İşleniyor</option>
                                        <option value="Shipping"
                                            <c:if test="${bookOrder.orderStatus eq 'Shipping' }">selected='selected'</c:if>>Shipping</option>
                                        <option value="Delivered"
                                            <c:if test="${bookOrder.orderStatus eq 'Delivered' }">selected='selected'</c:if>>Delivered</option>
                                        <option value="Completed"
                                            <c:if test="${bookOrder.orderStatus eq 'Completed' }">selected='selected'</c:if>>Completed</option>
                                        <option value="Cancelled"
                                            <c:if test="${bookOrder.orderStatus eq 'Cancelled' }">selected='selected'</c:if>>Cancelled</option>
                                </select></td>
                            </tr>
                            <tr>
                                <th>Sipariş Tarihi</th>
                                <td><c:out value="${bookOrder.orderDate}" /></td>
                            </tr>
                        </table>
                        <h2 class="sub-header">Sipariş Edilenler</h2>
                        <table class="table table-striped">
                            <thead>
                                <tr>
                                    <th>Index</th>
                                    <th>Kitap Adı</th>
                                    <th>Yazar</th>
                                    <th>Fiyat</th>
                                    <th>Miktar</th>
                                    <th>Adet Fiyat</th>
                                    <th>

                                        <button type="submit" name="button" class="btn btn-info"
                                            value="Ekle">Kitap Ekle</button>

                                    </th>
                                </tr>
                            </thead>
                            <tbody>
                                <c:forEach var="detailOrder" items="${bookOrder.orderDetails}"
                                    varStatus="status">
                                    <tr>
                                        <td><c:out value="${status.index + 1}" /></td>
                                        <td><c:out value="${detailOrder.book.title}" /></td>
                                        <td><c:out value="${detailOrder.book.author}" /></td>
                                        <td><i class="fa fa-turkish-lira"></i> <fmt:setLocale
                                                value="tr_TR" /> <fmt:formatNumber
                                                value="${detailOrder.book.price}" minFractionDigits="2" /></td>
                                        <td><input type="text" class="form-control"
                                            name="quantity${status.index + 1}"
                                            value="${detailOrder.quantity}" readonly size="5" /></td>
                                        <td><i class="fa fa-turkish-lira"></i> <fmt:setLocale
                                                value="tr_TR" /> <fmt:formatNumber
                                                value="${detailOrder.subTotal}" minFractionDigits="2" /></td>
                                        <td><input id="price" type="hidden" name="price"
                                            value="${detailOrder.book.price}" /> <input id="bookId"
                                            name="bookId" type=hidden value="${detailOrder.book.id}">
                                            <button type="submit" name="button" class="btn btn-danger"
                                                value="Delete">Delete</button></td>
                                    </tr>
                                </c:forEach>
                                <tr>
                                    <td colspan="4" align="right">Toplam :</td>
                                    <td><c:out value="${bookOrder.allBookCopies}" /></td>
                                    <td><i class="fa fa-turkish-lira"></i> <fmt:setLocale
                                            value="tr_TR" /> <fmt:formatNumber
                                            value="${bookOrder.orderTotal}" minFractionDigits="2" /></td>
                                </tr>
                            </tbody>

                        </table>

                        <div class="row">

                            <div class="col-sm-12  col-md-3 ">
                                <button type="submit" name="button"
                                    class="btn btn-warning btn-block" value="Kaydet">Kaydet</button>
                            </div>

                            <div class="col-sm-12 col-md-3 ">
                                <button type="reset" id="buttonCancel" name="button"
                                    class="btn btn-danger btn-block" value="Cancel">İptal</button>
                            </div>

                        </div>

                    </form>
                </div>
            </c:if>

订购流程Servlet

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        System.out.println("OrderProcessServlet");

        String button = request.getParameter("button");

        if("Kaydet".equals(button)) {
            System.out.println("OrderProcessServlet | Kaydet button is clicked");
            saveBookToOrder(request, response);

        }else if("Cancel".equals(button)) {
            System.out.println("OrderProcessServlet | Cancel button is clicked");
            String page = "list_orders.jsp";    
            response.sendRedirect(page);

        }else if("Delete".equals(button)) {
            System.out.println("OrderProcessServlet | Delete is working");
            delete(request, response);
        }else if("Ekle".equals(button)) {
            System.out.println("OrderProcessServlet | Ekle is working");
            addBookToOrderList(request, response);
        }
    }


    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

    private void saveBookToOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        System.out.println("OrderProcessServlet | saveBookToOrder is working");

        int orderId = Integer.parseInt(request.getParameter("orderId"));


        String recipientName = request.getParameter("recipientName");
        String recipientPhone = request.getParameter("recipientPhone");
        String shippingAddress = request.getParameter("shippingAddress");

        System.out.println("SaveBookOrderServlet | recipientName : " + recipientName);
        System.out.println("SaveBookOrderServlet | recipientPhone : " + recipientPhone);
        System.out.println("SaveBookOrderServlet | shippingAddress : " + shippingAddress);

        StringBuilder errorMessage = new StringBuilder();


        String errorMessageText = showErrorMessage(recipientName, recipientPhone, shippingAddress, errorMessage);
        System.out.println("OrderProcessServlet | Before if-else statement | errorMessageText : " + errorMessageText);
        if(!errorMessageText.isEmpty()) {
            System.out.println("OrderProcessServlet | !errorMessageText.isEmpty() | errorMessageText : " + errorMessageText);
            CommonUtility.errorMessage(errorMessageText, request);

            BookOrdersService bookOrdersService = new BookOrdersService();
            BookOrders bookOrder = bookOrdersService.getBookOrdersById(orderId);

            request.setAttribute("bookOrder", bookOrder);


            String resultPage = "order_form.jsp";
            RequestDispatcher dispatcher = request.getRequestDispatcher(resultPage);
            dispatcher.forward(request, response);

        }else {

            System.out.println("OrderProcessServlet | errorMessageText.isEmpty() | errorMessageText : " + errorMessageText);

            BookOrdersService bookOrdersService = new BookOrdersService();
            BookOrders bookOrder = bookOrdersService.getBookOrdersById(orderId);

            String paymentMethod = request.getParameter("paymentMethod");
            String orderStatus = request.getParameter("orderStatus");

            System.out.println("OrderProcessServlet | paymentMethod : " + paymentMethod);

            System.out.println("OrderProcessServlet | orderStatus : " + orderStatus);

            bookOrder.setRecipentName(recipientName);
            bookOrder.setRecipentPhone(recipientPhone);
            bookOrder.setShippingAddress(shippingAddress);
            bookOrder.setPaymentMethod(paymentMethod);
            bookOrder.setOrderStatus(orderStatus);

            String[] arrayBookId = request.getParameterValues("bookId");
            String[] arrayPrice = request.getParameterValues("price");
            String[] arrayQuantity = new String[arrayBookId.length];


            for (int i = 1; i <= arrayQuantity.length; i++) {
                arrayQuantity[i - 1] = request.getParameter("quantity" + i);
            }

            Set<DetailOrder> orderDetails = bookOrder.getOrderDetails();
            orderDetails.clear();

            float totalAmount = 0.0f;

            for (int i = 0; i < arrayBookId.length; i++) {
                int bookId = Integer.parseInt(arrayBookId[i]);
                int quantity = Integer.parseInt(arrayQuantity[i]);
                float price = Float.parseFloat(arrayPrice[i]);

                float subtotal = price * quantity;

                DetailOrder orderDetail = new DetailOrder();
                orderDetail.setBook(new Book(bookId));
                orderDetail.setQuantity(quantity);
                orderDetail.setSubTotal(subtotal);
                orderDetail.setOrders(bookOrder);

                orderDetails.add(orderDetail);

                totalAmount += subtotal;
            }

            bookOrder.setOrderTotal(totalAmount);

            System.out.println("------------------------------------------------------------");
            System.out.println(bookOrder.getId() + "# RecipentName : " + bookOrder.getRecipentName());
            System.out.println(bookOrder.getId() + "# RecipentPhone : " + bookOrder.getRecipentPhone());
            System.out.println(bookOrder.getId() + "# ShippingAddress : " + bookOrder.getShippingAddress());
            System.out.println(bookOrder.getId() + "# OrderStatus : " + bookOrder.getOrderStatus());
            System.out.println(bookOrder.getId() + "# PaymentMethod : " + bookOrder.getPaymentMethod());
            System.out.println(bookOrder.getId() + "# AllBookCopies : " + bookOrder.getAllBookCopies());            
            for (DetailOrder detail : bookOrder.getOrderDetails()) {
                Book book = detail.getBook();
                int quantity = detail.getQuantity();
                float subtotal = detail.getSubTotal();
                System.out.println("\tBook Name : " + book.getTitle() 
                                   + "| Book Quantity : " + quantity + " | Sub Total : " + subtotal);
                System.out.println("----------------------------------------------------------------------------");
            }           
            System.out.println("----------------------------------------------------------------");

            bookOrdersService.updateBookOrder(bookOrder);

            String message = "Order #  " + bookOrder.getId() + " Updated.";

            CommonUtility.successMessage(message, request);

            List<BookOrders> bookOrdersList = bookOrdersService.listBookOrders();


            // set attribute for usersList in request
            request.setAttribute("bookOrdersList", bookOrdersList);

            String page = "list_orders.jsp";

            RequestDispatcher dispatcher = request.getRequestDispatcher(page);
            dispatcher.forward(request, response);

        }



    }

    private String showErrorMessage(String recipientName, String recipientPhone, String shippingAddress,
            StringBuilder errorMessage) {
        System.out.println("OrderProcessServlet | showErrorMessage function is working");

        if(recipientName.equals("")) {
            System.out.println("OrderProcessServlet | recipientName is empty");
            String message = "Alıcı Alanı Boş Bırakılmış. ";
            errorMessage.append(message);
        }

        if(recipientPhone.equals("")) {
            System.out.println("OrderProcessServlet | recipientName is empty");
            String message = "Telefon Alanı Boş Bırakılmış. ";
            errorMessage.append(message);
        }

        if(shippingAddress.equals("")) {
            System.out.println("OrderProcessServlet | recipientName is empty");
            String message = "Teslimat Adresi Alanı Boş Bırakılmış. ";
            errorMessage.append(message);
        }

        String errorMessageText = errorMessage.toString();

        System.out.println("OrderProcessServlet | showErrorMessage | errorMessageText : " + errorMessageText);

        return errorMessageText;
    }

    private void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        System.out.println("OrderProcessServlet | delete is working");

        int bookId = Integer.parseInt(request.getParameter("bookId"));
        int orderId = Integer.parseInt(request.getParameter("orderId"));

        BookOrdersService bookOrdersService = new BookOrdersService();
        BookOrders bookOrder = bookOrdersService.getBookOrdersById(orderId);

        if(bookOrder == null) {
            System.out.println("OrderProcessServlet | delete |  bookOrder is null");
        }

        Set<DetailOrder> orderDetails = bookOrder.getOrderDetails();
        Iterator<DetailOrder> iterator = orderDetails.iterator();

        Set<DetailOrder> newOrderDetails = null;

        while (iterator.hasNext()) {
            DetailOrder orderDetail = iterator.next();

            if (orderDetail.getBook().getId() == bookId) {
                System.out.println("OrderProcessServlet | delete | Book Title : " + orderDetail.getBook().getTitle() + " silindi.");
                float newTotal = bookOrder.getOrderTotal() - orderDetail.getSubTotal();
                bookOrder.setOrderTotal(newTotal);
                iterator.remove();
            }
        }



        request.setAttribute("bookOrder", bookOrder);

        String editOrderFormPage = "order_form.jsp";
        RequestDispatcher dispatcher = request.getRequestDispatcher(editOrderFormPage);
        dispatcher.forward(request, response);
    }

    private void addBookToOrderList(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        BookService bookService = new BookService();
        BookOrdersService bookOrdersService = new BookOrdersService();
        int orderId = Integer.parseInt(request.getParameter("orderId"));

        BookOrders bookOrder = bookOrdersService.getBookOrdersById(orderId);
        if(bookOrder !=null) {
            System.out.println("OrderProcessServlet | addBookToOrderList | bookOrder is not null");
        }

        List<Book> bookList = bookService.listBook();
        request.setAttribute("bookList", bookList);
        request.setAttribute("bookOrder",bookOrder);
        request.setAttribute("orderId",orderId);

        String page = "list_order_books.jsp";
        RequestDispatcher dispatcher = request.getRequestDispatcher(page);
        dispatcher.forward(request, response);
    }

0 个答案:

没有答案