2016-07-28 92 views
1

我正在爲一家保險公司提出申請。它由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); 
     } 
    } 

回答

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



    } 

或者,如果你不想改變政策變量,使用:

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