コード例 #1
0
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    Connection connection = MyConnection.getConnection();

    request.setCharacterEncoding("utf-8");

    BuyList buyList = new BuyList();
    Product product = null;
    User user = null;

    try {
      int buyquantity = Integer.valueOf(request.getParameter("quantity"));

      buyList.setUserId(UserLoginServlet.getLoginId(request, response));
      buyList.setProductId(Integer.valueOf(request.getParameter("id")));
      buyList.setProductNum(Integer.valueOf(request.getParameter("quantity")));
      buyList.setState(0);

      System.out.println(buyList.getUserId() + "" + buyList.getProductId());
      BuyListRepository.getInstance().addBuyList(connection, buyList);

      product =
          ProductRepository.getInstance()
              .FindById(connection, Integer.valueOf(request.getParameter("id")));
      product.setQuantity(product.getQuantity() - buyquantity);
      ProductRepository.getInstance().modifyProduct(connection, product);

      user =
          UserRepository.getInstance()
              .findById(connection, UserLoginServlet.getLoginId(request, response));
      user.setMoney(user.getMoney() - buyquantity * product.getPrice());
      UserRepository.getInstance().updateUser(connection, user);
    } catch (Exception e) {
      e.printStackTrace();
      MyConnection.connException(connection);
    }

    MyConnection.endConnection(connection);

    response.sendRedirect("/shop/buyList");
  }