Exemplo n.º 1
0
  // update the book loan in the database
  // doesnt allow past times and times beyond a week from now
  private void editLoan(BookLoan loan) {
    // credit to stackoverflow for the date validation:
    // http://stackoverflow.com/questions/2149680/regex-date-format-validation-on-java
    boolean validDate = false;
    String newDate;
    Date today;
    Date parsedDate = new Date();
    SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
    do {
      System.out.println("Please put in the new due date [yyyy/mm/dd]");
      newDate = getInputString();

      try {
        today = new Date();
        parsedDate = format.parse(newDate);
        long timeDiff = parsedDate.getTime() - today.getTime();
        if (timeDiff < 0) {
          System.out.println("You can't pick a time that is past");
        } else if (timeDiff > 604800000) { // 604800000=7*24*3600*1000. A week from now
          System.out.println("The chosen time has to within a week from today");
        } else {
          validDate = true;
        }

      } catch (ParseException e) {
        validDate = false;
      }
    } while (!validDate);
    loan.setDueDate(format.format(parsedDate));
    try {
      Connection conn = getConnection();
      try {
        BookLoanDAO loanDAO = new BookLoanDAO(conn);
        loanDAO.update(loan);
        conn.commit();
        conn.close();
      } catch (Exception e) {
        conn.rollback();
        conn.close();
      }
    } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }