96 lines
2.8 KiB
Java
96 lines
2.8 KiB
Java
package Week3_Lab;
|
|
|
|
import java.util.*;
|
|
|
|
public class loan {
|
|
private double annualInterestRate;
|
|
private int numberOfYears;
|
|
private double loanAmount;
|
|
private java.util.Date loanDate;
|
|
|
|
public loan(){
|
|
//this.annualInterestRate = 2.5;
|
|
//this.numberOfYears = 1;
|
|
//this.loanAmount = 1000;
|
|
this(2.5, 1, 1000);
|
|
loanDate = new java.util.Date();
|
|
}
|
|
public loan(double annualInterestRate, int numberOfYears, double loanAmount){
|
|
this.annualInterestRate = annualInterestRate;
|
|
this.numberOfYears = numberOfYears;
|
|
this.loanAmount = loanAmount;
|
|
loanDate = new java.util.Date();
|
|
}
|
|
|
|
public double getAnnualInterestRate(){
|
|
return this.annualInterestRate;
|
|
}
|
|
|
|
public int getNumberOfYears(){
|
|
return this.numberOfYears;
|
|
}
|
|
|
|
public double getLoanAmount(){
|
|
return this.loanAmount;
|
|
}
|
|
|
|
public java.util.Date getLoanDate(){
|
|
return this.loanDate;
|
|
}
|
|
|
|
public void setAnnualInterestRate(double annualInterestRate){
|
|
this.annualInterestRate = annualInterestRate;
|
|
}
|
|
|
|
public void setNumberOfYears(int numberOfYears){
|
|
this.numberOfYears = numberOfYears;
|
|
}
|
|
|
|
public void setLoadAmount(double loanAmount){
|
|
this.loanAmount = loanAmount;
|
|
}
|
|
|
|
public double getMonthlyPayment(){
|
|
double monthlyInterestRate = annualInterestRate / 1200;
|
|
double monthlyPayment = loanAmount * monthlyInterestRate / (1 - (1 / Math.pow(1 + monthlyInterestRate, numberOfYears * 12)));
|
|
return monthlyPayment;
|
|
}
|
|
public double getTotalPayment(){
|
|
double totalPayment = getMonthlyPayment() * numberOfYears * 12;
|
|
return totalPayment;
|
|
}
|
|
|
|
public static void main(String[] args){
|
|
Scanner sc = new Scanner(System.in);
|
|
|
|
// Annual Interest
|
|
System.out.print("Enter annual interest rate, for example, 8.25: ");
|
|
double annualInterestRate = sc.nextDouble();
|
|
|
|
// Number of years
|
|
System.out.print("Enter number of years as an integer: ");
|
|
int numberOfYears = sc.nextInt();
|
|
|
|
// Loan Amount
|
|
System.out.print("Enter loan amount, for example, 120000.95: ");
|
|
double loanAmount = sc.nextDouble();
|
|
|
|
// Create a Loan object
|
|
loan loan1 = new loan(annualInterestRate, numberOfYears, loanAmount);
|
|
|
|
// Date
|
|
java.util.Date loanDate = loan1.getLoanDate();
|
|
System.out.println("The loan was created on " + loanDate);
|
|
|
|
|
|
// Monthly payment
|
|
double monthlyPayment = loan1.getMonthlyPayment();
|
|
System.out.printf("The monthly payment is %.2f\n", monthlyPayment);
|
|
|
|
// Total Payment
|
|
double totalPayment = loan1.getTotalPayment();
|
|
System.out.printf("The total payment is %.2f", totalPayment);
|
|
|
|
}
|
|
}
|