简体   繁体   English

使用“ add”方法将对象从单独的类添加到ArrayList

[英]Using an “add” method to add objects to an ArrayList from a separate class

I'm relatively new to Programming in general and I've been grafting over this piece of work for hours and haven't got any further. 总体而言,我对编程还比较陌生,并且在这方面的工作已经花了好几个小时而没有更多进展。 I've trawled through the depths of the internet and I cannot find an answer similar to what I am looking for. 我已经深入互联网的深处,找不到与我所寻找的答案相似的答案。 Here in the Spec: 在规格中:

A) Create two files FictionBook.java and Library.java. A)创建两个文件FictionBook.java和Library.java。 Define a FictionBook class that represents a single book. 定义代表一本书的FictionBook类。 The book will have a title, an author, an availability field (1=available, 0=on loan). 这本书将有一个标题,一个作者,一个可用性字段(1 =可用,0 =借用)。 You should define accessor methods to borrow and return the book and read the title and author and an accessor method to return the availability. 您应该定义访问器方法来借阅和返回书,阅读书名和作者,以及访问器方法来返回可用性。 Additionally, define a Library class that contains up to 200 books. 此外,定义一个库类,最多包含200本书。 The library should model containing books with an ArrayList. 该库应包含包含ArrayList的书籍的模型。 The Library class should contain a method to add a book to the library, delete a book from the library and borrow and return books. Library类应包含一种方法,该方法可将书添加到库中,从库中删除书以及借阅和还书。

B) Create a Librarian.java file and modify the Library.java file. B)创建一个Librarian.java文件并修改Library.java文件。 Write code to sort the messages in the Library so that all the books are in alphabetical order by Author's name. 编写代码以对库中的消息进行排序,以便所有书籍均按作者姓名的字母顺序排列。 Create a Librarian class with only a main method, so you can simulate the processing of books in the library. 仅使用main方法创建一个图书管理员类,因此您可以模拟图书馆中图书的处理。 Generate 10 new FictionBooks and add them to a Library using the addBook method. 生成10个新的FictionBook,并使用addBook方法将它们添加到库中。 Your Library should place the message in the correct place in the library depending on the name of the author. 您的图书馆应根据作者的姓名将消息放置在图书馆中的正确位置。

I think I have completed the first part, although I could be wrong. 我想我已经完成了第一部分,尽管我可能是错的。 It is the second part which I am complete stuck on. 这是我完全坚持的第二部分。 Here are my three classes 这是我的三个班

public class FictionBook {

    private String title, author;
    private int availability; 

    public FictionBook(String title, String author, int availability){
        super();
        this.title = title;
        this.author = author;
        this.availability = availability;
    }

    public FictionBook() {
        this.availability = 1;
    }

    public void borrowBook1() {
        setAvailability(0);
    }

    public void returnBook1() {
        setAvailability(1);
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public int getAvailability() {
        return availability;
    }

    public void setAvailability(int availability) {
        if(availability != 1 || availability != 0) {
            System.err.println("Enter 1 for available. Enter 2 for on loan.");
            throw new IndexOutOfBoundsException();
        } else {
            this.availability = availability;
        }
    }
}

import java.util.ArrayList;

public class Library {

    static ArrayList<FictionBook> BookList = new ArrayList<FictionBook>(200);

    public static void addBook(FictionBook String){
        BookList.add(String);
        System.out.println("Book Successfully Added To Library Database.");
        System.out.println(BookList);
    }

    public void deleteBook(){
        BookList.remove(index);
    }

    public void borrowBook(){
        BookList.get(index).FictionBook.borrowBook1();
    }

    public void returnBook(){
        BookList.get(index).FictionBook.returnBook1();  
    }

}

public class Librarian {

    public static void main(String args[]){

        FictionBook newBook1 = new FictionBook("USB Man", "Bob", 1);
        FictionBook newBook2 = new FictionBook("Bin Boys", "Chris", 1);
        FictionBook newBook3 = new FictionBook("Dinosaur", "Joe", 1);
        FictionBook newBook4 = new FictionBook("Pigasaurus", "Tom", 1);
        FictionBook newBook5 = new FictionBook("Cat Attack", "Calvin", 1);
        FictionBook newBook6 = new FictionBook("Shark Man", "Alfie", 1);
        FictionBook newBook7 = new FictionBook("Burnt Face Man", "Colin", 1);
        FictionBook newBook8 = new FictionBook("Egg Life", "Darwin", 1);
        FictionBook newBook9 = new FictionBook("Pizza King", "Pringle", 1);
        FictionBook newBook10 = new FictionBook("BillyBonka", "Randy", 1);

        Library.addBook();
    }
}

I am just wondering how I actually use the addBook(); 我只是想知道我如何实际使用addBook(); method in my Library class to add the objects defined in the Librarian class to the ArrayList in my Library class? 我的Library类中的方法是否将Librarian类中定义的对象添加到我的Library类中的ArrayList中? I've been messing around with the code a lot so there may be a lot of mistakes, apologies in advance. 我一直在弄乱代码,所以可能会出现很多错误,请提前道歉。 Any help would be super. 任何帮助都将是超级。 Thank you in advance for your time! 预先感谢您的宝贵时间!

Look at where you defined the addBook method: 查看您定义addBook方法的位置:

public static void addBook(FictionBook String)

this means that whenever you want to call addBook , you MUST include the name of the book (the name of the object of the FictionBook , not the literal title. And you must do this for each book since you're only doing them one at a time. 这意味着,每当您要调用addBook ,都必须包括书名( FictionBook对象的FictionBook ,而不是文字标题。)并且您必须为每本书进行此操作,因为您只能在一个时间。

so try this 所以试试这个

FictionBook newBook1 = new FictionBook("USB Man", "Bob", 1);
Library.addBook(newBook1);
FictionBook newBook2 = new FictionBook("Bin Boys", "Chris", 1);
Library.addBook(newBook2);

etc. etc. for each book you define 等您定义的每本书

also, like Compass said, naming a local variable "String" is not a good idea at all. 而且,就像Compass所说的那样,命名局部变量“ String”根本不是一个好主意。 I would rename it something like book because it's technically not the name of the book, it's the name of the object. 我将其重命名为book因为从技术上讲,它不是书的名称,而是对象的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM