Dfournier1
Dfournier1

Reputation: 25

Second day Price will not calculate within Windows Forms C#

So I am near the end of my project and I am currently running into an issue of where if a user wanted to choose the number of adults, children, days, and parking pass. The part in which is giving me issues is when the user wants two days, the total amount will remain at "0.00" below is my current code.

private void btnCalcuate_Click(object sender, EventArgs e)
        {


            double dblAdultPrice = 0; //adult price

            double dblChildPrice = 0; //child price

            

            if (lstAdult.SelectedIndex == -1)
            {
                //User Selected no Adult
                MessageBox.Show("Please choose an Adult value");
            }
            else
            {
                dblAdultPrice = 49 * lstAdult.SelectedIndex; // this could require a data type conversion               

            }

            // get child prices
            if (lstChildren.SelectedIndex == -1)
            {
                //User Selected no Adult
                MessageBox.Show("Please choose a Child value");
            }
            else
            {

                dblChildPrice = 42 * lstChildren.SelectedIndex;

            }

            // calculate day
            if (rdoSingle.Checked)

            {
                double dblSecondaryPrice = 0;
                {
                }
                
                double dblSecondDayPrice = 0;

                if (rdoTwo.Checked)
                {
                    dblSecondDayPrice = 25.00;  
                    {
                    }

                    //parking pass
                    double dblParkingPrice = 0;

                    if (ChkParking.Checked)
                    {
                        dblParkingPrice = 15.00;  
                    }
                    //calculate
                    double dblGrandTotal = dblAdultPrice + dblSecondaryPrice + dblChildPrice + dblSecondDayPrice + dblParkingPrice;


                    lblTotal.Text = dblGrandTotal.ToString("N2");
                }
            }
        }
    }
}

Upvotes: 1

Views: 55

Answers (1)

ADyson
ADyson

Reputation: 61849

It's hard to be 100% certain without seeing the form or being sure exactly what the business rules are, but to me it looks like the problem is a logic one - you've put the code which checks whether the second radio button is selected inside the code which checks if the first radio button is selected. Normally radio buttons are mutually exclusive, so it's impossible for both to be selected at the same time.

Therefore, rdoSingle.Checked and rdoTwo.Checked can never both be true at the same time, which means that it will never be able to reach the code inside the rdoTwo.Checked section.

I think this is probably more like what you need:

private void btnCalcuate_Click(object sender, EventArgs e)
{
    double dblAdultPrice = 0; //adult price
    double dblChildPrice = 0; //child price

    if (lstAdult.SelectedIndex == -1)
    {
        //User Selected no Adult
        MessageBox.Show("Please choose an Adult value");
    }
    else
    {
        dblAdultPrice = 49 * lstAdult.SelectedIndex; // this could require a data type conversion               
    }

    // get child prices
    if (lstChildren.SelectedIndex == -1)
    {
        //User Selected no Adult
        MessageBox.Show("Please choose a Child value");
    }
    else
    {
        dblChildPrice = 42 * lstChildren.SelectedIndex;
    }

    // calculate day
    double dblSecondaryPrice = 0;
    double dblSecondDayPrice = 0;

    if (rdoTwo.Checked)
    {
        dblSecondDayPrice = 25.00;  
    }

    //parking pass
    double dblParkingPrice = 0;

    if (ChkParking.Checked)
    {
        dblParkingPrice = 15.00;  
    }

    //calculate
    double dblGrandTotal = dblAdultPrice + dblSecondaryPrice + dblChildPrice + dblSecondDayPrice + dblParkingPrice;
    lblTotal.Text = dblGrandTotal.ToString("N2");
}

(N.B. Your dblSecondaryPrice variable seems to be redundant, because it's always 0. Also for calculations relating to money you should really be using the decimal data type instead of double.)

Upvotes: 2

Related Questions