Why Choose Us?
0% AI Guarantee
Human-written only.
24/7 Support
Anytime, anywhere.
Plagiarism Free
100% Original.
Expert Tutors
Masters & PhDs.
100% Confidential
Your privacy matters.
On-Time Delivery
Never miss a deadline.
The district central library needs an application to store book details of their library
The district central library needs an application to store
book details of their library. The clerk who has all the rights to add a new book,search for any book,display the book details and should update the count of total number of books.
You are provided with a Book with the following private attributes:
-int isbnno
-String bookName
-String authorNeeded getters and setters are written.
Create class Library with the following private attribute:
-ArrayList<Book> bookList = new ArrayList<Book>();
-Also provide the necessary setter and getter methods. Include the following public methods:
-void addBook(Book bobj) - This method should add the book object to the booklist.
-boolean isEmpty() - This method should return true if the booklist is empty else return false.
-ArrayList<Book> viewAllBooks() - This method should return the list of books maintained in the library.
-ArrayList<Book> viewBooksByAuthor(String author ) - This method should return a list of books written by the author passed as argument. When you display an empty list it should print the message "The list is empty".
-int countnoofbook(String bname) - this method should return the count of books with the name passed as argument.Write Main class to test the above functionalities.
Sample Input and Output 1:
1.Add Book
2.Display all book details
3.Search Book by author
4.Count number of books - by book name
5.Exit
Enter your choice:
1
Enter the isbn no:
123
Enter the book name:
Java
Enter the author name:
Bruce Eckel
1.Add Book
2.Display all book details
3.Search Book by author
4.Count number of books - by book name
5.Exit
Enter your choice:
1
Enter the isbn no:
124
Enter the book name:
C++
Enter the author name:
Eric Nagler
1.Add Book
2.Display all book details
3.Search Book by author
4.Count number of books - by book name
5.Exit
Enter your choice:
3
Enter the author name:
Henry
None of the book published by the author Henry
1.Add Book
2.Display all book details
3.Search Book by author
4.Count number of books - by book name
5.Exit
Enter your choice:
3
Enter the author name:
Eric Nagler
ISBN no: 124
Book name: C++
Author name: Eric Nagler
1.Add Book
2.Display all book details
3.Search Book by author
4.Count number of books - by book name
5.Exit
Enter your choice:
5
//Library.java----->Blank
//Main.java-------->Blabk
//Book.java-------->Mentioned Below
public class Book {
private int isbnno;
private String bookName;
private String author;
public int getIsbnno() {
return isbnno;
}
public void setIsbnno(int isbnno) {
this.isbnno = isbnno;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
}
Write a code in java .
Expert Solution
Answer:
Book.java
public class Book {
private int isbnno;
private String bookName;
private String author;
public int getIsbnno() {
return isbnno;
}
public void setIsbnno(int isbnno) {
this.isbnno = isbnno;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
@Override
public String toString() {
return "Book [isbnno=" + isbnno + ", bookName=" + bookName + ", author=" + author + "]";
}
}
Library.java
import java.util.ArrayList;
public class Library {
private ArrayList<Book> bookList = new ArrayList<Book>();
public void addBook(Book bobj) {
bookList.add(bobj);
}
public boolean isEmpty() {
if (bookList.size() == 0) {
return true;
} else {
return false;
}
}
public ArrayList<Book> viewAllBooks() {
return bookList;
}
public ArrayList<Book> viewBooksByAuthor(String author) {
ArrayList<Book> booksByAuthor = new ArrayList<>();
for (int i = 0; i < bookList.size(); i++) {
if (bookList.get(i).getAuthor().equalsIgnoreCase(author)) {
booksByAuthor.add(bookList.get(i));
}
}
return booksByAuthor;
}
public int countnoofbook(String bname) {
int count = 0;
for (int i = 0; i < bookList.size(); i++) {
if (bookList.get(i).getBookName().equalsIgnoreCase(bname)) {
count++;
}
}
return count;
}
}
Main.java
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Library library = new Library();
while(true)
{
System.out.println("1.Add Book");
System.out.println("2.Display all book details");
System.out.println("3.Search Book by author ");
System.out.println("4.Count number of books - by book name");
System.out.println("5.Exit");
System.out.println("Enter your choice:");
int choice = Integer.parseInt(sc.nextLine());
if(choice == 1)
{
System.out.println("Enter the isbn no:");
int isbn = Integer.parseInt(sc.nextLine());
System.out.println("Enter the book name:");
String bname = sc.nextLine();
System.out.println("Enter the author name:");
String author = sc.nextLine();
Book book = new Book();
book.setAuthor(author);
book.setBookName(bname);
book.setIsbnno(isbn);
library.addBook(book);
}
else if(choice == 2)
{
if(library.isEmpty())
{
System.out.println("Library is empty");
}
else
{
ArrayList<Book> bookList = library.viewAllBooks();
for(Book b: bookList)
{
System.out.println(b);
}
}
}
else if(choice == 3)
{
System.out.println("Enter the author name:");
String author = sc.nextLine();
ArrayList<Book> bookByAuthor = library.viewBooksByAuthor(author);
if(bookByAuthor.size() == 0)
{
System.out.println("None of the book published by the author Henry");
}
else
{
for(Book b: bookByAuthor)
{
System.out.println(b);
}
}
}
else if(choice == 4)
{
System.out.println("Enter the book name:");
String bname = sc.nextLine();
int count = library.countnoofbook(bname);
System.out.println("Number of the books by the name "+bname+" are : "+count);
}
else if(choice == 5)
{
break;
}
else
{
System.out.println("Invalid choice, please try again");
}
}
}
}
Step-by-step explanation
Please find the above code for all three required classes.
Archived Solution
You have full access to this solution. To save a copy with all formatting and attachments, use the button below.
For ready-to-submit work, please order a fresh solution below.





