// Following classes are imported to support gui development
//LoanCalculatorFrame contains presentation rules
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;
//gui Class extends JFrame, in this case LoanCalculatorFrame is gui class
public class LoanCalculatorFrame extends JFrame{
//The constructor for the Frame of the gui
public LoanCalculatorFrame(){
setTitle("Loan Calculator");
//Java toolkit will get screen size
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension d = tk.getScreenSize();
int height = 400;
int width = 300;
setBounds((d.width-width)/2, (d.height-height)/2, width, height);
setResizable(true);
//Window listener
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
Container contentPane = getContentPane();
//Build panel(s) add add to frame, part of constructor
JPanel panel = new LoanCalculatorPanel();
contentPane.add(panel);
}
//The main method, creates the frame, then uses it show method (shows it)
//Program execution starts here -main
public static void main(String[] args){
JFrame frame = new LoanCalculatorFrame();
frame.show();
}
}
//Constructor to build panel(s) in frame
class LoanCalculatorPanel extends JPanel implements ActionListener{
// Define fields 1) Add futureValueTextField here
private JTextField amountTextField, rateTextField, yearsTextField,
paymentTextField, futureValueTextField;
// Define label for fields 2) Add futureValueLabel here
private JLabel amountLabel, rateLabel, yearsLabel, paymentLabel,
futureValueLabel;
// Define buttons
private JButton calculateButton, exitButton;
//Method for LoanCalcualtor panel (what the user sees)
public LoanCalculatorPanel(){
JPanel displayPanel = new JPanel();
displayPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
//Assign Labels
amountLabel = new JLabel("Loan Amount:");
rateLabel = new JLabel("Yearly Interest Rate:");
yearsLabel = new JLabel("Number of Years:");
paymentLabel = new JLabel("Monthly Payment:");
//3) **Assign value a label for future value
futureValueLabel = new JLabel("Future Value:");
//Assign Text fields and size that go with labels
amountTextField = new JTextField(10);
rateTextField = new JTextField(10);
yearsTextField = new JTextField(10);
paymentTextField = new JTextField(10);
paymentTextField.setEditable(false);
//4) **Assign a text field for future value
futureValueTextField = new JTextField(10);
futureValueTextField.setEditable(false);
//Add fields to the display panel
displayPanel.add(amountLabel);
displayPanel.add(amountTextField);
displayPanel.add(rateLabel);
displayPanel.add(rateTextField);
displayPanel.add(yearsLabel);
displayPanel.add(yearsTextField);
displayPanel.add(paymentLabel);
displayPanel.add(paymentTextField);
//5) ** Add a display for future value
displayPanel.add(futureValueLabel);
displayPanel.add(futureValueTextField);
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
calculateButton = new JButton("Calculate");
exitButton = new JButton("Exit");
buttonPanel.add(calculateButton);
buttonPanel.add(exitButton);
calculateButton.addActionListener(this);
exitButton.addActionListener(this);
setLayout(new BorderLayout());
add(displayPanel, BorderLayout.CENTER);
add(buttonPanel, BorderLayout.SOUTH);
}
//Method for input action, gets the input and does the calcs
public void actionPerformed(ActionEvent e){
//Get input source and Check for errors (e.)
Object source = e.getSource();
try{
if (source == exitButton)
System.exit(0);
else if (source == calculateButton){
double amount = Double.parseDouble(amountTextField.getText());
double rate = Double.parseDouble(rateTextField.getText());
int years = Integer.parseInt(yearsTextField.getText());
double monthlyInterest = rate/12/100;
int months = years * 12;
//Use FinancialCalculations methods
double payment = FinancialCalculations.calculateMonthlyPayment(
amount, months, monthlyInterest);
NumberFormat currency = NumberFormat.getCurrencyInstance();
paymentTextField.setText(currency.format(payment));
//6) **Add statements here to get future value convert to currency
//**and display it.
double futureValue =
FinancialCalculations.calculateFutureValue
(payment, months, monthlyInterest);
futureValueTextField.setText(currency.format(futureValue));
//**
}
}
catch (NumberFormatException nfe){
JOptionPane.showMessageDialog(this, "Invalid data entered.\n"
+ "Please check all numbers and try again.");
}
}
}