Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class MortgageCalculator
- {
- private final static int ASSUMED_YEARS = 20;
- private final static int MONTHS_IN_YEAR = 12;
- private final static int APR_CONVERSION_CONSTANT = 100;
- private final static String WELCOME_PROMPT = "Welcome to the mortgage calculator.";
- private final static String PRINCIPAL_PROMPT = "Please enter the mortgage amount: ";
- private final static String APR_PROMPT = "Please enter the annual interest rate (APR): ";
- private final static String ASSUMING_X_YEARS_PROMPT = "Assuming a "+ASSUMED_YEARS+" year term, the monthly repayments would be ";
- private final static String AFFORD_TO_PAY_PROMPT = "How much can you afford to pay per month? ";
- 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";
- private final static String INVALID_DOUBLE_INPUT_PROMPT = "Invalid input! Please input a number greater or equal than 0!";
- private final static String RUN_AGAIN_KEYWORD = "yes";
- private final static String QUIT_KEYWORD = "no";
- private final static String GO_AGAIN_PROMPT = "Would you like to use the mortgage calculator again ("+RUN_AGAIN_KEYWORD+"/"+QUIT_KEYWORD+")? ";
- private final static String INVALID_GO_AGAIN_INPUT_PROMPT = "Invalid input! Please type '"+RUN_AGAIN_KEYWORD+"' or '"+QUIT_KEYWORD+"'!";
- private static Scanner input = new Scanner(System.in);
- private static boolean quit = false;
- public static void main(String[] args)
- {
- System.out.println(WELCOME_PROMPT);
- while(!quit)
- {
- double principal = readDoubleFromUser(PRINCIPAL_PROMPT);
- double apr = readDoubleFromUser(APR_PROMPT);
- System.out.println(ASSUMING_X_YEARS_PROMPT+String.format("%.2f",calculateMonthlyRepayment(principal, ASSUMED_YEARS, apr)));
- double ammountCanAffordToPayPerMonth = readDoubleFromUser(AFFORD_TO_PAY_PROMPT);
- System.out.printf(TIME_TO_PAY_USING_SPECIFIED_AMOUNT_PROMPT+"\n", ammountCanAffordToPayPerMonth, calculateMonthsToRepayMortgage(principal, ammountCanAffordToPayPerMonth, apr ));
- System.out.println(GO_AGAIN_PROMPT);
- goAgainPrompt();
- }
- input.close();
- }
- private static double readDoubleFromUser(String prompt)
- {
- System.out.println(prompt);
- double number = -1;
- if(input.hasNextDouble())
- {
- number = input.nextDouble();
- if(number >= 0)
- {
- return number;
- }
- else
- {
- return readDoubleFromUser(INVALID_DOUBLE_INPUT_PROMPT);
- }
- }
- else
- {
- input.next();
- return readDoubleFromUser(INVALID_DOUBLE_INPUT_PROMPT);
- }
- }
- private static double calculateMonthlyRepayment(double amount, double duration, double APR)
- {
- double rate = APR/MONTHS_IN_YEAR/APR_CONVERSION_CONSTANT;
- return (amount * rate) / (1 - Math.pow(1 + rate, -duration*MONTHS_IN_YEAR));
- }
- private static String calculateMonthsToRepayMortgage(double principal, double monthlyRepayment, double APR)
- {
- double base = (1+APR/MONTHS_IN_YEAR/APR_CONVERSION_CONSTANT);
- double x = (1-((principal * (APR/MONTHS_IN_YEAR/APR_CONVERSION_CONSTANT)/monthlyRepayment)));
- int monthsInTotal = (int)Math.ceil(-log(base , x));
- int years = monthsInTotal/MONTHS_IN_YEAR;
- int monthsLeftOver = monthsInTotal-(years*MONTHS_IN_YEAR);
- return years+(years == 1 ? " year ":" years ")+"and "+monthsLeftOver+(monthsLeftOver == 1 ? " month ":" months ");
- }
- private static double log(double base, double x)
- {
- return (double) (Math.log(x) / Math.log(base));
- }
- private static void goAgainPrompt()
- {
- if(input.hasNext(RUN_AGAIN_KEYWORD))
- {
- input.next();
- }
- else if(input.hasNext(QUIT_KEYWORD))
- {
- quit = true;
- input.next();
- }
- else
- {
- input.next();
- System.out.println(INVALID_GO_AGAIN_INPUT_PROMPT);
- goAgainPrompt();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement