2011-02-07 12 views
0

私はいくつかのGUI機能を追加したいJavaクラスを持っています。例えば、書籍のタイトルやコストを返すことができます。私はアクションリスナーを実装する必要がありますが、どこから始めればいいのか分からなくても、どんなポインタでも大いに感謝しています。以下のコード -JavaクラスにGUI機能を追加

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

} 

編集:

HI、HERE BOOKS FORリポジトリのSECOND CLASSだ - 私はGUI ELEMENTS Iを処理しますBOOKDISPLAYのようなものと呼ばれる第3のクラスを作成する必要があると仮定します私は行動リストを2つのクラスの書籍と書籍に追加する必要があると思っています。そして、第3クラスはディスプレイに対処しますが、私はトラブルが始まっています。ディスプレイクラスが書籍の方法と「話す」ことができ、書籍が本当に賞賛されているかどうかを知るために役立ちます。私はこのプロジェクトでGUIとリスナーパーツを始動させている最中の問題に疑問を呈しています。

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

} 
+0

Gustav、私はあなたのアカウントをマージしたので、この投稿とすべての回答にコメントを残すことができます。ありがとう、@ jjnguy。 –

答えて

1

私はあなたがあなたのGUIを作成する前に、あなたが最初に作成したいと思います少なくとも1つの他の非GUIクラス、必要に応じてクラスライブラリやBookStoreなどの書籍のコレクションを処理し、必要なすべての機能を備えています。次に、そのクラスをGUIの核となる「モデル」として使用します。

それ以外の場合は、具体的な質問なしにGUIを作成する方法について具体的な推奨事項を提示することは困難です。このプログラム/割り当ての完了の最大の問題は何ですか?また、プログラミングロジックと同様に、コーディングの前にプランを立てて、一度に1つずつ小さなステップを解決してください。

+1

アドバイスをいただき、ありがとうございます。書籍のリポジトリを作成することはそこから始まります。 G – Simon

+0

良い計画のように聞こえます。さらにご質問がありましたらお知らせください。必要に応じて、元の質問を編集して変更することができます。 –

関連する問題