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 Bookint 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 addBookBook book
books.addbook;
Method to remove a book from the library using its ID
public void removeBookint id
for Book book : books
if bookgetId id
books.removebook;
return;
Method to list all books in the library
public void listBooks
for Book book : books
System.out.printlnbook;
import java.ioFile;
import java.ioFileNotFoundException;
import java.util.Scanner;
public class Main
public static void mainString args
Library library new Library;
Read books from the text file and add them to the library
try
File file new Filebookstxt;
Scanner scanner new Scannerfile;
while scannerhasNextLine
String line scanner.nextLine;
String parts line.split;
int id Integer.parseIntparts;
String title parts;
String author parts;
Book book new Bookid title, author;
library.addBookbook;
scanner.close;
catch FileNotFoundException e
System.out.printlnFile not found.";
Scanner input new ScannerSystemin;
int choice;
do
System.out.printlnLibrary Management System";
System.out.println Add a book";
System.out.println Remove a book";
System.out.println List all books";
System.out.println Exit";
System.out.printEnter your choice: ;
choice input.nextInt;
switch choice
case :
System.out.printEnter book ID: ;
int id input.nextInt;
input.nextLine; Consume newline
System.out.printEnter book title: ;
String title input.nextLine;
System.out.printEnter book author: ;
String author input.nextLine;
Book newBook new Bookid title, author;
library.addBooknewBook;
System.out.printlnBook added successfully.";
break;
case :
System.out.printEnter book ID to remove: ;
int removeId input.nextInt;
library.removeBookremoveId;
System.out.printlnBook removed successfully.";
break;
case :
System.out.printlnList of all books:";
library.listBooks;
break;
case :
System.out.printlnExiting;
break;
default:
System.out.printlnInvalid choice. Please enter a valid option.";
break;
while choice ;
input.close;
least : 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 caseuser 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
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
