w3resource

Java: Library class with add and remove books methods

Java OOP: Exercise-11 with Solution

Write a Java program to create a class called "Library" with a collection of books and methods to add and remove books.

Sample Solution:

Java Code:

//Book.java
public class Book {
  private String title;
  private String author;

  public Book(String title, String author) {
    this.title = title;
    this.author = author;
  }

  public String getTitle() {
    return title;
  }

  public void setTitle(String title) {
    this.title = title;
  }

  public String getAuthor() {
    return author;
  }

  public void setAuthor(String author) {
    this.author = author;
  }
}

The above class has two private attributes, "title" and "author". It has a constructor that takes two arguments, the title and author of the book, and initializes the corresponding attributes. It also has getter and setter methods to access and modify the title and author attributes.

//Library.java
import java.util.ArrayList;

public class Library {
  private ArrayList < Book > books;

  public Library() {
    books = new ArrayList < Book > ();
  }

  public void addBook(Book book) {
    books.add(book);
  }

  public void removeBook(Book book) {
    books.remove(book);
  }

  public ArrayList < Book > getBooks() {
    return books;
  }
}

The above “Library” class has a private books attribute, which is an ArrayList of Book objects. The Library constructor initializes this attribute as an empty list. The “addBook()” method adds a Book object to the books list, while the “removeBook()” method removes a Book object from the list. The “getBooks()” method returns the books list.

//Main.java
public class Main {
  public static void main(String[] args) {
    Library library = new Library();

    Book book1 = new Book("Adventures of Tom Sawyer", "Mark Twain");
    Book book2 = new Book("Ben Hur", "Lewis Wallace");
    Book book3 = new Book("Time Machine", "H.G. Wells");
    Book book4 = new Book("Anna Karenina", "Leo Tolstoy");

    library.addBook(book1);
    library.addBook(book2);
    library.addBook(book3);
    library.addBook(book4);

    System.out.println("Books in the library:");
    for (Book book: library.getBooks()) {
      System.out.println(book.getTitle() + " by " + book.getAuthor());
    }

    library.removeBook(book2);
    System.out.println("\nBooks in the library after removing " + book2.getTitle() + ":");
    for (Book book: library.getBooks()) {
      System.out.println(book.getTitle() + " by " + book.getAuthor());
    }
  }
}

In the above class, we create an instance of the Library class and add two Book objects to the collection using the “addBook()” method. We then display the books in the library using the “displayBooks()” method. We remove one of the books using the “removeBook()” method and display the updated collection of books in the library.

Sample Output:

Books in the library:
Adventures of Tom Sawyer by Mark Twain
Ben Hur by Lewis Wallace
Time Machine by H.G. Wells
Anna Karenina by Leo Tolstoy

Books in the library after removing Ben Hur:
Adventures of Tom Sawyer by Mark Twain
Time Machine by H.G. Wells
Anna Karenina by Leo Tolstoy

Flowchart:

Flowchart: Java  OOP Exercises: Library class with add and remove books methods.
Flowchart: Java  OOP Exercises: Library class with add and remove books methods.

Java Code Editor:

Improve this sample solution and post your code through Disqus.

Java OOP Previous: Manage student courses using the Student class.
Java OOP Next: Airplane class to check flight status and delay.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.