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.
我在BookTest类上收到三条错误消息,它说无法在getCode(),getCategory和calculateTax上找到符号。我该如何解决?我试图在对话框中打印这些,但这三个是唯一不起作用的。
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;
}
}
3 个解决方案
#1
The tree methods are implemented in subclasses of Books. So you have to cast the value to the subclass.
树方法在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);
}
and so on
等等
Also you have to use equals()
for comparing string. Not ==
此外,您必须使用equals()来比较字符串。不是==
#2
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.
在您遇到错误的行中,您尝试打印的字段不是Book类的实例,而是其子类的实例。你有一系列的书籍,你迭代它们并尝试打印信息。但并非所有的书籍都有getCode()方法(只有小说书籍),只有非小说类书籍才有getCategory()方法。所以你不能为所有书籍做这件事。
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.
您可以根据书籍类型更改打印,也可以在Book类中创建一个方法,每个子类都可以覆盖它,打印您为此类提供的信息(如toString方法)。然后在主类中使用它。
And also as pointed in comments test if strings are equal with equals operator and not == or !=
并且如评论中所指出的那样,测试字符串是否与equals运算符相等而不是==或!=
#3
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.
稍后,您将子类添加到此数组中,如NonFictionBook。由于代码,类别等只是子类的一部分,因此无法使用Book数组引用来访问它们。
// 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).
您需要转换对象(基于数组中的类型)。
#1
The tree methods are implemented in subclasses of Books. So you have to cast the value to the subclass.
树方法在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);
}
and so on
等等
Also you have to use equals()
for comparing string. Not ==
此外,您必须使用equals()来比较字符串。不是==
#2
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.
在您遇到错误的行中,您尝试打印的字段不是Book类的实例,而是其子类的实例。你有一系列的书籍,你迭代它们并尝试打印信息。但并非所有的书籍都有getCode()方法(只有小说书籍),只有非小说类书籍才有getCategory()方法。所以你不能为所有书籍做这件事。
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.
您可以根据书籍类型更改打印,也可以在Book类中创建一个方法,每个子类都可以覆盖它,打印您为此类提供的信息(如toString方法)。然后在主类中使用它。
And also as pointed in comments test if strings are equal with equals operator and not == or !=
并且如评论中所指出的那样,测试字符串是否与equals运算符相等而不是==或!=
#3
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.
稍后,您将子类添加到此数组中,如NonFictionBook。由于代码,类别等只是子类的一部分,因此无法使用Book数组引用来访问它们。
// 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).
您需要转换对象(基于数组中的类型)。