java中的书店软件:基本类 - 需要APCS的帮助

时间:2014-05-19 23:49:52

标签: java arraylist constructor initialization

我正在尝试制作的程序有问题。当我实现一个arraylist时,它给了我不同的错误。我现在非常困惑如何初始化类型书的ArrayList。

这是Book类:

public class Book {

    private double myPrice;
    private String myTitle;
    private String bookAuthor;
    private String isbn;
    private int myCopies;   

    public Book(double price, int copies, String bookTitle, String Author, String isbnNumber) {
        myPrice = price;
        myCopies = copies;
        myTitle = bookTitle;
        bookAuthor = Author;
        isbn = isbnNumber;
    }

    public double getPrice() {
        return myPrice;
    }

    public String getIsbn() {
        return isbn;
    }

    public String getTitle() {
        return myTitle;
    }

    public String getAuthor() {
        return bookAuthor;
    }

    public int copiesLeft(){
        return myCopies;
    }

    public String toString() {
        return "Title: " + getTitle() + "\nAuthor: " + getAuthor()
               + "\nNumber of Available Books: " + copiesLeft() 
               + "\nPrice: $" + getPrice(); 
    }

}

这是库存类:

import java.util.ArrayList;

public class Inventory extends Book {
    private ArrayList<Book> allBooks = new ArrayList<Book>;
    private String customerName;

    public Inventory() {
        super();
    }

    //@param double price, int copies, String bookTitle, String Author, String isbnNumber
    public void addBooks() {
        allBooks.add(new Book(4.99, 6, "A Tale of Two Cities", "Charles Dickens", "9781783220731"));
    }

    public boolean isAvailable() {
        for(Book myBook : allBooks) {
            if(myBook.copiesLeft() == 0)
                return false;
            else
                return true;    
        }
    }

    public Book getBookByTitle(String titleSearch) {
        for (Book myBook : allBooks) {
            if (titleSearch.equals(myBook.getTitle()));
                return myBook;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

我不知道你得到了哪些错误,因此帮助你有点困难。但是这里有一些代码中的错误。它们标有//&lt; ----

import java.util.ArrayList;

public class Inventory extends Book {
    private ArrayList<Book> allBooks = new ArrayList<Book>; //<---- Should be new ArrayList<Book>(); that is, with parentheses.
    private String customerName;

    public Inventory() {
        super(); //<---- I'm rusty on inheritance, but this line** might fuck up some stuff
    }

    //@param double price, int copies, String bookTitle, String Author, String isbnNumber
    public void addBooks() {
        allBooks.add(new Book(4.99, 6, "A Tale of Two Cities", "Charles Dickens", "9781783220731"));
    }

    public boolean isAvailable() {
        for(Book myBook : allBooks) {
            if(myBook.copiesLeft() == 0)
                return false;
            else
                return true;    
        }
            //<---- You need a second return statement, in case the for-loop and if statement is never accessed. The method always needs to return something when you've told it to.
    }

    public Book getBookByTitle(String titleSearch) {
        for (Book myBook : allBooks) {
            if (titleSearch.equals(myBook.getTitle())); //<---- Semicolon
                return myBook;
        }
            //<---- You need a second return statement, in case the for-loop and if statement is never accessed. The method always needs to return something when you've told it to.
    }
}

如果您可以发布主要方法和您获得的具体错误,那么帮助会更容易。

另外,正如艾略特指出的那样;我不确定为什么你用书来扩展班级库存。

**我可能在这里错了;当调用super()(在本例中是Books(?)中的构造函数)时,由于缺少参数,可能会产生错误。此方法调用可能不应该在此类中,因为Inventory可能不应该扩展Book。