-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDriver.java
More file actions
811 lines (704 loc) · 29.9 KB
/
Driver.java
File metadata and controls
811 lines (704 loc) · 29.9 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
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 Driver class represents a driver in a transportation system with associated functionalities.
*/
public class Driver {
private Connection conn;
private Properties prop;
private String userID;
private Scanner scanner;
/**
* Constructs a Driver instance with the specified user ID.
*
* @param userID The unique identifier for the driver.
*/
public Driver(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);
conn.setAutoCommit(false);
System.out.println("Connected to the database successfully.");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Displays the welcome page and handles user interaction for various functionalities.
*/
public void show_welcome_page() {
int choice = 0;
do {
System.out.println("\nWelcome, Driver!");
System.out.println("-----------------------------------------------");
System.out.println("1. Add/Update/Delete/View Driver");
System.out.println("2. Add/Update/Delete/View Vehicle");
System.out.println("3. Pay/View/Appeal Citations");
System.out.println("4. View Permit Info");
System.out.println("5. Exit");
System.out.print("Enter your choice: ");
choice = scanner.nextInt();
switch (choice) {
case 1:
driverMenu(userID);
break;
case 2:
vehicleMenu(userID);
break;
case 3:
citationsMenu(userID);
break;
case 4:
viewPermitInfo(userID);
break;
case 5:
System.out.println("Exiting. Goodbye!");
break;
default:
System.out.println("Invalid choice. Please try again.");
}
} while (choice != 5);
}
/**
* Handles the driver-specific menu options and user interactions.
*
* @param userID The user ID of the driver.
*/
private void driverMenu(String userID) {
int choice = 0;
do {
System.out.println("1. Add Driver Info");
System.out.println("2. Update Driver Info");
System.out.println("3. Delete Driver INfo");
System.out.println("4. View Driver Info");
System.out.println("5. Exit");
// Call respective methods based on user choice
choice = scanner.nextInt();
switch (choice) {
case 1:
addDriverInfo();
break;
case 2:
updateDriverInfo(userID);
break;
case 3:
deleteDriverInfo(userID);
break;
case 4:
viewDriverInfo(userID);
break;
case 5:
System.out.println("Exiting. Goodbye!");
break;
default:
System.out.println("Invalid choice. Please try again.");
}
} while (choice != 5);
}
/**
* Handles the vehicle-specific menu options and user interactions.
*
* @param userID The user ID of the driver.
*/
private void vehicleMenu(String userID) {
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 Vehicle info");
System.out.println("5. Exit");
// Call respective methods based on user choice
choice = scanner.nextInt();
switch (choice) {
case 1:
addVehicle(userID);
break;
case 2:
updateVehicle();
break;
case 3:
deleteVehicle();
break;
case 4:
viewVehicle(userID);
break;
case 5:
System.out.println("Exiting. Goodbye!");
break;
default:
System.out.println("Invalid choice. Please try again.");
}
} while (choice != 5);
}
/**
* Handles the citations-specific menu options and user interactions.
*
* @param userID The user ID of the driver.
*/
private void citationsMenu(String userID) {
int choice = 0;
do {
System.out.println("1. View Citations");
System.out.println("2. Pay Citation fee");
System.out.println("3. Appeal against a citation");
System.out.println("4. Exit");
// Call respective methods based on user choice
choice = scanner.nextInt();
switch (choice) {
case 1:
viewCitations(userID);
break;
case 2:
payCitations(userID);
break;
case 3:
appealCitations(userID);
break;
case 4:
System.out.println("Exiting. Goodbye!");
break;
default:
System.out.println("Invalid choice. Please try again.");
}
} while (choice != 4);
}
/**
* Adds driver information to the database.
*/
public void addDriverInfo() {
scanner.nextLine();
System.out.print("Enter Driver's userID: ");
String userID = scanner.nextLine();
System.out.print("Enter Driver's name: ");
String name = scanner.nextLine();
System.out.print("Enter status: ");
String status = scanner.nextLine();
System.out.print("Enter disability status: ");
String disability_status = scanner.nextLine();
System.out.print("Enter password: ");
String password = scanner.nextLine();
String sql = "INSERT INTO User (userID, name, status, password) VALUES (?, ?, ?, ?)";
try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setString(1, userID);
pstmt.setString(2, name);
pstmt.setString(3, status);
pstmt.setString(4, password);
int affectedRows = pstmt.executeUpdate();
if (affectedRows > 0) {
System.out.println("A new user has been added.");
conn.commit();
} else {
System.out.println("A user could not be added.");
conn.rollback();
}
} catch (SQLException e) {
System.out.println(e.getMessage());
try {
conn.rollback();
} catch (SQLException rollbackException) {
rollbackException.printStackTrace();
}
}
String insert_sql = "INSERT INTO Driver (userID, disabilityStatus) VALUES (?, ?)";
try (PreparedStatement pstmt = conn.prepareStatement(insert_sql)) {
pstmt.setString(1, userID);
pstmt.setString(2, disability_status);
int affectedRows = pstmt.executeUpdate();
if (affectedRows > 0) {
System.out.println("A new driver has been added.");
conn.commit();
} else {
System.out.println("A driver could not be added.");
conn.rollback();
}
} catch (SQLException e) {
System.out.println(e.getMessage());
try {
conn.rollback();
} catch (SQLException rollbackException) {
rollbackException.printStackTrace();
}
}
}
/**
* Updates driver information in the database.
*
* @param userID The user ID of the driver whose information is to be updated.
*/
public void updateDriverInfo(String userID) {
scanner.nextLine();
System.out.print("Enter new Driver Name: ");
String name = scanner.nextLine();
System.out.print("Enter new status: ");
String status = scanner.nextLine();
System.out.print("Enter new password: ");
String password = scanner.nextLine();
System.out.print("Enter new disability status: ");
String disability_status = scanner.nextLine();
String update_sql = "UPDATE User SET name = ?, status = ?, password = ? WHERE userID = ?";
try (PreparedStatement pstmt = conn.prepareStatement(update_sql)) {
pstmt.setString(1, name);
pstmt.setString(2, status);
pstmt.setString(3, password);
pstmt.setString(4, userID);
int affectedRows = pstmt.executeUpdate();
if (affectedRows > 0) {
System.out.println("User info has been updated.");
conn.commit();
} else {
System.out.println("Could not update user info. Please check if the ID is correct.");
conn.rollback();
}
} catch (SQLException e) {
System.out.println(e.getMessage());
try {
conn.rollback();
} catch (SQLException rollbackException) {
rollbackException.printStackTrace();
}
}
String sql = "UPDATE Driver SET disabilityStatus = ? WHERE userID = ?";
try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setString(1, disability_status);
pstmt.setString(2, userID);
int affectedRows = pstmt.executeUpdate();
if (affectedRows > 0) {
System.out.println("Driver info has been updated.");
conn.commit();
} else {
System.out.println("Could not update driver info. Please check if the ID is correct.");
conn.rollback();
}
} catch (SQLException e) {
System.out.println(e.getMessage());
try {
conn.rollback();
} catch (SQLException rollbackException) {
rollbackException.printStackTrace();
}
}
}
/**
* Deletes driver information from the database.
*
* @param userID The user ID of the driver whose information is to be deleted.
*/
public void deleteDriverInfo(String userID) {
String sql = "DELETE FROM Driver WHERE userID = ?";
try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setString(1, userID);
int affectedRows = pstmt.executeUpdate();
if (affectedRows > 0) {
System.out.println("Driver "+ userID +" has been deleted.");
conn.commit();
} else {
System.out.println("Could not delete driver info. Please check if the ID is correct or login correctly.");
conn.rollback();
}
} catch (SQLException e) {
System.out.println(e.getMessage());
try {
conn.rollback();
} catch (SQLException rollbackException) {
rollbackException.printStackTrace();
}
}
}
/**
* Displays driver information from the database.
*
* @param userID The user ID of the driver whose information is to be displayed.
*/
public void viewDriverInfo(String userID) {
String sql_1 = "SELECT * FROM User WHERE userID = ?";
try (PreparedStatement pstmt_1 = conn.prepareStatement(sql_1)) {
pstmt_1.setString(1, userID);
ResultSet rs_1 = pstmt_1.executeQuery();
if (rs_1.next()) {
String sql_2 = "SELECT * FROM Driver WHERE userID = ?";
try (PreparedStatement pstmt_2 = conn.prepareStatement(sql_2)) {
pstmt_2.setString(1, userID);
ResultSet rs_2 = pstmt_2.executeQuery();
if (rs_2.next()) {
System.out.println("User ID: " + rs_2.getString("useriD") +
"\nName: " + rs_1.getString("name") +
"\nStatus: " + rs_1.getString("status") +
"\nDisability Status: " + rs_2.getString("disabilityStatus") +
"\nPassword: " + rs_1.getString("password"));
} else {
System.out.println("Driver with user ID " + userID + " not found.");
}
} catch (SQLException e) {
System.out.println(e.getMessage());
}
} else {
System.out.println("User with user ID " + userID + " not found.");
}
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
/**
* Adds a new vehicle and registers it to the driver in the database.
*
* @param userID The user ID of the driver.
*/
public void addVehicle(String userID) {
scanner.nextLine();
System.out.print("Enter Car License Number: ");
String carLicenseNo = scanner.nextLine();
System.out.print("Enter Manufacturer: ");
String manufacturer = scanner.nextLine();
System.out.print("Enter Model: ");
String model = scanner.nextLine();
System.out.print("Enter Color: ");
String color = scanner.nextLine();
System.out.print("Enter Year: ");
int year = scanner.nextInt();
scanner.nextLine(); // Consume the newline
String sql = "INSERT INTO Vehicle (carLicenseNo, manufacturer, model, color, year) VALUES (?, ?, ?, ?, ?)";
try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setString(1, carLicenseNo);
pstmt.setString(2, manufacturer);
pstmt.setString(3, model);
pstmt.setString(4, color);
pstmt.setInt(5, year);
int affectedRows = pstmt.executeUpdate();
if (affectedRows > 0) {
System.out.println("A new vehicle has been added.");
conn.commit(); // Commit the transaction
} else {
System.out.println("A vehicle 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());
}
String sql_1 = "INSERT INTO registered (userID, carLicenseNo) VALUES (?, ?)";
try (PreparedStatement pstmt = conn.prepareStatement(sql_1)) {
pstmt.setString(1, userID);
pstmt.setString(2, carLicenseNo);
int affectedRows = pstmt.executeUpdate();
if (affectedRows > 0) {
System.out.println("A new vehicle has been registered.");
conn.commit(); // Commit the transaction
} else {
System.out.println("A vehicle could not be registered.");
conn.rollback(); // Rollback in case of error
}
} catch (SQLException e) {
System.out.println(e.getMessage());
try {
conn.rollback(); // Rollback in case of any error
} catch (SQLException se) {
System.out.println(se.getMessage());
}
}
}
/**
* Updates information of an existing vehicle in the database.
*/
public void updateVehicle() {
scanner.nextLine();
System.out.print("Enter Car License Number to update: ");
String carLicenseNo = scanner.nextLine();
System.out.print("Enter new Manufacturer: ");
String manufacturer = scanner.nextLine();
System.out.print("Enter new Model: ");
String model = scanner.nextLine();
System.out.print("Enter new Color: ");
String color = scanner.nextLine();
System.out.print("Enter new Year: ");
int year = scanner.nextInt();
scanner.nextLine(); // Consume the newline
String sql = "UPDATE Vehicle SET manufacturer = ?, model = ?, color = ?, year = ? WHERE carLicenseNo = ?";
try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setString(1, manufacturer);
pstmt.setString(2, model);
pstmt.setString(3, color);
pstmt.setInt(4, year);
pstmt.setString(5, carLicenseNo);
int affectedRows = pstmt.executeUpdate();
if (affectedRows > 0) {
System.out.println("Vehicle has been updated.");
conn.commit(); // Commit the transaction
} else {
System.out.println("Could not update vehicle. Please check if the Car License Number is correct.");
conn.rollback(); // Rollback in case of error
}
} catch (SQLException e) {
System.out.println(e.getMessage());
try {
conn.rollback(); // Rollback in case of any error
} catch (SQLException se) {
System.out.println(se.getMessage());
}
}
}
/**
* Deletes an existing vehicle from the database.
*/
public void deleteVehicle() {
scanner.nextLine();
System.out.print("Enter Car License Number to delete: ");
String carLicenseNo = scanner.nextLine();
String sql_1 = "DELETE FROM registered WHERE carLicenseNo = ?";
try (PreparedStatement pstmt_1 = conn.prepareStatement(sql_1)) {
pstmt_1.setString(1, carLicenseNo);
int affectedRows = pstmt_1.executeUpdate();
if (affectedRows > 0) {
System.out.println("Vehicle's registration has been removed.");
String sql_2 = "DELETE FROM Vehicle WHERE carLicenseNo = ?";
try (PreparedStatement pstmt_2 = conn.prepareStatement(sql_2)) {
pstmt_2.setString(1, carLicenseNo);
int affectedRows_2 = pstmt_2.executeUpdate();
if (affectedRows_2 > 0) {
System.out.println("Vehicle has been removed.");
conn.commit(); // Commit the transaction
} else {
System.out.println("Could not delete vehicle. Please check the Car License Number 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());
}
} else {
System.out.println("Could not de-register the vehicle. Please check the Car License Number 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());
}
}
/**
* Displays information about the vehicles registered to the driver from the database.
*
* @param userID The user ID of the driver.
*/ public void viewVehicle(String userID) {
String sql = "SELECT * FROM Vehicle as v JOIN registered as r ON v.carLicenseNo = r.carLicenseNo WHERE r.userID = ?";
try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setString(1, userID);
ResultSet rs = pstmt.executeQuery();
// Check if at least one result has been returned
if (!rs.isBeforeFirst()) {
System.out.println("No vehicles found.");
return;
}
while (rs.next()) {
System.out.println("Car License Number: " + rs.getString("carLicenseNo") +
"\nManufacturer: " + rs.getString("manufacturer") +
"\nModel: " + rs.getString("model") +
"\nColor: " + rs.getString("color") +
"\nYear: " + rs.getInt("year"));
}
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
/**
* Displays information about the permit associated with the driver from the database.
*
* @param userID The user ID of the driver.
*/
public void viewPermitInfo(String userID) {
String sql = "SELECT * FROM Permit WHERE userID = ?";
try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setString(1, userID);
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
System.out.println("User ID: " + rs.getString("userID") +
"\nPermit ID: " + rs.getString("permitID") +
"\nCar License No: " + rs.getString("carLicenseNo") +
"\nParking Lot ID: " + rs.getString("parkingLotID") +
"\nZone ID: " + rs.getString("zoneID") +
"\spaceNumber: " + rs.getString("spaceNumber") +
"\nspaceType: " + rs.getString("spaceType") +
"\nStart Date: " + rs.getString("startDate") +
"\nExp Date : " + rs.getString("expDate") +
"\nExp TIme: " + rs.getString("expTime") +
"\nPermit Type: " + rs.getString("permitType")
);
} else {
System.out.println("Permit for user ID " + userID + " does not exist.");
}
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
/**
* Displays information about citations associated with the driver's vehicles from the database.
*
* @param userID The user ID of the driver.
*/
public void viewCitations(String userID) {
String sql_1 = "SELECT * FROM registered WHERE userID = ?";
try (PreparedStatement pstmt_1 = conn.prepareStatement(sql_1)) {
pstmt_1.setString(1, userID);
ResultSet rs = pstmt_1.executeQuery();
if (!rs.isBeforeFirst()) {
System.out.println("No vehicles found for this user " + userID);
return;
}
while (rs.next()) {
String carLicenseNo = rs.getString("carLicenseNo");
String sql_2 = "SELECT * FROM Citations WHERE carLicenseNo = ?";
try (PreparedStatement pstmt_2 = conn.prepareStatement(sql_2)) {
pstmt_2.setString(1, carLicenseNo);
ResultSet rs_1 = pstmt_2.executeQuery();
if (!rs_1.isBeforeFirst()) {
System.out.println("No citations this vehicle " + carLicenseNo);
return;
}
while (rs_1.next()) {
System.out.println("Citation Number: " + rs_1.getString("citationNumber") +
"\nCar License No: " + rs_1.getString("carLicenseNo") +
"\nParking Lot ID: " + rs_1.getString("parkingLotID") +
"\nCitation Date: " + rs_1.getString("citationDate") +
"\nCitation Time: " + rs_1.getString("citationTime") +
"\nCitation Date: " + rs_1.getString("citationDate") +
"\nCategory: " + rs_1.getString("category") +
"\nFee: " + rs_1.getString("fee") +
"\nPayment Status: " + rs_1.getString("paymentStatus") +
"\nAppeal: " + rs_1.getString("appeal")
);
}
}
catch (SQLException e) {
System.out.println(e.getMessage());
}
}
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
/**
* Pays outstanding citations associated with the driver's vehicles in the database.
*
* @param userID The user ID of the driver.
*/
public void payCitations(String userID) {
scanner.nextLine();
System.out.print("Enter Vehicle's License Number: ");
String carLicenseNo = scanner.nextLine();
String sql_1 = "SELECT * FROM Citations WHERE carLicenseNo = ?";
try (PreparedStatement pstmt_1 = conn.prepareStatement(sql_1)) {
pstmt_1.setString(1, carLicenseNo);
ResultSet rs_1 = pstmt_1.executeQuery();
if (!rs_1.isBeforeFirst()) {
System.out.println("No citations against this vehicle " + carLicenseNo);
return;
}
while (rs_1.next()) {
String paymentStatus = rs_1.getString("paymentStatus");
if (paymentStatus.equals("DUE")) {
paymentStatus = "PAID";
String sql_2 = "UPDATE Citations SET paymentStatus = ? WHERE carLicenseNo = ?";
try (PreparedStatement pstmt_2 = conn.prepareStatement(sql_2)) {
pstmt_2.setString(1, paymentStatus);
pstmt_2.setString(2, carLicenseNo);
int affectedRows = pstmt_2.executeUpdate();
if (affectedRows > 0) {
System.out.println("Citation has been paid successfully.");
conn.commit(); // Commit the transaction
} else {
System.out.println("Could not pay for the citation. Please check if the Car License Number is correct.");
conn.rollback(); // Rollback in case of error
}
} catch (SQLException e) {
System.out.println(e.getMessage());
try {
conn.rollback();
} catch (SQLException rollbackException) {
rollbackException.printStackTrace();
}
}
} else {
System.out.println("Citation has already been paid previously.");
}
}
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
/**
* Initiates an appeal against a citation associated with the driver's vehicles in the database.
*
* @param userID The user ID of the driver.
*/
public void appealCitations(String userID) {
scanner.nextLine();
System.out.print("Enter Vehicle's License Number: ");
String carLicenseNo = scanner.nextLine();
String sql_1 = "SELECT * FROM Citations WHERE carLicenseNo = ?";
try (PreparedStatement pstmt_1 = conn.prepareStatement(sql_1)) {
pstmt_1.setString(1, carLicenseNo);
ResultSet rs_1 = pstmt_1.executeQuery();
if (!rs_1.isBeforeFirst()) {
System.out.println("No citations against this vehicle " + carLicenseNo);
return;
}
while (rs_1.next()) {
String appeal = rs_1.getString("appeal");
if (appeal.equals("no")) {
appeal = "yes";
String sql_2 = "UPDATE Citations SET appeal = ? WHERE carLicenseNo = ?";
try (PreparedStatement pstmt_2 = conn.prepareStatement(sql_2)) {
pstmt_2.setString(1, appeal);
pstmt_2.setString(2, carLicenseNo);
int affectedRows = pstmt_2.executeUpdate();
if (affectedRows > 0) {
System.out.println("Appeal against the citation has been made.");
conn.commit(); // Commit the transaction
} else {
System.out.println("Could not appeal against the citation. Please check if the Car License Number is correct.");
conn.rollback(); // Rollback in case of error
}
} catch (SQLException e) {
System.out.println(e.getMessage());
try {
conn.rollback();
} catch (SQLException rollbackException) {
rollbackException.printStackTrace();
}
}
} else {
System.out.println("Appeal against this citation already exists.");
}
}
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
}