Question

Hello I'm working on a java program, called library which takes two previous classes I've made...

Hello I'm working on a java program, called library which takes two previous classes I've made "Book" and Author. Library creates a place where books can be stored and checked in and out. Im having trouble in my Library class with the method getStatus not working after I check a book in or out. I will copy and paste a portion of my library code on here, I have a link for the code for the book author as well as the testing code if needed.

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 in the library.
   *
   * @return Total number of copies in the library.
   */
   public int getTotalCopies() {
       int Totalcop = 0;
       for (int i = 0; i < numBooks; i++) {   //loops over number unique books that have copies
           Totalcop +=copies[i];                   //adds up the amount of copies for all books in one var
       }
       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 < numBooks; i++) {   //loops over number of unqiue books checked out
           Totalnum += checkedOut[i];                   // adds up the amount of checked out books for all in one var
       }
       return Totalnum;
   }
   /**
   * Get a String representing the status of the library.
   *
   * @return Status string.
   */
   public String getStatus() {
       if (numBooks != 0) {                //if library is not empty
           //System.out.println(numBooks + " " + getTotalCopies() +" "+ " " +getNumCheckedOut());
           return "Total unique books: " + numBooks + "\n" + "Total number of copies: " + getTotalCopies() + "\n"
                   + "Total checked out: " + getNumCheckedOut();
       }
       return "Total unique books: 0\n" + "Total number of copies: 0\n" + "Total checked out: 0";
   }
   /**
   * Add all the books in the array to the library. Adds one copy of each book.
   *
   * @param newBooks Books to add.
   */
   public void addBooks(Book[] newBooks) {
       int currentSize = numBooks;                           //current size for
       int newSize = currentSize + newBooks.length;       //creats int for number of books after newBooks is added
                   //int of unique books from newBook not already in library
       int[] tempCopies = new int[newSize]; // sets temp array for copies
       int[] tempCheckedOut = new int[newSize]; // sets temp array for checkout
       Book[] tempBooks = new Book[newSize]; // sets temp array for books
       for (int i = 0; i < numBooks; i++) { // loops number of unique books 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 (with newsize)
       checkedOut = tempCheckedOut; // copies tempcheckedout into checkedout with new size
       books = tempBooks; // copies temp book into books with new size
      
       for (int j = 0; j < newBooks.length; j++) {   // loops for numb of newbooks
           boolean flag=true;                       //creates boolean for if newbook is copy   
           for (int i = 0; i < numBooks; i++) {   //loops for numb of unique books in library
               if(books[i].equals(newBooks[j]))           //if newbook is in library
               {
                   copies[i] += 1;                   // add a copy of that book               
                   flag=false;                       //changes boolean to false
                                           //if book is in library break out of if statement
               }
           }
           if(flag)   {                           //if boolean is true          
              
                                       //number of newBooks not already in library
           books[ numBooks] = newBooks[j]; // adds element from newBooks at the end of books
           copies[numBooks] += 1; //adds 1 copy for each unique book
           checkedOut[numBooks] = 0;
           numBooks ++;
           }
       }
      
   }
   /**
   * 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 });       //calls addBook with just one book, therefore newSize is just one plus currentSize
   }
   /**
   * 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++) { //loops over length of library "books"
           if(b.equals(books[i])) {           //checks to see if book b is already in library
               if(copies[i] != 0) {           //if it is make sure there are still copies left to be checked out
                   checkedOut[i]++;           //check out the book by adding one to checkOut at index i
                   copies[i] -= 1;               //one less copy of book since checked out.
                   return "Checked out!";
              
               }
              
               return "All out of copies.";
          
           }
       }
       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++) { //loops over length of library "books"
           if(b.equals(books[i])) {           // checks to see if book b is already in library
               if(checkedOut[i] != 0) {       //makes sure that book was checked out
                   checkedOut[i] -= 1;           //checks the book in by subtracting of checkOut copies of book
                   return "Checked in!";
              
               }else {
                   return "All of our copies are already checked in.";
               }
           }      
       }
       return "Book not found.";
   }

   /**
   * Get string representation of entire library collection and status.
   *
   * @return String representation of library.
   */
   public String toString() {
       String a = "";
       int i = 0;
       while (i < books.length) {
           a = i + ". " + books[i].getTitle() + ". " + books[i].getAuthor() + ". " + " : " + copies[i] + "\n";
           i++;
           return a;
       }
       return getStatus();
   }

0 0
Add a comment Improve this question Transcribed image text
Answer #1
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 in the library.
         * 
         * @return Total number of copies in the library.
         */
        public int getTotalCopies() {
                int Totalcop = 0;
                for (int i = 0; i < numBooks; 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 < numBooks; i++) {
                        Totalnum = +checkedOut[i];
                }
                return Totalnum;
        }

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

        }

        /**
         * Add all the books in the array to the library. Adds one copy of each book.
         * 
         * @param newBooks Books to add.
         */
        public void addBooks(Book[] newBooks) {
                int currentSize = numBooks;
                int newSize = currentSize + newBooks.length;

                if (newSize > books.length) {
                        // we need to resize.
                        int[] tempCopies = new int[newSize];
                        int[] tempCheckedouts = new int[newSize];
                        Book[] tempBooks = new Book[newSize];
                        for (int i = 0; i < numBooks; i++) { // loops for length of books
                                tempBooks[i] = books[i]; // copies element from books to tempbook
                                tempCopies[i] = copies[i];
                                tempCheckedouts[i] = checkedOut[i];
                        }

                        copies = tempCopies;
                        checkedOut = tempCheckedouts;
                        books = tempBooks;
                }

                for (int j = 0; j < newBooks.length; j++) { // loops for length of newBooks
                        books[j + numBooks] = newBooks[j]; // copies elemnt from newBooks to tempbook
                }
                numBooks += newBooks.length;
        }

        /**
         * 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.equals(books[i])) {
                                if (copies[i] == 0) { // problem with index of copies
                                        return "No copies available.";
                                }
                                if (copies[i] == checkedOut[i]) { // problem with index of copies
                                        return "All out of copies.";
                                }

                                checkedOut[i] += 1;
                                return "Checked out!";
                        }
                }
                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.equals(books[i])) {
                                if (copies[i] == 0) { // problem with index of copies
                                        return "No copies available.";
                                }
                                if (0 == checkedOut[i]) { // problem with index of copies
                                        return "No copies checkedout.";
                                }

                                checkedOut[i] -= 1;
                                return "Check in!";
                        }
                }
                return "Book not found.";
        }

        /**
         * 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 });
        }
}

please use above code.. If any issues, ask in comments..

Add a comment
Know the answer?
Add Answer to:
Hello I'm working on a java program, called library which takes two previous classes I've made...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • A library maintains a collection of books. Books can be added to and deleted from and...

    A library maintains a collection of books. Books can be added to and deleted from and checked out and checked in to this collection. Title and author name identify a book. Each book object maintains a count of the number of copies available and the number of copies checked out. The number of copies must always be greater than or equal to zero. If the number of copies for a book goes to zero, it must be deleted from the...

  • In this assignment you will implement software for your local library. The user of the software...

    In this assignment you will implement software for your local library. The user of the software will be the librarian, who uses your program to issue library cards, check books out to patrons, check books back in, send out overdue notices, and open and close the library. class Calendar We need to deal with the passage of time, so we need to keep track of Java. Declare this variable as private int date within the class itself, not within any...

  • please Code in c++ Create a new Library class. You will need both a header file...

    please Code in c++ Create a new Library class. You will need both a header file and a source file for this class. The class will contain two data members: an array of Book objects the current number of books in the array Since the book array is moving from the main.cc file, you will also move the constant array size definition (MAX_ARR_SIZE) into the Library header file. Write the following functions for the Library class: a constructor that initializes...

  • Modify the library program as follows: Create a method in the LibraryMaterial class that accepts a...

    Modify the library program as follows: Create a method in the LibraryMaterial class that accepts a string s as a parameter and returns true if the title of the string is equal to s. Create the same method for your library material copies. Note that it will need to be abstract in the LibraryMaterialCopy class MY CODE **************************************************************** LibraryCard: import java.util.List; import java.util.ArrayList; import java.time.LocalDate; import    java.time.temporal.ChronoUnit; public class LibraryCard {    private String id;    private String cardholderName;   ...

  • You need to program a simple book library system. There are three java classes Book.java   //...

    You need to program a simple book library system. There are three java classes Book.java   // book object class Library.java   //library class A2.java //for testing The Book.java class represents book objects which contain the following fields title: a string which represents the book title. author: a string to hold the book author name year: book publication year isbn: a string of 10 numeric numbers. The book class will have also 3 constructors. -The default no argument constructor - A constructor...

  • Using C++ Skills Required Create and use classes Exception Handling, Read and write files, work vectors....

    Using C++ Skills Required Create and use classes Exception Handling, Read and write files, work vectors. Create Functions, include headers and other files, Loops(while, for), conditional(if, switch), datatypes,  etc. Assignment You work at the computer science library, and your boss just bought a bunch of new books for the library! All of the new books need to be cataloged and sorted back on the shelf. You don’t need to keep track of which customer has the book or not, just whether...

  • Subject: Java Program You are writing a simple library checkout system to be used by a...

    Subject: Java Program You are writing a simple library checkout system to be used by a librarian. You will need the following classes: Patron - A patron has a first and last name, can borrow a book, return a book, and keeps track of the books they currently have checked out (an array or ArrayList of books). The first and last names can be updated and retrieved. However, a patron can only have up to 3 books checked out at...

  • Really need help with this. This is for my Advanced Java Programming Class. If anyone is...

    Really need help with this. This is for my Advanced Java Programming Class. If anyone is an expert in Java I would very much appreciate the help. I will provide the code I was told to open. Exercise 13-3   Use JPA to work with the library checkout system In this exercise, you'll convert the application from the previous exercise to use JPA instead of JDBC to access the database. Review the project Start NetBeans and open the project named ch13_ex3_library that's...

  • In this project, you will construct an Object-Oriented framework for a library system. The library must...

    In this project, you will construct an Object-Oriented framework for a library system. The library must have books, and it must have patrons. The patrons can check books out and check them back in. Patrons can have at most 3 books checked out at any given time, and can only check out at most one copy of a given book. Books are due to be checked back in by the fourth day after checking them out. For every 5 days...

  • You will be writing a Library simulator involving multiple classes. You will write the LibraryItem, Patron,...

    You will be writing a Library simulator involving multiple classes. You will write the LibraryItem, Patron, and Library classes and the three classes that inherit from LibraryItem (Book, Album and Movie). All data members of each class should be marked as private and the classes should have any get or set methods that will be needed to access them. **USE PYTHON 3 ONLY!!! Here are descriptions of the three classes: LibraryItem: id_code - a unique identifier for a LibraryItem -...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT