/* * Serial_loan.java * * Created on 17 August 2007 * * The serial loan class. */ package loan; /** * * @author merethe */ public class Serial_loan extends Payment{ double[] payments; //Array of amount to pay each month /** Creates a new instance of Serial_loan */ public Serial_loan(){ } /** * Calculate the payment * @param years Number of years * @param money Amount of money * @param interest Interest per year */ public String calculate(int years, int money, double interest){ String calculation = ""; double each_month; each_month = money / (years * 12); double interest_pr_month; interest_pr_month = interest/12; payments = new double[(years * 12) + 1]; double calc_interest = 0; //interest pr month double calc_sum = 0; //to pay each month double sum = money; //Calculate money plus interest for all the months for(int i = 1; i <= years*12; i++){ calc_interest = (sum * interest_pr_month) / 100; calc_sum = each_month + calc_interest; payments[i] = calc_sum; sum = sum - each_month; } calculation = show_calculation(years); return calculation; } /** * Calculate the payment and return it as a String * @param years Number of years */ public String show_calculation(int years){ String calculation = ""; int month = 1; for(int i = 1; i <= years*12; i++){ if((i-1) % 12 == 0){ //Each year int year = ((i / 12) + 1); calculation = calculation + "\n\nYear " + year; month = 1; } calculation = calculation + "\nMonth " + month + ":\t" + payments[i]; month++; } return calculation; } }