Home > Software design >  Salary C# Programming for loop
Salary C# Programming for loop

Time:09-22

I have a form that calculates the salary bonus for a salary entered in a text box (bonus outputs in a label). I want to add the two together using a for loop statement and display as a label output. How do I go about doing this?

This is what I have for code.

    void calculateBonus()
    {

        try
        {
            double salary = Convert.ToDouble(textBox1.Text.Trim());
            if (salary <= 80000)
            {
                labelOutput.Text = "Employee bonus is $"   (salary * 0.4);
            }
            else if (salary <= 100000)
            {
                labelOutput.Text = "Employee bonus is $"   (salary * 0.6);
            }
            else
            {
                labelOutput.Text = "Employee bonus is $"   (salary * 0.8);
            }
        }catch(Exception ex)
        {
            MessageBox.Show("Please enter a number");
        }
    }

    private void clearControls()
    {
        label1.Text = "";
        textBox1.Text = "";
        labelOutput.Text = "";
    }

    private void bonusToolStripMenuItem_Click(object sender, EventArgs e)
    {
        clearControls();
        titleLabel.Text = "Calculate bonus";
        label1.Text = "Enter employee salary amount";
        toPerform = calculateBonus;
    }

    private void exitToolStripMenuItem_Click(object sender, EventArgs e)
    {
        Close();
    }

CodePudding user response:

Better practice is having a calculateBonus() method accept a salary input and return the bonus as a result, keeping this separate from creating the string to put in a label (also: use decimal rather than double when working with money):

decimal calculateBonus(decimal salary)
{
    if (salary <= 80000.0m)  return salary * 0.4m;
    if (salary <= 100000.0m) return salary * 0.6m;
    return salary * 0.8m;
}

But it looks like you're also assigning the calculateBonus method to a variable/delegate/event handler. So we need a new method that also does the other work and matches that expected signature:

void showTotalCompensation()
{
    if (Decimal.TryParse(textBox1.Text.Trim(), out decimal salary))
    {
         decimal bonus = calculateBonus(salary);
         labelOutput.Text = $"Updated employee compensation is {salary bonus:C2}";
    }
    else 
    {
        MessageBox.Show("Please enter a number");
    }
}

Or we could invert these:

decimal calculateCompensation(decimal salary)
{
    if (salary <= 80000.0m)  return salary * 1.4m;
    if (salary <= 100000.0m) return salary * 1.6m;
    return salary * 1.8m;
}

void showBonus()
{
    if (Decimal.TryParse(textBox1.Text.Trim(), out decimal salary))
    {
         salary = calculateCompensation(salary);
         labelOutput.Text = $"Updated employee compensation is {salary:C2}";
    }
    else 
    {
        MessageBox.Show("Please enter a number");
    }
}

CodePudding user response:

I do not know if I understood the problem. Do you want to concatenate the two values (salary and bonus) in the labelOutput? Or when you say "add" you want to sum the 2 values?

labelOutput.Text = "Total value is $"   (salary    salary * 0.4);
  • Related