计算小计,销售税和订单总额

问题描述 投票:-1回答:2

好吧,所以我确定之前已经问过,但我似乎找不到任何关系。我正在努力为学校创建一项任务。这是一个午餐订单菜单,您可以在其中下订单。我们必须计算小计,销售税和订单总额。我似乎无法找出正确的代码使用,我不是100%肯定在这里尝试什么。

 public partial class Form1 : Form
{

        decimal subtotal = 0m;
    decimal salesTax = 0m;
    decimal orderTotal = 0m; 
public Form1()
    {
        InitializeComponent();
        rdoBurger.Checked = true;
        rdoPizza.Checked = true;
        rdoSalad.Checked = true;

    }

    private void btnExit_Click(object sender, EventArgs e)
    {
        Close();
    }

    private void clearTotals()
    {

    }

    private void btnPlaceOrder_Click(object sender, EventArgs e)
    {
        if (sender is RadioButton)
        {
            clearTotals();
        }
        if (rdoBurger.Checked)
        {

            decimal subtotal = 6.95m;
            subtotal = Convert.ToDecimal(lblSubtotal.Text);
        }

这就是我所拥有的,但它没有显示实际的小计,它仍然是空白的。我在这里错过了什么?

enter image description here

c#
2个回答
0
投票

这不是一个糟糕的开始。这更像是我期望看到的:

    private void btnPlaceOrder_Click(object sender, EventArgs e)
    {
        // only ONE of these can be checked, so "else if" is used
        if (rdoBurger.Checked)
        {
            subtotal = 6.95m;
        }
        else if (rdoPizza.Checked)
        {
            subtotal = 5.95m;
        }
        else if (rdoSalad.Checked)
        {
            subtotal = 4.95m;
        }

        // multiple of these could be checked, so only "if" is used
        if (checkBox1.Checked)
        {
            subtotal = subtotal + 100.00m; // whatever this item costs
        }
        if (checkBox2.Checked)
        {
            subtotal = subtotal + 4.99m; // whatever this item costs
        }
        if (checkBox3.Checked)
        {
            subtotal = subtotal + 10.99m; // whatever this item costs
        }

        // compute the tax and the total:
        salesTax = subtotal * 0.0775m;
        orderTotal = subtotal + salesTax;

        // output it to your labels/textboxes?
        lblSubtotal.Text = "$" + subtotal.ToString("F2");
        lblSalesTax.Text = "$" + salesTax.ToString("F2");
        lblOrderTotal.Text = "$" + orderTotal.ToString("F2");
    }

0
投票

单选按钮一次只能在容器中选择一个按钮。在这种情况下,似乎GroupBox是容器。如果您有多组单选按钮,则可以将GroupBox用作容器,并在每个GroupBox中选择一个单选按钮。因此,您无法将所有单选按钮选中属性设置为true。

在你的btnPlaceOrder_Click中,发件人不可能是一个单选按钮。发件人是单击以运行事件代码的按钮。

    private void button1_Click(object sender, EventArgs e)
    {
        //Find the radio button that is selected
        RadioButton rButton = groupBox1.Controls.OfType<RadioButton>().FirstOrDefault(r => r.Checked == true);

        switch (rButton.Text)
        {
            case "Hamburger - $6.95":
                subTotal = 6.95m;
                break;
            case "Pizza - $5.95":
                subTotal = 5.95m;
                break;
            case "Salad - $4.95":
                subTotal = 4.95m;
                break;
        }

        //Add code to handle Add-on items
        //For example - The first check box is "Add Onions" - $0.50
        if (checkBox1.Checked)
            subTotal += .5m;

        lblSubTotal.Text = subTotal.ToString();
        decimal tax = subTotal * .0775m;
        lblTax.Text = tax.ToString();
        decimal total = subTotal + tax;
        lblTotal.Text = total.ToString();
    }
© www.soinside.com 2019 - 2024. All rights reserved.