为什么我不输入我的号码?

时间:2015-10-30 17:42:56

标签: java modifiers

大家好,所以每当我运行代码时,我都要求我输入我的名字,工作小时数和小时工资。这是正确的但是当它进行计算时,程序使用默认构造函数中的值而不是我输入的值

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 个答案:

答案 0 :(得分:1)

您正在创建一个新的本地变量

double hours = Double.parseDouble(howmanyHours);

隐藏了小时的类字段,并没有在其他任何地方使用

答案 1 :(得分:0)

您要将名称,小时数和工资分配给主方法中声明的变量,而不是属于double hours = Double.parseDouble(howmanyHours); 对象的变量。例如:

singh.hours = Double.parseDouble(howmanyHours);

这应该是:

singh

您还需要以这种方式修复要分配给.splice()的其他变量。