-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdminPanel.java
More file actions
149 lines (139 loc) · 5.23 KB
/
AdminPanel.java
File metadata and controls
149 lines (139 loc) · 5.23 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package GUI;
import Amazon.DataBase;
import Amazon.Product;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Vector;
public class AdminPanel extends JDialog {
private JPanel contentPane;
private JTextField brand_add;
private JButton addBrand;
private JTextField pname;
private JTextField brand_name;
private JSpinner price;
private JButton add_product;
private JTextField uid;
private JButton makeAdminButton;
private JSpinner balance;
private JButton provideVocherCardButton;
private JTable table;
private JButton refreshButton;
private JTextField catgeory;
private JCheckBox onlineCheckBox;
private DataBase dataBase;
public AdminPanel(DataBase dataBase) {
setSize(200, 200);
final Toolkit toolkit = Toolkit.getDefaultToolkit();
final Dimension screenSize = toolkit.getScreenSize();
final int x = (screenSize.width - this.getWidth()) / 2;
final int y = (screenSize.height - this.getHeight()) / 2;
setLocation(x, y);
setContentPane(contentPane);
this.dataBase = dataBase;
setModal(true);
addBrand.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String brand = brand_add.getText();
boolean done = dataBase.add_brand(brand);
MessageBox messageBox = new MessageBox();
if (done)
messageBox.setText("Added Successfully.");
else
messageBox.setText("Failed to add!");
messageBox.exec();
}
});
add_product.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String name = pname.getText();
String brand = brand_name.getText();
int Price = (int)price.getValue();
Product product = new Product();
product.setName(name);
product.setBrand(brand);
product.setPrice(Price);
product.setCategory(catgeory.getText());
if (onlineCheckBox.isSelected())
product.setType("Online");
else
product.setType("Normal");
int done = dataBase.add_product(product);
MessageBox messageBox = new MessageBox();
if (done != -1)
messageBox.setText("Added Successfully.");
else
messageBox.setText("Failed to add!");
messageBox.exec();
}
});
makeAdminButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String UID = uid.getText();
boolean done = dataBase.make_admin(UID);
MessageBox messageBox = new MessageBox();
if (done)
messageBox.setText("Added Successfully.");
else
messageBox.setText("Failed to add!");
messageBox.exec();
}
});
provideVocherCardButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String UID = uid.getText();
int value = (int)balance.getValue();
boolean done = dataBase.set_voucher(UID, value);
MessageBox messageBox = new MessageBox();
if (done)
messageBox.setText("Added Successfully.");
else
messageBox.setText("Failed to add!");
messageBox.exec();
}
});
refreshButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
refresh();
}
});
refresh();
}
private void refresh(){
ResultSet rs = dataBase.get_suggested();
try {
table.setModel(buildTableModel(rs));
} catch (SQLException e) {
e.printStackTrace();
}
}
private static DefaultTableModel buildTableModel(ResultSet rs) throws SQLException {
ResultSetMetaData metaData = rs.getMetaData();
// names of columns
Vector<String> columnNames = new Vector<>();
int columnCount = metaData.getColumnCount();
for (int column = 1; column <= columnCount; column++) {
columnNames.add(metaData.getColumnLabel(column));
}
// data of the table
Vector<Vector<String>> data = new Vector<>();
rs.beforeFirst();
while (rs.next()) {
Vector<String> vector = new Vector<>();
for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++)
vector.addElement(rs.getString(columnIndex));
data.addElement(vector);
}
return new DefaultTableModel(data, columnNames);
}
}