CodeBlanks Logo Icon CodeBlanks

Business logic

Write business rules with methods and conditions.

The borrowBook method

The borrowing logic: find the first available book in the list and set its available field to false. You scan the list with a for-each loop.

void borrowBook() {
  for (Book b : books) {
    if (b.available) {
      b.available = false;
      return;
    }
  }
}

When found, return exits the loop; this way only one book is borrowed. If no book is available, the method simply ends. This behavior will turn into throwing an error in the next lesson.

The returnBook method

To take back a borrowed book you write returnBook: find the first book with available == false and set it to true. The logic is the mirror of borrowBook.

void returnBook() {
  for (Book b : books) {
    if (!b.available) {
      b.available = true;
      return;
    }
  }
}

! takes the negation, so the borrowed book is found. In this lesson's code task you will write only borrowBook; returnBook is the mirror of the same logic and you can add it yourself if you want.

Practice Write and call borrowBook

Add void borrowBook() to the Catalog class: iterate with for (Book b : books), set the first available book's available field to false and exit with return. In main call cat.borrowBook() and print cat.books.

Starter code

Main.java
import java.util.ArrayList;

public class Main {
  public static void main(String[] args) {
    Catalog cat = new Catalog();
    cat.addBook("Suç ve Ceza");
    // Call borrowBook, print the list
  }
}

class Book {
  String title;
  boolean available;
  Book(String title) {
    this.title = title;
    this.available = true;
  }
  public String toString() {
    return (available ? "[müsait] " : "[ödünç] ") + title;
  }
}

class Catalog {
  ArrayList<Book> books = new ArrayList<>();
  void addBook(String title) {
    books.add(new Book(title));
  }
}

Sample solution

Main.java
// inside Catalog:
void borrowBook() {
  for (Book b : books) {
    if (b.available) {
      b.available = false;
      return;
    }
  }
}
// inside main:
cat.borrowBook();
System.out.println(cat.books);

Expected output

[[ödünç] Suç ve Ceza]

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Complete the for-each loop

Question
for (____ b : books) {
  if (b.available) { b.available = false; return; }
}
Answer
for (Book b : books) {
  if (b.available) { b.available = false; return; }
}

Explanation: the for-each loop iterates the list with a Book variable.

2 What does borrowBook do when it finds an available book?

  • Sets available to false and exits with return Correct answer
  • Deletes the list
  • Borrows all books
  • Throws an error

Explanation: available is set to false and return exits the loop; only one book is borrowed.

3 Match each method to its behavior

Term Meaning
borrowBook available false
returnBook available true
addBook Add to list
toString String representation

All lessons in Beginner