-6

Hi guys so Whenever I run the code, I asks me to put in my name, number of hours worked and hourly wages. That's correct however when it does the calculations, the program uses the values in the default constructor and not the values I type in

import javax.swing.JOptionPane;
public class Payroll
{
private double rate;
private double hours;
private String name;
static double grossPay=0;
static double unionDues=0;
static double medicalCoverage=0;
static double ss=0;
static double medicare=0;
static double state=0;
static double federal=0;
static double netPay=0;
static int numPeople=0;

public Payroll()
{
    rate = 0;
    hours = 0;
    name = "Manmeet Singh";       
}


public void setName(String newName)
{
    name=newName;
}

public void setHours(double newHours)
{
    hours=newHours;
}

public void setRate(double newRate)
{
    rate=newRate;
}

public void calcPay()
{
    double ot=hours-40;
    double otRate=rate*1.5;
    double mediPay=34.61;
    double uniPay=15.25;
    double otgrossPay=((40*rate)+(otRate*ot));
    double noOtPay=(hours*rate);
    double otgrossIncome=((otgrossPay)-(34.61+15.25));
    double grossIncome=((noOtPay)-(34.61+15.25));

    if (hours>40)
  {
        JOptionPane.showMessageDialog(null,"Gross Pay for " +name +" is: $"+otgrossPay);
        JOptionPane.showMessageDialog(null,"Health Deduction is: $"+mediPay);
        JOptionPane.showMessageDialog(null,"Union Deduction is: $"+uniPay);
        if (otgrossIncome>=0 && otgrossIncome<=150)
        {
            System.out.println("Federal Tax Deduction is: $"+(otgrossIncome*.07));
            System.out.println("State Tax Deduction is: $"+(otgrossIncome*.002));
            System.out.println("Social Security Deduction is: $"+(otgrossIncome*.062));
            System.out.println("Medicare Deduction is: $"+(otgrossIncome*.0145));
            System.out.println("Net Pay is: $"+(otgrossIncome-((otgrossIncome*.0145)+(otgrossIncome*.062)+(otgrossIncome*.002)+(otgrossIncome*.07))));
        }
        else if (otgrossIncome>=150.01 && otgrossIncome<=300)
        {
            System.out.println("Federal Tax Deduction is: $"+(otgrossIncome*.098));
            System.out.println("State Tax Deduction is: $"+(otgrossIncome*.021));
            System.out.println("Social Security Deduction is: $"+(otgrossIncome*.062));
            System.out.println("Medicare Deduction is: $"+(otgrossIncome*.0145));
            System.out.println("Net Pay is: $"+(otgrossIncome-((otgrossIncome*.0145)+(otgrossIncome*.062)+(otgrossIncome*.021)+(otgrossIncome*.098))));
        }
        else if (otgrossIncome>=300.01 && otgrossIncome<=600)
        {
           System.out.println("Federal Tax Deduction is: $"+(otgrossIncome*.124));
            System.out.println("State Tax Deduction is: $"+(otgrossIncome*.0386));
           System.out.println("Social Security Deduction is: $"+(otgrossIncome*.062));
            System.out.println("Medicare Deduction is: $"+(otgrossIncome*.0145));
           System.out.println("Net Pay is: $"+(otgrossIncome-((otgrossIncome*.0145)+(otgrossIncome*.062)+(otgrossIncome*.0386)+(otgrossIncome*.124))));
        }
        else if (otgrossIncome>=600)
        {
            System.out.println("Federal Tax Deduction is: $"+(otgrossIncome*.1887));
            System.out.println("State Tax Deduction is: $"+(otgrossIncome*.055));
            System.out.println("Social Security Deduction is: $"+(otgrossIncome*.062));
           System.out.println("Medicare Deduction is: $"+(otgrossIncome*.0145));
            System.out.println("Net Pay is: $"+(otgrossIncome-((otgrossIncome*.0145)+(otgrossIncome*.062)+(otgrossIncome*.055)+(otgrossIncome*.1887))));
        }
        grossPay=otgrossPay+grossPay;
        unionDues=unionDues+unionDues;
        medicalCoverage=medicalCoverage+medicalCoverage;
        ss=0;
        medicare=0;
        state=0;
        federal=0;
        netPay=0;
        numPeople=0;
 }
    else if (hours<=40)
    {
        System.out.println("Gross Pay for " +name + " is: $"+noOtPay);
        System.out.println("Health Deduction is: $"+mediPay);
      System.out.println("Union Deduction is: $"+uniPay);
        if (grossIncome>=0 && grossIncome<=150)
        {
            System.out.println("Federal Tax Deduction is: $"+(grossIncome*.07));
          System.out.println("State Tax Deduction is: $"+(grossIncome*.002));
            System.out.println("Social Security Deduction is: $"+(grossIncome*.062));
            System.out.println("Medicare Deduction is: $"+(grossIncome*.0145));
            System.out.println("Net Pay is: $"+(grossIncome-((grossIncome*.0145)+(grossIncome*.062)+(grossIncome*.002)+(grossIncome*.07))));
        }
        else if (grossIncome>=150.01 && grossIncome<=300)
        {
            System.out.println("Federal Tax Deduction is: $"+(grossIncome*.098));
            System.out.println("State Tax Deduction is: $"+(grossIncome*.021));
            System.out.println("Social Security Deduction is: $"+(grossIncome*.062));
            System.out.println("Medicare Deduction is: $"+(grossIncome*.0145));
            System.out.println("Net Pay is: $"+(grossIncome-((grossIncome*.0145)+(grossIncome*.062)+(grossIncome*.021)+(grossIncome*.098))));
        }
        else if (grossIncome>=300.01 && grossIncome<=600)
        {
            System.out.println("Federal Tax Deduction is: $"+(grossIncome*.124));
            System.out.println("State Tax Deduction is: $"+(grossIncome*.0386));
            System.out.println("Social Security Deduction is: $"+(grossIncome*.062));
            System.out.println("Medicare Deduction is: $"+(grossIncome*.0145));
            System.out.println("Net Pay is: $"+(grossIncome-((grossIncome*.0145)+(grossIncome*.062)+(grossIncome*.0386)+(grossIncome*.124))));
        }
        else if (grossIncome>=600)
        {
            System.out.println("Federal Tax Deduction is: $"+(grossIncome*.1887));
            System.out.println("State Tax Deduction is: $"+(grossIncome*.055));
            System.out.println("Social Security Deduction is: $"+(grossIncome*.062));
            System.out.println("Medicare Deduction is: $"+(grossIncome*.0145));
           System.out.println("Net Pay is: $"+(grossIncome-((grossIncome*.0145)+(grossIncome*.062)+(grossIncome*.055)+(grossIncome*.1887))));
        }
        grossPay=noOtPay+grossPay;
        unionDues=unionDues+uniPay;
        medicalCoverage=mediPay+medicalCoverage;
        ss=0;
        medicare=0;
        state=0;
        federal=0;
        netPay=0;
        numPeople=0;
}
else
{
   System.out.println("You entered a wrong value");
}
} 

public static void main (String [] args)
{

    boolean y=true;
    Payroll singh = new Payroll();


       while(y==true)
   {
         String employeeName=JOptionPane.showInputDialog("Please enter the name");
         String howmanyHours=JOptionPane.showInputDialog("Please enter the number of hours worked ");    
         double hours = Double.parseDouble(howmanyHours);
         String whatWages= JOptionPane.showInputDialog("Please enter the hourly wage ");
         double rate = Double.parseDouble(whatWages);
         if (rate<8.75)
         {
             JOptionPane.showMessageDialog(null," Oh No!! \n You are not being paid the New York State minimum \n Try Again");
         }
         singh.calcPay();
    String x = JOptionPane.showInputDialog("Are there more employees?\n Type y below");
         if (x.equals("y") || x.equals("Y"))
    {
        y = true;
    }
    else 
    {
        y = false;
        System.out.println("Summary of totals: ");
        System.out.println("Number of employees on the payroll: ");
        System.out.println("Total Health: "+medicalCoverage);
        System.out.println("Total Union Dues: "+unionDues);
        System.out.println("Total Federal Taxes: ");
        System.out.println("Total State Taxes: ");
        System.out.println("Total Social Security Taxes: ");
        System.out.println("Total Medicare Taxes: ");
    }

   }

} }

2 Answers2

1

you are creating a new local variable

double hours = Double.parseDouble(howmanyHours);

which is hiding the class field of hours, and is not used anywhere else

GHollies
  • 86
  • 5
0

You are assigning the name, hours, and wages to variables declared in your main method instead of the ones belonging to your Payroll object. For instance:

double hours = Double.parseDouble(howmanyHours);

This should be:

singh.hours = Double.parseDouble(howmanyHours);

You need to fix the other variables meant to be assigned to singh in this way as well.

Bethany Louise
  • 646
  • 7
  • 13