繁体   English   中英

进行2次独立计算的计数

[英]Make a count for 2 separate calculations

我正在向一家保险公司提出申请。 它由comboBoxdatePicker comboBix由司机和会计师组成。 该保单的起价为500英镑。 如果用户是司机,则策略增加10%;如果用户是会计师,则用户策略减少10%。 如果用户在21到25之间,则策略增加20%;如果用户在26到75之间,则策略减少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);
        }
    }

您绝不会修改策略值。

例如:

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



}

您不是在更新策略值,而是仅更新文本。 其次,您确定要从组合中选择一项来汇总所有策略吗? 如果不是,则更改策略范围:

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