Solved

Error trying to return size of an arraylist in a GUI

Posted on 2011-02-28
12
778 Views
Last Modified: 2012-05-11
hi, the code below is for a bookgui and following that is my bookshelf class - i'm trying to create a gui that will return the size of bookshelf but keep getting an error in eclipse that 'size' cannot be resolved.  any suggestions how to fix this please?

GUI

import java.awt.FlowLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import javax.swing.*;
import java.awt.event.ActionListener;
import java.util.ArrayList;


public class BookGUI extends JFrame implements ActionListener

{

      //String addBook="";
      // public ArrayList<Book> books;
      
      Book book = new Book("", "", 0, "", 0);
      String title  = "";
      String author  = "";
      int year = 0;
      String publisher  = "";
      double cost = 0;
      
      
      
      private BookShelf bookShelf = new BookShelf();
      private BookShelf books = new BookShelf();
    public static final int WIDTH = 300;
    public static final int HEIGHT = 200;

    //Creates & displays a window of the class FlowLayoutDemo
    public static void main(String[] args)
    {
        BookGUI gui = new BookGUI( );
        gui.setVisible(true);
    }
   
   
   
   // public String getTitle()
   // {
    //    return title;
    //}
   
    public void setTitle(String title) //this is relevant
    {
        this.title = title;
    }
   
    public void setAuthor(String author) //this is relevant
    {
        this.author = author;
    }
   
    public void setYear(int year) //this is relevant
    {
        this.year = year;
    }
    public void setPublisher(String publisher) //this is relevant
    {
        this.publisher = publisher;
    }
   
    public void setCost(int cost) //this is relevant
    {
        this.cost = cost;
    }
   
    public int sizeOfBookshelf()
    {
        return books.size();
    }
   
    //public void book()
    //{
        //this.title = title;
        //this.author = author;
        //this.year = year;
        //publisher = publisher;
        //this.cost = cost;
    //}
   
    //public int sizeOfBookshelf()
    //{
        //return books.size();
   // }
   
       
    public BookGUI( )
    {
       
          setSize(WIDTH, HEIGHT);
        addWindowListener(new WindowDestroyer( ));
        setTitle("GUI Assignment");
        Container content = getContentPane( );

        content.setLayout(new FlowLayout());
             
        JButton button1 = new JButton("Title");
        content.add(button1);
        button1.addActionListener(this);
        //contentPane.add(button1);
       
        JButton button2 = new JButton("Author");
        content.add(button2);
        button2.addActionListener(this);
       
        JButton button3 = new JButton("Size of BookShelf");
        content.add(button3);
        button3.addActionListener(this);
       
        JButton button4 = new JButton("Add Book");
        content.add(button4);
        button4.addActionListener(this);    
       
       
    }
   
     
    public void actionPerformed(ActionEvent e)
    {
       
        if (e.getActionCommand().equals("Add Book"))
               //book = JOptionPane.showInputDialog("Add Book");
             //set up the book object with all the data passed in
             
        title = JOptionPane.showInputDialog("Title");
        author = JOptionPane.showInputDialog("Author");
        publisher = JOptionPane.showInputDialog("Publisher");
        book.setTitle(title);
        book.setAuthor(author);
        book.setPublisher(publisher);

              bookShelf.addBook(book);
              
        // set up each individual title, author, etc.  use joptionpane and mutators?
       //exception handling...
        // add the book to the bookShelf
        //
       
       // if (e.getActionCommand().equals("Author"))
            //author = JOptionPane.showInputDialog("Enter Author");
       
        //else if (e.getActionCommand().equals("Publisher"))
           // publisher = JOptionPane.showInputDialog("Enter Publisher");
       
        //else
              //System.out.println("Error!");
       
        String message =  "The title of the book is :" + title +
            "the Author of the Book is : " + author + " and it's published by " + publisher;
       
        JOptionPane.showMessageDialog(null, message, "Book Details", JOptionPane.PLAIN_MESSAGE);
       
        //Container content = getContentPane( );
     if (e.getActionCommand().equals("Size of BookShelf"))
           return books.size; //= JOptionPane.showInputDialog("Size");
          
           bookShelf.sizeOfBookshelf();
       
        //label2 = new JButton("Get Book Information");
        //content.add(label2);

        //JButton label3 = new JButton("Show Total Cost of Books");
        //content.add(label3);
   
    }
   
   
   
   
   
}


BOOKSHELF

import java.util.ArrayList;

/**
  * This class holds an ArrayList of Book.  There are methods for adding Book objects to the ArrayList,
  * calculating the total cost of all books in the ArrayList, determining how many Book objects are
  * in the ArrayList and determining the highest price paid for any one Book.
  *
  * @author
  * @version 1.0
  */

public class BookShelf
{
    //create an ArrayList of Book.
    public ArrayList<Book> books;
   
    public BookShelf()
    {    
       books = new ArrayList<Book>();
    }

    /**
     * This method adds a Book object to the ArrayList
     *
     * @param theBook The book object that will be added to the ArrayList
     *
     */
    public void addBook(Book theBook)
    {
        books.add(theBook);
    }
   
    /**
     * This method returns the size of the ArrayList, that is, the number of books
     * that have been added to the ArrayList
     *
     * @return The size of the ArrayList
     */
    public int sizeOfBookshelf()
    {
        return books.size();
    }
   
    /**
     * This method calculates the cost of the book shelf, that is, it totals the
     * cost of all the books in the ArrayList and returns it.
     *
     * @return The cost of the book shelf
     */
    public double costOfBookshelf(){
       
        double total = 0;
       
        for(Book book : books) {
            total = total + book.getCost();
        }
        return total;
    }
       
   
    //create a method called highestPricePAid that will return the cost of the most expensive book in the ArrayList

    /**
     * This method finds the price of the most expensive book in the ArrayList and returns it.
     *
     * @return The cost of the most expensive book on the book shelf
     */
    public double highestPricePaid(){
       
       double highestPrice = 0;
       
       for(Book book : books) {
                     
            if((highestPrice == 0) || (highestPrice < book.getCost())) {
                highestPrice = book.getCost(); }
       }
       
       return highestPrice;
    }
         
}


0
Comment
Question by:t38
  • 5
  • 4
  • 2
  • +1
12 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 35002918
Did you post your class Book?

Please, use "CODE" tags when posting code - it is much more conveinient to copy it.
0
 
LVL 92

Expert Comment

by:objects
ID: 35003171
>         return books.size();

should be:


        return books.sizeOfBookshelf();
0
 

Author Comment

by:t38
ID: 35005008
tha ks, will try it and get back. for yan, will do re. code tags in future
0
 

Author Comment

by:t38
ID: 35005081
here's the book class

objects - i should have mentioned, i want to change the bookgui class to work with bookshelf and not make any changes to bookshelf class.  any other suggestions please?
import java.text.*;

/**
  * This class defines an object, Book.  
  * 
  * There are methods for adding Book objects to the ArrayList,
  * calculating the total cost of all books in the ArrayList, determining how many Book objects are
  * in the ArrayList and determining the highest price paid for any one Book.
  *
  * @author  Siobhan Drohan
  * @version 1.0
  */

public class Book
{
    // Instance variables
    private String title, author, publisher;
    private int year; 
    private double cost;

    /**
     * Book constructor that initialises the instance variables based on user input.
     * 
     * @param title     The title of the book
     * @param author    The author of the book
     * @param year      The year the book was published
     * @param publisher The name of the publisher of the book
     * @param cost      The cost of the book
     */
    public Book(String title, String author, int year, String publisher, double cost)
    {
        this.title = title;
        this.author = author;
        this.year = year;
        this.publisher = publisher;
        this.cost = cost;
    }
    
    /**
     * Accessor for the title of the book.
     * @return the title of the book.
     */
    public String getTitle()
    {
        return title;
    }
    
    /**
     * Accessor for the author of the book.
     * @return the author of the book.
     */
    public String getAuthor() 
    {
        return author;
    }
    
    /**
     * Accessor for the year the book was published.
     * @return the year the book was published.
     */    
    public int getYear() 
    {
        return year;
    }
    
    /**
     * Accessor for the publisher of the book.
     * @return the publisher of the book.
     */    
    public String getPublisher() 
    {
        return publisher;
    }
    
    /**
     * Accessor for the cost of the book.
     * @return the cost of the book.
     */    
    public double getCost() 
    {
        return cost;
    }
    
    /**
     * Mutator for updating the title of the book.
     * @param title The new title of the book.
     */
    public void setTitle(String title) 
    {
        this.title = title;
    }
    
    /**
     * Mutator for updating the author of the book.
     * @param author The new author of the book.
     */
    public void setAuthor(String author) 
    {
        this.author = author;
    }
    
    /**
     * Mutator for updating the year of the book.
     * @param year The new year of the book.
     */
    public void setYear(int year)
    {
        this.year = year;
    }
    
    /**
     * Mutator for updating the publisher of the book.
     * @param publisher The new publisher of the book.
     */    
    public void setPublisher(String publisher)
    {
        this.publisher = publisher;
    }
    
    /**
     * Mutator for updating the cost of the book.
     * @param cost The new cost of the book.
     */    
    public void setCost(double cost)
    {
        this.cost = cost;
    }
    
    
    /**
     * The standard toString method that returns the details of the book 
     * @return the details of the book formatted as a String.
     */
    public String toString()
    {
        return "The details of the book are: " + title + 
               ", " + author + ", " + year + ", " + publisher + ", " + cost;
    }

}

Open in new window

0
 
LVL 92

Expert Comment

by:objects
ID: 35005186
I wasn't talking about changing the bookshelf class
i meant here


    public int sizeOfBookshelf()
    {
        return books.size();
    }
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35005349
Thanks for Book class, but objects is of course right - how can you use books.size() in your BookGUI class if books in BookGUI
is instance of BookShelf and not ArrayList like in BookShelf. I just didn't notice it with my eyes and wanted to feed it to compiler
to see where the problem is, and doing it discovered that it needs Book class, but objects found it without any compiler.
So you have to change this piece.
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:t38
ID: 35005451
objects, i've amended the code as suggested (i think) but it's still returning an error in eclipse, full code attached.  any suggestions would be appreciated, i'm new to this gui business... thnaks -

  public int sizeOfBookshelf()
    {
    return books.sizeOfBookshelf();

   }
   
 THIS IS INCORRECT?  - if (e.getActionCommand().equals("Size of BookShelf"))
     sizeOfBookshelf = JOptionPane.showInputDialog("Size");
0
 
LVL 92

Expert Comment

by:objects
ID: 35005470
whats the error, what line?

>   sizeOfBookshelf = JOptionPane.showInputDialog("Size");

yes thats incorrect, and appears unecessary. What are you trying to do?
0
 

Author Comment

by:t38
ID: 35005546
LINE 76 - The method sizeOfBookshelf() is undefined for the type Book

LINE 149 - Multiple markers at this line
      - size cannot be resolved or is not a field
      - sizeOfBookshelf cannot be resolved to a
       variable

I'm trying to get the GUI to return The size of Bookshelf value using the sizeofBookshelf method in Bookshelf class
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35005595
>>
THIS IS INCORRECT?  - if (e.getActionCommand().equals("Size of BookShelf"))
     sizeOfBookshelf = JOptionPane.showInputDialog("Size");
>>

You probably want something like
sizeOfBookshelf = Integer.valueOf(JOptionPane.showInputDialog(null, "Enter size of bookshelf"));

Open in new window

0
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 35005670
use the following:

        String message = "Number of books in shelf "+books.sizeOfBookshelf();
        JOptionPane.showMessageDialog(null, message, "Book Shelf", JOptionPane.PLAIN_MESSAGE);
0
 

Author Closing Comment

by:t38
ID: 35006507
Thanks for your assistance, your input helped point me in the right direction.  theo
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now