CodeBlanks Logo Icon CodeBlanks

Borrow flow

Write borrow and return operations.

The borrow flow

The fourth lesson defines the Loan class. A loan represents a member borrowing a book. The Loan class brings together Book and Member objects.

Example: class Loan { Book book; Member member; Loan(Book b, Member m) { book = b; member = m; } }. This class holds two object references. A toString method shows the loan relationship: member.name + " <- " + book.title. The loan flow is one of the library's core operations; it tracks who has which book. Later, returns and dates can be added.

Object references

The Loan class holds object references, not primitive types. The Book book field points to a Book object, the Member member field to a Member object. This keeps the relationship between the book and the member live.

Example: Book b = new Book("Java", "Gosling"); Member m = new Member("Alex", 101); Loan loan = new Loan(b, m);. Here the loan object holds references to b and m. loan.member.name accesses the member's name, loan.book.title the book's title. Object references provide the link between classes.

Practice Add the Loan class

Add a Loan class to the file. Write Book book and Member member fields. Add a Loan(Book b, Member m) constructor; assign both fields. Add a public String toString() method returning member.name + " <- " + book.title. Put main inside Loan; create a Book, a Member and a Loan instance, print the Loan instance.

Starter code

Main.java
class Library {
    String name;
    Library(String n) {
        name = n;
    }
    public String toString() {
        return "Library: " + name;
    }
}
class Book {
    String title;
    String author;
    Book(String t, String a) {
        title = t;
        author = a;
    }
    public String toString() {
        return title + " - " + author;
    }
}
class Member {
    String name;
    int id;
    Member(String n, int i) {
        name = n;
        id = i;
    }
    public String toString() {
        return name + " (" + id + ")";
    }
}

// Add the Loan class
// book and member fields, constructor and toString
// Instantiate and print in main

Sample solution

Main.java
class Library {
    String name;
    Library(String n) {
        name = n;
    }
    public String toString() {
        return "Library: " + name;
    }
}
class Book {
    String title;
    String author;
    Book(String t, String a) {
        title = t;
        author = a;
    }
    public String toString() {
        return title + " - " + author;
    }
}
class Member {
    String name;
    int id;
    Member(String n, int i) {
        name = n;
        id = i;
    }
    public String toString() {
        return name + " (" + id + ")";
    }
}
class Loan {
    Book book;
    Member member;
    Loan(Book b, Member m) {
        book = b;
        member = m;
    }
    public String toString() {
        return member.name + " <- " + book.title;
    }
    public static void main(String[] args) {
        Book b = new Book("Java", "Gosling");
        Member m = new Member("Alex", 101);
        Loan loan = new Loan(b, m);
        System.out.println(loan);
    }
}

Expected output

Alex <- Java

Questions and answers

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

1 Complete the Loan constructor

Question
class Loan {
    Book book;
    Member member;
    ____(Book b, Member m) {
        book = b;
        member = m;
    }
}
Answer
class Loan {
    Book book;
    Member member;
    Loan(Book b, Member m) {
        book = b;
        member = m;
    }
}

Explanation: The constructor name must match the class name.

2 Match the Loan parts

Term Meaning
Book book Book reference
Member member Member reference
Loan(Book b, Member m) Constructor
member.name Member name access

3 Which two objects does the Loan class combine?

  • Book and Book
  • Book and Member Correct answer
  • Member and Library
  • Library and Book

Explanation: Book and Member — a loan links a book and a member.

All lessons in Intermediate