Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 用子类链接数组_Java_Arrays_Inheritance_Collections_Arraylist - Fatal编程技术网

Java 用子类链接数组

Java 用子类链接数组,java,arrays,inheritance,collections,arraylist,Java,Arrays,Inheritance,Collections,Arraylist,我目前的计划是一个学生图书馆系统,我有我的数组列表,菜单和所有工作的方法。我的问题是我需要数组从超类LoanBook读取,它接受子类(虚构和非虚构)的重写 正如您从AddBook方法中看到的那样,它接收书籍的详细信息并将其存储到数组列表中 我的问题: 我需要添加虚构或非虚构选项,但我需要arraylist从超类和子类中获取属性。我能得到一些帮助吗 我很乐意回答您的任何问题或提供更多信息 主类 import java.io.FileNotFoundException; import java.io

我目前的计划是一个学生图书馆系统,我有我的数组列表,菜单和所有工作的方法。我的问题是我需要数组从超类LoanBook读取,它接受子类(虚构和非虚构)的重写

正如您从AddBook方法中看到的那样,它接收书籍的详细信息并将其存储到数组列表中

我的问题: 我需要添加虚构或非虚构选项,但我需要arraylist从超类和子类中获取属性。我能得到一些帮助吗

我很乐意回答您的任何问题或提供更多信息

主类

import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Formatter;
import java.util.List;
import java.util.Scanner;

public class Main{
    static Scanner keyboard = new Scanner(System.in);
    static boolean run = true;
    static Formatter x;


    public static void main(String[]args){

        LoanBook myBook = new LoanBook();

        while (run){                    // this while statement allows the menu to come up again
            int answer = 0;
            boolean isNumber;
            do{                              // start of validation
                System.out.println("1.  Add book");
                System.out.println("2.  Display the books available for loan");
                System.out.println("3.  Display the books currently on loan");
                System.out.println("4.  Make a book loan");
                System.out.println("5.  Return book ");
                System.out.println("6   Write book details to file");
                if (keyboard.hasNextInt()){                       // I need to consider putting in a =>1 <=6
                    answer = keyboard.nextInt();
                    isNumber = true;
                }   else {
                    System.out.print(" You must enter a number from the menu to continue. \n");
                    isNumber = false;
                    keyboard.next(); // clears keyboard

                }
            }
            while (!(isNumber));
            switch (answer){

                case 1:
                    addBook();
                    break;
                case 2:
                    viewAll();
                    break;
                case 3:
                    booksOnLoan();
                    break;
                case 4:
                    loanBook();
                    break;
                case 5:
                    returnBook();
                    break;
                case 6:
                    writeToFile();
                    break;
                case 7:
                    break;
            }
        }
    }
    static List<String>pupilName = new ArrayList<String>();
    static List<String>issueDate = new ArrayList<String>();
    static List<String>bookTitle = new ArrayList<String>();
    static List<String>bookAuthor = new ArrayList<String>();
    static List bookOnloan = new ArrayList<Boolean>();
    public static void viewAll(){
        System.out.println("\n");
        for (int x = 0; x < bookTitle.size();x++){
            int counter = x+1;
            System.out.println("BookID:" +counter + "\n " + bookTitle.get(x) + " - " + bookAuthor.get(x)+" " + bookOnloan.get(x));
        }
    }
    public static void booksOnLoan(){
        System.out.println("\n");
        for (int x = 0; x < pupilName.size();x++){
            if (bookOnloan.contains(true)){
                int counter = x+1;
            System.out.println("BookID:" +counter + "\n "+"Pupil name: " + pupilName.get(x)
                    +"\n Book Title: "+ bookTitle.get(x) + " by " + bookAuthor.get(x)+" " + bookOnloan.get(x)+ "\n Issued: "+ issueDate.get(x)) ;
            }
        }
    }



    public static void addBook(){

        System.out.println("Please enter the book title: ");
        String newTitle = keyboard.next();
        bookTitle.add(newTitle);
        System.out.println("Please enter the book author");
        String newAuthor = keyboard.next();
        bookAuthor.add(newAuthor);
        bookOnloan.add(false);
        System.out.println("\n Your book: "+ bookTitle.get(bookTitle.size()-1)+ " has been added to the library" + "\n");
    }

    public static void loanBook(){
        viewAll();
        System.out.println("Please choose the BookID you would like to issue: ");
        int issue = keyboard.nextInt()-1;
        if (issue > 10){
            System.out.println("Invalid book selection");
        }
        else {
            bookOnloan.set(issue,true);
            System.out.println("Please enter pupil name: ");
            String newPupil = keyboard.next();
            pupilName.add(newPupil);
            System.out.println("Please enter date of issue: ");
            String newIssue = keyboard.next();
            issueDate.add(newIssue);
        }
    }
    public static void returnBook(){
       // booksOnLoan();
        System.out.println("Please choose the BookID you would like to return: ");
        int issue = keyboard.nextInt()-1;
        if (issue > 10){
            System.out.println("Invalid book selection");
        }
        else {
            bookOnloan.set(issue,false);
和其他子类

public class NonFiction extends LoanBook {

    private String type;

    public NonFiction(){

    }


    public NonFiction(String title,String author, String type){
        super(title,author);                    //calls constructor of the superclass
        this.type = type;
    }

    public void setType(String type){
        type = "Fiction";
    }
    public String getType(){
        return type;
    }

    public String toString(){
        return super.toString() + " The book type is: " + getType()+"\n";
    }
}

您的整个程序结构被打破,从过度使用静态字段到错误使用继承,再到在一个类中结合书籍和书籍集合的概念

建议:

  • 不要把你的书本课和你的藏书混在一起。这似乎是代码的主要问题
  • 从书本课开始。它不应该包含任何列表
  • 如果需要的话,您可以让虚构图书和非虚构图书扩展图书
  • 或者,您可以简单地给Book一个布尔字段“虚构”,并根据需要将其设置为true或false
  • 创建一个包含书籍列表的LoanBook类
  • 除非存在真正的“is-a”关系,否则不要使用继承。您的代码不能满足这一点主要是因为您的第一个问题,您将Book类与Book Library代码混合在一起,这迫使您的小说书和非小说书继承库代码,这不仅不需要,而且非常有害
  • 避免使用任何静态对象,除非它们用于特定的静态目的
  • 最好的办法是扔掉当前的代码,重新开始

一分钟内要理解的代码太多了,但是
LoanBook
中的静态列表看起来非常错误。5美元,他们造成了你的问题。一个好的建议是改变你对LoanBook类的设计。有人能纠正我的LoanBook类,这样我就可以从那里开始工作吗?
“有人能纠正我的LoanBook类,这样我就可以从那里开始工作吗?”
--不,这不是“为我修复代码网站”。我已经给了你一些关于你应该做什么的建议,我建议你注意它们。如果你在实施它们时遇到了困难,那么就提出问题,但请不要再让任何人为你做工作。这不是你学习的方式,也不是这个网站或任何知名网站的工作方式。你的建议要求我重新启动我的整个代码,我已经为此工作了很多年,明天就到期了。我从你的个人资料中看到你自学成才,那么我怎么能同意你的答案呢?
public class Fiction extends LoanBook {

    private String type;

    public Fiction(){

    }


    public Fiction(String title,String author, String type){
        super(title,author);                    //calls constructor of the superclass
        this.type = type;
    }

    public void setType(String type){
        type = "Fiction";
    }
    public String getType(){
        return type;
    }

    public String toString(){
        return super.toString() + " The book type is: " + getType()+"\n";
    }
}
public class NonFiction extends LoanBook {

    private String type;

    public NonFiction(){

    }


    public NonFiction(String title,String author, String type){
        super(title,author);                    //calls constructor of the superclass
        this.type = type;
    }

    public void setType(String type){
        type = "Fiction";
    }
    public String getType(){
        return type;
    }

    public String toString(){
        return super.toString() + " The book type is: " + getType()+"\n";
    }
}