import java.io.*;
/*
* Created on 2004¦~1¤ë30¤é
*
* This program prompts user for the three parameters loan amount,
* annual interest rate, and monthly payment; and prints a loan table.
*
*/
import java.io.*;
import java.text.*;
/**
* Class LoanTable prompts user for the three parameters loan amount,
* annual interest rate, and monthly payment; and prints a loan table.
*
* @author dcywchan
* @version 1.0
*/
public class LoanTable {
private static BufferedReader stdIn =
new BufferedReader(new InputStreamReader(System.in));
private static PrintWriter stdOut =
new PrintWriter(System.out, true);
private static PrintWriter stdErr =
new PrintWriter(System.err, true);
/**
* Prompts for the three parameters and conveys this information in a nice table.
*
* @param args not used
* @throws IOException if error occurs when reading from the keyboard.
*/
public static void main(String[] args) throws IOException {
DecimalFormat decimalformat = new DecimalFormat("0.00");
String template = " ";
int month;
double startingBalance, payment, middleBalance, interest, endingBalance;
double loanAmount, annualInterestRate, monthlyPayment;
stdErr.println("The loan amount is:");
loanAmount = Double.parseDouble(stdIn.readLine());
stdErr.println("The annual interest rate is:");
annualInterestRate = Double.parseDouble(stdIn.readLine());
stdErr.println("The monthly payment is:");
monthlyPayment = Double.parseDouble(stdIn.readLine());
stdOut.println("\t\tStarting\t\t\t\tMiddle\t\t\t\t\tEnding");
stdOut.println("Month\tBalance\t\tPayment\t\tBalance\t\tInterest\tBalance");
stdOut.println("---------------------------------------------------------------");
month = 1;
startingBalance = loanAmount;
payment = monthlyPayment;
middleBalance = loanAmount - payment;
interest = middleBalance * annualInterestRate/12/100;
endingBalance = middleBalance + interest;
while ( endingBalance > 0){
stdOut.println(month+" .\t"+
decimalformat.format( startingBalance,
new StringBuffer(template.substring(0,template.length()-decimalformat.format(startingBalance).length())),
new FieldPosition(NumberFormat.INTEGER_FIELD)) + "\t" +
decimalformat.format( payment,
new StringBuffer(template.substring(0,template.length()-decimalformat.format(payment).length())),
new FieldPosition(NumberFormat.INTEGER_FIELD)) + "\t" +
decimalformat.format( middleBalance,
new StringBuffer(template.substring(0,template.length()-decimalformat.format(middleBalance).length())),
new FieldPosition(NumberFormat.INTEGER_FIELD)) + "\t" +
decimalformat.format( interest,
new StringBuffer(template.substring(0,template.length()-decimalformat.format(interest).length())),
new FieldPosition(NumberFormat.INTEGER_FIELD)) + "\t" +
decimalformat.format( endingBalance,
new StringBuffer(template.substring(0,template.length()-decimalformat.format(endingBalance).length())),
new FieldPosition(NumberFormat.INTEGER_FIELD))
);
month++;
startingBalance = endingBalance;
payment = (endingBalance > monthlyPayment?monthlyPayment:endingBalance);
middleBalance = startingBalance - payment;
interest = middleBalance * annualInterestRate/12/100;
endingBalance = middleBalance + interest;
}
}
}
Previou page | Next page |