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
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
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
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
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
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
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
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 Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.