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--;
Community
  • 1
  • 1
Maitiu
  • 118
  • 2
  • 10
  • 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.) – JimmyB Apr 13 '16 at 08:42
  • Show us your "return book" code – stjepano Apr 13 '16 at 08: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. – Ben Green Apr 13 '16 at 08: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. – Maitiu Apr 13 '16 at 08: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? – Ben Green Apr 13 '16 at 08:51
  • I've edited above to inc. return method. The aim of that method is to simply update the status, not create or remove objects. – Maitiu Apr 13 '16 at 08:56
  • The variable `count` should be a local variable, not an instance or static variable. It's hard to say without seeing the rest of the code, but your problem is probably related to that design problem. – JB Nizet Apr 13 '16 at 09:15

1 Answers1

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.

Paweł.Ch
  • 44
  • 4