-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBillManagementSystem.java
More file actions
459 lines (404 loc) · 17.7 KB
/
BillManagementSystem.java
File metadata and controls
459 lines (404 loc) · 17.7 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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableColumn;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import java.util.HashMap;
import java.util.Map;
public class BillManagementSystem extends JFrame {
private CardLayout cardLayout;
private JPanel cardsPanel;
private JPanel loginPanel;
private JPanel mainPanel;
private JPanel createAccountPanel;
private JTable itemsTable;
private JButton generateBillBtn;
private JButton cancelBtn;
private JTextPane billTextPane;
private JTextField usernameField;
private JPasswordField passwordField;
private JButton createAccountBtn;
private JTextField newUsernameField;
private JPasswordField newPasswordField;
private JTextField phoneField; // Added for phone number field
private JLabel billIdLabel; // Added for displaying bill ID
private Map<String, Integer> itemPrices;
private Connection connection;
private String username;
public BillManagementSystem() {
setTitle("Bill Management System");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(600, 400);
setLocationRelativeTo(null);
cardLayout = new CardLayout();
cardsPanel = new JPanel(cardLayout);
initializeLoginPanel();
initializeMainPanel();
initializeCreateAccountPanel();
cardsPanel.add(loginPanel, "login");
cardsPanel.add(mainPanel, "main");
cardsPanel.add(createAccountPanel, "createAccount");
add(cardsPanel);
setVisible(true);
// Connect to the database
String url = "jdbc:mysql://localhost:3306/BillManagementDB";
String user = "root";
String password = "password@123";
try {
connection = DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(this, "Failed to connect to the database.");
System.exit(1);
}
showLoginPanel(); // Show the login panel initially
}
private void clearSelection() {
itemsTable.clearSelection();
}
private void initializeLoginPanel() {
loginPanel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 2;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(5, 5, 5, 5);
JLabel usernameLabel = new JLabel("Username:");
usernameField = new JTextField(15);
loginPanel.add(usernameLabel, gbc);
gbc.gridx = 2;
loginPanel.add(usernameField, gbc);
JLabel passwordLabel = new JLabel("Password:");
passwordField = new JPasswordField(15);
gbc.gridx = 0;
gbc.gridy = 1;
loginPanel.add(passwordLabel, gbc);
gbc.gridx = 2;
loginPanel.add(passwordField, gbc);
JLabel phoneLabel = new JLabel("Phone:");
phoneField = new JTextField(15);
gbc.gridx = 0;
gbc.gridy = 2;
loginPanel.add(phoneLabel, gbc);
gbc.gridx = 2;
loginPanel.add(phoneField, gbc); // Adding phone field to the login panel
gbc.gridx = 1;
gbc.gridy = 4;
gbc.gridwidth = 1;
JButton loginButton = new JButton("Login");
loginPanel.add(loginButton, gbc);
JButton createAccountButton = new JButton("Create Account");
createAccountButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
cardLayout.show(cardsPanel, "createAccount");
}
});
gbc.gridx = 2;
loginPanel.add(createAccountButton, gbc);
loginButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String username = usernameField.getText();
String password = new String(passwordField.getPassword());
if (authenticate(username, password)) {
BillManagementSystem.this.username = username;
cardLayout.show(cardsPanel, "main");
} else {
JOptionPane.showMessageDialog(BillManagementSystem.this, "Invalid username or password!");
}
}
});
}
private void initializeCreateAccountPanel() {
createAccountPanel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 2;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(5, 5, 5, 5);
JLabel newUsernameLabel = new JLabel("New Username:");
newUsernameField = new JTextField(15);
createAccountPanel.add(newUsernameLabel, gbc);
gbc.gridx = 2;
createAccountPanel.add(newUsernameField, gbc);
JLabel newPasswordLabel = new JLabel("New Password:");
newPasswordField = new JPasswordField(15);
gbc.gridx = 0;
gbc.gridy = 1;
createAccountPanel.add(newPasswordLabel, gbc);
gbc.gridx = 2;
createAccountPanel.add(newPasswordField, gbc);
JLabel phoneLabel = new JLabel("Phone:");
phoneField = new JTextField(15);
gbc.gridx = 0;
gbc.gridy = 2;
createAccountPanel.add(phoneLabel, gbc);
gbc.gridx = 2;
createAccountPanel.add(phoneField, gbc); // Adding phone field to the create account panel
gbc.gridx = 1;
gbc.gridy = 3;
gbc.gridwidth = 1;
createAccountBtn = new JButton("Create Account");
createAccountPanel.add(createAccountBtn, gbc);
createAccountBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String newUsername = newUsernameField.getText();
String newPassword = new String(newPasswordField.getPassword());
String phone = phoneField.getText(); // Retrieving phone number
if (createAccount(newUsername, newPassword, phone)) {
JOptionPane.showMessageDialog(BillManagementSystem.this, "Account created successfully!");
cardLayout.show(cardsPanel, "login");
} else {
JOptionPane.showMessageDialog(BillManagementSystem.this, "Failed to create account. Please try again.");
}
}
});
}
private boolean createAccount(String username, String password, String phone) {
// Create a new account in the database
String sql = "INSERT INTO Users (username, password, phone_no) VALUES (?, ?, ?)";
try (PreparedStatement stmt = connection.prepareStatement(sql)) {
stmt.setString(1, username);
stmt.setString(2, password);
stmt.setString(3, phone); // Setting phone number in the prepared statement
int rowsInserted = stmt.executeUpdate();
return rowsInserted > 0;
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
private boolean authenticate(String username, String password) {
// Authenticate user against database
String sql = "SELECT * FROM Users WHERE username = ? AND password = ?";
try (PreparedStatement stmt = connection.prepareStatement(sql)) {
stmt.setString(1, username);
stmt.setString(2, password);
ResultSet rs = stmt.executeQuery();
return rs.next(); // If there is a result, authentication successful
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
private void initializeMainPanel() {
mainPanel = new JPanel(new BorderLayout());
initializeItems();
JPanel buttonsPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
generateBillBtn = new JButton("Generate Bill");
generateBillBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
generateBill();
JOptionPane.showMessageDialog(BillManagementSystem.this, "Thank you for your order!");
}
});
cancelBtn = new JButton("Cancel");
cancelBtn.setPreferredSize(new Dimension(80, 30));
cancelBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
clearSelection();
billTextPane.setText("");
}
});
buttonsPanel.add(generateBillBtn);
buttonsPanel.add(cancelBtn);
JScrollPane tableScrollPane = new JScrollPane(itemsTable);
JPanel centerPanel = new JPanel(new BorderLayout());
centerPanel.add(tableScrollPane, BorderLayout.CENTER);
billTextPane = new JTextPane();
billTextPane.setEditable(false);
mainPanel.add(centerPanel, BorderLayout.CENTER);
mainPanel.add(billTextPane, BorderLayout.SOUTH);
mainPanel.add(buttonsPanel, BorderLayout.PAGE_END);
}
private void initializeItems() {
String[] columnNames = {"Item", "Price (₹)", "Quantity"};
DefaultTableModel model = new DefaultTableModel(columnNames, 0) {
@Override
public boolean isCellEditable(int row, int column) {
return column == 2; // Allow only the quantity column to be editable
}
};
itemsTable = new JTable(model);
TableColumn quantityColumn = itemsTable.getColumnModel().getColumn(2);
quantityColumn.setCellEditor(new SpinnerEditor());
itemPrices = new HashMap<>();
itemPrices.put("Apple", 100);
itemPrices.put("Banana", 40);
itemPrices.put("Orange", 60);
itemPrices.put("Potato", 30);
itemPrices.put("Onion", 50);
itemPrices.put("Tomato", 20);
itemPrices.put("Cucumber", 25);
itemPrices.put("Carrot", 35);
itemPrices.put("Cabbage", 30);
itemPrices.put("Spinach", 15);
itemPrices.put("Capsicum", 45);
itemPrices.put("Lemon", 10);
itemPrices.put("Ginger", 20);
itemPrices.put("Garlic", 30);
itemPrices.put("Coriander", 5);
itemPrices.put("Mint", 5);
itemPrices.put("Rice", 80);
itemPrices.put("Wheat Flour", 40);
itemPrices.put("Sugar", 50);
itemPrices.put("Salt", 10);
for (Map.Entry<String, Integer> entry : itemPrices.entrySet()) {
model.addRow(new Object[]{entry.getKey(), entry.getValue(), 0});
}
}
private void generateBill() {
int totalAmount = 0;
StringBuilder orderSummary = new StringBuilder("Thank you for your order, " + username + "!\nYou ordered:\n");
DefaultTableModel model = (DefaultTableModel) itemsTable.getModel();
for (int i = 0; i < model.getRowCount(); i++) {
String itemName = (String) model.getValueAt(i, 0);
int price = (int) model.getValueAt(i, 1);
int quantity = (int) model.getValueAt(i, 2);
if (quantity > 0) {
orderSummary.append("- ").append(itemName).append(" x ").append(quantity).append(" = ₹").append(price * quantity).append("\n");
totalAmount += price * quantity;
}
}
orderSummary.append("\nTotal Bill Amount: ₹").append(totalAmount).append("\n\nThank you for your order!");
// Generate and store reference ID
String referenceID = generateReferenceID();
storeReferenceID(username, referenceID);
// Store the bill in the database
int billId = storeBill(referenceID, totalAmount);
// Show the dialog box with bill ID
JOptionPane.showMessageDialog(BillManagementSystem.this, orderSummary.toString() + "\n\nYour bill ID is: " + billId);
}
private int storeBill(String referenceID, int totalAmount) {
int billId = -1;
// Store bill in the database
String sql = "INSERT INTO bills (username, total_amount, reference_id) VALUES (?, ?, ?)";
try (PreparedStatement stmt = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {
stmt.setString(1, username);
stmt.setInt(2, totalAmount);
stmt.setString(3, referenceID);
int rowsInserted = stmt.executeUpdate();
if (rowsInserted > 0) {
ResultSet generatedKeys = stmt.getGeneratedKeys();
if (generatedKeys.next()) {
billId = generatedKeys.getInt(1);
storeItemsInBill(billId); // Store items related to this bill
System.out.println("Bill stored successfully with ID: " + billId);
} else {
System.out.println("Failed to retrieve bill ID after insertion.");
}
} else {
System.out.println("Failed to store the bill.");
}
} catch (SQLException e) {
e.printStackTrace();
}
return billId;
}
private String generateReferenceID() {
// Generate a random reference ID
return "REF" + (int) (Math.random() * 10000);
}
private void storeReferenceID(String username, String referenceID) {
// Store reference ID in the database
String sql = "INSERT INTO References (username, reference_id) VALUES (?, ?)";
try (PreparedStatement stmt = connection.prepareStatement(sql)) {
stmt.setString(1, username);
stmt.setString(2, referenceID);
int rowsUpdated = stmt.executeUpdate();
if (rowsUpdated > 0) {
System.out.println("Reference ID stored successfully.");
} else {
System.out.println("Failed to store reference ID.");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
private void storeBill(String referenceID, int totalAmount) {
// Store bill in the database
String sql = "INSERT INTO bills (username, total_amount, reference_id) VALUES (?, ?, ?)";
try (PreparedStatement stmt = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {
stmt.setString(1, username);
stmt.setInt(2, totalAmount);
stmt.setString(3, referenceID);
int rowsInserted = stmt.executeUpdate();
if (rowsInserted > 0) {
ResultSet generatedKeys = stmt.getGeneratedKeys();
if (generatedKeys.next()) {
int billId = generatedKeys.getInt(1);
storeItemsInBill(billId); // Store items related to this bill
System.out.println("Bill stored successfully with ID: " + billId);
} else {
System.out.println("Failed to retrieve bill ID after insertion.");
}
} else {
System.out.println("Failed to store the bill.");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
private void storeItemsInBill(int billId) {
DefaultTableModel model = (DefaultTableModel) itemsTable.getModel();
for (int i = 0; i < model.getRowCount(); i++) {
String itemName = (String) model.getValueAt(i, 0);
int quantity = (int) model.getValueAt(i, 2);
if (quantity > 0) {
storeItemInBill(billId, itemName, quantity);
}
}
}
private void storeItemInBill(int billId, String itemName, int quantity) {
// Store individual items and quantities in the bill_items table
String sql = "INSERT INTO bill_items (bill_id, item_name, quantity) VALUES (?, ?, ?)";
try (PreparedStatement stmt = connection.prepareStatement(sql)) {
stmt.setInt(1, billId);
stmt.setString(2, itemName);
stmt.setInt(3, quantity);
int rowsUpdated = stmt.executeUpdate();
if (rowsUpdated > 0) {
System.out.println("Item '" + itemName + "' stored successfully for bill ID " + billId);
} else {
System.out.println("Failed to store item '" + itemName + "' for bill ID " + billId);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
class SpinnerEditor extends AbstractCellEditor implements TableCellEditor {
private final JSpinner spinner;
public SpinnerEditor() {
spinner = new JSpinner(new SpinnerNumberModel(0, 0, 100, 1));
}
@Override
public Object getCellEditorValue() {
return spinner.getValue();
}
@Override
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
spinner.setValue(value);
return spinner;
}
}
private void showLoginPanel() {
cardLayout.show(cardsPanel, "login");
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new BillManagementSystem();
}
});
}
}