

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;


/**
 * Calculate Compound Interests.
 */
//@width 500
//@height 500
public class Interest extends JApplet implements ActionListener {          /* extends WindowController */
    // Fields to hold principal, interest rate and years in investment
    private DoubleTextField principalField;
    private IntTextField lowIntRateField;
    private IntTextField highIntRateField;
    private IntTextField yearsField;

    // Button to trigger calculation of value of investment
    private JButton calculateButton;

    // Area to output value of investment
    private JTextArea amountArea;

    // Set up all GUI items on screen
    public void init() {
        Container contentPane = getContentPane();

        JPanel topPanel = new JPanel(new GridLayout(3, 1));
        JPanel princiPanel = new JPanel();
        principalField = new DoubleTextField(1000, 6);

        JLabel principalLabel = new JLabel("Principal:");
        princiPanel.add(principalLabel);
        princiPanel.add(principalField);
        yearsField = new IntTextField(10, 5);

        JLabel yearsLabel = new JLabel("Years:");
        princiPanel.add(yearsLabel);
        princiPanel.add(yearsField);
        topPanel.add(princiPanel);

        JPanel intPanel = new JPanel();
        lowIntRateField = new IntTextField(1, 8);
        highIntRateField = new IntTextField(10, 8);

        JLabel interestLabel = new JLabel("Range of (int) interest rates:");
        intPanel.add(interestLabel);
        intPanel.add(lowIntRateField);
        intPanel.add(highIntRateField);
        topPanel.add(intPanel);

        JPanel calcPanel = new JPanel();
        calculateButton = new JButton("Calculate amount");
        calculateButton.addActionListener(this);
        calcPanel.add(calculateButton);
        topPanel.add(calcPanel);
        contentPane.add(topPanel, BorderLayout.NORTH);

        amountArea = new JTextArea(20,40);
        contentPane.add(amountArea, BorderLayout.CENTER);

        contentPane.validate();
    }

    // Triggered by clicking button, calculate value of investment of principal
    // at given interest rate for given number of years.
    public void actionPerformed(ActionEvent evt) {

        // interest rate ranges, and initial amount
        int startRate = lowIntRateField.getIntValue();
        int endRate = highIntRateField.getIntValue();
        double startAmount = principalField.getDoubleValue();

        // number of years for investment
        int years = yearsField.getIntValue();
        
        amountArea.setText("");  // a lage text area component

        for (int rate = startRate; rate <= endRate; rate++) {
            
            // set to initial value of investment
            double amount = startAmount;

            for (int year = 0; year < years; year++) {
                amount = amount + ((amount * rate) / 100.0);
            }

            amountArea.append("At " + rate + "%, the amount is: $" + amount + "\n");
        }
    }
}
