Java 用于计算销售额和直观显示客户订单的程序

Java 用于计算销售额和直观显示客户订单的程序,java,Java,你好,这里是我的项目提示 编写一个程序来计算销售额和利润 陈列在线牛仔零售商销售五种产品,其零售价格如下: 产品一:牛仔靴,74.99美元 产品2:牧马人,25.99美元 产品3:牛仔帽,24.95美元 产品4:Chaps,34.89美元 产品5:马刺队,12.50美元 编写一个应用程序,读取每个产品的数量,直到用户完成订单。您的程序应该使用switch、if、for、while和do-while语句来 读取、计算并显示所有pro的总零售价值 为每个用户交易销售的管道。用户应该能够在第一个事务完

你好,这里是我的项目提示

编写一个程序来计算销售额和利润 陈列在线牛仔零售商销售五种产品,其零售价格如下: 产品一:牛仔靴,74.99美元 产品2:牧马人,25.99美元 产品3:牛仔帽,24.95美元 产品4:Chaps,34.89美元 产品5:马刺队,12.50美元 编写一个应用程序,读取每个产品的数量,直到用户完成订单。您的程序应该使用switch、if、for、while和do-while语句来 读取、计算并显示所有pro的总零售价值 为每个用户交易销售的管道。用户应该能够在第一个事务完成后启动新事务。用户总共可以下不超过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);
}
}
公共类商品
{
私家车;
私人int wranglersQTY;
哈茨基私人酒店;
查普斯基私人酒店;
私人住宅;
私人双总;
私人双价;
私有int productNOM;
公共商品()
{
总价=价格=0.00;
productNOM=bootsQTY=wranglersQTY=hatsQTY=chapsQTY=spursQTY=0;
}
public int getBootsQTY()
{
返回bootsQTY;
}
public int getWranglersQTY()
{
返回牧马人数量;
}
public int getHatsQTY()
{
返回hatsQTY;
}
public int getChapsQTY()
{
返回chapsQTY;
}
public int getSpursQTY()
{
回报率;
}
公共双getTotal()
{
返回总数;
}
公共空立根数量(内部b)
{
bootsQTY=b;
}
公共无效setWranglersQTY(整数w)
{
牧马人数量=w;
}
公共空间设置(内部h)
{
hatsQTY=h;
}
公共无效设置(内部c)
{
chapsQTY=c;
}
公共无效设置Pursqty(整数s)
{
spursQTY=s;
}
公共无效inputOrder()
{

对于商品类中的(inti=0;i),您能否为每个商品价格添加一个实例变量,如下例所示

    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;
    }
并在切换情况下指定项目价格,如下例所示

    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;
    }
对于那些项目数量设置器方法,您能添加一个“+”吗,如下例所示

    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;
    }
   public void paintComponent(Graphics g)
   {
     Graphics2D g2 = (Graphics2D) g;
     merch.draw(g2);
    }
另一方面,在MerchandiseComponent类paintComponent方法中,您可以按以下方式进行更改吗

    if(!done)
    {
        total = (bootsPrice * bootsQTY) + (wranglersPrice * wranglersQTY) + (hatsPrice * hatsQTY) + (chapsPrice * chapsQTY) + (spursPrice * spursQTY);
        continue;
    }
   public void paintComponent(Graphics g)
   {
     Graphics2D g2 = (Graphics2D) g;
     merch.draw(g2);
    }

更改后,它会打印正确的数量和价值。希望它能帮助您。

您有两个商品类实例

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

只需将显示的对象更改为已计算出结果的对象,即可解决显示问题,请参见下文:

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

非常感谢,这帮了大忙