import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public final class InterestCalcApplet extends Applet implements ActionListener {
TextField principalField, rateField, numYearsField;
double simpleInterest = 0, amount = 0, principal = 0, numYears = 0, rate = 0;
Button calcButton = new Button("Calculate");
boolean error, error2, error3, error4;
int er;
File file;
byte b[];
String cmd = "";
public InterestCalcApplet() {
super();
add("South", calcButton);
}
@Override
public void init() {
try {
principalField = new TextField(9);
rateField = new TextField(1);
numYearsField = new TextField(1);
Label principalLabel = new Label("Principal: ");
Label rateLabel = new Label("Rate (%): ");
Label numYearsLabel = new Label("Time (in years): ");
addStyle();
add(principalLabel);
add(principalField);
add(rateLabel);
add(rateField);
add(numYearsLabel);
add(numYearsField);
add("South", calcButton);
calcButton.addActionListener(this);
} catch (RuntimeException err) {
System.exit(0);
}
}
@Override
public void actionPerformed(ActionEvent e) {
try {
error = error2 = error3 = error4 = false;
setValue();
simpleInterest = (principal * rate * numYears) / 100;
amount = principal + simpleInterest;
repaint();
} catch (NullPointerException | NumberFormatException err) {
error = true;
} finally {
if (error == true) {
calcButton.setBackground(Color.red);
} else {
calcButton.setBackground(Color.decode("#294a8f"));
}
}
}
@Override
public void paint(Graphics g) {
if (error == false && error2 == false && error3 == false && error4 == false && er != 0) {
g.drawString("The Simple Interest & Amount after " + numYears + " years", 60, 100);
g.drawString("Simple Interest: " + simpleInterest, 60, 120);
g.drawString("Total Amount: " + amount, 60, 140);
g.setColor(Color.black);
}
if (error2 == true) {
g.setColor(Color.red);
g.drawString("Invalid Principal", 60, 100);
}
if (error3 == true) {
g.setColor(Color.red);
g.drawString("Invalid Time", 60, 120);
}
if (error4 == true) {
g.setColor(Color.red);
g.drawString("Invalid Rate", 60, 140);
}
g.drawString(cmd, 60, 160);
er++;
}
public void setValue() {
try {
principal = new Double(principalField.getText().trim()).doubleValue();
} catch (NumberFormatException | NullPointerException err) {
error2 = true;
} finally {
if (error2 == true) {
principalField.setBackground(Color.red);
} else {
principalField.setBackground(Color.decode("#294a8f"));
}
}
try {
numYears = new Double(numYearsField.getText().trim()).doubleValue();
} catch (NumberFormatException | NullPointerException err) {
error3 = true;
} finally {
if (error3 == true) {
numYearsField.setBackground(Color.red);
} else {
numYearsField.setBackground(Color.decode("#294a8f"));
}
}
try {
rate = new Double(rateField.getText().trim()).doubleValue();
} catch (NumberFormatException | NullPointerException err) {
error4 = true;
} finally {
if (error4 == true) {
rateField.setBackground(Color.red);
} else {
rateField.setBackground(Color.decode("#294a8f"));
}
}
}
public void addStyle() {
calcButton.setBackground(Color.decode("#294a8f"));
calcButton.setForeground(Color.white);
numYearsField.setBackground(Color.decode("#3B5998"));
principalField.setBackground(Color.decode("#3B5998"));
rateField.setBackground(Color.decode("#3B5998"));
numYearsField.setForeground(Color.white);
principalField.setForeground(Color.white);
rateField.setForeground(Color.white);
calcButton.setFocusable(false);
}
}
Comments