不想每次都创建一个新实例?

时间:2015-12-06 04:12:12

标签: java arrays arraylist console-application

我正在创建一个控制台库应用程序作为大学项目的一部分。我已经设置了两个对象,一个是书,另一个是期望书籍的ArrayList的库。当添加到现有的书籍ArrayList时,我想将它添加到库中。用户应该能够在不创建库的新实例的情况下添加新书,它应该添加到现有的书籍ArrayList而不是新的库实例。

以下是我正在使用的一些代码,第一段代码只是我book类中的构造函数,第二段是来自库类:

    // Initialising variables
private String title, author;
private int bookID, timesLoaned;
private boolean onLoan;

// Setting up the constructor 
public Book(String title, String author, int bookID, int timesLoaned, boolean onLoan) {
    this.title = title;
    this.author = author;
    this.bookID = bookID;
    this.timesLoaned = timesLoaned;
    this.onLoan = onLoan;
}

库类构造函数:

    // Initialising variables
int ID;
String name, address;
String phoneNumber;
ArrayList<Book> newBook = new ArrayList<Book>();

// Setting up the constructor 
public Library(int ID, String name, String address, String phoneNumber, ArrayList<Book> newBook) {
    this.ID = ID;
    this.name = name;
    this.address = address;
    this.phoneNumber = phoneNumber;
    this.newBook = newBook;
}

以下代码位于名为HelperUtilities的类中:

    public static ArrayList<Book> createBook(){
    // Book needs title, author, category, bookID, times loaned, onLoan
    String[] title = {"Harry Potter and the Goblet of Fire", "The Fault in Our Stars", 
            "Vampire Acadamy", "The Perks of Being a Wallflower", "Thirteen Reasons Why", 
            "The Maze Runner", "Looking for Alaska", "The Hunger Gamers"};

    String[] author = {"J.K. Rowling", "John Green", "Richelle Mead", "Stephen Chobsky", 
            "Jay Asher", "James Dashner", "John Green", "Suzanne Collins"};

    int[] bookID = new int[8];

    // Temporary values
    String tName = "", tAuthor = "";
    int tID = 0;

    ArrayList<Book> book = new ArrayList<Book>();
    for(int i = 0; i < 8; i++){
        tName = title[i];
        tAuthor = author[i];
        bookID[i] = (int) Math.round(Math.random() * 1000000);
        tID = bookID[i];

        book.add(new Book(tName, tAuthor, tID, 0, false));
    }

    return book;
}

public static ArrayList<Library> createLibrary(){
    String name = "Draperstown", address = "Magherafelt", phone = "000";
    int id = (int) Math.round(Math.random() * 1000000);

    ArrayList<Library> library = new ArrayList<Library>();
    for(int i = 0; i < 1; i++) {
        library.add(new Library(id, name, address, phone, createBook()));
    }

    return library;
}

您将看到的下一个方法是addBook方法,该方法应该创建一本书的新实例并将其添加到书籍的ArrayList中,然后将其添加到现有的库中,这就是我遇到的问题。

    public static void addBook(Scanner sc){
    // Books
    // Variables for use in addBook
    int bookID = (int) Math.round(Math.random() * 1000000); // Generates random book ID

    // User input
    System.out.print("Enter a title for the book: ");
    String title = sc.nextLine();

    System.out.print("Enter an author for the book: ");
    String author = sc.nextLine();

    // ArrayList holds values of user input for new book
    ArrayList<Book> newBook = new ArrayList<Book>();
    newBook.add(new Book(title, author, bookID, 0, false));

    // Takes existing ArrayList from HelperUtilities
    ArrayList<Book> existBook = new ArrayList<Book>();
    existBook = HelperUtilities.createBook();

    // Joins both ArrayLists together
    ArrayList<Book> addBook = new ArrayList<Book>();
    addBook.addAll(existBook);
    addBook.addAll(newBook);

    System.out.println("Book '" + title + "' has been added.");
}

我有一种感觉,我将需要添加一个新的库实例,但这是我最终要避免的。如果需要任何其他代码,我将很乐意提供。

1 个答案:

答案 0 :(得分:0)

我认为你没有以正确的方式思考这个问题。根据您的描述,您可能应该向Library类添加一个方法,该类具有书籍的arraylist作为参数。然后,您应该使用循环来循环遍历此arraylist,将此arraylist中的每本书添加到Library类已有的arraylist中。通过你的HelperUlitlies课程阅读,我不确定你为什么按照自己的方式使用这门课程。图书馆应该有一种方法来将书籍添加到书架上,你不应该使用外部类。拥有一个管理库类的类,获取用户输入并实例化库类的新arraylists以供添加库类可能是个好主意。