Exemplo n.º 1
0
        private void BtnCalculate_Click(object sender, EventArgs e)
        {
            //pass the input values through the SalesPerson constructor
            aSalesPerson = new SalesPerson(textBoxFirstName.Text, textBoxLastName.Text, textBoxInsuranceAmount.Text);

            //Check validation
            if (!aSalesPerson.isValidationPassed())
            {
                if (!aSalesPerson.IsValidLastName(aSalesPerson.LastName))
                {
                    lNameValitionError.Text      = aSalesPerson.ErrorText;
                    lNameValitionError.ForeColor = Color.Red;
                    textBoxLastName.Focus();
                }
                else
                {
                    lNameValitionError.Text = "";
                }
                if (!aSalesPerson.IsValidFirstName(aSalesPerson.FirstName))
                {
                    fNameValidationError.Text      = aSalesPerson.ErrorText;
                    fNameValidationError.ForeColor = Color.Red;
                    textBoxFirstName.Focus();
                }
                else
                {
                    fNameValidationError.Text = "";
                }
                if (!aSalesPerson.CheckInsuranceAmountAmount(textBoxInsuranceAmount.Text))
                {
                    insuranceAmountValidationError.Text      = aSalesPerson.ErrorText;
                    insuranceAmountValidationError.ForeColor = Color.Red;
                    textBoxInsuranceAmount.Focus();
                }
                else
                {
                    insuranceAmountValidationError.Text = "";
                }


                //Display appropriate message if validation failed
                richTextBoxForResult.Text = "Validation failed..!!!";

                MessageBox.Show("Please enter the appropriate values");
            }

            else
            {
                insuranceAmountValidationError.Text = "";
                lNameValitionError.Text             = "";
                fNameValidationError.Text           = "";

                //Ensure positive insurance amount
                if (aSalesPerson.InsuranceAmount < 0)
                {
                    MessageBox.Show("Insurance amount cannot be negative");
                }
                else
                {
                    //Determine and display the result for the appropriate input values from user
                    SalesPersonUtility.DetermineCommissionAndRate(aSalesPerson);


                    richTextBoxForResult.Text = "Hello " + aSalesPerson.FirstName + " " + aSalesPerson.LastName
                                                + ", You have earned the following commission at a rate of " + aSalesPerson.Rate * 100 + "%"
                                                + "\n\n"
                                                + "Commission amount: " + aSalesPerson.IndividualCommissionEarnedPerSales + "\n"
                                                + "Commission rate: " + aSalesPerson.Rate * 100 + "%";

                    //Add or update the current sales person info to the repository based on the requirements
                    SalesPersonUtility.AddOrUpdateToSalesPersonRepo(aSalesPerson, salesPersonRepo);

                    //Count policies
                    countTotalPolicies++;
                }
            }
        }