package sm;
import java.util.ArrayList;
import java.util.Scanner;
class Book {
String title;
String author;
boolean isBorrowed;
public Book(String title, String author) {
this.title = title;
this.author = author;
this.isBorrowed = false;
public void borrowBook() {
if (!isBorrowed) {
isBorrowed = true;
System.out.println("Book borrowed successfully!");
} else {
System.out.println("Sorry, this book is already borrowed.");
public void returnBook() {
if (isBorrowed) {
isBorrowed = false;
System.out.println("Book returned successfully!");
} else {
System.out.println("This book wasn't borrowed.");
@Override
public String toString() {
return "Title: " + title + ", Author: " + author + ", Status: " + (isBorrowed ? "Borrowed" : "Available");
class Library {
private ArrayList<Book> books = new ArrayList<>();
public void addBook(Book book) {
books.add(book);
System.out.println("Book added successfully!");
public void viewBooks() {
if (books.isEmpty()) {
System.out.println("No books in the library.");
} else {
for (int i = 0; i < books.size(); i++) {
System.out.println((i + 1) + ". " + books.get(i));
}
public void removeBook(int index) {
if (index >= 0 && index < books.size()) {
books.remove(index);
System.out.println("Book removed successfully!");
} else {
System.out.println("Invalid book index.");
public void borrowBook(int index) {
if (index >= 0 && index < books.size()) {
books.get(index).borrowBook();
} else {
System.out.println("Invalid book index.");
public void returnBook(int index) {
if (index >= 0 && index < books.size()) {
books.get(index).returnBook();
} else {
System.out.println("Invalid book index.");
public void searchBooks(String keyword) {
boolean found = false;
for (Book book : books) {
if (book.title.toLowerCase().contains(keyword.toLowerCase()) ||
book.author.toLowerCase().contains(keyword.toLowerCase())) {
System.out.println(book);
found = true;
if (!found) {
System.out.println("No matching books found.");
public void listBorrowedBooks() {
boolean found = false;
for (Book book : books) {
if (book.isBorrowed) {
System.out.println(book);
found = true;
if (!found) {
System.out.println("No borrowed books.");
public void listAvailableBooks() {
boolean found = false;
for (Book book : books) {
if (!book.isBorrowed) {
System.out.println(book);
found = true;
if (!found) {
System.out.println("No available books.");
public void updateBook(int index, String newTitle, String newAuthor) {
if (index >= 0 && index < books.size()) {
books.get(index).title = newTitle;
books.get(index).author = newAuthor;
System.out.println("Book details updated successfully!");
} else {
System.out.println("Invalid book index.");
public class LibraryManagementSystem {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Library library = new Library();
int choice;
do {
System.out.println("\n Menu");
System.out.println("\n===== Library Management System =====");
System.out.println("1. Add a Book");
System.out.println("2. View All Books");
System.out.println("3. Remove a Book");
System.out.println("4. Borrow a Book");
System.out.println("5. Return a Book");
System.out.println("6. Search Books");
System.out.println("7. List Borrowed Books");
System.out.println("8. List Available Books");
System.out.println("9. Update Book Details");
System.out.println("10. Exit");
System.out.print("Enter your choice: ");
System.out.println("\n");
choice = scanner.nextInt();
scanner.nextLine(); // Consume newline
switch (choice) {
case 1:
System.out.print("Enter book title: ");
String title = scanner.nextLine();
System.out.print("Enter book author: ");
String author = scanner.nextLine();
library.addBook(new Book(title, author));
break;
case 2:
System.out.println("\nAvailable Books:");
library.viewBooks();
break;
case 3:
System.out.println("\nEnter book index to remove: ");
library.viewBooks();
int removeIndex = scanner.nextInt() - 1;
library.removeBook(removeIndex);
break;
case 4:
System.out.println("\nEnter book index to borrow: ");
library.viewBooks();
int borrowIndex = scanner.nextInt() - 1;
library.borrowBook(borrowIndex);
break;
case 5:
System.out.println("\nEnter book index to return: ");
library.viewBooks();
int returnIndex = scanner.nextInt() - 1;
library.returnBook(returnIndex);
break;
case 6:
System.out.print("Enter keyword to search by title or author: ");
String keyword = scanner.nextLine();
library.searchBooks(keyword);
break;
case 7:
System.out.println("\nBorrowed Books:");
library.listBorrowedBooks();
break;
case 8:
System.out.println("\nAvailable Books:");
library.listAvailableBooks();
break;
case 9:
System.out.println("\nEnter book index to update: ");
library.viewBooks();
int updateIndex = scanner.nextInt() - 1;
scanner.nextLine(); // Consume newline
System.out.print("Enter new title: ");
String newTitle = scanner.nextLine();
System.out.print("Enter new author: ");
String newAuthor = scanner.nextLine();
library.updateBook(updateIndex, newTitle, newAuthor);
break;
case 10:
System.out.println("Exiting the system...");
break;
default:
System.out.println("Invalid choice. Please try again.");
} while (choice != 10);
scanner.close();