将数组与子类链接

时间:2013-12-19 21:58:09

标签: java arrays inheritance collections arraylist

我当前的程序是一个学生图书馆系统,我有我的数组列表,菜单和方法都可以工作。我的问题是我需要数组从超类LoanBook中读取,它接受子类(Fiction和NonFiction)的覆盖。

从AddBook方法中可以看出,它将详细介绍了书籍和存储到数组列表中。

我的问题: 我需要添加选项Fiction或Non-Fiction但我需要arraylist从Superclass和SubClasses中获取属性。我可以帮忙吗。

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

主类

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 LoanBook {


    private int bookID;
    private String title,author,name,date;
    boolean onLoan;
    private static int count = 0;
    static List<String> bookTitle = new ArrayList<String>();
    static List<String>bookAuthor = new ArrayList<String>();
    static List<String> pupilName = new ArrayList<String>();
    static List<String>issueDate = new ArrayList<String>();
    static List bookOnloan = new ArrayList<Boolean>();

    public LoanBook(String title,String author){  //constructor
        this.bookID = count;
        this.author = author;
        this.title = title;
        bookOnloan.add(false);
        count++;
    }



    public void setTitle(String title){
        bookTitle.set(1,title);

    }
    public String getTitle(){
        return  bookTitle.toString();
    }

    public void setAuthor(String author){
        bookTitle.set(1,author);

    }
    public String getAuthor(){
        return  bookAuthor.toString();
    }
    public String getName(){
        return pupilName.toString();
    }
    public void setName(String name){
           pupilName.set(1,name);
    }
    public String getDate(){
        return issueDate.toString();
    }
    public void setDate(String date){
        issueDate.set(1,date);
    }

    public Boolean getOnloan(){
        return bookOnloan.add(false);
    }
    public void setOnLoan(Boolean onLoan){
        bookOnloan.add(false);

    }

}

接下来我的子类

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

1 个答案:

答案 0 :(得分:1)

您的整个程序结构从您对静态字段的过度使用到您对继承的误用,以及将Book和Book集合的概念组合在一个类中都被打破了。

建议:

  • 请勿将Book类与Book集合混合使用。这看起来是您的代码的主要问题。
  • 从Book类开始。它根本不应包含任何列表。
  • 如果需要,您可以使用FictionBook和NonFictionBook扩展Book。
  • 或者你可以简单地给Book一个布尔字段,小说,并根据需要将其设置为true或false。
  • 创建一个包含书籍列表的LoanBook类。
  • 除非存在真正的“is-a”关系,否则不要使用继承。你的代码不满足这个主要是由于你的第一个问题,你将Book类与Book Library代码混合在一起,这迫使你的小说书和你的非小说书继承了不仅不需要的库代码,而且实际上是有害的
  • 避免使用静态任何东西,除非它们用于特定的静态目的。
  • 通过废弃当前代码并重新开始,您可能会得到最好的服务。