Table of contents
1.
Introduction
2.
Menu for Library Management
2.1.
Java
3.
Implementation
3.1.
1. Adding a Book:
3.2.
Java
3.3.
Java
3.4.
2. Viewing Available Books
3.5.
Java
3.6.
3. Borrowing a Book
3.7.
Java
3.8.
4. Returning a Book
3.9.
Java
3.10.
5. Displaying Member Information
3.11.
Java
3.12.
Java
4.
Frequently Asked Questions
4.1.
Can multiple members borrow the same book?
4.2.
Is there a limit to the number of books a member can borrow?
4.3.
Can a member borrow a book that is already borrowed by another member?
5.
Conclusion
Last Updated: Jul 25, 2024
Easy

Library Management System Project in Java

Author Rinki Deka
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

A library management system is software that helps libraries keep track of books and manage day-to-day operations. It makes it easy for librarians to handle tasks like adding new books, tracking borrowed items, and managing member information. 

Library Management System Project in Java

In this article, we will look at how to create a basic library management system using Java. We will learn the key features with code examples to help you with the program.

Menu for Library Management

The first step in creating a library management system is to design a menu that allows users to interact with the system. The menu should include options for adding books, viewing available books, borrowing books, returning books, and displaying member information. 

Let’s see an example of what the menu could look like:

  • Java

Java

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int choice = 0;

while (choice != 6) {
System.out.println("Library Management System");
System.out.println("1. Add Book");
System.out.println("2. View Available Books");
System.out.println("3. Borrow Book");
System.out.println("4. Return Book");
System.out.println("5. Display Member Information");
System.out.println("6. Exit");
System.out.print("Enter your choice: ");

choice = scanner.nextInt();

switch (choice) {
case 1:
addBook();
break;
case 2:
viewAvailableBooks();
break;
case 3:
borrowBook();
break;
case 4:
returnBook();
break;
case 5:
displayMemberInfo();
break;
case 6:
System.out.println("Thank you for using the Library Management System!");
break;
default:
System.out.println("Invalid choice. Please try again.");
}
}
}
You can also try this code with Online Java Compiler
Run Code


This menu uses a while loop to display the options until the user chooses to exit. The user's choice is processed using a switch statement, which calls the appropriate method based on the selected option.

Implementation

Now that we have the menu, let's look at how to implement each feature.

1. Adding a Book:

To add a book, we need to create a Book class that stores information about the book, such as its title, author, ISBN, and availability status. Here's an example of the Book class:

  • Java

Java

public class Book {
private String title;
private String author;
private String isbn;
private boolean isAvailable;

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

// Getters and setters

public void setAvailable(boolean isAvailable) {
this.isAvailable = isAvailable;
}
}
You can also try this code with Online Java Compiler
Run Code

To add a book, we can create an instance of the Book class and store it in a collection, such as an ArrayList. Here's an example of the addBook() method:

  • Java

Java

private static List<Book> books = new ArrayList<>();

public static void addBook() {

   Scanner scanner = new Scanner(System.in);

   System.out.print("Enter book title: ");

   String title = scanner.nextLine();

   System.out.print("Enter book author: ");

   String author = scanner.nextLine();

   System.out.print("Enter book ISBN: ");

   String isbn = scanner.nextLine();

  

   Book book = new Book(title, author, isbn);

   books.add(book);

   System.out.println("Book added successfully!");

}
You can also try this code with Online Java Compiler
Run Code

2. Viewing Available Books

To view the available books, we can iterate over the books collection and display the details of each book that is currently available. Here's an example of the viewAvailableBooks() method:

  • Java

Java

public static void viewAvailableBooks() {
System.out.println("Available Books:");
for (Book book : books) {
if (book.isAvailable()) {
System.out.println("Title: " + book.getTitle());
System.out.println("Author: " + book.getAuthor());
System.out.println("ISBN: " + book.getIsbn());
System.out.println("------------------------");
}
}
}
You can also try this code with Online Java Compiler
Run Code

3. Borrowing a Book

To borrow a book, we need to find the book in the books collection and mark it as unavailable. We can do this by prompting the user for the book's ISBN and searching for a match. Here's an example of the borrowBook() method:

  • Java

Java

public static void borrowBook() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter book ISBN: ");
String isbn = scanner.nextLine();

for (Book book : books) {
if (book.getIsbn().equals(isbn) && book.isAvailable()) {
book.setAvailable(false);
System.out.println("Book borrowed successfully!");
return;
}
}

System.out.println("Book not found or already borrowed.");
}
You can also try this code with Online Java Compiler
Run Code

4. Returning a Book

Returning a book is similar to borrowing a book. We search for the book using its ISBN and mark it as available. Here's an example of the returnBook() method:

  • Java

Java

public static void returnBook() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter book ISBN: ");
String isbn = scanner.nextLine();

for (Book book : books) {
if (book.getIsbn().equals(isbn) && !book.isAvailable()) {
book.setAvailable(true);
System.out.println("Book returned successfully!");
return;
}
}

System.out.println("Book not found or not borrowed.");
}
You can also try this code with Online Java Compiler
Run Code

5. Displaying Member Information

To display member information, we can create a Member class that stores details like name, ID, and borrowed books. We can then create instances of the Member class and store them in a separate collection. Here's an example of the Member class:

  • Java

Java

public class Member {
private String name;
private String id;
private List<Book> borrowedBooks;

public Member(String name, String id) {
this.name = name;
this.id = id;
this.borrowedBooks = new ArrayList<>();
}

// Getters and setters

public void borrowBook(Book book) {
borrowedBooks.add(book);
}

public void returnBook(Book book) {
borrowedBooks.remove(book);
}
}
You can also try this code with Online Java Compiler
Run Code

To display member information, we can iterate over the members collection and display the details of each member, including their borrowed books. Here's an example of the displayMemberInfo() method:

  • Java

Java

private static List<Member> members = new ArrayList<>();

public static void displayMemberInfo() {
System.out.println("Member Information:");
for (Member member : members) {
System.out.println("Name: " + member.getName());
System.out.println("ID: " + member.getId());
System.out.println("Borrowed Books:");
for (Book book : member.getBorrowedBooks()) {
System.out.println("- " + book.getTitle());
}
System.out.println("------------------------");
}
}
You can also try this code with Online Java Compiler
Run Code


These are the basic implementations of the key features of a library management system. You can expand upon these based on your specific requirements.

Frequently Asked Questions

Can multiple members borrow the same book?

No, each book can only be borrowed by one member at a time. The book must be returned before it can be borrowed by another member.

Is there a limit to the number of books a member can borrow?

In this basic implementation, there is no limit set for the number of books a member can borrow. However, you can easily add a limit by modifying the Member class.

Can a member borrow a book that is already borrowed by another member?

No, a book that is already borrowed cannot be borrowed again until it is returned. The system checks the availability of the book before allowing a member to borrow it.

Conclusion

In this article, we learned how to create a basic library management system using Java. We talked about the essential features, like adding books, viewing available books, borrowing and returning books, and displaying member information. With the help of code examples and explanations, we are sure your doubts will have cleared and could expand the functionality of the system according to your specific needs. 

You can also practice coding questions commonly asked in interviews on Coding Ninjas Code360

Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Live masterclass