简体   繁体   中英

My desired output is not coming out correct

This is my desired input when both check boxes are selected:

* *Input: hours = 45, rate 10.00, both Medical/Dental and 401k check boxes are checked

Here is what I expect to be the output:

· Output: gross pay = 475.00, medical/dental deduction= 50.00, 401k deduction = 23.75, tax = 100.31, net pay = 300.94**

However, I receive this when I run my project after hitting the calculate button and selecting both check boxes (ignore the name):

Name: Joe Hours: 45 Rate: 10.00 Gross Pay: $400.00 Taxes: $112.50 Net Pay: $337.50 Medical/Dental deduction: $400.00 401k deduction: $20.00

Any help as to what am I doing wrong would be greatly appreciated. I can't seem to figure out the problem.

This is what I have in my project:

These variables are declared at the top of the code:

private const decimal TAX = 0.25m;
private string name = "";
private decimal Gross_pay;
private decimal Taxes;
private decimal Net_Pay;
private decimal annual_salary;
private int NumberOfEmployees;
private decimal deductionMed;
private decimal deduction401k ;

This is where the calculations occur:

private void CalcButton_Click(object sender, EventArgs e)
    {  // The “Calculate” button calculates gross pay, taxes, and net pay and then displays name, department, gross pay, taxes, and net pay using currency format for various amounts in the rich text box
        // Gross pay=  (hours * rate)
        // Taxes= (25% of gross pay)
        // Net pay (gross pay ?taxes)



        //calculate         


        Gross_pay = Convert.ToInt32(HoursTextBox.Text) * decimal.Parse(RateTextBox.Text);
        Taxes = TAX * Gross_pay;
        Net_Pay = Gross_pay - Taxes;

        annual_salary = Net_Pay;

        Taxes = TAX * (Gross_pay - (deductionMed + deduction401k));

        //overtime pay
        if (Convert.ToInt32(HoursTextBox.Text) >= 41)
        {
            Gross_pay = Convert.ToInt32(HoursTextBox.Text) * decimal.Parse(RateTextBox.Text) * 1.5m;
            DisplayOutPut.Text += "\nOvertime:" + Gross_pay.ToString("C") + "\n";
        }

        //Medical/Dental and 401k deductions...as well as tax collected.
        if (MedicalDentalDeductions.Checked)
        {
            deductionMed = Gross_pay = Convert.ToInt32(HoursTextBox.Text) * decimal.Parse(RateTextBox.Text) - 50.00m;
        }


        if (FourOneKDeduction.Checked)
        {
            deduction401k = Gross_pay * 0.05m;

        }


        //display
        DisplayOutPut.Text = "Name: "+ "";
        DisplayOutPut.Text += NameTextBox.Text + "\n";
        DisplayOutPut.Text += "Hours: " + HoursTextBox.Text + "\n";
        DisplayOutPut.Text += "Rate: " + RateTextBox.Text + "\n";
        DisplayOutPut.Text += "Gross Pay: " + Gross_pay.ToString("C") + "\n"; // Hours*Rate
        DisplayOutPut.Text += "Taxes: " + Taxes.ToString("C") + "\n";
        DisplayOutPut.Text += "Net Pay: " + Net_Pay.ToString("C");
        DisplayOutPut.Text += "\nMedical/Dental deduction: " + deductionMed.ToString("C") + "\n401k deduction: " + deduction401k.ToString("C");

        //handling the invalid inputs
        if (NameTextBox.Text == "")
        { MessageBox.Show("Name is missing.", "Error"); }

        if (Convert.ToInt32(HoursTextBox.Text) >= 70)
        { MessageBox.Show("Please Enter a Valid hour.", "Invalid data type."); }

        if (RateTextBox.Text == "" && (RateTextBox.Text == ","))
        { MessageBox.Show("Please Enter a valid amount.", "Invalid data type ($)"); }

        if (Convert.ToInt32(HoursTextBox.Text) >= 70)
        { MessageBox.Show("You have exceeded the maximum hours per week."); }

        else if (Convert.ToInt32(HoursTextBox.Text) < 10)
        { MessageBox.Show("You cannot input less than 10 hours."); }

        if (Convert.ToDecimal(RateTextBox.Text) < 9.75m)
        { MessageBox.Show("Please enter the minimum wage."); }



      }

These are the methods for the check boxes:

 private void MedicalDentalDeductions_CheckedChanged(object sender, EventArgs e)
{






    } 

    private void FourOneKDeduction_CheckedChanged(object sender, EventArgs e)
    {


      }

You should really split your calculations into separate methods as it is much easier to debug. In saying that, I have not tested the following, but each method should produce the values you're after. It is just a matter of building your DisplayOutPut using values derived from these methods (I want to leave some work for you to do).

The first thing you should be calculating is the gross pay:

private double calculateGrossPay (double hours, double rate)
{
    double result = 0.00;
    double standardHours = 0.00;
    double overtimeHours = 0.00;
    if (hours > 40)
    {
        overtimeHours = (hours - 40) * (rate * 1.5);
        standardHours = 40 * rate;
    }
    else
    {
        standardHours = hours * rate;
    }
    result = standardHours + overtimeHours;
    return result;
}

Then calculate the tax:

private double caculateTax (double gross, double tax)
{
    double result = 0.00;
    result = gross * tax; // assuming tax is represented as 0.25 for 25%
    return result;
}

Followed by calculating the deductions:

private double caclulate401k (double gross) // Or net?
{
    double result = 0.00;
    result = gross * 0.05;
    return result;
}

private double calculateMedical (double gross) // Or net?
{
    double result = 0.00;
    result = 50.00; // I figure this should be an actual calculation, if medical is always $50, waste of a method...
    return result;
}

Finally, calculate the net.

private double calculateNet (double gross, double tax, double med, double 401k)
{
    double result = 0.00;
    double deductions = tax + med + 401k;
    result = gross - deductions;
    return result;  
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM