计算2次单独的计算

时间:2016-07-28 13:43:15

标签: c# asp.net wpf

我正在申请一家保险公司。它由comboBoxdatePicker组成。 comboBix由司机和会计师列表组成。该政策起价为500英镑。如果用户是司机,则如果用户是会计,则策略将增加10%,用户策略将减少10%。如果用户在21到25之间,则如果用户在26到75之间,策略将增加20%,则策略​​将减少10%。我有这些计算工作,但由于某种原因,我的年龄计算覆盖了整个政策。例如,如果用户是司机并且在21到25之间,则策略应该上升10%然后再增加20%,但是我的策略仅增加20%。我想我需要一个柜台,但我不确定我是否需要一个,如果是这样的话,我不确定如何制作一个。感谢

我的代码就像休耕一样

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

2 个答案:

答案 0 :(得分:1)

您永远不会修改政策价值。

例如:

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

这不会将策略更改为更新后的值。

尝试使用此代码:

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



    }

或者,如果您不想修改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();
    }



}

答案 1 :(得分:1)

您没有更新政策价值,只更新了文字。其次,您确定要在从组合中选择一个时总结所有策略吗?如果没有,那么改变政策的范围:

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