This document defines three classes - Book, Library, and testBook - to model a basic library system. The Book class represents a book with title, author, year, and availability status. The Library class manages a collection of books, allowing adding, searching by title, and checking out books. The testBook class contains a main method to test the Library class functionality.
This document defines three classes - Book, Library, and testBook - to model a basic library system. The Book class represents a book with title, author, year, and availability status. The Library class manages a collection of books, allowing adding, searching by title, and checking out books. The testBook class contains a main method to test the Library class functionality.
this.author = author; } public int getYear() { return year; }
public void setYear(int year) {
this.year = year; }
public boolean isAvailable() {
return isAvailable; }
public void setAvailable(boolean available) {
isAvailable = available; }
public void displayInfo() {
System.out.println("Title: " + title + ", Author: " + author + ", Year: " + year + ", Available: " + isAvailable); } } Library class import java.util.ArrayList;
public class Library {
private final ArrayList<Book> books;
public Library() { books = new ArrayList<>(); }
public void addBook(Book book) {
books.add(book); }
public void searchByTitle(String title) {
boolean found = false; for (Book book : books) { if (book.getTitle().equals(title)) { found = true; book.displayInfo(); } } if (!found) { System.out.println("No books found with the title " + title); } }
public void checkoutBook(String title) {
for (Book book : books) { if (book.getTitle().equals(title)) { if (book.isAvailable()) { book.setAvailable(false); System.out.println("Book checked out successfully."); } else { System.out.println("Book is not available."); } return; } } System.out.println("Book not found."); } } testBook (main method) public class testBook { public static void main(String[] args) { Library library = new Library();