2017-06-15 110 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)); 
    

由於零件現在小得多,所以更容易發現錯誤嵌套條件等問題。