In: Computer Science
using java
Define a class Library based on the following specifications: a. A library can have multiple books. Decide on the best possible data structure to store books. b. A library has a name. c. A library has an address. Define a 2-argument constructor for the Library class. Decide on the arguments needed for this constructor. e. Define a method that can add new books to the library. f. Define a method that allows a book to be borrowed (checked out). Note: a book can be borrowed if and only if it's available. Decide what parameter(s) if any to pass to this method and if this method needs to return a value (if any). 9. Define a method that allows a book to be returned to the library (checked in). Decide what parameter(s) if any to pass to this method and if this method needs to return a value (if any). h. Define a method that allows a library to be searched for a given book based on the book's ISBN. Decide what parameter(s) if any to pass to this method and if this method needs to return a value (if any). 1. Define a method to print the name and address of the library plus the list of books the library has.
Book.java
public class Book {
String title;
String author;
String publisher;
String ISBN;
String copyrightDate;
boolean borrowed;
public Book(String title, String author, String
publisher, String iSBN, String date) {
super();
this.title = title;
this.author = author;
this.publisher = publisher;
ISBN = iSBN;
this.copyrightDate = date;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public String getISBN() {
return ISBN;
}
public void setISBN(String iSBN) {
ISBN = iSBN;
}
public String getCopyrightDate() {
return copyrightDate;
}
public void setCopyrightDate(String copyrightDate)
{
this.copyrightDate =
copyrightDate;
}
public boolean isBorrowed() {
return borrowed;
}
public void setBorrowed(boolean borrowed) {
this.borrowed = borrowed;
}
@Override
public String toString() {
return "Book [title=" + title + ",
author=" + author + ", publisher=" + publisher + ", ISBN=" +
ISBN
+ ", copyrightDate=" + copyrightDate + ",
borrowed=" + borrowed + "]";
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return
true;
if (obj == null)
return
false;
if (getClass() !=
obj.getClass())
return
false;
Book other = (Book) obj;
if (ISBN == null) {
if (other.ISBN
!= null)
return false;
} else if
(!ISBN.equals(other.ISBN))
return
false;
return true;
}
}
Library.java
import java.util.ArrayList;
public class Library {
String name;
String address;
ArrayList<Book> books;
public Library(String name, String address) {
super();
this.name = name;
this.address = address;
this.books = new
ArrayList<>();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public ArrayList<Book> getBooks() {
return books;
}
public void setBooks(ArrayList<Book> books)
{
this.books = books;
}
public void addBook(Book book) {
books.add(book);
}
public boolean checkout(String ISBN) {
if(books.size()>0) {
for(int
i=0;i<books.size();i++)
if(books.get(i).getISBN().compareToIgnoreCase(ISBN)==0) {
if(!books.get(i).isBorrowed()) {
books.get(i).setBorrowed(true);
return true;
}
}
}
return false;
}
public boolean checkIn(String ISBN) {
if(books.size()>0) {
for(int
i=0;i<books.size();i++)
if(books.get(i).getISBN().compareToIgnoreCase(ISBN)==0) {
if(books.get(i).isBorrowed())
{
books.get(i).setBorrowed(false);
return true;
}
}
}
return false;
}
public Book searchBook(String ISBN) {
if(books.size()>0) {
for(int
i=0;i<books.size();i++)
if(books.get(i).getISBN().compareToIgnoreCase(ISBN)==0) {
return books.get(i);
}
}
return null;
}
public void print() {
System.out.println("Name of Library
: "+name);
System.out.println("Address of
Library : "+address);
books.forEach(s->System.out.println(s));
}
}
TestLibrary.java
public class TestLibrary {
public static void main(String[] args) {
// TODO Auto-generated method
stub
Book book = new Book("Title",
"author", "publisher", "iSBN", "date");
Library library = new
Library("Library", "Washington");
library.addBook(book);
library.addBook(new Book("Title1",
"author1", "publisher1", "iSBN1", "date1"));
boolean flag =
library.checkout("iSBNs");
if(flag)
System.out.println("Book with iSBNs is CheckedOut
Succesfully");
else
System.out.println("Book with iSBNs is already CheckedOut or not
available");
flag = library.checkIn("iSBN");
if(flag)
System.out.println("Book with iSBN is CheckedIn
Succesfully");
else
System.out.println("Book with iSBN is already CheckedIN or not
available");
Book book1 =
library.searchBook("isbn1");
System.out.println("Book searched
is "+book1);
System.out.println("Printing
library Details");
library.print();
}
}
Output
Book with iSBNs is already CheckedOut or not available
Book with iSBN is CheckedIn Succesfully
Book searched is Book [title=Title1, author=author1,
publisher=publisher1, ISBN=iSBN1, copyrightDate=date1,
borrowed=false]
Printing library Details
Name of Library : Library
Address of Library : Washington
Book [title=Title, author=author, publisher=publisher, ISBN=iSBN,
copyrightDate=date, borrowed=false]
Book [title=Title1, author=author1, publisher=publisher1,
ISBN=iSBN1, copyrightDate=date1, borrowed=false]
Feel free to ask any doubts, if you face any difficulty in understanding.
Please upvote the answer if you find it helpful