Вы находитесь на странице: 1из 3

/** * */ package Program_3; import java.util.*; import java.text.

DecimalFormat; /** * * @author James Crosby (jxc116230) * Creation Date : 10/01/13 * Last Modified Date : 10/03/13 */ public class PROBLEM_3 { /** * @param args */ public static void main(String[] args) { //Variable Declaration double monthlyPayment = 0; double monthlyInterestrate = 0; double monthlyInterest = 0; //double totalInterest = 0; Not used, carry ove r from Problem 2 double loanAmount = 0; double loanPeriod = 0; double annualInterestrate = 0; double totalPayment = 0; double realIR; int counter; //LOOP Control Variable double principal = 0; double balance = 0; Scanner keyboard = new Scanner(System.in); // User Intro System.out.println("Hello user, this program wil l ask you for some information "); System.out.println("to calculate information abo ut your home mortgage."); //Asks for user input, checks for negative value s while (loanAmount == 0) { System.out.println("Please enter the loa n amount : "); loanAmount = keyboard.nextDouble(); if (loanAmount < 0) { System.out.println("This is an i nvalid entry. Please enter a positive value : "); loanAmount = keyboard.nextDouble (); }

} while (loanPeriod == 0) { System.out.println("Please enter the loa n period : "); loanPeriod = keyboard.nextDouble(); if (loanPeriod < 0) { System.out.println("This is an i nvalid entry. Please enter a positive value : "); loanPeriod = keyboard.nextDouble (); } } while (annualInterestrate == 0) { System.out.println("Please enter the ann ual interest rate : "); annualInterestrate = keyboard.nextDouble (); if (annualInterestrate < 0) { System.out.println("This is an i nvalid entry. Please enter a positive value : "); annualInterestrate = keyboard.ne xtDouble(); } } //This section does calculations for the output to console realIR = annualInterestrate/100; monthlyInterestrate = realIR/12; //OMG Parenthesis monthlyPayment = ((loanAmount * monthlyInterestr ate)/(1-(1/(Math.pow(1+monthlyInterestrate, loanPeriod))))); totalPayment = monthlyPayment*loanPeriod; //totalInterest = totalPayment - loanAmount; Not used, carry over from Problem 2 //This section outputs the results to the consol e DecimalFormat formatter = new DecimalFormat ("#0 .00"); DecimalFormat nodecimal = new DecimalFormat ("#0 "); System.out.println("Loan Amount : " + formatter. format(loanAmount)); System.out.println("Number of months : " + nodec imal.format(loanPeriod));

System.out.println("Annual Interest Rate : " + f ormatter.format(annualInterestrate)); System.out.println(); System.out.println("Payment #:\t\t Monthly Payme nt\t Principal\t Interest\t Balance"); System.out.println(); for (counter = 1; counter <= loanPeriod; counter ++) { monthlyInterest = loanAmount*monthlyInte restrate; principal = monthlyPayment - monthlyInte rest; balance = loanAmount - principal; System.out.println("\t" + counter + "\t\ t " + formatter.format(monthlyPayment) + "\t\t " + formatter.format(principal ) + "\t " + formatter.format(monthlyInterest) + "\t\t " + formatter.format(bala nce)); loanAmount = balance; } System.exit(0); } }

Вам также может понравиться