private void setState(EBorrowState state) {
    System.out.println("Setting state: " + state);

    this.state = state;
    ui.setState(state);

    switch (state) {
      case INITIALIZED:
        reader.setEnabled(true);
        scanner.setEnabled(false);
        break;

      case SCANNING_BOOKS:
        reader.setEnabled(false);
        scanner.setEnabled(true);
        this.bookList = new ArrayList<IBook>();
        this.loanList = new ArrayList<ILoan>();
        scanCount = borrower.getLoans().size();

        // clear currentBook display
        ui.displayScannedBookDetails("");
        // clear pending loan display
        ui.displayPendingLoan("");
        break;

      case CONFIRMING_LOANS:
        reader.setEnabled(false);
        scanner.setEnabled(false);
        // display pending loans
        ui.displayConfirmingLoan(buildLoanListDisplay(loanList));
        break;

      case COMPLETED:
        reader.setEnabled(false);
        scanner.setEnabled(false);
        for (ILoan loan : loanList) {
          loanDAO.commitLoan(loan);
        }
        printer.print(buildLoanListDisplay(loanList));
        close();
        break;

      case CANCELLED:
        reader.setEnabled(false);
        scanner.setEnabled(false);
        close();
        break;

      case BORROWING_RESTRICTED:
        reader.setEnabled(false);
        scanner.setEnabled(false);
        ui.displayErrorMessage(
            String.format("Member %d cannot borrow at this time.", borrower.getID()));
        System.out.format("Member %d cannot borrow at this time.", borrower.getID());
        break;

      default:
        throw new RuntimeException("Unknown state");
    }
  }
  private void setupTestData() {
    IBook[] book = new IBook[15];
    IMember[] member = new IMember[6];

    book[0] = bookDao_.addBook("author1", "title1", "callNo1");
    book[1] = bookDao_.addBook("author1", "title2", "callNo2");
    book[2] = bookDao_.addBook("author1", "title3", "callNo3");
    book[3] = bookDao_.addBook("author1", "title4", "callNo4");
    book[4] = bookDao_.addBook("author2", "title5", "callNo5");
    book[5] = bookDao_.addBook("author2", "title6", "callNo6");
    book[6] = bookDao_.addBook("author2", "title7", "callNo7");
    book[7] = bookDao_.addBook("author2", "title8", "callNo8");
    book[8] = bookDao_.addBook("author3", "title9", "callNo9");
    book[9] = bookDao_.addBook("author3", "title10", "callNo10");
    book[10] = bookDao_.addBook("author4", "title11", "callNo11");
    book[11] = bookDao_.addBook("author4", "title12", "callNo12");
    book[12] = bookDao_.addBook("author5", "title13", "callNo13");
    book[13] = bookDao_.addBook("author5", "title14", "callNo14");
    book[14] = bookDao_.addBook("author5", "title15", "callNo15");

    member[0] = memberDao_.addMember("fName0", "lName0", "0001", "email0");
    member[1] = memberDao_.addMember("fName1", "lName1", "0002", "email1");
    member[2] = memberDao_.addMember("fName2", "lName2", "0003", "email2");
    member[3] = memberDao_.addMember("fName3", "lName3", "0004", "email3");
    member[4] = memberDao_.addMember("fName4", "lName4", "0005", "email4");
    member[5] = memberDao_.addMember("fName5", "lName5", "0006", "email5");

    Calendar cal = Calendar.getInstance();
    Date now = cal.getTime();

    // create a member with overdue loans
    for (int i = 0; i < 2; i++) {
      ILoan loan = loanDao_.createLoan(member[1], book[i]);
      loanDao_.commitLoan(loan);
    }
    cal.setTime(now);
    cal.add(Calendar.DATE, ILoan.LOAN_PERIOD + 1);
    Date checkDate = cal.getTime();
    loanDao_.updateOverDueStatus(checkDate);

    // create a member with maxed out unpaid fines
    member[2].addFine(10.0f);

    // create a member with maxed out loans
    for (int i = 2; i < 7; i++) {
      ILoan loan = loanDao_.createLoan(member[3], book[i]);
      loanDao_.commitLoan(loan);
    }

    // a member with a fine, but not over the limit
    member[4].addFine(5.0f);

    // a member with a couple of loans but not over the limit
    for (int i = 7; i < 9; i++) {
      ILoan loan = loanDao_.createLoan(member[5], book[i]);
      loanDao_.commitLoan(loan);
    }
    // Note - there are 9 total loans.
  }
 @Test
 public void testCardSwipedUnrestricted3() {
   // We will test swipe card with a member who exists and is not restricted.
   // This test will be with a member who has loans, but not overdue.
   // The state should still then become SCANNING_BOOKS.
   controlClass_.initialise();
   controlClass_.cardSwiped(6); // Member ID = 6.
   assertTrue(loanDao_.findLoansByBorrower(memberDao_.getMemberByID(6)).size() > 0);
   assertEquals(EBorrowState.SCANNING_BOOKS, controlClass_.getState());
 }
 @Test
 public void testLoansConfirmedLoanListCreated() {
   // We will set up a loan with a member who has no outstanding loans and
   // then check that the loan was correctly confirmed/committed.
   controlClass_.initialise();
   controlClass_.cardSwiped(1); // Member ID = 1.
   controlClass_.bookScanned(10); // An available book.
   controlClass_.scansCompleted();
   controlClass_.loansConfirmed();
   List<ILoan> loanList = loanDao_.listLoans();
   assertEquals(EBorrowState.COMPLETED, controlClass_.getState());
   // We know from the setUpTestData method that there are 9 loans, so now
   // that we have committed this most recent loan, there should be 10.
   assertEquals(10, loanList.size());
 }
  @Override
  public void bookScanned(int barcode) {
    System.out.println("bookScanned: got " + barcode);
    if (state != EBorrowState.SCANNING_BOOKS) {
      throw new RuntimeException(
          String.format("BorrowUC_CTL : bookScanned : illegal operation in state: %s", state));
    }
    ui.displayErrorMessage("");

    IBook book = bookDAO.getBookByID(barcode);
    if (book == null) {
      ui.displayErrorMessage(String.format("Book %d not found", barcode));
      return;
    }

    if (book.getState() != EBookState.AVAILABLE) {
      ui.displayErrorMessage(
          String.format("Book %d is not available: %s", book.getID(), book.getState()));
      return;
    }

    if (bookList.contains(book)) {
      ui.displayErrorMessage(String.format("Book %d already scanned: ", book.getID()));
      return;
    }

    scanCount++;
    bookList.add(book);
    ILoan loan = loanDAO.createLoan(borrower, book);
    loanList.add(loan);

    // display current book
    ui.displayScannedBookDetails(book.toString());

    // display pending loans
    ui.displayPendingLoan(buildLoanListDisplay(loanList));

    if (scanCount >= IMember.LOAN_LIMIT) {
      setState(EBorrowState.CONFIRMING_LOANS);
    }
  }