程序计算销售额并直观地显示客户订单

时间:2014-10-04 07:13:53

标签: java

你好,这是我项目的提示

编写程序来计算销售额并进行直观计算 显示。在线牛仔零售商销售五种产品,其零售价格如下: 产品1:牛仔靴,74.99美元 产品2:牧马人,25.99美元 产品3:牛仔帽,24.95美元 产品4:Chaps,$ 34.89 产品5:马刺,12.50美元 编写一个应用程序,读取每个产品的数量,直到用户完成订单。你的程序应该使用switch,if,for,while和do while语句 阅读,计算并显示所有专业人员的总零售价值 为每个用户交易销售的管道。用户应该能够在第一个事务完成后启动新事务。用户总共不能超过3个订单

public class Merchandise 
{
  private int bootsQTY;
  private int wranglersQTY;
  private int hatsQTY;
  private int chapsQTY;
  private int spursQTY;
  private double total;
  private double price;
  private int productNOM;

public Merchandise()
{
    total=price=0.00;
    productNOM=bootsQTY=wranglersQTY=hatsQTY=chapsQTY=spursQTY=0;
}

public int getBootsQTY()
{
    return bootsQTY;
}

public int getWranglersQTY()
{
    return wranglersQTY;
}

public int getHatsQTY()
{
    return hatsQTY;
}

public int getChapsQTY()
{
    return chapsQTY;
}

public int getSpursQTY()
{
    return spursQTY;
}

public double getTotal()
{
    return total;
}

public void setBootsQTY(int b)
{
    bootsQTY = b;
}

public void setWranglersQTY(int w)
{
    wranglersQTY = w;
}

public void setHatsQTY(int h)
{
    hatsQTY = h;
}

public void setChapsQTY(int c)
{
    chapsQTY = c;
}

public void setSpursQTY(int s)
{
    spursQTY = s;
}

public void inputOrder()
{
    for(int i = 0; i <= 4; i++)
    {
        String order = JOptionPane.showInputDialog("Product 1: Cowboy Boots $74.99 \nProduct 2: Wranglers $25.99  \nProduct 3: Cowboy Hats $24.95 \nProduct 4: Chaps $34.89 \nProduct 5: Spurs $12.50 \nEnter the number of the product you would like to order, enter a -1 when your order is complete: ");
        productNOM = Integer.parseInt(order);

        boolean done = false;
        switch(productNOM)
        {
            case 1: 
                price = 74.99;
                String bQTY = JOptionPane.showInputDialog("Enter the quantity of Cowboy Boots you would like to order: ");
                setBootsQTY(Integer.parseInt(bQTY));
                break;
            case 2:
                price = 25.99;
                String wQTY = JOptionPane.showInputDialog("Enter the quantity of Wranglers you would like to order: ");
                setWranglersQTY(Integer.parseInt(wQTY));
                break;
            case 3:
                price = 24.95;
                String hQTY = JOptionPane.showInputDialog("Enter the quantity of Cowboy Hats you would like to order: ");
                setHatsQTY(Integer.parseInt(hQTY));
                break;
            case 4:
                price = 34.89;
                String cQTY = JOptionPane.showInputDialog("Enter the quantity of the chaps you would like to order: ");
                setChapsQTY(Integer.parseInt(cQTY));
                break;
            case 5:
                price = 12.50;
                String sQTY = JOptionPane.showInputDialog("Enter the quantity of Spurs you would like to order: ");
                setSpursQTY(Integer.parseInt(sQTY));
                break;
            default:
                done = true;
                break;
        }
        total += price * bootsQTY + price * wranglersQTY + price * hatsQTY + price * chapsQTY + price * spursQTY;


        if(!done)
        {
            continue;
        }
        else
        {
            break;
        }           
    }
}

public void draw(Graphics g)
{
    g.drawString("Cowboys Boots $74.99 x" +getBootsQTY(), 25, 100);
    g.drawString("Wranglers     $25.99 x" +getWranglersQTY(), 25, 125);
    g.drawString("Cowboy Hats   $24.95 x" +getHatsQTY(), 25, 150);
    g.drawString("Chaps         $34.89 x" +getChapsQTY(), 25, 175);
    g.drawString("Spurs         $12.50 x" +getSpursQTY(), 25, 200);
    g.drawString("Total:                " +getTotal(), 25, 225);
}
}

public class MerchandiseComponent extends JComponent
{
private Merchandise merch;

public MerchandiseComponent(Merchandise m)
{
    merch = m;
}

public void paintComponent(Graphics g)
{
    Graphics2D g2 = (Graphics2D) g;
    Merchandise m = new Merchandise();
    m.draw(g2);
}
}

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

    merch.inputOrder();

    JFrame frame = new JFrame();

    frame.setSize(500, 750);
    frame.setTitle("Cowboy Store");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

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

    MerchandiseComponent store = new MerchandiseComponent(merch);

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

当我运行程序时,我的所有数量和总数都是0,我无法弄清楚如何获取存储在其中的值,任何帮助都将不胜感激。

2 个答案:

答案 0 :(得分:0)

在商品类中,您能否为每个商品价格添加一个实例变量,如下图所示?

    private double bootsPrice;

并在交换机情况下分配项目价格,如下所示?

     case 1: 
        bootsPrice = 74.99;
        String bQTY = JOptionPane.showInputDialog("Enter the quantity of Cowboy Boots you would like to order: ");
        setBootsQTY(Integer.parseInt(bQTY));
        break;

对于那些项目的数量设定方法,你可以在下面添加一个“+”吗?

    public void setBootsQTY(int b)
    {
       bootsQTY += b;
    }

您可以更改并将总计算放在if条件中,如下所示吗?

    if(!done)
    {
        total = (bootsPrice * bootsQTY) + (wranglersPrice * wranglersQTY) + (hatsPrice * hatsQTY) + (chapsPrice * chapsQTY) + (spursPrice * spursQTY);
        continue;
    }

另一方面,在MerchandiseComponent类的paintComponent方法中,您可以更改如下吗?

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

更改后打印正确的数量和值。希望它会对你有所帮助。

答案 1 :(得分:0)

您有两个Merchandise类实例。

一个计算价格,一个显示它们,因为它们是不同的实例,它们具有不同的值。

只需将显示的对象更改为计算出结果的对象即可解决显示问题,请参阅以下内容:

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