Menu

[solved]-Hello Trouble Java Program Assignment Create Library Allows Either Addbooks Library Check Q39084161

Hello, I am having trouble with my java program. The assignmentis to create a Library that allows you to either addbooks to theLibrary or check them out. I am having trouble with my methods”addBook” and “addBooks”. we already have an array called “books”that stores all the books in the library. I am having troubleadding to that already existing array in methods “addBook” and”addBooks”

I will post my code below.

public class Library {

/** Unique books in the library. */
private Book[] books;

/** Number of copies for each book. */
private int[] copies;

/** Number of copies currently checked out for each book.*/
private int[] checkedOut;

/** Number of unique books in the library. */
private int numBooks;

/** Construct a new empty Library. */
public Library(int librarySize) {
books = new Book[librarySize];
copies = new int[librarySize];
checkedOut = new int[librarySize];
numBooks = 0;
}

/**
* Get the number of total copies of all books that exist inthe
* library.
* @return Total number of copies in the library.
*/
public int getTotalCopies() {
   int Totalcop = 0;
   for(int i = 0; i < copies.length; i ++) {
       Totalcop =+ copies[i];
   }
return Totalcop;
}

/**
* Get the number of copies currently checked out.
* @return Total number of copies checked out.
*/
public int getNumCheckedOut() {
   int Totalnum = 0;
   for(int i = 0; i < checkedOut.length; i++) {
       Totalnum =+ checkedOut[i];
   }
return Totalnum;
}

/**
* Get a String representing the status of the library.
* @return Status string.
*/
public String getStatus() {
   if(books.length != 0){
       return “Total unique books: ” +numBooks + “n” + “Total number of copies: “+ getTotalCopies() +”n” + “Total checked out: ” + getNumCheckedOut();
   }
   return “Total unique books: 0n” + “Total number ofcopies: 0n” + “Total checked out: 0”;
  
}

/**
* Add all the books in the array to the library. Adds one copyof
* each book.
* @param newBooks Books to add.
*/
public void addBooks( Book[] newBooks ) {
   int currentSize = books.length;
   int newSize = currentSize + newBooks.length;
   Book[] tempBooks = new Book[newSize];
   int count = 0;
   for(int i = 0; i < books.length; i++ ) { //loopsfor length of books
       tempBooks[i] =books[i];              // copies element from books to tempbook
       count++;                         // at endcount will equal books.length
   }
   for (int j = 0; j < newBooks.length; j++){ //loopsfor length of newBooks
       tempBooks[count++] =newBooks[j];       // copies elemnt fromnewBooks to tempbook
   }
       books =tempBooks.clone();                     //makes tempbook into books
  
}

/**
* Add a single book the library.
*
* If the book is already present, adds another copy.
* If the book is new, add it after the existing books.
* @param b Book to add.
*/
public void addBook( Book b ) {
   int currentSize = books.length;
   int newSize = currentSize + 1;
   Book[] temBook = new Book[newSize];
   for(int j =0; j < books.length;j++) {
       temBook[j] = books[j];          //copies element from book totemBook
   }
   for(int i = 0; i < books.length; i++) {
       if(b == books[i]) {          //checks is b is already inlibrary
           //copies[i] =+1;       //if so add to copies
       }else {
          temBook[currentSize] = b;   //else add b to the lastelement
       }
   }       books =temBook.clone(); //makes book into temBook

}

thank you for the help

Expert Answer


Answer to Hello, I am having trouble with my java program. The assignment is to create a Library that allows you to either addbook… . . .

OR


Leave a Reply

Your email address will not be published. Required fields are marked *