-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdmin.java
More file actions
1289 lines (1131 loc) · 47.3 KB
/
Admin.java
File metadata and controls
1289 lines (1131 loc) · 47.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
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
import java.io.FileInputStream;
import java.util.Scanner;
/**
* The Admin class represents an administrator with access to manage various aspects of a parking system.
*/
public class Admin {
private Connection conn;
private Properties prop;
private String userID;
private Scanner scanner;
/**
* Constructs an Admin object with the specified user ID.
*
* @param userID The user ID of the administrator.
*/
public Admin(String userID) {
this.userID = userID;
this.scanner = new Scanner(System.in);
try {
// Load properties file
FileInputStream input = new FileInputStream("config.properties");
prop = new Properties();
prop.load(input);
// Get the property values
String url = prop.getProperty("db.url");
String user = prop.getProperty("db.user");
String password = prop.getProperty("db.password");
// Create a connection to the database
conn = DriverManager.getConnection(url, user, password);
System.out.println("Connected to the database successfully.");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Displays the welcome page and allows the administrator to choose various management options.
*/
public void show_welcome_page() {
int choice = 0;
do {
System.out.println("\nWelcome, Admin!");
System.out.println("-----------------------------------------------");
System.out.println("1. Add/Update/Delete/View Parking Lot");
System.out.println("2. Add/Update/Delete/View Zone");
System.out.println("3. Add/Update/Delete/View Space");
System.out.println("4. Add/Update/Delete/View Permit");
System.out.println("5. Add/Update/Delete/View Vehicle");
System.out.println("6. Add New Admin");
System.out.println("7. Add New Security Personnel");
System.out.println("8. Exit");
System.out.print("Enter your choice: ");
choice = scanner.nextInt();
switch (choice) {
case 1:
parkingLotMenu();
break;
case 2:
zoneMenu();
break;
case 3:
spaceMenu();
break;
case 4:
permitMenu();
break;
case 5:
vehicleMenu();
break;
case 6:
insertIntoAdmin(conn);
break;
case 7:
insertIntoSecurityPersonnel(conn);
break;
case 8:
System.out.println("Exiting. Goodbye!");
break;
default:
System.out.println("Invalid choice. Please try again.");
}
} while (choice != 8);
}
/**
* Displays the menu for managing parking lots and handles user input accordingly.
*/
private void parkingLotMenu() {
int choice = 0;
do {
System.out.println("1. Add Parking Lot");
System.out.println("2. Update Parking Lot");
System.out.println("3. Delete Parking Lot");
System.out.println("4. View all parking lots");
System.out.println("5. Exit");
choice = scanner.nextInt();
switch (choice) {
case 1:
addParkingLot();
break;
case 2:
updateParkingLot();
break;
case 3:
deleteParkingLot();
break;
case 4:
viewParkingLots();
break;
case 5:
System.out.println("Exiting. Goodbye!");
break;
default:
System.out.println("Invalid choice. Please try again.");
}
} while (choice != 5);
}
/**
* Displays the menu for managing zones and handles user input accordingly.
*/
private void zoneMenu() {
int choice = 0;
do {
System.out.println("1. Add Zone");
System.out.println("2. Update Zone info");
System.out.println("3. Delete Zone info");
System.out.println("4. View all zones");
System.out.println("5. Exit");
choice = scanner.nextInt();
switch (choice) {
case 1:
addZone();
break;
case 2:
updateZone();
break;
case 3:
deleteZone();
break;
case 4:
viewZones();
break;
case 5:
System.out.println("Exiting. Goodbye!");
break;
default:
System.out.println("Invalid choice. Please try again.");
}
} while (choice != 5);
}
/**
* Displays the menu for managing spaces and handles user input accordingly.
*/
private void spaceMenu() {
int choice = 0;
do {
System.out.println("1. Add Space");
System.out.println("2. Update Space");
System.out.println("3. Delete Space");
System.out.println("4. View all spaces");
System.out.println("5. Exit");
choice = scanner.nextInt();
switch (choice) {
case 1:
addSpace();
break;
case 2:
updateSpace();
break;
case 3:
deleteSpace();
break;
case 4:
viewSpaces();
break;
case 5:
System.out.println("Exiting. Goodbye!");
break;
default:
System.out.println("Invalid choice. Please try again.");
}
} while (choice != 5);
}
/**
* Displays the menu for managing permits and handles user input accordingly.
*/
private void permitMenu() {
int choice = 0;
do {
System.out.println("1. Add Permit");
System.out.println("2. Update Permit");
System.out.println("3. Delete Permit");
System.out.println("4. View all Permit");
System.out.println("5. Exit");
choice = scanner.nextInt();
switch (choice) {
case 1:
addPermit();
break;
case 2:
updatePermit();
break;
case 3:
deletePermit();
break;
case 4:
viewPermit();
break;
case 5:
System.out.println("Exiting. Goodbye!");
break;
default:
System.out.println("Invalid choice. Please try again.");
}
} while (choice != 5);
}
/**
* Displays the menu for managing vehicles and handles user input accordingly.
*/
private void vehicleMenu() {
int choice = 0;
do {
System.out.println("1. Add Vehicle");
System.out.println("2. Update Vehicle");
System.out.println("3. Delete Vehicle");
System.out.println("4. View all Vehicle");
System.out.println("5. Exit");
choice = scanner.nextInt();
switch (choice) {
case 1:
addVehicle();
break;
case 2:
updateVehicle();
break;
case 3:
deleteVehicle();
break;
case 4:
viewVehicle();
break;
case 5:
System.out.println("Exiting. Goodbye!");
break;
default:
System.out.println("Invalid choice. Please try again.");
}
} while (choice != 5);
}
/**
* Adds a new parking lot to the database.
*/
public void addParkingLot() {
scanner.nextLine();
System.out.print("Enter Parking Lot ID: ");
String id = scanner.nextLine();
System.out.print("Enter Parking Lot Name: ");
String name = scanner.nextLine();
System.out.print("Enter Parking Lot Address: ");
String address = scanner.nextLine();
String sql = "INSERT INTO ParkingLot (parkingLotID, name, address) VALUES (?, ?, ?)";
try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setString(1, id);
pstmt.setString(2, name);
pstmt.setString(3, address);
int affectedRows = pstmt.executeUpdate();
if (affectedRows > 0) {
System.out.println("A new parking lot has been added.");
} else {
System.out.println("A parking lot could not be added.");
}
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
/**
* Updates an existing parking lot in the database.
*/
public void updateParkingLot() {
scanner.nextLine();
System.out.print("Enter Parking Lot ID to update: ");
String id = scanner.nextLine();
System.out.print("Enter new Parking Lot Name: ");
String name = scanner.nextLine();
System.out.print("Enter new Parking Lot Address: ");
String address = scanner.nextLine();
String sql = "UPDATE ParkingLot SET name = ?, address = ? WHERE parkingLotID = ?";
try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setString(1, name);
pstmt.setString(2, address);
pstmt.setString(3, id);
int affectedRows = pstmt.executeUpdate();
if (affectedRows > 0) {
System.out.println("Parking lot has been updated.");
} else {
System.out.println("Could not update parking lot. Please check if the ID is correct.");
}
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
/**
* Deletes an existing parking lot from the database.
*/
public void deleteParkingLot() {
scanner.nextLine();
System.out.print("Enter Parking Lot ID to delete: ");
String id = scanner.nextLine();
String sql = "DELETE FROM ParkingLot WHERE parkingLotID = ?";
try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setString(1, id);
int affectedRows = pstmt.executeUpdate();
if (affectedRows > 0) {
System.out.println("Parking lot has been deleted.");
} else {
System.out.println("Could not delete parking lot. Please check if the ID is correct.");
}
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
/**
* Displays information about all parking lots in the database.
*/
public void viewParkingLots() {
String sql = "SELECT parkingLotID, name, address FROM ParkingLot";
try (PreparedStatement pstmt = conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery()) {
while (rs.next()) {
System.out.println("ID: " + rs.getString("parkingLotID") +
", Name: " + rs.getString("name") +
", Address: " + rs.getString("address"));
}
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
/**
* Adds a new zone to the database.
*/
public void addZone() {
scanner.nextLine();
System.out.print("Enter Parking Lot ID for the new zone: ");
String parkingLotID = scanner.nextLine();
System.out.print("Enter Zone ID: ");
String zoneID = scanner.nextLine();
String sql = "INSERT INTO Zone (parkingLotID, zoneID) VALUES (?, ?)";
try {
conn.setAutoCommit(false); // Start transaction
try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setString(1, parkingLotID);
pstmt.setString(2, zoneID);
int affectedRows = pstmt.executeUpdate();
if (affectedRows > 0) {
System.out.println("A new zone has been added.");
conn.commit(); // Commit transaction
} else {
System.out.println("A zone could not be added.");
conn.rollback(); // Rollback in case of error
}
}
} catch (SQLException e) {
try {
conn.rollback(); // Rollback in case of any error
} catch (SQLException se) {
System.out.println(se.getMessage());
}
System.out.println(e.getMessage());
} finally {
try {
conn.setAutoCommit(true); // Reset auto-commit to true
} catch (SQLException se) {
System.out.println(se.getMessage());
}
}
}
/**
* Updates an existing zone in the database.
*/
public void updateZone() {
scanner.nextLine();
System.out.print("Enter Parking Lot ID for the zone to update: ");
String parkingLotID = scanner.nextLine();
System.out.print("Enter Zone ID to update: ");
String zoneID = scanner.nextLine();
System.out.println("The Zone table only contains ID fields which are not typically updated.");
}
/**
* Delete an existing zone in the database.
*/
public void deleteZone() {
scanner.nextLine();
System.out.print("Enter Parking Lot ID for the zone to delete: ");
String parkingLotID = scanner.nextLine();
System.out.print("Enter Zone ID to delete: ");
String zoneID = scanner.nextLine();
String sql = "DELETE FROM Zone WHERE parkingLotID = ? AND zoneID = ?";
try {
conn.setAutoCommit(false); // Start transaction
try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setString(1, parkingLotID);
pstmt.setString(2, zoneID);
int affectedRows = pstmt.executeUpdate();
if (affectedRows > 0) {
System.out.println("Zone has been deleted.");
conn.commit(); // Commit transaction
} else {
System.out.println("Could not delete zone. Please check the IDs are correct.");
conn.rollback(); // Rollback in case of error
}
}
} catch (SQLException e) {
try {
conn.rollback(); // Rollback in case of any error
} catch (SQLException se) {
System.out.println(se.getMessage());
}
System.out.println(e.getMessage());
} finally {
try {
conn.setAutoCommit(true); // Reset auto-commit to true
} catch (SQLException se) {
System.out.println(se.getMessage());
}
}
}
/**
* Displays information about all zones in the database.
*/
public void viewZones() {
scanner.nextLine();
String sql = "SELECT parkingLotID, zoneID FROM Zone";
try (PreparedStatement pstmt = conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery()) {
while (rs.next()) {
System.out.println("Parking Lot ID: " + rs.getString("parkingLotID") +
", Zone ID: " + rs.getString("zoneID"));
}
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
/**
* Adds a new space to the database.
*/
public void addSpace() {
scanner.nextLine();
System.out.print("Enter Parking Lot ID for the new space: ");
String parkingLotID = scanner.nextLine();
System.out.print("Enter Zone ID for the new space: ");
String zoneID = scanner.nextLine();
System.out.print("Enter Space Number: ");
int spaceNumber = scanner.nextInt();
scanner.nextLine(); // Consume the newline
System.out.print("Enter Space Type (Regular/Handicap/Compact/Electric): ");
String spaceType = scanner.nextLine();
System.out.print("Enter Availability Status (Y/N): ");
String availability = scanner.nextLine();
String sql = "INSERT INTO Space (parkingLotID, zoneID, spaceNumber, spaceType, availability) VALUES (?, ?, ?, ?, ?)";
try {
conn.setAutoCommit(false); // Start transaction
try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setString(1, parkingLotID);
pstmt.setString(2, zoneID);
pstmt.setInt(3, spaceNumber);
pstmt.setString(4, spaceType);
pstmt.setString(5, availability);
int affectedRows = pstmt.executeUpdate();
if (affectedRows > 0) {
System.out.println("A new space has been added.");
conn.commit(); // Commit the transaction
} else {
System.out.println("A space could not be added.");
conn.rollback(); // Rollback in case of error
}
}
} catch (SQLException e) {
try {
conn.rollback(); // Rollback in case of any error
} catch (SQLException se) {
System.out.println(se.getMessage());
}
System.out.println(e.getMessage());
} finally {
try {
conn.setAutoCommit(true); // Reset auto-commit to true
} catch (SQLException se) {
System.out.println(se.getMessage());
}
}
}
/**
* Updates an existing space in the database.
*/
public void updateSpace() {
scanner.nextLine();
System.out.print("Enter Parking Lot ID for the space to update: ");
String parkingLotID = scanner.nextLine();
System.out.print("Enter Zone ID for the space to update: ");
String zoneID = scanner.nextLine();
System.out.print("Enter Space Number to update: ");
int spaceNumber = scanner.nextInt();
scanner.nextLine(); // Consume the newline
System.out.print("Enter new Space Type (Regular/Handicap/Compact/Electric): ");
String spaceType = scanner.nextLine();
System.out.print("Enter new Availability Status (Y/N): ");
String availability = scanner.nextLine();
String sql = "UPDATE Space SET spaceType = ?, availability = ? WHERE parkingLotID = ? AND zoneID = ? AND spaceNumber = ?";
try {
conn.setAutoCommit(false); // Start transaction
try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setString(1, spaceType);
pstmt.setString(2, availability);
pstmt.setString(3, parkingLotID);
pstmt.setString(4, zoneID);
pstmt.setInt(5, spaceNumber);
int affectedRows = pstmt.executeUpdate();
if (affectedRows > 0) {
System.out.println("Space has been updated.");
conn.commit(); // Commit the transaction
} else {
System.out.println("Could not update space. Please check if the IDs and space number are correct.");
conn.rollback(); // Rollback in case of error
}
}
} catch (SQLException e) {
try {
conn.rollback(); // Rollback in case of any error
} catch (SQLException se) {
System.out.println(se.getMessage());
}
System.out.println(e.getMessage());
} finally {
try {
conn.setAutoCommit(true); // Reset auto-commit to true
} catch (SQLException se) {
System.out.println(se.getMessage());
}
}
}
/**
* Deletes an existing space from the database.
*/
public void deleteSpace() {
scanner.nextLine();
System.out.print("Enter Parking Lot ID for the space to delete: ");
String parkingLotID = scanner.nextLine();
System.out.print("Enter Zone ID for the space to delete: ");
String zoneID = scanner.nextLine();
System.out.print("Enter Space Number to delete: ");
int spaceNumber = scanner.nextInt();
String sql = "DELETE FROM Space WHERE parkingLotID = ? AND zoneID = ? AND spaceNumber = ?";
try {
conn.setAutoCommit(false); // Start transaction
try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setString(1, parkingLotID);
pstmt.setString(2, zoneID);
pstmt.setInt(3, spaceNumber);
int affectedRows = pstmt.executeUpdate();
if (affectedRows > 0) {
System.out.println("Space has been deleted.");
conn.commit(); // Commit the transaction
} else {
System.out.println("Could not delete space. Please check the IDs and space number are correct.");
conn.rollback(); // Rollback in case of error
}
}
} catch (SQLException e) {
try {
conn.rollback(); // Rollback in case of any error
} catch (SQLException se) {
System.out.println(se.getMessage());
}
System.out.println(e.getMessage());
} finally {
try {
conn.setAutoCommit(true); // Reset auto-commit to true
} catch (SQLException se) {
System.out.println(se.getMessage());
}
}
}
/**
* Method to view all spaces in the parking lot.
*/
public void viewSpaces() {
scanner.nextLine();
String sql = "SELECT parkingLotID, zoneID, spaceNumber, spaceType, availability FROM Space";
try (PreparedStatement pstmt = conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery()) {
while (rs.next()) {
System.out.println("Parking Lot ID: " + rs.getString("parkingLotID") +
", Zone ID: " + rs.getString("zoneID") +
", Space Number: " + rs.getInt("spaceNumber") +
", Space Type: " + rs.getString("spaceType") +
", Availability: " + rs.getString("availability"));
}
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
/**
* Method to insert a new administrator into the system.
*
* @param conn The database connection.
*/
private void insertIntoAdmin(Connection conn) {
scanner.nextLine();
System.out.println("Enter userID for the new Admin: ");
String userId = scanner.nextLine();
String sql = "INSERT INTO Admin (userID) VALUES (?)";
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setString(1, userId);
System.out.println("Account created successfully.");
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* Method to insert a new security personnel into the system.
*
* @param conn The database connection.
*/
private void insertIntoSecurityPersonnel(Connection conn) {
scanner.nextLine();
System.out.println("Enter userID for the new Security Personnel: ");
String userId = scanner.nextLine();
String sql = "INSERT INTO SecurityPersonnel (userID) VALUES (?)";
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setString(1, userId);
System.out.println("Account created successfully.");
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* Method to add a permit for parking.
*/
public void addPermit() {
scanner.nextLine();
System.out.print("Enter Parking Lot ID: ");
String parkingLotID = scanner.nextLine();
System.out.print("Enter Zone ID: ");
String zoneID = scanner.nextLine();
System.out.print("Enter Space Number: ");
int spaceNumber = scanner.nextInt();
scanner.nextLine();
String checkSpaceSql = "SELECT availability FROM Space WHERE parkingLotID = ? AND zoneID = ? AND spaceNumber = ?";
try (PreparedStatement checkSpaceStmt = conn.prepareStatement(checkSpaceSql)) {
checkSpaceStmt.setString(1, parkingLotID);
checkSpaceStmt.setString(2, zoneID);
checkSpaceStmt.setInt(3, spaceNumber);
ResultSet rs = checkSpaceStmt.executeQuery();
if (rs.next()) {
String availability = rs.getString("availability");
if ("N".equals(availability)) {
System.out.println("The space is currently occupied. Cannot assign a permit.");
return;
}
} else {
System.out.println("The specified space does not exist.");
return;
}
} catch (SQLException e) {
System.out.println("Error checking space availability: " + e.getMessage());
return;
}
System.out.print("Enter User ID: ");
String userID = scanner.nextLine();
String userStatus = null;
// Check user status and zone ID compatibility
String checkUserStatusSql = "SELECT status FROM User WHERE userID = ?";
try (PreparedStatement checkUserStatusStmt = conn.prepareStatement(checkUserStatusSql)) {
checkUserStatusStmt.setString(1, userID);
ResultSet rs = checkUserStatusStmt.executeQuery();
if (rs.next()) {
userStatus = rs.getString("status");
if (("S".equals(userStatus) && !zoneID.matches("^[ABCD]S$")) ||
("E".equals(userStatus) && !zoneID.matches("^[ABCD]$")) ||
("V".equals(userStatus) && !"V".equals(zoneID))) {
System.out.println("This zone is not available for the user's status.");
return;
}
} else {
System.out.println("User status could not be determined.");
return;
}
} catch (SQLException e) {
System.out.println("Error checking user status: " + e.getMessage());
return;
}
// Check permit limits based on user status
String countPermitsSql = "SELECT COUNT(*) FROM Permit WHERE userID = ?";
try (PreparedStatement countPermitsStmt = conn.prepareStatement(countPermitsSql)) {
countPermitsStmt.setString(1, userID);
ResultSet permitCountRS = countPermitsStmt.executeQuery();
if (permitCountRS.next()) {
int permitCount = permitCountRS.getInt(1);
if (("S".equals(userStatus) || "V".equals(userStatus)) && permitCount >= 1) {
System.out.println("Students and visitors are allowed only one permit.");
return;
} else if ("E".equals(userStatus) && permitCount >= 2) {
System.out.println("Employees are allowed up to two permits.");
return;
}
}
} catch (SQLException e) {
System.out.println("Error checking permit count: " + e.getMessage());
return;
}
String checkDisabilityStatusSql = "SELECT disabilityStatus FROM Driver WHERE userID = ?";
String disabilityStatus = null;
try (PreparedStatement checkDisabilityStatusStmt = conn.prepareStatement(checkDisabilityStatusSql)) {
checkDisabilityStatusStmt.setString(1, userID);
ResultSet rs = checkDisabilityStatusStmt.executeQuery();
if (rs.next()) {
disabilityStatus = rs.getString("disabilityStatus");
}
} catch (SQLException e) {
System.out.println("Error checking disability status: " + e.getMessage());
return;
}
if (disabilityStatus == null) {
System.out.println("User's disability status could not be determined.");
return;
}
String checkSpaceSuitabilitySql = "SELECT spaceType FROM Space WHERE parkingLotID = ? AND zoneID = ? AND spaceNumber = ?";
String spaceType = null;
try (PreparedStatement checkSpaceSuitabilityStmt = conn.prepareStatement(checkSpaceSuitabilitySql)) {
checkSpaceSuitabilityStmt.setString(1, parkingLotID);
checkSpaceSuitabilityStmt.setString(2, zoneID);
checkSpaceSuitabilityStmt.setInt(3, spaceNumber);
ResultSet rs = checkSpaceSuitabilityStmt.executeQuery();
if (rs.next()) {
spaceType = rs.getString("spaceType");
if ("D".equals(disabilityStatus) && !"Handicap".equals(spaceType) ||
!"D".equals(disabilityStatus) && "Handicap".equals(spaceType)) {
System.out.println("The space is not suitable for the user's disability status.");
return;
}
} else {
System.out.println("The specified space does not exist.");
return;
}
} catch (SQLException e) {
System.out.println("Error checking space suitability: " + e.getMessage());
return;
}
System.out.print("Enter Permit ID: ");
String permitID = scanner.nextLine();
System.out.print("Enter Car License Number: ");
String carLicenseNo = scanner.nextLine();
System.out.print("Enter Start Date (YYYY-MM-DD): ");
String startDate = scanner.nextLine();
System.out.print("Enter Expiry Date (YYYY-MM-DD), leave blank if none: ");
String expDate = scanner.nextLine();
System.out.print("Enter Expiry Time (HH:MM:SS), leave blank if none: ");
String expTime = scanner.nextLine();
System.out.print("Enter Permit Type: ");
String permitType = scanner.nextLine();
String addPermitSql = "INSERT INTO Permit (permitID, carLicenseNo, parkingLotID, zoneID, spaceNumber,spaceType, startDate, expDate, expTime, userID, permitType) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
String updateSpaceSql = "UPDATE Space SET availability = 'N' WHERE parkingLotID = ? AND zoneID = ? AND spaceNumber = ?";
try {
conn.setAutoCommit(false); // Start transaction
// Insert the permit
try (PreparedStatement addPermitStmt = conn.prepareStatement(addPermitSql)) {
addPermitStmt.setString(1, permitID);
addPermitStmt.setString(2, carLicenseNo);
addPermitStmt.setString(3, parkingLotID);
addPermitStmt.setString(4, zoneID);
addPermitStmt.setInt(5, spaceNumber);
addPermitStmt.setString(6, spaceType);
addPermitStmt.setDate(7, java.sql.Date.valueOf(startDate));
if (!expDate.isEmpty()) {
addPermitStmt.setDate(8, java.sql.Date.valueOf(expDate));
} else {
addPermitStmt.setNull(8, java.sql.Types.DATE);
}
if (!expTime.isEmpty()) {
addPermitStmt.setTime(9, java.sql.Time.valueOf(expTime));
} else {
addPermitStmt.setNull(9, java.sql.Types.TIME);
}
addPermitStmt.setString(10, userID);
addPermitStmt.setString(11, permitType);
int affectedRows = addPermitStmt.executeUpdate();
if (affectedRows == 0) {
throw new SQLException("Inserting permit failed, no rows affected.");
}
}
try (PreparedStatement updateSpaceStmt = conn.prepareStatement(updateSpaceSql)) {
updateSpaceStmt.setString(1, parkingLotID);
updateSpaceStmt.setString(2, zoneID);
updateSpaceStmt.setInt(3, spaceNumber);
int affectedRows = updateSpaceStmt.executeUpdate();
if (affectedRows == 0) {
throw new SQLException("Updating space availability failed, no rows affected.");
}
}
// Commit the transaction if all operations were successful
conn.commit();
System.out.println("Permit added and space marked as occupied.");
} catch (SQLException e) {
System.out.println("Transaction failed: " + e.getMessage());
try {
conn.rollback(); // Rollback in case of any error
} catch (SQLException se) {
System.out.println("Error during rollback: " + se.getMessage());
}
} finally {
try {
conn.setAutoCommit(true); // Reset auto-commit to true
} catch (SQLException se) {
System.out.println("Error resetting auto-commit: " + se.getMessage());
}
}
}
/**
* Method to update an existing permit's details.
*/
public void updatePermit() {
scanner.nextLine();
System.out.print("Enter Permit ID to update: ");
String permitID = scanner.nextLine();
System.out.print("Enter new Expiry Date (YYYY-MM-DD), leave blank if none: ");
String expDate = scanner.nextLine();
System.out.print("Enter new Expiry Time (HH:MM:SS), leave blank if none: ");
String expTime = scanner.nextLine();
String sql = "UPDATE Permit SET expDate = ?, expTime = ? WHERE permitID = ?";
try {
conn.setAutoCommit(false); // Start transaction
try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
if (!expDate.isEmpty()) {
pstmt.setDate(1, java.sql.Date.valueOf(expDate));
} else {
pstmt.setNull(1, java.sql.Types.DATE);
}
if (!expTime.isEmpty()) {
pstmt.setTime(2, java.sql.Time.valueOf(expTime));
} else {
pstmt.setNull(2, java.sql.Types.TIME);
}
pstmt.setString(3, permitID);
int affectedRows = pstmt.executeUpdate();
if (affectedRows > 0) {
System.out.println("Permit has been updated.");
conn.commit(); // Commit the transaction
} else {
System.out.println("Could not update permit. Please check if the Permit ID is correct.");
conn.rollback(); // Rollback in case of error
}
}
} catch (SQLException e) {
try {
conn.rollback(); // Rollback in case of any error
} catch (SQLException se) {
System.out.println(se.getMessage());
}
System.out.println(e.getMessage());
} finally {
try {
conn.setAutoCommit(true); // Reset auto-commit to true
} catch (SQLException se) {
System.out.println(se.getMessage());
}
}
}
/**
* Method to delete an existing permit from the system.
*/
public void deletePermit() {
scanner.nextLine();
System.out.print("Enter Permit ID to delete: ");