Ejemplo n.º 1
0
  private void add(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {

    try {
      BusinessServiceImpl service = new BusinessServiceImpl();
      Book book;
      int userID; // = Integer.parseInt(request.getParameter("userId"));
      String purchasedBooks = "";
      Date date = new Date();
      Double amount = 0.00;
      HttpSession session = request.getSession(true);
      ArrayList<Book> bookList = (ArrayList<Book>) session.getAttribute("bookList");
      User authenticatedUser = (User) session.getAttribute("authenticatedUser");
      userID = authenticatedUser.getUserID();
      if (bookList == null) {
        // if shopping cart is empty, cannot place order
        request.setAttribute("message", "Shopping cart is empty!");
      } else {

        // compute purchasedBooks and amount
        book = bookList.get(0);
        purchasedBooks = Integer.toString(book.getBookID());
        amount += book.getPrice();
        for (int i = 1; i < bookList.size(); i++) {
          book = bookList.get(i);
          purchasedBooks = "," + Integer.toString(bookList.get(i).getBookID());
          amount += book.getPrice();
        }
      }

      Order order = new Order();
      order.setUserID(userID);
      order.setDate(date);
      order.setAmount(amount);
      order.setPurchasedBooks(purchasedBooks);
      service.addOrder(order);
      request.setAttribute("message", "Place order successfully!");
    } catch (Exception e) {
      e.printStackTrace();
      request.setAttribute("message", "Place order failed!");
    }
    request.getRequestDispatcher("/message.jsp").forward(request, response);
  }