Software di libreria in java: classi di base-hai bisogno di aiuto per APC [chiuso]


Sto avendo un problema con il programma che sto cercando di fare. Quando implemento un arraylist, mi sta dando diversi errori. Sono molto confuso in questo momento su come inizializzare il mio ArrayList di tipo book.

Ecco la classe del libro:

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(); 
    }

}

Ed ecco la classe di inventario:

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;
        }
    }
}
Author: Einar, 2014-05-20

1 answers

Non conosco quali errori hai, e quindi è un po ' difficile aiutarti. Ma ecco alcuni degli errori nel tuo codice. Sono contrassegnati con //

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.
    }
}

Sarebbe più facile aiutare se potessi pubblicare il metodo principale e gli errori specifici che ottieni.

Inoltre, come sottolineato da Elliott; Non sono sicuro del motivo per cui hai esteso l'inventario delle classi con il Libro.

** Potrei essere nel torto qui; Quando si chiama super(), che in questo caso è costruttore nei Libri(?), c' potrebbe generare errori poiché ti mancano i parametri. Questa chiamata al metodo probabilmente non dovrebbe essere in questa classe, poiché l'inventario probabilmente non dovrebbe estendere il Libro.

 1
Author: Einar, 2014-05-20 01:00:55