简体   繁体   中英

How do I fix “cannot find symbol” error?

i get three error message on my BookTest class and it says cannot find symbol on getCode() , getCategory , and calculateTax . how do i fix this? im trying to print these out in a dialog box but these three are the only ones not working.

import javax.swing. JOptionPane;

public class BookTest
{
    public static void main(String args[])
    {
        double charge;
        double grandTotal= 0;

        String dataArray[][] = {{"NonFiction", "Abraham Lincoln Vampire Hunter","Grahame-Smith","978-0446563079","13.99","Haper","NY","US","Political"},
                {"NonFiction", "Frankenstein","Shelley","978-0486282114","7.99","Pearson", "TX","England", "Historical"},
                {"Fiction", "Dracula","Stoker","978-0486411095","5.99","Double Day", "CA","4918362"},
                {"NonFiction", "Curse of the Wolfman"," Hageman","B00381AKHG","10.59","Harper", "NY","Transylvania","Historical"},
                {"Fiction", "The Mummy","Rice","978-0345369949","7.99","Nelson","GA","3879158"}};

        Book bookArray[] = new Book[dataArray.length];

        int quantityArray[] = {12, 3, 7, 23, 5};

        for (int i = 0; i < dataArray.length; i++)
        {
            if (dataArray[i][0] == "NonFiction")
            {
                bookArray[i] = new NonFictionBook(dataArray[i][1], dataArray[i][2], dataArray[i][3], Double.parseDouble(dataArray[i][4]),
                        new Publisher(dataArray[i][5], dataArray[i][6]), dataArray[i][7], dataArray[i][8]);
            }
            else
            {
                bookArray[i] = new FictionBook(dataArray[i][1], dataArray[i][2], dataArray[i][3], Double.parseDouble(dataArray[i][4]),
                        new Publisher(dataArray[i][5], dataArray[i][6]), Integer.parseInt(dataArray[i][7]));
            }
        }
        String msg = "";
        for (int i = 0; i < bookArray.length; i++)
       {
            charge = bookArray[i].calculateTotal(quantityArray[i]);
            grandTotal = charge + grandTotal;
            msg += String.format("%s  %s  %d  $%.2f  $%.2f\n", bookArray[i].getTitle(), bookArray[i].getCategory(), bookArray[i].getCode(), bookArray[i].calculateTax, charge);  //this is where i get the 3 error messages. im trying to print all in one dialog box the title, category of the book, charge and tax for each book.  
        }
        msg += String.format("Grand Total  $%.2f ",  grandTotal);
        JOptionPane.showMessageDialog(null, msg);
    }
}

**************************************************************
public abstract class Book implements Tax
{
    private String title;
    private String author;
    private String isbn;
    private Double price;
    private Publisher publisher;

    public Book()
    {
        setTitle("");
        setAuthor("");
        setIsbn("");
        setPrice(0.0);
        setPublisher(new Publisher());
    }

    public Book(String t, String a, String i, double p, Publisher n)
    {

        setTitle(t);
        setAuthor(a);
        setIsbn(i);
        setPrice(p);
        setPublisher(n);
    }

    public void setTitle(String t)
    {
        title = t;
    }

    public String getTitle()
    {
        return title;
    }

    public void setAuthor(String a)
    {
        author = a;
    }

    public String getAuthor()
    {
        return author;
    }

    public void setIsbn(String i)
    {
        isbn = i;
    }

    public String getIsbn()
    {
        return isbn;
    }

    public void setPrice(double p)
    {
        price = p;
    }

    public double getPrice()
    {
        return price;
    }

    public void setPublisher(Publisher n)
    {
        publisher = n;
    }

    public Publisher getPublisher()
    {
        return publisher;
    }

    public abstract double calculateTotal(int quantity);


    public double calculateTax(double a)
    {
        return a * .08;
    }

    public String toString()
    {
        return( " Title " + title + " Author " + author + " Isbn " + isbn
                + " Price " + price + " Publisher " + publisher.toString());
    }
}

********************************************************

public class NonFictionBook extends Book
{
    private String country;
    private String category;

    public NonFictionBook()
    {
        super();
        setCountry("");
        setCategory("");
    }

    public NonFictionBook(String t, String a, String i, double p, Publisher n, String c, String ca)
    {
        super(t,a,i,p,n);
        setCountry(c);
        setCategory(ca);
    }

    public void setCountry(String c)
    {
        country = c;
    }

    public void setCategory(String ca)
    {
        category = ca;
    }

    public String getCountry()
    {
        return country;
    }

    public String getCategory()
    {
        return category;
    }

    public String toStirng()
    {
        return( super.toString() + "Country " + country + " Category " + category);
    }

    public double calculateTotal(int quantity)
    {
        double charge =0;
        charge = (quantity * getPrice());

        if( country != "US" )
            charge += 50;

        return charge;
    }

}
*********************************************
public class FictionBook extends Book
{
    private int code;
    public FictionBook()
    {
        super();
        setCode(0);
    }

    public FictionBook(String t, String a, String i, double p, Publisher n, int c)
    {
        super(t,a,i,p,n);
        setCode(c);
    }

    public void setCode(int c)
    {
        code = c;
    }

    public int getCode()
    {
        return code;
    }

    public String toString()
    {
        return (super.toString() + " Code " + code);
    }

    public double calculateTotal(int quantity)
    {
        double charge =0;

        charge = (quantity * getPrice());

        if (quantity > 5)
            charge += 5 * (quantity - 5);

        return charge;
    }
}

The tree methods are implemented in subclasses of Books. So you have to cast the value to the subclass.

if (bookArray[i] instanceof FictionBook){
   FictionBook fb = (FictionBook)bookArray[i];
       msg += String.format("%s  %s  %d  $%.2f  $%.2f\n", fb.getTitle(), "", fb.getCode(), 0, charge);  
}

if (bookArray[i] instanceof NonFictionBook){
   NonFictionBook fb = (NonFictionBook)bookArray[i];
        msg += String.format("%s  %s  %d  $%.2f  $%.2f\n", nfb.getTitle(), nfb.getCategory(), nfb.getCode(), nfb.calculateTax, charge);
}

and so on

Also you have to use equals() for comparing string. Not ==

In the line you get error you try to print fields that are not instances of the Book class but of its subclasses. You got an array of Books, you iterate on them and try to print the information. But not all the Books have getCode() method (only the fiction books) and only the non fiction books have getCategory() method. So you can't do this for ALL books.

You can change your print depending on the book type or you can make a method in Book class and each subclass can override it that prints the information you have for this class (like the toString method). Then use that in the main class.

And also as pointed in comments test if strings are equal with equals operator and not == or !=

Your array uses the type book:

Book bookArray[] = new Book[dataArray.length];

Later, you add sub-classes into this array, like NonFictionBook . Since code , category etc. are only part of the sub-classes, you cannot access them using the Book array reference.

// won't work because class Book does not have these methods, properties
bookArray[i].getCategory(), bookArray[i].getCode(), bookArray[i].calculateTax

You need to cast the object (based on what type is in the array).

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