You are on page 1of 4

Book Manipulation

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 author
Needed getters and setters are written.
Create a 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:

1. void addBook(Book bobj) - This method should add the book object to the
booklist.
2. boolean isEmpty() - This method should return true if the booklist is
empty else return false.
3. ArrayList<Book> viewAllBooks() - This method should return the list of

m
books maintained in the library.

er as
4. ArrayList<Book> viewBooksByAuthor(String author ) - This method should

co
return a list of books written by the author passed as argument. When you

eH w
display an empty list it should print the message "The list is empty".
5. int countnoofbook(String bname) - this method should return the count of

o.
books with the name passed as argument.
rs e
Write a Main class to test the above functionalities.
ou urc
Sample Input and Output 1:
1.Add Book
o

2.Display all book details


aC s

3.Search Book by author


v i y re

4.Count number of books - by book name


5.Exit
Enter your choice:
1
Enter the isbn no:
ed d

123
ar stu

Enter the book name:


Java
Enter the author name:
Bruce Eckel
1.Add Book
sh is

2.Display all book details


3.Search Book by author
Th

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:

This study source was downloaded by 100000829897390 from CourseHero.com on 08-03-2021 03:24:44 GMT -05:00

https://www.coursehero.com/file/74716051/Bookmanipulation-javatxt/
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:

m
5

er as
co
Library.java

eH w
1 import java.util.*;
2 public class Library{

o.
3 private ArrayList<Book> bookList = new ArrayList<Book>();
rs e
4
ou urc
5 public void setBookList(ArrayList<Book> bookList){
6 this.bookList=bookList;
7 }
8 public ArrayList<Book> getBookList(){
o

9 return this.bookList;
aC s

10
v i y re

11 }
12 public void addBook(Book bob){
13 bookList.add(bob);
14 }
15 public boolean isEmpty(){
ed d

16 if(bookList.size()==0){
ar stu

17 return true;
18 }
19 else{
20 return false;
21 }
sh is

22 }
23
Th

public ArrayList<Book> viewAllBooks(){


24 return this.bookList;
25 }
26 public ArrayList<Book> viewBooksByAuthor(String author){
27 ArrayList<Book> a = new ArrayList<Book>();
28 for(Book b:bookList){
29 if((b.getAuthor()).equals(author)){
30 a.add(b);
31 }
32 }
33 return a;
34 }
35 public int countnoofbook(String bname){
36 int count=0;
37 for(Book b:bookList){
38 if((b.getBookName()).equals(bname)){
39 count++;

This study source was downloaded by 100000829897390 from CourseHero.com on 08-03-2021 03:24:44 GMT -05:00

https://www.coursehero.com/file/74716051/Bookmanipulation-javatxt/
40 }
41 }
42 return count;
43 }
44 }
Main.java
1 import java.util.*;
2 public class Main{
3 public static void main(String[] args){
4 Scanner sc= new Scanner(System.in);
5 Library l=new Library();
6 while(true){
7 System.out.println("1.Add Book");
8 System.out.println("2.Display all book details");
9 System.out.println("3.Search book by author");
10 System.out.println("4.Count number of books-by book name");
11 System.out.println("5.Exit");
12 System.out.println("Enter your choice:");
13 int num=sc.nextInt();
14 switch(num){
15 case 1:
16 Book b=new Book();

m
17 System.out.println("Enter the isbn no:");

er as
18 b.setIsbnno(sc.nextInt());

co
19 System.out.println("Enter the book name:");

eH w
20 sc.nextLine();
21 b.setBookName(sc.nextLine());

o.
22 System.out.println("Enter the author name:");
rs e
23 b.setAuthor(sc.nextLine());
ou urc
24 l.addBook(b);
25 break;
26 case 2:
27 ArrayList<Book> arr=l.getBookList();
o

28 for(Book b1:arr){
aC s

29 System.out.println(b1);
v i y re

30 }
31 break;
32 case 3:
33 System.out.println("Enter the author name:");
34 sc.nextLine();
ed d

35 String aut=sc.nextLine();
ar stu

36 ArrayList<Book> arr1=l.viewBooksByAuthor(aut);
37 if(arr1.isEmpty()){
38 System.out.println("None of the book published by
the author"+aut);
39 }
sh is

40 else{
41
Th

for(Book b2:arr1){
42 System.out.println(b2);
43 }
44 }
45 break;
46 case 4:
47 System.out.println("Enter the book name");
48 sc.nextLine();
49 String bname=sc.nextLine();
50 System.out.println(l.countnoofbook(bname));
51 break;
52 case 5:
53 System.exit(0);
54 }
55 }
56 }
57 }

This study source was downloaded by 100000829897390 from CourseHero.com on 08-03-2021 03:24:44 GMT -05:00

https://www.coursehero.com/file/74716051/Bookmanipulation-javatxt/
Book.java
1
2 public class Book {
3
4 private int isbnno;
5 private String bookName;
6 private String author;
7
8 public int getIsbnno() {
9 return isbnno;
10 }
11
12 public void setIsbnno(int isbnno) {
13 this.isbnno = isbnno;
14 }
15
16 public String getBookName() {
17 return bookName;
18 }
19
20 public void setBookName(String bookName) {
21 this.bookName = bookName;

m
22 }

er as
23

co
24 public String getAuthor() {

eH w
25 return author;
26 }

o.
27 rs e
28 public void setAuthor(String author) {
ou urc
29 this.author = author;
30
31 }
32 public String toString(){
o

33 return"ISBN no"+getIsbnno()+"\n Book name:"+getBookName()+"\n


aC s

Author name:"+getAuthor();
v i y re

34 }
35
36 }
37
38
ed d
ar stu
sh is
Th

This study source was downloaded by 100000829897390 from CourseHero.com on 08-03-2021 03:24:44 GMT -05:00

https://www.coursehero.com/file/74716051/Bookmanipulation-javatxt/
Powered by TCPDF (www.tcpdf.org)

You might also like