Last active
August 29, 2015 14:08
-
-
Save mooman219/68e5c9dc826b56cdd46c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.swing.JOptionPane; | |
public class DriverMortgageClass { | |
private Mortgage mortgage; | |
public void start() { | |
double yearlyInterestRate = 0; | |
int numberOfYears = 0; | |
double loanAmount = 0; | |
yearlyInterestRate = Double.parseDouble(JOptionPane.showInputDialog("Enter yearly interest rate, for example 8.50:")) / 100; | |
numberOfYears = Integer.parseInt(JOptionPane.showInputDialog("Enter number of years as an integer, for example 5:")); | |
loanAmount = Double.parseDouble(JOptionPane.showInputDialog("Enter loan amount, for example 10000.00:")); | |
mortgage = new Mortgage(yearlyInterestRate, numberOfYears, loanAmount); | |
JOptionPane.showMessageDialog(null, "Monthly payment is " + String.format("%.2f", mortgage.monthlyPayment()) | |
+ "\nThe total payment is " + String.format("%.2f", mortgage.totalPayment())); | |
} | |
/** | |
* Main method | |
*/ | |
public static void main(String[] args) { | |
DriverMortgageClass driver = new DriverMortgageClass(); | |
driver.start(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Mortgage { | |
private double annualInterestRate; | |
private int numberOfYears; | |
private double loanAmount; | |
public Morgage() { | |
this(0.085, 10, 10000); | |
} | |
public Morgage(double annualInterestRate, int numberOfYears, double loanAmount) { | |
this.annualInterestRate = annualInterestRate; | |
this.numberOfYears = numberOfYears; | |
this.loanAmount = loanAmount; | |
} | |
public double getAnnualInterestRate() { | |
return annualInterestRate; | |
} | |
public void setAnnualInterestRate(double annualInterestRate) { | |
this.annualInterestRate = annualInterestRate; | |
} | |
public int getNumberOfYears() { | |
return numberOfYears; | |
} | |
public void setNumberOfYears(int numberOfYears) { | |
this.numberOfYears = numberOfYears; | |
} | |
public double getLoanAmount() { | |
return loanAmount; | |
} | |
public void setLoanAmount(double loanAmount) { | |
this.loanAmount = loanAmount; | |
} | |
public double monthlyPayment() { | |
double monthlyInterestRate = annualInterestRate / 12; | |
return loanAmount * (monthlyInterestRate / (1 - Math.pow(1 + monthlyInterestRate, -numberOfYears * 12))); | |
} | |
public double totalPayment() { | |
return monthlyPayment() * numberOfYears * 12; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment