Link to home
Start Free TrialLog in
Avatar of t38
t38

asked on

Problem with Java GUI

The code below is for a GUI to interface with a bookshelf class and perform functions such as adding a book etc.  eclipse is giving an error at addbook, any advice on how to resolve this problem please?

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

public class BookGUI extends JFrame implements ActionListener

{

      //String addBook="";
      String book = "";
      String title  = "";
      String author  = "";
      String year = "";
      String publisher  = "";
      String cost = "";
      
      private BookShelf bookShelf = 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 Book()
    {
        this.title = title;
        this.author = author;
        this.year = year;
        this.publisher = publisher;
        //this.cost = cost;
    }
   
       
    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("Publisher");
        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
             
       
              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( );

       
        //label2 = new JButton("Get Book Information");
        //content.add(label2);

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





Avatar of Gurvinder Pal Singh
Gurvinder Pal Singh
Flag of India image

<<eclipse is giving an error at addbook>>
Please share the exception stack trace
Avatar of t38
t38

ASKER

This is the error Eclipse is returning.  The method addBook(Book) in the type BookShelf is not applicable for the arguments (String).

I should add that i have seperate classes for book and bookshelf

can you share the code for Bookshelf?

apparently in this code
bookShelf.addBook(book);

book is not of type String which the method 'addBook' is expecting
And also I don't think I see the code for the BookShelf class - if the exception happens inside its method
that is probably the first place to look - post it (of couesr stck trcae is most important).
Avatar of t38

ASKER

Here's the bookshelf class - thanks.

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  Siobhan Drohan
  * @version 1.0
  */

public class BookShelf
{
    //create an ArrayList of Book.
    private 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;
    }
         
}
book variable that you have declared is of type String, but Bookshelf class method addBook is expecting Book class variable as argument. So you need to either
--change the variable to Book
-- change the argument to String in BookShelf class
Avatar of t38

ASKER

thanks, change the variable to Book where?
please share the source code of Book class also
Avatar of t38

ASKER

here you go, thanks.

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.
  *
  * @
  * @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;
    }

}

ASKER CERTIFIED SOLUTION
Avatar of Gurvinder Pal Singh
Gurvinder Pal Singh
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of t38

ASKER

thank you, i'm not able to access eclipse at the moment but will do so asap and report back. thanks for all your help
Avatar of t38

ASKER

Gurvinder 372 - is the code you suggested just working around the problem and no using the addbook method at all?  i'll have to do a book.set for Author, Cost etc. as well?
Avatar of t38

ASKER

thank you for your help, much appreciated.