In: Computer Science
WPF application that calculates insurance rates using complex conditional statements.
Include the ability to enter the gender and age and click a button to calculate the insurance rate.
C#
Step 1 : open a new WPF Application. Name it as InsuranceCalculator.wpf
Step 2: Go to the Form window and Layout
Step: Create Labels and buttons as required
Label1.txt = "Please Enter the insurance amount"
Textbox1.txt = value entered by user
Label2.txt = "Please Enter the age"
Textbox2.txt = age
Lablel3.txt = " Please Enter the gender"
textbox3.txt = gender
Button1.txt = Calculate
Button2.txt = Cancel
C# code to be written as given below
using System;
using System.Windows;
using System.Windows.Controls;
namespace MyCalculatorv1
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
Button b = (Button) sender;
tb.Text += b.Content.ToString();
}
private void Result_click(object sender, RoutedEventArgs e)
{
try
{
calculatePremium(p);
}
catch (Exception ex1)
{
tb.Text = "Your Result is displayed";
}
}
private void Button_Click_2(object sender, RoutedEventArgs e)
{
Button b2 = (Button) sender;
tb2.Text += b2.Content.ToString();
}
private void Result_click2(object sender, RoutedEventArgs e)
{
try
{
calculatePremium(p);
}
catch (Exception ex2)
{
tb2.Text = "Please Enter the value again!";
}
}
public double calculatePremium(Person person) {
// Added base premium
double premium = 5000;
// Increase premium depending on age
if (person.getAge() >= 18) {
premium *= 1.1;
}
if (person.getAge() >= 25) {
premium *= 1.1;
}
if (person.getAge() >= 30) {
premium *= 1.1;
}
if (person.getAge() >= 35) {
premium *= 1.1;
}
if (person.getAge() >= 40) {
// Add 20% per 5 years above 40
int age = person.getAge() - 40;
while (age >= 5) {
premium *= 1.2;
age -= 5;
}
}
// Add gender specific adjustments
if (person.getGender() == Gender.MALE) {
premium *= 1.02;
}
}
}
}
return premium;
}
private void Off_Click_1(object sender, RoutedEventArgs e)
{
Application.Current.Shutdown();
}
private void Del_Click(object sender, RoutedEventArgs e)
{
tb.Text = "";
}
private void R_Click(object sender, RoutedEventArgs e)
{
if (tb.Text.Length > 0)
{
tb.Text = tb.Text.Substring(0, tb.Text.Length - 1);
}
}
}
}