srmjr srmjr - 4 months ago 8
Java Question

Possible scope issue, multiple classes

I'm working on an Ebook program that adds Ebooks to a library and then displays various information. There are portions of the program that I'm still working on, such as creating a valid statement to verify the ISBN, but I'm saving that for later. Right now I'm simply trying to create an Ebook object and add it to my ebooks array. However, when I try to invoke addEbook in EbookLibraryTest, I'm getting "cannot find symbol" for the line of ebook1.addEbook(...). I am confused because the other two classes compile. Am I calling the method correctly? If so, what other issue is causing this error?

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


}
}

Answer

You defined the method addEbook() in EbookLibrary, not in Ebook. However, you are trying to call it on an Ebook object.

Just call the method on your library object like this and it should work:

EbookLibrary myLibrary = new EbookLibrary()
myLibrary.addEbook(ebook1)
Comments