Write a GUI program to compute the amount of a certificate of deposit on maturity. The sample data follows:
Amount deposited: 80000.00
Years: 15
Interest rate: 7.75
Hint: To solve this problem, compute 80000.00 (1 + 7.75 / 100)15.
import javax.swing.*;
public class GUI extends JFrame {
public GUI() {
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
JLabel depositedAmountLabel = new JLabel("Amount deposited:");
JLabel yearsLabel = new JLabel("Years:");
JLabel interestRateLabel = new JLabel("Interest rate:");
JLabel resultLabel = new JLabel("Enter the values.");
JTextField depositedAmountField = new JTextField();
JTextField yearsField = new JTextField();
JTextField interestRateField = new JTextField();
JButton calculateButton = new JButton("Calculate");
calculateButton.addActionListener(actionEvent -> {
try {
double amount = Double.parseDouble(depositedAmountField.getText());
int years = Integer.parseInt(yearsField.getText());
double rate = Double.parseDouble(interestRateField.getText());
resultLabel.setText("The result: " + (amount * (1 + rate / 100) * years));
} catch (Exception e) {
}
});
panel.add(depositedAmountLabel);
panel.add(depositedAmountField);
panel.add(yearsLabel);
panel.add(yearsField);
panel.add(interestRateLabel);
panel.add(interestRateField);
panel.add(resultLabel);
panel.add(calculateButton);
add(panel);
setSize(250, 200);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new GUI();
}
}
Comments
Leave a comment