对2个单独的计算进行计数
本文关键字:计算 2个 单独 | 更新日期: 2023-09-27 17:59:08
我正在申请一家保险公司。它由一个comboBox
和一个datePicker
组成。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();
}
}
扩展.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();
}
}
或者,如果您不想修改策略变量,请使用以下选项:
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();
}