-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.java
More file actions
254 lines (244 loc) · 9.65 KB
/
MainWindow.java
File metadata and controls
254 lines (244 loc) · 9.65 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package GUI;
import Amazon.DataBase;
import Amazon.Order;
import Amazon.USER;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Objects;
import java.util.Vector;
public class MainWindow extends JDialog {
private JPanel contentPane;
private JTextField username;
private JTextField name;
private JTextField search;
private JTable table;
private JScrollPane list;
private JButton searchButton;
private JTextField pid;
private JButton addToCartButton;
private JButton buyCartButton;
private JComboBox comboBox1;
private JButton showButton;
private JButton addToFavouriteButton;
private JButton adminPanelButton;
private JButton storeOwnerPanelButton;
private JComboBox snames;
private JLabel mostp;
private JLabel label;
private JCheckBox vocherCardCheckBox;
private JTextField wallet;
private JButton refreshButton;
private JButton removeFromCartButton;
private DataBase dataBase;
private String type;
public MainWindow(DataBase dataBase, String id, String Name, String type) {
this.type = type;
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);
if (Objects.equals(type, "n")){
adminPanelButton.setVisible(false);
adminPanelButton.setEnabled(false);
storeOwnerPanelButton.setEnabled(false);
storeOwnerPanelButton.setVisible(false);
mostp.setVisible(false);
label.setVisible(false);
snames.setVisible(false);
comboBox1.removeItemAt(2);
}
else if (Objects.equals(type, "s")){
adminPanelButton.setVisible(false);
adminPanelButton.setEnabled(false);
}
else if (Objects.equals(type, "a")){
storeOwnerPanelButton.setEnabled(false);
storeOwnerPanelButton.setVisible(false);
mostp.setVisible(false);
label.setVisible(false);
snames.setVisible(false);
comboBox1.removeItemAt(2);
}
username.setText(id);
name.setText(Name);
this.dataBase = dataBase;
setContentPane(contentPane);
setModal(true);
USER user = dataBase.get_user(id);
wallet.setText(String.valueOf(user.getPayment().getAmount()));
searchButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
ResultSet rs = dataBase.search(search.getText(), type.charAt(0));
table.setModel(buildTableModel(rs));
} catch (SQLException e1) {
e1.printStackTrace();
}
}
});
contentPane.registerKeyboardAction(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dataBase.close();
dispose();
}
}, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
addToCartButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
boolean done = dataBase.add_to_cart(username.getText(), pid.getText());
MessageBox messageBox = new MessageBox();
if (done)
messageBox.setText("Added Successfully.");
else
messageBox.setText("Failed to add!");
messageBox.exec();
}
});
showButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ResultSet resultSet;
if (comboBox1.getSelectedItem().equals("Cart"))
resultSet = dataBase.get_carts(username.getText(), type.charAt(0));
else if (comboBox1.getSelectedItem().equals("by Store name")){
resultSet = dataBase.get_all_in_store(snames.getSelectedItem().toString()) ;
}
else
resultSet = dataBase.get_fav(username.getText(), type.charAt(0));
try {
table.setModel(buildTableModel(resultSet));
if (comboBox1.getSelectedItem().equals("by Store name"))
max_viewed();
else
mostp.setText(null);
} catch (SQLException e1) {
e1.printStackTrace();
}
}
});
addToFavouriteButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
boolean done = dataBase.add_to_fav(username.getText(), pid.getText());
MessageBox messageBox = new MessageBox();
if (done)
messageBox.setText("Added Successfully.");
else
messageBox.setText("Failed to add!");
messageBox.exec();
}
});
buyCartButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String text = "", Type = "VCard";
MessageBox messageBox = new MessageBox();
USER user = dataBase.get_user(username.getText());
if (!vocherCardCheckBox.isSelected())
Type = "cash";
int on = dataBase.buy(username.getText(), Type);
boolean done = dataBase.make_order(String.valueOf(on), username.getText());
if (done && on > 0) {
Order order = dataBase.get_order(String.valueOf(on));
messageBox.setText("Order Number : " + String.valueOf(on) + ". Delivery Price : " + String.valueOf(order.getDeliver_price())
+ ". Product Prices : " + String.valueOf(order.getProduct_price()) +
". Total : " + String.valueOf(order.getTotal_price())
);
dataBase.delete_cart_items(user.getCart().getId());
}
else
messageBox.setText("Failed to buy!");
messageBox.exec();
}
});
adminPanelButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
AdminPanel adminPanel = new AdminPanel(dataBase);
adminPanel.pack();
adminPanel.setVisible(true);
}
});
storeOwnerPanelButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
StoreOwner storeOwner = new StoreOwner(dataBase, username.getText());
storeOwner.pack();
storeOwner.setVisible(true);
}
});
update();
refreshButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
USER user = dataBase.get_user(id);
wallet.setText(String.valueOf(user.getPayment().getAmount()));
}
});
removeFromCartButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String cid = dataBase.get_user(username.getText()).getCart().getId();
String p = pid.getText();
dataBase.delete_cart_item(cid, p);
}
});
}
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);
}
private void update(){
ResultSet resultSet = dataBase.getStores(username.getText());
try {
snames.removeAllItems();
while (resultSet.next()){
snames.addItem(resultSet.getString(1));
}
} catch (SQLException e) {
e.printStackTrace();
}
}
private void max_viewed(){
int max = 0;
int maxindex = -1;
for (int i = 0; i < table.getRowCount(); i++) {
int tmp = Integer.valueOf(table.getValueAt(i, 4).toString());
if (tmp > max){
max = tmp;
maxindex = i;
}
}
if (maxindex != -1){
mostp.setText(table.getValueAt(maxindex, 1).toString());
}
else {
mostp.setText(null);
}
}
}