Advertisement
arkshay_

Java vending machine

Apr 9th, 2020
805
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.36 KB | None | 0 0
  1. import java.awt.BorderLayout;
  2. import java.awt.EventQueue;
  3.  
  4. import javax.swing.JFrame;
  5. import javax.swing.JPanel;
  6. import javax.swing.border.EmptyBorder;
  7. import javax.swing.JLabel;
  8. import java.awt.Font;
  9. import java.awt.Color;
  10. import javax.swing.JRadioButton;
  11. import javax.swing.JTextField;
  12. import javax.swing.JCheckBox;
  13. import java.awt.SystemColor;
  14. import javax.swing.JButton;
  15. import javax.swing.UIManager;
  16. import java.awt.event.ActionListener;
  17. import java.awt.event.ActionEvent;
  18. import javax.swing.JList;
  19. import javax.swing.JComboBox;
  20.  
  21. public class drinkselect extends JFrame {
  22.  
  23.     private JPanel contentPane;
  24.     private JTextField totalPrice;
  25.     private JTextField drinkPrice;
  26.     private JTextField balanceField;
  27.     private JTextField inputField;
  28.     protected Object btn50cent;
  29.     private int Pepsi;
  30.     private int Sprite;
  31.     private JLabel thanksLabel;
  32.  
  33.     /**
  34.      * Launch the application.
  35.      */
  36.     public static void main(String[] args) {
  37.         EventQueue.invokeLater(new Runnable() {
  38.             public void run() {
  39.                 try {
  40.                     drinkselect frame = new drinkselect();
  41.                     frame.setVisible(true);
  42.                 } catch (Exception e) {
  43.                     e.printStackTrace();
  44.                 }
  45.             }
  46.         });
  47.     }
  48.  
  49.    
  50.    
  51.    
  52.     public float totalCoin = 0;
  53.    
  54.     /**
  55.      * Create the frame.
  56.      */
  57.     public drinkselect() {
  58.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  59.         setBounds(100, 100, 702, 534);
  60.         contentPane = new JPanel();
  61.         contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  62.         setContentPane(contentPane);
  63.         contentPane.setLayout(null);
  64.        
  65.         JLabel lblVendingMachine = new JLabel("Vending Machine");
  66.         lblVendingMachine.setFont(new Font("Stencil Std", Font.BOLD | Font.ITALIC, 27));
  67.         lblVendingMachine.setBounds(211, 10, 292, 36);
  68.         contentPane.add(lblVendingMachine);
  69.        
  70.         JLabel lblNewLabel = new JLabel("Drinks :");
  71.         lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 12));
  72.         lblNewLabel.setBounds(78, 59, 57, 13);
  73.         contentPane.add(lblNewLabel);
  74.        
  75.         JPanel drinkPanel = new JPanel();
  76.         drinkPanel.setBackground(Color.LIGHT_GRAY);
  77.         drinkPanel.setForeground(Color.BLACK);
  78.         drinkPanel.setBounds(78, 82, 516, 177);
  79.         contentPane.add(drinkPanel);
  80.         drinkPanel.setLayout(null);
  81.        
  82.         JLabel lblCokePrice = new JLabel("RM 2.50");
  83.         lblCokePrice.setFont(new Font("Tahoma", Font.BOLD, 12));
  84.         lblCokePrice.setBounds(134, 26, 64, 17);
  85.         drinkPanel.add(lblCokePrice);
  86.        
  87.         JLabel lblPepsiPrice = new JLabel("RM 2.50");
  88.         lblPepsiPrice.setFont(new Font("Tahoma", Font.BOLD, 12));
  89.         lblPepsiPrice.setBounds(134, 82, 62, 13);
  90.         drinkPanel.add(lblPepsiPrice);
  91.        
  92.         JLabel lblSpritePrice = new JLabel("RM 2.50");
  93.         lblSpritePrice.setFont(new Font("Tahoma", Font.BOLD, 12));
  94.         lblSpritePrice.setBounds(134, 143, 64, 13);
  95.         drinkPanel.add(lblSpritePrice);
  96.        
  97.         JLabel lbl100PlusPrice = new JLabel("RM 2.20");
  98.         lbl100PlusPrice.setFont(new Font("Tahoma", Font.BOLD, 12));
  99.         lbl100PlusPrice.setBounds(320, 28, 62, 13);
  100.         drinkPanel.add(lbl100PlusPrice);
  101.        
  102.         JLabel lblWaterPrice = new JLabel("RM 2.00");
  103.         lblWaterPrice.setFont(new Font("Tahoma", Font.BOLD, 12));
  104.         lblWaterPrice.setBounds(320, 82, 62, 13);
  105.         drinkPanel.add(lblWaterPrice);
  106.        
  107.         JLabel lblKickapooPrice = new JLabel("RM 2.40");
  108.         lblKickapooPrice.setFont(new Font("Tahoma", Font.BOLD, 12));
  109.         lblKickapooPrice.setBounds(320, 143, 62, 13);
  110.         drinkPanel.add(lblKickapooPrice);
  111.        
  112.         JPanel cokePanel = new JPanel();
  113.         cokePanel.setBounds(34, 10, 90, 41);
  114.         drinkPanel.add(cokePanel);
  115.         cokePanel.setLayout(null);
  116.        
  117.         JLabel lblCoke = new JLabel("Coke");
  118.         lblCoke.setFont(new Font("Tahoma", Font.BOLD, 13));
  119.         lblCoke.setBounds(30, 10, 38, 21);
  120.         cokePanel.add(lblCoke);
  121.        
  122.         JPanel pepsiPanel = new JPanel();
  123.         pepsiPanel.setLayout(null);
  124.         pepsiPanel.setBounds(34, 68, 90, 41);
  125.         drinkPanel.add(pepsiPanel);
  126.        
  127.         JLabel lblPepsi = new JLabel("Pepsi");
  128.         lblPepsi.setFont(new Font("Tahoma", Font.BOLD, 13));
  129.         lblPepsi.setBounds(30, 10, 38, 21);
  130.         pepsiPanel.add(lblPepsi);
  131.        
  132.         JPanel spritePanel = new JPanel();
  133.         spritePanel.setLayout(null);
  134.         spritePanel.setBounds(34, 127, 90, 41);
  135.         drinkPanel.add(spritePanel);
  136.        
  137.         JLabel lblSprite = new JLabel("Sprite");
  138.         lblSprite.setFont(new Font("Tahoma", Font.BOLD, 13));
  139.         lblSprite.setBounds(25, 10, 39, 21);
  140.         spritePanel.add(lblSprite);
  141.        
  142.         JPanel hundredplusPanel = new JPanel();
  143.         hundredplusPanel.setLayout(null);
  144.         hundredplusPanel.setBounds(392, 10, 90, 41);
  145.         drinkPanel.add(hundredplusPanel);
  146.        
  147.         JLabel lblRootBeer = new JLabel("Root Beer");
  148.         lblRootBeer.setFont(new Font("Tahoma", Font.BOLD, 13));
  149.         lblRootBeer.setBounds(10, 10, 70, 21);
  150.         hundredplusPanel.add(lblRootBeer);
  151.        
  152.         JPanel waterPanel = new JPanel();
  153.         waterPanel.setLayout(null);
  154.         waterPanel.setBounds(392, 68, 90, 41);
  155.         drinkPanel.add(waterPanel);
  156.        
  157.         JLabel lblWater = new JLabel("Mineral Water");
  158.         lblWater.setFont(new Font("Tahoma", Font.BOLD, 12));
  159.         lblWater.setBounds(0, 10, 90, 21);
  160.         waterPanel.add(lblWater);
  161.        
  162.         JPanel kickapooPanel = new JPanel();
  163.         kickapooPanel.setLayout(null);
  164.         kickapooPanel.setBounds(392, 127, 90, 41);
  165.         drinkPanel.add(kickapooPanel);
  166.        
  167.         JLabel lblKickapoo = new JLabel("Kickapoo");
  168.         lblKickapoo.setFont(new Font("Tahoma", Font.BOLD, 13));
  169.         lblKickapoo.setBounds(20, 10, 60, 21);
  170.         kickapooPanel.add(lblKickapoo);
  171.        
  172.         JPanel panel_1 = new JPanel();
  173.         panel_1.setBackground(Color.LIGHT_GRAY);
  174.         panel_1.setBounds(29, 282, 635, 205);
  175.         contentPane.add(panel_1);
  176.         panel_1.setLayout(null);
  177.        
  178.         JPanel moneyPanel = new JPanel();
  179.         moneyPanel.setBackground(Color.WHITE);
  180.         moneyPanel.setBounds(10, 10, 308, 185);
  181.         panel_1.add(moneyPanel);
  182.         moneyPanel.setLayout(null);
  183.        
  184.         JLabel lblNewLabel_1 = new JLabel("Please select your drink :");
  185.         lblNewLabel_1.setBounds(10, 10, 161, 13);
  186.         moneyPanel.add(lblNewLabel_1);
  187.         lblNewLabel_1.setFont(new Font("Tahoma", Font.BOLD, 12));
  188.        
  189.         JLabel lblNewLabel_2 = new JLabel("Your input");
  190.         lblNewLabel_2.setBounds(111, 79, 68, 22);
  191.         moneyPanel.add(lblNewLabel_2);
  192.         lblNewLabel_2.setFont(new Font("Tahoma", Font.BOLD, 12));
  193.        
  194.         totalPrice = new JTextField();
  195.         totalPrice.setBounds(97, 148, 103, 31);
  196.         moneyPanel.add(totalPrice);
  197.         totalPrice.setColumns(10);
  198.        
  199.         JList list = new JList();
  200.         list.setBounds(93, 73, -39, -21);
  201.         moneyPanel.add(list);
  202.        
  203.        
  204.         //set up combo box
  205.         JComboBox drinkcomboBox = new JComboBox();
  206.         drinkcomboBox.addItem("Drinks");
  207.         drinkcomboBox.addItem("Coke");
  208.         drinkcomboBox.addItem("Pepsi");
  209.         drinkcomboBox.addItem("Sprite");
  210.         drinkcomboBox.addItem("Root Beer");
  211.         drinkcomboBox.addItem("Mineral Water");
  212.         drinkcomboBox.addItem("Kickapoo");
  213.         moneyPanel.add(drinkcomboBox);
  214.         drinkcomboBox.setBounds(10, 28, 125, 21);
  215.         moneyPanel.add(drinkcomboBox);
  216.        
  217.        
  218.         Object select1 = drinkcomboBox.getSelectedItem();
  219.         if(e.getSource() == drinkcomboBox) {
  220.             if (select1 == "") {
  221.                 totalPrice.setText("Drinks");
  222.             } else if (select1.equals("Coke") {
  223.                 totalPrice.setText("RM"+Coke);
  224.             } else if (select1 == "Pepsi") {
  225.                 totalPrice.setText("RM"+Pepsi);
  226.             } else if (select1 == "Sprite") {
  227.                 totalPrice.setText("RM"+Sprite);
  228.             } else if (select1 == " Root Beer") {
  229.                 totalPrice.setText("RM"+RootBeer);
  230.             } else if (select1 == "Mineral Water") {
  231.                 totalPrice.setText("RM"+MineralWater);
  232.             } else if (select1 == "Kickapoo") {
  233.                 totalPrice.setText("RM"+Kickapoo);
  234.             }
  235.         } else if(e.getSource() == btnEnter) {
  236.             if (select1 == "Coke") {
  237.             totalPrice.setText("RM"+Coke);
  238.                 if(totalCoin>=Coke) {
  239.                     balance=totalCoin-Coke;
  240.                     drinkPrice.setText("Change= RM"+balance);
  241.                     totalCoin=0;
  242.                     drinkPrice.setText("Total Coin= RM"+ totalCoin);
  243.                     thanksLabel.setText( "Thank You!" );
  244.                     balanceField.setText("Balance= RM"+balance);
  245.                 }else if(totalCoin>=Pepsi) {
  246.                     balance=totalCoin-Pepsi;
  247.                     drinkPrice.setText("Change= RM"+balance);
  248.                     totalCoin=0;
  249.                     drinkPrice.setText("Total Coin= RM"+ totalCoin);
  250.                     thanksLabel.setText( "Thank You!" );
  251.                     balanceField.setText("Balance= RM"+balance);
  252.                 }else if(totalCoin>=Sprite) {
  253.                     balance=totalCoin-Sprite;
  254.                     drinkPrice.setText("Change= RM"+balance);
  255.                     totalCoin=0;
  256.                     drinkPrice.setText("Total Coin= RM"+ totalCoin);
  257.                     thanksLabel.setText( "Thank You!" );
  258.                     balanceField.setText("Balance= RM"+balance);
  259.                 }else if(totalCoin>=RootBeer) {
  260.                     balance=totalCoin-RootBeer;
  261.                     drinkPrice.setText("Change= RM"+balance);
  262.                     totalCoin=0;
  263.                     drinkPrice.setText("Total Coin= RM"+ totalCoin);
  264.                     thanksLabel.setText( "Thank You!" );
  265.                     balanceField.setText("Balance= RM"+balance);
  266.                 }else if(totalCoin>=MineralWater) {
  267.                     balance=totalCoin-MineralWater;
  268.                     drinkPrice.setText("Change= RM"+balance);
  269.                     totalCoin=0;
  270.                     drinkPrice.setText("Total Coin= RM"+ totalCoin);
  271.                     thanksLabel.setText( "Thank You!" );
  272.                     balanceField.setText("Balance= RM"+balance);
  273.                 }else if(totalCoin>=Kickapoo) {
  274.                     balance=totalCoin-Kickapoo;
  275.                     drinkPrice.setText("Change= RM"+balance);
  276.                     totalCoin=0;
  277.                     drinkPrice.setText("Total Coin= RM"+ totalCoin);
  278.                     thanksLabel.setText( "Thank You!" );
  279.                     balanceField.setText("Balance= RM"+balance);
  280.                 }
  281.             }
  282.                            
  283.        
  284.        
  285.         JButton btnRM1 = new JButton("RM 1");
  286.         btnRM1.addActionListener(new ActionListener() {
  287.  
  288.             public void actionPerformed(ActionEvent e) {
  289.                
  290.                 if(e.getSource() == btnRM1) {
  291.                     totalCoin+=1.00;
  292.                     inputField.setText("RM "+ totalCoin);
  293.                 }
  294.                 else if(e.getSource() == btn50cent) {
  295.                     totalCoin+=0.50;
  296.                     inputField.setText("RM"+ totalCoin);
  297.                 }
  298.             }
  299.            
  300.         });
  301.         btnRM1.setBounds(174, 111, 74, 27);
  302.         moneyPanel.add(btnRM1);
  303.         btnRM1.setBackground(SystemColor.activeCaption);
  304.         btnRM1.setFont(new Font("Tahoma", Font.BOLD, 10));
  305.        
  306.         JButton btn50cent = new JButton("50 cent");
  307.         btn50cent.addActionListener(new ActionListener() {
  308.             public void actionPerformed(ActionEvent e) {
  309.                
  310.                 if(e.getSource() == btn50cent) {
  311.                     totalCoin+=0.50;
  312.                     inputField.setText("RM "+ totalCoin);
  313.                 }
  314.                 else if(e.getSource() == btnRM1) {
  315.                     totalCoin+=1.00;
  316.                     inputField.setText("RM"+ totalCoin);
  317.                 }
  318.             }
  319.        
  320.         });
  321.        
  322.         btn50cent.setBounds(61, 111, 74, 27);
  323.         moneyPanel.add(btn50cent);
  324.         btn50cent.setBackground(SystemColor.activeCaption);
  325.         btn50cent.setFont(new Font("Tahoma", Font.BOLD, 10));
  326.        
  327.         drinkPrice = new JTextField();
  328.         drinkPrice.setBounds(181, 24, 109, 30);
  329.         moneyPanel.add(drinkPrice);
  330.         drinkPrice.setColumns(10);
  331.        
  332.         JButton btnEnter = new JButton("Enter");
  333.         btnEnter.setFont(new Font("Tahoma", Font.BOLD, 12));
  334.         btnEnter.setBackground(SystemColor.activeCaption);
  335.         btnEnter.setBounds(10, 149, 74, 27);
  336.         moneyPanel.add(btnEnter);
  337.        
  338.         inputField = new JTextField();
  339.         inputField.setBounds(181, 73, 109, 28);
  340.         moneyPanel.add(inputField);
  341.         inputField.setColumns(10);
  342.        
  343.         JPanel itemPanel = new JPanel();
  344.         itemPanel.setBackground(Color.WHITE);
  345.         itemPanel.setBounds(419, 10, 206, 185);
  346.         panel_1.add(itemPanel);
  347.         itemPanel.setLayout(null);
  348.        
  349.         JLabel lblNewLabel_3 = new JLabel("Output");
  350.         lblNewLabel_3.setFont(new Font("Tahoma", Font.BOLD, 12));
  351.         lblNewLabel_3.setBounds(77, 10, 47, 13);
  352.         itemPanel.add(lblNewLabel_3);
  353.        
  354.         balanceField = new JTextField();
  355.         balanceField.setColumns(10);
  356.         balanceField.setBounds(94, 144, 102, 31);
  357.         itemPanel.add(balanceField);
  358.        
  359.         JLabel lblYourBalance = new JLabel("Your balance");
  360.         lblYourBalance.setFont(new Font("Tahoma", Font.BOLD, 12));
  361.         lblYourBalance.setBounds(6, 147, 78, 22);
  362.         itemPanel.add(lblYourBalance);
  363.        
  364.         JLabel thanksLabel = new JLabel("");
  365.         thanksLabel.setFont(new Font("Tahoma", Font.BOLD, 12));
  366.         thanksLabel.setBounds(35, 39, 145, 22);
  367.         itemPanel.add(thanksLabel);
  368.        
  369.         JLabel lblNewLabel_5 = new JLabel("We serve it, you deserve it.");
  370.         lblNewLabel_5.setFont(new Font("Sitka Display", Font.ITALIC, 14));
  371.         lblNewLabel_5.setBounds(347, 43, 156, 13);
  372.         contentPane.add(lblNewLabel_5);
  373.        
  374.         JButton btnAdmin = new JButton("ADMIN");
  375.         btnAdmin.addActionListener(new ActionListener() {
  376.             public void actionPerformed(ActionEvent e) {
  377.                 new login().setVisible(true);
  378.             }
  379.  
  380.             private void setVisible(boolean b) {
  381.                 // TODO Auto-generated method stub
  382.                
  383.             }
  384.         });
  385.         btnAdmin.setBackground(UIManager.getColor("Button.darkShadow"));
  386.         btnAdmin.setFont(new Font("Tahoma", Font.BOLD, 13));
  387.         btnAdmin.setBounds(593, 10, 85, 25);
  388.         contentPane.add(btnAdmin);
  389.     }
  390.    
  391.  }
  392. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement