Reputation: 3
i have a java class that i want to add some gui functionality to so that i can for instance return book titles or cost, i know i need to implement action listeners but i'm at a loss as to where to start, any pointers would be much appreciated. code below -
import java.text.*;
public class Book
{
public static void main (String[] args)
{
}
// 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;
}
}
Edit:
HI, HERE'S THE SECOND CLASS THAT'S A REPOSITORY FOR BOOKS - I ASSUME I NEED TO CREATE A THIRD CLASS CALLED SOMETHING LIKE BOOKDISPLAY WHICH WILL HANDLE THE GUI ELEMENTS I KNOW I ASSUME I NEED TO ADD ACTION LISTENERS TO THE TWO CLASSES BOOK AND BOOKSHELF AND THE THIRD CLASS WILL DEAL WITH THE DISPLAY BUT I'M HAVING TROUBLE GETTING STARTED. SOME HELP GETTING IT GOING AND EXPLAINING HOW THE DISPLAY CLASS WILL 'TALK' TO THE METHODS IN BOOK AND BOOKSHELF WOULD BE REALLY APPRECIATED. THANKS, G TO ANSWER 'HOVERCRAFTS' QUESTION THE BIGGEST PROBLEM I'M HAVING WITH THIS PROJECT IS GETTING THE GUI AND LISTENER PARTS STARTED!
import java.util.ArrayList;
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;
}
}
Upvotes: 0
Views: 2716
Reputation: 285430
I think that before you create your GUI, you'll want to first create at least one other non-GUI class, one that handles a collection of Books, such as a class Library or BookStore, depending on your need, with all the functionality necessary. Then I would use that class as the nucleus or "model" of the GUI.
Otherwise, it will be hard to give you specific recommendations about how to create your GUI without more specific questions. What exactly is the biggest problem in your completion of this program/assignment? Also, as with any programming logic, plan before coding, solve small steps one at a time.
Upvotes: 1
Reputation: 5930
You should have a look at this tutorial to start building graphical user interfaces with Java:
http://download.oracle.com/javase/tutorial/uiswing/
Upvotes: 2