简体   繁体   English

如何使用类定义的数组并向其添加值(预定义的参数)?

[英]How do I use an class defined array and add values (pre-defined parameters) to it?

so what I did was create 3 classes: 1. Library Class (main) 2. Book Class (which I created an object instance of it on the main class) 3. LibraryException (just to handle exceptions) 所以我要做的是创建3个类:1.库类(主)2.书本类(我在主类上创建了它的对象实例)3. LibraryException(仅用于处理异常)

However I am puzzled, I cannot figure out how to use the Array with the parameters / settings set in the Book class, here is my program: 但是我很困惑,我无法弄清楚如何使用Book类中设置的参数/设置使用数组,这是我的程序:

public class Library
{
    String name;
    Book[] books;
    int nrBookz = 0;

    public Library(String name, int nrBookz) throws LibraryException {  
        if(nrBookz < 500){
            throw new LibraryException();
        }
        this.name = name;
        this.nrBookz = nrBookz;


    }


    public void addBook(Book Book) throws LibraryException {
        if(indexOf(Book) == -1){
            if(nrBookz < books.length){
                books[nrBookz++] = Book;
            }
        }
    }


    private int indexOf(Book Book)throws LibraryException {
        for(int i = 0; i < books.length; i++){
            if(books[i] == null){
                throw new LibraryException("That book does not exist!");
            }

            else if(books[i].equals(Book)){
                return i;
            }
        }
        return -1;
    }

    public static void main(String[]args) throws LibraryException{
        Library b = new Library("Sami Frasheri", 750);
        **b.addBook(new Book("Paul Colhen - Alchemist", 138));
        b.addBook(new Book("Paul Colhen - Winners Stand ALone", 139));
        b.addBook(new Book("Paul Colhen - The river Piedra I sat and I cried", 140));**
    }
}

.

public class Book {
    String name;
    int isbn;


    public Book(String e, int iNr) throws LibraryException{
        if(e == ""){
            throw new LibraryException("Book name is not allowed to be blank / empty!!");
        }

        else if(iNr < 1 || iNr > 9000){
            throw new LibraryException("The isbn is not within the allowed range (1 - 9000)! ");
        }
        name = e;
        isbn = iNr;
    }


    public boolean equals(Object obj){
        if(obj instanceof Book){
            Book p = (Book)obj;
            return name.toLowerCase().equals(p.getName().toLowerCase());
        }
        return false;
    }



    public String getName() {
        if(name == null || name == ""){
            System.out.print("name (Book) EXCEPTION TO BE ADDED!");
        }
        return name;
    }

    public void setname(String name) {
        this.name = name;
    }

    public int getIsbn() {
        if(isbn < 0){
            System.out.print("ISBN (Book) EXCEPTION TO BE ADDED!");
        }
        return isbn;
    }

    public void setIsbn(int isbn) {
        this.isbn = isbn;
    }

}

So my question is: How can I add (books) or make it possible to be added to the book array? 所以我的问题是:如何添加(书籍)或使其可以添加到书籍数组中? (see blow - after I create the new Library (object of the class Library) *Marked with bold on the main method (Library class) (请参见打击-在创建新的Library(类Library的对象)之后*在主方法(Library类)上标记为粗体

You are missing the array initialization : 您缺少数组初始化:

public Library(String name, int nrBookz) throws LibraryException {  
    if(nrBookz < 500){
        throw new LibraryException();
    }
    this.name = name;
    this.nrBookz = nrBookz;
    this.books = new Book[nrBookz]; // add this

}

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

相关问题 如何在Java中将预定义值添加到Pair Hashmap - How to add pre-defined values to Pair Hashmap in Java 如果元组是一个预定义的类而没有Java中的实现比较器,则如何对元组数组进行排序 - How to sort a array of tuple if the tuple is a pre-defined class without implement comparator in Java 当我尝试将新列表 object 添加到预定义列表 object 时出现错误 - Getting error when I am trying to add a new List object to a pre-defined List object 如果类是预定义的,则实例变量将充当对象 - instance variable acts an object , if class is pre-defined 如何使用一次只需要 2 个 arrays 的预定义方法将 2D 数组合并为一个? - How to combine a 2D array into one, using a pre-defined method that only takes 2 arrays at a time? 我如何退出while循环? 基于我在Java中的预定义倒计时 - How can i exit from a while loop? Based on my pre-defined count down in Java 如何按我的预定义顺序对集合进行排序? - How to sort collection in my pre-defined order? 如何在包外部修改预定义的包方法? - How to modify a pre-defined package methods outside of the package? jdb:在断点处执行预定义的操作 - jdb : Perform a pre-defined action at a breakpoint 为Java运行预定义的输入脚本 - running a pre-defined input script for java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM