Question: public class Book { private int id; private String title; private String author; / / Constructor public Book ( int id , String title, String

public class Book {
private int id;
private String title;
private String author;
// Constructor
public Book(int id, String title, String author){
this.id = id;
this.title = title;
this.author = author;
}
// Getters
public int getId(){
return id;
}
public String getTitle(){
return title;
}
public String getAuthor(){
return author;
}
// toString method to represent the book as a string
@Override
public String toString(){
return id +"."+ title +","+ author;
}
}
import java.util.ArrayList;
import java.util.List;
public class Library {
private List books;
// Constructor
public Library(){
this.books = new ArrayList<>();
}
// Method to add a book to the library
public void addBook(Book book){
books.add(book);
}
// Method to remove a book from the library using its ID
public void removeBook(int id){
for (Book book : books){
if (book.getId()== id){
books.remove(book);
return;
}
}
}
// Method to list all books in the library
public void listBooks(){
for (Book book : books){
System.out.println(book);
}
}
}
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Library library = new Library();
// Read books from the text file and add them to the library
try {
File file = new File("books.txt");
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()){
String line = scanner.nextLine();
String[] parts = line.split(",");
int id = Integer.parseInt(parts[0]);
String title = parts[1];
String author = parts[2];
Book book = new Book(id, title, author);
library.addBook(book);
}
scanner.close();
} catch (FileNotFoundException e){
System.out.println("File not found.");
}
Scanner input = new Scanner(System.in);
int choice;
do {
System.out.println("Library Management System");
System.out.println("1. Add a book");
System.out.println("2. Remove a book");
System.out.println("3. List all books");
System.out.println("0. Exit");
System.out.print("Enter your choice: ");
choice = input.nextInt();
switch (choice){
case 1:
System.out.print("Enter book ID: ");
int id = input.nextInt();
input.nextLine(); // Consume newline
System.out.print("Enter book title: ");
String title = input.nextLine();
System.out.print("Enter book author: ");
String author = input.nextLine();
Book newBook = new Book(id, title, author);
library.addBook(newBook);
System.out.println("Book added successfully.");
break;
case 2:
System.out.print("Enter book ID to remove: ");
int removeId = input.nextInt();
library.removeBook(removeId);
System.out.println("Book removed successfully.");
break;
case 3:
System.out.println("List of all books:");
library.listBooks();
break;
case 0:
System.out.println("Exiting...");
break;
default:
System.out.println("Invalid choice. Please enter a valid option.");
break;
}
} while (choice !=0);
input.close();
}
}
least 5: adding a book, removing a book, checking in a book, checking out a book, and displaying the contents of the database. And, each of the use case/user stories need to have a UML diagram.

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!