Question

In: Computer Science

using java Define a class Library based on the following specifications: a. A library can have...

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.

Solutions

Expert Solution

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


Related Solutions

Class Exercise: Constructor using JAVA Let’s define a Class together and have a constructor while at...
Class Exercise: Constructor using JAVA Let’s define a Class together and have a constructor while at it. - What should the Class object represent? (What is the “real life object” to represent)? - What properties should it have? (let’s hold off on the methods/actions for now – unless necessary for the constructor) - What should happen when a new instance of the Class is created? - Question: What are you allowed to do in the constructor? - Let’s test this...
Write a program in JAVA using the BigInteger library that can be used to check a...
Write a program in JAVA using the BigInteger library that can be used to check a RSA signature, based on the signer's RSA public key pair. To test your program, take the following information about a message Alice signed and use the verify signature to reproduce the message Alice signed and convert it back to String format. n = 68236588817658935156357212288430888402056854883696767622850112840388111129987 e = 65537 signature = 46612763171375975923246342580942010388414761162366281695045830390867474569531
Define empty methods in Queue class using LinkedList class in Java ------------------------------------------------------------------------------- //Queue class public class...
Define empty methods in Queue class using LinkedList class in Java ------------------------------------------------------------------------------- //Queue class public class Queue{ public Queue(){ // use the linked list } public void enqueue(int item){ // add item to end of queue } public int dequeue(){ // remove & return item from the front of the queue } public int peek(){ // return item from front of queue without removing it } public boolean isEmpty(){ // return true if the Queue is empty, otherwise false }...
Define empty methods in Stack class using LinkedList class in Java ------------------------------------------------------------------------------- //Stack class public class...
Define empty methods in Stack class using LinkedList class in Java ------------------------------------------------------------------------------- //Stack class public class Stack{ public Stack(){ // use LinkedList class } public void push(int item){ // push item to stack } public int pop(){ // remove & return top item in Stack } public int peek(){ // return top item in Stack without removing it } public boolean isEmpty(){ // return true if the Stack is empty, otherwise false } public int getElementCount(){ // return current number...
Create a class library (.dll) in a particular namespace (using csc or VS2017) to define one...
Create a class library (.dll) in a particular namespace (using csc or VS2017) to define one or more classes, each contains fields, methods, constructors, and/or properties, including r/w properties, auto-implemented and/or expression-bodied properties.
JAVA PROBLEM Part 1: Create a Car class in accordance with the following specifications. I will...
JAVA PROBLEM Part 1: Create a Car class in accordance with the following specifications. I will provide the CarClassTester class as a test driver to test your Car class for its basic structure. Do not change the CarClassTester class source code. After you get your Car class working correctly using this test driver, proceed to part 2 below. Car Class Specifications: The Car class must be in a separate package from any driver/tester program. The Car class will contain, at...
I need this done in JAVA. Define a class named Cash. The class contains the following...
I need this done in JAVA. Define a class named Cash. The class contains the following public elements: A Double stored property that contains the amount of money (dollars and cents) described by an object of the class. A read-only, computed property. It calculates and returns the minimum number of U.S. bills and coins that add up to the amount in the stored property.  The return value is an Int array of length 9 that contains (beginning with index 0 of...
The java.awt.Rectangle class of the standard Java library does not supply a method to compute the...
The java.awt.Rectangle class of the standard Java library does not supply a method to compute the area or perimeter of a rectangle. Provide a subclass BetterRectangle of the Rectangle class that has getPerimeter and getArea methods. Do not add any instance variables. In the constructor, call the setLocation and setSize methods of the Rectangle class. In Main class, provide 3 test cases that tests the methods that you supplied printing expected and actual results. The test cases are: 1) One...
JAVA program Create a class called Array Outside of the class, import the Scanner library Outside...
JAVA program Create a class called Array Outside of the class, import the Scanner library Outside of main declare two static final variables and integer for number of days in the week and a double for the revenue per pizza (which is $8.50). Create a method called main Inside main: Declare an integer array that can hold the number of pizzas purchased each day for one week. Declare two additional variables one to hold the total sum of pizzas sold...
Build a web page using an external CSS file based on the following specifications : a)...
Build a web page using an external CSS file based on the following specifications : a) The page must have a breaking point at 768px (desktop/mobile). b) The three sections contents (header, main and footer) must be full-width and no larger than 960px (horizontally centered block). Ma c) Header must be 80px high, full width and fixed and will show your name on the left /5 side. The burger icon should appear when the browser’s window is 768px or lower....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT