2017-06-15 37 views
-3

私はサイドプロジェクトの給与計算システムを作成しています。私は入れ子のifステートメントを使用して、ユーザーが選択した請求スケジュール(52または26)を確認してから、総賃金、税金およびその他の控除を計算します。 52週の賃金表は正しく機能しますが、26週間の賃金表には値が表示されません。ネストされたIf文が正しく表示されない

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

コードには多くの重複が含まれているため、読み込みが難しくデバッグが難しくなります。

基本的にそれは6つの部分に分解:あなたは

double basicPay = hoursWorked * hourlyRate; 
double overtimePay = overtimeHours * overtimeRate; 
  • 総額を示したコードで実際に入力

    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 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」はどちらを選択した場合に行われるものを示していない - 私のコードはこれをエラーとして扱うAN計算を中止します。あなたは、何かを行う必要があります。)

    • 税率を決定

      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)); 
      
    を計算します

    パーツがずっと小さくなっているので、間違ってネストされた状態のような問題を見つけるのがはるかに簡単になります。

  • 関連する問題