2016-07-30 74 views
-1

我正在研究一个电子书程序,该程序会将Ebooks添加到库中,然后显示各种信息。还有一些我仍在使用的程序部分,比如创建一个有效的语句来验证ISBN,但是我稍后再保存它。现在我只是试图创建一个Ebook对象,并将其添加到我的电子书数组中。但是,当我尝试在EbookLibraryTest中调用addEbook时,我得到了ebook1.addEbook(...)行的“无法找到符号”。我很困惑,因为其他两个类都编译了。我是否正确调用该方法?如果是这样,还有什么其他问题导致此错误?可能的范围问题,多个类

public class Ebook 
{ 
    private String author = ""; 
    private String title = ""; 
    private double price = 0; 
    private int isbn = 0; 


    public Ebook(String author, String title, double price, int isbn) 
    { 
     this.author = author; 
     this.title = title; 
     this.price = price; 

     if (isbn > 0) 
     this.isbn = isbn; 

     else 
      isbn = 0; 
    } 
     public void setPrice(double price) 
     { 
      if (price < 0) 
      { 
        System.out.println("Invalid price"); 
      } 

      else 
       this.price = price; 
     } 
     public double getPrice() 
     { 

      if (price < 0) 
      { 
       System.out.println("Invalid price"); 
       price = 0.0; 
       return price; 
      } 
      else 
       this.price = price; 
       return price; 


     } 
     public void setAuthor(String theAuthor) 
     { 
      this.author = theAuthor; 
     } 
     public String getAuthor() 
     { 
      return author; 
     } 
     public void setIsbn(int isbn) 
     { 
      if (isbn > 0) 
      { 
       this.isbn = isbn; 
      } 
      else 
       isbn = 0; 
     } 
     public int getIsbn() 
     { 
      if (isbn > 0) 
      { 
       this.isbn = isbn; 
       return isbn; 
      } 
      else 
      System.out.println("Invalid isbn"); 
          isbn = 0; 
      return isbn; 
     } 
     public void setTitle(String title) 
     { 
      this.title = title; 
     } 
     public String getTitle() 
     { 

      return title; 
     } 
     public String toString() 
     { 
      return String.format("The author is %s, the title is %s, the price is %f, the isbn is%d,", 
      author,title,price,isbn); 
     } 
} 
public class EbookLibrary 
{ 
    private int count = 0; 
    private double total_cost = 0.0; 

    Ebook[] ebooks = new Ebook[25]; 

    public EbookLibrary() 
    { 

    } 
    public int getCount() 
    { 
     return count; 
    } 
    public double getCost() 
    { 
     return total_cost; 
    } 
    public String toString() 
    { 
     return String.format("The count is %d, the total cost is %f,", count, total_cost); 
    } 
     public void addEbook(String theAuthor, String aTitle, double thePrice, int theIsbn) 
     { 

      Ebook anEbook = new Ebook("blah", "thing", 1.0, 1); 
      for (int counter = 0; counter < ebooks.length; counter++) 
      { 

      ebooks[counter] = anEbook; 
      count++; 
      price += total_cost; 
      } 

     } 
} 
public class EbookLibraryTest 
{ 
    public static void main(String[] args) 
    { 
     Ebook ebook1 = new Ebook("Tom Sawyer", "The Title", 77.0, 33); 
     Ebook ebook2 = new Ebook("Thing Do", "What What", 45.0, 15); 
     Ebook ebook3 = new Ebook("Stephen King","The Thing",1.1, 7); 
     Ebook ebook4 = new Ebook("Robert","A Title", 1.0, 1); 
     Ebook ebook5 = new Ebook("Tom","Bad Title", 33.1, 17); 
     Ebook ebook6 = new Ebook("Bob", "lol", 25.0, 15); 

     ebook1.addEbook("Tom Sawyer", "The Title", 77.0, 33); 


    } 
} 
+1

您添加一个电子书的图书馆,而不是其他的电子书。 .. –

+0

所有这些类在上面显示的一个文件中?请格式化您的代码。 –

+0

是的,他们是。抱歉。什么是多个类的正确格式? – srmjr

回答

3

您在EbookLibrary定义的方法addEbook(),而不是在Ebook。但是,您正尝试在Ebook对象上调用它。

只需拨打你的库对象这样的方法,它应该工作:

EbookLibrary myLibrary = new EbookLibrary(); 
myLibrary.addEbook(ebookAuthor, ebookTitle, ebookPrice, ebookIsbn); 

假设ebookAuthorebookTitleebookPriceebookIsbn已宣布和这个代码片段之前分配。

您也可以重载addEbook()方法直接在现有的电子书添加到库中,这将使它能够使用这样的:

myLibrary.addEbook(ebook1); 
+0

正确,但该方法需要一些字符串而不是电子书对象 –

+0

@ cricket_007这只意味着OP应提供多个不同输入的实现,对吗? –

+0

@MeikVtune方法超负荷或干脆重写方法,是 –