/** @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */
  protected void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    // TODO Auto-generated method stub
    int change, empty;
    String tablename = "ParkingInfo";

    HttpSession session = request.getSession();
    String LotId = (String) session.getAttribute("lotid");
    session.setAttribute("wronginfo", null);

    DynamoDB dynamoDB = new DynamoDB(new AmazonDynamoDBClient());
    Table table = dynamoDB.getTable(tablename);
    Item item = table.getItem("Parkurself", "parkurself", "Id", LotId);
    int newe = item.getInt("AvailableSpots");

    change = Integer.parseInt(request.getParameter("carsin"));
    empty = newe - change;
    item.withInt("AvailableSpots", empty);
    table.deleteItem("Parkurself", "parkurself", "Id", LotId);
    table.putItem(item);
    newe = item.getInt("AvailableSpots");

    change = Integer.parseInt(request.getParameter("carsout"));
    empty = newe + change;
    item.withInt("AvailableSpots", empty);
    table.deleteItem("Parkurself", "parkurself", "Id", LotId);
    table.putItem(item);
    newe = item.getInt("AvailableSpots");

    if (item.getInt("AvailableSpots") > item.getInt("Total")) {
      item.withInt("AvailableSpots", item.getInt("Total"));
      table.deleteItem("Parkurself", "parkurself", "Id", LotId);
      table.putItem(item);
      session.setAttribute("wronginfo", "minus");
    } else if (item.getInt("AvailableSpots") < 0) {
      int wrongnum = 0 - item.getInt("AvailableSpots");
      item.withInt("AvailableSpots", 0);
      table.deleteItem("Parkurself", "parkurself", "Id", LotId);
      table.putItem(item);
      session.setAttribute("wronginfo", "exceed");
      session.setAttribute("wrongnum", wrongnum);
    }
    session.setAttribute("olde", item.get("AvailableSpots"));
    session.setAttribute("newe", item.get("AvailableSpots"));
    response.sendRedirect("home.jsp");
  }
  @Override
  public void saveUserPreferences(String principalName, String preferences) {
    Item item = Item.fromJSON(preferences);
    item = item.withString(PRINCIPAL_NAME_KEY, principalName);

    DynamoDB dynamoDB = new DynamoDB(documentstoreClient.getClient());
    Table table = dynamoDB.getTable(USER_PREFERENCES);
    PutItemOutcome outcome = table.putItem(item);
    LOG.debug("Saved user preferences: " + outcome);
  }
  public static void createItems(ChatData chatData) {

    Table table = dynamoDB.getTable(tableName);
    System.out.println(table.getTableName());
    try {

      Item item =
          new Item()
              .withPrimaryKey("TimeStamp", dateNow())
              .withString("user", chatData.getUser())
              .withString("chat", chatData.getChat());
      table.putItem(item);
    } catch (Exception e) {
      System.err.println("Create items failed.");
      System.err.println(e.getMessage());
    }
  }