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);   ...

Java Program to calculate the Strike Rate of a Cricket Batsman

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

WP similar or related posts widget without using any plugin

Hi guys, Recently I was working on some WP website and my client told me that he required a widget for displaying related/similar posts on the single post page. But as his website was already using many plugins, even for some pretty small tasks like this one, I decided not to use another WP plugin (plugins are not good for your WP websites, we will discuss about that on some other post.) I am not explaining the code as it is pretty simple if you are familiar with WP classes. But please let me know if you have any questions related to the PHP code posted below in the comments section or even much better, on Gist. You can add the following code directly in your child theme's functions.php file or you can create a separate file and include this at the bottom of functions.php file. <?php class similar_posts_widget extends WP_Widget {     function __construct()     {         parent::__construct('similar_posts_...