java - 如何修复 "cannot find symbol"错误?

标签 java

我在 BookTest 类上收到三个错误消息,显示在 getCode()getCategory 上找不到符号计算税金。我该如何解决?我试图在对话框中打印这些内容,但这三个是唯一不起作用的。

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

最佳答案

树方法在 Books 的子类中实现。所以你必须将值转换为子类。

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

等等

此外,您还必须使用equals()来比较字符串。不是 ==

关于java - 如何修复 "cannot find symbol"错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30044881/

相关文章:

java - 如何在 Gradle 中编写此 Maven 排除代码?

java - GraphStream 中更具可读性的图形 - JAVA

java - 如何使用 Playframework 渲染特殊的 XML/JSON Flavors

java - 如何组合两个 Collections.sort 函数

使用 JavaMail 发送邮件时出现 javax.net.ssl.SSLException

java - 创建一个文件只是为了上传到在线表格

Java:字符串文字和 + 运算符

java - KeyPressed 事件没有发生?

java - 崩溃后在netbeans中恢复java文件

java - 另一个 java.lang.IndexOutOfBoundsException,但索引 < 大小