2016-04-11 10 views
1

Ich versuche Zeitplan Termine in meinem Loan Zeitplan zu haben, aber die Daten sind falsch, dass sie einige Monate übersprungen haben:Loan amortizarion Zeitplan mit Zeitplan Daten in Java

public static void printAmortizationSchedule(double principal, double annualInterestRate, int numYears) { 
     double interestPaid, principalPaid, newBalance; 
     double monthlyInterestRate, monthlyPayment; 
     int month; 
     int numMonths = numYears * 12; 

     // Output monthly payment and total payment 
     monthlyInterestRate = annualInterestRate/12; 
     monthlyPayment = monthlyPayment(principal, monthlyInterestRate, numYears); 
     System.out.format("Monthly Payment: %8.2f%n", monthlyPayment); 
     System.out.format("Total Payment: %8.2f%n", monthlyPayment * numYears * 12); 

     // Print the table header 
     printTableHeader(); 
     Calendar cal = Calendar.getInstance(); 
     cal.setTime(new Date()); 
     for (month = 1; month <= numMonths; month++) { 
      // Compute amount paid and new balance for each payment period 
      interestPaid = principal * (monthlyInterestRate/100); 
      principalPaid = monthlyPayment - interestPaid; 
      newBalance = principal - principalPaid; 
      cal.add(Calendar.MONTH, month); 
      SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-YYYY", Locale.getDefault()); 
      String dddate = sdf.format(cal.getTime()); 
      // Output the data item 
      printScheduleItem(month, dddate, interestPaid, principalPaid, newBalance); 

      // Update the balance 
      principal = newBalance; 
     } 
    } 

Druckablauf-Methode:

private static void printScheduleItem(int month, String dddate, double interestPaid, double principalPaid, double newBalance) { 
     System.out.format("%8d%12s%10.2f%10.2f%12.2f\n", 
       month, dddate, interestPaid, principalPaid, newBalance); 
    } 

Ich bin mir sicher, dass das Problem darin kommt, die Monate zu den Daten hinzuzufügen, wie kann ich das lösen?

+1

'cal.add (Calendar.MONTH, Monat);' 'sollte wohl sein cal.add (Calendar.MONTH , 1); ' – assylias

+0

Auch wenn Sie ein Datum benötigen (ohne Zeit), sollten Sie wahrscheinlich [ein' LocalDate'] (https://docs.oracle.com/javase/8/docs/api/java/time) verwenden /LocalDate.html) anstelle von 'Calendar'. – assylias

Antwort

1

Ihr Fehler ist hier:

cal.add(Calendar.MONTH, month); 

Es sollte:

cal.add(Calendar.MONTH, 1); 
+0

Das sieht so aus, danke. – ErrorNotFoundException

+0

@ErrorNotFoundException gute Nachrichten –