【问题标题】:Add data to superclass and array using method使用方法将数据添加到超类和数组
【发布时间】:2013-12-17 23:25:01
【问题描述】:

我有一个包含 4 个 Java 类的 Java 应用程序,它们是 Menu,LoanBook(超类), FictionBook(扩展loanBook), NonFictionBook(扩展loanBook)

我被困在一种方法上——该方法的目的是从我的阵列中向学生发行一本书。我的目标是搜索一本书并将学生详细信息添加到该书中。我无法弄清楚我哪里出错了,我只是无法理解这种方法。

请忽略任何额外的变量,但要考虑其他所有因素,如果需要更多信息,任何建议都会非常有帮助..

我的问题是让某人检查一下,告诉我不需要什么以及我哪里出错了。有没有更简单的方法来做到这一点?

PS 我已经从原始代码更改了以下代码,但是在添加名称后我得到了运行时错误

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

        }

【问题讨论】:

  • 真正的问题是什么?
  • 尝试重构为一个接受 LoanBook[] 和标题的方法,并返回匹配数组中的书籍。这应该使它更具可读性。
  • 嗨,戴夫,我已经更新了这个问题。 User60561,我不确定你的意思
  • public static LoanBook find(LoanBook[] books, String title)。返回尚未借出的具有给定名称的书。看起来 if 语句的两端是相同的,因此这将减少重复并使其更易于推理。
  • 从描述中不清楚是什么问题,在哪个类中是方法……但很明显,在 else 分支中增加索引是错误的。您对“for”循环和“do”“while”使用相同的变量。其实我不明白“for”循环的目的。

标签: java arrays superclass subclassing


【解决方案1】:

这似乎是作业,所以我不会发布如何做到这一点,但一些通用的方法结构可能是这样的:

// 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);
    }
}

根据要求编辑完整示例:

隐含代码:

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];

示例代码:

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

【讨论】:

  • 这是一个家庭作业,但只是一个更大的程序的一小部分,包括写书、添加书、加载文件、写文件、搜索排序、显示借书等,所以更准确的答案会有帮助。老实说,我认为这是我最初的数组问题。我在主要问题中更改了我的代码,请查看
  • location 来自哪里?堆栈跟踪以及 setName 方法的来源也可能会有所帮助。
猜你喜欢
  • 2021-10-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-09
  • 1970-01-01
  • 2014-01-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多