I want to split the following into a Main class and do all the math in a mortagecalc class The Arrays are tripping me up im not sure how to pass these to another class.

Thank you

package mortgageCalc;

import java.text.DecimalFormat;

public class Mainbkp {

	/**
	 * @param args
	 * @throws InterruptedException 
	 */
	public static void main(String[] args) throws InterruptedException {
		
		
		double loan = 200000.00; 
		
		DecimalFormat df = new DecimalFormat("#,###.00");
		
		//Loan Term Array
	int[] loanTerm = new int[3];
					loanTerm[0]=7;
					loanTerm[1]=15;
					loanTerm[2]=30;

       //Interest Array for Loan 
     double [] interest = new double [3];
					interest[0] = .0535;
					interest[1] = .0550;
					interest[2] = .0575;
					
				
					for (int month = 0; month < loanTerm.length; month++)
					{
						double payment = (loan*(interest[month]/12))/(1-(Math.pow(1/(1+(interest[month]/12)),(loanTerm[month]*12)))); 
						//Display Text to explain values
						System.out.println("\nYour Mortgage Payment " + df.format(payment) + "\nfor the " + loanTerm[month] + " Year Contract "); 
						Thread.sleep(600);
					}
						
		}
}
class MortgageCalc {
    private int[] loanTerm = new int[3];
    private double [] interest = new double [3];
    private double loan = 200000.00;
    public MortgageCalc(){
        loanTerm[0]=7;
        loanTerm[1]=15;
        loanTerm[2]=30;
        interest[0] = .0535;
        interest[1] = .0550;
        interest[2] = .0575;
    }

    public int[] getLoanTerm() {
        return loanTerm;
    }

    public void setLoanTerm(int[] loanTerm) {
        this.loanTerm = loanTerm;
    }

    public double[] getInterest() {
        return interest;
    }

    public void setInterest(double[] interest) {
        this.interest = interest;
    }

    public double getLoan() {
        return loan;
    }

    public void setLoan(double loan) {
        this.loan = loan;
    }
}

public class test {
    public static void main(String[] args) throws InterruptedException {
        DecimalFormat df = new DecimalFormat("#,###.00");
        MortgageCalc mc = new MortgageCalc();
        for (int month = 0; month < mc.getLoanTerm().length; month++)
        {
            double payment = (mc.getLoan()*(mc.getInterest()[month]/12))/(1-(Math.pow(1/(1+(mc.getInterest()[month]/12)),(mc.getLoanTerm()[month]*12))));

            System.out.println("Your Mortgage Payment " + df.format(payment) + "for the " + mc.getLoanTerm()[month] + " Year Contract ");
            Thread.sleep(600);
        }

    }
}

Edited 4 Years Ago by crash1989: n/a

Thank you! for your help and time! i was on the right path i started with the getter / setter but i got stumped. I really want to try and keep my code as clean and re-usable as possible. This way it will help me stay organized in my head.

As a further improvement you can move the entire for loop calculation into the MortgageCalc class and even simplify code. That is the beauty of Java - abstraction :).

Thank you! for your help and time! i was on the right path i started with the getter / setter but i got stumped. I really want to try and keep my code as clean and re-usable as possible. This way it will help me stay organized in my head.

As a further improvement you can move the entire for loop calculation into the MortgageCalc class and even simplify code. That is the beauty of Java - abstraction :).

so to do this how would i handle the month variable in the println statement ?

Well .. what I mean is that you still use a for loop and inside it you call a function like this:
double payment = mc.getPayment(month);

you can move all the calculation out of the for loop and put it in a public method called getPayment in the MortgageCalc class.

Hope this is clear.

Edited 4 Years Ago by crash1989: n/a

ahh i see yes that makes sense now thank you for all your help.

This article has been dead for over six months. Start a new discussion instead.