Java 在一个作业窗格中跨多个类打印

Java 在一个作业窗格中跨多个类打印,java,dialog,joptionpane,Java,Dialog,Joptionpane,我试图将所有输出放在一个包含3个不同类的作业窗格中。目前,每当我打印电子书时,它们都会被打印在各自的对话框中,一次只能按一个OK键,以循环浏览所有书籍 我需要他们包括在一个对话框中,以及“编程人”和电子书计数/成本部分 我很好奇是否必须在每个类之间导入。多谢各位 import javax.swing.JOptionPane; // dialog box public class Ebook { private String author = ""; private String

我试图将所有输出放在一个包含3个不同类的作业窗格中。目前,每当我打印电子书时,它们都会被打印在各自的对话框中,一次只能按一个OK键,以循环浏览所有书籍

我需要他们包括在一个对话框中,以及“编程人”和电子书计数/成本部分

我很好奇是否必须在每个类之间导入。多谢各位

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




    }
}
import javax.swing.JOptionPane;//对话框
公共类电子书
{
私有字符串author=“”;
私有字符串title=“”;
私人双价=0.0;
私有字符串isbn=“”;
公共电子书(字符串作者、字符串标题、双倍价格、字符串isbn)//电子书构造函数
{
this.author=作者;
this.title=标题;
if(price>0)//验证非负价格
这个价格=价格;
其他的
{   
这个价格=0.0;
系统输出打印项次(“无效价格”);
}
如果(isbn.length()==10 | | isbn.length()==13)//isbn长度必须正好是10或13
这是isbn=isbn;
其他的
this.isbn=“无”;
}   
公共定价(双倍价格)
{
if(价格<0)//验证
{
系统输出打印项次(“无效价格”);
}
其他的
这个价格=价格;
}
公开双价
{
退货价格;
}
public void setAuthor(字符串author)
{
this.author=作者;
}
公共字符串getAuthor()
{
返回作者;
}
公共无效集合isbn(字符串isbn)
{
如果(isbn.length()==10 | | isbn.length()==13)//验证
{
这是isbn=isbn;
}
其他的
isbn=“无”;
}
公共字符串getIsbn()
{
返回isbn;
}
公共无效集合标题(字符串标题)
{
this.title=标题;
}
公共字符串getTitle()
{
返回标题;
}
公共字符串toString()
{
返回String.format(“作者:%s%n主题:%s%n价格:$%.1f%nISBN:%s%n”,
作者、标题、价格、isbn);
} 
}//这是由--
导入javax.swing.JOptionPane;//对话框
公共类电子书图书馆
{
私有整数计数=0;
私人双倍总成本=0.0;
电子书[]电子书=新电子书[25];//电子书对象数组
public EbookLibrary()//库测试中没有EbookLibrary对象的参数构造函数
{
}
public int getCount()//电子书总数
{
返回计数;
}
public double getCost()//所有电子书的总和
{
返回总成本;
}
public String toString()//包含所有电子书数量和成本的格式化字符串
{
String message2=String.format(“电子书计数:%d%n总成本:$%.1f”,计数,总成本);//用于对话框
返回消息2;
}
public void addbook(字符串作者、字符串标题、双倍价格、字符串isbn)//将电子书添加到数组中
{
如果(计数<25)//不要离开数组
{
Ebook anEbook=新电子书(作者、标题、价格、isbn);//调用addEbook时要创建的电子书对象
电子书[计数]=电子书;
总成本+=价格;//所有书籍的总数
String message=String.format(“%s%n”,电子书[count]);
showMessageDialog(null,message);//在对话框中显示书籍
计数+++;//每次递增计数
}
} 
}//这是由------
导入javax.swing.JOptionPane;//对话框
公共类电子书库测试
{
公共静态void main(字符串[]args)
{
EbookLibrary aLibrary=new EbookLibrary();//用于调用addEbook的EbookLibrary对象
//电子书对象
aLibrary.addEbook(“废话”,“什么”,88.8,“1234567891”)
addEbook(“做什么”,“做什么”,45.0,“1234567891111”);
aLibrary.addEbook(“斯蒂芬·金”,“这个东西”,1.1,“1234567891”);
aLibrary.addEbook(“Robert”,“A Title”,-1.0,“1234567891”);//测试无效价格,应返回0.0和“无效价格”
aLibrary.addEbook(“汤姆”,“坏标题”,33.1,“1234567891111”);
addEbook(“Bob”、“FML和其他首字母缩写词”,25.0,“1”);//测试ISBN值,应返回“无”
aLibrary.getCount();
aLibrary.getCost();
String message=“编程人----”;
String message2=String.format(“%s%n”,aLibrary.toString());
JOptionPane.showMessageDialog(null,message+“\n”+message2);
}
}

首先,您需要删除添加
电子书时添加对话框的部分。相反,您希望有一种方法可以同时完成所有任务

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

您显示的是7个消息对话框,而不是1个

只需创建一个方法来返回ur
EBookLibrary
类中每个addedEbook的信息,然后主要使用一个循环,让这些书一次添加一个。每次添加新书时,该书返回的信息(
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);
EbookLibrary aLibrary=new EbookLibrary();//用于调用addEbook的EbookLibrary对象
String message=“编程人----”;
而(计数<25){
//电子书对象
message+=aLibrary.addEbook(“废话”,“什么”,88.8,“1234567891”)+/n':
message+=aLibrary.addEbook(“thingdo”,“What-What”,45.0,“1234567891111”)+/n';
message+=aLibrary.add
    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);