سؤال

I am trying to make a small pizza order form, but I have a problem with the calculations. After selecting a pizza, the unit price and total calculations are OK but selecting additions introduces a problem. After changing the NumericUpDown value, the calories are not correct (all units have constant prices and calories). The NumericUpDown's name is numberofunit. How can I calculate them?

if (pepper.Checked)
{
    string peppereklendi = 
        Convert.ToString(Convert.ToDouble(unitprice.Text)+ pepperprice);

    unitprice.Text = peppereklendi;

    total.Text = 
        Convert.ToString(Convert.ToDecimal(unitprice.Text) * numberofunit.Value);

    string pepperkaloriekle = 
        Convert.ToString(Convert.ToInt16(gizlikalori.Text) + pepperkalori);

    gizlikalori.Text = pepperkaloriekle;

    amountofcalorie.Text = 
        Convert.ToString(Convert.ToDecimal(gizlikalori.Text) * numberofunit.Value);
}
else
{
    string peppereklendi = unitprice.Text;

    unitprice.Text = 
        Convert.ToString(Convert.ToDouble(peppereklendi) - pepperprice);

    total.Text = Convert.ToString(Convert.ToDecimal(unitprice.Text) * numberofunit.Value);

    string pepperkaloriekle = gizlikalori.Text;

    gizlikalori.Text = 
        Convert.ToString(Convert.ToDouble(pepperkaloriekle) - pepperkalori);

    amountofcalorie.Text = 
        Convert.ToString(Convert.ToDecimal(gizlikalori.Text) * numberofunit.Value);
}

This Code is pepper's checkbox code.

This is the form of my application.

هل كانت مفيدة؟

المحلول

You should really try to separate the calculation logic from the UI logic (the form). Then the things will become much clearer:

// Get values from the text boxes
decimal up = Convert.ToDecimal(unitprice.Text);
decimal calories = Convert.ToDecimal(gizlikalori.Text);
decimal tot, totCalories;

// Do the calculation
if (pepper.Checked) {
    up = up + pepperprice;
    calories = calories + pepperkalori;
}
tot = up * numberofunit.Value;
totCalories = calories * numberofunit.Value;

// Assign the results to text boxes
unitprice.Text = up.ToString();
total.Text = tot.ToString();
gizlikalori.Text = calories.ToString();
amountofcalorie.Text = totCalories.ToString();

What you do wrong, is that you subtract the pepper price and pepper calories from the unit price and the unit calories if no pepper is selected. However, the unit price (and calories) are without the pepper already!

I do not see when you are performing this calculation, however if you perform it every time you are increasing the number of units, then you would be adding the pepper price each time! It would probaly be better to have a separate variable for the base unit price that remains unchanged when you check additions. Then always start the calculation from the base unit price.

In addition, you are mixing many different number types. This makes no sense.

The next step to enhance the code even more would be to create a separate class for the calculations. You could also use data binding. This would remove completely the need for doing conversions. See my answer to the following post : manipulating-textbox-variables-in-calculations

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top