2016-07-31 1 views
0

enter image description here 3つの問題があります。配列とtoStringを出力するときの出力の問題

1)私は印刷しようとしている電子ブックの配列を持っています。配列は25要素で、6つの電子ブックを配置しています。印刷すると、1回ではなく、各電子ブックが25回印刷されます。

2)私はJOptionPaneの、または他のいくつかのクラスを実装するにはどうすればよい私の希望print文

3)の前に大きな進数を印刷している私のプログラムの終了時に出力を持って、1にすべての出力を印刷しますダイアログボックス?

import javax.swing.JOptionPane; // dialog box 

public class Ebook 
{ 
    private String author = ""; 
    private String title = ""; 
    private double price = 0.0; 
    private String isbn = ""; 


     public Ebook(String author, String title, double price, String isbn) // ebook constructor 
     { 
      this.author = author; 
      this.title = title; 

      if (price > 0) // validate non-negative price 
       this.price = price; 

      else 
      { 
       this.price = 0.0; 
        System.out.println("Invalid price"); 
      } 

      if (isbn.length() == 10 || isbn.length() == 13) // isbn length must be exactly 10 or 13 
       this.isbn = isbn; 

      else 
       this.isbn = "None"; 
     } 

     public void setPrice(double price) 
     { 
      if (price < 0) // vallidate 
      { 
       System.out.println("Invalid price"); 
      } 

      else 
       this.price = price; 
     } 

     public double getPrice() 
     { 
      return price; 
     } 

     public void setAuthor(String theAuthor) 
     { 
      this.author = theAuthor; 
     } 

     public String getAuthor() 
     { 
      return author; 
     } 

     public void setIsbn(String isbn) 
     { 
      if (isbn.length() == 10 || isbn.length() == 13) // validate 
      { 
       this.isbn = isbn; 
      } 
      else 
       isbn = "None"; 
     } 

     public String getIsbn() 
     { 
      return isbn; 
     } 

     public void setTitle(String title) 
     { 
      this.title = title; 
     } 

     public String getTitle() 
     { 
      return title; 
     } 

     public String toString() 
     { 
      return String.format("Author: %s%nTitle: %s%nPrice: $%.1f%nISBN: %s%n", 
       author,title,price,isbn); 
     } 
} // This was made by ------ 

import javax.swing.JOptionPane; // dialog box 

public class EbookLibrary 
{ 
    private int count = 0; 
    private double total_cost = 0.0; 


    Ebook[] ebooks = new Ebook[25]; // array of ebook objects 

    public EbookLibrary() // no argument constructor for ebooklibrary object in library test 
    { 

    } 
    public int getCount() // total number of ebooks 
    { 
     return count; 
    } 
    public double getCost() // sum of all ebooks 
    { 
     return total_cost; 
    } 
    public String toString() // formatted string with the number and cost of all ebooks 
    { 
     return String.format("Ebook count: %d%nTotal Cost: $%.1f", count, total_cost); 
    } 
    public void addEbook(String author, String title, double price, String isbn) // adds ebooks to the array 
    { 
     Ebook anEbook = new Ebook(author,title,price,isbn); // not sure if this is a "constructor", but I think it is 

     for (int counter = 0; counter < ebooks.length; counter++) // for the length of the array, add ebook 
     { 
      ebooks[counter] = anEbook; // for each counter, add the ebook 
       total_cost += price; 
        count++; // used to find the total number of ebooks 
         System.out.printf("%s%n", ebooks[counter]); 

     } 


    } 



} // This was made by ----- 

import javax.swing.JOptionPane; // dialog box 

public class EbookLibraryTest 
{ 
    public static void main(String[] args) 
    { 

     EbookLibrary aLibrary = new EbookLibrary(); // EbookLibrary object for calling addEbook 

     //ebook objects, more can be added to test set, get methods 
     aLibrary.addEbook("Blah", "What", 88.8, "1234567891"); 
     aLibrary.addEbook("Thing Do", "What What", 45.0, "1234567891111"); 
     aLibrary.addEbook("Stephen King","The Thing",1.1, "1234567891"); 
     aLibrary.addEbook("Robert","A Title", -1.0, "1234567891"); // test invalid price, should return 0.0 and "invalid price" 
     aLibrary.addEbook("Tom","Bad Title", 33.1, "1234567891111"); 
     aLibrary.addEbook("Bob", "FML and Other Acronyms", 25.0, "1"); // test ISBN value, should return "None" 




     System.out.printf("%d%f%s%n", aLibrary.getCount(), // call methods, print with toString 
      aLibrary.getCost(), aLibrary.toString()); 

     System.out.println("Programmed by -----"); 

    } 
} 
+0

私はそれが入っている3つの別々のクラスファイルに入れようとしましたが、どうすればよいか分かりません。 Ebookクラス、EbookLibraryクラス、EbookLibraryTestクラスがあります。 – srmjr

+0

プリントアウトは何ですか? 'aLibrary.toString()'は、countとtotal_costだけを出力します。 – c0der

+0

出力のスクリーンショットを追加しましたが、削除されたと思います。それは今起きている。 – srmjr

答えて

2

私はこの問題は、あなたのEbookLibraryクラスのごaddEbook方法であると信じています。新しいEbookを追加するたびに、ebooksの配列全体が入力されます。各繰り返しで、total_costcountも増分しています。私はあなたがそれを配列に一度追加し、その前に配列がいっぱいでないことを確認したいと仮定しています。これを試して。

public void addEbook(String author, String title, double price, String isbn) // adds ebooks to the array 
{ 
    if(count==ebooks.length-1) { 
     return; 
    } 
    Ebook anEbook = new Ebook(author,title,price,isbn); 

    ebooks[count] = anEbook; 
    total_cost += price; 
    System.out.printf("%s%n", anEbook); 
    count++; // used to find the total number of ebooks 
} 
+0

ありがとうございました。なぜこのコードが帳簿の値ではなくヌルを出力するのか説明できますか? – srmjr

+2

@srmjrはい、それは印刷前に 'count'をインクリメントしたためです。それは私が修正するのを忘れた別の間違いでした。私は私の答えを更新しました。 – kamoroso94

+0

ありがとうございます。非常に役立ちます。正直言って、インクリメントの順序と印刷への影響については考慮していませんでした。まだ学んでいる。ありがとうございました。 – srmjr

2

進数は、この行にある: System.out.printf( "%D%F%S%N"、aLibrary.getCount()、aLibrary.getCost()、aLibrary.toString( ));

%dは整数を表示しますが、%fはfloatを表示します。それを取り除くと、この部分は機能します。

addEbook機能も間違っています。 1つのEbookで配列全体を塗りつぶし、毎回表示します(25回表示されます)。

+0

ああ、なぜ私は1つのprintステートメントですべてを呼び出す必要があると思ったのか分かりません。私はgetCountとgetCostを呼び出し、次にtoStringを別々に呼び出しました。出来た。ありがとうございました。 2番目の部分については、私は何度も何度も印刷して問題を解決しましたが、現在は帳簿の値として「null」を印刷しています。 'Ebook anEbook =新しい電子ブック(著者、タイトル、価格、isbn); \t \t \t \t ebooks [count] = anEbook; \t \t total_cost + = price; \t \tカウント++; \t \t System.out.printf( "%s%n"、ebooks [count]); ' – srmjr

+0

最新バージョンのコードを提供できますか? [Pastebin](http://pastebin.com/)を使用してリンクすることをお勧めします(登録不要)。 – pie3636

+0

'Ebook anEbook =新しい電子ブック(" blah "、" blah "、1。0、 "123"); \t \t \t \t ebooks [count] = anEbook; \t \t total_cost + = price; \t \tカウント++;\t \t System.out.printf( "%s%n"、ebooks [count]); 'これは、ヌルを6回印刷してから、正しい数と価格を出力します。コードフォーマットについてお詫び申し上げます。以前はペーストビンを使ってみましたが、しないように言われました。 – srmjr