Advertisement
Guest User

Vending machine

a guest
Dec 7th, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.57 KB | None | 0 0
  1. mport java.awt.Color;
  2. import javax.swing.JOptionPane;
  3. import java.text.*;
  4. import java.io.*;
  5. import java.awt.Image;
  6. import javax.swing.ImageIcon;
  7.  
  8.  
  9. /**
  10.  *
  11.  * @author student
  12.  */
  13. public class vendingMachine extends javax.swing.JFrame {
  14.    
  15.     /**
  16.      * Creates new form vendingMachine
  17.      */
  18.     public vendingMachine() {
  19.         initComponents();
  20.        
  21.     }
  22.     public static double totalMoneyEntered = 0; //Assigning a value of zero to the total money/balance global variable
  23.     //Public array with the stock set to 10 by default for each coin
  24.     public static int[] coinStock = {10, 10, 10, 10, 10}; //Two euro, One Euro, Fifty Cent, Twenty cent, Ten cent
  25.     public static double[] coinValue = {2, 1, 0.5, 0.2, 0.1}; //Two euro, One Euro, Fifty Cent, Twenty cent, Ten cent
  26.     public static double[] productPrice = {1.1, 1.2, 1.3, 1.4};
  27.     public static int[] productStock = {3, 5, 2, 5}; //colastock, diet cola, sprite stock, fanta stock
  28.     DecimalFormat df = new DecimalFormat("#.##"); //Two decimal places object
  29.    
  30.     /**
  31.      * This method is called from within the constructor to initialize the form.
  32.      * WARNING: Do NOT modify this code. The content of this method is always
  33.      * regenerated by the Form Editor.
  34.      */
  35.     @SuppressWarnings("unchecked")
  36.  
  37.     private void jButtonTwoEuroActionPerformed(java.awt.event.ActionEvent evt) {                                              
  38.         addCoin(0,0); //Add Two euro                    
  39.     }                                              
  40.  
  41.     private void jButtonSpriteActionPerformed(java.awt.event.ActionEvent evt) {                                              
  42.         if (totalMoneyEntered < productPrice[3]) { //Checking if the totalMoneyEntered variable has the a value below 1.3 and calling a method
  43.             insufficient();
  44.         }else{
  45.             if (productStock[2] < 1) { //Checking if a product is in stock
  46.             outOfStock("Sprite");
  47.             jButtonSprite.setEnabled(false);
  48.         } else {
  49.             buying(2,2);
  50.         }
  51.         }
  52.     }                                            
  53.  
  54.     private void jButtonOneEuroActionPerformed(java.awt.event.ActionEvent evt) {                                              
  55.         addCoin(1,1); //Add One euro
  56.     }                                              
  57.  
  58.     private void jButtonFiftyCentActionPerformed(java.awt.event.ActionEvent evt) {                                                
  59.         addCoin(2,2); //Add Fity cent
  60.     }                                                
  61.  
  62.     private void jButtonTwentyCentActionPerformed(java.awt.event.ActionEvent evt) {                                                  
  63.          addCoin(3,3); //Add Tenty cent
  64.     }                                                
  65.  
  66.     private void jButtonCheckStockActionPerformed(java.awt.event.ActionEvent evt) {                                                  
  67.         list1.clear(); //Checks the stock of products
  68.         list1.setForeground(Color.white);
  69.         list1.add("Cola in stock: " + productStock[0]);
  70.         list1.add("Diet Cola in stock: " + productStock[1]);
  71.         list1.add("Sprite in stock: " + productStock[2]);
  72.         list1.add("Fanta in stock: " + productStock[3]);
  73.     }                                                
  74.  
  75.     private void jButtonColaActionPerformed(java.awt.event.ActionEvent evt) {                                            
  76.        if (totalMoneyEntered < productPrice[0]) { //Checking if the totalMoneyEntered variable has the a value below 1.1 and calling a method
  77.             insufficient();
  78.         } else {
  79.             if (productStock[0] < 1) { //Checking if a product is in stock
  80.                 outOfStock("Cola");
  81.                 jButtonCola.setEnabled(false);          
  82.             } else {
  83.                 buying(0,0);
  84.             }
  85.         }
  86.     }                                          
  87.  
  88.     private void formWindowActivated(java.awt.event.WindowEvent evt) {                                    
  89.         this.setResizable(false); //Setting the resizing of the window to false at activation
  90.         stockEnable();
  91.     }                                    
  92.  
  93.     private void jButtonDietActionPerformed(java.awt.event.ActionEvent evt) {                                            
  94.         if (totalMoneyEntered < productPrice[1]) { //Checking if the totalMoneyEntered variable has the a value below 1.2 and calling a method
  95.             insufficient();
  96.         } else {
  97.             if (productStock[1] < 1) { //Checking if a product is in stock
  98.             outOfStock("Diet Cola");
  99.             jButtonDiet.setEnabled(false);
  100.             }else{
  101.             buying(1,1);
  102.         }
  103.       }
  104.     }                                          
  105.  
  106.     private void jButtonFantaActionPerformed(java.awt.event.ActionEvent evt) {                                            
  107.         if (totalMoneyEntered < productPrice[3]) { //Checking if the totalMoneyEntered variable has the a value below 1.4 and calling a method
  108.             insufficient();
  109.         }else{
  110.             if (productStock[3] < 1) { //Checking if a product is in stock
  111.             outOfStock("Fanta");
  112.             jButtonFanta.setEnabled(false);
  113.             } else {
  114.            buying(3,3);
  115.         }
  116.       }
  117.     }                                            
  118.  
  119.     private void jButtonRefilStockActionPerformed(java.awt.event.ActionEvent evt) {                                                  
  120.         stockFrame sf = new stockFrame(); //New frame object and setting it to visible button
  121.         sf.setVisible(true);
  122.     }                                                
  123.  
  124.     private void jButtonTenCentActionPerformed(java.awt.event.ActionEvent evt) {                                              
  125.         addCoin(4,4); //Add Ten cent
  126.     }                                              
  127.  
  128.     private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  129. //Change loop
  130.         int x = 0; //Variables to count the ammount of coins that have been removed from coin stock/An if statement has executed
  131.         int y = 0;
  132.         int z = 0;
  133.         int v = 0;
  134.         int t = 0;
  135.         list1.clear();
  136.         list1.setForeground(Color.green);
  137.         enable();
  138.         while (totalMoneyEntered>0.0){ //checking if totalMoneyEntered has a value above of zero          
  139.         if(totalMoneyEntered >= coinValue[0] && coinStock[0]>0){ //Checking if totalMoneyEntered is 2 or above and if a coin is still in stock
  140.             deduct(0,0);
  141.             x++; //counter                            
  142.         }  
  143.         else if(totalMoneyEntered >= coinValue[1] && coinStock[1]>0){ //Checking if totalMoneyEntered is 1 or above and if a coin is still in stock
  144.             deduct(1,1);
  145.             y++; //counter
  146.         }          
  147.         else if(totalMoneyEntered >= coinValue[2] && coinStock[2]>0){ //Checking if totalMoneyEntered is 0.5 or above and if a coin is still in stock
  148.             deduct(2,2);
  149.             z++; //counter
  150.         }
  151.         else if(totalMoneyEntered >= coinValue[3] && coinStock[3]>0){ //Checking if totalMoneyEntered is 0.2 or above and if a coin is still in stock
  152.             deduct(3,3);
  153.             v++; //counter
  154.         }
  155.         else if(totalMoneyEntered >= coinValue[4] && coinStock[4]>0){ //Checking if totalMoneyEntered is 0.1 or above and if a coin is still in stock
  156.             deduct(4,4);
  157.             t++; //counter
  158.         }    
  159.         }
  160.         //Displaying how many coins have been returned
  161.             list1.add(x + " Two Euro coins returned");
  162.             list1.add(y + " One Euro coins returned");
  163.             list1.add(z + " Fifty cent coins returned");
  164.             list1.add(v + " Twenty cent coins returned");
  165.             list1.add(t + " Ten cent coins returned");
  166.        
  167.     }                                        
  168.    
  169.    
  170.    
  171.     //methods
  172.  
  173.     public void insufficient() { //Method for the display of insufficient funds
  174.         list1.clear();
  175.         list1.setForeground(Color.red);
  176.         list1.add("Insufficient funds, your balance is: " + df.format(totalMoneyEntered));
  177.     }
  178.     public void boughtDisplay() { //Method for the display of buying of a product
  179.         list1.clear();
  180.         list1.setForeground(Color.green);
  181.         list1.add("Thanks, your balance is now: " + df.format(totalMoneyEntered));
  182.     }
  183.     public void fundsAddedDipsplay() { //Method for the display of funds added
  184.         list1.clear();
  185.         list1.setForeground(Color.white);
  186.         list1.add("Balance has increased to: " + df.format(totalMoneyEntered));
  187.     }
  188.     public void outOfStock(String product) { //Method for the display of an out of stock product
  189.         list1.clear();
  190.         list1.setForeground(Color.red);
  191.         list1.add("Sorry " + product + " is out of stock");
  192.     }
  193.     public void disabled(){ //Limit method, disabling of buttons
  194.  
  195.             jButtonTwoEuro.setEnabled(false);
  196.             jButtonOneEuro.setEnabled(false);
  197.             jButtonFiftyCent.setEnabled(false);
  198.             jButtonTwentyCent.setEnabled(false);
  199.             jButtonTenCent.setEnabled(false);
  200.             list1.add("Please choose a product you wish to purchase");
  201.     }
  202.     public void enable(){ //Enabling of buttons method
  203.             jButtonTwoEuro.setEnabled(true);
  204.             jButtonOneEuro.setEnabled(true);
  205.             jButtonFiftyCent.setEnabled(true);
  206.             jButtonTwentyCent.setEnabled(true);
  207.             jButtonTenCent.setEnabled(true);
  208.        
  209.     }
  210.     public void addCoin(int x, int y){
  211.          if(totalMoneyEntered >= 10.0){ //Checking if totalMoneyEntered variable is above or equal to 10 and calling a disabled method
  212.             disabled();
  213.         }else{
  214.         totalMoneyEntered = totalMoneyEntered + coinValue[x]; //Addition of coin
  215.         fundsAddedDipsplay();
  216.         coinStock[y]++;
  217.         }
  218.     }
  219.     public void deduct(int x, int y){
  220.         totalMoneyEntered = totalMoneyEntered - coinValue[x]; //Deducton of variables
  221.             totalMoneyEntered = Math.round(totalMoneyEntered * 100) / 100.0;
  222.             coinStock[y]--;      
  223.     }
  224.     public void buying(int x, int y){
  225.                 totalMoneyEntered = totalMoneyEntered - productPrice[x]; //Buying and the deduction of totalMoneyEntered and stock
  226.                 totalMoneyEntered = Math.round(totalMoneyEntered * 100) / 100.0;
  227.                 productStock[y]--;
  228.                 boughtDisplay();
  229.                 enable();
  230.     }
  231.     public void stockEnable(){
  232.         if(productStock[0]>0){
  233.             jButtonCola.setEnabled(true);
  234.         }
  235.         if(productStock[1]>0){
  236.             jButtonDiet.setEnabled(true);
  237.         }
  238.         if(productStock[2]>0){
  239.             jButtonSprite.setEnabled(true);
  240.         }
  241.         if(productStock[3]>0){
  242.             jButtonFanta.setEnabled(true);
  243.         }}
  244.     /**
  245.      * @param args the command line arguments
  246.      */
  247.     public static void main(String args[]) {
  248.         /* Set the Nimbus look and feel */
  249.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  250.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  251.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  252.          */
  253.         try {
  254.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  255.                 if ("Nimbus".equals(info.getName())) {
  256.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  257.                     break;
  258.                 }
  259.             }
  260.         } catch (ClassNotFoundException ex) {
  261.             java.util.logging.Logger.getLogger(vendingMachine.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  262.         } catch (InstantiationException ex) {
  263.             java.util.logging.Logger.getLogger(vendingMachine.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  264.         } catch (IllegalAccessException ex) {
  265.             java.util.logging.Logger.getLogger(vendingMachine.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  266.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  267.             java.util.logging.Logger.getLogger(vendingMachine.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  268.         }
  269.         //</editor-fold>
  270.  
  271.         /* Create and display the form */
  272.         java.awt.EventQueue.invokeLater(new Runnable() {
  273.             public void run() {
  274.                 new vendingMachine().setVisible(true);
  275.             }
  276.         });
  277.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement