Skip to main content

Simple Interest Calculation Java Applet


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

Popular posts from this blog

Java Program to calculate the Run Rate per over in a cricket match

import java.io.*; import java.util.*; public class RunRate{     Scanner scan=new Scanner(System.in);     int runs, balls;     float runRate;     public void input(){         try{             System.out.println("Enter Runs Scored: ");             runs=scan.nextInt();             System.out.println("Enter Balls Delivered: ");             balls=scan.nextInt();         }         catch(NumberFormatException e){             System.out.println("Error Code: "+e);             System.exit(0);   ...

Vanilla Javascript each()

JQuery's each() is very useful when iterating through elements. But you don't want to use JQuery in your project you can simply add the following javascript code which works somewhat similar to the JQuery's each function. Here the fnc parameter is the function string which is converted to a valid function call replacing all the $(this) with this /**  * This function binds a particular function to every element with the specified selector. It is somewhat same as JQuery's each() with less functionality  * @param {String|DOMElement} selector  * @param {Function} fnc  */ function each(selector, fnc) {     var elem;     if (typeof selector === "string") {         elem = $_(selector);     } else {         elem = selector;     }     fnc = (fnc.toString().replace("$(this)", "elem") + "();").replace("function () {", "").replac...

Java Program to display Welcome Message

import java.io.*;// I/O package imported. public class Welcome{        //class name is "Welcome"     public Welcome(){      //constructor declaired to print the message.         System.out.println("Welcome to Java Programming Language!");/* System.out.println is used for output. Welcome Message is written within " ".*/     }//display() closes here.     public static void main(String[] args){        //main() is declaired to declair an object in it.         Welcome obj=new Welcome();  //Object "Obj" is bean created.     }//main() closes. }//class "Welcome" ends here. Above program displays the message which is written by you in " ".  In programs "/*" and "*/" are use for multiple line comment(s) and "//" is use for single line comment. Code line "Welc...