1

With the code I currently have the number of loaned out books won't update, so for example if there are 20 books in the array. If 3 are loaned out, obviously the count goes to 3, but if these are returned the count seems to stay at 3. So if another book is loaned out, instead of loanedBookCount() returning 1, it will return 4. Essentially what I seem to have is a method that counts the loaned book transactions.

This is not what I want, I want a method to count the number of books that are currently on loan at that time.

public int loanedBookCount(){
    count = 0;
    for(Book bkObj: bookStore){
        if(bkObj.getCurrentStatus()== CurrentStatus.LOANED){
            count++;
        }
    }
    return count;
}

I have read through this link and this one but can't seem to find how to properly count every single occurrence.

It seems almost like finds the first one it gets to, then increases the count, but I could be wrong and the issue is something else. Either way, can anyone help me try to search for every single book object that is currently loaned out?

I have a method to return the books, but don't think this should include a way to update the number of books on loan (it is in a difference class for staters), I feel like this should a easy enough thing to do from searching the arraylist each time.

I should point out that in the main method uses a for loop to simulate the transactions such as loaning and returning books.

edit: method to return books as requested:

//The aim of this method is to update the status, not create or remove objects
//if there are reservations the book is immediately loaned out, 
//or set to available again if no reservations
public class Book{
...
public void returnBook(){
    if(numberOfReservations ==0){
        status.equals(CurrentStatus.AVAILABLE); 
    }
    else{
        status.equals(CurrentStatus.LOANED);
        numberOfReservations--;
7
  • 1
    The code you posted is correct. The problem is in your transaction handling where apparently new book objects are added upon return instead of updating the objects already present in the list. (Btw, while it will work, a list may not be the best data structure for your case.) Commented Apr 13, 2016 at 8:42
  • Show us your "return book" code Commented Apr 13, 2016 at 8:47
  • 1
    When a book gets returned, do you remove it from the list, update the record to show that it has been returned, or add a new record to say a book was returned? If the latter, then this is the problem as you are not updating the list to show a book has been returned. Commented Apr 13, 2016 at 8:48
  • I might be misunderstanding what you are saying. However I have a method to return the number of books in the store (bookStore.size), this value never changes, including when books are returned. From the first to final transaction the number of books never changes. Commented Apr 13, 2016 at 8:49
  • @Maitiu so when a book gets returned, the object in bookStore gets updated to show that it is no longer loaned out? Can you add the code for this? Commented Apr 13, 2016 at 8:51

1 Answer 1

1

What kind of object is your status in function returnBook() ?

In java function equals on object compare two obejcts.

public boolean equals(Object obj)

In your code calling

status.equals(CurrentStatus.AVAILABLE); 

do nothing for you, since you are not using returned boolean value after comparing objects. You need to status.set(CurrentStatus.AVAIBLE) or status = CurrentStatus.AVAIBLE (depending what your status is) in this function.

Sign up to request clarification or add additional context in comments.

1 Comment

private CurrentStatus status;, and CurrentStatus is enum.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.