-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFinalProjectGUI.java
More file actions
672 lines (519 loc) · 19.3 KB
/
FinalProjectGUI.java
File metadata and controls
672 lines (519 loc) · 19.3 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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
//in this Java file, the only functions you need to complete are the following:
//RegisterNewUser()
//LoginWithCreds()
//GetAllProducts()
//GetSalesTotal()
//SubmitOrder()
//SubmitNewProduct()
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.*;
@SuppressWarnings({ "rawtypes", "unchecked", "serial" })
public class FinalProjectGUI extends JFrame {
//global variables used by many functions
GridBagConstraints gbc;
CustomActionListener cal;
ComboBoxActionListener cbal;
String connection = "jdbc:mysql://localhost:3306/finalguiproject?";
String user = "guest";
String pass = "guest";
//constructor
public FinalProjectGUI() {
setLayout(new GridBagLayout());
gbc = new GridBagConstraints();
gbc.insets = new Insets(10,10,10,10);
cal = new CustomActionListener();
cbal = new ComboBoxActionListener();
DisplaySplashScreen();
}
//this function removes all of the controls from the content pane;
//this should not be called except by the pre-defined functions!!
private void ClearScreen()
{
this.getContentPane().removeAll();
}
//this screen is the first screen the user sees!
JLabel welcomeMessage;
JButton registerButton;
JButton loginButton;
private void DisplaySplashScreen()
{
ClearScreen();
welcomeMessage = new JLabel("Welcome to the Shoe Store application! What function do you want to do?");
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 2;
this.add(welcomeMessage, gbc);
registerButton = new JButton("Register New User");
gbc.gridwidth = 1;
gbc.gridx = 0;
gbc.gridy = 1;
registerButton.addActionListener(cal);
registerButton.setName("register");
this.add(registerButton, gbc);
loginButton = new JButton("Login with Existing Credentials");
gbc.gridx = 1;
loginButton.addActionListener(cal);
loginButton.setName("login");
this.add(loginButton, gbc);
this.validate();
this.pack();
this.repaint();
}
//this screen lets a user sign up for an account!
JLabel registerMessage;
JLabel usernameLabel;
JLabel passwordLabel;
JTextField usernameField;
JTextField passwordField;
JButton submitButton;
JTextArea errorMessageArea;
private void DisplayRegisterScreen()
{
ClearScreen();
registerMessage = new JLabel("Please enter a new username and password below.");
gbc.gridx = 0;
gbc.gridy = 0;
add(registerMessage, gbc);
usernameLabel = new JLabel("User Name:");
gbc.gridy++;
add(usernameLabel, gbc);
usernameField = new JTextField(20);
gbc.gridy++;
add(usernameField, gbc);
passwordLabel = new JLabel("Password:");
gbc.gridy++;
add(passwordLabel, gbc);
passwordField = new JTextField(20);
gbc.gridy++;
add(passwordField, gbc);
submitButton = new JButton("Submit");
submitButton.setName("newUserRegister");
submitButton.addActionListener(cal);
gbc.gridy++;
add(submitButton, gbc);
errorMessageArea = new JTextArea(10,50);
gbc.gridy++;
add(errorMessageArea, gbc);
//all 3 of these are necessary to display the new form!
this.validate();
this.pack();
this.repaint();
}
//this function re-uses some of the controls from the Register User screen.
//this lets any user signin to their account
/*
JLabel usernameLabel;
JLabel passwordLabel;
JTextField usernameField;
JTextField passwordField;
JButton submitButton;
JTextArea errorMessageArea;
*/
JLabel loginLabel;
private void DisplayLoginScreen()
{
ClearScreen();
loginLabel = new JLabel("Please enter your credentials below.");
gbc.gridx = 0;
gbc.gridy = 0;
add(loginLabel, gbc);
usernameLabel = new JLabel("User Name:");
gbc.gridy++;
add(usernameLabel, gbc);
usernameField = new JTextField(20);
gbc.gridy++;
add(usernameField, gbc);
passwordLabel = new JLabel("Password:");
gbc.gridy++;
add(passwordLabel, gbc);
passwordField = new JTextField(20);
gbc.gridy++;
add(passwordField, gbc);
submitButton = new JButton("Submit");
submitButton.setName("loginWithCreds");
submitButton.addActionListener(cal);
gbc.gridy++;
add(submitButton, gbc);
errorMessageArea = new JTextArea(10,50);
gbc.gridy++;
add(errorMessageArea, gbc);
this.validate();
this.pack();
this.repaint();
}
//this screen is shown once a user successfully logs in.
//if the user has the Role of admin (denoted by the value 1 in the table "Users"),
//then they can see buttons to manage inventory and view a sales report.
//otherwise, they can see a button to place an order.
//finally, all users will be able to see a Logout button.
JLabel selectFormLabel;
JButton orderFormButton;
JButton inventoryFormButton;
JButton salesReportButton;
JButton logoutButton;
private void DisplayFormSelectScreen()
{
ClearScreen();
selectFormLabel = new JLabel("Welcome, " + loggedInUserName + ", please select a form below.");
gbc.gridx = 0;
gbc.gridy = 0;
add(selectFormLabel, gbc);
if(loggedInUserRole == 2)
{
orderFormButton = new JButton("Place an Order");
orderFormButton.setName("placeOrder");
orderFormButton.addActionListener(cal);
gbc.gridy++;
add(orderFormButton, gbc);
}
else if(loggedInUserRole == 1)
{
inventoryFormButton = new JButton("Manage Inventory");
inventoryFormButton.setName("inventory");
inventoryFormButton.addActionListener(cal);
gbc.gridy++;
add(inventoryFormButton, gbc);
salesReportButton = new JButton("See Sales Report");
salesReportButton.setName("salesReport");
salesReportButton.addActionListener(cal);
gbc.gridy++;
add(salesReportButton, gbc);
}
logoutButton = new JButton("Log Out");
logoutButton.setName("logout");
logoutButton.addActionListener(cal);
gbc.gridy++;
add(logoutButton, gbc);
this.validate();
this.pack();
this.repaint();
}
//this screen allows a non-admin user (i.e. has a Role of "2" in the Users table)
//to order a new item.
//This screen's comboBox will be populated with data from the database!
JLabel orderLabel;
JLabel priceLabel;
JComboBox productListComboBox;
JTextField productQtyField;
JButton placeOrderButton;
JButton cancelButton;
private void DisplayOrderScreen()
{
productNames = new ArrayList<String>();
productPrices = new ArrayList<Double>();
productIDs = new ArrayList<Integer>();
GetAllProducts();
ClearScreen();
orderLabel = new JLabel("Please select the item you want to order and specify the quantity.");
gbc.gridx = 0;
gbc.gridy = 0;
add(orderLabel, gbc);
productListComboBox = new JComboBox(productNames.toArray());
gbc.gridy++;
productListComboBox.addActionListener(cbal);
add(productListComboBox, gbc);
//this might throw an error if there are no items in the arraylist.
//that means you need to complete the "GetAllProducts()" function first :)
priceLabel = new JLabel("Price: " + productPrices.get(0).toString());
gbc.gridy++;
add(priceLabel, gbc);
productQtyField = new JTextField(5);
gbc.gridy++;
add(productQtyField, gbc);
placeOrderButton = new JButton("Place Order");
placeOrderButton.setName("submitOrder");
placeOrderButton.addActionListener(cal);
gbc.gridy++;
add(placeOrderButton, gbc);
cancelButton = new JButton("Go Back");
cancelButton.setName("return");
cancelButton.addActionListener(cal);
gbc.gridy++;
add(cancelButton, gbc);
this.validate();
this.pack();
this.repaint();
}
//this screen allows an admin user (i.e. with role "1" in the Users table)
//to add new products to the Product table
JLabel productNameLabel;
JTextField productNameField;
JLabel productPriceLabel;
JTextField productPriceField;
//JButton submitButton;
JButton returnButton;
private void DisplayInventoryScreen()
{
ClearScreen();
productNameLabel = new JLabel("Product Name:");
gbc.gridx = 0;
gbc.gridy = 0;
add(productNameLabel, gbc);
productNameField = new JTextField(25);
gbc.gridy++;
add(productNameField, gbc);
productPriceLabel = new JLabel("Price:");
gbc.gridy++;
add(productPriceLabel, gbc);
productPriceField = new JTextField(10);
gbc.gridy++;
add(productPriceField, gbc);
submitButton = new JButton("Add New Product");
submitButton.setName("submitNewProduct");
submitButton.addActionListener(cal);
gbc.gridy++;
add(submitButton, gbc);
returnButton = new JButton("Go Back");
returnButton.setName("return");
returnButton.addActionListener(cal);
gbc.gridy++;
add(returnButton, gbc);
this.validate();
this.pack();
this.repaint();
}
//this screen allows an admin user (i.e. with role "1" in the Users table)
//to see a simple report of all sales logged in the database
JLabel totalSalesLabel;
//JButton returnButton;
private void DisplaySalesReportScreen()
{
ClearScreen();
GetSalesTotal();
gbc.gridx = 0;
gbc.gridy = 0;
totalSalesLabel = new JLabel("Total Sales: " + salesTotal);
add(totalSalesLabel, gbc);
returnButton = new JButton("Return");
returnButton.setName("return");
gbc.gridy++;
returnButton.addActionListener(cal);
add(returnButton, gbc);
this.validate();
this.pack();
this.repaint();
}
//custom action listener for when a combobox changes values.
//useful for displaying the price of the selected item.
class ComboBoxActionListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
JComboBox theBox = (JComboBox)e.getSource();
int selectedIndex = theBox.getSelectedIndex();
priceLabel.setText("Price: " + productPrices.get(selectedIndex).toString());
}
}
//custom action listener for all buttons in the program. dang!
class CustomActionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
JButton button = (JButton)e.getSource();
String name = button.getName();
//this button is in the splash screen
if(name.equals("login"))
DisplayLoginScreen();
//this button is in the splash screen
else if (name.equals("register"))
DisplayRegisterScreen();
//this button is in the Register New User screen
else if (name.equals("newUserRegister"))
RegisterNewUser();
//this button is in the Login screen
else if(name.equals("loginWithCreds"))
LoginWithCreds();
//this button is in the Form Select screen
else if (name.equals("placeOrder"))
DisplayOrderScreen();
//this button is in the Form Select screen
else if(name.equals("inventory"))
DisplayInventoryScreen();
//this button is in the Form Select screen
else if(name.equals("salesReport"))
DisplaySalesReportScreen();
//this button is found on the Form Select screen
else if(name.equals("logout"))
DisplaySplashScreen();
//this button is in the Order form for customers
else if(name.equals("submitOrder"))
SubmitOrder();
//this button is found on both the Order form and the Inventory form
else if(name.equals("return"))
DisplayFormSelectScreen();
//this button is found on the Inventory screen
else if(name.equals("submitNewProduct"))
SubmitNewProduct();
}
}
/////////////////////////////////////////
/////////////////////////////////////////
//Begin completing the functions below!//
//Everything else, above, do not touch!//
/////////////////////////////////////////
/////////////////////////////////////////
//this function calls a stored procedure to insert a new user into the "User" table
//Any error from the database (e.g. duplicate username) should appear in the "errorMessageArea" JTextArea.
//On a success, the function "DisplaySplashScreen()" should be called.
private void RegisterNewUser() {
String username = usernameField.getText();
String password = passwordField.getText();
String conString = connection;
if (username.isEmpty() || password.isEmpty()) {
errorMessageArea.setText("Username and password cannot be empty.");
return;
}
try (Connection conn = DriverManager.getConnection(conString, user, pass)) {
// Prepare the stored procedure call
String storedProcedure = "{CALL addNewUser(?, ?, ?)}";
try (CallableStatement stmt = conn.prepareCall(storedProcedure)) {
stmt.setString(1, username);
stmt.setString(2, password);
stmt.setInt(3, 2); // assuming default user role is 2
stmt.executeUpdate();
errorMessageArea.setText("User registration successful!");
usernameField.setText("");
passwordField.setText("");
}
} catch (SQLException e) {
errorMessageArea.setText("Error: " + e.getMessage());
}
}
//this function calls a stored procedure to determine if there exists
//a username / value pair in the database that matches the user's input.
//If there is, the below variables should be populated, then call the DisplayFormSelectScreen() function.
//If there is not, then the "errorMessageArea" (JTextArea) should display an 'error' message
int loggedInUserID;
int loggedInUserRole;
String loggedInUserName;
private void LoginWithCreds() {
String username = usernameField.getText();
String password = passwordField.getText();
String conString = connection;
if (username.isEmpty() || password.isEmpty()) {
errorMessageArea.setText("Username and password cannot be empty.");
return;
}
try (Connection conn = DriverManager.getConnection(conString, user, pass)) {
// Prepare the stored procedure call
String storedProcedure = "{CALL LoginWithCreds(?, ?)}";
try (CallableStatement stmt = conn.prepareCall(storedProcedure)) {
stmt.setString(1, username);
stmt.setString(2, password);
// Execute the query
try (ResultSet resultSet = stmt.executeQuery()) {
if (resultSet.next()) {
// User found, capture ID, username, and role
loggedInUserID = resultSet.getInt("id");
loggedInUserName = resultSet.getString("username");
loggedInUserRole = resultSet.getInt("userRole");
// Display the appropriate screen based on user role
DisplayFormSelectScreen();
// Clear the username and password fields
usernameField.setText("");
passwordField.setText("");
} else {
errorMessageArea.setText("Invalid username/password.");
}
}
}
} catch (SQLException e) {
errorMessageArea.setText("Error: " + e.getMessage());
}
}
ArrayList<String> productNames;
ArrayList<Double> productPrices;
ArrayList<Integer> productIDs;
private void GetAllProducts() {
productNames = new ArrayList<>();
productPrices = new ArrayList<>();
productIDs = new ArrayList<>();
String conString = connection;
try (Connection conn = DriverManager.getConnection(conString, user, pass)) {
String storedProcedure = "{CALL getAllProducts()}";
try (CallableStatement stmt = conn.prepareCall(storedProcedure)) {
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
int productId = rs.getInt("id");
String productName = rs.getString("prodName");
double productPrice = rs.getDouble("price");
productIDs.add(productId);
productNames.add(productName);
productPrices.add(productPrice);
}
}
} catch (SQLException e) {
e.printStackTrace();
}
}
double salesTotal = -1;
private void GetSalesTotal() {
String conString = connection;
try (Connection conn = DriverManager.getConnection(conString, user, pass)) {
String storedProcedure = "{CALL sumOfSales(?)}";
try (CallableStatement stmt = conn.prepareCall(storedProcedure)) {
stmt.registerOutParameter(1, Types.DECIMAL);
stmt.execute();
salesTotal = stmt.getDouble(1);
}
} catch (SQLException e) {
e.printStackTrace();
// Handle the exception (e.g., log the error, show an error message, etc.)
}
}
private void SubmitOrder()
{
String username = user;
String password = pass;
int selectedProductIndex = productListComboBox.getSelectedIndex();
int selectedProductID = productIDs.get(selectedProductIndex);
int quantity = Integer.parseInt(productQtyField.getText());
// double total = productPrices.get(selectedProductIndex) * quantity;
String conString = connection;
try (Connection conn = DriverManager.getConnection(conString, username, password)) {
String storedProcedure = "{CALL newSale (?, ?, ?)}";
try (CallableStatement stmt = conn.prepareCall(storedProcedure)) {
stmt.setInt(1, loggedInUserID);
stmt.setInt(2, selectedProductID);
stmt.setInt(3 , quantity);
stmt.executeUpdate();
errorMessageArea.setText("Order submitted successfully!");
DisplayFormSelectScreen();
//productQtyField.setText("");
}
} catch (SQLException e) {
// Handle any SQL exceptions
errorMessageArea.setText("Error submitting order: " + e.getMessage());
}
}
//this function calls a stored procedure to add a new product to the database
private void SubmitNewProduct() {
String productName = productNameField.getText();
String productPriceStr = productPriceField.getText();
if (productName.isEmpty() || productPriceStr.isEmpty()) {
errorMessageArea.setText("Product name and price cannot be empty.");
return;
}
try {
double productPrice = Double.parseDouble(productPriceStr);
// Call stored procedure to insert a new record into "Product" table
String conString = connection;
try (Connection conn = DriverManager.getConnection(conString, user, pass)) {
String storedProcedure = "{CALL newProduct(?, ?)}";
try (CallableStatement stmt = conn.prepareCall(storedProcedure)) {
stmt.setString(1, productName);
stmt.setDouble(2, productPrice);
stmt.executeUpdate();
errorMessageArea.setText("Product added successfully!");
}
}
} catch (NumberFormatException e) {
errorMessageArea.setText("Invalid product price. Please enter a valid number.");
} catch (SQLException e) {
errorMessageArea.setText("Error: " + e.getMessage());
}
}
}