I need to display the loan table with the annual interest rate, monthly payment, and total payment for the number of years entered. When I run my program, it only displays the info for 8%, when I need to display all interest rates from 5% to 8% in increments of 1/8%.
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Ch1613 extends JFrame implements ActionListener
{
private JLabel jlbAmount = new JLabel("Loan Amount");
private JTextField jtfAmount = new JTextField(8);
private JLabel jlbYears = new JLabel("Number of Years");
private JTextField jtfYears = new JTextField(2);
private JTextArea jtaTable = new JTextArea("Interest Rate" + "\t" + "Monthly Payment" + "\t\t" + "Total Payment");
private JButton showTable = new JButton("Show Table");
private JScrollPane scrollPane = new JScrollPane();
public Ch1613()
{
JPanel p = new JPanel();
p.add(jlbAmount);
p.add(jtfAmount);
p.add(jlbYears);
p.add(jtfYears);
p.add(showTable);
add(p, BorderLayout.NORTH);
add(scrollPane, BorderLayout.CENTER);
scrollPane.getViewport().add(jtaTable, null);
showTable.addActionListener(this);
}
// Main method
public static void main(String[] args)
{
JFrame frame = new Ch1613();
frame.pack();
frame.setTitle("Exercise16_13");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
double amount = Double.valueOf(jtfAmount.getText().trim()).doubleValue();
int years = Integer.valueOf(jtfYears.getText().trim()).intValue();
if (e.getSource() == showTable)
{
double monthlyInterestRate;
double monthlyPayment;
double totalPayment;
for (double annualInterestRate = 5.0; annualInterestRate <= 8.0;
annualInterestRate += 1.0 / 8)
{
// Obtain monthly interest rate
monthlyInterestRate = annualInterestRate / 1200;
// Compute mortgage
monthlyPayment = amount * monthlyInterestRate /
(1 - (Math.pow(1 / (1 + monthlyInterestRate), years * 12)));
totalPayment = monthlyPayment * years * 12;
// Display results
// Display the header
jtaTable.setText((int)annualInterestRate + "%" + ("\t\t" + (int)(monthlyPayment * 100) / 100.0) + "\t\t\t" + (int)(totalPayment * 100) / 100.0);
}
}
}
}