Menu

[solved]-Hello Trouble Java Program Assignment Create Library Allows Either Add Books Library Check Q39085340

Hello, I am havingtrouble with my java program. The assignment is to create a Librarythat allows you to either add books to the Library or check booksout. I am having trouble with my methods “checkOut” and”CheckIn”.

I will post my codebelow if you can help me with these methods thank you.

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 = numBooks;
   int newSize = currentSize + newBooks.length;
  

       int[] tempCopies = newint[newSize];       //sets temp array forcopies
       int[] tempCheckedOut = newint[newSize];   //sets temp array for checkout
       Book[] tempBooks = newBook[newSize];       // sets temp arrayfor books
       for(int i = 0; i < numBooks;i++) {       //loops number of uniquebooks in library
           tempBooks[i] =books[i];              //copies element from book to tempbook
           tempCopies[i] =copies[i];              //copies element from copies to tempcopies
          tempCheckedOut[i] = checkedOut[i];      //copies element from checkedout to tempcheckout
       }
       copies = tempCopies;                 //copies tempcopie into copies (withnewsize)
       checkedOut =tempCheckedOut;          //copies tempcheckedout into checkedout with new size
       books = tempBooks;                     //copies temp book into bookswith new size
  
       for(int j = 0; j <newBooks.length; j++) {   //loops for numb ofnewbooks
           books[j+numBooks] = newBooks[j];   //adds element from newBooksat the end of books
       }
       numBooks +=newBooks.length;   //updates number of unique books inlibrary
}

/**
* 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 ) {
   addBooks(new Book[] {b});

}

/**
* Checks out a book from the library if possible.
* @param b Book to check out.
* @return String denoting success or failure.
*/
public String checkOut ( Book b ) {
   for(int i =0; i < books.length; i++) {
       if(b == books[i]) {
           //if(copies[i]==0) { //problem with index of copies
           //  return “All out of copies.”;
           //}
           return “Checkedout!”;
       }
   }
return “Book not found.”;
}

/**
* Checks in a book to the library if possible.
* @param b Book to check in.
* @return String denoting success or failure.
*/
public String checkIn ( Book b ) {
   for(int i = 0; i < books.length; i++) {
       if(b == books[i]) {
           //if(copies)
           return “Checkedin!”;
       }
   }
return “Book not found.”;
}

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 add boo… . . .

OR


Leave a Reply

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