-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMortgageLoan.java
More file actions
169 lines (141 loc) · 5.86 KB
/
MortgageLoan.java
File metadata and controls
169 lines (141 loc) · 5.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import java.util.Scanner;
// TO DO:
// Closing for Mortgage Loan
// Applying for Mortgage Loan
// Checking if mortgage loan is greater than the user balance
// Fees?
public class MortgageLoan {
public static void main(user Person) {
Scanner scan = new Scanner(System.in);
// Asking for principal amount and checking if it is greater than 0.
System.out.println("Welcome! Please enter principal amount: ");
double userprincipal = scan.nextDouble();
while (userprincipal <= 0) {
System.out.println("Please enter a positive principal amount greater than 0. Try again!");
userprincipal = scan.nextDouble();
if (userprincipal > 0) {
break;
}
}
// Asking for annual rate and checking if it is greater than 0.
System.out.print("Enter Annual Rate of Interest (exclude the %): ");
double annualrate = scan.nextDouble();
while (annualrate <= 0) {
System.out.println("Please enter a positive interest rate greater than 0. Try again!");
annualrate = scan.nextDouble();
if (annualrate > 0) {
break;
}
}
// Asking for time period and checking if it is greater than 0.
System.out.print("Enter Time period in years : ");
double timeperiod = scan.nextDouble();
while (timeperiod <= 0) {
System.out.println("Please enter a positive time period (greater than 0). Try again!");
timeperiod = scan.nextDouble();
if (timeperiod > 0) {
break;
}
}
// Creating the object and calling the methods
MortgagePayment hello = new MortgagePayment(userprincipal, annualrate, timeperiod);
double monthlyInterest = hello.calculatemonthlyinterest();
double numberofmonths = hello.calculatenumberofmonths();
double monthlypaym = hello.calculatemonthlyMortgage(monthlyInterest, numberofmonths);
System.out.println("Monthly mortgage payment: " + " $" + monthlypaym);
double loanfee = hello.calculateloanOriginationFee();
System.out.println("Loan Origination Fee (0.5%): " + loanfee);
double totalpaym = hello.calculatetotalMortgage(monthlypaym, numberofmonths) + loanfee;
System.out.println("Total mortgage payment: " + " $" + totalpaym);
client.Menu(Person);
}
}
class MortgagePayment {
// Instance variables
private double principalAmount;
private double annualInterest;
private double timePeriod;
private user Person;
//private static user abc;
// Overloaded Constructor
public MortgagePayment(double principal, double interest, double time) {
principalAmount = principal;
annualInterest = interest;
timePeriod = time;
}
public MortgagePayment(double principal, double interest, double time, user abc) {
principalAmount = principal;
annualInterest = interest;
timePeriod = time;
Person = abc;
}
// Get methods
public double getprincipalAmount() {
return principalAmount;
}
public double getannualInterest() {
return annualInterest;
}
public double gettimePeriod() {
return timePeriod;
}
// Interesting methods
public double calculatemonthlyinterest() {
double monthlyInterest = (annualInterest / 100) / 12;
return monthlyInterest;
}
public double calculatenumberofmonths() {
double months = timePeriod * 12;
return months;
}
//public calculate total payments P [ i(1 + i)^n ] / [ (1 + i)^n – 1]- ***NEED TO FIX
public double calculatemonthlyMortgage(double monthlyInterestRate, double months) {
double first = Math.pow(1 + monthlyInterestRate, months);
double second = Math.pow(1 + monthlyInterestRate, months);
double monthlypay = (principalAmount * monthlyInterestRate * first) / (second - 1);
return monthlypay;
}
public double calculatetotalMortgage(double monthlymortgage, double months) {
double total = monthlymortgage * months;
return total;
}
public double calculateloanOriginationFee() {
double fee = 0.005 * principalAmount;
return fee;
}
public void determineAccount(double monthlypayment) {
System.out.println("Which of your accounts would you like to use to pay?");
Scanner keyboard = new Scanner(System.in);
System.out.println("1) Savings Account");
System.out.println("2) Checking Account");
System.out.println("3) Exit");
String userInput = keyboard.nextLine();
if (userInput.equals("1")) {
Person.getSavingsAccount().withdraw(monthlypayment, Person.getSavingsAccount().getBalance(), Person.getSavingsAccount().getMonthlyWithdrawals());
} else if (userInput.equals("2")) {
Person.getCheckingAccount().withdraw(monthlypayment, Person.getCheckingAccount().getBalance());
} //abc.getMoneyMarketAccount().withdraw(monthlypayment);
else if (userInput.equals("3")) {
System.out.println("Thank you for banking with Bing Bong Byron");
System.exit(0);
}
else {
System.out.println("Sorry! I did not understand that input, please try again.");
determineAccount(monthlypayment);
}
}
public boolean agree() {
Scanner keyboard2 = new Scanner(System.in);
System.out.print("Do you agree? y/n ");
String agree = keyboard2.nextLine();
if (agree.equalsIgnoreCase("y")) {
return true;
} else if (agree.equalsIgnoreCase("n")) {
System.out.println("Exiting...");
return false;
} else {
System.out.println("Input not understood, please try again!");
return agree();
}
}
}