SHARE
TWEET

Untitled

a guest Feb 26th, 2020 83 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * @author Max Tessier
  3.  * @version NetBeans IDE 11.2
  4.  * @course CPSC 1302
  5.  */
  6.  
  7. public class Main {
  8.     //below I instantiate all variables. I tried after the constructor but it wasn't working.
  9.     //This is probably not 'ideal' per coding conventions but it works
  10.     private static final double PERIODYEAR = 12;
  11.     private double lifeYear;
  12.     private double initialLoan;
  13.     private double intRate;
  14.     private static final double INVALID = -1.0;
  15.     private double periodTotal;
  16.     private double monthRate;
  17.     private double monthExp;
  18.     private double division;
  19.     private double monthPay;
  20.     private double interestPaid = 0;
  21.     private double interestPaidz = 0;
  22.     private double currentBal;
  23.     private double currentBalz;
  24.     private double intMonth;
  25.     private double intMonthz;
  26.     private double monthBal;
  27.     private double monthBalz;
  28.  
  29.     public Main(){  //empty constructor
  30.                
  31.     }
  32.    
  33.     public Main( double Balance, double Interest, double years){ //constructor 3 args
  34.         initialLoan = Balance;
  35.         intRate = Interest;
  36.         lifeYear = years;
  37.         currentBal = initialLoan;
  38.         currentBalz = initialLoan;
  39.         periodTotal = PERIODYEAR*lifeYear;
  40.         monthRate = intRate/PERIODYEAR;
  41.         monthExp = 1+monthRate;
  42.         division = Math.pow(monthExp,periodTotal);
  43.         monthPay = initialLoan * (monthRate + (monthRate / (division-1)));
  44.    
  45.     for(int i=0; i<periodTotal; i++){  //for loop to calculate rates per month
  46.             intMonth = monthRate * currentBal;
  47.             interestPaid = interestPaid + intMonth;
  48.             monthBal = monthPay - intMonth;
  49.             currentBal = currentBal - monthBal;
  50.         }
  51.    
  52.     for (int k=0;k<=periodTotal - 1 ; k++){
  53.                 intMonthz = monthRate * currentBalz;
  54.                 interestPaidz = interestPaidz + intMonthz;
  55.                 monthBalz = monthPay - intMonthz;
  56.                 currentBalz = currentBalz - monthBalz;
  57.                
  58. }
  59.     }
  60.    
  61.    
  62.     public double getMonthlyPayment(){ //delivers the base monthly payment, identical every month
  63.         return Math.round(monthPay *100.0) / 100.0;
  64.     }
  65.     public double getTotalInterest(){ //delivers total paid towards interest, at the end of the loan
  66.         return Math.round(interestPaid * 100.0) / 100.0;
  67.     }
  68.     public double getTotalPayments(){ //delivers total paid, monthly payment * # of payments
  69.         return Math.round((monthPay*periodTotal)* 100.0) / 100.0;
  70.     }
  71.     public double getAmount(String month, String payment){ //different deliverable, based on input
  72.         if (month.equalsIgnoreCase("first")){ // first month deliverables, based on 2nd input
  73.             if (payment.equalsIgnoreCase("interest")){
  74.                 return Math.round((monthRate * initialLoan) * 100.0) / 100.0 ;
  75.             }
  76.             else if (payment.equalsIgnoreCase("principal")){
  77.                 return Math.round((monthPay - (monthRate*initialLoan))* 100.0) / 100.0;
  78.             }
  79.             else if (payment.equalsIgnoreCase("balance")){
  80.                 return Math.round((initialLoan-(monthPay - (monthRate*initialLoan))) * 100.0) / 100.0;
  81.             }
  82.             else {
  83.                 return INVALID;
  84.             }
  85.         }
  86.         else if (month.equalsIgnoreCase("last")){ // calculating all values to last month, instead of entire loan
  87.            
  88.                    
  89.                     if (payment.equalsIgnoreCase("interest")){
  90.                 return Math.round(intMonthz * 100.0) / 100.0;
  91.             }
  92.                     else if (payment.equalsIgnoreCase("principal")){
  93.                 return Math.round((monthPay - intMonthz) * 100.0 ) / 100.0;
  94.             }
  95.                     else if (payment.equalsIgnoreCase("balance")){
  96.                 return Math.round(currentBalz * 100.0) / 100.0;
  97.             }
  98.                     else {
  99.                 return INVALID;
  100.             }
  101.                 }        
  102.                        
  103.                                        
  104.         else { //in the case the first input was invalid, returns invalid
  105.             return INVALID;
  106.         }
  107.    
  108. }
  109. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top