JOptionPane,JTextArea和JScrollPane,显示输出

时间:2015-06-26 02:08:48

标签: java output jscrollpane joptionpane

这是给予班级的硬件分配,我相信我的一切工作正常,但我在显示输出时需要的所有信息时遇到问题。如果你看TestCashier.java,你可以看到我的预期输出。我需要知道如何在窗格中显示generateReceipt()average()makeChange()tendered()。任何帮助都将不胜感激。我已经被困在这部分很长一段时间了。

如果有人愿意采取额外措施帮助我add()并找出为什么在向用户询问每件商品价格时无法让它工作。我需要使用add()才能使用该总计,但是当我手动将它们添加到一起并将该值分配给totalSum时,我只能让我的程序正常工作。感谢您抽出宝贵时间阅读本文,并感谢所有输入/反馈。 (甚至批评......我在这里学习)

Cashier.java

package cashier;
import java.text.NumberFormat;

public class Cashier {
    static int numItems;
    static double totalSum,averagePrice,price;
    private int   pennies,nickles, dimes, quarters, dollars;
    private double tendered,change;

    public void Cashier(){
        this.numItems = 0;
        this.totalSum = 0;
    }

    public void average(){
       averagePrice = totalSum/numItems; 
       NumberFormat nf1 = NumberFormat.getCurrencyInstance();
       System.out.println("The average price per item is "+ nf1.format(averagePrice));//Must format

    }
      public void add(String name, Double price) {
        numItems++;
        //totalSum =+price;
    }


    public void tendered(double t) {
       this.tendered = t;
       this.change = tendered - totalSum;
       NumberFormat df1 = NumberFormat.getCurrencyInstance();
       System.out.println("Amount tendered is " + df1.format(tendered));
   }

    void makeChange(Cashier c){

        change = (tendered-totalSum);
        change =change*100;
        NumberFormat df = NumberFormat.getCurrencyInstance();
        System.out.println("The change is: " + df.format(change/100)+"\n");//Must format


        dollars = (int)(change/100);
        change %= 100;


        quarters = (int) (change/25);
        change %=25;


        dimes = (int) (change/10);
        change = change%10;


        nickles = (int) (change/5);
        change = change%5;

        pennies = (int)change;

        System.out.println("The change includes...");
        System.out.println(dollars+" dollars");
        System.out.println(quarters+" quarters");
        System.out.println(dimes+" dimes");
        System.out.println(nickles+" nickles");
        System.out.println(pennies+" pennies");

    }






}

GetData.java

package cashier;


import javax.swing.JOptionPane;
import java.text.NumberFormat;


public class GetData {

    static double getDouble(String c){
        return Double.parseDouble(getWord(c));
    }

    static String getWord(String c){
        return JOptionPane.showInputDialog(c);
    }


}

TestCashier.java

package cashier;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;



import static cashier.Cashier.totalSum;
import java.text.DateFormat;
import java.text.NumberFormat;
import java.util.Date;

public class TestCashier{

    public static void main(String[]arg){
        NumberFormat nf = NumberFormat.getCurrencyInstance();
        Cashier c = new Cashier();

        String name = GetData.getWord(" Enter name of first item");
        double price = GetData.getDouble("Enter price of item");
        c.add(name,price);


        String name2 = GetData.getWord(" Enter name of second item");
        double price2 = GetData.getDouble("Enter price of item");
        c.add(name2,price2);

        String name3 = GetData.getWord(" Enter name of third item");
        Double price3 = GetData.getDouble("Enter price of item");
        c.add(name3,price3);

        String name4 = GetData.getWord(" Enter name of fourth item");
        Double price4 = GetData.getDouble("Enter price of item");
        c.add(name4,price4);

        totalSum = price+price2+price3+price4;



        //make payment 
        double tendered = GetData.getDouble("Enter amount of money for payment");



        generateReceipt(c);
        NumberFormat nf1 = NumberFormat.getCurrencyInstance();
        String s = (name+"\t\t"+nf1.format(price));
        s = s +("\n"+name2+"\t\t"+nf1.format(price2));
        s = s +("\n"+name3+"\t\t"+nf1.format(price3));
        s = s +("\n"+name4+"\t\t"+nf1.format(price4));
        s = s +("\n"+"_______________________________");
        s = s +("\n"+"Total:\t\t"+nf1.format(totalSum));


        s = s +("\n\n"+"The number of items purchased is "+Cashier.numItems+" item(s)");


        c.tendered(tendered);
        c.makeChange(c);

        JTextArea text = new JTextArea(s,30,30);
        JScrollPane pane = new JScrollPane(text);
        JOptionPane.showMessageDialog(null, pane,"THE RIP-OFF STORE",JOptionPane.PLAIN_MESSAGE);



    }
    static void generateReceipt(Cashier c){

       Date current = new Date();
       DateFormat df = DateFormat.getDateInstance(DateFormat.FULL);
       System.out.println("WELCOME TO THE RIP-OFF STORE!");
       System.out.println("Home of the 'not so good' deals...");
       System.out.println("Thank you for stopping by on "+ df.format(current));
       System.out.println("");


    }
}

1 个答案:

答案 0 :(得分:0)

您需要更改函数以连接到字符串并返回字符串。在您的函数generateReceipt()中,进行如下编辑:

static String generateReceipt(Cashier c){
    String s = "";
    Date current = new Date();
    DateFormat df = DateFormat.getDateInstance(DateFormat.FULL);
    s += ("WELCOME TO THE RIP-OFF STORE!\n");
    s += ("Home of the 'not so good' deals...\n");
    s += ("Thank you for stopping by on "+ df.format(current)) + "\n\n";
    return s;
}

并从该函数获取信息只需调用它:

String s = generateReceipt(c);

您需要从Cashier.java课程输出的所有功能都是如此。我不会向你展示每个功能,因为每个功能都是一样的。请记住它会被这样调用:

s += c.average();

以下是来自TestCashier的所有字符串连接以及我如何拥有它:

    String s = generateReceipt(c);
    NumberFormat nf1 = NumberFormat.getCurrencyInstance();
    s += (name+"\t\t"+nf1.format(price));
    s = s +("\n"+name2+"\t\t"+nf1.format(price2));
    s = s +("\n"+name3+"\t\t"+nf1.format(price3));
    s = s +("\n"+name4+"\t\t"+nf1.format(price4));
    s = s +("\n"+"_______________________________");
    s = s +("\n"+"Total:\t\t"+nf1.format(totalSum));


    s = s +("\n\n"+"The number of items purchased is "+Cashier.numItems+" item(s)");

    s += c.average();
    s += c.tendered(tendered);
    s += c.makeChange(c);

    JTextArea text = new JTextArea(s,30,30);
    JScrollPane pane = new JScrollPane(text);
    JOptionPane.showMessageDialog(null, pane,"THE RIP-OFF STORE",JOptionPane.PLAIN_MESSAGE);

我没有看过你的add()函数,但我也很高兴。如果您需要任何帮助,请与我聊天。

**这是一个imgur链接,指向我的IDE(IntelliJ Idea)上的输出:   http://imgur.com/gkG01Oc

编辑:

你几乎让你正确添加功能。在Cashier.java中,您的add()发表了一条声明:

totalSum =+ price;只需切换运算符totalSum += price;

即可

创建一个类似getTotalSum()的getter或类似的东西,因为你总是想使用getter和setter而不是直接访问类变量(至少在大多数情况下)。在TestCashier.java totalSum = c.getTotalSum();中调用它,它应该可以正常工作。请注意您如何name作为add()函数的参数。这是为什么?如果您不需要或使用它,请将其废弃。