Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * @author Max Tessier
- * @version NetBeans IDE 11.2
- * @course CPSC 1302
- */
- public class Main {
- //below I instantiate all variables. I tried after the constructor but it wasn't working.
- //This is probably not 'ideal' per coding conventions but it works
- private static final double PERIODYEAR = 12;
- private double lifeYear;
- private double initialLoan;
- private double intRate;
- private static final double INVALID = -1.0;
- private double periodTotal;
- private double monthRate;
- private double monthExp;
- private double division;
- private double monthPay;
- private double interestPaid = 0;
- private double interestPaidz = 0;
- private double currentBal;
- private double currentBalz;
- private double intMonth;
- private double intMonthz;
- private double monthBal;
- private double monthBalz;
- public Main(){ //empty constructor
- }
- public Main( double Balance, double Interest, double years){ //constructor 3 args
- initialLoan = Balance;
- intRate = Interest;
- lifeYear = years;
- currentBal = initialLoan;
- currentBalz = initialLoan;
- periodTotal = PERIODYEAR*lifeYear;
- monthRate = intRate/PERIODYEAR;
- monthExp = 1+monthRate;
- division = Math.pow(monthExp,periodTotal);
- monthPay = initialLoan * (monthRate + (monthRate / (division-1)));
- for(int i=0; i<periodTotal; i++){ //for loop to calculate rates per month
- intMonth = monthRate * currentBal;
- interestPaid = interestPaid + intMonth;
- monthBal = monthPay - intMonth;
- currentBal = currentBal - monthBal;
- }
- for (int k=0;k<=periodTotal - 1 ; k++){
- intMonthz = monthRate * currentBalz;
- interestPaidz = interestPaidz + intMonthz;
- monthBalz = monthPay - intMonthz;
- currentBalz = currentBalz - monthBalz;
- }
- }
- public double getMonthlyPayment(){ //delivers the base monthly payment, identical every month
- return Math.round(monthPay *100.0) / 100.0;
- }
- public double getTotalInterest(){ //delivers total paid towards interest, at the end of the loan
- return Math.round(interestPaid * 100.0) / 100.0;
- }
- public double getTotalPayments(){ //delivers total paid, monthly payment * # of payments
- return Math.round((monthPay*periodTotal)* 100.0) / 100.0;
- }
- public double getAmount(String month, String payment){ //different deliverable, based on input
- if (month.equalsIgnoreCase("first")){ // first month deliverables, based on 2nd input
- if (payment.equalsIgnoreCase("interest")){
- return Math.round((monthRate * initialLoan) * 100.0) / 100.0 ;
- }
- else if (payment.equalsIgnoreCase("principal")){
- return Math.round((monthPay - (monthRate*initialLoan))* 100.0) / 100.0;
- }
- else if (payment.equalsIgnoreCase("balance")){
- return Math.round((initialLoan-(monthPay - (monthRate*initialLoan))) * 100.0) / 100.0;
- }
- else {
- return INVALID;
- }
- }
- else if (month.equalsIgnoreCase("last")){ // calculating all values to last month, instead of entire loan
- if (payment.equalsIgnoreCase("interest")){
- return Math.round(intMonthz * 100.0) / 100.0;
- }
- else if (payment.equalsIgnoreCase("principal")){
- return Math.round((monthPay - intMonthz) * 100.0 ) / 100.0;
- }
- else if (payment.equalsIgnoreCase("balance")){
- return Math.round(currentBalz * 100.0) / 100.0;
- }
- else {
- return INVALID;
- }
- }
- else { //in the case the first input was invalid, returns invalid
- return INVALID;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement