0% found this document useful (0 votes)
4 views6 pages

Document

This document contains a Java implementation of a simple library management system. It includes classes for Book and Library, allowing users to add, display, borrow, and return books. The main class provides a user interface for interacting with the library system through a console menu.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views6 pages

Document

This document contains a Java implementation of a simple library management system. It includes classes for Book and Library, allowing users to add, display, borrow, and return books. The main class provides a user interface for interacting with the library system through a console menu.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

import java.util.

ArrayList;

import java.util.Scanner;

// Book class

class Book {

private String title;

private String author;

private boolean isAvailable;

public Book(String title, String author) {

this.title = title;

this.author = author;

this.isAvailable = true;

public String getTitle() {

return title;

public String getAuthor() {

return author;

public boolean isAvailable() {

return isAvailable;

}
public void borrowBook() {

if (isAvailable) {

isAvailable = false;

System.out.println("You borrowed: " + title);

} else {

System.out.println("Sorry, this book is already borrowed.");

public void returnBook() {

isAvailable = true;

System.out.println("You returned: " + title);

@Override

public String toString() {

return "Title: " + title + ", Author: " + author + ", Available: " +
(isAvailable ? "Yes" : "No");

// Library class

class Library {

private ArrayList<Book> books;

public Library() {

books = new ArrayList<>();


}

public void addBook(Book book) {

books.add(book);

System.out.println("Book added: " + book.getTitle());

public void displayBooks() {

if (books.isEmpty()) {

System.out.println("No books available in the library.");

return;

for (Book book : books) {

System.out.println(book);

public Book findBook(String title) {

for (Book book : books) {

if (book.getTitle().equalsIgnoreCase(title)) {

return book;

return null;

}
// Main class

public class LibraryManagement {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

Library library = new Library();

// Adding some books

library.addBook(new Book("Java Programming", "James Gosling"));

library.addBook(new Book("The Great Gatsby", "F. Scott Fitzgerald"));

library.addBook(new Book("Harry Potter", "J.K. Rowling"));

while (true) {

System.out.println("\nLibrary Menu:");

System.out.println("1. Display Books");

System.out.println("2. Borrow Book");

System.out.println("3. Return Book");

System.out.println("4. Exit");

System.out.print("Choose an option: ");

int choice = scanner.nextInt();

scanner.nextLine(); // Consume newline

switch (choice) {

case 1:

library.displayBooks();

break;

case 2:
System.out.print("Enter book title to borrow: ");

String borrowTitle = scanner.nextLine();

Book borrowBook = library.findBook(borrowTitle);

if (borrowBook != null) {

borrowBook.borrowBook();

} else {

System.out.println("Book not found.");

break;

case 3:

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

String returnTitle = scanner.nextLine();

Book returnBook = library.findBook(returnTitle);

if (returnBook != null) {

returnBook.returnBook();

} else {

System.out.println("Book not found.");

break;

case 4:

System.out.println("Exiting Library System. Goodbye!");

scanner.close();

return;

default:

System.out.println("Invalid option! Please try again.");

}
}

You might also like