-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomerOrderPage.java
More file actions
213 lines (173 loc) · 7.44 KB
/
Copy pathCustomerOrderPage.java
File metadata and controls
213 lines (173 loc) · 7.44 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
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
class CartItem {
private int productID;
private String productName;
private double price;
private int quantity;
public CartItem(int productID, String productName, double price, int quantity) {
this.productID = productID;
this.productName = productName;
this.price = price;
this.quantity = quantity;
}
public int getProductID() {
return productID;
}
public String getProductName() {
return productName;
}
public double getPrice() {
return price;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
}
public class CustomerOrderPage {
private JFrame frame;
private DefaultTableModel productTableModel;
private DefaultTableModel cartTableModel;
private List<CartItem> cartItems = new ArrayList<>();
private int customerId;
public CustomerOrderPage(int customerId) {
this.customerId = customerId;
frame = new JFrame("Wee Ken Shin Bakers - Order Page");
frame.setSize(900, 600);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setLayout(null);
JLabel titleLabel = new JLabel("Products:");
titleLabel.setBounds(30, 10, 100, 30);
frame.add(titleLabel);
productTableModel = new DefaultTableModel(new String[]{"Product ID", "Name", "Price", "Stock"}, 0);
JTable productTable = new JTable(productTableModel);
JScrollPane productScrollPane = new JScrollPane(productTable);
productScrollPane.setBounds(30, 50, 400, 300);
frame.add(productScrollPane);
JLabel cartLabel = new JLabel("Your Cart:");
cartLabel.setBounds(500, 10, 100, 30);
frame.add(cartLabel);
cartTableModel = new DefaultTableModel(new String[]{"Product Name", "Quantity", "Price"}, 0);
JTable cartTable = new JTable(cartTableModel);
JScrollPane cartScrollPane = new JScrollPane(cartTable);
cartScrollPane.setBounds(500, 50, 350, 300);
frame.add(cartScrollPane);
JLabel quantityLabel = new JLabel("Quantity:");
quantityLabel.setBounds(30, 370, 100, 30);
frame.add(quantityLabel);
JSpinner quantitySpinner = new JSpinner(new SpinnerNumberModel(1, 1, 100, 1));
quantitySpinner.setBounds(120, 370, 50, 30);
frame.add(quantitySpinner);
JButton addToCartButton = new JButton("Add to Cart");
addToCartButton.setBounds(200, 370, 150, 30);
frame.add(addToCartButton);
JButton removeFromCartButton = new JButton("Remove Item");
removeFromCartButton.setBounds(500, 370, 150, 30);
frame.add(removeFromCartButton);
JButton submitOrderButton = new JButton("Submit Order");
submitOrderButton.setBounds(500, 420, 150, 30);
frame.add(submitOrderButton);
// Add Order History Button
JButton orderHistoryButton = new JButton("Order History");
orderHistoryButton.setBounds(30, 500, 150, 30);
frame.add(orderHistoryButton);
// Add Logout Button
JButton logoutButton = new JButton("Logout");
logoutButton.setBounds(200, 500, 150, 30);
frame.add(logoutButton);
loadProducts();
addToCartButton.addActionListener(e -> {
int selectedRow = productTable.getSelectedRow();
if (selectedRow == -1) {
JOptionPane.showMessageDialog(frame, "Please select a product to add to the cart.");
return;
}
int productID = Integer.parseInt(productTableModel.getValueAt(selectedRow, 0).toString());
String productName = productTableModel.getValueAt(selectedRow, 1).toString();
double price = Double.parseDouble(productTableModel.getValueAt(selectedRow, 2).toString());
int quantity = (int) quantitySpinner.getValue();
addToCart(productID, productName, price, quantity);
updateCartTable();
});
removeFromCartButton.addActionListener(e -> {
int selectedRow = cartTable.getSelectedRow();
if (selectedRow == -1) {
JOptionPane.showMessageDialog(frame, "Please select an item to remove from the cart.");
return;
}
cartItems.remove(selectedRow);
updateCartTable();
});
submitOrderButton.addActionListener(e -> submitOrder());
orderHistoryButton.addActionListener(e -> {
frame.dispose();
new OrderHistoryPage(customerId);
});
logoutButton.addActionListener(e -> {
frame.dispose();
new BakerySystemGUI();
});
frame.setVisible(true);
}
private void loadProducts() {
String sql = "SELECT * FROM Products";
try (Connection conn = DatabaseConnection.getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql)) {
while (rs.next()) {
int productID = rs.getInt("productID");
String name = rs.getString("name");
double price = rs.getDouble("price");
int stock = rs.getInt("stock");
productTableModel.addRow(new Object[]{productID, name, price, stock});
}
} catch (SQLException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(frame, "Error loading products.");
}
}
private void addToCart(int productID, String productName, double price, int quantity) {
for (CartItem item : cartItems) {
if (item.getProductID() == productID) {
item.setQuantity(item.getQuantity() + quantity);
return;
}
}
cartItems.add(new CartItem(productID, productName, price, quantity));
}
private void updateCartTable() {
cartTableModel.setRowCount(0);
for (CartItem item : cartItems) {
cartTableModel.addRow(new Object[]{item.getProductName(), item.getQuantity(), item.getPrice() * item.getQuantity()});
}
}
private void submitOrder() {
if (cartItems.isEmpty()) {
JOptionPane.showMessageDialog(frame, "Your cart is empty.");
return;
}
String insertOrderSQL = "INSERT INTO Orders (customerID, productID, quantity, status) VALUES (?, ?, ?, 'Unpaid')";
try (Connection conn = DatabaseConnection.getConnection();
PreparedStatement pstmt = conn.prepareStatement(insertOrderSQL)) {
for (CartItem item : cartItems) {
pstmt.setInt(1, customerId);
pstmt.setInt(2, item.getProductID());
pstmt.setInt(3, item.getQuantity());
pstmt.addBatch();
}
pstmt.executeBatch();
JOptionPane.showMessageDialog(frame, "Order submitted successfully!");
cartItems.clear();
updateCartTable();
} catch (SQLException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(frame, "Error submitting order.");
}
}
}