我正在創建一個控制檯庫應用程序作爲大學項目的一部分。我已經建立了兩個對象,一個是書,另一個是期望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.");
}
我有一種感覺,我會被要求添加一個庫的新實例,但這是我最終想避免的。如果需要其他代碼,我將很樂意提供。
您尚未將代碼發佈到您的Book類或您的Library類,並且您發佈的所有方法都是靜態的,即沒有OOP。如果這是班級的一部分,我強烈建議與您的老師交談......我不相信您瞭解OOP – ControlAltDel
@ControlAltDel的基礎知識,感謝您的反饋。我現在已經包括書和圖書館課。如果有幫助。 –