Question: Step 1 : Create the Book Class Objective: The Book class models a book in the system with attributes like title, author, barcode, and status
Step : Create the Book Class
Objective:
The Book class models a book in the system with attributes like title, author, barcode, and status whether its checked out or not
Code:
Book class to represent each book in the Library Management System.
Author: Your Name
Course: Course Name
Date: Date
public class Book
private String title;
private String author;
private String barcode;
private boolean isCheckedOut;
Constructor
public BookString title, String author, String barcode
this.title title;
this.author author;
this.barcode barcode;
this.isCheckedOut false; Book is initially checked in
Getters
public String getTitle return title;
public String getAuthor return author;
public String getBarcode return barcode;
public boolean isCheckedOut return isCheckedOut;
Check out the book
public void checkOut this.isCheckedOut true;
Check in the book
public void checkIn this.isCheckedOut false;
ToString method to display book details
public String toString
return title by author Barcode: barcode
isCheckedOut "Checked Out" : "Checked In;
import java.util.ArrayList;
Library class to manage the collection of books in the system.
Author: Your Name
Course: Course Name
Date: Date
public class Library
private ArrayList books;
Constructor
public Library
books new ArrayList;
Add a book to the library
public void addBookBook book
books.addbook;
Remove a book by barcode
public boolean removeBookByBarcodeString barcode
for Book book : books
if bookgetBarcodeequalsbarcode
books.removebook;
return true; Book removed successfully
return false; Book not found
Remove a book by title
public boolean removeBookByTitleString title
for Book book : books
if bookgetTitleequalsIgnoreCasetitle
books.removebook;
return true; Book removed successfully
return false; Book not found
Check out a book by title
public boolean checkOutBookString title
for Book book : books
if bookgetTitleequalsIgnoreCasetitle && book.isCheckedOut
book.checkOut;
return true; Book checked out successfully
return false; Book not available for checkout
Check in a book by title
public boolean checkInBookString title
for Book book : books
if bookgetTitleequalsIgnoreCasetitle && book.isCheckedOut
book.checkIn;
return true; Book checked in successfully
return false; Book not found or not checked out
Print all books in the library
public void printLibrary
if booksisEmpty
System.out.printlnThe library is currently empty.";
else
for Book book : books
System.out.printlnbook;
import java.util.Scanner;
import java.ioFile;
import java.ioFileNotFoundException;
LibraryApp class contains the main method to run the Library Management System.
Author: Your Name
Course: Course Name
Date: Date
public class LibraryApp
public static void mainString args
Library library new Library;
Scanner scanner new ScannerSystemin;
Step : Load books from a file
System.out.printEnter the filename to load books: ;
String filename scanner.nextLine;
try
File file new Filefilename;
Scanner fileScanner new Scannerfile;
while fileScannerhasNextLine
String bookData fileScanner.nextLinesplit;
String title bookData;
String author bookData;
String barcode bookData;
library.addBooknew Booktitle author, barcode;
fileScanner.close;
System.out.printlnBooks loaded successfully.";
catch FileNotFoundException e
System.out.printlnFile not found.";
Step : Print the library contents
System.out.printlnCurrent Library:";
library.printLibrary;
Step : Remove a book by barcode
System.out.printEnter a barcode to remove a book: ;
String barcodeToRemove scanner
import java.utiPROVIDED SCREENSHOT
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
