-1

I am working on a library application which allows users to store, borrow and return technical manuals.

I have ran into a problem when building the return section of the application.

Currently, if the user borrows a manual and they wish to return it, they must enter the title of the manual from the list of borrowed manuals displayed. However, if the user wishes to return any manual after the first on that list, nothing happens. The user enters the name and hits the enter key, but the application simply halts.

Here is an example of the error I am describing:

enter image description here

Here is the relevant code:

static void returnManual(){
    System.out.printf("\n\nHere are the Manual/s currently out on loan:\n\n");
    if(ManualList.get(ManualChoice).status.equalsIgnoreCase(status2) && borrowedManuals.size() >= ManualChoice){
        for (int i = 0; i < borrowedManuals.size(); i++)
            System.out.println(borrowedManuals.get(i).displayManual());
        returnManualTitle = Console.readString(Messages.enterManualTitle, Messages.tooShortMessage, 3);
    }

    int x = 0;
    boolean titleExistance = false;
    while (x < ManualList.size()){//Search for the Manual by title, if it exists change it's status,
                                //it's borrower and borrowDate.

        if (ManualList.get(x).title.equalsIgnoreCase(returnManualTitle)){

            ManualList.get(x).status = "Available";
            ManualList.get(x).borrower = "N/A";
            ManualList.get(x).borrowDate = "N/A";
            ManualList.get(x).returnDate = "N/A";

            int p = 0;
            borrowLoop:
            while (p < borrowedManuals.size()){//Search for the Manual by title, if it exists change it's status,
                //it's borrower and borrowDate.

                if (borrowedManuals.get(p).title.equalsIgnoreCase(returnManualTitle)){

                    borrowedManuals.remove(p);
                    break borrowLoop;
                }

            }               
            System.out.println(Messages.successReturnMessage);
            titleExistance = true;

            break;//if a title is found, break out of the loop and display choice menu.
        }
        x = x+1;
    }//end of while loop.
    if(titleExistance == false){
        boolean repeatReturnManual = Console.readYesNo("\n--------------------------------------------------------------------------" + "\n\nThe Manual with the title "+"\""+returnManualTitle +"\""+ " wasn't found!"
                                                        +"\n\nDo you want to try again? (Y/N):\n");
        System.out.println("\n--------------------------------------------------------------------------");
        if(repeatReturnManual){
            returnManual();
        }else{
            Menu.displayMenu();
        }
    }else if(titleExistance){
        Menu.menuChoice = 7;
    }               
}

/**
 * Removes the Manual.
 */
eckes
  • 10,103
  • 1
  • 59
  • 71
Oscar
  • 511
  • 2
  • 10
  • 25
  • 1
    Probably show us how/where you use `returnManualTitle` and add some tracing output to see where is the problem. – PeterMmm Jan 03 '15 at 17:56
  • 1
    I dont see where you expect the string "Please enter the title...."? – SMA Jan 03 '15 at 17:56
  • 1
    How do you get input? – AmirSojoodi Jan 03 '15 at 17:56
  • 1
    You dont show the code after the entry. Actually it looks like the loop is more asking for yes/no than the title. – eckes Jan 03 '15 at 17:58
  • Updated with more code, is this okay now? @eckes – Oscar Jan 03 '15 at 18:00
  • 1
    `break LABEL` is valid Java code, but IMHO a bit quirky. Try to refactor to *plain* loops or call a method. – PeterMmm Jan 03 '15 at 18:06
  • @PeterMmm thanks a lot for your reply, as I am new to Java would it be possible to show me an example of what you mean? If not thank you for helping regardless :) – Oscar Jan 03 '15 at 18:10
  • 1
    @JamesPatterson yes the problem was in the code you did not show - see my answer below :) I am actually not sure why the question is inside the loop, that might be another problem. – eckes Jan 03 '15 at 18:11
  • 1
    Your test data "TITLE1" is rather easy, but imagine your book title is 2 lines long - how wants to enter that? – eckes Jan 03 '15 at 18:18
  • 1
    Ah, just noticed, the input was not in a loop, I fixed the indention of your code to make this clearer. – eckes Jan 03 '15 at 18:21
  • 1
    Feel free to actually accept the answer if it fixes the problem. – eckes Jan 03 '15 at 18:27
  • @eckes Will do, sorry for the late reply. I have implemented your changes and they work great, however now when I return one book and go back to return another, nothing is displayed under the "Here are the Manual/s currently out on loan:" line apart from a message saying a manual has been returned, but this is not reflected once the user views all manuals stored in the library as the manual still says it is being borrowed. Any idea why this might be happening? – Oscar Jan 03 '15 at 18:59
  • No idea, but I dont understand what "ManualChoice" and "status2" is. So maybe it is releated. – eckes Jan 03 '15 at 19:09

2 Answers2

1

The p in the borrowsManual while loop needs to be incremented, otherwise it will run in a endless lopp.

while (p < borrowedManuals.size()) {
    Manual borrowed = borrowedManuals.get(p); // guessing the name of this class
    if (borrowed.title.equalsIgnoreCase(returnManualTitle)) {
        borrowedManuals.remove(p);
        break;
    }
    p++; // this is mising
}        

(I am not sure if this whole linear search business is so good, but we don't want to rewrite the whole application, right? :)

eckes
  • 10,103
  • 1
  • 59
  • 71
  • Thanks for your answer, is it possible to show me a working example of what you mean? I am new to Java so its difficult for me to understand :( – Oscar Jan 03 '15 at 18:12
  • 1
    @JamesPatterson The answerer means adding a `p++;` or `++p;` statement before the close brace of your loop labelled `borrowLoop`. – bcsb1001 Jan 03 '15 at 18:15
  • 1
    @JamesPatterson i added the code for the increment (not endorsing the rest of the code :) – eckes Jan 03 '15 at 18:17
0

Your method does not have the correct return type. Change your return type to String and put the line with the Console.readString() outside the for loop. Hope that helps!

Hajmola
  • 93
  • 1
  • 6