示例#1
0
  // display the possible effects on a book loan
  private BookLoan getLoan() {
    System.out.println("Which library processed the loan?");
    LibraryBranch branch = this.showAllBranches();
    Borrower bor = new Borrower();
    Book book = new Book();
    BookLoan loan = null;
    try {
      Connection conn = getConnection();
      try {
        LibraryBranchDAO libDAO = new LibraryBranchDAO(conn);
        BookLoanDAO bookLoanDAO = new BookLoanDAO(conn);
        BorrowerDAO borDAO = new BorrowerDAO(conn);
        BookDAO bookDAO = new BookDAO(conn);
        ArrayList<String> actions = new ArrayList<String>();
        actions.add("Cancel");
        if (branch != null) {
          Borrower borrower = null;
          System.out.println("What is the user's card number? [0 to cancel]");
          do {
            int card = getInputInt(0, 10000);
            if (card == 0) {
              break;
            }
            bor = borDAO.readOne(card);
            if (bor == null) {
              System.out.println("Invalid card number.try again.");
            }
          } while (bor == null);
          if (bor != null) {
            String sql =
                "SELECT * FROM tbl_book WHERE tbl_book.bookId IN (SELECT tbl_book_loans.bookId FROM tbl_book_loans WHERE branchId = ? AND cardNo =? AND dateIn IS NULL )";
            List<Book> books =
                (List<Book>)
                    bookDAO.read(sql, new Object[] {branch.getBranchId(), bor.getCardNo()});
            System.out.println("Which book?");
            int choice = getChoiceNumber(books, actions);
            if (choice > 0 && choice <= books.size()) {
              book = books.get(choice - 1);
              loan = bookLoanDAO.readOne(book.getBookId(), branch.getBranchId(), bor.getCardNo());
            }
          }
        }

      } catch (Exception e) {
        conn.rollback();
        conn.close();
        e.printStackTrace();
      }

    } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    return loan;
  }
示例#2
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();
    }
  }
示例#3
0
  private void deleteBranch(LibraryBranch branch) {
    try {
      Connection conn = getConnection();
      try {
        LibraryBranchDAO libDAO = new LibraryBranchDAO(conn);
        BookLoanDAO loanDAO = new BookLoanDAO(conn);
        List<BookLoan> branchLoans =
            (List<BookLoan>)
                loanDAO.read(
                    "SELECT * FROM tbl_book_loans WHERE branchId = ? AND dateIn IS NULL",
                    new Object[] {branch.getBranchId()});
        if (branchLoans.size() > 0) {
          ArrayList<String> answers = new ArrayList<String>();
          answers.add("No, nevermind");
          answers.add("Yes, delete this branch");
          System.out.println(
              "This branch has " + branchLoans.size() + " books that are not returned");
          System.out.println("Are you sure you still want to delete? These books will be lost");
          displayOptions(answers);
          int in = getInputInt(1, 2);
          if (in == 1) {
            return;
          }
        }
        libDAO.delete(branch);
        conn.commit();
        conn.close();
      } catch (Exception e) {
        conn.rollback();
        conn.close();
      }

    } catch (Exception e) {
      // TODO Auto-generated catch block
      System.err.println("Error while connecting to database");
      e.printStackTrace();
    }
  }