Home > Software engineering >  Problem with adding totals from user selected CheckBoxes
Problem with adding totals from user selected CheckBoxes

Time:12-03

I am creating a form that allows the user to select from a group of checkboxes for automotive services. In the form, the user selects from a list of priced services and a final total is calculated based on what is selected.

The logic of the selected services being added up is placed within a method that returns the total.

photo of form.

Once the user clicks on the calculate button, all selected prices will be added up and displayed by the total fees label.

    public partial class Automotive_Shop : Form
    {
        const int salesTax = (6 / 100);
        // prices for services
        const int
            oilChange = 26,
            lubeJob = 18,
            radiatorFlush = 30,
            transissionFlush = 80,
            inspection = 15,
            mufflerReplacement = 100,
            tireRotation = 20;

        int total = 0;

        public Automotive_Shop()
        {
            InitializeComponent();
        }




        private int OilLubeCharges()
        {
            if (oilChangeCheckBox.Checked == true)
            {
                total  = oilChange;
            }                 
            if (lubeJobCheckBox.Checked == true)
            {
                total  = lubeJob;
            }
          
            return total;
        }
           
      

      


        private void calculateButton_Click(object sender, EventArgs e)
        {
            totalFeesOutput.Text = OilLubeCharges().ToString("C");
                
        }

        private void exitButton_Click(object sender, EventArgs e)
        {
            // close application
            this.Close();
        }
    }

The total should only be added once.

For instance: if the "oil change" check box is selected, then the total should be $26.

if the "lube job" check box is selected, then the total should be $18.

And if both check boxes are selected, then the total should be $44.

What ends up happening is that after the first check box is selected and the calculate button is clicked, the "total" variable value continues to be added up.

So if i select "oil change" then click calculate, I get $26. if I deselect it and select "lube job" the total doesn't equal $18, but $44.

CodePudding user response:

To fix this problem, the total variable needs to be reset to 0 before the calculation happens.

The calculate button click event should be updated to look like this:

private void calculateButton_Click(object sender, EventArgs e)
{
    total = 0;
    totalFeesOutput.Text = OilLubeCharges().ToString("C");
}

CodePudding user response:

A function should be responsible for whatever it does. So everything it does should be there, nothing more and nothing less.

This means when a member function is calculating a total, and in the comments you refer to it as a subtotal, this is what the function should do.

Therefore you declare in that function int subtotal = 0; and return that.

Then you could store this to a member variable if you like.


As an example regarding your comments, I have added the member function int ApplyDiscount(...).

The only thing it does is applying a discount to 'a' subtotal you pass in. It should be improved for a working application.


At the button click the OilLubeCharges are calcultated, then that is passed into ApplyDiscount.

This return value you could store at a global variable. Both you could to event specify the full cost, the discount in price and the total.

        // Added a functionality due to the comments
        // When `discount` is 0.2f for 20%
        private int ApplyDiscount(int subtotal, float discount)
        {
            return (int)( subtotal - ( subtotal * discount ) );
        }

        private int OilLubeCharges()
        {

            int subtotal = 0;

            if (oilChangeCheckBox.Checked == true)
            {
                subtotal  = oilChange;
            }                 
            if (lubeJobCheckBox.Checked == true)
            {
                subtotal  = lubeJob;
            }
          
            return subtotal;

        }

        ...

        private void calculateButton_Click(object sender, EventArgs e)
        {

            int subtotal = OilLubeCharges();
            int total = ApplyDiscount(subtotal, 0.2f);

            totalFeesOutput.Text = total.ToString("C");
                
        }
  • Related