2016-07-31 34 views
-1

我試圖將所有輸出放到3個不同類中的一個JOption窗格中。目前,每當我打印電子書時,都會打印在他們自己的單獨對話框中,不得不一次一個地按OK,以循環瀏覽所有書籍。在多個類中的一個JOption窗格中打印

我需要將它們包含在一個對話框中,以及「編程方式」和電子書計數/成本部分。

我很好奇,如果我必須導入每個類。謝謝。

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) // validate 
      { 
       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 
    { 
     String message2 = String.format("Ebook count: %d%nTotal Cost: $%.1f", count, total_cost); // for dialog box 



     return message2; 
    } 
    public void addEbook(String author, String title, double price, String isbn) // adds ebooks to the array 
    { 

    if (count < 25) // dont walk off array 
    { 


     Ebook anEbook = new Ebook(author,title,price,isbn); // ebook object to be created when addEbook is called 

     ebooks[count] = anEbook; 

     total_cost += price; // total of all the books 

     String message = String.format("%s%n", ebooks[count]); 
      JOptionPane.showMessageDialog(null,message); // display books in dialog boxes 

     count++; // increment count each time 


    } 



    } 



} // 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 
     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" 


     aLibrary.getCount(); 
     aLibrary.getCost(); 

     String message = "Programmed by ---- "; 
      String message2 = String.format("%s%n", aLibrary.toString()); 
       JOptionPane.showMessageDialog(null,message + "\n " + message2); 




    } 
} 

回答

1

那麼首先您需要刪除添加對話框時添加對話框的部分。相反,你希望有一種方法可以一次完成。

public void printOrder(String signature){ 
    String message = ""; 
    for (int q = 0; q < count; q++){ 
     message += String.format("%s%n", ebooks[q]); 
    } 
    message += signature; 
    JOptionPane.showMessageDialog(null,message); 
} 

該代碼將在EBookLibrary去。我還想指出,您可能想要查看List以便存儲Ebook,因爲它可以根據您需要的尺寸進行增大和縮小。


現在在哪裏結束,你將打印您的簽名,您可以更改爲printOrder()方法和分析,你就會打印出來的文本。

String message = "Programmed by ---- "; 
String message2 = String.format("%s%n", aLibrary.toString()); 

aLibrary.printOrder(message + "\n " + message2); 
1

您顯示7個消息對話框,而不是1

只需創建返回大約每addedEbook烏爾EBookLibrary類環內,然後在主要使用的信息的方法,並在書增加了一個在一次。每次添加新書時,本書返回的信息(string)都會將其自身添加到main中的message2。的

或者不是創建一個新方法,你可以編輯addEbook方式類似:

String message = String.format("%s%n", ebooks[count]); 
count++; 
return message; //instead of JOptionPane 

,並在main方法:

EbookLibrary aLibrary = new EbookLibrary(); // EbookLibrary object for calling addEbook 
    String message = "Programmed by ---- "; 

    while(count < 25) { 
    // ebook objects 
    message += aLibrary.addEbook("Blah", "What", 88.8, "1234567891") + '/n': 
    message += aLibrary.addEbook("Thing Do", "What What", 45.0, "1234567891111") + '/n'; 
    message += aLibrary.addEbook("Stephen King","The Thing",1.1, "1234567891") + '/n'; 
    message += aLibrary.addEbook("Robert","A Title", -1.0, "1234567891") + '/n'; // test invalid price, should return 0.0 and "invalid price" 
    message += aLibrary.addEbook("Tom","Bad Title", 33.1, "1234567891111") + '/n'; 
    message += aLibrary.addEbook("Bob", "FML and Other Acronyms", 25.0, "1") + '/n'; // test ISBN value, should return "None" 


    aLibrary.getCount(); 
    aLibrary.getCost(); 


     //String message2 = String.format("%s%n", aLibrary.toString()); 
      JOptionPane.showMessageDialog(null,message); 

試試這個,或者嘗試只取出JOptionPanes關閉庫並僅顯示main中的單個窗格。

相關問題