Dispense_Class

    1| /*
    2| NTC Java Programming 152-116
    3| Instructor: John Heckendorf
    4| Student: Jan Young & Keith Gallistel
    5| Lab ID: & 46314
    6| Project:pepsi.java
    7| */
    8| 
    9| import javax.swing.*;
  10| import java.util.*;
  11| import java.awt.Color;
  12| import java.awt.event.*;
  13| 
  14| public class Dispense_Class extends JFrame implements ActionListener
  15| {
  16|     final static long serialVersionUID = 5;
  17| 
  18|     //variables
  19|     Control_Class oC;
  20| 
  21|     int iBinIndex = 0;
  22|     double dPrice = 0;
  23|     int iQty = 0;
  24| 
  25|     //Instantiated Class
  26|     public Dispense_Class(Control_Class oControl)
  27|     {
  28|         oC = oControl;
  29|     }
  30| 
  31|     //Method that will process GUI events
  32|     public void actionPerformed(ActionEvent e)
  33|     {
  34|         boolean bEnoughMoney = false;
  35|         boolean bEnoughInventory = false;
  36|         boolean bExactChange = false;
  37|         boolean bProductBinEmpty = false;
  38|         boolean bChangeBinNoOdd = false;
  39| 
  40|         for(int x = 0; x < oC.iBT; x++)
  41|         {
  42|             if(e.getSource() == oC.oBT[x])
  43|             {
  44|                 iBinIndex = x;
  45|             }
  46|         }
  47|         Get_Price_Qty(iBinIndex);
  48| 
  49|         bEnoughMoney = Enough_Money(oC.oMoney);
  50|         bEnoughInventory = Enough_Inventory();
  51|         bExactChange = Exact_Change(iBinIndex, oC.oMoney, oC.jrbnExactChange);
  52|         bProductBinEmpty = Bin_Empty(oC.jbtnProductBin);
  53|         bChangeBinNoOdd = Change_No_Odd(oC.jbtnChangeBin);
  54| 
  55|         if(bEnoughMoney && bEnoughInventory && bExactChange && bProductBinEmpty && bChangeBinNoOdd)
  56|         {
  57|             Decrease_Inventory(iBinIndex);
  58|             oC.jbtnProductBin.setText(oC.sBT[iBinIndex].toString());
  59| 
  60|             Calculate_Change(oC.jbtnChangeBin, oC.oMoney);
  61| 
  62|             oC.bExact = Update_Bank_Clear_Transaction(oC.oMoney);
  63| 
  64|             oC.jtxtMoneyDisplay.setText(null);
  65|         }
  66| 
  67| 
  68|     }
  69| 
  70|     //Accesses database to get the price and quantity of product
  71|     private void Get_Price_Qty(int iBI)
  72|     {
  73|         String sBI = "";
  74|         String sQ = "";
  75| 
  76|         sBI = Integer.toString(iBI);
  77|         sQ ="SELECT * FROM Products WHERE pm_bin_number = " + sBI + ";";
  78| 
  79|         DB_Interface oDBI = new DB_Interface();
  80|         oDBI.Query(sQ);
  81| 
  82|         dPrice = oDBI.dPrice;
  83|         iQty = oDBI.iOn_Hand;
  84|     }
  85| 
  86|     //Checks if there is enough money in current
  87|     private boolean Enough_Money(Money_Class oMoney)
  88|     {
  89|         boolean bEMoney = true;
  90| 
  91|         if(dPrice > oMoney.Get_Current())
  92|         {
  93|             bEMoney = false;
  94|         }
  95| 
  96|         return bEMoney;
  97|     }
  98| 
  99|     //Checks if quantity is greater than one
  100|     private boolean Enough_Inventory()
101|     {
102|         boolean bEInv = true;
103| 
104|         if(iQty < 1)
105|         {
106|             bEInv = false;
107|         }
108| 
109|         return bEInv;
110|     }
111| 
112|     //Checks if product bin is empty
113|     private boolean Bin_Empty (JButton jbtnProductBin)
114|     {
115|         boolean bBin = true;
116| 
117|         if(jbtnProductBin.getText() != "Empty")
118|         {
119|             jbtnProductBin.setBackground(Color.RED);
120|             bBin = false;
121|         }
122| 
123|         return bBin;
124|     }
125| 
126|     //Checks if change bin contains garbage
127|     private boolean Change_No_Odd(JButton jbtnChangeBin)
128|     {
129|         boolean bVM = false;
130|         double dData = 0;
131| 
132|         if(jbtnChangeBin.getText() != "Change")
133|         {
134|             try
135|             {
136|                 dData = Double.parseDouble(jbtnChangeBin.getText());
137|                 bVM = true;
138|             }
139|             catch(NumberFormatException e)
140|             {
141|                 bVM = false;
142|                 jbtnChangeBin.setBackground(Color.RED);
143|             }
144|         }
145|         else
146|         {
147|             bVM = true;
148|         }
149| 
150|         return bVM;
151|     }
152| 
153|     //Decreases the on-hand inventory in the database
154|     private void Decrease_Inventory(int iBI)
155|     {
156|         String sBI = "";
157|         String sQ = "";
158| 
159|         sBI = Integer.toString(iBI);
160|         sQ ="SELECT * FROM Products WHERE pm_bin_number = " + sBI + ";";
161| 
162|         DB_Interface oDBI = new DB_Interface();
163|         oDBI.Update_OnHand_Sold(sQ);
164| 
165|     }
166| 
167|     //Calculates the change
168|     private void Calculate_Change(JButton jbtnChangeBin, Money_Class oMoney)
169|     {
170|         String sCBin;
171|         double dCBin;
172|         double dCurrent;
173|         double dNewCurrent;
174|         String sNewCurrent;
175|         double dCalc;
176|         String sCalc;
177| 
178|         sCBin = jbtnChangeBin.getText();
179|         dCurrent = oMoney.Get_Current();
180| 
181|         try
182|         {
183|             Formatter oF = new Formatter();
184| 
185|             dCalc = dCurrent - dPrice;
186|             oF.format("%,.2f", dCalc);
187|             sCalc = oF.toString();
188| 
189|             if(dCalc >= 0)
190|             {
191|                 if(sCBin == "Change" && dCalc != 0.0)
192|                 {
193|                     jbtnChangeBin.setText(sCalc);
194|                 }
195| 
196|                 if(sCBin != "Change")
197|                 {
198|                     Formatter oH = new Formatter();
199|                     dCBin = Double.parseDouble(jbtnChangeBin.getText());
197|                     dNewCurrent = dCalc + dCBin;
198|                     oH.format("%,.2f", dNewCurrent);
199|                     sNewCurrent = oH.toString();
200|                     jbtnChangeBin.setText(sNewCurrent);
201|                 }
202|             }
203|         }
204|         catch(NumberFormatException e)
205|         {
206| 
207|         }
208| 
209|     }
210| 
211|     //Test for Exact Change
212|     private boolean Exact_Change(int iBI, Money_Class oMoney, JRadioButton JRB)
213|     {
214|         boolean bEC = false;
215|         double dData = 0;
216|         double dPrice = 0;
217| 
218|         String sBI = "";
219|         String sQ = "";
220| 
221|         sBI = Integer.toString(iBI);
222|         sQ ="SELECT * FROM Products WHERE pm_bin_number = " + sBI + ";";
223| 
224|         DB_Interface oDBI = new DB_Interface();
225|         oDBI.Query(sQ);
226| 
227|         dPrice = oDBI.dPrice;
228| 
229|         dData = oMoney.Get_Current();
230| 
231|         if(JRB.isSelected() == true)
232|         {
233|             if(dPrice == dData)
234|             {
235|                 bEC = true;
236|             }
237|         }
238|         else
239|         {
240|             bEC = true;
241|         }
242| 
243|         return bEC;
244|     }
245| 
246|     //Update the bank and clear current
247|     private boolean Update_Bank_Clear_Transaction(Money_Class oMoney)
248|     {
249|         boolean bExact = false;
250| 
251|         double dCurrent = 0;
252|         double dSale = 0;
253|         double dBank = 0;
254| 
255|         dCurrent = oMoney.Get_Current();
256| 
257|         dSale = dCurrent - dPrice;
258| 
259|         oMoney.Register_Sale(dSale);
260| 
261|         oMoney.Reset_Current();
262| 
263|         dBank = oMoney.Get_Bank();
264| 
265|         if(dBank < 5.00)
266|         {
267|             bExact = true;
268|         }
269|         return bExact;
270|     }
271| }