0

I have a Java application with 4 Java classes which are Menu,LoanBook(superclass), FictionBook(extends loanBook), NonFictionBook (extends loanBook)

I'm stuck on a method - the purpose of the method is to issue a book from my array to a pupil. I aim to search for a book and add pupil details to that book. I cant work out where im going wrong, i just cant get my head around this method.

Please ignore any extra variables, but take into account everything else, any advice would be very helpful, if any more information is required..

My question is for somebody to look over this, tell me what's not needed and where i am going wrong. Is there an easier way to do this?

PS i have changed the below code from original, but with this i am getting a runtime error after i have added the name

static void issueBook() {
        int choice,x = 0;
        String title,name,date;
        boolean found = false;
        System.out.println("1.Fiction or 2.NonFiction?");
        choice = keyboard.nextInt();
        if (choice == 1){
            System.out.println("Please enter the title of the book to loan:");
            title = keyboard.next();
            System.out.println("Please enter your name:");
            name = keyboard.next();
            date = "todays date";
            do {
                if (fictionBooksArray[x].getTitle().equals(title)){
                    found = true;
                    fictionBooksArray[x].setOnLoan(true);
                    fictionBooksArray[location].setName(name);
                    fictionBooksArray[location].setDate(date);
                    System.out.println("Loan successful ");
                    }
                else x++;
            }
            while ((x < fictionBooksArray.length)&& (!found));
            if (!found){
                System.out.println("This book title could not be located.");
            }

        }
6
  • What's the actual question? Commented Dec 17, 2013 at 23:29
  • Try refactoring into a method that takes a LoanBook[] and a title, and returns the book in the array matching. That should make it more readable. Commented Dec 17, 2013 at 23:30
  • Hi, dave, i have updated this question. User60561, im not sure what you mean Commented Dec 17, 2013 at 23:33
  • public static LoanBook find(LoanBook[] books, String title). Returns the book with the given name that is not on loan already. It looks like both ends of the if statement are identical, so this would reduce duplication and make it easier to reason about. Commented Dec 17, 2013 at 23:39
  • 1
    from description is not clear what is the problem, in which class is the method … but is clear that incrementing of index in else branch is wrong. You are using the same variable for "for" loop and "do" "while" one. Actually I do not understand purpose of the "for" loop. Commented Dec 17, 2013 at 23:40

1 Answer 1

2

This seems like homework, so I won't post how to do this, but some general method structure might be like so:

// Returns the book if it exists in books, otherwise returns null
static LoanBook find(LoanBook[] books, String title)

// Prints the prompt to the console, returns whatever the user types next
static String getUserInput(String prompt)

// Takes the input title and name, tries to find the book in the array
// If it detects the find method has failed by returning null, it prompts
// the user for new information
static void takeOutBook(LoanBook[] books)

// The big method is much clearer now, this depends on the other methods to work
static void issueBook() {
    int choice = Integer.parseInt(getUserInput("1. Fiction: 2. Non Fiction:"));
    if (choice == 1) {
        takeOutBook(fictionBooksArray);
    } else if (choice == 0) {
        takeOutBook(nonfictionBookArray);
    }
}

Edit, full example as requested:

Implied code:

static class LoanBook {
    String getTitle() { return ""; }
    boolean isOnLoan() { return true; }
    void setOnLoan(boolean loan) { }
    void setDate(String d){ }
    void setName(String d){ }
}
static class FictionBook extends LoanBook { }
static class NonFictionBook extends LoanBook { }
static Scanner keyboard = new Scanner(System.in);
static FictionBook[] fictionBooksArray = new FictionBook[10];
static NonFictionBook[] nonfictionBookArray = new NonFictionBook[10];

Example code:

static void issueBook() {
    int choice = Integer.parseInt(getUserInput("1. Fiction: 2. Non Fiction:"));
    if (choice == 1) {
        takeOutBook(fictionBooksArray);
    } else if (choice == 0) {
        takeOutBook(nonfictionBookArray);
    }
}

static LoanBook find(LoanBook[] books, String title) {
    for (LoanBook book : books) {
        if (book.getTitle().equals(title) && !book.isOnLoan()) {
            return book; // When the book is found, exit the loop and return the book
        }
    }
    return null; // Returns null if book not found
}

static String getUserInput(String prompt) {
    System.out.println(prompt);
    return keyboard.next(); // You can then use Integer.parseInt(String param) to get the int value
}

static void takeOutBook(LoanBook[] books) {
    String title = getUserInput("Please enter title of the book to loan");
    String name = getUserInput("Please enter your name: ");
    String date = "???";

    LoanBook book = find(fictionBooksArray, title);
    if (book != null) {
        book.setOnLoan(true);
        book.setName(name);
        book.setDate(date);
    } else {
        System.out.println("The title has not been found, please try again");
        takeOutBook(books); // The method calls itself in an loop until the user inserts valid information
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

This is homework, but just a small part of a larger program, including write books, add books, load file, write file, search sort, display books onloan etc etc , so a more accurate answer would be helpful. i honestly think its my initial arrays which are the problems. Ive changed my code in the main question , please look
Where is location coming from? A stack trace might also help, as well as the source to your setName method.

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.