samedi 18 novembre 2017

Shorten nested if with else

Homework question. I've build an app that lets you calculate the ammount of discount.

3 radiobuttons.

rb_korting5  = 5% discount
rb_korting10 = 10% discount
rb_korting15 = 15% discount

and if your 65 or older you get an additonal +10 discount.

I need to shorten the follow code with else if. I've tried it but it keeps messing up the calculations.

public void button_Click(object sender, RoutedEventArgs e)
    {

        DateTime todate = DateTime.Today;
        int CurrentYear = todate.Year;
        int price = Convert.ToInt32(tb_Price.Text);
        int ammount = Convert.ToInt32(tb_Ammount.Text);
        double result = Convert.ToDouble(price * ammount);
        int dob = Convert.ToInt32(tb_dob.Text);
        int age = Convert.ToInt32(CurrentYear - dob);






        if ((rbkorting5.IsChecked == true) && ( age >= 65))         
        {
            result = result * 0.05 + 10;
        }

        if ((rbkorting5.IsChecked == true) && (age < 65))
        {
            result = result * 0.05;
        }


        if ((rbkorting10.IsChecked == true) && (age >= 65))
        {
            result = result * 0.10 + 10;
        }

        if ((rbkorting15.IsChecked == true) && (age >= 65))
        {
            result = result * 0.15 + 10;
        }


     if ((rbkorting10.IsChecked == true) && (age < 65))
        {
            result = result * 0.10;
        }

     if ((rbkorting15.IsChecked == true) && (age < 65))
        {
            result = result * 0.15;
        }



        lb_result.Content = result;

Aucun commentaire:

Enregistrer un commentaire