-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDriver.java
699 lines (554 loc) · 31.6 KB
/
Driver.java
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
package DBMS_Project_Demo;
import java.sql.*;
import java.sql.Date;
import java.util.*;
public class Driver{
public static void driverOptions(Statement statement) {
try {
Scanner scanner = new Scanner(System.in);
int option;
do {
displayDriverMenu();
option = scanner.nextInt();
switch (option) {
case 1:
displayAllDrivers(statement);
break;
case 2:
addDriver(statement, scanner);
break;
case 3:
deleteDriver(statement, scanner);
break;
case 4:
updateDriver(statement, scanner);
break;
case 5:
displayDriverByID(statement, scanner);
break;
case 6:
assignPermitToDriver2(statement, scanner);
break;
case 7:
displayAllottedPermitToDriver(statement, scanner);
break;
case 8:
getCitationsForDriver(statement, scanner);
break;
case 9:
settlesCitation(statement, scanner);
break;
case 10:
appealsCitation(statement,scanner);
break;
case 0:
System.out.println("Exiting Driver Management");
break;
default:
System.out.println("Invalid option. Please choose a valid option.");
break;
}
} while (option != 0);
} catch (SQLException e) {
e.printStackTrace();
}
}
private static void displayDriverMenu() {
System.out.println("Driver Management Menu:");
System.out.println("1) Display All Drivers");
System.out.println("2) Add Driver");
System.out.println("3) Delete Driver");
System.out.println("4) Update Driver");
System.out.println("5) Get Driver Details by ID");
System.out.println("6) Assign Permit to Driver");
System.out.println("7) Get Permit details assigned to a particular Driver");
System.out.println("8) Get Citations details assigned to a particular Driver");
System.out.println("9) Settle citations for a particular Driver");
System.out.println("10) Appeal citations for a particular Driver");
System.out.println("0) Go Back to Main Menu");
System.out.println("Select an option: ");
}
private static void displayAllDrivers(Statement statement) throws SQLException {
String query = "SELECT * FROM Driver";
ResultSet resultSet = statement.executeQuery(query);
// Display all drivers
while (resultSet.next()) {
long driverID = resultSet.getLong("driverID");
String name = resultSet.getString("name");
String status = resultSet.getString("status");
String isHandicapped = resultSet.getString("isHandicapped");
System.out.println("Driver ID: " + driverID + ", Name: " + name + ", Status: " + status + ", Handicapped: " + isHandicapped);
}
resultSet.close();
}
private static void addDriver(Statement statement, Scanner scanner) throws SQLException {
System.out.print("Enter Driver ID: ");
long driverID = scanner.nextLong();
scanner.nextLine(); // Consume the newline character
// Check if the driverID already exists in the database
if (isDriverIDExists(statement, driverID)) {
System.out.println("Driver ID already exists in the database.");
return;
}
System.out.print("Enter Driver Name: ");
String name = scanner.nextLine();
System.out.print("Enter Driver Status (E/V/S): ");
String status = scanner.next().toUpperCase();
// Validate the status (E/V/S)
if (!status.equals("E") && !status.equals("V") && !status.equals("S"))
{
System.out.println("Invalid status. Status must be E or V or S.");
//return;
}
else {
System.out.print("Is the Driver Handicapped? (Yes/No): ");
String isHandicapped = scanner.next();
String query = String.format("INSERT INTO Driver (driverID, name, status, isHandicapped) VALUES (%d, '%s', '%s', '%s')", driverID, name, status, isHandicapped);
statement.executeUpdate(query);
System.out.println("Driver added successfully.");
}
}
private static boolean isDriverIDExists(Statement statement, long driverID) throws SQLException {
String query = String.format("SELECT * FROM Driver WHERE driverID = %d", driverID);
ResultSet resultSet = statement.executeQuery(query);
boolean exists = resultSet.next();
resultSet.close();
return exists;
}
private static void deleteDriver(Statement statement, Scanner scanner) throws SQLException {
System.out.print("Enter Driver ID to delete: ");
long driverID = scanner.nextLong();
String query = String.format("DELETE FROM Driver WHERE driverID = %d", driverID);
int rowsAffected = statement.executeUpdate(query);
if (rowsAffected > 0) {
System.out.println("Driver deleted successfully.");
} else {
System.out.println("No driver found with ID: " + driverID);
}
}
private static void updateDriver(Statement statement, Scanner scanner) throws SQLException {
System.out.print("Enter Driver ID to update: ");
long driverID = scanner.nextLong();
scanner.nextLine(); // Consume the newline character
System.out.print("Enter updated Driver Name: ");
String name = scanner.nextLine();
System.out.print("Enter updated Driver Status: ");
String status = scanner.nextLine();
System.out.print("Is the Driver Handicapped? (true/false): ");
String isHandicapped = scanner.nextLine();
String query = String.format("UPDATE Driver SET name='%s', status='%s', isHandicapped='%s' WHERE driverID=%d",
name, status, isHandicapped, driverID);
int rowsAffected = statement.executeUpdate(query);
if (rowsAffected > 0) {
System.out.println("Driver updated successfully.");
} else {
System.out.println("No driver found with ID: " + driverID);
}
}
private static void displayDriverByID(Statement statement, Scanner scanner) throws SQLException {
System.out.print("Enter Driver ID: ");
long driverID = scanner.nextLong();
String query = String.format("SELECT * FROM Driver WHERE driverID = %d", driverID);
ResultSet resultSet = statement.executeQuery(query);
if (resultSet.next()) {
String name = resultSet.getString("name");
String status = resultSet.getString("status");
String isHandicapped = resultSet.getString("isHandicapped");
System.out.println("Driver ID: " + driverID + ", Name: " + name + ", Status: " + status + ", Handicapped: " + isHandicapped);
} else {
System.out.println("No driver found with ID: " + driverID);
}
resultSet.close();
}
private static int getAssignedPermitsCount(Statement statement, int driverID) throws SQLException {
String query = String.format("SELECT COUNT(*) AS assignedPermits FROM AllottedTo WHERE driverID = %d", driverID);
ResultSet resultSet = statement.executeQuery(query);
int assignedPermits = 0;
if (resultSet.next()) {
assignedPermits = resultSet.getInt("assignedPermits");
}
resultSet.close();
return assignedPermits;
}
private static void displayAllottedPermitToDriver(Statement statement, Scanner scanner) throws SQLException {
System.out.print("Enter Driver ID: ");
long driverID = scanner.nextLong();
String query = String.format("SELECT p.* FROM Permit p JOIN AllottedTo a ON p.permitID = a.permitID WHERE a.driverID = %d", driverID);
ResultSet resultSet = statement.executeQuery(query);
System.out.println("Allotted Permits to Driver ID " + driverID + ":");
while (resultSet.next()) {
int permitID = resultSet.getInt("permitID");
String spaceType = resultSet.getString("spaceType");
Date startDate = resultSet.getDate("startDate");
Date expirationDate = resultSet.getDate("expirationDate");
String permitType = resultSet.getString("permitType");
Time expirationTime = resultSet.getTime("expirationTime");
System.out.println("Permit ID: " + permitID + ", Space Type: " + spaceType + ", Start Date: " + startDate +
", Expiration Date: " + expirationDate + ", Permit Type: " + permitType + ", Expiration Time: " + expirationTime);
}
}
private static String getDriverCategory(Statement statement, int driverID) throws SQLException {
String query = String.format("SELECT status FROM Driver WHERE driverID = %d", driverID);
ResultSet resultSet = statement.executeQuery(query);
String driverCategory = "";
if (resultSet.next()) {
driverCategory = resultSet.getString("status");
}
resultSet.close();
return driverCategory;
}
private static int getMaxPermitsForCategory(String driverCategory) {
if (driverCategory.equalsIgnoreCase("student") || driverCategory.equalsIgnoreCase("visitor")) {
return 1; // Max 1 permit for student/visitor
} else if (driverCategory.equalsIgnoreCase("employee")) {
return 2; // Max 2 permits for employee
}
return 0;
}
private static boolean checkSpecialEventOrParkAndRide(Statement statement, int permitID) throws SQLException {
String query = String.format("SELECT permitType FROM Permit WHERE permitID = %d", permitID);
ResultSet resultSet = statement.executeQuery(query);
boolean isSpecialEventOrParkAndRide = false;
if (resultSet.next()) {
String permitType = resultSet.getString("permitType");
isSpecialEventOrParkAndRide = permitType.equalsIgnoreCase("special event") || permitType.equalsIgnoreCase("park & ride");
}
resultSet.close();
return isSpecialEventOrParkAndRide;
}
private static void getCitationsForDriver(Statement statement, Scanner scanner) throws SQLException {
System.out.print("Enter Driver ID: ");
long driverID = scanner.nextLong();
String query = "SELECT r.citationNum, r.licenseNum, c.citationTime, c.citationDate, c.lot, c.paymentStatus, c.category " +
"FROM RaisedTo r " +
"JOIN Has h ON r.licenseNum = h.licenseNum " +
"JOIN Citation c ON r.citationNum = c.citationNum " +
"WHERE h.driverID = " + driverID;
ResultSet resultSet = statement.executeQuery(query);
// if(resultSet.getFetchSize()==0) {
// System.out.println("No Citation Details found for this driver");
// return;
// }
System.out.println("Citations with Car Details for Driver ID " + driverID + ":");
while (resultSet.next()) {
int citationNum = resultSet.getInt("citationNum");
String licenseNum = resultSet.getString("licenseNum");
Time citationTime = resultSet.getTime("citationTime");
Date citationDate = resultSet.getDate("citationDate");
int lot = resultSet.getInt("lot");
String paymentStatus = resultSet.getString("paymentStatus");
String category = resultSet.getString("category");
System.out.println("Citation Number: " + citationNum + ", License Number: " + licenseNum +
", Citation Time: " + citationTime + ", Citation Date: " + citationDate +
", Lot: " + lot + ", Payment Status: " + paymentStatus + ", Category: " + category);
}
resultSet.close();
}
private static void appealsCitation(Statement statement, Scanner scanner) throws SQLException {
System.out.print("Enter Driver ID: ");
long driverID = scanner.nextLong();
String checkAppealQuery = "SELECT citationNum FROM Appeals WHERE driverID = " + driverID;
ResultSet appealResultSet = statement.executeQuery(checkAppealQuery);
if (appealResultSet.next()) {
int citedAppeal = appealResultSet.getInt("citationNum");
System.out.println("Driver with ID " + driverID + " has already appealed Citation: " + citedAppeal);
} else {
String query = "SELECT citationNum FROM RaisedTo r JOIN Has h ON r.licenseNum = h.licenseNum WHERE h.driverID = " + driverID;
ResultSet resultSet = statement.executeQuery(query);
int citationNum = 0;
if (resultSet.next()) {
citationNum = resultSet.getInt("citationNum");
} else {
System.out.println("No citation found for Driver ID: " + driverID);
return;
}
// Appeal Citation
String appealQuery = String.format("INSERT INTO Appeals (driverID, citationNum) VALUES (%d, %d)", driverID, citationNum);
statement.executeUpdate(appealQuery);
System.out.println("Citation " + citationNum + " appealed successfully for Driver ID: " + driverID);
}
}
private static void settlesCitation(Statement statement, Scanner scanner) throws SQLException {
System.out.print("Enter Driver ID: ");
long driverID = scanner.nextLong();
String checkSettleQuery = "SELECT citationNum FROM Settles WHERE driverID = " + driverID;
ResultSet settleResultSet = statement.executeQuery(checkSettleQuery);
if (settleResultSet.next()) {
int citedSettle = settleResultSet.getInt("citationNum");
System.out.println("Driver with ID " + driverID + " has already settled Citation: " + citedSettle);
} else {
String query = "SELECT citationNum FROM RaisedTo r JOIN Has h ON r.licenseNum = h.licenseNum WHERE h.driverID = " + driverID;
ResultSet resultSet = statement.executeQuery(query);
int citationNum = 0;
if (resultSet.next()) {
citationNum = resultSet.getInt("citationNum");
} else {
System.out.println("No citation found for Driver ID: " + driverID);
return;
}
// Settle Citation
System.out.print("Enter the Payment ID of the settlement: ");
int payID = scanner.nextInt();
String settleQuery = String.format("INSERT INTO Settles (driverID, citationNum, payID) VALUES (%d, %d, %d)", driverID, citationNum,payID);
statement.executeUpdate(settleQuery);
System.out.println("Citation " + citationNum + " settled successfully for Driver ID: " + driverID);
}
}
private static void assignPermitToDriver(Statement statement, Scanner scanner) throws SQLException
{
System.out.print("Enter Driver ID: ");
long driverID = scanner.nextLong();
String permitCheckQuery = "SELECT permitID FROM AllottedTo WHERE driverID = " + driverID;
ResultSet permitResultSet = statement.executeQuery(permitCheckQuery);
if (permitResultSet.next()) {
System.out.println("Driver with ID " + driverID + " already has a permit assigned.");
}
else {
scanner.nextLine(); // Consume the newline character
System.out.println("Valid Space Types: Regular, Handicapped, Compact Car, Electric");
System.out.print("Select Space Type: ");
String spaceType = scanner.nextLine();
System.out.println("Valid Lots: 1, 2, 3, 4");
System.out.print("Select Lot: ");
int LotId = scanner.nextInt();
scanner.nextLine();
String zoneOptions = getZoneOptions(statement, driverID);
System.out.println("As the Driver is visitor, status is V");
System.out.println("Select Zone from the below options:");
System.out.println(zoneOptions);
String zone = scanner.nextLine();
System.out.println("Select Space Number from 1, 2, 3, 4, 5");
System.out.print("Enter Space Number: ");
int spaceNum = scanner.nextInt();
// Check availability and validity of selected space details
String spaceAvailabilityCheckQuery = String.format("SELECT * FROM Space WHERE zoneID='%s' AND LotID= '%d' AND spacetype='%s' AND spaceNum=%d AND availabilityStatus='Available'", zone,LotId, spaceType, spaceNum);
ResultSet spaceAvailabilityResultSet = statement.executeQuery(spaceAvailabilityCheckQuery);
if (spaceAvailabilityResultSet.next())
{
// Insert into Permit table
System.out.println(spaceAvailabilityResultSet);
System.out.println("Enter Permit ID");
int permitID = scanner.nextInt(); // Implement your logic to generate a unique permit ID
scanner.nextLine();
System.out.println("Enter Start Date");
String startDate = scanner.nextLine();
System.out.println("Enter Expiration Date");
String expirationDate = scanner.nextLine();
//scanner.nextLine();
System.out.println("Enter ExpirationTime");
String expirationTime = scanner.nextLine();
// scanner.nextLine();
System.out.println("Enter PermitType");
String permitType = scanner.nextLine();
String insertPermitQuery = String.format("INSERT INTO Permit (permitID,spaceType,startDate,expirationDate,permitType, expirationTime,lot, zoneId) VALUES (%d, '%s', '%s', '%s','%s', '%s', %d , '%s')", permitID, spaceType, startDate, expirationDate, permitType,expirationTime,LotId,zone);
statement.executeUpdate(insertPermitQuery);
// Insert into AllottedTo table
String assignPermitQuery = String.format("INSERT INTO AllottedTo (permitID, driverID) VALUES (%d, %d)", permitID, driverID);
statement.executeUpdate(assignPermitQuery);
// Update space availability status
String updateSpaceAvailabilityQuery = String.format("UPDATE Space SET availabilityStatus='Not Available' WHERE zoneID='%s' AND spaceType='%s' AND spaceNum=%d", zone, spaceType, spaceNum);
statement.executeUpdate(updateSpaceAvailabilityQuery);
System.out.println("Permit assigned successfully to Driver ID: " + driverID);
} else
{
System.out.println("Space is not available or invalid details provided.");
}
}
}
private static String getZoneOptions(Statement statement, long driverID) throws SQLException {
String driverStatusQuery = "SELECT status FROM Driver WHERE driverID = " + driverID;
ResultSet statusResultSet = statement.executeQuery(driverStatusQuery);
if (statusResultSet.next()) {
String driverStatus = statusResultSet.getString("status");
if (driverStatus.equals("E")) {
return "Employee Zones: AE, BE, CE, DE";
} else if (driverStatus.equals("S")) {
return "Student Zones: AS, BS, CS, DS";
}
else if(driverStatus.equals('V'))
{return "V";}
}
return "No valid zones available for this driver status.";
}
private static void assignPermitToDriver1(Statement statement, Scanner scanner) throws SQLException {
System.out.print("Enter Driver ID: ");
long driverID = scanner.nextLong();
String permitCheckQuery = "SELECT permitID FROM AllottedTo WHERE driverID = " + driverID;
ResultSet permitResultSet = statement.executeQuery(permitCheckQuery);
if (permitResultSet.next()) {
System.out.println("Driver with ID " + driverID + " already has a permit assigned.");
} else {
scanner.nextLine(); // Consume the newline character
System.out.println("Valid Space Types: Regular, Handicapped, Compact Car, Electric");
System.out.print("Select Space Type: ");
String spaceType = scanner.nextLine();
System.out.println("Valid Lots: 1, 2, 3, 4");
System.out.print("Select Lot: ");
int LotId = scanner.nextInt();
scanner.nextLine();
String zoneOptions = getZoneOptions1(statement, driverID);
String zone = "";
System.out.println("As the Driver is visitor, status is V");
if (zoneOptions.equals("V")) {
zone = "V";
} else {
System.out.println("Select Zone from the below options:");
System.out.println(zoneOptions);
zone = scanner.nextLine();
}
System.out.println("Select Space Number from 1, 2, 3, 4, 5");
System.out.print("Enter Space Number: ");
int spaceNum = scanner.nextInt();
// Check availability and validity of selected space details
String spaceAvailabilityCheckQuery = String.format("SELECT * FROM Space WHERE zoneID='%s' AND LotID='%d' AND spaceType='%s' AND spaceNum=%d AND availabilityStatus='Available'", zone, LotId, spaceType, spaceNum);
ResultSet spaceAvailabilityResultSet = statement.executeQuery(spaceAvailabilityCheckQuery);
if (spaceAvailabilityResultSet.next()) {
// Insert into Permit table
System.out.println("Enter Permit ID");
int permitID = scanner.nextInt(); // Implement your logic to generate a unique permit ID
scanner.nextLine();
System.out.println("Enter Start Date");
String startDate = scanner.nextLine();
// ... Rest of the permit details input
String insertPermitQuery = String.format("INSERT INTO Permit (permitID, spaceType, startDate, lot, zoneId) VALUES (%d, '%s', '%s', %d, '%s')", permitID, spaceType, startDate, LotId, zone);
statement.executeUpdate(insertPermitQuery);
// Insert into AllottedTo table
String assignPermitQuery = String.format("INSERT INTO AllottedTo (permitID, driverID) VALUES (%d, %d)", permitID, driverID);
statement.executeUpdate(assignPermitQuery);
// Update space availability status
String updateSpaceAvailabilityQuery = String.format("UPDATE Space SET availabilityStatus='Not Available' WHERE zoneID='%s' AND spaceType='%s' AND spaceNum=%d", zone, spaceType, spaceNum);
statement.executeUpdate(updateSpaceAvailabilityQuery);
System.out.println("Permit assigned successfully to Driver ID: " + driverID);
} else {
System.out.println("Space is not available or invalid details provided.");
}
}
}
private static String getZoneOptions1(Statement statement, long driverID) throws SQLException {
String driverStatusQuery = "SELECT status FROM Driver WHERE driverID = " + driverID;
ResultSet statusResultSet = statement.executeQuery(driverStatusQuery);
if (statusResultSet.next()) {
String driverStatus = statusResultSet.getString("status");
if (driverStatus.equals("E")) {
return "Employee Zones: A, B, C, D";
} else if (driverStatus.equals("S")) {
return "Student Zones: AS, BS, CS, DS";
} else if (driverStatus.equals("V")) {
return "V";
}
}
return "No valid zones available for this driver status.";
}
private static void assignPermitToDriver2(Statement statement, Scanner scanner) throws SQLException {
System.out.print("Enter Driver ID: ");
long driverID = scanner.nextLong();
String permitCheckQuery = "SELECT permitID FROM AllottedTo WHERE driverID = " + driverID;
ResultSet permitResultSet = statement.executeQuery(permitCheckQuery);
if (permitResultSet.next()) {
String driverStatusQuery = "SELECT status FROM Driver WHERE driverID = " + driverID;
ResultSet statusResultSet = statement.executeQuery(driverStatusQuery);
if (statusResultSet.next()) {
String driverStatus = statusResultSet.getString("status");
if (driverStatus.equals("E")) {
System.out.println("Driver with ID " + driverID + " is an Employee.");
System.out.println("As the Driver is an Employee, they can have maximum upto two permits assigned.");
int count = countPermitsForDriverID(statement,driverID);
System.out.println("Do you want to continue assigning another permit? (Y/N)");
String continueInput = scanner.next().toUpperCase();
if (!continueInput.equals("Y")) {
// System.out.println("This Driver is an Employee and since this driver already has 2 Permits Assigned, More permits cannot be assigned\n");
return;}
if (count ==2) {
System.out.println("This Driver is an Employee and since this driver already has 2 Permits Assigned, More permits cannot be assigned\n");
return;
}
} else if (driverStatus.equals("S")) {
System.out.println("Driver with ID " + driverID + " is a Student.");
System.out.println("As the Driver is a Student, they can have only one permit assigned.");
return;
} else if (driverStatus.equals("V")){
System.out.println("Driver with ID " + driverID + " is a Visitor.");
System.out.println("As the Driver is a Visitor, they can have only one permit assigned.");
return;
}
}
}
scanner.nextLine(); // Consume the newline character
System.out.println("Valid Space Types: Regular, Handicapped, Compact Car, Electric");
System.out.print("Select Space Type: ");
String spaceType = scanner.nextLine();
System.out.println("Valid Lots: 1, 2, 3, 4");
System.out.print("Select Lot: ");
int LotId = scanner.nextInt();
scanner.nextLine();
String zoneOptions = getZoneOptions2(statement, driverID);
String zone = "";
//System.out.println("As the Driver is visitor, status is V");
if (zoneOptions.equals("V")) {
zone = "V";
} else {
System.out.println("Select Zone from the below options:");
System.out.println(zoneOptions);
zone = scanner.nextLine();
}
System.out.println("Select Space Number from 1, 2, 3, 4, 5");
System.out.print("Enter Space Number: ");
int spaceNum = scanner.nextInt();
// Check availability and validity of selected space details
String spaceAvailabilityCheckQuery = String.format("SELECT * FROM Space WHERE zoneID='%s' AND LotID='%d' AND spaceType='%s' AND spaceNum=%d AND availabilityStatus='Available'", zone, LotId, spaceType, spaceNum);
ResultSet spaceAvailabilityResultSet = statement.executeQuery(spaceAvailabilityCheckQuery);
if (spaceAvailabilityResultSet.next()) {
// Insert into Permit table
System.out.println("Enter Permit ID");
int permitID = scanner.nextInt(); // Implement your logic to generate a unique permit ID
scanner.nextLine();
System.out.println("Enter Start Date");
String startDate = scanner.nextLine();
System.out.println("Enter Expiration Date");
String expirationDate = scanner.nextLine();
//scanner.nextLine();
System.out.println("Enter ExpirationTime");
String expirationTime = scanner.nextLine();
// scanner.nextLine();
System.out.println("Enter PermitType");
String permitType = scanner.nextLine();
// ... Rest of the permit details input
//String insertPermitQuery = String.format("INSERT INTO Permit (permitID, spaceType, startDate,expirationDate,permitType,expirationTime, lot, zoneId) VALUES (%d, '%s', '%s','%s','%s','%s' %d, '%s')", permitID, spaceType, startDate,expirationDate,expirationTime,permitType, LotId, zone);
//statement.executeUpdate(insertPermitQuery);
String insertPermitQuery = String.format("INSERT INTO Permit (permitID,spaceType,startDate,expirationDate,permitType, expirationTime,lot, zoneId) VALUES (%d, '%s', '%s', '%s','%s', '%s', %d , '%s')", permitID, spaceType, startDate, expirationDate, permitType,expirationTime,LotId,zone);
statement.executeUpdate(insertPermitQuery);
// Insert into AllottedTo table
String assignPermitQuery = String.format("INSERT INTO AllottedTo (permitID, driverID) VALUES (%d, %d)", permitID, driverID);
statement.executeUpdate(assignPermitQuery);
// Update space availability status
String updateSpaceAvailabilityQuery = String.format("UPDATE Space SET availabilityStatus='Not Available' WHERE zoneID='%s' AND spaceType='%s' AND spaceNum=%d", zone, spaceType, spaceNum);
statement.executeUpdate(updateSpaceAvailabilityQuery);
System.out.println("Permit assigned successfully to Driver ID: " + driverID);
} else {
System.out.println("Space is not available or invalid details provided.");
}
}
private static String getZoneOptions2(Statement statement, long driverID) throws SQLException {
String driverStatusQuery = "SELECT status FROM Driver WHERE driverID = " + driverID;
ResultSet statusResultSet = statement.executeQuery(driverStatusQuery);
if (statusResultSet.next()) {
String driverStatus = statusResultSet.getString("status");
if (driverStatus.equals("E")) {
return "Employee Zones: A, B, C, D";
} else if (driverStatus.equals("S")) {
return "Student Zones: AS, BS, CS, DS";
} else if (driverStatus.equals("V")) {
return "V";
}
}
return "No valid zones available for this driver status.";
}
private static int countPermitsForDriverID(Statement statement, long driverID) throws SQLException {
String countQuery = "SELECT COUNT(*) AS permitCount FROM AllottedTo WHERE driverID = " + driverID;
ResultSet countResultSet = statement.executeQuery(countQuery);
if (countResultSet.next()) {
return countResultSet.getInt("permitCount");
}
return 0; // Return 0 if no permits found or an error occurs
}
}