Sunday, January 24, 2016

Compute Total Price


//Lab Assignment
//****************
//Provide gui to allow user to enter quantity and unitprice for 2 items
//your code is to compute the subtotals, tax, and total
//display all in currency format
//provide a second button to clear
//use second option in dealing with action event



package ComputeTotalPackage;

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

/**
 *
 * @author Zau
 */
public class ComputeTotal extends javax.swing.JFrame {

    /**
     * Creates new form ComputeTotal
     */
    public ComputeTotal() {
        initComponents();
       
        //method 1
        ComputeTotalListener computeListener = new ComputeTotalListener();
        jbtnComputeTotal.addActionListener(computeListener);
        //method 2 in single line
        jbtnClear.addActionListener(new ClearListener());
    }

    // Variables declaration - do not modify                    
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JButton jbtnClear;
    private javax.swing.JButton jbtnComputeTotal;
    private javax.swing.JTextField jtxtItem1;
    private javax.swing.JTextField jtxtItem2;
    private javax.swing.JTextField jtxtSubTotal;
    private javax.swing.JTextField jtxtTax;
    private javax.swing.JTextField jtxtTotal;
    private javax.swing.JTextField jtxtUnitPrice1;
    private javax.swing.JTextField jtxtUnitPrice2;
    // End of variables declaration                  

    //Step 1: Define an inner class to listen for click (action) event
    //!!!!! write code before end "}" curly bracket
   
    private class ComputeTotalListener implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent ae) {
       
        //Code here run when a button associated with this class is clicked
       
        double item1 = Double.parseDouble(jtxtItem1.getText());
        double item2 = Double.parseDouble(jtxtItem2.getText());
       
        double price1 = Double.parseDouble(jtxtUnitPrice1.getText());
        double price2 = Double.parseDouble(jtxtUnitPrice2.getText());
       
        double subTotal = (item1 * price1) + (item2 * price2);
        double totalTax = subTotal * 0.098;
        double total = (subTotal + totalTax );
       
       
        NumberFormat formatter = NumberFormat.getCurrencyInstance();
       
        jtxtSubTotal.setText(formatter.format(subTotal));
        jtxtTax.setText(formatter.format(totalTax));
        jtxtTotal.setText(formatter.format(total));
    }
   
}
//Another inner listener class for clear button
    private class ClearListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent ae) {
            //the clear button will be associated with this class
            jtxtItem1.setText("");
            jtxtItem2.setText("");
            jtxtUnitPrice1.setText("");
            jtxtUnitPrice2.setText("");
            jtxtSubTotal.setText("");
            jtxtTax.setText("");
            jtxtTotal.setText("");
        }
       
    }
}

No comments:

Post a Comment