Sunday, January 24, 2016

Wage Calculator with Anonymous ActionListener


package wageCalculator;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import java.text.NumberFormat;



/**
 *
 * @author zbawk
 */
public class WageFrame extends javax.swing.JFrame {

    /**
     * Creates new form WageFrame
     */
    public WageFrame() {
        initComponents();
       
        //Z.B-1: Register button with anonymous actionlistener class
        jbtnCompute.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                //read data
               
                double hours = Double.parseDouble(jtxtHours.getText());
                double wage = Double.parseDouble(jtxtWage.getText());
                double salary = 0;
                //compute
                if(hours <= 40){
                    salary = hours * wage;
                }
                else
                {
                    salary = 40*wage + (hours-40)*wage*1.5;
                }

                //display in currency
//                //get currency formatter
                NumberFormat formatter = NumberFormat.getCurrencyInstance();
                jtxtSalary.setText(formatter.format(salary));
            }
        });
        //add action event to clear the button
        jbtnClear.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                jtxtHours.setText("");
                jtxtWage.setText("");
                jtxtSalary.setText("");
            }
        });
    }

No comments:

Post a Comment