2011-02-07 7 views
0

Java 클래스를 사용하여 일부 GUI 기능을 추가하여 책 제목 또는 비용을 반환 할 수 있습니다. 작업 수신기를 구현해야하지만 어디에서 시작 해야할지에 대한 손실, 모든 포인터가 많이 감사하게 될 것입니다. 아래 코드 -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; 
    } 

} 

편집 : - 나는 내가 GUI ELEMENTS I을 처리 할 BOOKDISPLAY 같은라는 제 3 유형을 만들 필요가 부담

HI는 여기 책 저장소는 년대의 제 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, 귀하의 계정을 통합 했으므로이 게시물과 답변에 대한 의견을 남길 수 있습니다. 고마워, @jnguy. –

답변

1

나는 당신이 당신의 GUI를 작성하기 전에, 당신이 처음에 만들 있다고 생각을 적어도 하나의 GUI가 아닌 다른 클래스, 필요에 따라 필요한 모든 기능을 갖춘 클래스 라이브러리 또는 BookStore와 같은 책 컬렉션을 처리하는 클래스. 그런 다음 해당 클래스를 GUI의 핵심 또는 "모델"로 사용합니다.

그렇지 않으면 더 구체적인 질문없이 GUI를 만드는 방법에 대한 구체적인 권장 사항을 제공하기 어렵습니다. 이 프로그램/과제 완료의 가장 큰 문제점은 무엇입니까? 또한 프로그래밍 논리와 마찬가지로 코딩하기 전에 한 번에 하나씩 작은 단계를 해결하십시오.

+1

조언을 주셔서 감사합니다, 나는 그것을 다시 할 것입니다. 도서 저장소를 만드는 것은 거기에서 시작됩니다. G – Simon

+0

좋은 계획 같아요. 추가 질문이 있으면 알려주십시오. 필요한 경우 원래 질문을 편집하고 수정할 수 있습니다. –