简体   繁体   中英

Make a count for 2 separate calculations

I am making an application for an insurance company. It consists of a comboBox and a datePicker . The comboBix is made up of a list of Chauffeur and Accountant. The Policy starts of at £500. If the user is a Chauffeur the policy goes up by 10% if the user is an Accountant the users policy is decreased by 10%. If the user is between 21 and 25 the policy is increased by 20% if the user is between 26 and 75 the policy is decreased by 10%. I have these calculations working but for some reason the total Policy is being overwritten by my age calculation. For example if the user is a Chauffeur and is between 21 and 25 the policy should go up by 10% and then go up by another 20% however my Policy is only increasing by 20%. I think I need a counter but I'm not sure if I need one and if so I'm not sure how I would go about making one. Thanks

My Code is as fallows

xaml

  <ComboBox x:Name="cmbOccupation" Grid.Row="7" Grid.Column="1" HorizontalAlignment="Center" VerticalAlignment="Top" Width="120" Loaded="cmbOccupation_Loaded" />

        <DatePicker HorizontalAlignment="Center" Name="dpkDOB" Grid.Column="1" VerticalAlignment="Top" Grid.Row="10" />

        <TextBlock x:Name="txtPolicy" Grid.Row="2" HorizontalAlignment="Left" TextWrapping="Wrap" Text="" VerticalAlignment="Top"/>

xaml.cs

  enum Occumpation
    {
        Chauffeur,
            Accountant
    }

        int policy = 500;
        double Chauffeur = 0.10;
        double Accountant = 0.10;
        double age2125 = 0.20;
        double age2675 = 0.10;

        private void cmbOccupation_Loaded(object sender, RoutedEventArgs e)
        {
            // ... A List.
            List<string> occupation = new List<string>();
            occupation.Add(Occumpation.Chauffeur.ToString());
            occupation.Add(Occumpation.Accountant.ToString());


            // ... Get the ComboBox reference.
            var comboBox = sender as ComboBox;

            // ... Assign the ItemsSource to the List.
            comboBox.ItemsSource = occupation;

            // ... Make the first item selected.
            comboBox.SelectedIndex = 0;
        }

        private void btnAddDriver_Click(object sender, RoutedEventArgs e)
        {




            if (cmbOccupation.SelectedItem.ToString() == Occumpation.Chauffeur.ToString())
            {
                txtPolicy.Text =  (policy + policy * Chauffeur).ToString();
            }
            else if(cmbOccupation.SelectedItem.ToString()== Occumpation.Accountant.ToString())
            {
                txtPolicy.Text = (policy - policy * Accountant).ToString();
            }




            DateTime birthDate = Convert.ToDateTime(dpkDOB.SelectedDate);

            if (birthDate.Age().Years() > 21 && birthDate.Age().Years() < 26)
            {
                txtPolicy.Text = (policy + policy * age2125).ToString();
            }
            else if (birthDate.Age().Years() > 26 && birthDate.Age().Years() < 76)
            {
                txtPolicy.Text = (policy - policy * age2675).ToString();
            }



        }

Extensions.cs

  public static class Extensions
    {
        public static TimeSpan Age(this DateTime dt)
        {
            return (DateTime.Now - dt);
        }

        public static int Years(this TimeSpan ts)
        {
            return (int)((double)ts.Days / 365.2425);
        }
    }

You're Never modifying the policy value.

For example:

if (cmbOccupation.SelectedItem.ToString() == Occumpation.Chauffeur.ToString())
        {
            txtPolicy.Text =  (policy + policy * Chauffeur).ToString();
        }
        else if(cmbOccupation.SelectedItem.ToString()== Occumpation.Accountant.ToString())
        {
            txtPolicy.Text = (policy - policy * Accountant).ToString();
        }

this does not change policy to the updated value.

Try Using This Code:

private void btnAddDriver_Click(object sender, RoutedEventArgs e)
    {




        if (cmbOccupation.SelectedItem.ToString() == Occumpation.Chauffeur.ToString())
        {
            policy = (policy + policy*Chauffeur);
            txtPolicy.Text =  policy.ToString();
        }
        else if(cmbOccupation.SelectedItem.ToString()== Occumpation.Accountant.ToString())
        {
            policy = (policy - policy*Accountant);
            txtPolicy.Text = policy.ToString();
        }




        DateTime birthDate = Convert.ToDateTime(dpkDOB.SelectedDate);

        if (birthDate.Age().Years() > 21 && birthDate.Age().Years() < 26)
        {
            policy = (policy + policy*age2125);
            txtPolicy.Text = policy.ToString();
        }
        else if (birthDate.Age().Years() > 26 && birthDate.Age().Years() < 76)
        {
            policy = (policy - policy*age2675);
            txtPolicy.Text = policy.ToString();
        }



    }

Alternatively, If you don't want to modify the Policy variable, use this:

private void btnAddDriver_Click(object sender, RoutedEventArgs e)
{
    double tempPolicy = policy;



    if (cmbOccupation.SelectedItem.ToString() == Occumpation.Chauffeur.ToString())
    {
        tempPolicy = (tempPolicy + tempPolicy*Chauffeur);
        txtPolicy.Text =  tempPolicy.ToString();
    }
    else if(cmbOccupation.SelectedItem.ToString()== Occumpation.Accountant.ToString())
    {
        tempPolicy = (tempPolicy - tempPolicy*Accountant);
        txtPolicy.Text = tempPolicy.ToString();
    }




    DateTime birthDate = Convert.ToDateTime(dpkDOB.SelectedDate);

    if (birthDate.Age().Years() > 21 && birthDate.Age().Years() < 26)
    {
        tempPolicy = (tempPolicy + tempPolicy*age2125);
        txtPolicy.Text = tempPolicy.ToString();
    }
    else if (birthDate.Age().Years() > 26 && birthDate.Age().Years() < 76)
    {
        tempPolicy = (tempPolicy - tempPolicy*age2675);
        txtPolicy.Text = tempPolicy.ToString();
    }



}

You are not updating policy value but only the text. Second, are you sure you want to sum up all the policies as you select one from the combo? If not then change the scope of the policy:

private void btnAddDriver_Click(object sender, RoutedEventArgs e)
{

    decimal policy = 500M;
    decimal Chauffeur = 0.10M;
    decimal Accountant = 0.10M;
    decimal age2125 = 0.20M;
    decimal age2675 = 0.10M;

    if (cmbOccupation.SelectedItem.ToString() == Occumpation.Chauffeur.ToString())
    {
        policy += policy * Chauffeur;
    }
    else if (cmbOccupation.SelectedItem.ToString() == Occumpation.Accountant.ToString())
    {
        policy -= policy * Accountant;
    }

    DateTime? birthDate = dpkDOB.SelectedDate;
    if (birthDate != null)
    {
        if (birthDate.Age().Years() > 21 && birthDate.Age().Years() < 26)
        {
            policy += policy * age2125;
        }
        else if (birthDate.Age().Years() > 26 && birthDate.Age().Years() < 76)
        {
            policy -= policy * age2675;
        }
    }

    txtPolicy.Text = policy.ToString();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM