Export month element

Hello y’all,

Hope you guys are staying safe and enjoying great holidays.

I’d just like to ask you guys what is the best method to export the “month” element from MortgageCalculator to MortgageReport as shown below in order to see the number of the payment schedule by the month. (Currently, we don’t show the number of the months)

I would really appreciate your help.

public class MortgageCalculator {

public double [] getRemainingBalances() {
	
		var balances = new double [getNumberOfPayment()];
	
		for(short month = 1; month <= balances.length; month++)
		
		balances [month - 1] = calculateBalance(month);
		
		return balances;
}

public class MortgageReport {

public void printPaymentSchedule () {

System.out.println("Payment Schedule: ");

System.out.println("-------------------------");

System. out .println();

for(double balance : calculator.getRemainingBalances())

System.out.println(currency.format(balance));

}

Probably using a custom iterator (rather than returning the array of doubles).

That being said, you can use the index to know which month you are at. So do an index based loop rather than a for-each loop:

double[] balances = calculator.getRemainingBalances();
for (int month = 0; month < balances.length; month++) {
  // Do whatever you want to do with the month number
  System.out.println(currency.format(balance));
}