2015-12-06 109 views
-2

我正在創建一個控制檯庫應用程序作爲大學項目的一部分。我已經建立了兩個對象,一個是書,另一個是期望ArrayList書的圖書館。添加到書籍的現有ArrayList時,我想將其添加到庫。用戶應該能夠在不創建庫的新實例的情況下添加新書,它應該添加到現有的書籍ArrayList而不是新的庫實例。不想每次都創建一個新的實例嗎?

下面是一些我一起工作的代碼,代碼的第一部分是剛剛從我的書類的構造函數,第二個是從圖書館類:

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

我有一種感覺,我會被要求添加一個庫的新實例,但這是我最終想避免的。如果需要其他代碼,我將很樂意提供。

+0

您尚未將代碼發佈到您的Book類或您的Library類,並且您發佈的所有方法都是靜態的,即沒有OOP。如果這是班級的一部分,我強烈建議與您的老師交談......我不相信您瞭解OOP – ControlAltDel

+0

@ControlAltDel的基礎知識,感謝您的反饋。我現在已經包括書和圖書館課。如果有幫助。 –

回答

0

我認爲你沒有正確的思考這個問題。從你的描述中,你可能應該添加一個方法到Library類中,該類有一個書籍數組列表作爲參數。然後,您應該使用一個循環遍歷此數組列表,將此數組中的每本書添加到Library類已具有的數組列表中。通過你的HelperUlitlies課閱讀,我不確定你爲什麼以這種方式使用這門課。圖書館應該有一種方法將書籍添加到書架上,你不應該爲此使用外部類。這可能是一個好主意,有一個管理圖書館類的類,接受用戶輸入並實例化圖書館類的新圖書列表添加。

+0

由於該程序將用於演示,我們需要設置一個類,以便我們快速添加元素。但是,我覺得這是問題所在,因此造成混亂。 –

相關問題