0% found this document useful (0 votes)
9 views

import java A.........L

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)
9 views

import java A.........L

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/ 5

import java.util.

ArrayList;

// Book class

class Book {

// Class attributes for storing book details

private String title;

private String author;

private String isbn;

private boolean isBorrowed;

// Constructor to initialize a new book

public Book(String title, String author, String isbn) {

this.title = title;

this.author = author;

this.isbn = isbn;

this.isBorrowed = false;

// Method to get the string representation of the book

@Override

public String toString() {

return "'" + title + "' by " + author + " (ISBN: " + isbn + ")";

// Method to borrow the book

public boolean borrow() {

if (!isBorrowed) {

isBorrowed = true;

return true;
}

return false;

// Method to return the book

public boolean returnBook() {

if (isBorrowed) {

isBorrowed = false;

return true;

return false;

// Method to check if the book is borrowed

public boolean isBorrowed() {

return isBorrowed;

// Getter for ISBN

public String getIsbn() {

return isbn;

// Library class

class Library {

// Class attribute to store the list of books

private ArrayList<Book> books;


public Library() {

books = new ArrayList<>(); // Initialize the list of books

// Method to add a new book to the library

public void addBook(Book book) {

books.add(book);

// Method to list all books in the library

public void listBooks() {

System.out.println("Books available in the library:");

for (Book book : books) {

String status = book.isBorrowed() ? "Borrowed" : "Available";

System.out.println(book + " - Status: " + status);

// Method to borrow a book by ISBN

public boolean borrowBook(String isbn) {

for (Book book : books) {

if (book.getIsbn().equals(isbn) && !book.isBorrowed()) {

return book.borrow();

return false;

// Method to return a book by ISBN


public boolean returnBook(String isbn) {

for (Book book : books) {

if (book.getIsbn().equals(isbn) && book.isBorrowed()) {

return book.returnBook();

return false;

// Main class

public class LibrarySystem {

public static void main(String[] args) {

// Create a library instance

Library library = new Library();

// Create book objects and add them to the library

Book book1 = new Book("1984", "George Orwell", "9780451524935");

Book book2 = new Book("To Kill a Mockingbird", "Harper Lee", "9780061120084");

Book book3 = new Book("The Great Gatsby", "F. Scott Fitzgerald", "9780743273565");

library.addBook(book1);

library.addBook(book2);

library.addBook(book3);

// List all books

library.listBooks();

// Borrow a book
String isbnToBorrow = "9780451524935";

if (library.borrowBook(isbnToBorrow)) {

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

} else {

System.out.println("Book not found or already borrowed.");

// List all books again

System.out.println("\nBooks available in the library:");

library.listBooks();

// Return the borrowed book

if (library.returnBook(isbnToBorrow)) {

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

} else {

System.out.println("Book not found or was not borrowed.");

// List all books again

System.out.println("\nBooks available in the library:");

library.listBooks();

You might also like