Add data to superclass and array using method

206 views Asked by At

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.");
            }

        }
1

There are 1 answers

2
flaviut On BEST ANSWER

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
    }
}