SHARE
TWEET

Untitled

a guest Nov 18th, 2019 106 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.Scanner;
  2.  
  3. public class MortgageCalculator
  4. {
  5.    
  6.     private final static int ASSUMED_YEARS = 20;
  7.     private final static int MONTHS_IN_YEAR = 12;
  8.     private final static int APR_CONVERSION_CONSTANT = 100;
  9.    
  10.     private final static String WELCOME_PROMPT = "Welcome to the mortgage calculator.";
  11.     private final static String PRINCIPAL_PROMPT = "Please enter the mortgage amount: ";
  12.     private final static String APR_PROMPT = "Please enter the annual interest rate (APR): ";
  13.     private final static String ASSUMING_X_YEARS_PROMPT = "Assuming a "+ASSUMED_YEARS+" year term, the monthly repayments would be ";
  14.     private final static String AFFORD_TO_PAY_PROMPT = "How much can you afford to pay per month? ";
  15.     private final static String TIME_TO_PAY_USING_SPECIFIED_AMOUNT_PROMPT = "If you pay %s per month your mortgage would be paid off in %s";
  16.     private final static String INVALID_DOUBLE_INPUT_PROMPT = "Invalid input! Please input a number greater or equal than 0!";
  17.     private final static String RUN_AGAIN_KEYWORD = "yes";
  18.     private final static String QUIT_KEYWORD = "no";
  19.     private final static String GO_AGAIN_PROMPT = "Would you like to use the mortgage calculator again ("+RUN_AGAIN_KEYWORD+"/"+QUIT_KEYWORD+")? ";
  20.     private final static String INVALID_GO_AGAIN_INPUT_PROMPT = "Invalid input! Please type '"+RUN_AGAIN_KEYWORD+"' or '"+QUIT_KEYWORD+"'!";
  21.    
  22.     private static Scanner input = new Scanner(System.in);
  23.     private static boolean quit = false;
  24.    
  25.    
  26.     public static void main(String[] args)
  27.     {
  28.         System.out.println(WELCOME_PROMPT);
  29.         while(!quit)
  30.         {
  31.             double principal = readDoubleFromUser(PRINCIPAL_PROMPT);
  32.             double apr = readDoubleFromUser(APR_PROMPT);
  33.             System.out.println(ASSUMING_X_YEARS_PROMPT+String.format("%.2f",calculateMonthlyRepayment(principal, ASSUMED_YEARS, apr)));
  34.             double ammountCanAffordToPayPerMonth = readDoubleFromUser(AFFORD_TO_PAY_PROMPT);
  35.             System.out.printf(TIME_TO_PAY_USING_SPECIFIED_AMOUNT_PROMPT+"\n", ammountCanAffordToPayPerMonth, calculateMonthsToRepayMortgage(principal, ammountCanAffordToPayPerMonth, apr ));
  36.             System.out.println(GO_AGAIN_PROMPT);
  37.             goAgainPrompt();
  38.         }
  39.         input.close();
  40.     }
  41.    
  42.    
  43.     private static double readDoubleFromUser(String prompt)
  44.     {
  45.         System.out.println(prompt);
  46.         double number = -1;
  47.         if(input.hasNextDouble())
  48.         {
  49.             number = input.nextDouble();
  50.             if(number >= 0)
  51.             {
  52.                 return number;
  53.             }
  54.             else
  55.             {
  56.                 return readDoubleFromUser(INVALID_DOUBLE_INPUT_PROMPT);
  57.             }
  58.         }
  59.         else
  60.         {
  61.             input.next();
  62.             return readDoubleFromUser(INVALID_DOUBLE_INPUT_PROMPT);
  63.         }
  64.     }
  65.  
  66.    
  67.     private static double calculateMonthlyRepayment(double amount, double duration, double APR)
  68.     {
  69.         double rate = APR/MONTHS_IN_YEAR/APR_CONVERSION_CONSTANT;
  70.         return  (amount * rate) / (1 - Math.pow(1 + rate, -duration*MONTHS_IN_YEAR));
  71.     }
  72.    
  73.     private static String calculateMonthsToRepayMortgage(double principal, double monthlyRepayment, double APR)
  74.     {
  75.         double base = (1+APR/MONTHS_IN_YEAR/APR_CONVERSION_CONSTANT);
  76.         double x = (1-((principal * (APR/MONTHS_IN_YEAR/APR_CONVERSION_CONSTANT)/monthlyRepayment)));
  77.         int monthsInTotal  = (int)Math.ceil(-log(base , x));
  78.        
  79.         int years = monthsInTotal/MONTHS_IN_YEAR;
  80.         int monthsLeftOver = monthsInTotal-(years*MONTHS_IN_YEAR);
  81.        
  82.         return years+(years == 1 ? " year ":" years ")+"and "+monthsLeftOver+(monthsLeftOver == 1 ? " month ":" months ");
  83.     }
  84.  
  85.     private static double log(double base, double x)
  86.     {
  87.         return (double) (Math.log(x) / Math.log(base));
  88.     }
  89.    
  90.     private static void goAgainPrompt()
  91.     {
  92.  
  93.         if(input.hasNext(RUN_AGAIN_KEYWORD))
  94.         {
  95.             input.next();
  96.         }
  97.         else if(input.hasNext(QUIT_KEYWORD))
  98.         {
  99.             quit = true;
  100.             input.next();
  101.         }
  102.         else
  103.         {
  104.             input.next();
  105.             System.out.println(INVALID_GO_AGAIN_INPUT_PROMPT);
  106.             goAgainPrompt();
  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