Exemplo 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;
  }
Exemplo n.º 2
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();
    }
  }