简体   繁体   English

进行2次独立计算的计数

[英]Make a count for 2 separate calculations

I am making an application for an insurance company. 我正在向一家保险公司提出申请。 It consists of a comboBox and a datePicker . 它由comboBoxdatePicker The comboBix is made up of a list of Chauffeur and Accountant. comboBix由司机和会计师组成。 The Policy starts of at £500. 该保单的起价为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%. 如果用户是司机,则策略增加10%;如果用户是会计师,则用户策略减少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%. 如果用户在21到25之间,则策略增加20%;如果用户在26到75之间,则策略减少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%. 例如,如果用户是司机,年龄在21到25岁之间,则该政策应上升10%,然后再上升20%,但是我的政策仅增长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 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 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 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: 或者,如果您不想修改Policy变量,请使用以下命令:

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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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