简体   繁体   中英

I'm trying to test the parametrized construtor and the toString and it won't work

I need to test the parametrized constructor and I don't understand the problem why it won't work. I have error in the main method

public class Book {

    private String isbn;
    private int numberOnShelf;


    public void setNumberOnShelf(int numberOnShelf) {
        this.numberOnShelf = numberOnShelf;
    }

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

    public int getNumberOnShelf() {
        return numberOnShelf;
    }

    public String getIsbn() {
        return isbn;
    }

    public void addToNumberOnshelf(int numInShipment) {
        if (numInShipment > 0) {
            numberOnShelf += numInShipment;
        }
    }
     @Override
    public String toString() {
        return String.format("%-15s%5d", isbn, numberOnShelf);
    }

    public static void main(String[] args) {
        Book book = new Book("1234567890123",5);
        System.out.println(book);
        book.addToNumberOnshelf(4);
        System.out.println(book);
    }
}

Add to your class this code:

public Book(String isbn, int numberOnShelf) {
    this.isbn = isbn;
    this.numberOnShelf = numberOnShelf;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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