Commit d9606378 authored by Tom's avatar Tom

Vending Machine Resit

parent 96e2943e
import javax.*;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class VendingMachineClass extends javax.swing.JFrame { import java.awt.*;
import java.awt.event.*;
public class VendingMachineClass extends javax.swing.JFrame implements ActionListener{
public VendingMachineClass() { public VendingMachineClass() {
...@@ -8,6 +15,12 @@ public class VendingMachineClass extends javax.swing.JFrame { ...@@ -8,6 +15,12 @@ public class VendingMachineClass extends javax.swing.JFrame {
} }
private void initComponents() { private void initComponents() {
//setting up a bunch of string variables, these are all for the items. //setting up a bunch of string variables, these are all for the items.
...@@ -28,6 +41,13 @@ public class VendingMachineClass extends javax.swing.JFrame { ...@@ -28,6 +41,13 @@ public class VendingMachineClass extends javax.swing.JFrame {
jButton2 = new javax.swing.JButton(); jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton(); jButton3 = new javax.swing.JButton();
//Here I am adding action listeners for each button, this allows me to add functionality to them.
jButton3.addActionListener(this);
jButton2.addActionListener(this);
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jComboBox1.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { chocolate , water , hot_drink, snack, sweet })); jComboBox1.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { chocolate , water , hot_drink, snack, sweet }));
...@@ -38,12 +58,14 @@ public class VendingMachineClass extends javax.swing.JFrame { ...@@ -38,12 +58,14 @@ public class VendingMachineClass extends javax.swing.JFrame {
jLabel1.setText("Selected Item"); jLabel1.setText("Selected Item");
jButton1.setText("Money Out"); jButton1.setText("Cost");
jButton2.setText("Money In"); jButton2.setText("Purchase");
jButton3.setText("Add"); jButton3.setText("Add");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout); getContentPane().setLayout(layout);
layout.setHorizontalGroup( layout.setHorizontalGroup(
...@@ -96,6 +118,26 @@ public class VendingMachineClass extends javax.swing.JFrame { ...@@ -96,6 +118,26 @@ public class VendingMachineClass extends javax.swing.JFrame {
pack(); pack();
} }
//Here I create some variables
int cost = 0;
int stock = 20;
public void actionPerformed(ActionEvent e) {
//Checks which button has been clicked
if(e.getActionCommand().equals("Add")) {
//If the add button is clicked the item will be displayed in the following text areas.
jTextField1.setText(jComboBox1.getSelectedItem().toString());
jTextArea1.setText(jComboBox1.getSelectedItem().toString());
}
if(e.getActionCommand().equals("Cost")) {
System.out.print("Test");
}
}
public static void main(String args[]) { public static void main(String args[]) {
...@@ -121,11 +163,13 @@ public class VendingMachineClass extends javax.swing.JFrame { ...@@ -121,11 +163,13 @@ public class VendingMachineClass extends javax.swing.JFrame {
java.awt.EventQueue.invokeLater(new Runnable() { java.awt.EventQueue.invokeLater(new Runnable() {
public void run() { public void run() {
new VendingMachineClass().setVisible(true); new VendingMachineClass().setVisible(true);
} }
}); });
} }
private javax.swing.JButton jButton1; private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2; private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3; private javax.swing.JButton jButton3;
...@@ -138,3 +182,4 @@ public class VendingMachineClass extends javax.swing.JFrame { ...@@ -138,3 +182,4 @@ public class VendingMachineClass extends javax.swing.JFrame {
private javax.swing.JTextField jTextField3; private javax.swing.JTextField jTextField3;
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment