0

I am trying to print all books with the type "Paperback" to the console. But I am getting the issue where it says the variable type is not located in the class book. I suppose this is because the objects are stored in an array of book objects but I need all the books to be stored in one array. If anyone has a recommendation as to how I can better store the objects in one array or how I can access the variable I need, it would be greatly appreciated.

Here is the driver class Test

public class Test {
 public static void main(String[] args) {
      BookCollection bc = new BookCollection();
      bc.addNewBook("Big Java", "Horstmann", 2014, 150.0,"Paperback");
      bc.addUsedBook("Java", "Deitel", 1999, 120.0, 0.75);
      bc.addNewBook("JavaScript", "Hoque", 2005, 80.50, "Hardcover");
      bc.addNewBook("C++", "Smith", 2004, 135.0, "Paperback");
      bc.addUsedBook("C", "Jones", 2000, 110.0, 0.6);

      System.out.println(bc.printReport());
      System.out.println("****************************************");
      //System.out.println(bc.printAllBooksWithSellingPriceBelow(100.0));
     //System.out.println("****************************************");
      System.out.println(bc.printAllPaperbackBooks());
 }

}// end of Hello

Here is the Book class

class Book {
 public String title;
 public String author;
 public int year;
 public double price;
 public double usageIndex;

 public Book(String t, String a, int y, double p) {
      title = t;
      author = a;
      year = y;
      price = p;
 } // This creates the parent class book with the default attributes

 public String toString() {
      return title + author + year + price;
 } // This puts anything the book class contains into a string form

}// end of Book

Here are the classes NewBook and UsedBook that both extend book

class BookCollection {
 private Book[] books;
 private String pT;
 private String pA;
 private int pY;
 private double pP;
 private String pTy;
 private double pI;
 private int i = 0;
 private int count = 0;
 private String report = "";
 private String below = "";
 private double pr;
 private String paper = "";

 public BookCollection() {
      books = new Book[1000];
 }

 public void addNewBook(String pTitle, String pAuthor, int pYear, double pPrice, String pType) {
      pT = pTitle;
      pA = pAuthor;
      pY = pYear;
      pP = pPrice;
      pTy = pType;

      books[i++] = new NewBook(pT, pA, pY, pP, pTy);
      count++;
 }

 public void addUsedBook(String pTitle, String pAuthor, int pYear, double pPrice, double pIndex) {
      pT = pTitle;
      pA = pAuthor;
      pY = pYear;
      pP = pPrice;
      pI = pIndex;

      books[i++] = new UsedBook(pT, pA, pY, pP, pI);
      count++;
 }


 public String printReport() {
      for (int j = 0; j < count; j++) {
           if (books[j] instanceof UsedBook) {
                report = report.concat("Books " + (j + 1) + ": Used Book\n" + books[j].toString());
           } // if object is UsedBook, then say so
           if (books[j] instanceof NewBook) {
                report = report.concat("Books " + (j + 1) + ": New Book\n" + books[j].toString());
           } // if object is NewBook, then say so
      }
      return report;
 } // end of printReport()
public String printAllPaperbackBooks() {
      for (int z = 0; z < count; z++) {
           if (books[z] instanceof NewBook) {
                if (books[z].type.equalsIgnoreCase("Paperback")) {
                     paper = paper.concat("Books " + (z + 1) + ": New Book\n" + books[z].toString());
                }
           } // if object is NewBook, then say so
      }
      return paper;
 }

}

Here is the error I am getting

Test.java:139: error: cannot find symbol
                if (books[z].type.equalsIgnoreCase("Paperback")) {
                            ^
  symbol:   variable type
  location: class Book
1 error

1 Answers1

0

This problem occurred because type is not a public field in Book.

Assuming type is a public field in a class called NewBook (which you have not shown us), you need to cast books[z] to a NewBook:

if (((NewBook)books[z]).type.equalsIgnoreCase("Paperback")) {
Jason
  • 11,744
  • 3
  • 42
  • 46