Write a java program to calculate Total amount and Interest amount Using textbox,label and 3 button for Calculate Button ,Clear Button ,Close Button.
import java.awt.*;
import java.awt.event.*;
public class
InterestCalculatorAWT extends Frame {
private TextField p, r, time1, total,
interestAmount;
private Button calculate, clear, close;
public InterestCalculatorAWT() {
setTitle("Interest
Calculator");
setSize(400, 200);
setLayout(null);
Label l1 = new Label("Principal
Amount:");
l1.setBounds(20, 30, 120, 20);
add(l1);
p = new TextField();
p.setBounds(160, 30, 200, 20);
add(p);
Label r1 = new Label("Rate of
Interest (%):");
r1.setBounds(20, 60, 120, 20);
add(r1);
r = new TextField();
r.setBounds(160, 60, 200, 20);
add(r);
Label l3 = new Label("Time (years):");
l3.setBounds(20, 90, 120, 20);
add(l3);
time1 = new TextField();
time1.setBounds(160, 90, 200, 20);
add(time1);
calculate = new
Button("Calculate");
calculate.setBounds(20, 120, 100, 30);
add(calculate);
clear = new Button("Clear");
clear.setBounds(140, 120, 100, 30);
add(clear);
close = new Button("Close");
close.setBounds(260, 120, 100, 30);
add(close);
total = new TextField();
total.setBounds(20, 160, 340, 20);
total.setEditable(false);
add(total);
interestAmount = new TextField();
interestAmount.setBounds(20, 190, 340,
20);
interestAmount.setEditable(false);
add(interestAmount);
calculate.addActionListener(new
ActionListener() {
public void
actionPerformed(ActionEvent e) {
calculate();
}
});
clear.addActionListener(new ActionListener()
{
public void
actionPerformed(ActionEvent e) {
clear();
}
});
close.addActionListener(new
ActionListener() {
public void
actionPerformed(ActionEvent e) {
System.exit(0);
}
});
addWindowListener(new WindowAdapter() {
public void
windowClosing(WindowEvent we) {
dispose();
}
});
setVisible(true);
}
private void calculate() {
try {
double principal =
Double.parseDouble(p.getText());
double rate =
Double.parseDouble(r.getText());
double time =
Double.parseDouble(time1.getText());
double interest = (principal * rate
* time) / 100;
double totalAmount = principal +
interest;
total.setText("Total Amount:
" + totalAmount);
interestAmount.setText("Interest Amount: " + interest);
} catch (NumberFormatException e) {
// Handle invalid input
total.setText("Invalid
input");
interestAmount.setText("Invalid input");
}
}
private void clear() {
p.setText("");
r.setText("");
time1.setText("");
total.setText("");
interestAmount.setText("");
}
public static void main(String[] args) {
new InterestCalculatorAWT();
}
}