-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClickListener.java
More file actions
63 lines (51 loc) · 1.88 KB
/
Copy pathClickListener.java
File metadata and controls
63 lines (51 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import java.awt.Graphics;
import java.awt.Color;
public class ClickListener implements ActionListener {
private MachineInterface machine;
private double totalSales;
public ClickListener(JButton target, MachineInterface window, double sales) {
machine = window;
}
public void actionPerformed(ActionEvent event) {
JButton pressed = (JButton) event.getSource(); //overwrites 'pressed' with the source of the click
totalSales = machine.getTotalSales(); //import arrays from machine object
JButton[] buttonarray = machine.getButtonArray();
JLabel[] labelsarray = machine.getLabelArray();
int[] stock = machine.getStockLevels();
JButton vendinfo = machine.getvendinfo();
double[] costArray = machine.getCosts();
if (pressed.equals(vendinfo)) { //if vendinfo button clicked, open VendInfo window
VendorInfo vendor = new VendorInfo(totalSales, machine);
vendor.setTitle("Vendor Information");
vendor.setBounds(0, 0, 200, 100);
vendor.setVisible(true);
}
else {
for (int i = 0; i < 9; i++) {
if (pressed == buttonarray[i]) {
if (stock[i] == 0) { //if no stock left for item selected, open error message window
ErrorMessage error = new ErrorMessage();
error.setTitle("Message");
error.setBounds(0, 0, 200, 100);
error.setVisible(true);
}
else {
stock[i] -= 1; //removes 1 from stock
labelsarray[i].setText(stock[i] + " left"); //overwrite text of label with new stock level
totalSales += costArray[i]; //adds cost of purchased item to totalsales
machine.setTotalSales(totalSales);
if (stock[i] == 0) { //if no stock left, change label colour to red
labelsarray[i].setOpaque(true);
labelsarray[i].setBackground(Color.red);
machine.setLabelArray(labelsarray);
}
}
}
}
}
}
}