3

I've been having this issue with a 'Loanbook' method, the function of it is to increment the 'numOnLoan' variable stored in an object ArrayList by 1, however when i run the method it doesn't seem to change the value.

//Allows loaning of books
public static void Loanbook(Scanner sc, ArrayList<Book> books){
System.out.println("Please enter a book title");
    if(sc.hasNext()){
        String criteria = sc.nextLine();
        for (int i = 0; i < books.size(); i++){
            if(criteria.equals(books.get(i).getBookTitle())){
            System.out.println("The book " + books.get(i).getBookTitle() + " there are " + books.get(i).getNumInStock() + " in stock");
                    books.get(i).setNumOnLoan(books.get(i).GetNumOnLoan()+1);
                    System.out.println("number on loan: " + books.get(i).GetNumOnLoan()); break;    
                        }
                    } System.out.println("Book Loaned");
                        LibraryTester.MenuReturn(sc, books);

            }   
                LibraryTester.MenuReturn(sc, books);
        }

i suspect this is an issue with the logic of the code by however i edit it, the code doesn't seem to do what i want.

Edit: book.java

public class Book {
private int id;
private String bookTitle;
private String authorName;
private int bookReleaseYear;
private int numOnLoan;
private int numInStock;

//constructor
public Book(int id, String bookTitle, String authorName, int bookReleaseYear, int numOnLoan, int numInStock) {
    this.id = id;
    this.bookTitle = bookTitle;
    this.authorName = authorName;
    this.bookReleaseYear = bookReleaseYear;
    this.numOnLoan = numOnLoan;
    this.numInStock = numInStock;
}
//Getters/Setters
public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getBookTitle() {
    return bookTitle;
}

public void setBookTitle(String bookTitle) {
    this.bookTitle= bookTitle;
}

public String getAuthorName() {
    return authorName;
}

public void setAuthorName(String authorName) {
    this.authorName = authorName;
}

public int GetNumOnLoan() {
    return numOnLoan;
}

public void setNumOnLoan(int numOnLoan) {
    this.numOnLoan = numOnLoan;
}

public int getNumInStock() {
    return numInStock;
}

public void setNumInStock(int numInStock) {
    this.numInStock = numInStock;
}



public int getBookReleaseYear() {
    return bookReleaseYear;
}

public void setBookReleaseYear(int bookReleaseYear) {
    this.bookReleaseYear = bookReleaseYear;
}

Library.java

import java.util.ArrayList;

import java.util.Scanner;

public class Library {

public ArrayList<Book> books = new ArrayList<Book>();

public Library(){
      super();
    }

//Getters/Setters
public Library(ArrayList<Book> books) {
    this.books = books;
}

public ArrayList<Book> getBooks() {
    return books;
}

public void setBooks(ArrayList<Book> books) {
    this.books = books;
}

//Methods

//Allows loaning of books
public static void Loanbook(Scanner sc, ArrayList<Book> books){
System.out.println("Please enter a book title");
    if(sc.hasNext()){
        String criteria = sc.nextLine();
        for (int i = 0; i < books.size(); i++){
            if(criteria.equals(books.get(i).getBookTitle())){
            System.out.println("The book " + books.get(i).getBookTitle() + "    there are " + books.get(i).getNumInStock() + " in stock");
                    books.get(i).setNumOnLoan(+1);
                    System.out.println("number on loan: " +    books.get(i).GetNumOnLoan()); break; 
                        }
                    } System.out.println("Book Loaned");
                        LibraryTester.MenuReturn(sc, books);

            }   
                LibraryTester.MenuReturn(sc, books);
        }


}

public static void Returnbook(Scanner sc, ArrayList<Book> books){
System.out.println("Please enter a book title");
    if(sc.hasNext()){
        String criteria = sc.nextLine();
    for (int i = 0; i < books.size(); i++){
        if(criteria.equals(books.get(i).getBookTitle())){
        System.out.println("The book " + books.get(i).getBookTitle() +
        " is in stock," + " there are " + books.get(i).getNumInStock() + " in stock and " + books.get(i).GetNumOnLoan() + " out on loan");
        books.get(i).setNumOnLoan(-1);; break;

                        } 


                    } System.out.println("Book returned");
                        LibraryTester.MenuReturn(sc, books);

            }
        }

LibraryTester.java

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;

public class LibraryTester {

public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);
    Library lib = new Library();
    ArrayList<Book> books = lib.getBooks();
    books = Library.CreateBooksArrayList();
    MenuInput(sc, books);
    sc.close();
    lib.setBooks(books);

}



//prints menu
public static void PrintMenu(){
    System.out.println("Public Library Menu System");
    System.out.println("---------------------------");
    System.out.println("1: Add Book");
    System.out.println("2: Search for book");
    System.out.println("3: Loan Book");
    System.out.println("4: Return Book");
    System.out.println("5: Amend Book Details");
    System.out.println("6: Display all Books");
    System.out.println("7: Delete a Book");
    System.out.println("8: Other Options");
    System.out.println("9: Exit system");
    System.out.println("----------------------------");
}


// Allows menu input
public static void MenuInput(Scanner sc, ArrayList<Book> books){
int input = 0;
PrintMenu();
if(sc.hasNextInt()){
    input = sc.nextInt();

    switch(input){
    case 1:Library.AddBook(sc, books);
    case 2:Library.SearchBooks(sc, books);;
    case 3:Library.Loanbook(sc,books);
    case 4:Library.Returnbook(sc, books);
    case 5:Library.AmendDetails(sc,books);
    case 6:Library.DisplayAllBooks(sc,books);
    case 7:Library.Removebook(sc, books);;
    case 8:OtherMenu(sc,books);
    case 9:System.exit(0);
    }
} else {
    System.out.println("Please enter a number");
}



}



//Returns user to main menu
public static void MenuReturn(Scanner sc, ArrayList<Book> books){
System.out.println("Press any key to return to the main menu");
if(sc.hasNextLine()){
    sc.nextLine();
    MenuInput(sc, books);
    }
}





}

EDIT: I seem to get the output:

Please enter a book title
Input:   book1
Please enter a book title
The book book1 is in stock, there are 5 in stock and 1 out on loan
Book returned
Press any key to return to the main menu

Final Edit: Fixed the issue, the code was not running due to an issue with the if statement, when i removed it there was a problem with all the text appearing at once, which was fixed with am extra 'sc.nextLine'. The issue with the ReturnBook running without call was due to lack of a 'break;' in the menu system.

Thanks everybody for your help, especially Shreyans Sheth

9
  • 4
    Just a tip to simplify the code: if you don't use i for anything other than the index, use an enhanced for statement instead: for (Book book : books) { ... }, and refer to book instead of books.get(i) all the time. Commented Dec 11, 2015 at 11:17
  • 2
    Can you post the complete code? I think the issue might be that the String input and that in the array list object is not same Commented Dec 11, 2015 at 11:18
  • 1
    ahh, that would make it easier to understand, ill experiment about with that, thanks for the tip! Commented Dec 11, 2015 at 11:19
  • 2
    @d1234 (and ShreyansSheth): posting the entire code is not useful: you bury the actual problem in other code which is not clearly relevant, and it is unclear whether that code works or is broken. Please post a MCVE to illustrate the specific problem. Commented Dec 11, 2015 at 11:30
  • 1
    Is this the output you are expecting? <br>Please enter a book title (input)bbbbb<br> The book bbbbb there are 5 in stock<br> number on loan: **2**<br> Book Loaned<br> Please enter a book title<br> bbbbb<br> The book bbbbb there are 5 in stock<br> number on loan: **3**<br> Book Loaned<br> Commented Dec 11, 2015 at 11:45

1 Answer 1

2

I just dug deep into your code.

It seems you had a nasty bug (If it is one).

I'll be addressing this specific line in your question: "the function of it is to increment the 'numOnLoan' variable stored in an object ArrayList by 1, however when i run the method it doesn't seem to change the value."

Original method:

public void setNumOnLoan(int numOnLoan)  
{
    this.numOnLoan= numOnLoan;
}

Method call in Loanbook:

books.get(i).setNumOnLoan(1);


In the LoanBook method, you are simply passing '+1' as a parameter and everytime, only 1 is being assigned. Here's what you need to do.

public void setNumOnLoan(int numOnLoan)  
{
    //When numOnLoan is 1, as you have passed everytime
    //the current value gets incremented by one. I think that is what you wanted.
    this.numOnLoan += numOnLoan; //You add it to the existing variable
}



You really should have named that function something else. I'm pretty sure there are more bugs out there but does THIS accomplish what you wanted?


Output:
(First call to Loanbook)
Please enter a book title
input :bbbbb
The book bbbbb there are 5 in stock
number on loan: 2
Book Loaned

(Second call to Loanbook)
Please enter a book title
input: bbbbb
The book bbbbb there are 5 in stock
number on loan: 3
Book Loaned

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

10 Comments

Worth noting that if this is what the OP meant to do, the function shouldn't be called setNumOnLoan, and should be incrementNumOnLoan (if it always increments by 1) or incrementNumOnLoanByValue (if incremented by parameter) or similar... set implies the function works as it was originally.
Yes you are absolutely right. Wrong coding+naming convention. I had a bad time going through all that spaghetti code. You got a lot to work on @d1234 :)
Thank you very much Shreyans Sheth for taking the time to look at my code, I'm not sure what im doing but i still seem to be getting the same problem, and 'numOnLoan' seems to be going down rather than up. I greatly appreciate the help and advice though, I'm an absolute beginner and have a long way to go.
It seems as if my code is somehow referencing the returnbook method instead of the Loanbook method, i cant see how that could be though
* i cant see how that could be though * Find the function call then!
|

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.