示例#1
0
  private void editBorrower(Borrower bor) {
    try {
      Connection conn = getConnection();
      System.out.println("Enter new name: [N/A to skip]");
      String name = getInputString();
      System.out.println("Enter new address: [N/A to skip]");
      String address = getInputString();
      System.out.println("Enter new phone: [N/A to skip]");
      String phone = getInputString();
      if (!name.equals("N/A")) {
        bor.setName(name);
      }
      if (!address.equals("N/A")) {
        bor.setAddress(address);
      }
      if (!phone.equals("N/A")) {
        bor.setPhone(phone);
      }

      try {
        BorrowerDAO borDAO = new BorrowerDAO(conn);
        borDAO.update(bor);
        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();
    }
  }
 /** *************************************************************************************** */
 public void UpdateBorrower(Borrower bor) throws Exception {
   ConnectionUtil c = new ConnectionUtil();
   Connection conn = c.createConnection();
   try {
     if (bor == null
         || bor.getCardNo() == 0
         || bor.getBorrowerName() == null
         || bor.getBorrowerName().length() == 0
         || bor.getBorrowerName().length() > 45
         || bor.getBorrowerAddress() == null
         || bor.getBorrowerAddress().length() == 0
         || bor.getBorrowerAddress().length() > 45
         || bor.getBorrowerPhone() == null
         || bor.getBorrowerPhone().length() == 0
         || bor.getBorrowerPhone().length() > 45) {
       throw new Exception("The Borrower cannot be null or CardNo can not be 0 ");
     } else {
       BorrowerDAO bordao = new BorrowerDAO(conn);
       bordao.update(bor);
       conn.commit();
     }
   } catch (Exception e) {
     e.printStackTrace();
     conn.rollback();
   } finally {
     conn.close();
   }
 }
示例#3
0
 private void addBorrower() {
   try {
     Connection conn = getConnection();
     try {
       BorrowerDAO borDAO = new BorrowerDAO(conn);
       Borrower toAdd = new Borrower();
       System.out.println("What is the borrower's name?");
       String borName = getInputString();
       System.out.println("What is the borrower's address?");
       String borAddress = getInputString();
       System.out.println("What is the borrower's phone address?");
       String borPhone = getInputString();
       toAdd.setName(borName);
       toAdd.setAddress(borAddress);
       toAdd.setPhone(borPhone);
       borDAO.create(toAdd);
       conn.commit();
       conn.close();
     } catch (Exception e) {
       conn.rollback();
       conn.close();
       e.printStackTrace();
     }
   } catch (Exception e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
   }
 }
 /**
  * ************************************* COPIES
  * SECTION********************************************************************
  */
 public void createBorrower(Borrower bor) throws Exception {
   ConnectionUtil c = new ConnectionUtil();
   Connection conn = c.createConnection();
   try {
     if (bor == null
         || bor.getBorrowerName() == null
         || bor.getBorrowerName().length() == 0
         || bor.getBorrowerName().length() > 45
         || bor.getBorrowerAddress() == null
         || bor.getBorrowerAddress().length() == 0
         || bor.getBorrowerAddress().length() > 45
         || bor.getBorrowerPhone() == null
         || bor.getBorrowerPhone().length() == 0
         || bor.getBorrowerPhone().length() > 45) {
       throw new Exception("Borrower Name, Address, Phone cannot be empty or more than 45 Chars");
     } else {
       BorrowerDAO bdao = new BorrowerDAO(conn);
       bdao.create(bor);
       conn.commit();
     }
   } catch (Exception e) {
     e.printStackTrace();
     conn.rollback();
   } finally {
     conn.close();
   }
 }
 @RequestMapping(
     value = "/deleteBorrower",
     method = {RequestMethod.GET, RequestMethod.POST},
     consumes = "application/json")
 public List<Borrower> deleteBorrower(@RequestBody Borrower borrower) {
   borrowerdao.deleteBorrower(borrower);
   return borrowerdao.getAllBorrowers(1, 5);
 }
示例#6
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;
  }
 public List<Borrower> ListBorrower() throws Exception {
   ConnectionUtil c = new ConnectionUtil();
   Connection conn = c.createConnection();
   try {
     BorrowerDAO bdao = new BorrowerDAO(conn);
     List<Borrower> bor = bdao.readAll();
     conn.commit();
     return bor;
   } catch (Exception e) {
     e.printStackTrace();
     return null;
   } finally {
     conn.close();
   }
 }
 @RequestMapping(
     value = "/getBorrowersCount",
     method = {RequestMethod.GET, RequestMethod.POST},
     produces = "application/json")
 public int getBorrowersCount() {
   return borrowerdao.getBorrowersCount();
 }
 @RequestMapping(
     value = "/editBorrower",
     method = {RequestMethod.GET, RequestMethod.POST},
     consumes = "application/json")
 public void editBorrower(@RequestBody List<Borrower> borrowers) {
   for (Borrower borrower : borrowers) borrowerdao.updateBorrower(borrower);
 }
示例#10
0
 /*Admin Borrower*/
 @RequestMapping(
     value = "/getBorrowers/{pageNo}/{pageSize}",
     method = {RequestMethod.GET, RequestMethod.POST},
     produces = "application/json")
 public List<Borrower> getBorrowers(@PathVariable int pageNo, @PathVariable int pageSize) {
   return borrowerdao.getAllBorrowers(pageNo, pageSize);
 }
 /** *************************************************************************************** */
 public void DeleteBorrower(Borrower bor) throws Exception {
   ConnectionUtil c = new ConnectionUtil();
   Connection conn = c.createConnection();
   try {
     if (bor == null) {
       throw new Exception("The Borrower cannot be null");
     } else {
       BorrowerDAO bordao = new BorrowerDAO(conn);
       bordao.delete(bor);
       conn.commit();
     }
   } catch (Exception e) {
     e.printStackTrace();
     conn.rollback();
   } finally {
     conn.close();
   }
 }
  /** *************************************************************************************** */
  public Borrower ListOneBorrower(int cardNo) throws Exception {

    ConnectionUtil c = new ConnectionUtil();
    Connection conn = c.createConnection();

    try {

      BorrowerDAO bdao = new BorrowerDAO(conn);
      Borrower bor = bdao.readOne(cardNo);
      return bor;

    } catch (Exception e) {
      // TODO: handle exception
      e.printStackTrace();
      return null;
    } finally {
      conn.close();
    }
  }
示例#13
0
  private Borrower showAllBorrowers() {
    Borrower bor = null;
    try {
      Connection conn = getConnection();
      BorrowerDAO borDAO = new BorrowerDAO(conn);
      List<Borrower> allBorrowers = borDAO.readAll();
      ArrayList<String> actions = new ArrayList<String>();
      actions.add("Cancel");
      int choice = getChoiceNumber(allBorrowers, actions);
      if (choice > 0 && choice <= allBorrowers.size()) {
        bor = allBorrowers.get(choice - 1);
      }
      conn.close();
    } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

    return bor;
  }
示例#14
0
  private void deleteBorrower(Borrower bor) {
    try {
      Connection conn = getConnection();
      try {
        BorrowerDAO borDAO = new BorrowerDAO(conn);
        BookLoanDAO loanDAO = new BookLoanDAO(conn);
        List<BookLoan> branchLoans =
            (List<BookLoan>)
                loanDAO.read(
                    "SELECT * FROM tbl_book_loans WHERE cardNo = ? AND  dateIn IS NULL)",
                    new Object[] {bor.getCardNo()});
        if (branchLoans.size() > 0) {
          ArrayList<String> answers = new ArrayList<String>();
          answers.add("No, nevermind");
          answers.add("Yes, delete this branch");
          System.out.println(
              "This user 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;
          }
        }
        borDAO.delete(bor);
        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();
    }
  }