Ejemplo n.º 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;
  }