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 1: 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 Book(String 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 addBook(Book book){
books.add(book);
}
// Remove a book by barcode
public boolean removeBookByBarcode(String barcode){
for (Book book : books){
if (book.getBarcode().equals(barcode)){
books.remove(book);
return true; // Book removed successfully
}
}
return false; // Book not found
}
// Remove a book by title
public boolean removeBookByTitle(String title){
for (Book book : books){
if (book.getTitle().equalsIgnoreCase(title)){
books.remove(book);
return true; // Book removed successfully
}
}
return false; // Book not found
}
// Check out a book by title
public boolean checkOutBook(String title){
for (Book book : books){
if (book.getTitle().equalsIgnoreCase(title) && !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 checkInBook(String title){
for (Book book : books){
if (book.getTitle().equalsIgnoreCase(title) && 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 (books.isEmpty()){
System.out.println("The library is currently empty.");
} else {
for (Book book : books){
System.out.println(book);
}
}
}
}
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
/**
* 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 main(String[] args){
Library library = new Library();
Scanner scanner = new Scanner(System.in);
// Step 1: Load books from a file
System.out.print("Enter the filename to load books: ");
String filename = scanner.nextLine();
try {
File file = new File(filename);
Scanner fileScanner = new Scanner(file);
while (fileScanner.hasNextLine()){
String[] bookData = fileScanner.nextLine().split(",");
String title = bookData[0];
String author = bookData[1];
String barcode = bookData[2];
library.addBook(new Book(title, author, barcode));
}
fileScanner.close();
System.out.println("Books loaded successfully.");
} catch (FileNotFoundException e){
System.out.println("File not found.");
}
// Step 2: Print the library contents
System.out.println("Current Library:");
library.printLibrary();
// Step 3: Remove a book by barcode
System.out.print("Enter 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 blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!