2017-06-15 75 views
-3

我正在为一个副项目创建工资系统。我使用嵌套的if语句来查看用户选择的结算计划(52或26),然后计算总工资,税收和其他扣除额。 52周的工资计划正常工作,但26周的工资计划没有显示任何价值。嵌套如果陈述不正确显示

double hoursWorked = Double.parseDouble(txtHoursWorked.getText()); 
    double hourlyRate = Double.parseDouble(txtHourlyPay.getText()); 
    double overtimeHours = Double.parseDouble(txtOvertimeHours.getText()); 
    double overtimeRate = Double.parseDouble(txtOvertimePay.getText()); 

    // Declare variables 
    double basicPay; 
    double overtimePay; 
    double grossPay; 

    double taxes; 
    double yearlyCompensation; 
    double cpp; 
    double ei; 
    double deductions; 
    double netPay; 


    if (cmbPayPeriod.getSelectedItem().equals("52 Week Pay Period")) { 

     grossPay = basicPay + overtimePay; 
     txtGrossPay.setText(x.format(grossPay)); 

     yearlyCompensation = grossPay * 52; 

     if (yearlyCompensation < 45282) { 

      taxes = (yearlyCompensation * 0.15)/52; 
      cpp = (yearlyCompensation * 0.0495)/52; 
      ei = (yearlyCompensation * 0.0163)/52; 

      deductions = taxes + cpp + ei; 

      netPay = grossPay - deductions; 

      txtTaxP.setText(x.format(taxes)); 
      txtCPP.setText(x.format(cpp)); 
      txtEI.setText(x.format(ei)); 
      txtDeductions.setText(x.format(deductions)); 
      txtNetPay.setText(x.format(netPay)); 

     } 

     else if (yearlyCompensation < 90536) { 

      taxes = (yearlyCompensation * 0.205)/52; 
      cpp = (yearlyCompensation * 0.0495)/52; 
      ei = (yearlyCompensation * 0.0163)/52; 

      deductions = taxes + cpp + ei; 

      netPay = grossPay - deductions; 

      txtTaxP.setText(x.format(taxes)); 
      txtCPP.setText(x.format(cpp)); 
      txtEI.setText(x.format(ei)); 
      txtDeductions.setText(x.format(deductions)); 
      txtNetPay.setText(x.format(netPay)); 
     } 

     else if (yearlyCompensation < 140388) { 

      taxes = (yearlyCompensation * 0.265)/52; 
      cpp = (yearlyCompensation * 0.0495)/52; 
      ei = (yearlyCompensation * 0.0163)/52; 

      deductions = taxes + cpp + ei; 

      netPay = grossPay - deductions; 

      txtTaxP.setText(x.format(taxes)); 
      txtCPP.setText(x.format(cpp)); 
      txtEI.setText(x.format(ei)); 
      txtDeductions.setText(x.format(deductions)); 
      txtNetPay.setText(x.format(netPay)); 
     } 

     else if (yearlyCompensation < 200000) { 

      taxes = (yearlyCompensation * 0.29)/52; 
      cpp = (yearlyCompensation * 0.0495)/52; 
      ei = (yearlyCompensation * 0.0163)/52; 

      deductions = taxes + cpp + ei; 

      netPay = grossPay - deductions; 

      txtTaxP.setText(x.format(taxes)); 
      txtCPP.setText(x.format(cpp)); 
      txtEI.setText(x.format(ei)); 
      txtDeductions.setText(x.format(deductions)); 
      txtNetPay.setText(x.format(netPay)); 
     } 

     else { 

      taxes = (yearlyCompensation * 0.33)/52; 
      cpp = (yearlyCompensation * 0.0495)/52; 
      ei = (yearlyCompensation * 0.0163)/52; 

      deductions = taxes + cpp + ei; 

      netPay = grossPay - deductions; 

      txtTaxP.setText(x.format(taxes)); 
      txtCPP.setText(x.format(cpp)); 
      txtEI.setText(x.format(ei)); 
      txtDeductions.setText(x.format(deductions)); 
      txtNetPay.setText(x.format(netPay)); 
     } 


    if (cmbPayPeriod.getSelectedItem().equals("n")) { 

     grossPay = (basicPay + overtimePay) * 2; 
     txtGrossPay.setText(x.format(grossPay)); 

     yearlyCompensation = grossPay * 26; 

     if (yearlyCompensation < 45282) { 


      taxes = (yearlyCompensation * 0.15)/52; 
      cpp = (yearlyCompensation * 0.0495)/52; 
      ei = (yearlyCompensation * 0.0163)/52; 

      deductions = taxes + cpp + ei; 

      txtTaxP.setText(x.format(taxes)); 
      txtCPP.setText(x.format(cpp)); 
      txtEI.setText(x.format(ei)); 
      txtDeductions.setText(x.format(deductions));  

     } 

     else if (yearlyCompensation < 90536) { 


      taxes = (yearlyCompensation * 0.205)/52; 
      cpp = (yearlyCompensation * 0.0495)/52; 
      ei = (yearlyCompensation * 0.0163)/52; 

      deductions = taxes + cpp + ei; 

      txtTaxP.setText(x.format(taxes)); 
      txtCPP.setText(x.format(cpp)); 
      txtEI.setText(x.format(ei)); 
      txtDeductions.setText(x.format(deductions)); 
     } 

     else if (yearlyCompensation < 140388) { 

      taxes = (yearlyCompensation * 0.265)/52; 
      cpp = (yearlyCompensation * 0.0495)/52; 
      ei = (yearlyCompensation * 0.0163)/52; 

      deductions = taxes + cpp + ei; 

      txtTaxP.setText(x.format(taxes)); 
      txtCPP.setText(x.format(cpp)); 
      txtEI.setText(x.format(ei)); 
      txtDeductions.setText(x.format(deductions)); 

     } 

     else if (yearlyCompensation < 200000) { 

      taxes = (yearlyCompensation * 0.29)/52; 
      cpp = (yearlyCompensation * 0.0495)/52; 
      ei = (yearlyCompensation * 0.0163)/52; 

      deductions = taxes + cpp + ei; 

      txtTaxP.setText(x.format(taxes)); 
      txtCPP.setText(x.format(cpp)); 
      txtEI.setText(x.format(ei)); 
      txtDeductions.setText(x.format(deductions)); 
     } 

     else { 

      taxes = (yearlyCompensation * 0.33)/52; 
      cpp = (yearlyCompensation * 0.0495)/52; 
      ei = (yearlyCompensation * 0.0163)/52; 

      deductions = taxes + cpp + ei; 

      txtTaxP.setText(x.format(taxes)); 
      txtCPP.setText(x.format(cpp)); 
      txtEI.setText(x.format(ei)); 
      txtDeductions.setText(x.format(deductions)); 


     } 
    } 
    } 
+1

没有人想要读取200行代码。请减少到[mcve]。 –

+0

当你用调试器遍历代码时发生了什么? – Kon

+0

你确定这个测试是你想要的吗?if(cmbPayPeriod.getSelectedItem()。equals(“n”))'? –

回答

0

您的代码包含许多重复项,这使得它很难读取,也很难调试。

本质上,它分解成六个部分:

  • 解析输入

    double hoursWorked = Double.parseDouble(txtHoursWorked.getText()); 
    double hourlyRate = Double.parseDouble(txtHourlyPay.getText()); 
    double overtimeHours = Double.parseDouble(txtOvertimeHours.getText()); 
    double overtimeRate = Double.parseDouble(txtOvertimePay.getText()); 
    
  • 工资计算,而不是实际的代码,您显示

    double basicPay = hoursWorked * hourlyRate; 
    double overtimePay = overtimeHours * overtimeRate; 
    
  • 毛按计划付款计算

    double grossPay; 
    double yearlyCompensation; 
    if (cmbPayPeriod.getSelectedItem().equals("52 Week Pay Period")) { 
        grossPay = basicPay + overtimePay; 
        yearlyCompensation = grossPay * 52; 
    } else if (cmbPayPeriod.getSelectedItem().equals("n")) { 
        grossPay = (basicPay + overtimePay) * 2; 
        yearlyCompensation = grossPay * 26; 
    } else { 
        //this must be an error 
        //just bail out: 
        return; 
    } 
    

(您当前的代码不显示,如果用户选择既不是“52周的支付期”,也不是“N”会怎样做 - 我的代码处理为错误的中止计算。您可能需要做其他事情。)

  • 确定税率

    double taxRate; 
    if (yearlyCompensation < 45282) { 
        taxRate = 0.15; 
    } else if (yearlyCompensation < 90536) { 
        taxRate = 0.205; 
    } else if (yearlyCompensation < 140388) { 
        taxRate = 0.265; 
    } else if (yearlyCompensation < 200000) { 
        taxRate = 0.29; 
    } else { 
        taxRate = 0.33; 
    } 
    
  • 计算税额和净工资

    double taxes = (yearlyCompensation * taxRate)/52; 
    double cpp = (yearlyCompensation * 0.0495)/52; 
    double ei = (yearlyCompensation * 0.0163)/52; 
    double deductions = taxes + cpp + ei; 
    double netPay = grossPay - deductions; 
    
  • 显示效果

    txtGrossPay.setText(x.format(grossPay)); 
    txtTaxP.setText(x.format(taxes)); 
    txtCPP.setText(x.format(cpp)); 
    txtEI.setText(x.format(ei)); 
    txtDeductions.setText(x.format(deductions)); 
    txtNetPay.setText(x.format(netPay)); 
    

由于零件现在小得多,所以更容易发现错误嵌套条件等问题。