import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;

public class FutureValueApp
{
    public static void main(String[] args)
    {
        JFrame frame = new FutureValueFrame();
        frame.setVisible(true);
    }
}
//---------------------------------------------------------------------

class FutureValueFrame extends JFrame
{
    public FutureValueFrame()
    {
        this.setTitle("Future Value Calculator");
        this.setSize(267, 200);						//width, height
        this.centerFrame();
        this.setResizable(false);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new FutureValuePanel();
        this.add(panel);
    }

    private void centerFrame()
    {
        Toolkit tk    = Toolkit.getDefaultToolkit();
        Dimension d   = tk.getScreenSize();
		int xPosition = (d.width  - this.getWidth())  / 2;
		int yPosition = (d.height - this.getHeight()) / 2;  
        this.setLocation(xPosition, yPosition);
    }
}
//---------------------------------------------------------------------

class FutureValuePanel extends JPanel implements ActionListener
{
    private JLabel      paymentLabel, rateLabel, yearsLabel, futureValueLabel;
    private JTextField  paymentField, rateField, yearsField, futureValueField;
    private JButton     calculateButton, exitButton;

    public FutureValuePanel()
    {
        this.setLayout(new BorderLayout());

        // create input panel ----------------------------------------
        JPanel inputPanel = new JPanel();
        inputPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));

        // payment info
        paymentLabel = new JLabel("Monthly Payment:");
        paymentField = new JTextField(10);
        inputPanel.add(paymentLabel);
        inputPanel.add(paymentField);

        // interest rate info
        rateLabel = new JLabel("Yearly Interest Rate:");
        rateField = new JTextField(10);
        inputPanel.add(rateLabel);
        inputPanel.add(rateField);

        // years info
        yearsLabel = new JLabel("Number of Years:");
        yearsField = new JTextField(10);
        inputPanel.add(yearsLabel);
        inputPanel.add(yearsField);

        // future value result into
        futureValueLabel = new JLabel("Future Value:");
        futureValueField = new JTextField(10);
        inputPanel.add(futureValueLabel);
        inputPanel.add(futureValueField);
        futureValueField.setEditable(false);
        futureValueField.setFocusable(false);

        // create button panel ----------------------------------------
        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER));

        // calculate button
        calculateButton = new JButton("Calculate");
        buttonPanel.add(calculateButton);
        calculateButton.addActionListener(this);

        // exit button
        exitButton = new JButton("Exit");
        buttonPanel.add(exitButton);
        exitButton.addActionListener(this);

        // add both panels to main panel --------------------------------
        this.add(inputPanel,  BorderLayout.CENTER);
        this.add(buttonPanel, BorderLayout.SOUTH);
    }

    public void actionPerformed(ActionEvent e)
    {
        Object source = e.getSource();

        if (source == exitButton)
            System.exit(0);         

        if (source == calculateButton)
        {
			if (paymentField.getText().equals("")) paymentField.setText("1000");
			if (rateField.getText().equals(""))    rateField.setText("5");
			if (yearsField.getText().equals(""))   yearsField.setText("10");

            double payment = Double.parseDouble(paymentField.getText());
            double rate    = Double.parseDouble(rateField.getText());
            double years   = Double.parseDouble(yearsField.getText());

            double futureValue = FutureValueCalc.calculate(payment, rate, years);

            NumberFormat currency = NumberFormat.getCurrencyInstance();
            futureValueField.setText(currency.format(futureValue));
        }
    }
}