Refund_Event_Class

    1| //NTC Java Programming 152-116
    2| //Instructor: John Heckendorf
    3| //Student: Jan Young & Keith Gallistel
    4| //Lab ID:
    5| 
    6| import javax.swing.*;
    7| import java.awt.Color;
    8| import java.awt.event.*;
    9| import java.util.*;
  10| 
  11| public class Refund_Event_Class extends JFrame implements ActionListener
  12| {
  13|     final static long serialVersionUID = 5;
  14| 
  15|     Control_Class oC;
  16| 
  17|     //Instantiated Class
  18|     public Refund_Event_Class(Control_Class oControl)
  19|     {
  20|         oC = oControl;
  21|     }
  22| 
  23|     //Method that will process GUI events
  24|     public void actionPerformed(ActionEvent e)
  25|     {
  26|     Refund_Event(oC.jbtnChangeBin, oC.oMoney);
  27|     }
  28| 
  29|     //method that calculates refund
  30|     public void Refund_Event(JButton jbtnChangeBin, Money_Class oMoney)
  31|     {
  32|         String sCBin;
  33|         double dCBin;
  34|         double dCurrent;
  35|         String sCurrent;
  36|         double dNewCurrent;
  37|         String sNewCurrent;
  38| 
  39|         boolean bChangeBinNoOdd = false;
  40| 
  41|         Formatter oF = new Formatter();
  42| 
  43|         sCBin = jbtnChangeBin.getText();
  44|         dCurrent = oMoney.Get_Current();
  45|         oF.format("%,.2f", dCurrent);
  46|         sCurrent = oF.toString();
  47| 
  48|         bChangeBinNoOdd = Change_No_Odd(jbtnChangeBin);
  49| 
  50|         if(bChangeBinNoOdd)
  51|         {
  52|             try
  53|             {
  54|                 if(dCurrent != 0)
  55|                 {
  56|                     if(sCBin == "Change")
  57|                     {
  58|                         oC.jbtnChangeBin.setText(sCurrent);
  59|                         oMoney.Reset_Current();
  60|                         oC.jtxtMoneyDisplay.setText(null);
  61|                         }
  62|                      if(sCBin != "Change")
  63|                     {
  64|                          Formatter oH = new Formatter();
  65|                         dCBin = Double.parseDouble(jbtnChangeBin.getText());
  66|                         dNewCurrent = dCurrent + dCBin;
  67|                          oH.format("%,.2f", dNewCurrent);
  68|                         sNewCurrent = oH.toString();
  69|                          oC.jbtnChangeBin.setText(sNewCurrent);
  70|                          oMoney.Reset_Current();
  71|                         oC.jtxtMoneyDisplay.setText(null);
  72|                     }
  73|                 }
  74|             }
  75|             catch(NumberFormatException e)
  76|             {
  78|                 jbtnChangeBin.setBackground(Color.RED);
  79|             }
  80|         }
  81| 
  82|     }
  83| 
  84|     //Checks if change bin contains garbage
  85|     private boolean Change_No_Odd(JButton jbtnChangeBin)
  86|     {
  87|         boolean bVM = false;
  88|         double dData = 0;
  89| 
  90|         if(jbtnChangeBin.getText() != "Change")
  91|         {
  92|             try
  93|             {
  94|                 dData = Double.parseDouble(jbtnChangeBin.getText());
  95|                 bVM = true;
  96|             }
  97|             catch(NumberFormatException e)
  98|             {
  99|                 bVM = false;
  100|                 jbtnChangeBin.setBackground(Color.RED);
  101|             }
  102|         }
  103|         else
  104|         {
  105|             bVM = true;
  106|         }
  107|         return bVM;
  108|     }
  109| }