How do I go about coding this in Java I need to model a Libr

How do I go about coding this in Java?

I need to model a Library. Each of these have to be a class.

A Book has a title, author, and ISBN number.

A BookCopy refers to the actual copy of the book that the library has. A library can have multiple BookCopies of the same Book.A BookCopy contains a Book. It also has a LibraryCard that is currently borrowing the book (or null if it is not checked out).

A LibraryCard has an ID number, the name of the cardholder, and a collection of BookCopies that are checked out to that account.

A LibraryCard has methods for checking books in and out. Think carefully about what those methods should look like. What does LibraryCard need to do? What does BookCopy need to do?

Solution

-------------------------------------------------------------------------------------------

import java.util.ArrayList;
import java.util.List;

public class LibraryCard {
private final int id;
private final String name;
private List<BookCopy> issuedBooks = new ArrayList<BookCopy>();
  
   public LibraryCard(int id, String name) {
       this.id = id;
       this.name = name;
   }

   public int getId() {
       return id;
   }

   public String getName() {
       return name;
   }
  
   public void issueBook(BookCopy bookCopy) {
       if(!issuedBooks.contains(bookCopy)) {
           bookCopy.register(this);
           issuedBooks.add(bookCopy);
       } else {
           System.out.println(\"Current book already issued\");
       }
   }
  
   public void returnBook(BookCopy bookCopy) {
       if(issuedBooks.contains(bookCopy)) {
           bookCopy.deRegister();
           issuedBooks.remove(bookCopy);
       }
   }

}

--------------------------------------------------------------------------------------------------

public class BookCopy {
private final Book book;
private LibraryCard libraryCard;
  
   public Book getBook() {
       return book;
   }

   public BookCopy(Book book) {
       this.book = book;
   }
  
   public void register(LibraryCard libraryCard) {
       this.libraryCard = libraryCard;
   }
  
   public void deRegister() {
       this.libraryCard = null;
   }
  
   public LibraryCard getCurrentBookCopyHolder() {
       return libraryCard;
   }

   @Override
   public int hashCode() {
       final int prime = 31;
       int result = 1;
       result = prime * result + ((book == null) ? 0 : book.hashCode());
       return result;
   }

   @Override
   public boolean equals(Object obj) {
       if (this == obj)
           return true;
       if (obj == null)
           return false;
       if (getClass() != obj.getClass())
           return false;
       BookCopy other = (BookCopy) obj;
       if (book == null) {
           if (other.book != null)
               return false;
       } else if (!book.equals(other.book))
           return false;
       return true;
   }

}

-----------------------------------------------------------------------------------------------------


public class Book {
   private final String title;
   private final String author;
   private final String isbnNo;

   public Book(String title, String author, String isbnNo) {
       this.title = title;
       this.author = author;
       this.isbnNo = isbnNo;
   }
   public String getTitle() {
       return title;
   }

   public String getAuthor() {
       return author;
   }

   public String getIsbnNo() {
       return isbnNo;
   }
   @Override
   public int hashCode() {
       final int prime = 31;
       int result = 1;
       result = prime * result + ((isbnNo == null) ? 0 : isbnNo.hashCode());
       return result;
   }
   @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 (isbnNo == null) {
           if (other.isbnNo != null)
               return false;
       } else if (!isbnNo.equals(other.isbnNo))
           return false;
       return true;
   }
}

How do I go about coding this in Java? I need to model a Library. Each of these have to be a class. A Book has a title, author, and ISBN number. A BookCopy refe
How do I go about coding this in Java? I need to model a Library. Each of these have to be a class. A Book has a title, author, and ISBN number. A BookCopy refe
How do I go about coding this in Java? I need to model a Library. Each of these have to be a class. A Book has a title, author, and ISBN number. A BookCopy refe

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site