Java收据程序未显示结果

时间:2015-09-21 16:19:23

标签: java swing jframe contentpane

我创建了一个简单的应用程序,它读取了一系列产品*销售量并生成最终订单收据,其中包括每件商品的销售商品名称,数量和总数以及整个收据的总数(包括6%)销售税)。我觉得我完成了这个,除了我无法测试应用程序,因为没有打印到“收据”AKA ContentPane。以下是我使用的三个课程。

产品类:

public class Product
{
  private int coffeeQTY;
  private int teaQTY;
  private int bagelQTY;
  private int muffinQTY;
  private double tax;
  private double total;
  private double price;
  private int productNOM;

public Product()
{
    total=price=0.00;
    productNOM=coffeeQTY=teaQTY=bagelQTY=muffinQTY=0;
}

//setters
public void setcoffeeQTY(int c)
{
    coffeeQTY += c;
}

public void setteaQTY(int t)
{
    teaQTY += t;
}

public void setbagelQTY(int b)
{
    bagelQTY += b;
}

public void setmuffinQTY(int m)
{
    muffinQTY += m;
}

//getters
public int getcoffeeQTY()
{
    return coffeeQTY;
}

public int getteaQTY()
{
    return teaQTY;
}

public int getbagelQTY()
{
    return bagelQTY;
}

public int getmuffinQTY()
{
    return muffinQTY;
}

public double getTAX()
{
    return tax;
}

public double getTotal()
{
    return total;
}

private double coffeePrice;
private double teaPrice;
private double bagelPrice;
private double muffinPrice;

public void inputOrder()
{
    for(int i = 0; i <= 3; i++)
    {
        String order = JOptionPane.showInputDialog("Product 1: Coffee $3.65 "
                + "\nProduct 2: Tea $2.45  "
                + "\nProduct 3: Bagel $1.50 "
                + "\nProduct 4: Muffins $1.85  "
                + "\nEnter the number of the product you would like to order. Enter -1 when your order is complete: ");
        productNOM = Integer.parseInt(order);

        boolean done = false;
        switch(productNOM)
        {
            case 1: 
                coffeePrice = 3.65;
                String cQTY = JOptionPane.showInputDialog("Enter the quantity of Coffee you would like to order: ");
                setcoffeeQTY(Integer.parseInt(cQTY));
                break;
            case 2:
                teaPrice = 2.45;
                String tQTY = JOptionPane.showInputDialog("Enter the quantity of Tea you would like to order: ");
                setteaQTY(Integer.parseInt(tQTY));
                break;
            case 3:
                bagelPrice = 1.50;
                String bQTY = JOptionPane.showInputDialog("Enter the quantity of Bagels you would like to order: ");
                setbagelQTY(Integer.parseInt(bQTY));
                break;
            case 4:
                muffinPrice = 1.85;
                String mQTY = JOptionPane.showInputDialog("Enter the quantity of the Muffins you would like to order: ");
                setmuffinQTY(Integer.parseInt(mQTY));
                break;
            default:
                done = true;
                break;
        }
        total += coffeePrice * coffeeQTY + teaPrice * teaQTY + bagelPrice * bagelQTY + muffinPrice * muffinQTY;


        if(!done)
        {
            tax = ((coffeePrice * coffeeQTY) * 0.06) + ((teaPrice * teaQTY) * 0.06) + ((bagelPrice * bagelQTY) * 0.06) + ((muffinPrice * muffinQTY) * 0.06);
            total = (coffeePrice * coffeeQTY) + (teaPrice * teaQTY) + (bagelPrice * bagelQTY) + (muffinPrice * muffinQTY) + tax;
            continue;
        }
        else
        {
            break;
        }           
    }
}

public void draw(Graphics g)
{
    g.drawString("Coffee       $3.65 x" +getcoffeeQTY(), 25, 100);
    g.drawString("Tes          $2.45 x" +getteaQTY(), 25, 125);
    g.drawString("Bagel        $1.50 x" +getbagelQTY(), 25, 150);
    g.drawString("Muffin       $1.85 x" +getmuffinQTY(), 25, 175);
    g.drawString("Tax(6%):            " +getTAX(), 25, 225);
    g.drawString("Total:              " +getTotal(), 25, 250);

}
}

SALES课程

public class Sales extends JComponent
{

private Product merch;

public Sales(Product m)
{
   merch = m;
}


public void printSales(Graphics g)
{
  Graphics2D g2 = (Graphics2D) g;
  merch.draw(g2);
 }
}

PRINTSALES课程

public class PrintSales
{
public static void main(String[] args) 
{
    // TODO Auto-generated method stub
    Product merch = new Product();

    merch.inputOrder();

    JFrame frame = new JFrame();

    frame.setSize(500, 750);
    frame.setTitle("Coffee Shop");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.getContentPane().setBackground(Color.WHITE);

    Sales store = new Sales(merch);

    frame.add(store);
    frame.setVisible(true);
}
}

1 个答案:

答案 0 :(得分:3)

我不知道它是否与tax字段的添加有关,但看起来似乎永远不会调用printSales(...)函数,因此不会绘制任何内容。