是否可以修改我的代码以将数据保存到相同的数组列表?

问题描述 投票:-4回答:1

我想创建一个带有子类的超类,并一次调用我的文本文件,也希望将数据保存在同一arraylist中。我第一本书的代码是

public class LiteraryBook {

    public String Li_Title;
    public String Li_Author;
    public String Li_Isbn;
    public String Li_Date;
    public String Li_Kind;

    public LiteraryBook(String Li_Title, String Li_Author, String Li_Isbn, String Li_Date, String Li_Kind) {
        this.Li_Title = Li_Title;
        this.Li_Author = Li_Author;
        this.Li_Isbn = Li_Isbn;
        this.Li_Date = Li_Date;
        this.Li_Kind = Li_Kind;
    }

    public String getLi_Title() {
        return Li_Title;
    }

    public String getLi_Author() {
        return Li_Author;
    }

    public String getLi_Isbn() {
        return Li_Isbn;
    }

    public String getLi_Date() {
        return Li_Date;
    }

    public String getLi_Kind() {
        return Li_Kind;
    }

    @Override
    public String toString() {
        String s;
        s = Li_Title + "\n";
        s = s + Li_Author + "\n";
        s = s + Li_Isbn + "\n";
        s = s + Li_Date + "\n";
        s = s + Li_Kind + "\n";
        return s;
    }
}

我第二本书的代码是

public class ScientificBook {

    public String sci_title;
    public String sci_author;
    public String sci_isbn;
    public String sci_date;
    public String sci_kind;
    public String sci_field;

    public ScientificBook(String sci_title, String sci_author, String sci_isbn, String sci_date, String sci_kind, String sci_field) {
        this.sci_title = sci_title;
        this.sci_author = sci_author;
        this.sci_isbn = sci_isbn;
        this.sci_date = sci_date;
        this.sci_kind = sci_kind;
        this.sci_field = sci_field;
    }

    public String getSci_Title() {
        return sci_title;
    }

    public String getSci_Author() {
        return sci_author;
    }

    public String getSci_Isbn() {
        return sci_isbn;
    }

    public String getSci_Date() {
        return sci_date;
    }

    public String getSci_Kind() {
        return sci_kind;
    }

    public String getSci_field() {
        return sci_field;
    }

    @Override
    public String toString() {
        String s;
        s = sci_title + "\n";
        s = s + sci_author + "\n";
        s = s + sci_isbn + "\n";
        s = s + sci_date + "\n";
        s = s + sci_kind + "\n";
        s = s + sci_field + "\n";
        return s;
    }
}

在下面的代码中,我为第一本书创建一个数组列表,并将数据写入文本文件

public class LiteraryBookList {

    private ArrayList<LiteraryBook> LibookList;

    public LiteraryBookList() {
        LibookList = new ArrayList<LiteraryBook>();
    }

    public void add_li(LiteraryBook lb) {
        LibookList.add(lb);
    }

    public ArrayList<LiteraryBook> getLiBooks() {
        return LibookList;
    }

    public void readFromTxtLi(String filename) {
        File file = new File(filename);
        FileReader reader = null;
        try {
            reader = new FileReader(file);
        } catch (FileNotFoundException e) {
            System.exit(1);
        }
        BufferedReader infile = new BufferedReader(reader);
        String line = "";
        int counter = 0;
        String Li_Title = "";
        String Li_Author = "";
        String Li_Isbn = "";
        String Li_Date = "";
        String Li_Kind = "";
        String Li_field = "";

        try {
            while ((line = infile.readLine()) != null) {
                if(line.isEmpty() || line.contains("ΛΟΓΟΤΕΧΝΙΚΟ"))
                    continue;

                counter++;
                if (counter == 1) {
                    Li_Title = line;
                } else if (counter == 2) {
                    Li_Author = line;
                } else if (counter == 3) {
                    Li_Isbn = line;
                } else if (counter == 4) {
                    Li_Date = line;
                } else if (counter == 5) {
                    Li_Kind = line;

                    LiteraryBook lb = new LiteraryBook(Li_Title, Li_Author, Li_Isbn, Li_Date, Li_Kind);
                    LibookList.add(lb);
                    counter = 0;
                }
            }
        } catch (IOException ex) {
            Logger.getLogger(LiteraryBookList.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public Object[][] convert2Data() {
        Object[][] data = new Object[LibookList.size()][5];
        for (int i = 0; i < LibookList.size(); i++) {
            data[i][0] = LibookList.get(i).getLi_Title();
            data[i][1] = LibookList.get(i).getLi_Author();
            data[i][2] = LibookList.get(i).getLi_Isbn();
            data[i][3] = LibookList.get(i).getLi_Date();
            data[i][4] = LibookList.get(i).getLi_Kind();

        }
        return data;
    }

}

也用于第二本书 公共类ScientificBookList {

    private ArrayList<ScientificBook> ScibookList;

    public ScientificBookList() {
        ScibookList = new ArrayList<ScientificBook>();
    }

    public void add_sci(ScientificBook sb) {
        ScibookList.add(sb);
    }

    public ArrayList<ScientificBook> getSciBooks() {
        return ScibookList;
    }

    public void readFromTxtSci(String filename) {
        File file = new File(filename);
        FileReader reader = null;
        try {
            reader = new FileReader(file);
        } catch (FileNotFoundException e) {
            System.exit(1);
        }
        BufferedReader infile = new BufferedReader(reader);
        String line = "";
        int counter = 0;
        String Sci_Title = "";
        String Sci_Author = "";
        String Sci_Isbn = "";
        String Sci_Date = "";
        String Sci_Kind = "";
        String Sci_field = "";

        try {
            while ((line = infile.readLine()) != null) {
                if (line.isEmpty() || line.contains("ΕΠΙΣΤΗΜΟΝΙΚΟ")) {
                    continue;
                }

                counter++;
                if (counter == 1) {
                    Sci_Title = line;
                } else if (counter == 2) {
                    Sci_Author = line;
                } else if (counter == 3) {
                    Sci_Isbn = line;
                } else if (counter == 4) {
                    Sci_Date = line;
                } else if (counter == 5) {
                    Sci_Kind = line;
                } else if (counter == 6) {
                    Sci_field = line;

                    ScientificBook sb = new ScientificBook(Sci_Title, Sci_Author, Sci_Isbn, Sci_Date, Sci_Kind, Sci_field);
                    ScibookList.add(sb);
                    counter = 0;

                }

            }

        } catch (IOException ex) {
            Logger.getLogger(LiteraryBookList.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public Object[][] convert2Data() {
        Object[][] data = new Object[ScibookList.size()][6];
        for (int i = 0; i < ScibookList.size(); i++) {
            data[i][0] = ScibookList.get(i).getSci_Title();
            data[i][1] = ScibookList.get(i).getSci_Author();
            data[i][2] = ScibookList.get(i).getSci_Isbn();
            data[i][3] = ScibookList.get(i).getSci_Date();
            data[i][4] = ScibookList.get(i).getSci_Kind();
            data[i][5] = ScibookList.get(i).getSci_field();

        }
        return data;
    }
}
java arraylist
1个回答
0
投票

我认为您应该考虑采用特定的设计模式,这将使您的代码易于维护。

查看代码后,我觉得您应该尝试使用工厂方法模式和/或策略模式。

© www.soinside.com 2019 - 2024. All rights reserved.