我正在创建一个控制台库应用程序作为大学项目的一部分。我已经建立了两个对象,一个是书,另一个是期望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的基础知识,感谢您的反馈。我现在已经包括书和图书馆课。如果有帮助。 –